This repository has been archived by the owner on Nov 4, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
link.go
135 lines (109 loc) · 2.57 KB
/
link.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package main
import (
"fmt"
"sort"
"strings"
"time"
"golang.org/x/net/context"
"google.golang.org/appengine/datastore"
"google.golang.org/appengine/log"
)
type Link struct {
Title string
Url string
Description string `datastore:",noindex"` // Markdown
Tags []string
Posted time.Time
Created time.Time
Modified time.Time
}
func NewLink(title string, url string, desc string, tags []string, when time.Time) *Link {
e := new(Link)
// User supplied content
e.Title = title
e.Url = url
e.Description = desc
e.Tags = tags
e.Posted = when
// Computer generated content
e.Created = time.Now()
e.Modified = time.Now()
return e
}
func (l *Link) TagString() string {
tags := []string{}
for _, t := range l.Tags {
tags = append(tags, fmt.Sprintf("#%s", t))
}
return strings.Join(tags, " ")
}
func (e *Link) Save(c context.Context) error {
k := datastore.NewKey(c, "Link", e.Url, 0, nil)
k2, err := datastore.Put(c, k, e)
if err == nil {
log.Debugf(c, "Wrote %+v", e)
log.Debugf(c, "Old key: %+v; New Key: %+v", k, k2)
} else {
log.Warningf(c, "Error writing link: %v", e)
}
return err
}
func LinkQuery(c context.Context, limit int, recentFirst bool) *datastore.Query {
q := datastore.NewQuery("Link")
if recentFirst {
q = q.Order("-Posted")
}
if limit > 0 {
q = q.Limit(limit)
}
return q
}
func Links(c context.Context, limit int, recentFirst bool) (*[]Link, error) {
q := LinkQuery(c, limit, recentFirst)
links := new([]Link)
_, err := q.GetAll(c, links)
return links, err
}
// Data for building a list of links by day.
type LinkDay struct {
Links []Link
Day time.Time
}
type linkDays []*LinkDay
// These three functions are needed for Sort.
func (p linkDays) Len() int { return len(p) }
func (p linkDays) Less(i, j int) bool { return p[i].Day.Before(p[j].Day) }
func (p linkDays) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
func (p linkDays) HasDate(day time.Time) int {
for i, d := range p {
if d.Day == day {
return i
}
}
return -1
}
func LinksByDay(c context.Context, days int) (*linkDays, error) {
links, err := Links(c, days*50, true)
if err != nil {
return nil, err
}
ds := make([]*LinkDay, 0)
for _, l := range *links {
date := l.Posted.Round(time.Hour * 24)
if linkDays(ds).HasDate(date) < 0 {
ds = append(ds, &LinkDay{
Day: date,
Links: []Link{l},
})
} else {
i := linkDays(ds).HasDate(date)
ds[i].Links = append(ds[i].Links, l)
}
}
lds := linkDays(ds)
sort.Reverse(lds)
if days > 0 && len(lds) > 0 {
lds = lds[0:days]
}
return &lds, nil
}