all repos — cameraman @ 89eab556b38d04c80687610802d3397ddd83800d

add soft notifications
Marco Andronaco andronacomarco@gmail.com
Mon, 12 Aug 2024 19:36:32 +0200
commit

89eab556b38d04c80687610802d3397ddd83800d

parent

e9af61f938b31b2bb9589e5bb079370941294021

2 files changed, 64 insertions(+), 42 deletions(-)

jump to
M main.gomain.go

@@ -20,13 +20,14 @@ ID uint `json:"id" gorm:"primaryKey"`

CreatedAt time.Time `json:"created_at"` UpdatedAt time.Time `json:"updated_at"` - Day uint `json:"day"` - Month uint `json:"month"` - Year *uint `json:"year"` - Name string `json:"name"` - Description string `json:"description"` - Notify bool `json:"notify"` - Notified bool `json:"notified"` + Day uint `json:"day"` + Month uint `json:"month"` + Year *uint `json:"year"` + Name string `json:"name"` + Description string `json:"description"` + Notify bool `json:"notify"` + Notified bool `json:"notified"` + NotifiedSoft bool `json:"notified_soft"` } const (
M notify.gonotify.go

@@ -26,11 +26,12 @@ MessageID int `json:"message_id"`

} var ( - NotificationWindow int - SleepDuration time.Duration - telegramToken string - chatID string - threadID string + NotificationWindow int + SoftNotificationWindow int + SleepDuration time.Duration + telegramToken string + chatID string + threadID string ) func sendPostRequest(url string, payload map[string]interface{}) (*http.Response, error) {

@@ -54,7 +55,7 @@

return resp, nil } -func notifyTelegram(occurrence Occurrence) error { +func notifyTelegram(occurrence Occurrence, soft bool) error { log.Println("Sending notification for occurrence", occurrence.ID) var message string if occurrence.Year != nil {

@@ -103,6 +104,10 @@ log.Printf("Telegram API returned an error: %v", r)

return fmt.Errorf("telegram API error: %v", r) } + if soft { + return nil + } + msgId := r.Result.MessageID // Prepare the request to pin the message

@@ -131,12 +136,13 @@ log.Printf("Message pinned: %s, Response: %s", message, string(bodyBytes))

return nil } -func resetNotifications() { - if err := db.Model(&Occurrence{}).Where("notified = ?", true).Update("notified", false).Error; err != nil { +func resetNotifications(notified_column string) (err error) { + if err = db.Model(&Occurrence{}).Where(notified_column+" = ?", true).Update(notified_column, false).Error; err != nil { log.Printf("Failed to reset notifications: %v", err) } else { log.Println("Notifications have been reset for the new year.") } + return } func initNotifications() error {

@@ -151,6 +157,46 @@ }

return nil } +func check(notificationWindowDays int, soft bool) (err error) { + now := time.Now() + today := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local) + endWindow := today.AddDate(0, 0, notificationWindowDays) + + notified_column := "notified" + if soft { + notified_column += "_soft" + } + + var occurrences []Occurrence + db.Where(notified_column+" = ? AND ((month = ? AND day >= ?) OR (month = ? AND day <= ?))", + false, today.Month(), today.Day(), endWindow.Month(), endWindow.Day()).Find(&occurrences) + + for _, occurrence := range occurrences { + occurrenceDate := time.Date(today.Year(), time.Month(occurrence.Month), int(occurrence.Day), 0, 0, 0, 0, time.Local) + if occurrenceDate.Before(today) || occurrenceDate.After(endWindow) || !occurrence.Notify || occurrence.Notified { + continue + } + + err = notifyTelegram(occurrence, soft) + if err != nil { + return err + } + + err = db.Model(&Occurrence{}).Where("id = ?", occurrence.ID).Update(notified_column, true).Error + if err != nil { + return err + } + } + + // Check if New Year's Eve is within the next sleep cycle + nextCheck := now.Add(SleepDuration) + if (now.Month() == 12 && now.Day() == 31) && (nextCheck.Month() == 1 && nextCheck.Day() == 1) { + resetNotifications(notified_column) + } + + return +} + func CheckOccurrences() { err := initNotifications() if err != nil {

@@ -160,34 +206,9 @@ }

for { log.Println("Checking for new occurrences.") - now := time.Now() - today := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local) - endWindow := today.AddDate(0, 0, NotificationWindow) - var occurrences []Occurrence - db.Where("notified = ? AND ((month = ? AND day >= ?) OR (month = ? AND day <= ?))", - false, today.Month(), today.Day(), endWindow.Month(), endWindow.Day()).Find(&occurrences) - - for _, occurrence := range occurrences { - occurrenceDate := time.Date(today.Year(), time.Month(occurrence.Month), int(occurrence.Day), 0, 0, 0, 0, time.Local) - if occurrenceDate.Before(today) || occurrenceDate.After(endWindow) || !occurrence.Notify || occurrence.Notified { - continue - } - - err := notifyTelegram(occurrence) - if err != nil { - log.Println(err.Error()) - return - } - occurrence.Notified = true - db.Save(&occurrence) - } - - // Check if New Year's Eve is within the next sleep cycle - nextCheck := now.Add(SleepDuration) - if (now.Month() == 12 && now.Day() == 31) && (nextCheck.Month() == 1 && nextCheck.Day() == 1) { - resetNotifications() - } + check(NotificationWindow, false) + check(SoftNotificationWindow, true) time.Sleep(SleepDuration) }