-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
post.go
438 lines (372 loc) · 9.7 KB
/
post.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
package graphql
import (
"context"
"database/sql"
"fmt"
"html/template"
"math"
"regexp"
"sort"
"strconv"
"strings"
"time"
"github.com/lib/pq"
)
// Post is our representation of a post in the database.
type Post struct {
ID string `json:"id"`
Title string `json:"title"`
Content string `json:"content"`
Datetime time.Time `json:"datetime"`
Created time.Time `json:"created"`
Modified time.Time `json:"modified"`
Draft bool `json:"draft"`
Tags []string `json:"tags"`
Links []*Link `json:"links"`
}
// GetMaxID returns the greatest post ID in the database.
func GetMaxID(ctx context.Context) (int64, error) {
row := db.QueryRowContext(ctx, "SELECT MAX(id) from posts")
var id int64
if err := row.Scan(&id); err != nil {
return -1, err
}
return id, nil
}
// GetPostString gets a post by an ID string.
func GetPostString(ctx context.Context, id string) (*Post, error) {
match, err := regexp.MatchString("^[0-9]+$", id)
if err != nil {
return nil, err
}
if !match {
return nil, fmt.Errorf("no post with id %s", id)
}
i, err := strconv.ParseInt(id, 10, 64)
if err != nil {
return nil, err
}
return GetPost(ctx, i)
}
// GetPost gets a post by ID from the database.
func GetPost(ctx context.Context, id int64) (*Post, error) {
var post Post
row := db.QueryRowContext(ctx, "SELECT id, title, content, date, created_at, modified_at, tags, draft FROM posts WHERE id = $1", id)
err := row.Scan(&post.ID, &post.Title, &post.Content, &post.Datetime, &post.Created, &post.Modified, pq.Array(&post.Tags), &post.Draft)
switch {
case err == sql.ErrNoRows:
return nil, nil
case err != nil:
return nil, fmt.Errorf("error with get: %w", err)
default:
return &post, nil
}
}
// AllTags returns all tags used in all posts.
func AllTags(ctx context.Context) ([]string, error) {
rows, err := db.QueryContext(ctx, "SELECT UNNEST(tags) AS tag, COUNT(*) AS cnt FROM posts GROUP BY tag ORDER BY cnt DESC")
if err != nil {
return nil, err
}
defer rows.Close()
tags := make([]string, 0)
for rows.Next() {
var tag string
var cnt int
err := rows.Scan(&tag, &cnt)
if err != nil {
return tags, err
}
tags = append(tags, tag)
}
if err = rows.Err(); err != nil {
return tags, err
}
return tags, nil
}
// Drafts is a simple wrapper around Posts that does return drafts.
func Drafts(ctx context.Context, limit, offset int) ([]*Post, error) {
query := `
SELECT id, title, content, date, created_at, modified_at, tags, draft
FROM posts
WHERE draft = true
ORDER BY date DESC
LIMIT $1 OFFSET $2
`
return postQuery(ctx, query, limit, offset)
}
var tagAliases = map[string]string{
"hackerschool": "recursecenter",
}
// ParseTags returns a list of all hashtags currently in a post.
func ParseTags(text string) ([]string, error) {
// http://golang.org/pkg/regexp/#Regexp.FindAllStringSubmatch
finds := HashtagRegex.FindAllStringSubmatch(text, -1)
tagMap := map[string]int{}
for _, v := range finds {
if len(v) > 2 {
tag := strings.ToLower(v[2])
if alias, ok := tagAliases[tag]; ok {
tagMap[alias]++
}
tagMap[tag]++
}
}
ret := []string{}
for k := range tagMap {
ret = append(ret, k)
}
sort.Strings(ret)
return ret, nil
}
// Save inserts or updates a post into the database.
func (p *Post) Save(ctx context.Context) error {
if p.ID == "" {
maxID, err := GetMaxID(ctx)
if err != nil {
return err
}
p.ID = fmt.Sprintf("%d", maxID+1)
}
tags, err := ParseTags(p.Content)
if err != nil {
return err
}
p.Tags = tags
if p.Title == "" {
p.Title = fmt.Sprintf("Untitled #%s", p.ID)
}
if p.Datetime.IsZero() {
p.Datetime = time.Now()
}
if p.Created.IsZero() {
p.Created = time.Now()
}
p.Modified = time.Now()
if _, err := db.ExecContext(
ctx,
`
INSERT INTO posts(id, title, content, date, draft, created_at, modified_at, tags)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
ON CONFLICT (id) DO UPDATE
SET (title, content, date, draft, created_at, modified_at, tags) = ($2, $3, $4, $5, $6, $7, $8)
WHERE posts.id = $1;
`,
p.ID,
p.Title,
p.Content,
p.Datetime,
p.Draft,
p.Created,
p.Modified,
pq.Array(p.Tags)); err != nil {
return err
}
_, err = strconv.ParseInt(p.ID, 10, 64)
if err != nil {
return err
}
return nil
}
// Comments returns the comments for a post
func (p *Post) Comments(ctx context.Context, input *Limit) ([]*Comment, error) {
limit := 100
offset := 0
if input != nil {
i := *input
if i.Limit != nil {
limit = *i.Limit
}
if i.Offset != nil {
offset = *i.Offset
}
}
return PostComments(ctx, p.ID, limit, offset)
}
// IntID returns this posts ID as an int.
func (p *Post) IntID() int64 {
i, err := strconv.ParseInt(p.ID, 10, 64)
if err != nil {
return 0
}
return i
}
func (p *Post) SocialImage(ctx context.Context) (*URI, error) {
return GenerateSocialImage(ctx, p.Title, p.Datetime)
}
// Summary returns the first sentence of a post.
func (p *Post) Summary() string {
return SummarizeText(p.Content)
}
// HTML returns the post as rendered HTML.
func (p *Post) HTML() template.HTML {
return Markdown(p.Content)
}
// URI returns an absolute link to this post.
func (p *Post) URI() *URI {
return NewURI(fmt.Sprintf("https://writing.natwelch.com/post/%s", p.ID))
}
func (p *Post) GetURI() URI {
return *p.URI()
}
// Next returns the next post chronologically.
func (p *Post) Next(ctx context.Context) (*Post, error) {
var postID string
row := db.QueryRowContext(ctx, "SELECT id FROM posts WHERE draft = false AND date > (SELECT date FROM posts WHERE id = $1) ORDER BY date ASC LIMIT 1", p.ID)
err := row.Scan(&postID)
switch {
case err == sql.ErrNoRows:
return nil, nil
case err != nil:
return nil, err
default:
return GetPostString(ctx, postID)
}
}
// Prev returns the previous post chronologically.
func (p *Post) Prev(ctx context.Context) (*Post, error) {
var postID string
row := db.QueryRowContext(ctx, "SELECT id FROM posts WHERE draft = false AND date < (SELECT date FROM posts WHERE id = $1) ORDER BY date DESC LIMIT 1", p.ID)
err := row.Scan(&postID)
switch {
case err == sql.ErrNoRows:
return nil, nil
case err != nil:
return nil, err
default:
return GetPostString(ctx, postID)
}
}
// ReadTime calculates the number of seconds it should take to read the post.
func (p *Post) Readtime(_ context.Context) int32 {
ReadingSpeed := 265.0
words := len(strings.Split(p.Content, " "))
seconds := int32(math.Ceil(float64(words) / ReadingSpeed * 60.0))
return seconds
}
// IsLinkable exists to show that this method implements the Linkable type in
// graphql.
func (p *Post) IsLinkable() {}
// Related returns an array of related posts. It is quite slow in comparison to
// other queries.
func (p *Post) Related(ctx context.Context, input *Limit) ([]*Post, error) {
limit := 3
offset := 0
if input != nil {
i := *input
if i.Limit != nil {
limit = *i.Limit
}
if i.Offset != nil {
offset = *i.Offset
}
}
_, err := db.QueryContext(ctx, "SELECT set_limit(0.6)")
if err != nil {
return nil, err
}
// From https://www.postgresql.org/docs/9.6/pgtrgm.html
query := `
SELECT SIMILARITY($1, title) AS sim, id
FROM posts
WHERE id != $2
AND title % $1
AND draft = false
ORDER BY sim DESC
LIMIT $3 OFFSET $4`
rows, err := db.QueryContext(ctx, query, p.Title, p.ID, limit, offset)
if err != nil {
return nil, err
}
defer rows.Close()
posts := make([]*Post, 0)
for rows.Next() {
var id string
var sim float64
err := rows.Scan(&sim, &id)
if err != nil {
return nil, err
}
post, err := GetPostString(ctx, id)
if err != nil {
return nil, err
}
posts = append(posts, post)
}
if len(posts) < limit {
existing := []int64{}
for _, p := range posts {
existing = append(existing, p.IntID())
}
addPosts, err := GetRandomPosts(ctx, limit-len(posts), existing)
if err != nil {
return nil, err
}
posts = append(posts, addPosts...)
}
return posts, nil
}
// GetRandomPosts returns a random selection of posts.
func GetRandomPosts(ctx context.Context, limit int, notIn []int64) ([]*Post, error) {
query := `SELECT id, title, content, date, created_at, modified_at, tags, draft
FROM posts
WHERE draft = false
AND id <> ALL($1)
ORDER BY random() DESC LIMIT $2`
return postQuery(ctx, query, pq.Array(notIn), limit)
}
// Posts returns some posts.
func Posts(ctx context.Context, limit int, offset int) ([]*Post, error) {
query := `
SELECT id, title, content, date, created_at, modified_at, tags, draft
FROM posts
WHERE draft = false
AND date <= NOW()
ORDER BY date DESC
LIMIT $1 OFFSET $2
`
return postQuery(ctx, query, limit, offset)
}
// FuturePosts returns some posts that are in the future.
func FuturePosts(ctx context.Context, limit int, offset int) ([]*Post, error) {
query := `
SELECT id, title, content, date, created_at, modified_at, tags, draft
FROM posts
WHERE draft = false
AND date > NOW()
ORDER BY date DESC
LIMIT $1 OFFSET $2
`
return postQuery(ctx, query, limit, offset)
}
// PostsByTag returns all posts with a tag.
func PostsByTag(ctx context.Context, tag string) ([]*Post, error) {
query := `
SELECT id, title, content, date, created_at, modified_at, tags, draft
FROM posts
WHERE $1 = ANY(tags)
AND draft = false
ORDER BY date DESC
`
return postQuery(ctx, query, tag)
}
func postQuery(ctx context.Context, query string, args ...interface{}) ([]*Post, error) {
rows, err := db.QueryContext(ctx, query, args...)
if err != nil {
return nil, err
}
defer rows.Close()
posts := make([]*Post, 0)
for rows.Next() {
post := new(Post)
err := rows.Scan(&post.ID, &post.Title, &post.Content, &post.Datetime, &post.Created, &post.Modified, pq.Array(&post.Tags), &post.Draft)
if err != nil {
return nil, err
}
posts = append(posts, post)
}
if err = rows.Err(); err != nil {
return nil, err
}
return posts, nil
}