Fix #2: ical calendar: take 'cancelled' status into account #4
37
periodic.go
37
periodic.go
@ -5,6 +5,7 @@ package chatbot
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@ -49,21 +50,35 @@ func (pn *PeriodicNotifier) Run(ctx context.Context, channel chan<- time.Time) {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case <-time.After(1 * time.Minute):
|
||||
pn.checkAndNotify(channel)
|
||||
case <-time.After(20 * time.Second):
|
||||
pn.checkAndNotify(log, channel)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (pn *PeriodicNotifier) checkAndNotify(channel chan<- time.Time) {
|
||||
nextMeetingTime := pn.meetingTimeSource.NextMeetingTime().UTC()
|
||||
func (pn *PeriodicNotifier) checkAndNotify(log *slog.Logger, channel chan<- time.Time) {
|
||||
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
|
||||
for _, duration := range pn.durations {
|
||||
notifTime := nextMeetingTime.Add(-duration)
|
||||
|
||||
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
|
||||
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) {
|
||||
timeUntilMeeting := time.Until(timeOfMeeting).Round(time.Minute)
|
||||
log := GetBotLog(ctx)
|
||||
|
||||
// The main info: how long until the next meeting.
|
||||
hours := int(timeUntilMeeting.Truncate(time.Hour).Hours())
|
||||
minutes := int(timeUntilMeeting.Truncate(time.Minute).Minutes()) % 60
|
||||
timeUntilMeeting := time.Until(timeOfMeeting)
|
||||
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 **"
|
||||
switch {
|
||||
|
Loading…
Reference in New Issue
Block a user