mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-11-15 22:38:31 +01:00
d8bc0495de
Enable [unparam](https://github.com/mvdan/unparam) linter. Often I could not tell the intention why param is unused, so I put `//nolint` for those cases like webhook request creation functions never using `ctx`. --------- Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com> Co-authored-by: delvh <dev.lh@web.de> (cherry picked from commit fc2d75f86d77b022ece848acf2581c14ef21d43b) Conflicts: modules/setting/config_env.go modules/storage/azureblob.go services/webhook/dingtalk.go services/webhook/discord.go services/webhook/feishu.go services/webhook/matrix.go services/webhook/msteams.go services/webhook/packagist.go services/webhook/slack.go services/webhook/telegram.go services/webhook/wechatwork.go run make lint-go and fix Forgejo specific warnings
78 lines
2 KiB
Go
78 lines
2 KiB
Go
// Copyright 2023 The Forgejo Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
package mailer
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"strconv"
|
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
|
"code.gitea.io/gitea/modules/base"
|
|
"code.gitea.io/gitea/modules/log"
|
|
"code.gitea.io/gitea/modules/setting"
|
|
"code.gitea.io/gitea/modules/templates"
|
|
"code.gitea.io/gitea/modules/translation"
|
|
)
|
|
|
|
const (
|
|
tplNewUserMail base.TplName = "notify/admin_new_user"
|
|
)
|
|
|
|
// MailNewUser sends notification emails on new user registrations to all admins
|
|
func MailNewUser(ctx context.Context, u *user_model.User) {
|
|
if !setting.Admin.SendNotificationEmailOnNewUser {
|
|
return
|
|
}
|
|
if setting.MailService == nil {
|
|
// No mail service configured
|
|
return
|
|
}
|
|
|
|
recipients, err := user_model.GetAllAdmins(ctx)
|
|
if err != nil {
|
|
log.Error("user_model.GetAllAdmins: %v", err)
|
|
return
|
|
}
|
|
|
|
langMap := make(map[string][]string)
|
|
for _, r := range recipients {
|
|
langMap[r.Language] = append(langMap[r.Language], r.Email)
|
|
}
|
|
|
|
for lang, tos := range langMap {
|
|
mailNewUser(ctx, u, lang, tos)
|
|
}
|
|
}
|
|
|
|
func mailNewUser(_ context.Context, u *user_model.User, lang string, tos []string) {
|
|
locale := translation.NewLocale(lang)
|
|
|
|
manageUserURL := setting.AppURL + "admin/users/" + strconv.FormatInt(u.ID, 10)
|
|
subject := locale.TrString("mail.admin.new_user.subject", u.Name)
|
|
body := locale.TrString("mail.admin.new_user.text", manageUserURL)
|
|
mailMeta := map[string]any{
|
|
"NewUser": u,
|
|
"NewUserUrl": u.HTMLURL(),
|
|
"Subject": subject,
|
|
"Body": body,
|
|
"Language": locale.Language(),
|
|
"Locale": locale,
|
|
"SanitizeHTML": templates.SanitizeHTML,
|
|
}
|
|
|
|
var mailBody bytes.Buffer
|
|
|
|
if err := bodyTemplates.ExecuteTemplate(&mailBody, string(tplNewUserMail), mailMeta); err != nil {
|
|
log.Error("ExecuteTemplate [%s]: %v", string(tplNewUserMail)+"/body", err)
|
|
return
|
|
}
|
|
|
|
msgs := make([]*Message, 0, len(tos))
|
|
for _, to := range tos {
|
|
msg := NewMessage(to, subject, mailBody.String())
|
|
msg.Info = subject
|
|
msgs = append(msgs, msg)
|
|
}
|
|
SendAsync(msgs...)
|
|
}
|