Fix sending notifications to users if the user object didn't have an email
This commit is contained in:
parent
58492fffce
commit
83f003355d
3 changed files with 21 additions and 7 deletions
|
@ -32,9 +32,7 @@ type Mail struct {
|
||||||
|
|
||||||
// NewMail creates a new mail object with a default greeting
|
// NewMail creates a new mail object with a default greeting
|
||||||
func NewMail() *Mail {
|
func NewMail() *Mail {
|
||||||
return &Mail{
|
return &Mail{}
|
||||||
greeting: "Hi,",
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// From sets the from name and email address
|
// From sets the from name and email address
|
||||||
|
|
|
@ -32,7 +32,7 @@ type Notification interface {
|
||||||
// Notifiable is an entity which can be notified. Usually a user.
|
// Notifiable is an entity which can be notified. Usually a user.
|
||||||
type Notifiable interface {
|
type Notifiable interface {
|
||||||
// Should return the email address this notifiable has.
|
// Should return the email address this notifiable has.
|
||||||
RouteForMail() string
|
RouteForMail() (string, error)
|
||||||
// Should return the id of the notifiable entity
|
// Should return the id of the notifiable entity
|
||||||
RouteForDB() int64
|
RouteForDB() int64
|
||||||
}
|
}
|
||||||
|
@ -73,7 +73,11 @@ func notifyMail(notifiable Notifiable, notification Notification) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
mail.To(notifiable.RouteForMail())
|
to, err := notifiable.RouteForMail()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
mail.To(to)
|
||||||
|
|
||||||
return SendMail(mail)
|
return SendMail(mail)
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,6 +17,7 @@
|
||||||
package user
|
package user
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"code.vikunja.io/api/pkg/db"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
@ -75,8 +76,19 @@ type User struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// RouteForMail routes all notifications for a user to its email address
|
// RouteForMail routes all notifications for a user to its email address
|
||||||
func (u *User) RouteForMail() string {
|
func (u *User) RouteForMail() (string, error) {
|
||||||
return u.Email
|
|
||||||
|
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
|
// RouteForDB routes all notifications for a user to their id
|
||||||
|
|
Loading…
Reference in a new issue