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