From 3086da92843b2db7f769126f299857fe9d543703 Mon Sep 17 00:00:00 2001 From: "Benjamin A. Shelton" Date: Fri, 23 Oct 2026 20:26:26 -0600 Subject: [PATCH] WIP birthdays. --- internal/commands/birthdays.go | 99 ++++++++++++++++++++++++++++++++++ internal/commands/common.go | 11 ++++ internal/models/birthdays.go | 47 +++++++++------- 3 files changed, 139 insertions(+), 18 deletions(-) create mode 100644 internal/commands/birthdays.go diff --git a/internal/commands/birthdays.go b/internal/commands/birthdays.go new file mode 100644 index 0000000..75ebbd7 --- /dev/null +++ b/internal/commands/birthdays.go @@ -0,0 +1,99 @@ +package commands + +import ( + "context" + "time" + + "code.destrealm.net/zancarius/justusbot/internal/models" + "code.destrealm.net/zancarius/justusbot/internal/settings" + "github.com/urfave/cli/v3" + "go.destrealm.net/errors" + "go.destrealm.net/logging/v3" + "go.destrealm.net/vfs" +) + +type birthday struct { + fs vfs.VFS + settings *settings.Settings +} + +func (b *birthday) ListWeeklyBirthdays(ctx context.Context, cmd *cli.Command) error { + logger := logging.Get("commands") + logger.SetFormat("{{message}}") + logger.SetLevel(logging.Info) + + chatid := cmd.Int64("chatid") + + mdb := models.OpenDatabase(ctx, b.settings) + if err := mdb.Open(); err != nil { + logger.Error("connection error:", errors.Unfurl(err)) + return err + } + + mdl := models.NewBirthdayModel(ctx, mdb.DB()) + bdays, err := mdl.BirthdaysThisWeek(chatid) + if err != nil { + if errors.Is(err, models.ErrNoRows) { + logger.Error("no chats configured") + return nil + } + return err + } + if len(bdays) == 0 { + logger.Info("Birthdays this week (Sunday to Sunday):") + logger.Info("=======================================") + logger.Info(" (none)") + return nil + } + logger.Info("Birthdays this week (Sunday to Sunday):") + logger.Info("=======================================") + for _, bday := range bdays { + d, err := time.Parse("2006-01-02", bday.Birthdate) + if err != nil { + logger.Error("cannot parse date:", bday.Birthdate) + continue + } + logger.Infof(" • %s: %s", bday.Name, d.Format("Mon, January _2")) + } + return nil +} + +func (b *birthday) ListTodaysBirthdays(ctx context.Context, cmd *cli.Command) error { + logger := logging.Get("commands") + logger.SetFormat("{{message}}") + logger.SetLevel(logging.Info) + + chatid := cmd.Int64("chatid") + + mdb := models.OpenDatabase(ctx, b.settings) + if err := mdb.Open(); err != nil { + logger.Error("connection error:", errors.Unfurl(err)) + return err + } + + mdl := models.NewBirthdayModel(ctx, mdb.DB()) + bdays, err := mdl.BirthdaysToday(chatid) + if err != nil { + if errors.Is(err, models.ErrNoRows) { + logger.Error("no chats configured") + return nil + } + return err + } + if len(bdays) == 0 { + logger.Info("Birthdays today:") + logger.Info("=======================================") + logger.Info(" (none)") + return nil + } + logger.Info("Birthdays today:", time.Now().Format("Mon, January _2")) + logger.Info("=======================================") + for _, bday := range bdays { + if err != nil { + logger.Error("cannot parse date:", bday.Birthdate) + continue + } + logger.Infof(" • %s", bday.Name) + } + return nil +} diff --git a/internal/commands/common.go b/internal/commands/common.go index 850c84b..848802f 100644 --- a/internal/commands/common.go +++ b/internal/commands/common.go @@ -12,6 +12,7 @@ import ( type commands struct { ctx context.Context root vfs.VFS + birthday *birthday chats *chats meta *metadata verse *verse @@ -55,6 +56,16 @@ func (c *commands) Initalizer() *installer { return i } +func (c *commands) Birthdays() *birthday { + if c.birthday == nil { + c.birthday = &birthday{ + fs: c.root, + settings: c.settings, + } + } + return c.birthday +} + func (c *commands) Chats() *chats { if c.chats == nil { c.chats = &chats{ diff --git a/internal/models/birthdays.go b/internal/models/birthdays.go index 2e63545..6ea6adb 100644 --- a/internal/models/birthdays.go +++ b/internal/models/birthdays.go @@ -2,7 +2,6 @@ package models import ( "context" - "time" "github.com/jmoiron/sqlx" "github.com/mattn/go-sqlite3" @@ -11,7 +10,7 @@ import ( type Birthday struct { ID int `db:"id"` - UserID int `db:"userid"` + UserID *int `db:"userid"` ChatID int64 `db:"chatid"` Name string `db:"name"` Month int `db:"month"` @@ -19,7 +18,7 @@ type Birthday struct { Delta int `db:"leap_delta"` // Birthdate; computed field. - Birthdate *time.Time `db:"birthdate"` + Birthdate string `db:"birthdate"` } type BirthdayModel struct { @@ -56,17 +55,25 @@ func (bm *BirthdayModel) AddBirthdayByUserID(chatid int64, userid int, month, da } func (bm *BirthdayModel) BirthdaysThisWeek(chatid int64) ([]*Birthday, error) { - var bdays []*Birthday + bdays := []*Birthday{} q := `select id, userid, chatid, name, leap_delta, - date(concat(strftime('%Y', date('now')), '-', + date(concat(strftime('%Y', date('now', 'localtime')), '-', format('%02d', month), '-', format('%02d', day))) as birthdate from birthdays where chatid = ? and ( - birthdate >= date('now', 'weekday 0') and - birthdate <= date('now', 'weekday 0', '+7 days'));` - if err := bm.db.GetContext(bm.ctx, &bdays, q, chatid); err != nil { + birthdate >= date('now', 'localtime', 'weekday 0') and + birthdate <= date('now', 'localtime', 'weekday 0', '+7 days')) order by birthdate asc;` + err := bm.db.SelectContext(bm.ctx, &bdays, q, chatid) + if err != nil { + var serr sqlite3.Error + if errors.As(err, &serr) { + switch serr.Code { + case sqlite3.ErrMismatch: + return nil, ErrNoRows.Do(err) + } + } return nil, err } @@ -77,22 +84,26 @@ func (bm *BirthdayModel) BirthdaysToday(chatid int64) ([]*Birthday, error) { var bdays []*Birthday q := `select id, userid, chatid, name, leap_delta, - date(concat(strftime('%Y', date('now')), '-', + date(concat(strftime('%Y', + date('now', 'localtime')), '-', format('%02d', month), '-', format('%02d', day))) as birthdate from birthdays where - birthdate == date('now');` + chatid = ? and + birthdate == date('now', 'localtime') + order by name;` - if err := bm.db.GetContext(bm.ctx, &bdays, q, chatid); err != nil { + err := bm.db.SelectContext(bm.ctx, &bdays, q, chatid) + if err != nil { + var serr sqlite3.Error + if errors.As(err, &serr) { + switch serr.Code { + case sqlite3.ErrMismatch: + return nil, ErrNoRows.Do(err) + } + } return nil, err } return bdays, nil } - -//month >= cast(strftime('%m', date('now', 'weekday 0')) as integer) and month <= cast(strftime('%m', date(date('now', 'weekday 0'), '+7 days')) as integer) - -/* -select * from birthdays where (day >= cast(strftime('%e', date('now', 'weekday 0')) as integer) and day <= max(cast(strftime('%e', date('now', 'weekday 0', '+7 days')) as integer), 31)) or (day >= min(cast(strftime('%e', date('now', 'weekday 0')) as integer), 1) and day <= max(cast(strftime('%e', date('now', 'weekday 0', '+7 days')) as integer), 31)); - -*/