-
-
Notifications
You must be signed in to change notification settings - Fork 72
/
notification_manager.go
316 lines (246 loc) · 10.3 KB
/
notification_manager.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
316
package servermanager
import (
"fmt"
"net/url"
"os"
"strconv"
"strings"
"time"
"github.com/hako/durafmt"
"github.com/sirupsen/logrus"
)
type NotificationDispatcher interface {
HasNotificationReminders() bool
GetNotificationReminders() []int
SendMessage(title string, msg string) error
SendMessageWithLink(title string, msg string, linkText string, link *url.URL) error
SendRaceStartMessage(config ServerConfig, event RaceEvent) error
SendRaceScheduledMessage(event *CustomRace, date time.Time) error
SendRaceCancelledMessage(event *CustomRace, date time.Time) error
SendRaceReminderMessage(event *CustomRace, timer int) error
SendChampionshipReminderMessage(championship *Championship, event *ChampionshipEvent, timer int) error
SendRaceWeekendReminderMessage(raceWeekend *RaceWeekend, session *RaceWeekendSession, timer int) error
SaveServerOptions(oldServerOpts *GlobalServerConfig, newServerOpts *GlobalServerConfig) error
}
// NotificationManager is the generic notification handler, which calls the individual notification
// managers. Initially, only a Discord manager is implemented.
type NotificationManager struct {
discordManager *DiscordManager
carManager *CarManager
store Store
testing bool
}
func NewNotificationManager(discord *DiscordManager, cars *CarManager, store Store) *NotificationManager {
return &NotificationManager{
discordManager: discord,
carManager: cars,
store: store,
testing: os.Getenv("NOTIFICATION_TEST_MODE") == "true",
}
}
// check to see if any notification handlers need to process option changes
func (nm *NotificationManager) SaveServerOptions(oldServerOpts *GlobalServerConfig, newServerOpts *GlobalServerConfig) error {
return nm.discordManager.SaveServerOptions(oldServerOpts, newServerOpts)
}
func (nm *NotificationManager) Stop() error {
return nm.discordManager.Stop()
}
// HasNotificationReminders just tells us if we need to do any reminder scheduling
func (nm *NotificationManager) HasNotificationReminders() bool {
reminders := nm.GetNotificationReminders()
return len(reminders) > 0
}
// GetNotificationReminders returns an array of int timers
// Doesn't return errors, just omits anything it doesn't like and logs errors
func (nm *NotificationManager) GetNotificationReminders() []int {
var reminders []int
serverOpts, err := nm.store.LoadServerOptions()
if err != nil {
logrus.WithError(err).Errorf("couldn't load server options")
return reminders
}
timers := strings.Split(serverOpts.NotificationReminderTimers, ",")
for _, a := range timers {
if strings.TrimSpace(a) == "" {
continue
}
i, err := strconv.Atoi(strings.TrimSpace(a))
if err != nil {
logrus.WithError(err).Errorf("couldn't convert notification time to int")
continue
}
if i == 0 {
continue
}
reminders = append(reminders, i)
}
return reminders
}
// SendMessage sends a message (surprise surprise)
func (nm *NotificationManager) SendMessage(title string, msg string) error {
var err error
// Call all message senders here ... atm just discord. The manager will know if it's enabled or not, so just call it
if !nm.testing {
err = nm.discordManager.SendMessage(title, msg)
}
return err
}
// SendMessageWithLink sends a message with an embedded CM join link
func (nm *NotificationManager) SendMessageWithLink(title string, msg string, linkText string, link *url.URL) error {
var err error
// Call all message senders here ... atm just discord. The manager will know if it's enabled or not, so just call it
if !nm.testing {
err = nm.discordManager.SendMessageWithLink(title, msg, linkText, link)
}
return err
}
// SendRaceStartMessage sends a message as a race session is started
func (nm *NotificationManager) SendRaceStartMessage(config ServerConfig, event RaceEvent) error {
serverOpts, err := nm.store.LoadServerOptions()
if err != nil {
logrus.WithError(err).Errorf("couldn't load server options, skipping notification")
return err
}
msg := ""
eventName := event.EventName()
trackInfo := trackSummary(config.CurrentRaceConfig.Track, config.CurrentRaceConfig.TrackLayout)
if eventName != "" {
msg = fmt.Sprintf("%s event at %s is starting now", eventName, trackInfo)
} else {
msg = fmt.Sprintf("Event at %s is starting now", trackInfo)
}
msg += fmt.Sprintf("\nServer: %s", serverOpts.Name)
if serverOpts.ShowPasswordInNotifications == 1 {
passwordString := "\nNo password"
if event.OverrideServerPassword() {
if event.ReplacementServerPassword() != "" {
passwordString = fmt.Sprintf("\nPassword is '%s' (no quotes)", event.ReplacementServerPassword())
}
} else if config.GlobalServerConfig.Password != "" {
passwordString = fmt.Sprintf("\nPassword is '%s' (no quotes)", config.GlobalServerConfig.Password)
}
msg += passwordString
}
title := fmt.Sprintf("Event starting at %s", trackInfo)
if config.GlobalServerConfig.ShowContentManagerJoinLink == 1 {
link, err := getContentManagerJoinLink(config.GlobalServerConfig)
linkText := ""
if err != nil {
logrus.WithError(err).Errorf("could not get CM join link")
return nm.SendMessage(title, msg)
}
linkText = "Content Manager join link"
// delay sending message by 20 seconds to give server time to register with lobby so CM link works
time.AfterFunc(time.Duration(20)*time.Second, func() {
_ = nm.SendMessageWithLink(title, msg, linkText, link)
})
return nil
}
return nm.SendMessage(title, msg)
}
// GetCarList takes a ; sep string of cars from a race config, returns , sep of UI names with download links added
func (nm *NotificationManager) GetCarList(cars string) string {
var aCarNames []string
for _, carName := range strings.Split(cars, ";") {
car, err := nm.carManager.LoadCar(carName, nil)
if err != nil {
logrus.WithError(err).Warnf("Could not load car details for: %s", carName)
continue
}
if car.Details.DownloadURL != "" {
aCarNames = append(aCarNames, car.Details.Name+" ([download]("+car.Details.DownloadURL+"))")
} else {
aCarNames = append(aCarNames, car.Details.Name)
}
}
return strings.Join(aCarNames, ", ")
}
// GetTrackInfo returns the track summary with any download link appended
func (nm *NotificationManager) GetTrackInfo(track string, layout string, download bool) string {
trackInfo := trackSummary(track, layout)
if download {
trackLink := trackDownloadLink(track)
if trackLink != "" {
trackInfo += " ([download](" + trackLink + "))"
}
}
return trackInfo
}
// SendRaceScheduledMessage sends a notification when a race is scheduled
func (nm *NotificationManager) SendRaceScheduledMessage(event *CustomRace, date time.Time) error {
serverOpts, err := nm.store.LoadServerOptions()
if err != nil {
logrus.WithError(err).Errorf("couldn't load server options, skipping notification")
return err
}
if serverOpts.NotifyWhenScheduled != 1 {
return nil
}
msg := "A new event has been scheduled\n"
msg += fmt.Sprintf("Server: %s\n", serverOpts.Name)
eventName := event.EventName()
if eventName != "" {
msg += fmt.Sprintf("Event name: %s\n", eventName)
}
msg += fmt.Sprintf("Date: %s\n", date.Format("Mon, 02 Jan 2006 15:04:05 MST"))
carNames := nm.GetCarList(event.RaceConfig.Cars)
trackInfo := nm.GetTrackInfo(event.RaceConfig.Track, event.RaceConfig.TrackLayout, true)
msg += fmt.Sprintf("Track: %s\n", trackInfo)
msg += fmt.Sprintf("Car(s): %s\n", carNames)
title := fmt.Sprintf("Event scheduled at %s", nm.GetTrackInfo(event.RaceConfig.Track, event.RaceConfig.TrackLayout, false))
return nm.SendMessage(title, msg)
}
// SendRaceCancelledMessage sends a notification when a race is cancelled
func (nm *NotificationManager) SendRaceCancelledMessage(event *CustomRace, date time.Time) error {
serverOpts, err := nm.store.LoadServerOptions()
if err != nil {
logrus.WithError(err).Errorf("couldn't load server options, skipping notification")
return err
}
if serverOpts.NotifyWhenScheduled != 1 {
return nil
}
dateStr := date.Format("Mon, 02 Jan 2006 15:04:05 MST")
msg := "The following scheduled Event has been cancelled\n"
msg += fmt.Sprintf("Server: %s\n", serverOpts.Name)
eventName := event.EventName()
trackInfo := trackSummary(event.RaceConfig.Track, event.RaceConfig.TrackLayout)
if eventName != "" {
msg += fmt.Sprintf("Event name: %s\n", eventName)
}
msg += fmt.Sprintf("Date: %s\n", dateStr)
msg += fmt.Sprintf("Track: %s\n", trackInfo)
title := fmt.Sprintf("Event cancelled at %s", trackInfo)
return nm.SendMessage(title, msg)
}
// SendRaceReminderMessage sends a reminder a configurable number of minutes prior to a race starting
func (nm *NotificationManager) SendRaceReminderMessage(event *CustomRace, timer int) error {
msg := ""
trackInfo := nm.GetTrackInfo(event.RaceConfig.Track, event.RaceConfig.TrackLayout, true)
eventName := event.EventName()
carList := nm.GetCarList(event.RaceConfig.Cars)
reminder := durafmt.Parse(time.Duration(timer) * time.Minute).String()
if eventName != "" {
msg = fmt.Sprintf("%s event at %s starts in %s\nCars: %s", eventName, trackInfo, reminder, carList)
} else {
msg = fmt.Sprintf("Event at %s starts in %s\nCars: %s", trackInfo, reminder, carList)
}
title := fmt.Sprintf("Event reminder - %s", reminder)
return nm.SendMessage(title, msg)
}
// SendChampionshipReminderMessage sends a reminder a configurable number of minutes prior to a championship race starting
func (nm *NotificationManager) SendChampionshipReminderMessage(championship *Championship, event *ChampionshipEvent, timer int) error {
reminder := durafmt.Parse(time.Duration(timer) * time.Minute).String()
title := fmt.Sprintf("Event reminder - %s", reminder)
trackInfo := nm.GetTrackInfo(event.RaceSetup.Track, event.RaceSetup.TrackLayout, true)
msg := fmt.Sprintf("%s event at %s starts in %s", championship.Name, trackInfo, reminder)
return nm.SendMessage(title, msg)
}
// SendRaceWeekendReminderMessage sends a reminder a configurable number of minutes prior to a RaceWeekendSession starting
func (nm *NotificationManager) SendRaceWeekendReminderMessage(raceWeekend *RaceWeekend, session *RaceWeekendSession, timer int) error {
reminder := durafmt.Parse(time.Duration(timer) * time.Minute).String()
title := fmt.Sprintf("Event reminder - %s", reminder)
trackInfo := nm.GetTrackInfo(session.RaceConfig.Track, session.RaceConfig.TrackLayout, true)
msg := fmt.Sprintf("%s at %s (%s Race Weekend) starts in %s", session.Name(), raceWeekend.Name, trackInfo, reminder)
return nm.SendMessage(title, msg)
}