-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotification.go
213 lines (184 loc) · 5.3 KB
/
notification.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
package main
import (
"context"
"encoding/hex"
"log"
"math"
"strconv"
"strings"
"time"
"git.maharshi.ninja/root/rss2email/structures"
"github.com/mmcdole/gofeed"
smtp "github.com/xhit/go-simple-mail/v2"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo/options"
"golang.org/x/sync/errgroup"
)
var (
tickerTime = 10 * time.Minute
tickerTimeHalf = tickerTime / 2
batchSize int64 = 1000
)
type FeedWithUser struct {
*structures.Feed `bson:",inline"`
OwnerList []*structures.User `bson:"owner_list"`
}
func (a *app) sendEmailForItem(feed *structures.Feed, user *structures.User, item *gofeed.Item) error {
bldr := new(strings.Builder)
// Not bothering to use the localizer or a template (because templates are painful) and because the locale isn't known, so we have to use English, unfortunately
bldr.WriteString("New post on ")
bldr.WriteString(feed.Name)
bldr.WriteString(": ")
bldr.WriteString(item.Title)
subject := bldr.String()
bldr.WriteString("\nURL: ")
bldr.WriteString(item.Link)
bldr.WriteRune('\n')
if len(item.Links) != 0 {
bldr.WriteString("Additional Links:\n")
for num, link := range item.Links {
bldr.WriteString(strconv.Itoa(num + 1))
bldr.WriteString(". ")
bldr.WriteString(link)
bldr.WriteRune('\n')
}
}
bldr.WriteString("\n\n")
bldr.WriteString(item.Content)
body := bldr.String()
msg := smtp.NewMSG()
msg.SetFrom(a.config.EmailConfig.FromAddr)
msg.SetSubject(subject)
err := setEmailToAddress(msg, user.Email)
if err != nil {
return err
}
msg.SetBody(smtp.TextPlain, body)
conn, err := a.emailClient.Connect()
if err != nil {
return err
}
err = msg.Send(conn)
if err != nil {
return err
}
return nil
}
func (a *app) onNotificationTick(tym time.Time) {
totalCount, err := a.feeds.CountDocuments(context.TODO(), bson.M{})
if err != nil {
panic(err)
}
timeUnix := tym.UnixNano()
batches := int64(math.Ceil(float64(totalCount) / float64(batchSize)))
log.Printf("Processing %d batches\n", batches)
// outer:
for counter := int64(0); counter != batches; counter++ {
var feedList []FeedWithUser
log.Printf("Processing %d\n", counter)
crsr, err := a.feeds.Aggregate(context.TODO(), bson.A{
bson.M{
"$limit": batchSize,
},
bson.M{
"$skip": counter * batchSize,
},
bson.M{
"$lookup": bson.M{
"from": "users",
"localField": "owner_id",
"foreignField": "_id",
"as": "owner_list",
},
},
}, options.Aggregate().SetAllowDiskUse(true).SetBatchSize(1000))
if err != nil {
panic(err)
}
err = crsr.All(context.TODO(), &feedList)
if err != nil {
panic(err)
}
grp := new(errgroup.Group)
for _, _feedDoc := range feedList {
feedDoc := _feedDoc
if len(feedDoc.OwnerList) == 0 {
log.Printf("WARNING! Orphaned feed: %s\n", hex.EncodeToString(feedDoc.ID[:]))
continue
}
diff := time.Duration(timeUnix) % feedDoc.Frequency
// If diff is greater than tickerTime or less than -tickerTime, skip because it's not relevant
if diff >= tickerTime || diff <= -tickerTime {
continue
}
if !feedDoc.OwnerList[0].EmailVerified {
continue
}
grp.Go(func() error {
feed, err := a.feedParser.ParseURL(feedDoc.URL)
if err != nil {
log.Printf("Couldn't fetch %s\n", hex.EncodeToString(feedDoc.ID[:]))
return nil // doesn't need to interrupt the fetching
}
for _, item := range feed.Items {
// Do NOT report items created before the feed creation unless NotifyOldItems is set to true
if feedDoc.CreatedAt.After(*item.PublishedParsed) && a.config.NotifyOldItems == false {
continue
}
exists, err := a.seenItems.CountDocuments(context.TODO(), bson.M{
"feed_id": feedDoc.ID,
"guid": item.GUID,
})
if err != nil {
log.Printf("Tried to find %s (%s), failed with %s\n", item.GUID, hex.EncodeToString(feedDoc.ID[:]), err.Error())
continue
}
if exists != 0 {
continue
}
err = a.sendEmailForItem(feedDoc.Feed, feedDoc.OwnerList[0], item)
if err != nil {
log.Printf("Failed while sending email for %s (%s), failed with %s\n", item.GUID, hex.EncodeToString(feedDoc.ID[:]), err.Error())
continue
}
_, err = a.seenItems.InsertOne(context.TODO(), structures.SeenItem{
ID: primitive.NewObjectID(),
FeedID: feedDoc.ID,
GUID: item.GUID,
Timestamp: time.Now(),
})
if err != nil {
log.Printf("Failed while inserting seen item for %s (%s), failed with %s\n", item.GUID, hex.EncodeToString(feedDoc.ID[:]), err.Error())
continue
}
}
_, err = a.feeds.UpdateByID(context.TODO(), feedDoc.ID, bson.M{
"$set": bson.M{
"last_fetched": time.Now(),
},
})
if err != nil {
log.Printf("Failed while updating last_fetched for %s, failed with %s\n", hex.EncodeToString(feedDoc.ID[:]), err.Error())
}
return nil
})
}
err = grp.Wait()
if err != nil {
panic(err)
}
err = crsr.Close(context.TODO())
if err != nil {
log.Printf("Failed while closing the cursor: %s\n", err.Error())
return
}
}
}
func (a *app) notificationLoop() {
go a.onNotificationTick(time.Now())
ticker := time.NewTicker(tickerTime)
for t := range ticker.C {
a.onNotificationTick(t)
}
}