From 19f295c16bd392aa438477fa3c42038d63d1a06a Mon Sep 17 00:00:00 2001 From: JakobDev Date: Tue, 19 Sep 2023 18:40:01 +0200 Subject: [PATCH] [GITEA] notifies admins on new user registration (squash) performance bottleneck Refs: https://codeberg.org/forgejo/forgejo/issues/1479 (cherry picked from commit 97ac9147ff3643cca0a059688c6b3c53479e28a7) --- models/user/user.go | 6 ++++++ models/user/user_test.go | 10 ++++++++++ services/mailer/mail_admin_new_user.go | 8 +++----- 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/models/user/user.go b/models/user/user.go index 63b95816ce..a3bdff33d2 100644 --- a/models/user/user.go +++ b/models/user/user.go @@ -223,6 +223,12 @@ func GetAllUsers(ctx context.Context) ([]*User, error) { return users, db.GetEngine(ctx).OrderBy("id").Where("type = ?", UserTypeIndividual).Find(&users) } +// GetAllAdmins returns a slice of all adminusers found in DB. +func GetAllAdmins(ctx context.Context) ([]*User, error) { + users := make([]*User, 0) + return users, db.GetEngine(ctx).OrderBy("id").Where("type = ?", UserTypeIndividual).And("is_admin = ?", true).Find(&users) +} + // IsLocal returns true if user login type is LoginPlain. func (u *User) IsLocal() bool { return u.LoginType <= auth.Plain diff --git a/models/user/user_test.go b/models/user/user_test.go index 971117482c..fed2cb2c48 100644 --- a/models/user/user_test.go +++ b/models/user/user_test.go @@ -544,3 +544,13 @@ func Test_ValidateUser(t *testing.T) { assert.EqualValues(t, expected, err == nil, fmt.Sprintf("case: %+v", kase)) } } + +func TestGetAllAdmins(t *testing.T) { + assert.NoError(t, unittest.PrepareTestDatabase()) + + admins, err := user_model.GetAllAdmins(db.DefaultContext) + assert.NoError(t, err) + + assert.Len(t, admins, 1) + assert.Equal(t, int64(1), admins[0].ID) +} diff --git a/services/mailer/mail_admin_new_user.go b/services/mailer/mail_admin_new_user.go index 0ddff152bc..fbed615b67 100644 --- a/services/mailer/mail_admin_new_user.go +++ b/services/mailer/mail_admin_new_user.go @@ -32,17 +32,15 @@ func MailNewUser(ctx context.Context, u *user_model.User) { return } - recipients, err := user_model.GetAllUsers(ctx) + recipients, err := user_model.GetAllAdmins(ctx) if err != nil { - log.Error("user_model.GetAllUsers: %v", err) + log.Error("user_model.GetAllAdmins: %v", err) return } langMap := make(map[string][]string) for _, r := range recipients { - if r.IsAdmin { - langMap[r.Language] = append(langMap[r.Language], r.Email) - } + langMap[r.Language] = append(langMap[r.Language], r.Email) } for lang, tos := range langMap {