-
Notifications
You must be signed in to change notification settings - Fork 89
/
notifs.go
371 lines (312 loc) · 11.1 KB
/
notifs.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
package notifs
import (
"context"
"fmt"
"time"
appbskytypes "github.com/bluesky-social/indigo/api/bsky"
lexutil "github.com/bluesky-social/indigo/lex/util"
"github.com/bluesky-social/indigo/models"
"github.com/ipfs/go-cid"
cbg "github.com/whyrusleeping/cbor-gen"
"gorm.io/gorm"
"gorm.io/gorm/clause"
)
type NotificationManager interface {
GetNotifications(ctx context.Context, user models.Uid) ([]*appbskytypes.NotificationListNotifications_Notification, error)
GetCount(ctx context.Context, user models.Uid) (int64, error)
UpdateSeen(ctx context.Context, usr models.Uid, seen time.Time) error
AddReplyTo(ctx context.Context, user models.Uid, replyid uint, replyto *models.FeedPost) error
AddMention(ctx context.Context, user models.Uid, postid uint, mentioned models.Uid) error
AddUpVote(ctx context.Context, voter models.Uid, postid uint, voteid uint, postauthor models.Uid) error
AddFollow(ctx context.Context, follower, followed models.Uid, recid uint) error
AddRepost(ctx context.Context, op models.Uid, repost uint, reposter models.Uid) error
}
var _ NotificationManager = (*DBNotifMan)(nil)
type DBNotifMan struct {
db *gorm.DB
getRecord GetRecord
}
type GetRecord func(ctx context.Context, user models.Uid, collection string, rkey string, maybeCid cid.Cid) (cid.Cid, cbg.CBORMarshaler, error)
func NewNotificationManager(db *gorm.DB, getrec GetRecord) *DBNotifMan {
db.AutoMigrate(&NotifRecord{})
db.AutoMigrate(&NotifSeen{})
return &DBNotifMan{
db: db,
getRecord: getrec,
}
}
const (
NotifKindReply = 1
NotifKindMention = 2
NotifKindUpVote = 3
NotifKindFollow = 4
NotifKindRepost = 5
)
type NotifRecord struct {
gorm.Model
For models.Uid
Kind int64
Record uint
Who models.Uid
ReplyTo uint
}
type NotifSeen struct {
ID uint `gorm:"primarykey"`
Usr models.Uid `gorm:"uniqueIndex"`
LastSeen time.Time
}
type HydratedNotification struct {
Record any
IsRead bool
IndexedAt time.Time
Uri string
Cid string
Author *appbskytypes.ActorDefs_ProfileViewBasic
Reason string
ReasonSubject *string
}
func (nm *DBNotifMan) GetNotifications(ctx context.Context, user models.Uid) ([]*appbskytypes.NotificationListNotifications_Notification, error) {
var lastSeen time.Time
if err := nm.db.Model(NotifSeen{}).Where("usr = ?", user).Select("last_seen").Scan(&lastSeen).Error; err != nil {
return nil, err
}
var notifs []NotifRecord
if err := nm.db.Order("created_at desc").Find(¬ifs, "for = ?", user).Error; err != nil {
return nil, err
}
/*
Record any `json:"record" cborgen:"record"`
IsRead bool `json:"isRead" cborgen:"isRead"`
IndexedAt string `json:"indexedAt" cborgen:"indexedAt"`
Uri string `json:"uri" cborgen:"uri"`
Cid string `json:"cid" cborgen:"cid"`
Author *ActorRef_WithInfo `json:"author" cborgen:"author"`
Reason string `json:"reason" cborgen:"reason"`
ReasonSubject *string `json:"reasonSubject" cborgen:"reasonSubject"`
*/
out := []*appbskytypes.NotificationListNotifications_Notification{}
for _, n := range notifs {
hn, err := nm.hydrateNotification(ctx, &n, lastSeen)
if err != nil {
return nil, err
}
// TODO: muting
hn.Author.Viewer = &appbskytypes.ActorDefs_ViewerState{}
out = append(out, hn)
}
return out, nil
}
func (nm *DBNotifMan) hydrateNotification(ctx context.Context, nrec *NotifRecord, lastSeen time.Time) (*appbskytypes.NotificationListNotifications_Notification, error) {
switch nrec.Kind {
case NotifKindReply:
return nm.hydrateNotificationReply(ctx, nrec, lastSeen)
case NotifKindFollow:
return nm.hydrateNotificationFollow(ctx, nrec, lastSeen)
case NotifKindUpVote:
return nm.hydrateNotificationUpVote(ctx, nrec, lastSeen)
case NotifKindRepost:
return nm.hydrateNotificationRepost(ctx, nrec, lastSeen)
/*
case NotifKindMention:
return nm.hydrateNotificationMention(ctx, nrec, lastSeen)
*/
default:
return nil, fmt.Errorf("attempted to hydrate unknown notif kind: %d", nrec.Kind)
}
}
func (nm *DBNotifMan) getActor(ctx context.Context, act models.Uid) (*models.ActorInfo, error) {
var ai models.ActorInfo
if err := nm.db.First(&ai, "uid = ?", act).Error; err != nil {
return nil, err
}
return &ai, nil
}
func (nm *DBNotifMan) hydrateNotificationUpVote(ctx context.Context, nrec *NotifRecord, lastSeen time.Time) (*appbskytypes.NotificationListNotifications_Notification, error) {
var votedOn models.FeedPost
if err := nm.db.First(&votedOn, "id = ?", nrec.Record).Error; err != nil {
return nil, err
}
voter, err := nm.getActor(ctx, nrec.Who)
if err != nil {
return nil, err
}
var vote models.VoteRecord
if err := nm.db.First(&vote, "id = ?", nrec.Record).Error; err != nil {
return nil, err
}
_, rec, err := nm.getRecord(ctx, voter.Uid, "app.bsky.feed.vote", vote.Rkey, cid.Undef)
if err != nil {
return nil, fmt.Errorf("getting vote: %w", err)
}
postAuthor, err := nm.getActor(ctx, votedOn.Author)
if err != nil {
return nil, err
}
rsub := "at://" + postAuthor.Did + "/app.bsky.feed.post/" + votedOn.Rkey
return &appbskytypes.NotificationListNotifications_Notification{
Record: &lexutil.LexiconTypeDecoder{Val: rec},
IsRead: nrec.CreatedAt.Before(lastSeen),
IndexedAt: nrec.CreatedAt.Format(time.RFC3339),
Uri: "at://" + voter.Did + "/app.bsky.feed.vote/" + vote.Rkey,
Cid: vote.Cid,
Author: voter.ActorView(),
Reason: "vote",
ReasonSubject: &rsub,
}, nil
}
func (nm *DBNotifMan) hydrateNotificationRepost(ctx context.Context, nrec *NotifRecord, lastSeen time.Time) (*appbskytypes.NotificationListNotifications_Notification, error) {
var reposted models.FeedPost
if err := nm.db.First(&reposted, "id = ?", nrec.Record).Error; err != nil {
return nil, err
}
reposter, err := nm.getActor(ctx, nrec.Who)
if err != nil {
return nil, err
}
var repost models.RepostRecord
if err := nm.db.First(&repost, "id = ?", nrec.Record).Error; err != nil {
return nil, err
}
_, rec, err := nm.getRecord(ctx, reposter.Uid, "app.bsky.feed.repost", repost.Rkey, cid.Undef)
if err != nil {
return nil, fmt.Errorf("getting repost: %w", err)
}
postAuthor, err := nm.getActor(ctx, repost.Author)
if err != nil {
return nil, err
}
rsub := "at://" + postAuthor.Did + "/app.bsky.feed.post/" + reposted.Rkey
return &appbskytypes.NotificationListNotifications_Notification{
Record: &lexutil.LexiconTypeDecoder{Val: rec},
IsRead: nrec.CreatedAt.Before(lastSeen),
IndexedAt: nrec.CreatedAt.Format(time.RFC3339),
Uri: "at://" + reposter.Did + "/app.bsky.feed.repost/" + repost.Rkey,
Cid: repost.RecCid,
Author: reposter.ActorView(),
Reason: "repost",
ReasonSubject: &rsub,
}, nil
}
func (nm *DBNotifMan) hydrateNotificationReply(ctx context.Context, nrec *NotifRecord, lastSeen time.Time) (*appbskytypes.NotificationListNotifications_Notification, error) {
var fp models.FeedPost
if err := nm.db.First(&fp, "id = ?", nrec.Record).Error; err != nil {
return nil, err
}
var replyTo models.FeedPost
if err := nm.db.First(&replyTo, "id = ?", nrec.ReplyTo).Error; err != nil {
return nil, err
}
var author models.ActorInfo
if err := nm.db.First(&author, "id = ?", fp.Author).Error; err != nil {
return nil, err
}
var opAuthor models.ActorInfo
if err := nm.db.First(&opAuthor, "id = ?", replyTo.Author).Error; err != nil {
return nil, err
}
_, rec, err := nm.getRecord(ctx, author.Uid, "app.bsky.feed.post", fp.Rkey, cid.Undef)
if err != nil {
return nil, err
}
rsub := "at://" + opAuthor.Did + "/app.bsky.feed.post/" + replyTo.Rkey
return &appbskytypes.NotificationListNotifications_Notification{
Record: &lexutil.LexiconTypeDecoder{Val: rec},
IsRead: nrec.CreatedAt.Before(lastSeen),
IndexedAt: nrec.CreatedAt.Format(time.RFC3339),
Uri: "at://" + author.Did + "/app.bsky.feed.post/" + fp.Rkey,
Cid: fp.Cid,
Author: author.ActorView(),
Reason: "reply",
ReasonSubject: &rsub,
}, nil
}
func (nm *DBNotifMan) hydrateNotificationFollow(ctx context.Context, nrec *NotifRecord, lastSeen time.Time) (*appbskytypes.NotificationListNotifications_Notification, error) {
var frec models.FollowRecord
if err := nm.db.First(&frec, "id = ?", nrec.Record).Error; err != nil {
return nil, err
}
var follower models.ActorInfo
if err := nm.db.First(&follower, "id = ?", nrec.Who).Error; err != nil {
return nil, err
}
_, rec, err := nm.getRecord(ctx, follower.Uid, "app.bsky.graph.follow", frec.Rkey, cid.Undef)
if err != nil {
return nil, err
}
return &appbskytypes.NotificationListNotifications_Notification{
Record: &lexutil.LexiconTypeDecoder{Val: rec},
IsRead: nrec.CreatedAt.Before(lastSeen),
IndexedAt: nrec.CreatedAt.Format(time.RFC3339),
Uri: "at://" + follower.Did + "/app.bsky.graph.follow/" + frec.Rkey,
Cid: frec.Cid,
Author: follower.ActorView(),
Reason: "follow",
}, nil
}
func (nm *DBNotifMan) GetCount(ctx context.Context, user models.Uid) (int64, error) {
// TODO: sql count is inefficient
var lseen time.Time
if err := nm.db.Model(NotifSeen{}).Where("usr = ?", user).Select("last_seen").Scan(&lseen).Error; err != nil {
return 0, err
}
var c int64
//seen := nm.db.Model(NotifSeen{}).Where("usr = ?", user).Select("last_seen")
if err := nm.db.Model(NotifRecord{}).Where("for = ? AND created_at > ?", user, lseen).Count(&c).Error; err != nil {
return 0, err
}
return c, nil
}
func (nm *DBNotifMan) UpdateSeen(ctx context.Context, usr models.Uid, seen time.Time) error {
if err := nm.db.Clauses(clause.OnConflict{
Columns: []clause.Column{{Name: "usr"}},
DoUpdates: clause.AssignmentColumns([]string{"last_seen"}),
}).Create(NotifSeen{
Usr: usr,
LastSeen: seen,
}).Error; err != nil {
return err
}
return nil
}
func (nm *DBNotifMan) AddReplyTo(ctx context.Context, user models.Uid, replyid uint, replyto *models.FeedPost) error {
return nm.db.Create(&NotifRecord{
Kind: NotifKindReply,
For: replyto.Author,
Who: user,
ReplyTo: replyto.ID,
Record: replyid,
}).Error
}
func (nm *DBNotifMan) AddMention(ctx context.Context, user models.Uid, postid uint, mentioned models.Uid) error {
return nm.db.Create(&NotifRecord{
For: mentioned,
Kind: NotifKindMention,
Record: postid,
Who: user,
}).Error
}
func (nm *DBNotifMan) AddUpVote(ctx context.Context, voter models.Uid, postid uint, voteid uint, postauthor models.Uid) error {
return nm.db.Create(&NotifRecord{
For: postauthor,
Kind: NotifKindUpVote,
ReplyTo: postid,
Record: voteid,
Who: voter,
}).Error
}
func (nm *DBNotifMan) AddFollow(ctx context.Context, follower, followed models.Uid, recid uint) error {
return nm.db.Create(&NotifRecord{
Kind: NotifKindFollow,
For: followed,
Who: follower,
Record: recid,
}).Error
}
func (nm *DBNotifMan) AddRepost(ctx context.Context, op models.Uid, repost uint, reposter models.Uid) error {
return nm.db.Create(&NotifRecord{
Kind: NotifKindRepost,
For: op,
Record: repost,
Who: reposter,
}).Error
}