/
comment.go
605 lines (513 loc) · 15 KB
/
comment.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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
package nakama
import (
"bytes"
"context"
"database/sql"
"encoding/gob"
"encoding/json"
"fmt"
"io"
"strings"
"time"
"unicode/utf8"
"github.com/cockroachdb/cockroach-go/v2/crdb"
)
const commentContentMaxLength = 2048
var (
// ErrInvalidCommentID denotes an invalid comment ID; that is not uuid.
ErrInvalidCommentID = InvalidArgumentError("invalid comment ID")
// ErrCommentNotFound denotes a not found comment.
ErrCommentNotFound = NotFoundError("comment not found")
ErrUpdateCommentDenied = PermissionDeniedError("update comment denied")
)
// Comment model.
type Comment struct {
ID string `json:"id"`
UserID string `json:"-"`
PostID string `json:"-"`
Content string `json:"content"`
Reactions []Reaction `json:"reactions"`
CreatedAt time.Time `json:"createdAt"`
User *User `json:"user,omitempty"`
Mine bool `json:"mine"`
}
type UpdateComment struct {
ID string `json:"-"`
Content *string `json:"content"`
}
type UpdatedComment struct {
Content string `json:"content"`
}
// CreateComment on a post.
func (s *Service) CreateComment(ctx context.Context, postID string, content string) (Comment, error) {
var c Comment
uid, ok := ctx.Value(KeyAuthUserID).(string)
if !ok {
return c, ErrUnauthenticated
}
if !reUUID.MatchString(postID) {
return c, ErrInvalidPostID
}
content = smartTrim(content)
if content == "" || utf8.RuneCountInString(content) > commentContentMaxLength {
return c, ErrInvalidContent
}
tags := collectTags(content)
err := crdb.ExecuteTx(ctx, s.DB, nil, func(tx *sql.Tx) error {
query := `
INSERT INTO comments (user_id, post_id, content) VALUES ($1, $2, $3)
RETURNING id, created_at`
err := tx.QueryRowContext(ctx, query, uid, postID, content).Scan(&c.ID, &c.CreatedAt)
if isForeignKeyViolation(err) {
return ErrPostNotFound
}
if err != nil {
return fmt.Errorf("could not insert comment: %w", err)
}
c.UserID = uid
c.PostID = postID
c.Content = content
c.Mine = true
query = `
INSERT INTO post_subscriptions (user_id, post_id) VALUES ($1, $2)
ON CONFLICT (user_id, post_id) DO NOTHING`
if _, err = tx.ExecContext(ctx, query, uid, postID); err != nil {
return fmt.Errorf("could not insert post subcription after commenting: %w", err)
}
if len(tags) != 0 {
var values []string
args := []interface{}{postID, c.ID}
for i := 0; i < len(tags); i++ {
values = append(values, fmt.Sprintf("($1, $2, $%d)", i+3))
args = append(args, tags[i])
}
query := `INSERT INTO post_tags (post_id, comment_id, tag) VALUES ` + strings.Join(values, ", ")
_, err := tx.ExecContext(ctx, query, args...)
if err != nil {
return fmt.Errorf("could not sql insert post (comment) tags: %w", err)
}
}
query = "UPDATE posts SET comments_count = comments_count + 1 WHERE id = $1"
if _, err = tx.ExecContext(ctx, query, postID); err != nil {
return fmt.Errorf("could not update and increment post comments count: %w", err)
}
return nil
})
if err != nil {
return c, err
}
go s.commentCreated(c)
return c, nil
}
func (s *Service) commentCreated(c Comment) {
u, err := s.userByID(context.Background(), c.UserID)
if err != nil {
_ = s.Logger.Log("error", fmt.Errorf("could not fetch comment user: %w", err))
return
}
c.User = &u
c.Mine = false
go s.notifyComment(c)
go s.notifyCommentMention(c)
go s.broadcastComment(c)
}
type Comments []Comment
func (cc Comments) EndCursor() *string {
if len(cc) == 0 {
return nil
}
last := cc[len(cc)-1]
return ptrString(encodeCursor(last.ID, last.CreatedAt))
}
// Comments from a post in descending order with backward pagination.
func (s *Service) Comments(ctx context.Context, postID string, last uint64, before *string) (Comments, error) {
if !reUUID.MatchString(postID) {
return nil, ErrInvalidPostID
}
var beforeCommentID string
var beforeCreatedAt time.Time
if before != nil {
var err error
beforeCommentID, beforeCreatedAt, err = decodeCursor(*before)
if err != nil || !reUUID.MatchString(beforeCommentID) {
return nil, ErrInvalidCursor
}
}
uid, auth := ctx.Value(KeyAuthUserID).(string)
last = normalizePageSize(last)
query, args, err := buildQuery(`
SELECT comments.id
, comments.content
, comments.reactions
, comments.created_at
, users.username
, users.avatar
{{if .auth}}
, comments.user_id = @uid AS comment_mine
, reactions.user_reactions
{{end}}
FROM comments
INNER JOIN users ON comments.user_id = users.id
{{if .auth}}
LEFT JOIN (
SELECT user_id
, comment_id
, json_agg(json_build_object('reaction', reaction, 'type', type)) AS user_reactions
FROM comment_reactions
GROUP BY user_id, comment_id
) AS reactions ON reactions.user_id = @uid AND reactions.comment_id = comments.id
{{end}}
WHERE comments.post_id = @postID
{{ if and .beforeCommentID .beforeCreatedAt }}
AND comments.created_at <= @beforeCreatedAt
AND (
comments.id < @beforeCommentID
OR comments.created_at < @beforeCreatedAt
)
{{ end }}
ORDER BY comments.created_at DESC, comments.id ASC
LIMIT @last`, map[string]interface{}{
"auth": auth,
"uid": uid,
"postID": postID,
"last": last,
"beforeCommentID": beforeCommentID,
"beforeCreatedAt": beforeCreatedAt,
})
if err != nil {
return nil, fmt.Errorf("could not build comments sql query: %w", err)
}
rows, err := s.DB.QueryContext(ctx, query, args...)
if err != nil {
return nil, fmt.Errorf("could not query select comments: %w", err)
}
defer rows.Close()
var cc Comments
for rows.Next() {
var c Comment
var rawReactions []byte
var rawUserReactions []byte
var u User
var avatar sql.NullString
dest := []interface{}{&c.ID, &c.Content, &rawReactions, &c.CreatedAt, &u.Username, &avatar}
if auth {
dest = append(dest, &c.Mine, &rawUserReactions)
}
if err = rows.Scan(dest...); err != nil {
return nil, fmt.Errorf("could not scan comment: %w", err)
}
if rawReactions != nil {
err = json.Unmarshal(rawReactions, &c.Reactions)
if err != nil {
return nil, fmt.Errorf("could not json unmarshall comment reactions: %w", err)
}
}
if rawUserReactions != nil {
var userReactions []userReaction
err = json.Unmarshal(rawUserReactions, &userReactions)
if err != nil {
return nil, fmt.Errorf("could not json unmarshall user comment reactions: %w", err)
}
for i, r := range c.Reactions {
var reacted bool
for _, ur := range userReactions {
if r.Type == ur.Type && r.Reaction == ur.Reaction {
reacted = true
break
}
}
c.Reactions[i].Reacted = &reacted
}
}
u.AvatarURL = s.avatarURL(avatar)
c.User = &u
cc = append(cc, c)
}
if err = rows.Err(); err != nil {
return nil, fmt.Errorf("could not iterate comment rows: %w", err)
}
return cc, nil
}
// CommentStream to receive comments in realtime.
func (s *Service) CommentStream(ctx context.Context, postID string) (<-chan Comment, error) {
if !reUUID.MatchString(postID) {
return nil, ErrInvalidPostID
}
cc := make(chan Comment)
uid, auth := ctx.Value(KeyAuthUserID).(string)
unsub, err := s.PubSub.Sub(commentTopic(postID), func(data []byte) {
go func(r io.Reader) {
var c Comment
err := gob.NewDecoder(r).Decode(&c)
if err != nil {
_ = s.Logger.Log("error", fmt.Errorf("could not gob decode comment: %w", err))
return
}
if auth && uid == c.UserID {
return
}
cc <- c
}(bytes.NewReader(data))
})
if err != nil {
return nil, fmt.Errorf("could not subscribe to comments: %w", err)
}
go func() {
<-ctx.Done()
if err := unsub(); err != nil {
_ = s.Logger.Log("error", fmt.Errorf("could not unsubcribe from comments: %w", err))
// don't return
}
close(cc)
}()
return cc, nil
}
func (s *Service) UpdateComment(ctx context.Context, in UpdateComment) (UpdatedComment, error) {
var out UpdatedComment
in.ID = strings.TrimSpace(in.ID)
if !reUUID.MatchString(in.ID) {
return out, ErrInvalidCommentID
}
if in.Content != nil {
*in.Content = smartTrim(*in.Content)
if *in.Content == "" || utf8.RuneCountInString(*in.Content) > commentContentMaxLength {
return out, ErrInvalidContent
}
}
uid, ok := ctx.Value(KeyAuthUserID).(string)
if !ok {
return out, ErrUnauthenticated
}
return out, crdb.ExecuteTx(ctx, s.DB, nil, func(tx *sql.Tx) error {
var isOwner bool
query := "SELECT user_id = $1 FROM comments WHERE id = $2"
row := tx.QueryRowContext(ctx, query, uid, in.ID)
err := row.Scan(&isOwner)
if err == sql.ErrNoRows {
return ErrCommentNotFound
}
if err != nil {
return fmt.Errorf("could not sql query select comment is owner: %w", err)
}
if !isOwner {
return ErrPermissionDenied
}
var createdAt time.Time
query = "SELECT created_at FROM comments WHERE id = $1"
row = tx.QueryRowContext(ctx, query, in.ID)
err = row.Scan(&createdAt)
if err == sql.ErrNoRows {
return ErrCommentNotFound
}
if err != nil {
return fmt.Errorf("could not sql query select comment created at: %w", err)
}
isRecent := time.Since(createdAt) <= time.Minute*15
if !isRecent {
return ErrUpdateCommentDenied
}
query = "UPDATE comments SET content = COALESCE($1::varchar, content) WHERE id = $2 RETURNING content"
row = tx.QueryRowContext(ctx, query, in.Content, in.ID)
err = row.Scan(&out.Content)
if err == sql.ErrNoRows {
return ErrCommentNotFound
}
if err != nil {
return fmt.Errorf("could not sql update comment: %w", err)
}
return nil
})
}
func (s *Service) DeleteComment(ctx context.Context, commentID string) error {
uid, ok := ctx.Value(KeyAuthUserID).(string)
if !ok {
return ErrUnauthenticated
}
if !reUUID.MatchString(commentID) {
return ErrInvalidCommentID
}
err := crdb.ExecuteTx(ctx, s.DB, nil, func(tx *sql.Tx) error {
var postID string
query := "SELECT post_id FROM comments WHERE id = $1 AND user_id = $2"
row := tx.QueryRowContext(ctx, query, commentID, uid)
err := row.Scan(&postID)
if err == sql.ErrNoRows {
return ErrCommentNotFound
}
if err != nil {
return fmt.Errorf("could not sql query select comment to delete post id: %w", err)
}
query = "DELETE FROM comments WHERE id = $1"
_, err = tx.ExecContext(ctx, query, commentID)
if err != nil {
return fmt.Errorf("could not delete comment: %w", err)
}
query = "UPDATE posts SET comments_count = comments_count - 1 WHERE id = $1"
_, err = tx.ExecContext(ctx, query, postID)
if err != nil {
return fmt.Errorf("could not update post comments count after comment deletion: %w", err)
}
return nil
})
if err != nil && err != ErrCommentNotFound {
return err
}
return nil
}
func (s *Service) ToggleCommentReaction(ctx context.Context, commentID string, in ReactionInput) ([]Reaction, error) {
uid, ok := ctx.Value(KeyAuthUserID).(string)
if !ok {
return nil, ErrUnauthenticated
}
if !reUUID.MatchString(commentID) {
return nil, ErrInvalidCommentID
}
if in.Type != "emoji" || in.Reaction == "" {
return nil, ErrInvalidReaction
}
if in.Type == "emoji" && !validEmoji(in.Reaction) {
return nil, ErrInvalidReaction
}
var out []Reaction
err := crdb.ExecuteTx(ctx, s.DB, nil, func(tx *sql.Tx) error {
out = nil
var rawReactions []byte
var rawUserReactions []byte
query := `
SELECT comments.reactions, reactions.user_reactions
FROM comments
LEFT JOIN (
SELECT user_id
, comment_id
, json_agg(json_build_object('reaction', reaction, 'type', type)) AS user_reactions
FROM comment_reactions
GROUP BY user_id, comment_id
) AS reactions ON reactions.user_id = $1 AND reactions.comment_id = comments.id
WHERE comments.id = $2`
row := tx.QueryRowContext(ctx, query, uid, commentID)
err := row.Scan(&rawReactions, &rawUserReactions)
if err == sql.ErrNoRows {
return ErrCommentNotFound
}
if err != nil {
return fmt.Errorf("could not sql scan comment and user reactions: %w", err)
}
var reactions []Reaction
if rawReactions != nil {
err = json.Unmarshal(rawReactions, &reactions)
if err != nil {
return fmt.Errorf("could not json unmarshall comment reactions: %w", err)
}
}
var userReactions []userReaction
if rawUserReactions != nil {
err = json.Unmarshal(rawUserReactions, &userReactions)
if err != nil {
return fmt.Errorf("could not json unmarshall user comment reactions: %w", err)
}
}
userReactionIdx := -1
for i, ur := range userReactions {
if ur.Type == in.Type && ur.Reaction == in.Reaction {
userReactionIdx = i
break
}
}
reacted := userReactionIdx != -1
if !reacted {
query = "INSERT INTO comment_reactions (user_id, comment_id, type, reaction) VALUES ($1, $2, $3, $4)"
_, err = tx.ExecContext(ctx, query, uid, commentID, in.Type, in.Reaction)
if err != nil {
return fmt.Errorf("could not sql insert comment reaction: %w", err)
}
} else {
query = `
DELETE FROM comment_reactions
WHERE user_id = $1
AND comment_id = $2
AND type = $3
AND reaction = $4
`
_, err = tx.ExecContext(ctx, query, uid, commentID, in.Type, in.Reaction)
if err != nil {
return fmt.Errorf("could not sql delete comment reaction: %w", err)
}
}
if reacted {
userReactions = append(userReactions[:userReactionIdx], userReactions[userReactionIdx+1:]...)
} else {
userReactions = append(userReactions, userReaction{
Type: in.Type,
Reaction: in.Reaction,
})
}
var updated bool
zeroReactionsIdx := -1
for i, r := range reactions {
if !(r.Type == in.Type && r.Reaction == in.Reaction) {
continue
}
if !reacted {
reactions[i].Count++
} else {
reactions[i].Count--
if reactions[i].Count == 0 {
zeroReactionsIdx = i
}
}
updated = true
break
}
if !updated {
reactions = append(reactions, Reaction{
Type: in.Type,
Reaction: in.Reaction,
Count: 1,
})
}
if zeroReactionsIdx != -1 {
reactions = append(reactions[:zeroReactionsIdx], reactions[zeroReactionsIdx+1:]...)
}
rawReactions, err = json.Marshal(reactions)
if err != nil {
return fmt.Errorf("could not json marshall comment reactions: %w", err)
}
query = "UPDATE comments SET reactions = $1 WHERE comments.id = $2"
_, err = tx.ExecContext(ctx, query, rawReactions, commentID)
if err != nil {
return fmt.Errorf("could not sql update comment reactions: %w", err)
}
if len(userReactions) != 0 {
for i, r := range reactions {
var reacted bool
for _, ur := range userReactions {
if r.Type == ur.Type && r.Reaction == ur.Reaction {
reacted = true
break
}
}
reactions[i].Reacted = &reacted
}
}
out = reactions
return nil
})
if err != nil {
return nil, err
}
return out, nil
}
func (s *Service) broadcastComment(c Comment) {
var b bytes.Buffer
err := gob.NewEncoder(&b).Encode(c)
if err != nil {
_ = s.Logger.Log("error", fmt.Errorf("could not gob encode comment: %w", err))
return
}
err = s.PubSub.Pub(commentTopic(c.PostID), b.Bytes())
if err != nil {
_ = s.Logger.Log("error", fmt.Errorf("could not publish comment: %w", err))
return
}
}
func commentTopic(postID string) string { return "comment_" + postID }