From d9545c503ea332eb567b052701f273361cbb4639 Mon Sep 17 00:00:00 2001 From: christopher-besch Date: Mon, 26 Jan 2026 22:58:22 +0100 Subject: [PATCH] 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 Co-authored-by: christopher-besch Co-committed-by: christopher-besch --- .deadcode-out | 3 --- models/repo/watch.go | 10 ++++++++-- services/user/block_test.go | 7 +++++++ 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/.deadcode-out b/.deadcode-out index 20f13a03a1..97093ce93b 100644 --- a/.deadcode-out +++ b/.deadcode-out @@ -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 diff --git a/models/repo/watch.go b/models/repo/watch.go index ceec20fdd6..3a20880a5c 100644 --- a/models/repo/watch.go +++ b/models/repo/watch.go @@ -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 } diff --git a/services/user/block_test.go b/services/user/block_test.go index b9f23a7cda..f23a5cf91a 100644 --- a/services/user/block_test.go +++ b/services/user/block_test.go @@ -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) {