-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
notifs.go
176 lines (148 loc) · 4.02 KB
/
notifs.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 main
import (
"fmt"
"strconv"
fcm "github.com/NaySoftware/go-fcm"
"github.com/underlx/disturbancesmlx/types"
)
var enableStatusNotifs = true
var enableAnnouncementNotifs = true
func init() {
go func(newNotifChan <-chan types.StatusNotification) {
for sn := range newNotifChan {
SendNotificationForDisturbance(sn.Disturbance, sn.Status)
}
}(types.NewStatusNotification)
}
// SendNotificationForDisturbance sends a FCM notification for this disturbance about the specified status
func SendNotificationForDisturbance(d *types.Disturbance, s *types.Status) {
if !enableStatusNotifs {
return
}
downtimeStr := "false"
if s.IsDowntime {
downtimeStr = "true"
}
officialStr := "false"
if s.Source.Official {
officialStr = "true"
}
data := map[string]string{
"network": d.Line.Network.ID,
"line": d.Line.ID,
"disturbance": d.ID,
"status": s.Status,
"downtime": downtimeStr,
"official": officialStr,
"msgType": string(s.MsgType),
}
if fcmcl == nil {
// too soon
return
}
mainLog.Println("Sending notification for disturbance " + d.ID + ": " + s.Status)
if DEBUG {
fcmcl.NewFcmMsgTo("/topics/disturbances-debug", data)
} else {
fcmcl.NewFcmMsgTo("/topics/disturbances", data)
}
fcmcl.SetCollapseKey(d.ID)
fcmcl.SetPriority(fcm.Priority_HIGH)
_, err := fcmcl.Send()
if err != nil {
mainLog.Println(err)
}
}
// SendNotificationForAnnouncement sends a FCM notification for the specified announcement
func SendNotificationForAnnouncement(a *types.Announcement) {
if !enableAnnouncementNotifs {
return
}
data := map[string]string{
"network": a.Network.ID,
"title": a.Title,
"body": a.Body,
"url": a.URL,
"source": a.Source,
}
if fcmcl == nil {
// too soon
return
}
mainLog.Println("Sending notification for announcement: " + a.Title)
if DEBUG {
fcmcl.NewFcmMsgTo("/topics/announcements-debug-"+a.Source, data)
} else {
fcmcl.NewFcmMsgTo("/topics/announcements-"+a.Source, data)
}
fcmcl.SetPriority(fcm.Priority_HIGH)
_, err := fcmcl.Send()
if err != nil {
mainLog.Println(err)
}
}
// SendMetaBroadcast sends a FCM message containing actions to execute on all clients
func SendMetaBroadcast(shardID, shardMax int, id, broadcastType, forVersions, forLocales string, args ...[2]string) {
if fcmcl == nil {
// too soon
return
}
data := map[string]string{
"id": id,
"type": broadcastType,
}
if shardID > 0 && shardMax > 0 {
data["shardID"] = strconv.Itoa(shardID)
data["shardMax"] = strconv.Itoa(shardMax)
}
if forVersions != "" {
data["versions"] = forVersions
}
if forLocales != "" {
data["locales"] = forLocales
}
for _, arg := range args {
data[arg[0]] = arg[1]
}
mainLog.Println("Sending meta-broadcast " + id)
if DEBUG {
fcmcl.NewFcmMsgTo("/topics/broadcasts-debug", data)
} else {
fcmcl.NewFcmMsgTo("/topics/broadcasts", data)
}
fcmcl.SetPriority(fcm.Priority_HIGH)
_, err := fcmcl.Send()
if err != nil {
mainLog.Println(err)
}
}
// SendPersonalNotification sends a FCM message to a specific user
func SendPersonalNotification(pair *types.APIPair, msgType string, data map[string]string) {
mainLog.Println("Sending personal notification to pair " + pair.Key)
data["type"] = msgType
topicName := fmt.Sprintf("/topics/pair-%s", pair.Key)
if DEBUG {
fcmcl.NewFcmMsgTo(topicName+"-debug", data)
} else {
fcmcl.NewFcmMsgTo(topicName, data)
}
fcmcl.SetPriority(fcm.Priority_HIGH)
_, err := fcmcl.Send()
if err != nil {
mainLog.Println(err)
}
}
func handleControlNotifs(notiftype string, enable bool) {
switch notiftype {
case "status":
enableStatusNotifs = enable
case "announcements":
enableAnnouncementNotifs = enable
}
}
// SendNotificationForContest does nothing (stub method to be overriden on runtime by our awesome scripting system)
func SendNotificationForContest(a *types.Announcement) {
}
// SendNotificationForInstitutionalPost does nothing (stub method to be overriden on runtime by our awesome scripting system)
func SendNotificationForInstitutionalPost(a *types.Announcement) {
}