fix: decrease watch count when blocking user (#10882)
Fixes #10881 Call the proper function for each repository the user watches, so adjusting the watch count can be done properly. Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/10882 Reviewed-by: Gusted <gusted@noreply.codeberg.org> Co-authored-by: christopher-besch <mail@chris-besch.com> Co-committed-by: christopher-besch <mail@chris-besch.com>
This commit is contained in:
committed by
Gusted
parent
023a894677
commit
d9545c503e
@@ -50,9 +50,6 @@ forgejo.org/models/organization
|
||||
forgejo.org/models/perm/access
|
||||
GetRepoWriters
|
||||
|
||||
forgejo.org/models/repo
|
||||
WatchRepoMode
|
||||
|
||||
forgejo.org/models/user
|
||||
IsErrUserWrongType
|
||||
IsErrExternalLoginUserAlreadyExist
|
||||
|
||||
@@ -194,6 +194,12 @@ func WatchIfAuto(ctx context.Context, userID, repoID int64) error {
|
||||
|
||||
// UnwatchRepos will unwatch the user from all given repositories.
|
||||
func UnwatchRepos(ctx context.Context, userID int64, repoIDs []int64) error {
|
||||
_, err := db.GetEngine(ctx).Where("user_id=?", userID).In("repo_id", repoIDs).Delete(&Watch{})
|
||||
return err
|
||||
// Unfortunatly, we can't simply delete the Watch records because we do watcher counting in the repo relation.
|
||||
for _, repoID := range repoIDs {
|
||||
err := WatchRepoMode(ctx, userID, repoID, WatchModeNone)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -48,10 +48,17 @@ func TestBlockUser(t *testing.T) {
|
||||
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{OwnerID: doer.ID})
|
||||
require.NoError(t, repo_model.WatchRepo(db.DefaultContext, blockedUser.ID, repo.ID, true))
|
||||
|
||||
repo = unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{OwnerID: doer.ID})
|
||||
oldNumWatchers := repo.NumWatches
|
||||
|
||||
require.NoError(t, BlockUser(db.DefaultContext, doer.ID, blockedUser.ID))
|
||||
|
||||
// Ensure blocked user isn't following doer's repository.
|
||||
assert.False(t, repo_model.IsWatching(db.DefaultContext, blockedUser.ID, repo.ID))
|
||||
|
||||
// Ensure the watcher count was reduced by one.
|
||||
repo = unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{OwnerID: doer.ID})
|
||||
require.Equal(t, oldNumWatchers-1, repo.NumWatches)
|
||||
})
|
||||
|
||||
t.Run("Collaboration", func(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user