package remind import ( "errors" "log" "time" "git.fromouter.space/crunchy-rocks/clessy-ng/modules" "git.fromouter.space/crunchy-rocks/clessy-ng/utils" "git.fromouter.space/hamcha/tg" "git.sr.ht/~hamcha/containers" "github.com/cockroachdb/pebble" ) type Reminder struct { ReminderID string TargetID int64 When int64 Text string Reference *ReminderReference } type ReminderReference struct { Chat int64 Message int64 } const reminderKey = "mod/reminder/pending" const reminderMaxDuration = time.Hour * 24 * 30 * 3 var defaultLocation *time.Location = nil var pending = containers.NewRWSyncMap[string, Reminder]() type Module struct { client *tg.Telegram name string kv *pebble.DB } func (m *Module) Initialize(options modules.ModuleOptions) error { m.client = options.API m.name = options.Name m.kv = options.KV var err error defaultLocation, err = time.LoadLocation("Europe/Rome") if err != nil { log.Fatalf("[remind] Something is really wrong: %s\n", err.Error()) return err } var reminders map[string]Reminder err = utils.ReadJSON(m.kv, reminderKey, &reminders) if err != nil { if !errors.Is(err, pebble.ErrNotFound) { log.Println("[remind] WARN: Could not load pending reminders (db error): " + err.Error()) return err } } // Fix old reminders dirty := false for reminderID, reminder := range reminders { if reminder.ReminderID == "" { reminder.ReminderID = reminderID dirty = true } } if dirty { m.save() } // Set schedule for id, reminder := range reminders { pending.SetKey(id, reminder) go m.schedule(id) } log.Printf("[remind] Loaded %d pending reminders\n", len(reminders)) return nil } func (m *Module) OnUpdate(update tg.APIUpdate) { // Check for updates to existing messages if update.Callback != nil { m.handleCallback(update.Callback) } if utils.IsCommand(update, m.name, "ricordami") || utils.IsCommand(update, m.name, "remind") { m.cmdRicordami(update.Message) return } if utils.IsCommand(update, m.name, "reminders") { m.cmdReminder(update.Message) return } }