Skip to content

Commit d7d094b

Browse files
appleboylunny
authored andcommitted
fix: ignore email notifications if user is not active. (#820)
1 parent 2db0ffe commit d7d094b

File tree

3 files changed

+62
-2
lines changed

3 files changed

+62
-2
lines changed

models/fixtures/user.yml

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
salt: salt
1010
is_admin: true
1111
avatar: avatar1
12-
avatar_email: user2@example.com
12+
avatar_email: user1@example.com
1313
num_repos: 0
1414

1515
-
@@ -69,6 +69,7 @@
6969
avatar_email: [email protected]
7070
num_repos: 1
7171
allow_create_organization: false
72+
is_active: true
7273

7374
-
7475
id: 6
@@ -99,3 +100,35 @@
99100
avatar_email: [email protected]
100101
num_repos: 0
101102
num_members: 1
103+
104+
-
105+
id: 8
106+
lower_name: user8
107+
name: user8
108+
full_name: User Eight
109+
110+
passwd: password
111+
type: 0 # user
112+
salt: salt
113+
is_admin: false
114+
avatar: avatar8
115+
avatar_email: [email protected]
116+
num_repos: 0
117+
num_members: 1
118+
is_active: true
119+
120+
-
121+
id: 9
122+
lower_name: user9
123+
name: user9
124+
full_name: User Nine
125+
126+
passwd: password
127+
type: 0 # user
128+
salt: salt
129+
is_admin: false
130+
avatar: avatar9
131+
avatar_email: [email protected]
132+
num_repos: 0
133+
num_members: 1
134+
is_active: false

models/user.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,12 @@ func (u *User) ShortName(length int) string {
537537
return base.EllipsisString(u.Name, length)
538538
}
539539

540+
// IsMailable checks if a user is elegible
541+
// to receive emails.
542+
func (u *User) IsMailable() bool {
543+
return u.IsActive
544+
}
545+
540546
// IsUserExist checks if given user name exist,
541547
// the user name should be noncased unique.
542548
// If uid is presented, then check will rule out that one,
@@ -1047,7 +1053,9 @@ func GetUserEmailsByNames(names []string) []string {
10471053
if err != nil {
10481054
continue
10491055
}
1050-
mails = append(mails, u.Email)
1056+
if u.IsMailable() {
1057+
mails = append(mails, u.Email)
1058+
}
10511059
}
10521060
return mails
10531061
}

models/user_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright 2017 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package models
6+
7+
import (
8+
"testing"
9+
10+
"github.com/stretchr/testify/assert"
11+
)
12+
13+
func TestGetUserEmailsByNames(t *testing.T) {
14+
assert.NoError(t, PrepareTestDatabase())
15+
16+
// ignore none active user email
17+
assert.Equal(t, []string{"[email protected]"}, GetUserEmailsByNames([]string{"user8", "user9"}))
18+
assert.Equal(t, []string{"[email protected]", "[email protected]"}, GetUserEmailsByNames([]string{"user8", "user5"}))
19+
}

0 commit comments

Comments
 (0)