-
Notifications
You must be signed in to change notification settings - Fork 246
/
database.go
315 lines (273 loc) · 10.3 KB
/
database.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
package notificationssettings
import (
"database/sql"
"fmt"
)
type settingType int
const (
stringType settingType = iota
integerType
boolType
)
// Columns' names
type column string
const (
columnTextValue column = "text_value"
columnIntValue column = "int_value"
columnBoolValue column = "bool_value"
columnExMuteAllMessages column = "ex_mute_all_messages"
columnExPersonalMentions column = "ex_personal_mentions"
columnExGlobalMentions column = "ex_global_mentions"
columnExOtherMessages column = "ex_other_messages"
)
// Static ids we use for notifications settings
const (
keyAllowNotifications = "AllowNotifications"
keyOneToOneChats = "OneToOneChats"
keyGroupChats = "GroupChats"
keyPersonalMentions = "PersonalMentions"
keyGlobalMentions = "GlobalMentions"
keyAllMessages = "AllMessages"
keyContactRequests = "ContactRequests"
keyIdentityVerificationRequests = "IdentityVerificationRequests"
keySoundEnabled = "SoundEnabled"
keyVolume = "Volume"
keyMessagePreview = "MessagePreview"
)
// Possible values
const (
valueSendAlerts = "SendAlerts"
//valueDeliverQuietly = "DeliverQuietly" // currently unused
valueTurnOff = "TurnOff"
)
// Default values
const (
defaultAllowNotificationsValue = true
defaultOneToOneChatsValue = valueSendAlerts
defaultGroupChatsValue = valueSendAlerts
defaultPersonalMentionsValue = valueSendAlerts
defaultGlobalMentionsValue = valueSendAlerts
defaultAllMessagesValue = valueTurnOff
defaultContactRequestsValue = valueSendAlerts
defaultIdentityVerificationRequestsValue = valueSendAlerts
defaultSoundEnabledValue = true
defaultVolumeValue = 50
defaultMessagePreviewValue = 2
defaultExMuteAllMessagesValue = false
defaultExPersonalMentionsValue = valueSendAlerts
defaultExGlobalMentionsValue = valueSendAlerts
defaultExOtherMessagesValue = valueTurnOff
)
type NotificationsSettings struct {
db *sql.DB
}
func NewNotificationsSettings(db *sql.DB) *NotificationsSettings {
return &NotificationsSettings{
db: db,
}
}
func (ns *NotificationsSettings) buildSelectQuery(column column) string {
return fmt.Sprintf("SELECT %s FROM notifications_settings WHERE id = ?", column)
}
func (ns *NotificationsSettings) buildInsertOrUpdateQuery(isExemption bool, settingType settingType) string {
var values string
if isExemption {
values = "VALUES(?, 1, NULL, NULL, NULL, ?, ?, ?, ?)"
} else {
switch settingType {
case stringType:
values = "VALUES(?, 0, ?, NULL, NULL, NULL, NULL, NULL, NULL)"
case integerType:
values = "VALUES(?, 0, NULL, ?, NULL, NULL, NULL, NULL, NULL)"
case boolType:
values = "VALUES(?, 0, NULL, NULL, ?, NULL, NULL, NULL, NULL)"
}
}
return fmt.Sprintf(`INSERT INTO notifications_settings (
id,
exemption,
text_value,
int_value,
bool_value,
ex_mute_all_messages,
ex_personal_mentions,
ex_global_mentions,
ex_other_messages)
%s;`, values)
}
// Non exemption settings
func (ns *NotificationsSettings) GetAllowNotifications() (result bool, err error) {
err = ns.db.QueryRow(ns.buildSelectQuery(columnBoolValue), keyAllowNotifications).Scan(&result)
if err != nil && err == sql.ErrNoRows {
return defaultAllowNotificationsValue, err
}
return result, err
}
func (ns *NotificationsSettings) SetAllowNotifications(value bool) error {
_, err := ns.db.Exec(ns.buildInsertOrUpdateQuery(false, boolType), keyAllowNotifications, value, value)
return err
}
func (ns *NotificationsSettings) GetOneToOneChats() (result string, err error) {
err = ns.db.QueryRow(ns.buildSelectQuery(columnTextValue), keyOneToOneChats).Scan(&result)
if err != nil && err == sql.ErrNoRows {
return defaultOneToOneChatsValue, err
}
return result, err
}
func (ns *NotificationsSettings) SetOneToOneChats(value string) error {
_, err := ns.db.Exec(ns.buildInsertOrUpdateQuery(false, stringType), keyOneToOneChats, value, value)
return err
}
func (ns *NotificationsSettings) GetGroupChats() (result string, err error) {
err = ns.db.QueryRow(ns.buildSelectQuery(columnTextValue), keyGroupChats).Scan(&result)
if err != nil && err == sql.ErrNoRows {
return defaultGroupChatsValue, err
}
return result, err
}
func (ns *NotificationsSettings) SetGroupChats(value string) error {
_, err := ns.db.Exec(ns.buildInsertOrUpdateQuery(false, stringType), keyGroupChats, value, value)
return err
}
func (ns *NotificationsSettings) GetPersonalMentions() (result string, err error) {
err = ns.db.QueryRow(ns.buildSelectQuery(columnTextValue), keyPersonalMentions).Scan(&result)
if err != nil && err == sql.ErrNoRows {
return defaultPersonalMentionsValue, err
}
return result, err
}
func (ns *NotificationsSettings) SetPersonalMentions(value string) error {
_, err := ns.db.Exec(ns.buildInsertOrUpdateQuery(false, stringType), keyPersonalMentions, value, value)
return err
}
func (ns *NotificationsSettings) GetGlobalMentions() (result string, err error) {
err = ns.db.QueryRow(ns.buildSelectQuery(columnTextValue), keyGlobalMentions).Scan(&result)
if err != nil && err == sql.ErrNoRows {
return defaultGlobalMentionsValue, err
}
return result, err
}
func (ns *NotificationsSettings) SetGlobalMentions(value string) error {
_, err := ns.db.Exec(ns.buildInsertOrUpdateQuery(false, stringType), keyGlobalMentions, value, value)
return err
}
func (ns *NotificationsSettings) GetAllMessages() (result string, err error) {
err = ns.db.QueryRow(ns.buildSelectQuery(columnTextValue), keyAllMessages).Scan(&result)
if err != nil && err == sql.ErrNoRows {
return defaultAllMessagesValue, err
}
return result, err
}
func (ns *NotificationsSettings) SetAllMessages(value string) error {
_, err := ns.db.Exec(ns.buildInsertOrUpdateQuery(false, stringType), keyAllMessages, value, value)
return err
}
func (ns *NotificationsSettings) GetContactRequests() (result string, err error) {
err = ns.db.QueryRow(ns.buildSelectQuery(columnTextValue), keyContactRequests).Scan(&result)
if err != nil && err == sql.ErrNoRows {
return defaultContactRequestsValue, err
}
return result, err
}
func (ns *NotificationsSettings) SetContactRequests(value string) error {
_, err := ns.db.Exec(ns.buildInsertOrUpdateQuery(false, stringType), keyContactRequests, value, value)
return err
}
func (ns *NotificationsSettings) GetIdentityVerificationRequests() (result string, err error) {
err = ns.db.QueryRow(ns.buildSelectQuery(columnTextValue), keyIdentityVerificationRequests).Scan(&result)
if err != nil && err == sql.ErrNoRows {
return defaultIdentityVerificationRequestsValue, err
}
return result, err
}
func (ns *NotificationsSettings) SetIdentityVerificationRequests(value string) error {
_, err := ns.db.Exec(ns.buildInsertOrUpdateQuery(false, stringType), keyIdentityVerificationRequests, value, value)
return err
}
func (ns *NotificationsSettings) GetSoundEnabled() (result bool, err error) {
err = ns.db.QueryRow(ns.buildSelectQuery(columnBoolValue), keySoundEnabled).Scan(&result)
if err != nil && err == sql.ErrNoRows {
return defaultSoundEnabledValue, err
}
return result, err
}
func (ns *NotificationsSettings) SetSoundEnabled(value bool) error {
_, err := ns.db.Exec(ns.buildInsertOrUpdateQuery(false, boolType), keySoundEnabled, value, value)
return err
}
func (ns *NotificationsSettings) GetVolume() (result int, err error) {
err = ns.db.QueryRow(ns.buildSelectQuery(columnIntValue), keyVolume).Scan(&result)
if err != nil && err == sql.ErrNoRows {
return defaultVolumeValue, err
}
return result, err
}
func (ns *NotificationsSettings) SetVolume(value int) error {
_, err := ns.db.Exec(ns.buildInsertOrUpdateQuery(false, integerType), keyVolume, value, value)
return err
}
func (ns *NotificationsSettings) GetMessagePreview() (result int, err error) {
err = ns.db.QueryRow(ns.buildSelectQuery(columnIntValue), keyMessagePreview).Scan(&result)
if err != nil && err == sql.ErrNoRows {
return defaultMessagePreviewValue, err
}
return result, err
}
func (ns *NotificationsSettings) SetMessagePreview(value int) error {
_, err := ns.db.Exec(ns.buildInsertOrUpdateQuery(false, integerType), keyMessagePreview, value, value)
return err
}
// Exemption settings
func (ns *NotificationsSettings) GetExMuteAllMessages(id string) (result bool, err error) {
err = ns.db.QueryRow(ns.buildSelectQuery(columnExMuteAllMessages), id).Scan(&result)
if err != nil && err == sql.ErrNoRows {
return defaultExMuteAllMessagesValue, nil
}
return result, err
}
func (ns *NotificationsSettings) GetExPersonalMentions(id string) (result string, err error) {
err = ns.db.QueryRow(ns.buildSelectQuery(columnExPersonalMentions), id).Scan(&result)
if err != nil && err == sql.ErrNoRows {
return defaultExPersonalMentionsValue, nil
}
return result, err
}
func (ns *NotificationsSettings) GetExGlobalMentions(id string) (result string, err error) {
err = ns.db.QueryRow(ns.buildSelectQuery(columnExGlobalMentions), id).Scan(&result)
if err != nil && err == sql.ErrNoRows {
return defaultExGlobalMentionsValue, nil
}
return result, err
}
func (ns *NotificationsSettings) GetExOtherMessages(id string) (result string, err error) {
err = ns.db.QueryRow(ns.buildSelectQuery(columnExOtherMessages), id).Scan(&result)
if err != nil && err == sql.ErrNoRows {
return defaultExOtherMessagesValue, nil
}
return result, err
}
func (ns *NotificationsSettings) SetExemptions(id string, muteAllMessages bool, personalMentions string,
globalMentions string, otherMessages string) error {
_, err := ns.db.Exec(ns.buildInsertOrUpdateQuery(true, stringType), id, muteAllMessages, personalMentions, globalMentions,
otherMessages)
return err
}
func (ns *NotificationsSettings) DeleteExemptions(id string) error {
switch id {
case
keyAllowNotifications,
keyOneToOneChats,
keyGroupChats,
keyPersonalMentions,
keyGlobalMentions,
keyAllMessages,
keyContactRequests,
keyIdentityVerificationRequests,
keySoundEnabled,
keyVolume,
keyMessagePreview:
return fmt.Errorf("%s, static notifications settings cannot be deleted", id)
}
_, err := ns.db.Exec("DELETE FROM notifications_settings WHERE id = ?", id)
return err
}