-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnotifications.go
299 lines (240 loc) · 7.21 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
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
package main
import (
"errors"
"github.com/bwmarrin/discordgo"
"sync"
"time"
)
// Notifications struct
type Notifications struct {
db *DBHandler
querylocker sync.RWMutex
}
// Notification struct
type Notification struct {
ID string `storm:"id"`
Message string
}
// ChannelNotification struct
type ChannelNotification struct {
ID string `storm:"id"`
Notification string // The ID of our notification message
ChannelID string `storm:"index"` // Limit our notifications per channel
LastRun time.Time
Timeout string `storm:"index"`
}
// AddNotificationToDB function
func (h *Notifications) AddNotificationToDB(notification Notification) (err error) {
h.querylocker.Lock()
defer h.querylocker.Unlock()
db := h.db.rawdb.From("Notifications")
err = db.Save(¬ification)
return err
}
// RemoveNotificationFromDB function
func (h *Notifications) RemoveNotificationFromDB(notification Notification) (err error) {
h.querylocker.Lock()
defer h.querylocker.Unlock()
db := h.db.rawdb.From("Notifications")
err = db.DeleteStruct(¬ification)
return err
}
// RemoveNotificationFromDBByID function
func (h *Notifications) RemoveNotificationFromDBByID(messageid string, s *discordgo.Session) (err error) {
notification, err := h.GetNotificationFromDB(messageid)
if err != nil {
return err
}
channelnotifications, err := h.GetAllChannelNotifications()
if err != nil {
return err
}
found := false
channelsinuse := ""
for _, channelnotification := range channelnotifications {
if channelnotification.Notification == notification.ID {
found = true
mention, err := MentionChannel(channelnotification.ChannelID, s)
if err != nil {
return err
}
if channelsinuse != "" {
channelsinuse = channelsinuse + ", " + mention
} else {
channelsinuse = mention
}
}
}
if found {
return errors.New("Could not remove message from db, it is currently in use by the following channel(s):\n" + channelsinuse)
}
err = h.RemoveNotificationFromDB(notification)
if err != nil {
return err
}
return nil
}
// GetNotificationFromDB function
func (h *Notifications) GetNotificationFromDB(messageid string) (notification Notification, err error) {
notifications, err := h.GetAllNotifications()
if err != nil {
return notification, err
}
for _, i := range notifications {
if i.ID == messageid {
return i, nil
}
}
return notification, errors.New("No record found")
}
// GetAllNotifications function
func (h *Notifications) GetAllNotifications() (notificationlist []Notification, err error) {
h.querylocker.Lock()
defer h.querylocker.Unlock()
db := h.db.rawdb.From("Notifications")
err = db.All(¬ificationlist)
if err != nil {
return notificationlist, err
}
return notificationlist, nil
}
// AddChannelNotificationToDB function
func (h *Notifications) AddChannelNotificationToDB(channelnotification ChannelNotification) (err error) {
h.querylocker.Lock()
defer h.querylocker.Unlock()
db := h.db.rawdb.From("ChannelNotifications")
err = db.Save(&channelnotification)
return err
}
// RemoveChannelNotificationFromDB function
func (h *Notifications) RemoveChannelNotificationFromDB(channelnotification ChannelNotification) (err error) {
h.querylocker.Lock()
defer h.querylocker.Unlock()
db := h.db.rawdb.From("ChannelNotifications")
err = db.DeleteStruct(&channelnotification)
return err
}
// UpdateChannelNotification function
func (h *Notifications) UpdateChannelNotification(channelnotification ChannelNotification) (err error) {
h.querylocker.Lock()
defer h.querylocker.Unlock()
db := h.db.rawdb.From("ChannelNotifications")
err = db.Update(&channelnotification)
if err != nil {
return err
}
return nil
}
// RemoveChannelNotificationFromDBByID function
func (h *Notifications) RemoveChannelNotificationFromDBByID(channelnotificationid string, channelid string) (err error) {
channelnotification, err := h.GetChannelNotificationFromDB(channelnotificationid, channelid)
if err != nil {
return err
}
err = h.RemoveChannelNotificationFromDB(channelnotification)
if err != nil {
return err
}
return nil
}
// GetChannelNotificationFromDB function
func (h *Notifications) GetChannelNotificationFromDB(channelnotificationid string, channelid string) (channelnotification ChannelNotification, err error) {
channelnotifications, err := h.GetAllChannelNotifications()
if err != nil {
return channelnotification, err
}
for _, i := range channelnotifications {
if i.ID == channelnotificationid {
if i.ChannelID == channelid {
return i, nil
}
}
}
return channelnotification, errors.New("Channel notification " + channelnotificationid + " does not exist for this channel!")
}
// GetAllChannelNotifications function
func (h *Notifications) GetAllChannelNotifications() (channelnotificationlist []ChannelNotification, err error) {
h.querylocker.Lock()
defer h.querylocker.Unlock()
db := h.db.rawdb.From("ChannelNotifications")
err = db.All(&channelnotificationlist)
if err != nil {
return channelnotificationlist, err
}
return channelnotificationlist, nil
}
// FlushChannelNotifications function
func (h *Notifications) FlushChannelNotifications(channelid string) (err error) {
channelnotifications, err := h.GetAllChannelNotifications()
if err != nil {
return err
}
for _, notification := range channelnotifications {
err = h.RemoveChannelNotificationFromDB(notification)
if err != nil {
return err
}
}
return nil
}
// CreateChannelNotification function
func (h *Notifications) CreateChannelNotification(id string, notificationid string, channelid string, timeout string) (err error) {
_, err = h.GetNotificationFromDB(notificationid)
if err != nil {
return errors.New("Notification ID: " + notificationid + " - not found")
}
channelnotifications, err := h.GetAllChannelNotifications()
if err != nil {
return err
}
for _, i := range channelnotifications {
idmatches := false
channelmatches := false
if i.Notification == notificationid {
idmatches = true
}
if i.ChannelID == channelid {
channelmatches = true
}
if idmatches && channelmatches {
return errors.New("Notification is already enabled for this channel")
}
}
channelnotification := ChannelNotification{ID: id, Notification: notificationid, ChannelID: channelid, LastRun: time.Now(), Timeout: timeout}
err = h.AddChannelNotificationToDB(channelnotification)
if err != nil {
return err
}
return nil
}
// GetNotificationLinkedChannels function
func (h *Notifications) GetNotificationLinkedChannels(messageid string, s *discordgo.Session) (channels string, err error) {
notification, err := h.GetNotificationFromDB(messageid)
if err != nil {
return channels, err
}
channelnotifications, err := h.GetAllChannelNotifications()
if err != nil {
return channels, err
}
found := false
channelsinuse := ""
for _, channelnotification := range channelnotifications {
if channelnotification.Notification == notification.ID {
found = true
mention, err := MentionChannel(channelnotification.ChannelID, s)
if err != nil {
return channels, err
}
if channelsinuse != "" {
channelsinuse = channelsinuse + ", " + mention
} else {
channelsinuse = mention
}
}
}
if found {
return channelsinuse, nil
}
return channels, nil
}