From 9d04ff6a4d1dde541a0253e0db59957c69fec4cf Mon Sep 17 00:00:00 2001 From: cel 🌸 Date: Tue, 10 Sep 2024 17:33:54 +0100 Subject: WIP: genericize notifier as interface --- internal/notifier/notifier.go | 17 +++++++++++++++++ internal/notifier/notifiers.go | 15 +++++++++++++++ internal/notifier/scheduler.go | 5 ++--- internal/notifier/telegram/telegram.go | 2 +- 4 files changed, 35 insertions(+), 4 deletions(-) create mode 100644 internal/notifier/notifier.go create mode 100644 internal/notifier/notifiers.go (limited to 'internal') diff --git a/internal/notifier/notifier.go b/internal/notifier/notifier.go new file mode 100644 index 0000000..7ce94b0 --- /dev/null +++ b/internal/notifier/notifier.go @@ -0,0 +1,17 @@ +package notifier + +import ( + "context" + + chModel "donetick.com/core/internal/chore/model" + nModel "donetick.com/core/internal/notifier/model" + uModel "donetick.com/core/internal/user/model" +) + +type Notifier interface { + SendChoreReminder(c context.Context, chore *chModel.Chore, users []*uModel.User) + SendChoreCompletion(c context.Context, chore *chModel.Chore, user *uModel.User) + SendChoreOverdue(c context.Context, chore *chModel.Chore, users []*uModel.User) + SendChorePreDue(c context.Context, chore *chModel.Chore, users []*uModel.User) + SendNotification(c context.Context, notification *nModel.Notification) +} diff --git a/internal/notifier/notifiers.go b/internal/notifier/notifiers.go new file mode 100644 index 0000000..5f6087c --- /dev/null +++ b/internal/notifier/notifiers.go @@ -0,0 +1,15 @@ +package notifier + +import ( + "donetick.com/core/config" + "donetick.com/core/internal/notifier/telegram" +) + +type Notifiers []Notifier + +func NewNotifiers(config *config.Config) Notifiers { + var notifiers []Notifier + notifiers = append(notifiers, telegram.NewTelegramNotifier(config)) + + return notifiers +} diff --git a/internal/notifier/scheduler.go b/internal/notifier/scheduler.go index 69470d2..3011c66 100644 --- a/internal/notifier/scheduler.go +++ b/internal/notifier/scheduler.go @@ -8,7 +8,6 @@ import ( "donetick.com/core/config" chRepo "donetick.com/core/internal/chore/repo" nRepo "donetick.com/core/internal/notifier/repo" - notifier "donetick.com/core/internal/notifier/telegram" uRepo "donetick.com/core/internal/user/repo" "donetick.com/core/logging" ) @@ -23,12 +22,12 @@ type Scheduler struct { choreRepo *chRepo.ChoreRepository userRepo *uRepo.UserRepository stopChan chan bool - notifier *notifier.TelegramNotifier + notifier Notifier notificationRepo *nRepo.NotificationRepository SchedulerJobs config.SchedulerConfig } -func NewScheduler(cfg *config.Config, ur *uRepo.UserRepository, cr *chRepo.ChoreRepository, n *notifier.TelegramNotifier, nr *nRepo.NotificationRepository) *Scheduler { +func NewScheduler(cfg *config.Config, ur *uRepo.UserRepository, cr *chRepo.ChoreRepository, n Notifier, nr *nRepo.NotificationRepository) *Scheduler { return &Scheduler{ choreRepo: cr, userRepo: ur, diff --git a/internal/notifier/telegram/telegram.go b/internal/notifier/telegram/telegram.go index 3f064b6..eaa89d1 100644 --- a/internal/notifier/telegram/telegram.go +++ b/internal/notifier/telegram/telegram.go @@ -19,7 +19,7 @@ type TelegramNotifier struct { } func NewTelegramNotifier(config *config.Config) *TelegramNotifier { - bot, err := tgbotapi.NewBotAPI(config.Telegram.Token) + bot, err := tgbotapi.NewBotAPI(config.Notifiers.Telegram.Token) if err != nil { fmt.Println("Error creating bot: ", err) return nil -- cgit