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 --- config/config.go | 11 ++++++----- internal/notifier/notifier.go | 17 +++++++++++++++++ internal/notifier/notifiers.go | 15 +++++++++++++++ internal/notifier/scheduler.go | 5 ++--- internal/notifier/telegram/telegram.go | 2 +- main.go | 7 ++++--- 6 files changed, 45 insertions(+), 12 deletions(-) create mode 100644 internal/notifier/notifier.go create mode 100644 internal/notifier/notifiers.go diff --git a/config/config.go b/config/config.go index 52ed1db..139687d 100644 --- a/config/config.go +++ b/config/config.go @@ -10,7 +10,7 @@ import ( type Config struct { Name string `mapstructure:"name" yaml:"name"` - Telegram TelegramConfig `mapstructure:"telegram" yaml:"telegram"` + Notifiers NotifiersConfig `mapstructure:"notifiers" yaml:"notifiers"` Database DatabaseConfig `mapstructure:"database" yaml:"database"` Jwt JwtConfig `mapstructure:"jwt" yaml:"jwt"` Server ServerConfig `mapstructure:"server" yaml:"server"` @@ -20,6 +20,10 @@ type Config struct { IsDoneTickDotCom bool `mapstructure:"is_done_tick_dot_com" yaml:"is_done_tick_dot_com"` } +type NotifiersConfig struct { + Telegram TelegramConfig `mapstructure:"telegram" yaml:"telegram"` +} + type TelegramConfig struct { Token string `mapstructure:"token" yaml:"token"` } @@ -80,9 +84,6 @@ type EmailConfig struct { func NewConfig() *Config { return &Config{ - Telegram: TelegramConfig{ - Token: "", - }, Database: DatabaseConfig{ Type: "sqlite", Migration: true, @@ -96,7 +97,7 @@ func NewConfig() *Config { } func configEnvironmentOverrides(Config *Config) { if os.Getenv("DONETICK_TELEGRAM_TOKEN") != "" { - Config.Telegram.Token = os.Getenv("DONETICK_TELEGRAM_TOKEN") + Config.Notifiers.Telegram.Token = os.Getenv("DONETICK_TELEGRAM_TOKEN") } } func LoadConfig() *Config { 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 diff --git a/main.go b/main.go index e49f8ab..ab1176c 100644 --- a/main.go +++ b/main.go @@ -24,7 +24,7 @@ import ( notifier "donetick.com/core/internal/notifier" nRepo "donetick.com/core/internal/notifier/repo" nps "donetick.com/core/internal/notifier/service" - telegram "donetick.com/core/internal/notifier/telegram" + "donetick.com/core/internal/notifier/telegram" "donetick.com/core/internal/thing" tRepo "donetick.com/core/internal/thing/repo" "donetick.com/core/internal/user" @@ -60,8 +60,9 @@ func main() { fx.Provide(nRepo.NewNotificationRepository), fx.Provide(nps.NewNotificationPlanner), - // add notifier - fx.Provide(telegram.NewTelegramNotifier), + // add notifiers + fx.Provide(notifier.NewNotifiers), + fx.Supply(fx.Annotate(telegram.NewTelegramNotifier, fx.As(new(notifier.Notifier)))), // Rate limiter fx.Provide(utils.NewRateLimiter), -- cgit