Add CircleGroup notifications to chore notifications

This commit is contained in:
Mo Tarbin 2024-07-17 01:09:47 -04:00
parent 12aaa90387
commit c7b29fa195
3 changed files with 78 additions and 11 deletions

View File

@ -55,6 +55,8 @@ type NotificationMetadata struct {
Completion bool `json:"completion,omitempty"` Completion bool `json:"completion,omitempty"`
Nagging bool `json:"nagging,omitempty"` Nagging bool `json:"nagging,omitempty"`
PreDue bool `json:"predue,omitempty"` PreDue bool `json:"predue,omitempty"`
CircleGroup bool `json:"circleGroup,omitempty"`
CircleGroupID *int64 `json:"circleGroupID,omitempty"`
} }
type Tag struct { type Tag struct {

View File

@ -62,6 +62,9 @@ func (n *NotificationPlanner) GenerateNotifications(c context.Context, chore *ch
if mt.Nagging { if mt.Nagging {
notifications = append(notifications, generateOverdueNotifications(chore, assignees)...) notifications = append(notifications, generateOverdueNotifications(chore, assignees)...)
} }
if mt.CircleGroup {
notifications = append(notifications, generateCircleGroupNotifications(chore, mt)...)
}
n.nRepo.BatchInsertNotifications(notifications) n.nRepo.BatchInsertNotifications(notifications)
return true return true
@ -150,3 +153,48 @@ func generateOverdueNotifications(chore *chModel.Chore, users []*cModel.UserCirc
return notifications return notifications
} }
func generateCircleGroupNotifications(chore *chModel.Chore, mt *chModel.NotificationMetadata) []*nModel.Notification {
var notifications []*nModel.Notification
if !mt.CircleGroup || mt.CircleGroupID == nil || *mt.CircleGroupID == 0 {
return notifications
}
if mt.DueDate {
notifications = append(notifications, &nModel.Notification{
ChoreID: chore.ID,
IsSent: false,
ScheduledFor: *chore.NextDueDate,
CreatedAt: time.Now().UTC(),
TypeID: 1,
TargetID: fmt.Sprint(*mt.CircleGroupID),
Text: fmt.Sprintf("📅 Reminder: *%s* is due today.", chore.Name),
})
}
if mt.PreDue {
notifications = append(notifications, &nModel.Notification{
ChoreID: chore.ID,
IsSent: false,
ScheduledFor: *chore.NextDueDate,
CreatedAt: time.Now().UTC().Add(-time.Hour * 3),
TypeID: 3,
TargetID: fmt.Sprint(*mt.CircleGroupID),
Text: fmt.Sprintf("📢 Heads up! *%s* is due soon (on %s).", chore.Name, chore.NextDueDate.Format("January 2nd")),
})
}
if mt.Nagging {
for _, hours := range []int{24, 48, 72} {
scheduleTime := chore.NextDueDate.Add(time.Hour * time.Duration(hours))
notifications = append(notifications, &nModel.Notification{
ChoreID: chore.ID,
IsSent: false,
ScheduledFor: scheduleTime,
CreatedAt: time.Now().UTC(),
TypeID: 2,
TargetID: fmt.Sprint(*mt.CircleGroupID),
Text: fmt.Sprintf("🚨 *%s* is now %d hours overdue. Please complete it as soon as possible.", chore.Name, hours),
})
}
}
return notifications
}

View File

@ -2,6 +2,7 @@ package telegram
import ( import (
"context" "context"
"encoding/json"
"fmt" "fmt"
"strconv" "strconv"
@ -49,22 +50,38 @@ func (tn *TelegramNotifier) SendChoreReminder(c context.Context, chore *chModel.
} }
} }
func (tn *TelegramNotifier) SendChoreCompletion(c context.Context, chore *chModel.Chore, users []*uModel.User) { func (tn *TelegramNotifier) SendChoreCompletion(c context.Context, chore *chModel.Chore, user *uModel.User) {
log := logging.FromContext(c) log := logging.FromContext(c)
for _, user := range users { var mt *chModel.NotificationMetadata
if user.ChatID == 0 { if err := json.Unmarshal([]byte(*chore.NotificationMetadata), &mt); err != nil {
continue log.Error("Error unmarshalling notification metadata", err)
} }
targets := []int64{}
if user.ChatID != 0 {
targets = append(targets, user.ChatID)
}
if mt.CircleGroup && mt.CircleGroupID != nil {
// attempt to parse it:
if *mt.CircleGroupID != 0 {
targets = append(targets, *mt.CircleGroupID)
}
}
text := fmt.Sprintf("🎉 *%s* is completed! is off the list, %s! 🌟 ", chore.Name, user.DisplayName) text := fmt.Sprintf("🎉 *%s* is completed! is off the list, %s! 🌟 ", chore.Name, user.DisplayName)
msg := tgbotapi.NewMessage(user.ChatID, text) for _, target := range targets {
msg := tgbotapi.NewMessage(target, text)
msg.ParseMode = "Markdown" msg.ParseMode = "Markdown"
_, err := tn.bot.Send(msg) _, err := tn.bot.Send(msg)
if err != nil { if err != nil {
log.Error("Error sending message to user: ", err) log.Error("Error sending message to user: ", err)
log.Debug("Error sending message, chore: ", chore.Name, " user: ", user.DisplayName, " chatID: ", user.ChatID, " user id: ", user.ID) log.Debug("Error sending message, chore: ", chore.Name, " user: ", user.DisplayName, " chatID: ", user.ChatID, " user id: ", user.ID)
} }
} }
} }
func (tn *TelegramNotifier) SendChoreOverdue(c context.Context, chore *chModel.Chore, users []*uModel.User) { func (tn *TelegramNotifier) SendChoreOverdue(c context.Context, chore *chModel.Chore, users []*uModel.User) {