1
0
mirror of https://github.com/hrfee/jfa-go.git synced 2024-12-23 01:20:11 +00:00

use bulk email sending on account deletion

This commit is contained in:
Harvey Tindall 2021-02-19 14:51:36 +00:00
parent ca00796077
commit b25f786018
Signed by: hrfee
GPG Key ID: BBC65952848FB1A2
2 changed files with 16 additions and 12 deletions

26
api.go
View File

@ -539,8 +539,9 @@ func (app *appContext) Announce(gc *gin.Context) {
// @Router /users [delete] // @Router /users [delete]
// @Security Bearer // @Security Bearer
// @tags Users // @tags Users
func (app *appContext) DeleteUser(gc *gin.Context) { func (app *appContext) DeleteUsers(gc *gin.Context) {
var req deleteUserDTO var req deleteUserDTO
var addresses []string
gc.BindJSON(&req) gc.BindJSON(&req)
errors := map[string]string{} errors := map[string]string{}
ombiEnabled := app.config.Section("ombi").Key("enabled").MustBool(false) ombiEnabled := app.config.Section("ombi").Key("enabled").MustBool(false)
@ -569,19 +570,22 @@ func (app *appContext) DeleteUser(gc *gin.Context) {
if emailEnabled && req.Notify { if emailEnabled && req.Notify {
addr, ok := app.storage.emails[userID] addr, ok := app.storage.emails[userID]
if addr != nil && ok { if addr != nil && ok {
go func(userID, reason, address string) { addresses = append(addresses, addr.(string))
msg, err := app.email.constructDeleted(reason, app)
if err != nil {
app.err.Printf("%s: Failed to construct account deletion email: %s", userID, err)
} else if err := app.email.send(msg, address); err != nil {
app.err.Printf("%s: Failed to send to %s: %s", userID, address, err)
} else {
app.info.Printf("%s: Sent deletion email to %s", userID, address)
}
}(userID, req.Reason, addr.(string))
} }
} }
} }
if len(addresses) != 0 {
go func(reason string, addresses []string) {
msg, err := app.email.constructDeleted(reason, app)
if err != nil {
app.err.Printf("Failed to construct account deletion emails: %s", err)
} else if err := app.email.send(msg, addresses...); err != nil {
app.err.Printf("Failed to send account deletion emails: %s", err)
} else {
app.info.Println("Sent account deletion emails")
}
}(req.Reason, addresses)
}
app.jf.CacheExpiry = time.Now() app.jf.CacheExpiry = time.Now()
if len(errors) == len(req.Users) { if len(errors) == len(req.Users) {
respondBool(500, false, gc) respondBool(500, false, gc)

View File

@ -123,7 +123,7 @@ func (app *appContext) loadRoutes(router *gin.Engine) {
api := router.Group("/", app.webAuth()) api := router.Group("/", app.webAuth())
for _, p := range routePrefixes { for _, p := range routePrefixes {
router.POST(p+"/logout", app.Logout) router.POST(p+"/logout", app.Logout)
api.DELETE(p+"/users", app.DeleteUser) api.DELETE(p+"/users", app.DeleteUsers)
api.GET(p+"/users", app.GetUsers) api.GET(p+"/users", app.GetUsers)
api.POST(p+"/users", app.NewUserAdmin) api.POST(p+"/users", app.NewUserAdmin)
api.POST(p+"/invites", app.GenerateInvite) api.POST(p+"/invites", app.GenerateInvite)