getemail - use username if its an email addres

This commit is contained in:
Your Name 2024-01-26 12:31:58 -05:00
parent 00af249ff2
commit 972110aded
2 changed files with 20 additions and 1 deletions

View File

@ -161,7 +161,7 @@ func SendAlert(u int64, subject, body string) error {
if !ok {
return fmt.Errorf("scsusers.SendAlert: User %d not found", u)
}
email, ok := user.Get("email")
email, ok := user.GetEmail()
if !ok {
return fmt.Errorf("scsusers.SendAlert: User %d has no email", u)
}

19
meta.go
View File

@ -4,6 +4,7 @@ import (
"database/sql"
"fmt"
"log"
"net/mail"
)
type Metadata struct {
@ -11,6 +12,24 @@ type Metadata struct {
MetaValue string `db:"meta_value"`
}
func (u *UserData) GetEmail() (string, bool) {
_, err := mail.ParseAddress(u.Username)
if err == nil {
return u.Username, true
}
e, ok := u.Get("email")
if !ok {
return "", false
}
_, err = mail.ParseAddress(e)
if err != nil {
return "", false
}
return e, true
}
func (u *UserData) LoadMeta() bool {
q := fmt.Sprintf("select meta_key, meta_value, id from %s_meta where user=?", c.TablePrefix)
rows, err := c.db.Query(q, u.UserID)