From 83f003355d9f9dae9778c9569db947dac08f2a63 Mon Sep 17 00:00:00 2001 From: kolaente Date: Sun, 14 Feb 2021 22:44:01 +0100 Subject: [PATCH] Fix sending notifications to users if the user object didn't have an email --- pkg/notifications/mail.go | 4 +--- pkg/notifications/notification.go | 8 ++++++-- pkg/user/user.go | 16 ++++++++++++++-- 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/pkg/notifications/mail.go b/pkg/notifications/mail.go index b7080ebcb..5becd57f9 100644 --- a/pkg/notifications/mail.go +++ b/pkg/notifications/mail.go @@ -32,9 +32,7 @@ type Mail struct { // NewMail creates a new mail object with a default greeting func NewMail() *Mail { - return &Mail{ - greeting: "Hi,", - } + return &Mail{} } // From sets the from name and email address diff --git a/pkg/notifications/notification.go b/pkg/notifications/notification.go index da3898c09..4d6bba46b 100644 --- a/pkg/notifications/notification.go +++ b/pkg/notifications/notification.go @@ -32,7 +32,7 @@ type Notification interface { // Notifiable is an entity which can be notified. Usually a user. type Notifiable interface { // Should return the email address this notifiable has. - RouteForMail() string + RouteForMail() (string, error) // Should return the id of the notifiable entity RouteForDB() int64 } @@ -73,7 +73,11 @@ func notifyMail(notifiable Notifiable, notification Notification) error { return nil } - mail.To(notifiable.RouteForMail()) + to, err := notifiable.RouteForMail() + if err != nil { + return err + } + mail.To(to) return SendMail(mail) } diff --git a/pkg/user/user.go b/pkg/user/user.go index d2077efe3..8f877e8da 100644 --- a/pkg/user/user.go +++ b/pkg/user/user.go @@ -17,6 +17,7 @@ package user import ( + "code.vikunja.io/api/pkg/db" "errors" "fmt" "reflect" @@ -75,8 +76,19 @@ type User struct { } // RouteForMail routes all notifications for a user to its email address -func (u *User) RouteForMail() string { - return u.Email +func (u *User) RouteForMail() (string, error) { + + if u.Email == "" { + s := db.NewSession() + defer s.Close() + user, err := getUser(s, &User{ID: u.ID}, true) + if err != nil { + return "", err + } + return user.Email, nil + } + + return u.Email, nil } // RouteForDB routes all notifications for a user to their id