Skip to content

Commit 8f6cc95

Browse files
GiteaBotlunny
andauthored
Fix GetInactiveUsers (#32540) (#32588)
Backport #32540 by @lunny Fix #31480 Co-authored-by: Lunny Xiao <[email protected]>
1 parent 0b5da27 commit 8f6cc95

File tree

3 files changed

+27
-6
lines changed

3 files changed

+27
-6
lines changed

models/fixtures/user.yml

+1
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,7 @@
332332
repo_admin_change_team_access: false
333333
theme: ""
334334
keep_activity_private: false
335+
created_unix: 1730468968
335336

336337
-
337338
id: 10

models/user/user.go

+12-6
Original file line numberDiff line numberDiff line change
@@ -46,19 +46,19 @@ const (
4646
UserTypeIndividual UserType = iota // Historic reason to make it starts at 0.
4747

4848
// UserTypeOrganization defines an organization
49-
UserTypeOrganization
49+
UserTypeOrganization // 1
5050

5151
// UserTypeUserReserved reserves a (non-existing) user, i.e. to prevent a spam user from re-registering after being deleted, or to reserve the name until the user is actually created later on
52-
UserTypeUserReserved
52+
UserTypeUserReserved // 2
5353

5454
// UserTypeOrganizationReserved reserves a (non-existing) organization, to be used in combination with UserTypeUserReserved
55-
UserTypeOrganizationReserved
55+
UserTypeOrganizationReserved // 3
5656

5757
// UserTypeBot defines a bot user
58-
UserTypeBot
58+
UserTypeBot // 4
5959

6060
// UserTypeRemoteUser defines a remote user for federated users
61-
UserTypeRemoteUser
61+
UserTypeRemoteUser // 5
6262
)
6363

6464
const (
@@ -829,7 +829,13 @@ func UpdateUserCols(ctx context.Context, u *User, cols ...string) error {
829829

830830
// GetInactiveUsers gets all inactive users
831831
func GetInactiveUsers(ctx context.Context, olderThan time.Duration) ([]*User, error) {
832-
var cond builder.Cond = builder.Eq{"is_active": false}
832+
cond := builder.And(
833+
builder.Eq{"is_active": false},
834+
builder.Or( // only plain user
835+
builder.Eq{"`type`": UserTypeIndividual},
836+
builder.Eq{"`type`": UserTypeUserReserved},
837+
),
838+
)
833839

834840
if olderThan > 0 {
835841
cond = cond.And(builder.Lt{"created_unix": time.Now().Add(-olderThan).Unix()})

models/user/user_test.go

+14
Original file line numberDiff line numberDiff line change
@@ -562,3 +562,17 @@ func TestDisabledUserFeatures(t *testing.T) {
562562
assert.True(t, user_model.IsFeatureDisabledWithLoginType(user, f))
563563
}
564564
}
565+
566+
func TestGetInactiveUsers(t *testing.T) {
567+
assert.NoError(t, unittest.PrepareTestDatabase())
568+
569+
// all inactive users
570+
// user1's createdunix is 1730468968
571+
users, err := user_model.GetInactiveUsers(db.DefaultContext, 0)
572+
assert.NoError(t, err)
573+
assert.Len(t, users, 1)
574+
interval := time.Now().Unix() - 1730468968 + 3600*24
575+
users, err = user_model.GetInactiveUsers(db.DefaultContext, time.Duration(interval*int64(time.Second)))
576+
assert.NoError(t, err)
577+
assert.Len(t, users, 0)
578+
}

0 commit comments

Comments
 (0)