gosora/common/email_store.go
Azareal bf851bd9fc We now use Go 1.11 modules. This should help with build times, deployment and development, although it does mean that the minimum requirement for Gosora has been bumped up from Go 1.10 to Go 1.11
Added support for dyntmpl to the template system.
The Account Dashboard now sort of uses dyntmpl, more work needed here.
Renamed the pre_render_view_topic hook to pre_render_topic.
Added the GetCurrentLangPack() function.
Added the alerts_no_new_alerts phrase.
Added the account_level_list phrase.

Refactored the route rename logic in the patcher to cut down on the amount of boilerplate.
Added more route renames to the patcher. You will need to run the patcher / updater in this commit.
2018-10-27 13:40:36 +10:00

53 lines
1.3 KiB
Go

package common
import "database/sql"
import "github.com/Azareal/Gosora/query_gen"
var Emails EmailStore
type EmailStore interface {
GetEmailsByUser(user *User) (emails []Email, err error)
VerifyEmail(email string) error
}
type DefaultEmailStore struct {
getEmailsByUser *sql.Stmt
verifyEmail *sql.Stmt
}
func NewDefaultEmailStore(acc *qgen.Accumulator) (*DefaultEmailStore, error) {
return &DefaultEmailStore{
getEmailsByUser: acc.Select("emails").Columns("email, validated, token").Where("uid = ?").Prepare(),
// Need to fix this: Empty string isn't working, it gets set to 1 instead x.x -- Has this been fixed?
verifyEmail: acc.Update("emails").Set("validated = 1, token = ''").Where("email = ?").Prepare(),
}, acc.FirstError()
}
func (store *DefaultEmailStore) GetEmailsByUser(user *User) (emails []Email, err error) {
email := Email{UserID: user.ID}
rows, err := store.getEmailsByUser.Query(user.ID)
if err != nil {
return emails, err
}
defer rows.Close()
for rows.Next() {
err := rows.Scan(&email.Email, &email.Validated, &email.Token)
if err != nil {
return emails, err
}
if email.Email == user.Email {
email.Primary = true
}
emails = append(emails, email)
}
return emails, rows.Err()
}
func (store *DefaultEmailStore) VerifyEmail(email string) error {
_, err := store.verifyEmail.Exec(email)
return err
}