Live video on the AT Protocol
1package model
2
3import (
4 "fmt"
5 "time"
6
7 "gorm.io/gorm"
8)
9
10type Notification struct {
11 Token string `gorm:"primarykey"`
12 RepoDID string `json:"repoDID,omitempty" gorm:"column:repo_did;index"`
13 CreatedAt time.Time
14 UpdatedAt time.Time
15 DeletedAt gorm.DeletedAt `gorm:"index"`
16}
17
18func (m *DBModel) CreateNotification(token string, repoDID string) error {
19 not := Notification{
20 Token: token,
21 }
22 if repoDID != "" {
23 not.RepoDID = repoDID
24 }
25 err := m.DB.Save(¬).Error
26 if err != nil {
27 return err
28 }
29 return nil
30}
31
32func (m *DBModel) ListNotifications() ([]Notification, error) {
33 nots := []Notification{}
34 err := m.DB.Find(¬s).Error
35 if err != nil {
36 return nil, fmt.Errorf("error retrieving notifications: %w", err)
37 }
38 return nots, nil
39}
40
41func (m *DBModel) ListUserNotifications(userDID string) ([]Notification, error) {
42 nots := []Notification{}
43 err := m.DB.Where("repo_did = ?", userDID).Find(¬s).Error
44 if err != nil {
45 return nil, fmt.Errorf("error retrieving notifications: %w", err)
46 }
47 return nots, nil
48}
49
50func (m *DBModel) GetFollowersNotificationTokens(userDID string) ([]string, error) {
51 var tokens []string
52
53 err := m.DB.Model(&Notification{}).
54 Distinct("notifications.token").
55 Joins("JOIN follows ON follows.user_did = notifications.repo_did").
56 Where("follows.subject_did = ?", userDID).
57 Pluck("notifications.token", &tokens).
58 Error
59
60 if err != nil {
61 return nil, fmt.Errorf("error retrieving follower notification tokens: %w", err)
62 }
63
64 // also you prolly wanna get one for yourself
65 nots, err := m.ListUserNotifications(userDID)
66 if err != nil {
67 return nil, fmt.Errorf("error retrieving user notifications: %w", err)
68 }
69 for _, not := range nots {
70 tokens = append(tokens, not.Token)
71 }
72
73 return tokens, nil
74}