loading up the forgejo repo on tangled to test page performance
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Fix send mail (#13312)

* Fix send mail

* Fix send mail

* Update modules/private/mail.go

Co-authored-by: techknowlogick <matti@mdranta.net>

authored by

Lunny Xiao
techknowlogick
and committed by
GitHub
38d11eea dbebc6b0

+36 -6
+5 -2
cmd/mailer.go
··· 9 9 "net/http" 10 10 11 11 "code.gitea.io/gitea/modules/private" 12 + "code.gitea.io/gitea/modules/setting" 12 13 "github.com/urfave/cli" 13 14 ) 14 15 15 16 func runSendMail(c *cli.Context) error { 17 + setting.NewContext() 18 + 16 19 if err := argsSet(c, "title"); err != nil { 17 20 return err 18 21 } ··· 38 41 39 42 status, message := private.SendEmail(subject, body, nil) 40 43 if status != http.StatusOK { 41 - fmt.Printf("error: %s", message) 44 + fmt.Printf("error: %s\n", message) 42 45 return nil 43 46 } 44 47 45 - fmt.Printf("Succseded: %s", message) 48 + fmt.Printf("Success: %s\n", message) 46 49 47 50 return nil 48 51 }
+6 -1
modules/private/mail.go
··· 49 49 return http.StatusInternalServerError, fmt.Sprintf("Response body error: %v", err.Error()) 50 50 } 51 51 52 - return http.StatusOK, fmt.Sprintf("Was sent %s from %d", body, len(to)) 52 + var users = fmt.Sprintf("%d", len(to)) 53 + if len(to) == 0 { 54 + users = "all" 55 + } 56 + 57 + return http.StatusOK, fmt.Sprintf("Sent %s email(s) to %s users", body, users) 53 58 }
+25 -3
routers/private/mail.go
··· 5 5 package private 6 6 7 7 import ( 8 + "encoding/json" 8 9 "fmt" 9 10 "net/http" 10 11 "strconv" ··· 12 13 "code.gitea.io/gitea/models" 13 14 "code.gitea.io/gitea/modules/log" 14 15 "code.gitea.io/gitea/modules/private" 16 + "code.gitea.io/gitea/modules/setting" 15 17 "code.gitea.io/gitea/services/mailer" 16 18 "gitea.com/macaron/macaron" 17 19 ) ··· 19 21 // SendEmail pushes messages to mail queue 20 22 // 21 23 // It doesn't wait before each message will be processed 22 - func SendEmail(ctx *macaron.Context, mail private.Email) { 24 + func SendEmail(ctx *macaron.Context) { 25 + if setting.MailService == nil { 26 + ctx.JSON(http.StatusInternalServerError, map[string]interface{}{ 27 + "err": "Mail service is not enabled.", 28 + }) 29 + return 30 + } 31 + 32 + var mail private.Email 33 + rd := ctx.Req.Body().ReadCloser() 34 + defer rd.Close() 35 + if err := json.NewDecoder(rd).Decode(&mail); err != nil { 36 + log.Error("%v", err) 37 + ctx.JSON(http.StatusInternalServerError, map[string]interface{}{ 38 + "err": err, 39 + }) 40 + return 41 + } 42 + 23 43 var emails []string 24 44 if len(mail.To) > 0 { 25 45 for _, uname := range mail.To { ··· 33 53 return 34 54 } 35 55 36 - if user != nil { 56 + if user != nil && len(user.Email) > 0 { 37 57 emails = append(emails, user.Email) 38 58 } 39 59 } 40 60 } else { 41 61 err := models.IterateUser(func(user *models.User) error { 42 - emails = append(emails, user.Email) 62 + if len(user.Email) > 0 { 63 + emails = append(emails, user.Email) 64 + } 43 65 return nil 44 66 }) 45 67 if err != nil {