[v14.0/forgejo] fix: decrease watch count when blocking user (#11060)

Fixes #10881

Call the proper function for each repository the user watches, so adjusting the watch count can be done properly.

Co-authored-by: christopher-besch <mail@chris-besch.com>
Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/11060
Reviewed-by: Gusted <gusted@noreply.codeberg.org>
Reviewed-by: Christopher Besch <mail@chris-besch.com>
Co-authored-by: forgejo-backport-action <forgejo-backport-action@noreply.codeberg.org>
Co-committed-by: forgejo-backport-action <forgejo-backport-action@noreply.codeberg.org>
This commit is contained in:
forgejo-backport-action 2026-01-27 13:57:57 +01:00 committed by Gusted
commit 7410ef5b9f
3 changed files with 15 additions and 5 deletions

View file

@ -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

View file

@ -185,6 +185,12 @@ func WatchIfAuto(ctx context.Context, userID, repoID int64, isWrite bool) 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
}

View file

@ -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) {