diff options
author | cel 🌸 <cel@blos.sm> | 2024-09-10 17:33:54 +0100 |
---|---|---|
committer | cel 🌸 <cel@blos.sm> | 2024-09-10 17:33:54 +0100 |
commit | 9d04ff6a4d1dde541a0253e0db59957c69fec4cf (patch) | |
tree | 70c7f9a2e6b64eef21e5efe78012d2e3261f4de0 | |
parent | 6fb41ab9b491a257b027bf8a3c8a0828dc681b9a (diff) | |
download | donetick-9d04ff6a4d1dde541a0253e0db59957c69fec4cf.tar.gz donetick-9d04ff6a4d1dde541a0253e0db59957c69fec4cf.tar.bz2 donetick-9d04ff6a4d1dde541a0253e0db59957c69fec4cf.zip |
Diffstat (limited to '')
-rw-r--r-- | config/config.go | 11 | ||||
-rw-r--r-- | internal/notifier/notifier.go | 17 | ||||
-rw-r--r-- | internal/notifier/notifiers.go | 15 | ||||
-rw-r--r-- | internal/notifier/scheduler.go | 5 | ||||
-rw-r--r-- | internal/notifier/telegram/telegram.go | 2 | ||||
-rw-r--r-- | main.go | 7 |
6 files changed, 45 insertions, 12 deletions
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 @@ -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), |