[GITEA] notifies admins on new user registration (squash) performance bottleneck

Refs: https://codeberg.org/forgejo/forgejo/issues/1479
(cherry picked from commit 97ac9147ff)
This commit is contained in:
JakobDev 2023-09-19 18:40:01 +02:00 committed by Earl Warren
parent 283f964894
commit 19f295c16b
No known key found for this signature in database
GPG key ID: 0579CB2928A78A00
3 changed files with 19 additions and 5 deletions

View file

@ -223,6 +223,12 @@ func GetAllUsers(ctx context.Context) ([]*User, error) {
return users, db.GetEngine(ctx).OrderBy("id").Where("type = ?", UserTypeIndividual).Find(&users) 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. // IsLocal returns true if user login type is LoginPlain.
func (u *User) IsLocal() bool { func (u *User) IsLocal() bool {
return u.LoginType <= auth.Plain return u.LoginType <= auth.Plain

View file

@ -544,3 +544,13 @@ func Test_ValidateUser(t *testing.T) {
assert.EqualValues(t, expected, err == nil, fmt.Sprintf("case: %+v", kase)) 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)
}

View file

@ -32,18 +32,16 @@ func MailNewUser(ctx context.Context, u *user_model.User) {
return return
} }
recipients, err := user_model.GetAllUsers(ctx) recipients, err := user_model.GetAllAdmins(ctx)
if err != nil { if err != nil {
log.Error("user_model.GetAllUsers: %v", err) log.Error("user_model.GetAllAdmins: %v", err)
return return
} }
langMap := make(map[string][]string) langMap := make(map[string][]string)
for _, r := range recipients { 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 { for lang, tos := range langMap {
mailNewUser(ctx, u, lang, tos) mailNewUser(ctx, u, lang, tos)