-
-
Notifications
You must be signed in to change notification settings - Fork 32
/
post.go
798 lines (694 loc) · 19.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
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
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
package nakama
import (
"bytes"
"context"
"database/sql"
"encoding/gob"
"encoding/json"
"fmt"
"io"
"strings"
"time"
"unicode/utf8"
"github.com/cockroachdb/cockroach-go/v2/crdb"
"github.com/lib/pq"
)
const (
postContentMaxLength = 2048
postSpoilerMaxLength = 64
)
var (
// ErrInvalidPostID denotes an invalid post ID; that is not uuid.
ErrInvalidPostID = InvalidArgumentError("invalid post ID")
// ErrInvalidContent denotes an invalid content.
ErrInvalidContent = InvalidArgumentError("invalid content")
// ErrInvalidSpoiler denotes an invalid spoiler title.
ErrInvalidSpoiler = InvalidArgumentError("invalid spoiler")
// ErrPostNotFound denotes a not found post.
ErrPostNotFound = NotFoundError("post not found")
// ErrInvalidUpdatePostParams denotes invalid params to update a post, that is no params altogether.
ErrInvalidUpdatePostParams = InvalidArgumentError("invalid update post params")
// ErrInvalidCursor denotes an invalid cursor, that is not base64 encoded and has a key and timestamp separated by comma.
ErrInvalidCursor = InvalidArgumentError("invalid cursor")
// ErrInvalidReaction denotes an invalid reaction, that may by an invalid reaction type, or invalid reaction by itslef,
// not a valid emoji, or invalid reaction image URL.
ErrInvalidReaction = InvalidArgumentError("invalid reaction")
ErrUpdatePostDenied = PermissionDeniedError("update post denied")
)
// Post model.
type Post struct {
ID string `json:"id"`
UserID string `json:"-"`
Content string `json:"content"`
SpoilerOf *string `json:"spoilerOf"`
NSFW bool `json:"nsfw"`
Reactions []Reaction `json:"reactions"`
CommentsCount int `json:"commentsCount"`
MediaURLs []string `json:"mediaURLs"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
User *User `json:"user,omitempty"`
Mine bool `json:"mine"`
Subscribed bool `json:"subscribed"`
}
type Reaction struct {
Type string `json:"type"`
Reaction string `json:"reaction"`
Count uint64 `json:"count"`
Reacted *bool `json:"reacted,omitempty"`
}
type userReaction struct {
Reaction string `json:"reaction"`
Type string `json:"type"`
}
// ToggleSubscriptionOutput response.
type ToggleSubscriptionOutput struct {
Subscribed bool `json:"subscribed"`
}
type Posts []Post
func (pp Posts) EndCursor() *string {
if len(pp) == 0 {
return nil
}
last := pp[len(pp)-1]
return ptrString(encodeCursor(last.ID, last.CreatedAt))
}
type PostsOpts struct {
Username *string
Tag *string
}
type PostsOpt func(*PostsOpts)
func PostsFromUser(username string) PostsOpt {
return func(opts *PostsOpts) {
opts.Username = &username
}
}
func PostsTagged(tag string) PostsOpt {
return func(opts *PostsOpts) {
opts.Tag = &tag
}
}
// Posts in descending order and with backward pagination.
// They can be filtered from a specific user by using `PostsFromUser` option
// in this late case, user field won't be populated.
// They can also be filtered by tag using `PostsTagged`.
func (s *Service) Posts(ctx context.Context, last uint64, before *string, opts ...PostsOpt) (Posts, error) {
var options PostsOpts
for _, o := range opts {
o(&options)
}
if options.Username != nil {
*options.Username = strings.TrimSpace(*options.Username)
if !ValidUsername(*options.Username) {
return nil, ErrInvalidUsername
}
}
var beforePostID string
var beforeCreatedAt time.Time
if before != nil {
var err error
beforePostID, beforeCreatedAt, err = decodeCursor(*before)
if err != nil || !reUUID.MatchString(beforePostID) {
return nil, ErrInvalidCursor
}
}
uid, auth := ctx.Value(KeyAuthUserID).(string)
last = normalizePageSize(last)
query, args, err := buildQuery(`
SELECT posts.id
, posts.content
, posts.spoiler_of
, posts.nsfw
, posts.reactions
, posts.comments_count
, posts.media
, posts.created_at
, posts.updated_at
{{ if .auth }}
, posts.user_id = @uid AS post_mine
, reactions.user_reactions
, subscriptions.user_id IS NOT NULL AS post_subscribed
{{ end }}
{{ if not .username }}
, users.username
, users.avatar
{{ end }}
FROM posts
{{ if .auth }}
LEFT JOIN (
SELECT user_id
, post_id
, json_agg(json_build_object('reaction', reaction, 'type', type)) AS user_reactions
FROM post_reactions
GROUP BY user_id, post_id
) AS reactions ON reactions.user_id = @uid AND reactions.post_id = posts.id
LEFT JOIN post_subscriptions AS subscriptions
ON subscriptions.user_id = @uid AND subscriptions.post_id = posts.id
{{ end }}
{{ if not .username }}
INNER JOIN users ON posts.user_id = users.id
{{ end }}
{{ if .tag }}
INNER JOIN post_tags ON post_tags.post_id = posts.id AND post_tags.tag = @tag
{{ end }}
{{ if or .username (and .beforePostID .beforeCreatedAt) }}
WHERE
{{ end }}
{{ if .username }}
posts.user_id = (SELECT id FROM users WHERE username = @username)
{{ end }}
{{ if and .beforePostID .beforeCreatedAt }}
{{ if .username }}
AND
{{ end }}
posts.created_at <= @beforeCreatedAt
AND (
posts.id < @beforePostID
OR posts.created_at < @beforeCreatedAt
)
{{ end }}
ORDER BY posts.created_at DESC, posts.id ASC
LIMIT @last`, map[string]interface{}{
"auth": auth,
"uid": uid,
"username": options.Username,
"tag": options.Tag,
"last": last,
"beforePostID": beforePostID,
"beforeCreatedAt": beforeCreatedAt,
})
if err != nil {
return nil, fmt.Errorf("could not build posts sql query: %w", err)
}
rows, err := s.DB.QueryContext(ctx, query, args...)
if err != nil {
return nil, fmt.Errorf("could not query select posts: %w", err)
}
defer rows.Close()
var pp Posts
for rows.Next() {
var p Post
var u User
var avatar sql.NullString
var rawReactions []byte
var rawUserReactions []byte
var media []string
dest := []interface{}{
&p.ID,
&p.Content,
&p.SpoilerOf,
&p.NSFW,
&rawReactions,
&p.CommentsCount,
pq.Array(&media),
&p.CreatedAt,
&p.UpdatedAt,
}
if auth {
dest = append(dest, &p.Mine, &rawUserReactions, &p.Subscribed)
}
if options.Username == nil {
dest = append(dest, &u.Username, &avatar)
}
if err = rows.Scan(dest...); err != nil {
return nil, fmt.Errorf("could not scan post: %w", err)
}
if rawReactions != nil {
err = json.Unmarshal(rawReactions, &p.Reactions)
if err != nil {
return nil, fmt.Errorf("could not json unmarshall post 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 post reactions: %w", err)
}
for i, r := range p.Reactions {
var reacted bool
for _, ur := range userReactions {
if r.Type == ur.Type && r.Reaction == ur.Reaction {
reacted = true
break
}
}
p.Reactions[i].Reacted = &reacted
}
}
if options.Username == nil {
u.AvatarURL = s.avatarURL(avatar)
p.User = &u
}
p.MediaURLs = s.mediaURLs(media)
pp = append(pp, p)
}
if err = rows.Err(); err != nil {
return nil, fmt.Errorf("could not iterate posts rows: %w", err)
}
return pp, nil
}
// PostStream to receive posts in realtime.
func (s *Service) PostStream(ctx context.Context) (<-chan Post, error) {
pp := make(chan Post)
unsub, err := s.PubSub.Sub(postsTopic, func(data []byte) {
go func(r io.Reader) {
var p Post
err := gob.NewDecoder(r).Decode(&p)
if err != nil {
_ = s.Logger.Log("error", fmt.Errorf("could not gob decode post: %w", err))
return
}
pp <- p
}(bytes.NewReader(data))
})
if err != nil {
return nil, fmt.Errorf("could not subscribe to posts: %w", err)
}
go func() {
<-ctx.Done()
if err := unsub(); err != nil {
_ = s.Logger.Log("error", fmt.Errorf("could not unsubcribe from posts: %w", err))
// don't return
}
close(pp)
}()
return pp, nil
}
// Post with the given ID.
func (s *Service) Post(ctx context.Context, postID string) (Post, error) {
var p Post
if !reUUID.MatchString(postID) {
return p, ErrInvalidPostID
}
uid, auth := ctx.Value(KeyAuthUserID).(string)
query, args, err := buildQuery(`
SELECT posts.id
, posts.content
, posts.spoiler_of
, posts.nsfw
, posts.reactions
, posts.comments_count
, posts.media
, posts.created_at
, posts.updated_at
, users.username
, users.avatar
{{if .auth}}
, posts.user_id = @uid AS mine
, reactions.user_reactions
, subscriptions.user_id IS NOT NULL AS subscribed
{{end}}
FROM posts
INNER JOIN users ON posts.user_id = users.id
{{if .auth}}
LEFT JOIN (
SELECT user_id
, post_id
, json_agg(json_build_object('reaction', reaction, 'type', type)) AS user_reactions
FROM post_reactions
GROUP BY user_id, post_id
) AS reactions ON reactions.user_id = @uid AND reactions.post_id = posts.id
LEFT JOIN post_subscriptions AS subscriptions
ON subscriptions.user_id = @uid AND subscriptions.post_id = posts.id
{{end}}
WHERE posts.id = @post_id`, map[string]interface{}{
"auth": auth,
"uid": uid,
"post_id": postID,
})
if err != nil {
return p, fmt.Errorf("could not build post sql query: %w", err)
}
var rawReactions []byte
var rawUserReactions []byte
var u User
var avatar sql.NullString
var media []string
dest := []interface{}{
&p.ID,
&p.Content,
&p.SpoilerOf,
&p.NSFW,
&rawReactions,
&p.CommentsCount,
pq.Array(&media),
&p.CreatedAt,
&p.UpdatedAt,
&u.Username,
&avatar,
}
if auth {
dest = append(dest, &p.Mine, &rawUserReactions, &p.Subscribed)
}
err = s.DB.QueryRowContext(ctx, query, args...).Scan(dest...)
if err == sql.ErrNoRows {
return p, ErrPostNotFound
}
if err != nil {
return p, fmt.Errorf("could not query select post: %w", err)
}
if rawReactions != nil {
err = json.Unmarshal(rawReactions, &p.Reactions)
if err != nil {
return p, fmt.Errorf("could not json unmarshall post reactions: %w", err)
}
}
if rawUserReactions != nil {
var userReactions []userReaction
err = json.Unmarshal(rawUserReactions, &userReactions)
if err != nil {
return p, fmt.Errorf("could not json unmarshall user post reactions: %w", err)
}
for i, r := range p.Reactions {
var reacted bool
for _, ur := range userReactions {
if r.Type == ur.Type && r.Reaction == ur.Reaction {
reacted = true
break
}
}
p.Reactions[i].Reacted = &reacted
}
}
p.MediaURLs = s.mediaURLs(media)
u.AvatarURL = s.avatarURL(avatar)
p.User = &u
return p, nil
}
type UpdatePost struct {
Content *string `json:"content"`
SpoilerOf *string `json:"spoilerOf"`
NSFW *bool `json:"nsfw"`
}
func (params UpdatePost) Empty() bool {
return params.Content == nil && params.NSFW == nil && params.SpoilerOf == nil
}
type UpdatedPost struct {
Content string `json:"content"`
SpoilerOf *string `json:"spoilerOf"`
NSFW bool `json:"nsfw"`
UpdatedAt time.Time `json:"updatedAt"`
}
func (s *Service) UpdatePost(ctx context.Context, postID string, params UpdatePost) (UpdatedPost, error) {
var out UpdatedPost
createdAt, err := s.postCreatedAt(ctx, postID)
if err != nil {
return out, err
}
isRecent := time.Since(createdAt) < time.Minute*15
if !isRecent {
return out, ErrUpdatePostDenied
}
return s.updatePost(ctx, postID, params)
}
func (s *Service) postCreatedAt(ctx context.Context, postID string) (time.Time, error) {
const q = `
SELECT created_at FROM posts WHERE id = $1
`
var createdAt time.Time
err := s.DB.QueryRowContext(ctx, q, postID).Scan(&createdAt)
if err == sql.ErrNoRows {
return createdAt, ErrPostNotFound
}
if err != nil {
return createdAt, fmt.Errorf("sql query select post created at: %w", err)
}
return createdAt, nil
}
func (s *Service) updatePost(ctx context.Context, postID string, params UpdatePost) (UpdatedPost, error) {
var updated UpdatedPost
if params.Empty() {
return updated, ErrInvalidUpdatePostParams
}
uid, ok := ctx.Value(KeyAuthUserID).(string)
if !ok {
return updated, ErrUnauthenticated
}
if !reUUID.MatchString(postID) {
return updated, ErrInvalidPostID
}
if params.Content != nil {
*params.Content = smartTrim(*params.Content)
if *params.Content == "" || utf8.RuneCountInString(*params.Content) > postContentMaxLength {
return updated, ErrInvalidContent
}
}
if params.SpoilerOf != nil {
*params.SpoilerOf = smartTrim(*params.SpoilerOf)
if *params.SpoilerOf == "" || utf8.RuneCountInString(*params.SpoilerOf) > postSpoilerMaxLength {
return updated, ErrInvalidSpoiler
}
}
var set []string
if params.Content != nil {
set = append(set, "content = @content")
}
if params.SpoilerOf != nil {
set = append(set, "spoiler_of = @spoiler_of")
}
if params.NSFW != nil {
set = append(set, "nsfw = @nsfw")
}
set = append(set, "updated_at = now()")
query, args, err := buildQuery(`
UPDATE posts
SET {{ .set }}
WHERE id = @post_id
AND user_id = @auth_user_id
RETURNING content, spoiler_of, nsfw, updated_at
`, map[string]interface{}{
"content": params.Content,
"spoiler_of": params.SpoilerOf,
"nsfw": params.NSFW,
"set": strings.Join(set, ", "),
"post_id": postID,
"auth_user_id": uid,
})
if err != nil {
return updated, fmt.Errorf("could not sql update post: %w", err)
}
row := s.DB.QueryRowContext(ctx, query, args...)
err = row.Scan(&updated.Content, &updated.SpoilerOf, &updated.NSFW, &updated.UpdatedAt)
if err != nil {
return updated, fmt.Errorf("could not sql update post content: %w", err)
}
return updated, nil
}
func (s *Service) DeletePost(ctx context.Context, postID string) error {
uid, ok := ctx.Value(KeyAuthUserID).(string)
if !ok {
return ErrUnauthenticated
}
if !reUUID.MatchString(postID) {
return ErrInvalidPostID
}
query := "DELETE FROM posts WHERE id = $1 AND user_id = $2"
_, err := s.DB.ExecContext(ctx, query, postID, uid)
if err != nil {
return fmt.Errorf("could not sql delete post: %w", err)
}
return nil
}
type ReactionInput struct {
Type string `json:"type"`
Reaction string `json:"reaction"`
}
func (s *Service) TogglePostReaction(ctx context.Context, postID string, in ReactionInput) ([]Reaction, error) {
uid, ok := ctx.Value(KeyAuthUserID).(string)
if !ok {
return nil, ErrUnauthenticated
}
if !reUUID.MatchString(postID) {
return nil, ErrInvalidPostID
}
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 posts.reactions, reactions.user_reactions
FROM posts
LEFT JOIN (
SELECT user_id
, post_id
, json_agg(json_build_object('reaction', reaction, 'type', type)) AS user_reactions
FROM post_reactions
GROUP BY user_id, post_id
) AS reactions ON reactions.user_id = $1 AND reactions.post_id = posts.id
WHERE posts.id = $2`
row := tx.QueryRowContext(ctx, query, uid, postID)
err := row.Scan(&rawReactions, &rawUserReactions)
if err == sql.ErrNoRows {
return ErrPostNotFound
}
if err != nil {
return fmt.Errorf("could not sql scan post 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 post 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 post 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 post_reactions (user_id, post_id, type, reaction) VALUES ($1, $2, $3, $4)"
_, err = tx.ExecContext(ctx, query, uid, postID, in.Type, in.Reaction)
if err != nil {
return fmt.Errorf("could not sql insert post reaction: %w", err)
}
} else {
query = `
DELETE FROM post_reactions
WHERE user_id = $1
AND post_id = $2
AND type = $3
AND reaction = $4
`
_, err = tx.ExecContext(ctx, query, uid, postID, in.Type, in.Reaction)
if err != nil {
return fmt.Errorf("could not sql delete post 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 post reactions: %w", err)
}
query = "UPDATE posts SET reactions = $1 WHERE posts.id = $2"
_, err = tx.ExecContext(ctx, query, rawReactions, postID)
if err != nil {
return fmt.Errorf("could not sql update post 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
}
// TogglePostSubscription so you can stop receiving notifications from a thread.
func (s *Service) TogglePostSubscription(ctx context.Context, postID string) (ToggleSubscriptionOutput, error) {
var out ToggleSubscriptionOutput
uid, ok := ctx.Value(KeyAuthUserID).(string)
if !ok {
return out, ErrUnauthenticated
}
if !reUUID.MatchString(postID) {
return out, ErrInvalidPostID
}
err := crdb.ExecuteTx(ctx, s.DB, nil, func(tx *sql.Tx) error {
query := `SELECT EXISTS (
SELECT 1 FROM post_subscriptions WHERE user_id = $1 AND post_id = $2
)`
err := tx.QueryRowContext(ctx, query, uid, postID).Scan(&out.Subscribed)
if err != nil {
return fmt.Errorf("could not query select post subscription existence: %w", err)
}
if out.Subscribed {
query = "DELETE FROM post_subscriptions WHERE user_id = $1 AND post_id = $2"
if _, err = tx.ExecContext(ctx, query, uid, postID); err != nil {
return fmt.Errorf("could not delete post subscription: %w", err)
}
} else {
query = "INSERT INTO post_subscriptions (user_id, post_id) VALUES ($1, $2)"
_, err = tx.ExecContext(ctx, query, uid, postID)
if isForeignKeyViolation(err) {
return ErrPostNotFound
}
if err != nil {
return fmt.Errorf("could not insert post subscription: %w", err)
}
}
return nil
})
if err != nil {
return out, err
}
out.Subscribed = !out.Subscribed
return out, nil
}
const postsTopic = "posts"
func (s *Service) broadcastPost(p Post) {
var b bytes.Buffer
err := gob.NewEncoder(&b).Encode(p)
if err != nil {
_ = s.Logger.Log("error", fmt.Errorf("could not gob encode post: %w", err))
return
}
err = s.PubSub.Pub(postsTopic, b.Bytes())
if err != nil {
_ = s.Logger.Log("error", fmt.Errorf("could not publish post: %w", err))
return
}
}