More adjusting email sending behavior

This commit is contained in:
Noah Petherbridge 2024-09-09 20:59:46 -07:00
parent 463253dbb5
commit 79ea384d40
4 changed files with 27 additions and 15 deletions

View File

@ -50,7 +50,11 @@ const (
ResetPasswordRedisKey = "reset-password/%s" ResetPasswordRedisKey = "reset-password/%s"
ChangeEmailRedisKey = "change-email/%s" ChangeEmailRedisKey = "change-email/%s"
SignupTokenExpires = 24 * time.Hour // used for all tokens so far SignupTokenExpires = 24 * time.Hour // used for all tokens so far
EmailDebounceExpires = 24 * time.Hour // how to rate limit duplicate mail being sent
// How to rate limit same types of emails being delivered, e.g.
// signups, cert approvals (double post), etc.
EmailDebounceDefault = 24 * time.Hour // default debounce per type of email
EmailDebounceResetPassword = 4 * time.Hour // "forgot password" emails debounce
// Rate limits // Rate limits
RateLimitRedisKey = "rate-limit/%s/%s" // namespace, id RateLimitRedisKey = "rate-limit/%s/%s" // namespace, id

View File

@ -135,17 +135,25 @@ func ForgotPassword() http.HandlerFunc {
return return
} }
// Email them their reset link. // Email them their reset link -- if not banned.
if err := mail.Send(mail.Message{ if !user.IsBanned() {
To: user.Email, if err := mail.LockSending("reset_password", user.Email, config.EmailDebounceResetPassword); err == nil {
Subject: "Reset your forgotten password", if err := mail.Send(mail.Message{
Template: "email/reset_password.html", To: user.Email,
Data: map[string]interface{}{ Subject: "Reset your forgotten password",
"Username": user.Username, Template: "email/reset_password.html",
"URL": config.Current.BaseURL + "/forgot-password?token=" + token.Token, Data: map[string]interface{}{
}, "Username": user.Username,
}); err != nil { "URL": config.Current.BaseURL + "/forgot-password?token=" + token.Token,
session.FlashError(w, r, "Error sending an email: %s", err) },
}); err != nil {
session.FlashError(w, r, "Error sending an email: %s", err)
}
} else {
log.Error("LockSending: reset_password e-mail is not sent to %s: one was sent recently", user.Email)
}
} else {
log.Error("Do not send 'forgot password' e-mail to %s: user is banned", user.Email)
} }
// Success message and redirect away. // Success message and redirect away.

View File

@ -145,7 +145,7 @@ func Signup() http.HandlerFunc {
if user.IsBanned() { if user.IsBanned() {
log.Error("Do not send signup e-mail to %s: user is banned", email) log.Error("Do not send signup e-mail to %s: user is banned", email)
} else { } else {
if err := mail.LockSending("signup", email, config.EmailDebounceExpires); err == nil { if err := mail.LockSending("signup", email, config.EmailDebounceDefault); err == nil {
err := mail.Send(mail.Message{ err := mail.Send(mail.Message{
To: email, To: email,
Subject: "You already have a nonshy account", Subject: "You already have a nonshy account",

View File

@ -434,7 +434,7 @@ func AdminCertification() http.HandlerFunc {
} }
// Notify the user via email. // Notify the user via email.
if err := mail.LockSending("cert_rejected", user.Email, config.EmailDebounceExpires); err == nil { if err := mail.LockSending("cert_rejected", user.Email, config.EmailDebounceDefault); err == nil {
if err := mail.Send(mail.Message{ if err := mail.Send(mail.Message{
To: user.Email, To: user.Email,
Subject: "Your certification photo has been denied", Subject: "Your certification photo has been denied",
@ -507,7 +507,7 @@ func AdminCertification() http.HandlerFunc {
} }
// Notify the user via email. // Notify the user via email.
if err := mail.LockSending("cert_approved", user.Email, config.EmailDebounceExpires); err == nil { if err := mail.LockSending("cert_approved", user.Email, config.EmailDebounceDefault); err == nil {
if err := mail.Send(mail.Message{ if err := mail.Send(mail.Message{
To: user.Email, To: user.Email,
Subject: "Your certification photo has been approved!", Subject: "Your certification photo has been approved!",