-
Notifications
You must be signed in to change notification settings - Fork 95
/
notifications.go
176 lines (141 loc) · 4.29 KB
/
notifications.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
package presentator
import (
"errors"
"fmt"
"regexp"
"strings"
"time"
"github.com/pocketbase/dbx"
"github.com/pocketbase/pocketbase/core"
"github.com/pocketbase/pocketbase/daos"
"github.com/pocketbase/pocketbase/models"
"github.com/pocketbase/pocketbase/tools/list"
"github.com/pocketbase/pocketbase/tools/types"
"golang.org/x/sync/errgroup"
)
func createNotifications(app core.App, comment *models.Record) error {
notificationsCollection, err := app.Dao().FindCollectionByNameOrId("notifications")
if err != nil {
return err
}
project, err := getProjectWithUsersByComment(app.Dao(), comment)
if err != nil {
return err
}
var errs []error
users := project.ExpandedAll("users")
for _, user := range users {
// skip if the author of the comment
if comment.GetString("user") == user.Id {
continue
}
pref, err := app.Dao().FindFirstRecordByFilter("projectUserPreferences", "user={:user} && project={:project}", dbx.Params{
"user": user.Id,
"project": project.Id,
})
if err != nil {
errs = append(errs, err)
continue
}
// not watched project
if !pref.GetBool("watch") {
// check if mentioned
mentions := extractMentions(comment.GetString("message"))
if !list.ExistInSlice(user.Username(), mentions) {
continue
}
}
record := models.NewRecord(notificationsCollection)
record.Set("user", user.Id)
record.Set("comment", comment.Id)
// mark as processed to prevent sending email notification
record.Set("processed", !user.GetBool("allowEmailNotifications"))
if err := app.Dao().SaveRecord(record); err != nil {
errs = append(errs, err)
continue
}
}
return errors.Join(errs...)
}
func getProjectWithUsersByComment(dao *daos.Dao, comment *models.Record) (*models.Record, error) {
clone := comment.CleanCopy() // create a copy to avoid modifying the original record
if errs := dao.ExpandRecord(clone, []string{"screen.prototype.project.users"}, nil); len(errs) > 0 {
return nil, fmt.Errorf("failed to expand: %v", errs)
}
// extract the project from the expanded relations
model := clone
rels := []string{"screen", "prototype", "project"}
for _, rel := range rels {
model = model.ExpandedOne(rel)
if model == nil {
return nil, fmt.Errorf("missing comment relation %s", rel)
}
}
return model, nil
}
var mentionRegex = regexp.MustCompile(`[@+]([\w\.\-\@]+)`)
func extractMentions(message string) []string {
matches := mentionRegex.FindAllStringSubmatch(message, -1)
result := make([]string, 0, len(matches))
for _, match := range matches {
if len(match) != 2 {
continue
}
if m := strings.TrimSpace(match[1]); m != "" {
result = append(result, m)
}
}
return result
}
// -------------------------------------------------------------------
func processUnreadNotifications(app core.App, limit int) error {
notifications, err := findUnreadNotifications(app.Dao(), limit)
if err != nil {
return fmt.Errorf("failed to fetch unread notifications: %w", err)
}
g := new(errgroup.Group)
g.SetLimit(100)
for userId, notifications := range notifications {
userId := userId
notifications := notifications
g.Go(func() error {
if err := sendUnreadEmail(app, notifications, userId); err != nil {
return err
}
var errs []error
for _, n := range notifications {
n.Set("processed", true)
if err := app.Dao().SaveRecord(n); err != nil {
errs = append(errs, err)
}
}
return errors.Join(errs...)
})
}
if err := g.Wait(); err != nil {
return fmt.Errorf("failed to send all unread notifications: %w", err)
}
return nil
}
func findUnreadNotifications(dao *daos.Dao, limit int) (map[string][]*models.Record, error) {
notifications := []*models.Record{}
// the notification must be created at least 2 minutes ago
minDate := time.Now().Add(-2 * time.Minute).UTC().Format(types.DefaultDateLayout)
query := dao.RecordQuery("notifications").
AndWhere(dbx.NewExp("created <= {:created}", dbx.Params{"created": minDate})).
AndWhere(dbx.HashExp{
"read": false,
"processed": false,
}).
OrderBy("user ASC", "created ASC").
Limit(int64(limit))
if err := query.All(¬ifications); err != nil {
return nil, err
}
result := map[string][]*models.Record{}
for _, n := range notifications {
user := n.GetString("user")
result[user] = append(result[user], n)
}
return result, nil
}