Skip to content

Commit 9bf821a

Browse files
authored
Fix GetInactiveUsers (#32540)
Fix #31480
1 parent 9ac74a1 commit 9bf821a

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
@@ -333,6 +333,7 @@
333333
repo_admin_change_team_access: false
334334
theme: ""
335335
keep_activity_private: false
336+
created_unix: 1730468968
336337

337338
-
338339
id: 10

models/user/user.go

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

5050
// UserTypeOrganization defines an organization
51-
UserTypeOrganization
51+
UserTypeOrganization // 1
5252

5353
// 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
54-
UserTypeUserReserved
54+
UserTypeUserReserved // 2
5555

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

5959
// UserTypeBot defines a bot user
60-
UserTypeBot
60+
UserTypeBot // 4
6161

6262
// UserTypeRemoteUser defines a remote user for federated users
63-
UserTypeRemoteUser
63+
UserTypeRemoteUser // 5
6464
)
6565

6666
const (
@@ -884,7 +884,13 @@ func UpdateUserCols(ctx context.Context, u *User, cols ...string) error {
884884

885885
// GetInactiveUsers gets all inactive users
886886
func GetInactiveUsers(ctx context.Context, olderThan time.Duration) ([]*User, error) {
887-
var cond builder.Cond = builder.Eq{"is_active": false}
887+
cond := builder.And(
888+
builder.Eq{"is_active": false},
889+
builder.Or( // only plain user
890+
builder.Eq{"`type`": UserTypeIndividual},
891+
builder.Eq{"`type`": UserTypeUserReserved},
892+
),
893+
)
888894

889895
if olderThan > 0 {
890896
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
@@ -588,3 +588,17 @@ func TestDisabledUserFeatures(t *testing.T) {
588588
assert.True(t, user_model.IsFeatureDisabledWithLoginType(user, f))
589589
}
590590
}
591+
592+
func TestGetInactiveUsers(t *testing.T) {
593+
assert.NoError(t, unittest.PrepareTestDatabase())
594+
595+
// all inactive users
596+
// user1's createdunix is 1730468968
597+
users, err := user_model.GetInactiveUsers(db.DefaultContext, 0)
598+
assert.NoError(t, err)
599+
assert.Len(t, users, 1)
600+
interval := time.Now().Unix() - 1730468968 + 3600*24
601+
users, err = user_model.GetInactiveUsers(db.DefaultContext, time.Duration(interval*int64(time.Second)))
602+
assert.NoError(t, err)
603+
assert.Len(t, users, 0)
604+
}

0 commit comments

Comments
 (0)