-
Notifications
You must be signed in to change notification settings - Fork 2
/
command_handler.go
774 lines (703 loc) · 25.9 KB
/
command_handler.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
package main
import (
"errors"
"fmt"
"regexp"
"strconv"
"strings"
"time"
"github.com/google/uuid"
log "github.com/sirupsen/logrus"
"maunium.net/go/mautrix"
mevent "maunium.net/go/mautrix/event"
mid "maunium.net/go/mautrix/id"
"github.com/beeper/standupbot/types"
)
type StandupFlowState int
const (
FlowNotStarted StandupFlowState = iota
Yesterday
Friday
Weekend
Today
Blockers
Notes
Confirm
Sent
Threads
ThreadsFriday
)
type StandupItem struct {
EventID mid.EventID
Body string
FormattedBody string
}
type StandupFlow struct {
FlowID uuid.UUID
State StandupFlowState
ReactableEvents []mid.EventID
PreviewEventId mid.EventID
ResendEventId *mid.EventID
Yesterday []StandupItem
Friday []StandupItem
Weekend []StandupItem
Today []StandupItem
Blockers []StandupItem
Notes []StandupItem
// Root events for threads
YesterdayThreadEvents []mid.EventID
FridayThreadEvents []mid.EventID
WeekendThreadEvents []mid.EventID
TodayThreadEvents []mid.EventID
BlockersThreadEvents []mid.EventID
NotesThreadEvents []mid.EventID
}
var currentStandupFlows map[mid.UserID]*StandupFlow = make(map[mid.UserID]*StandupFlow)
func BlankStandupFlow() *StandupFlow {
uuid, _ := uuid.NewUUID()
return &StandupFlow{
FlowID: uuid,
State: FlowNotStarted,
ReactableEvents: make([]mid.EventID, 0),
Yesterday: make([]StandupItem, 0),
Friday: make([]StandupItem, 0),
Weekend: make([]StandupItem, 0),
Today: make([]StandupItem, 0),
Blockers: make([]StandupItem, 0),
Notes: make([]StandupItem, 0),
YesterdayThreadEvents: make([]mid.EventID, 0),
FridayThreadEvents: make([]mid.EventID, 0),
WeekendThreadEvents: make([]mid.EventID, 0),
TodayThreadEvents: make([]mid.EventID, 0),
BlockersThreadEvents: make([]mid.EventID, 0),
NotesThreadEvents: make([]mid.EventID, 0),
}
}
func SendReaction(roomId mid.RoomID, eventID mid.EventID, reaction string) (resp *mautrix.RespSendEvent, err error) {
r, err := DoRetry("send reaction", func() (interface{}, error) {
return client.SendReaction(roomId, eventID, reaction)
})
if err != nil {
// give up
log.Errorf("Failed to send reaction to %s in %s: %s", eventID, roomId, err)
return nil, err
}
return r.(*mautrix.RespSendEvent), err
}
func SendHelp(roomId mid.RoomID) {
// send message to channel confirming join (retry 3 times)
noticeText := `COMMANDS:
* new -- prepare a new standup post
* show -- show the current standup post
* edit [Friday|Weekend|Yesterday|Today|Blockers|Notes] -- edit the given section of the standup post
* cancel -- cancel the current standup post
* undo -- undo sending the current standup post to the send room
* help -- show this help
* vanquish -- tell the bot to leave the room
* tz [timezone] -- show or set the timezone to use for configuring notifications
* notify [time]|stop -- show or set the time at which the standup notification will be sent
* room [room alias or ID] -- show or set the room where your standup notification will be sent
* threads [true|false] -- whether or not to use threads for composing standup posts
Version %s. Source code: https://gitlab.com/beeper/standupbot/`
noticeHtml := `<b>COMMANDS:</b>
<ul>
<li><b>new</b> — prepare a new standup post</li>
<li><b>show</b> — show the current standup post</li>
<li><b>edit [Friday|Weekend|Yesterday|Today|Blockers|Notes]</b> — edit the given section of the standup post</li>
<li><b>cancel</b> — cancel the current standup post</li>
<li><b>undo</b> — undo sending the current standup post to the send room</li>
<li><b>help</b> — show this help</li>
<li><b>vanquish</b> — tell the bot to leave the room</li>
<li><b>tz [timezone]</b> — show or set the timezone to use for configuring notifications</li>
<li><b>notify [time]|stop</b> — show or set the time at which the standup notification will be sent</li>
<li><b>room [room alias or ID]</b> — show or set the room where your standup notification will be sent</li>
<li><b>threads [true|false]</b> — whether or not to use threads for composing standup posts</li>
</ul>
Version %s. <a href="https://gitlab.com/beeper/standupbot/">Source code</a>.`
SendMessage(roomId, &mevent.MessageEventContent{
MsgType: mevent.MsgNotice,
Body: fmt.Sprintf(noticeText, VERSION),
Format: mevent.FormatHTML,
FormattedBody: fmt.Sprintf(noticeHtml, VERSION),
})
}
// Timezone
func HandleTimezone(roomId mid.RoomID, sender mid.UserID, params []string) {
stateKey := strings.TrimPrefix(sender.String(), "@")
if len(params) == 0 {
tzStr := "not set"
var tzSettingEventContent types.TzSettingEventContent
err := client.StateEvent(roomId, types.StateTzSetting, stateKey, &tzSettingEventContent)
if err == nil {
tzStr = tzSettingEventContent.TzString
}
noticeText := fmt.Sprintf("Timezone is set to %s", tzStr)
SendMessage(roomId, &mevent.MessageEventContent{MsgType: mevent.MsgNotice, Body: noticeText})
return
}
location, err := time.LoadLocation(params[0])
if err != nil {
errorMessageText := fmt.Sprintf("%s is not a recognized timezone. Use the name corresponding to a file in the IANA Time Zone database, such as 'America/New_York'", params[0])
SendMessage(roomId, &mevent.MessageEventContent{MsgType: mevent.MsgNotice, Body: errorMessageText})
}
_, err = client.SendStateEvent(roomId, types.StateTzSetting, stateKey, types.TzSettingEventContent{
TzString: location.String(),
})
noticeText := fmt.Sprintf("Timezone set to %s", location.String())
if err != nil {
noticeText = fmt.Sprintf("Failed setting timezone: %s\nCheck to make sure that standupbot is a mod/admin in the room!", err)
} else {
stateStore.SetTimezone(sender, location.String())
}
SendMessage(roomId, &mevent.MessageEventContent{MsgType: mevent.MsgNotice, Body: noticeText})
}
// Notify
func HandleNotify(roomId mid.RoomID, sender mid.UserID, params []string) {
stateKey := strings.TrimPrefix(sender.String(), "@")
if len(params) == 0 {
var notifyEventContent types.NotifyEventContent
err := client.StateEvent(roomId, types.StateNotify, stateKey, ¬ifyEventContent)
var noticeText string
if err != nil || notifyEventContent.MinutesAfterMidnight == nil {
noticeText = "Notification time is not set"
} else {
offset := time.Minute * time.Duration(*notifyEventContent.MinutesAfterMidnight)
offset = offset.Round(time.Minute)
noticeText = fmt.Sprintf("Notification time is set to %02d:%02d", int(offset.Hours()), int(offset.Minutes())%60)
}
SendMessage(roomId, &mevent.MessageEventContent{MsgType: mevent.MsgNotice, Body: noticeText})
return
}
if params[0] == "stop" {
_, err := client.SendStateEvent(roomId, types.StateNotify, stateKey, struct{}{})
noticeText := "Notifications successfully disabled"
if err != nil {
noticeText = "Failed to disable notifications"
}
stateStore.RemoveNotify(sender)
SendMessage(roomId, &mevent.MessageEventContent{MsgType: mevent.MsgNotice, Body: noticeText})
return
}
timeRe := regexp.MustCompile(`(\d\d?):?(\d\d)`)
groups := timeRe.FindStringSubmatch(params[0])
noticeText := ""
if groups == nil {
noticeText = fmt.Sprintf("%s is not a valid time. Please specify it in 24-hour time like: 13:30.", params[0])
} else {
hours, hoursErr := strconv.Atoi(groups[1])
minutes, minutesErr := strconv.Atoi(groups[2])
if hoursErr != nil || minutesErr != nil || hours < 0 || hours > 24 || minutes < 0 || minutes > 60 {
noticeText = fmt.Sprintf("%s is not a valid time. Please specify it in 24-hour time like: 13:30.", params[0])
} else {
noticeText = fmt.Sprintf("Notification time set to %02d:%02d", hours, minutes)
minutesAfterMidnight := minutes + hours*60
_, err := client.SendStateEvent(roomId, types.StateNotify, stateKey, types.NotifyEventContent{
MinutesAfterMidnight: &minutesAfterMidnight,
})
if err != nil {
noticeText = fmt.Sprintf("Failed setting notification time: %s\nCheck to make sure that standupbot is a mod/admin in the room!", err)
} else {
stateStore.SetNotify(sender, minutesAfterMidnight)
}
}
}
SendMessage(roomId, &mevent.MessageEventContent{MsgType: mevent.MsgNotice, Body: noticeText})
}
// Threads
func HandleThreads(roomID mid.RoomID, sender mid.UserID, params []string) {
if len(params) == 0 {
useThreads, err := stateStore.GetUseThreads(sender)
var noticeText string
if err != nil {
noticeText = "Use threads setting is not set. Will default to not using threads."
} else if useThreads {
noticeText = "Using threads is enabled."
} else {
noticeText = "Using threads is not enabled."
}
SendMessage(roomID, &mevent.MessageEventContent{MsgType: mevent.MsgNotice, Body: noticeText})
return
}
useThreadsStr := strings.ToLower(params[0])
var useThreads bool
if useThreadsStr == "true" {
useThreads = true
} else if useThreadsStr == "false" {
useThreads = false
} else {
noticeText := fmt.Sprintf("Failed setting use threads option: %s is not valid. Use 'true' or 'false'.", useThreadsStr)
SendMessage(roomID, &mevent.MessageEventContent{MsgType: mevent.MsgNotice, Body: noticeText})
return
}
stateKey := strings.TrimPrefix(sender.String(), "@")
_, err := client.SendStateEvent(roomID, types.StateUseThreads, stateKey, types.UseThreadsEventContent{
UseThreads: useThreads,
})
var noticeText string
if err != nil {
noticeText = fmt.Sprintf("Failed setting use threads option: %+v", err)
} else {
noticeText = fmt.Sprintf("Set use threads option to %s", useThreadsStr)
stateStore.SetUseThreads(sender, useThreads)
}
SendMessage(roomID, &mevent.MessageEventContent{MsgType: mevent.MsgNotice, Body: noticeText})
}
// Room
func HandleRoom(roomID mid.RoomID, event *mevent.Event, params []string) {
stateKey := strings.TrimPrefix(event.Sender.String(), "@")
if len(params) == 0 {
sendRoomID, err := stateStore.GetSendRoomId(event.Sender)
var noticeText string
if err != nil {
noticeText = "Send room not set"
} else {
noticeText = fmt.Sprintf("Send room is set to %s", sendRoomID)
}
SendMessage(roomID, &mevent.MessageEventContent{MsgType: mevent.MsgNotice, Body: noticeText})
return
}
roomIdToJoin := params[0]
serverName := ""
if len(params) > 1 {
serverName = params[1]
}
log.Info("Joining ", roomIdToJoin)
respJoinRoom, err := DoRetry("join room", func() (interface{}, error) {
return client.JoinRoom(roomIdToJoin, serverName, nil)
})
noticeText := ""
if err != nil {
noticeText = fmt.Sprintf("Could not join room %s: %s", roomIdToJoin, err)
} else {
sendRoomID := respJoinRoom.(*mautrix.RespJoinRoom).RoomID
noticeText = fmt.Sprintf("Joined %s and set that as your send room", roomIdToJoin)
_, err := client.SendStateEvent(roomID, types.StateSendRoom, stateKey, types.SendRoomEventContent{
SendRoomID: sendRoomID,
})
if err != nil {
noticeText = fmt.Sprintf("Failed setting send room: %s\nCheck to make sure that standupbot is a mod/admin in the room!", err)
} else {
stateStore.SetSendRoomId(event.Sender, sendRoomID)
}
}
SendMessage(roomID, &mevent.MessageEventContent{MsgType: mevent.MsgNotice, Body: noticeText})
if currentFlow, found := currentStandupFlows[event.Sender]; found && currentFlow.State == Confirm {
client.RedactEvent(event.RoomID, currentFlow.PreviewEventId)
ShowMessagePreview(event.RoomID, event.Sender, currentFlow, false)
}
}
func EditPreview(roomID mid.RoomID, userID mid.UserID, flow *StandupFlow) []mid.EventID {
newPost := FormatPost(userID, flow, true, true, false)
resp, _ := SendMessage(roomID, &mevent.MessageEventContent{
MsgType: mevent.MsgText,
Body: " * " + newPost.Body,
Format: mevent.FormatHTML,
FormattedBody: " * " + newPost.FormattedBody,
RelatesTo: &mevent.RelatesTo{
Type: mevent.RelReplace,
EventID: flow.PreviewEventId,
},
NewContent: newPost,
})
return append(flow.ReactableEvents, resp.EventID)
}
func getCommandParts(body string) ([]string, error) {
userId := mid.UserID(configuration.Username)
localpart, _, _ := userId.ParseAndDecode()
// Valid command strings include:
// standupbot: foo
// !su foo
// !standupbot foo
// @standupbot foo
// @standupbot: foo
validCommandRegexes := []*regexp.Regexp{
regexp.MustCompile(fmt.Sprintf("^%s:(.*)$", localpart)),
regexp.MustCompile(fmt.Sprintf("^@%s:?(.*)$", localpart)),
regexp.MustCompile("^!standupbot$"),
regexp.MustCompile("^!standupbot:? (.*)$"),
regexp.MustCompile("^!su$"),
regexp.MustCompile("^!su:? (.*)$"),
}
body = strings.TrimSpace(body)
isCommand := false
commandParts := []string{}
for _, commandRe := range validCommandRegexes {
match := commandRe.FindStringSubmatch(body)
if match != nil {
isCommand = true
if len(match) > 1 {
commandParts = strings.Split(match[1], " ")
} else {
commandParts = []string{"help"}
}
break
}
}
if !isCommand {
return []string{}, errors.New("Not a command")
}
return commandParts, nil
}
func tryEditListItem(standupList []StandupItem, editEventID mid.EventID, newContent *mevent.MessageEventContent) bool {
for i, item := range standupList {
if item.EventID == editEventID {
standupList[i].Body = mevent.TrimReplyFallbackText(newContent.Body)
standupList[i].FormattedBody = mevent.TrimReplyFallbackHTML(newContent.FormattedBody)
return true
}
}
return false
}
func HandleEdit(event *mevent.Event, standupFlow *StandupFlow) {
// This is an edit. If it's an edit to one of the messages in the
// current standup, then edit the entry in the corresponding list.
messageEventContent := event.Content.AsMessage()
relatesTo := messageEventContent.RelatesTo
standupLists := [][]StandupItem{standupFlow.Yesterday, standupFlow.Friday, standupFlow.Weekend, standupFlow.Today, standupFlow.Blockers, standupFlow.Notes}
edited := false
for _, standupList := range standupLists {
if tryEditListItem(standupList, relatesTo.EventID, messageEventContent.NewContent) {
edited = true
break
}
}
if edited {
if standupFlow.State == Threads || standupFlow.State == ThreadsFriday {
EditPreview(event.RoomID, event.Sender, standupFlow)
} else if standupFlow.State == Confirm {
standupFlow.ReactableEvents = EditPreview(event.RoomID, event.Sender, standupFlow)
} else if standupFlow.State == Sent {
client.RedactEvent(event.RoomID, standupFlow.PreviewEventId)
ShowMessagePreview(event.RoomID, event.Sender, standupFlow, true)
}
}
}
func HandleReply(event *mevent.Event, standupFlow *StandupFlow) {
// This is a reply. This only matters in thread mode.
switch standupFlow.State {
case Threads, ThreadsFriday, Confirm, Sent:
log.Info("Reply in thread mode.")
break
default:
log.Info("Reply not in thread mode.")
return
}
messageEventContent := event.Content.AsMessage()
relatesTo := messageEventContent.RelatesTo
edited := false
standupItem := StandupItem{
EventID: event.ID,
Body: mevent.TrimReplyFallbackText(messageEventContent.Body),
FormattedBody: mevent.TrimReplyFallbackHTML(messageEventContent.FormattedBody),
}
for _, eventID := range standupFlow.YesterdayThreadEvents {
if eventID == relatesTo.EventID {
standupFlow.Yesterday = append(standupFlow.Yesterday, standupItem)
standupFlow.YesterdayThreadEvents = append(standupFlow.YesterdayThreadEvents, event.ID)
edited = true
}
}
for _, eventID := range standupFlow.FridayThreadEvents {
if eventID == relatesTo.EventID {
standupFlow.Friday = append(standupFlow.Friday, standupItem)
standupFlow.FridayThreadEvents = append(standupFlow.FridayThreadEvents, event.ID)
edited = true
}
}
for _, eventID := range standupFlow.WeekendThreadEvents {
if eventID == relatesTo.EventID {
standupFlow.Weekend = append(standupFlow.Weekend, standupItem)
standupFlow.WeekendThreadEvents = append(standupFlow.WeekendThreadEvents, event.ID)
edited = true
}
}
for _, eventID := range standupFlow.TodayThreadEvents {
if eventID == relatesTo.EventID {
standupFlow.Today = append(standupFlow.Today, standupItem)
standupFlow.TodayThreadEvents = append(standupFlow.TodayThreadEvents, event.ID)
edited = true
}
}
for _, eventID := range standupFlow.BlockersThreadEvents {
if eventID == relatesTo.EventID {
standupFlow.Blockers = append(standupFlow.Blockers, standupItem)
standupFlow.BlockersThreadEvents = append(standupFlow.BlockersThreadEvents, event.ID)
edited = true
}
}
for _, eventID := range standupFlow.NotesThreadEvents {
if eventID == relatesTo.EventID {
standupFlow.Notes = append(standupFlow.Notes, standupItem)
standupFlow.NotesThreadEvents = append(standupFlow.NotesThreadEvents, event.ID)
edited = true
}
}
if edited {
// Update the message preview.
standupFlow.ReactableEvents = EditPreview(event.RoomID, event.Sender, standupFlow)
if standupFlow.State == Sent && standupFlow.ResendEventId == nil {
resp, err := SendMessage(event.RoomID, &mevent.MessageEventContent{
MsgType: mevent.MsgText,
Body: fmt.Sprintf("Send Edit (%s) or Cancel (%s)?", CHECKMARK, RED_X),
Format: mevent.FormatHTML,
FormattedBody: fmt.Sprintf("Send Edit (%s) or Cancel (%s)?", CHECKMARK, RED_X),
})
if err != nil {
log.Error("Failed to ask user if the want to send an edit")
return
}
SendReaction(event.RoomID, resp.EventID, CHECKMARK)
SendReaction(event.RoomID, resp.EventID, RED_X)
standupFlow.ReactableEvents = append(standupFlow.ReactableEvents, resp.EventID)
standupFlow.ResendEventId = &resp.EventID
}
}
}
func HandleMessage(_ mautrix.EventSource, event *mevent.Event) {
userId := mid.UserID(configuration.Username)
if event.Sender == userId {
return
}
messageEventContent := event.Content.AsMessage()
commandParts, err := getCommandParts(messageEventContent.Body)
if err != nil {
// This message is not a command.
if stateStore.GetConfigRoomId(event.Sender) != event.RoomID {
// Ignore non-command messages if not in config room.
return
}
if val, found := currentStandupFlows[event.Sender]; found {
// Mark the message as read after we've handled it.
defer client.MarkRead(event.RoomID, event.ID)
// Handle edits and thread replies
relatesTo := messageEventContent.RelatesTo
if relatesTo != nil {
if relatesTo.Type == mevent.RelReplace {
HandleEdit(event, val)
} else if relatesTo.Type == mevent.RelReply || relatesTo.Type == "io.element.thread" || relatesTo.Type == "m.thread" {
HandleReply(event, val)
}
return
}
standupItem := StandupItem{
EventID: event.ID,
Body: messageEventContent.Body,
FormattedBody: messageEventContent.FormattedBody,
}
switch val.State {
case Yesterday:
val.Yesterday = append(val.Yesterday, standupItem)
break
case Friday:
val.Friday = append(val.Friday, standupItem)
break
case Weekend:
val.Weekend = append(val.Weekend, standupItem)
break
case Today:
val.Today = append(val.Today, standupItem)
break
case Blockers:
val.Blockers = append(val.Blockers, standupItem)
break
case Notes:
val.Notes = append(val.Notes, standupItem)
break
default:
return
}
SendReaction(event.RoomID, event.ID, CHECKMARK)
val.ReactableEvents = append(val.ReactableEvents, event.ID)
}
// This is not a bot command. Return.
return
}
// Mark the message as read after we've handled it.
defer client.MarkRead(event.RoomID, event.ID)
stateStore.SetConfigRoom(event.Sender, event.RoomID)
switch strings.ToLower(commandParts[0]) {
case "vanquish":
DoRetry("leave room", func() (interface{}, error) {
return client.LeaveRoom(event.RoomID)
})
break
case "tz":
HandleTimezone(event.RoomID, event.Sender, commandParts[1:])
break
case "notify":
HandleNotify(event.RoomID, event.Sender, commandParts[1:])
break
case "threads":
HandleThreads(event.RoomID, event.Sender, commandParts[1:])
break
case "new":
currentStandupFlows[event.Sender] = BlankStandupFlow()
CreatePost(event.RoomID, event.Sender)
break
case "show":
if currentFlow, found := currentStandupFlows[event.Sender]; found && currentFlow.State != FlowNotStarted {
SendMessage(event.RoomID, FormatPost(event.Sender, currentFlow, true, false, false))
} else {
SendMessage(event.RoomID, &mevent.MessageEventContent{MsgType: mevent.MsgText, Body: "No standup post to show."})
}
break
case "edit":
if useThreads, _ := stateStore.GetUseThreads(event.Sender); useThreads {
SendMessage(event.RoomID, &mevent.MessageEventContent{
MsgType: mevent.MsgNotice,
Body: fmt.Sprintf("You cannot use !edit when using threads. Just reply to the corresponding thread."),
})
return
}
if len(commandParts) != 2 {
SendMessage(event.RoomID, &mevent.MessageEventContent{
MsgType: mevent.MsgNotice,
Body: fmt.Sprintf("Invalid item to edit! Must be one of Friday, Weekend, Yesterday, Today, Blockers, or Notes"),
})
return
}
if currentFlow, found := currentStandupFlows[event.Sender]; !found || currentFlow.State == FlowNotStarted {
SendMessage(event.RoomID, &mevent.MessageEventContent{MsgType: mevent.MsgNotice, Body: "No standup post to edit."})
}
switch strings.ToLower(commandParts[1]) {
case "friday":
if stateStore.GetCurrentWeekdayInUserTimezone(event.Sender) != time.Monday {
SendMessage(event.RoomID, &mevent.MessageEventContent{MsgType: mevent.MsgNotice, Body: "It's not Monday, so you can't go back to edit Friday."})
return
}
GoToStateAndNotify(event.RoomID, event.Sender, Friday)
break
case "weekend":
if stateStore.GetCurrentWeekdayInUserTimezone(event.Sender) != time.Monday {
SendMessage(event.RoomID, &mevent.MessageEventContent{MsgType: mevent.MsgNotice, Body: "It's not Monday, so you can't go back to edit the weekend."})
return
}
GoToStateAndNotify(event.RoomID, event.Sender, Weekend)
break
case "yesterday":
if stateStore.GetCurrentWeekdayInUserTimezone(event.Sender) == time.Monday {
SendMessage(event.RoomID, &mevent.MessageEventContent{MsgType: mevent.MsgNotice, Body: "It's Monday, so you can't go back to edit yesterday. Edit Friday or Weekend instead."})
return
}
GoToStateAndNotify(event.RoomID, event.Sender, Yesterday)
break
case "today":
GoToStateAndNotify(event.RoomID, event.Sender, Today)
break
case "blockers":
GoToStateAndNotify(event.RoomID, event.Sender, Blockers)
break
case "notes":
GoToStateAndNotify(event.RoomID, event.Sender, Notes)
break
}
break
case "undo":
if val, found := currentStandupFlows[event.Sender]; !found || val.State != Sent {
SendMessage(event.RoomID, &mevent.MessageEventContent{MsgType: mevent.MsgNotice, Body: "No sent standup post to undo."})
} else {
sendRoomID, err := stateStore.GetSendRoomId(event.Sender)
if err != nil {
log.Debugf("No send room configured for %s, can't undo anything", event.Sender)
SendMessage(event.RoomID, &mevent.MessageEventContent{MsgType: mevent.MsgNotice, Body: "No send room configured. Can't undo anything."})
}
stateKey := strings.TrimPrefix(event.Sender.String(), "@")
var previousPostEventContent PreviousPostEventContent
err = client.StateEvent(event.RoomID, StatePreviousPost, stateKey, &previousPostEventContent)
if err != nil {
log.Debug("Couldn't find previous post info.")
SendMessage(event.RoomID, &mevent.MessageEventContent{MsgType: mevent.MsgNotice, Body: "No previous standup post to undo."})
}
_, err = client.RedactEvent(sendRoomID, previousPostEventContent.EditEventID)
if err != nil {
SendMessage(event.RoomID, &mevent.MessageEventContent{Body: "Failed to redact the standup post!"})
} else {
SendMessage(event.RoomID, &mevent.MessageEventContent{
Body: fmt.Sprintf("Redacted standup post with ID: %s in %s", previousPostEventContent.EditEventID, event.RoomID),
})
currentStandupFlows[event.Sender].State = Confirm
client.SendStateEvent(event.RoomID, StatePreviousPost, stateKey, struct{}{})
}
}
break
case "cancel":
if val, found := currentStandupFlows[event.Sender]; !found || val.State == FlowNotStarted {
SendMessage(event.RoomID, &mevent.MessageEventContent{MsgType: mevent.MsgNotice, Body: "No standup post to cancel."})
} else {
currentStandupFlows[event.Sender] = BlankStandupFlow()
SendMessage(event.RoomID, &mevent.MessageEventContent{MsgType: mevent.MsgNotice, Body: "Standup post cancelled"})
}
break
case "room":
HandleRoom(event.RoomID, event, commandParts[1:])
break
default:
SendHelp(event.RoomID)
break
}
}
func HandleRedaction(_ mautrix.EventSource, event *mevent.Event) {
// Mark the redaction as read after we've handled it.
defer client.MarkRead(event.RoomID, event.ID)
// Handle redactions
if val, found := currentStandupFlows[event.Sender]; found {
removedItem := false
for i, item := range val.Yesterday {
if item.EventID == event.Redacts {
removedItem = true
val.Yesterday = append(val.Yesterday[:i], val.Yesterday[i+1:]...)
break
}
}
if !removedItem {
for i, item := range val.Friday {
if item.EventID == event.Redacts {
removedItem = true
val.Friday = append(val.Friday[:i], val.Friday[i+1:]...)
break
}
}
}
if !removedItem {
for i, item := range val.Weekend {
if item.EventID == event.Redacts {
removedItem = true
val.Weekend = append(val.Weekend[:i], val.Weekend[i+1:]...)
break
}
}
}
if !removedItem {
for i, item := range val.Today {
if item.EventID == event.Redacts {
removedItem = true
val.Today = append(val.Today[:i], val.Today[i+1:]...)
break
}
}
}
if !removedItem {
for i, item := range val.Blockers {
if item.EventID == event.Redacts {
removedItem = true
val.Blockers = append(val.Blockers[:i], val.Blockers[i+1:]...)
break
}
}
}
if !removedItem {
for i, item := range val.Notes {
if item.EventID == event.Redacts {
removedItem = true
val.Notes = append(val.Notes[:i], val.Notes[i+1:]...)
break
}
}
}
if removedItem {
if val.PreviewEventId.String() != "" {
val.ReactableEvents = EditPreview(event.RoomID, event.Sender, val)
}
}
}
}