Fix #2: ical calendar: take 'cancelled' status into account #4

Closed
Sybren A. Stüvel wants to merge 3 commits from fix/2-handle-cancelled-events into main

When changing the target branch, be careful to rebase the branch in your fork to match. See documentation.
Showing only changes of commit 4cc88710e9 - Show all commits

View File

@ -5,6 +5,7 @@ package chatbot
import ( import (
"context" "context"
"fmt" "fmt"
"log/slog"
"strings" "strings"
"time" "time"
@ -49,21 +50,35 @@ func (pn *PeriodicNotifier) Run(ctx context.Context, channel chan<- time.Time) {
select { select {
case <-ctx.Done(): case <-ctx.Done():
return return
case <-time.After(1 * time.Minute): case <-time.After(20 * time.Second):
pn.checkAndNotify(channel) pn.checkAndNotify(log, channel)
} }
} }
} }
func (pn *PeriodicNotifier) checkAndNotify(channel chan<- time.Time) { func (pn *PeriodicNotifier) checkAndNotify(log *slog.Logger, channel chan<- time.Time) {
nextMeetingTime := pn.meetingTimeSource.NextMeetingTime().UTC()
now := pn.clock.Now().UTC() now := pn.clock.Now().UTC()
if pn.lastCheckTime.IsZero() {
// Never checked before, so just remember that this was the first time, and
// don't notify. We should only notify about stuff that happens while we're
// running, otherwise a restart of the bot will always trigger a notification.
pn.lastCheckTime = now
return
}
nextMeetingTime := pn.meetingTimeSource.NextMeetingTime().UTC()
shouldNotify := false shouldNotify := false
for _, duration := range pn.durations { for _, duration := range pn.durations {
notifTime := nextMeetingTime.Add(-duration) notifTime := nextMeetingTime.Add(-duration)
if pn.lastCheckTime.Before(notifTime) && (now == notifTime || now.After(notifTime)) { if pn.lastCheckTime.Before(notifTime) && (now == notifTime || now.After(notifTime)) {
log.Info("periodic notifier: notification should get sent",
"lastCheck", pn.lastCheckTime,
"notifTime", notifTime,
"timeUntilMeeting", duration,
)
shouldNotify = true shouldNotify = true
break break
} }
@ -79,11 +94,19 @@ func (pn *PeriodicNotifier) checkAndNotify(channel chan<- time.Time) {
} }
func SendMeetingTimeNotification(ctx context.Context, client *mautrix.Client, roomID id.RoomID, timeOfMeeting time.Time) { func SendMeetingTimeNotification(ctx context.Context, client *mautrix.Client, roomID id.RoomID, timeOfMeeting time.Time) {
timeUntilMeeting := time.Until(timeOfMeeting).Round(time.Minute) log := GetBotLog(ctx)
// The main info: how long until the next meeting. // The main info: how long until the next meeting.
hours := int(timeUntilMeeting.Truncate(time.Hour).Hours()) timeUntilMeeting := time.Until(timeOfMeeting)
minutes := int(timeUntilMeeting.Truncate(time.Minute).Minutes()) % 60 hours := int(timeUntilMeeting.Round(time.Minute).Truncate(time.Hour).Hours())
minutes := int(timeUntilMeeting.Round(time.Minute).Minutes()) % 60
log.Info("sending meeting time notification",
"meetingTime", timeOfMeeting.UTC(),
"timeUntilMeeting", timeUntilMeeting,
"hours", hours,
"minutes", minutes,
)
firstLine := "The next module meeting will be in **" firstLine := "The next module meeting will be in **"
switch { switch {