Skip to content

Fix the missing users in the subscriptions endpoint. #26797

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions models/fixtures/comment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
-
id: 2
type: 0 # comment
poster_id: 3 # user not watching (see watch.yml)
poster_id: 3 # the user not explicitly not watching (see issue_watch.yml)
issue_id: 1 # in repo_id 1
content: "good work!"
created_unix: 946684811
updated_unix: 946684811
-
id: 3
type: 0 # comment
poster_id: 5 # user not watching (see watch.yml)
poster_id: 5 # the user not explicitly not watching (see issue_watch.yml)
issue_id: 1 # in repo_id 1
content: "meh..."
created_unix: 946684812
Expand Down
33 changes: 24 additions & 9 deletions models/issues/issue_watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
repo_model "code.gitea.io/gitea/models/repo"
user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/timeutil"

"xorm.io/builder"
)

// IssueWatch is connection request for receiving issue notification.
Expand Down Expand Up @@ -97,20 +99,33 @@ func GetIssueWatchersIDs(ctx context.Context, issueID int64, watching bool) ([]i
}

// GetIssueWatchers returns watchers/unwatchers of a given issue
func GetIssueWatchers(ctx context.Context, issueID int64, listOptions db.ListOptions) (IssueWatchList, error) {
sess := db.GetEngine(ctx).
Where("`issue_watch`.issue_id = ?", issueID).
And("`issue_watch`.is_watching = ?", true).
And("`user`.is_active = ?", true).
And("`user`.prohibit_login = ?", false).
Join("INNER", "`user`", "`user`.id = `issue_watch`.user_id")
func GetIssueWatchers(ctx context.Context, issueID int64, listOptions db.ListOptions) ([]int64, error) {
subscribeWatchers := builder.Select("`issue_watch`.user_id").
From("issue_watch").
Where(builder.Eq{"`issue_watch`.issue_id": issueID}).
And(builder.Eq{"`issue_watch`.is_watching": true})

participantsWatchers := builder.Select("`comment`.poster_id").
From("comment").
Where(builder.Eq{"`comment`.issue_id": issueID}).
And(builder.In("`comment`.type", CommentTypeComment, CommentTypeCode, CommentTypeReview)).
And(builder.NotIn("`comment`.poster_id",
builder.Select("`issue_watch`.user_id").
From("issue_watch").
Where(builder.Eq{"`issue_watch`.issue_id": issueID}).
And(builder.Eq{"`issue_watch`.is_watching": false})))

sess := db.GetEngine(ctx).Select("id").Table("user").
Where(builder.Or(builder.In("id", participantsWatchers), builder.In("id", subscribeWatchers))).
And(builder.Eq{"`user`.is_active": true}).
And(builder.Eq{"`user`.prohibit_login": false})

if listOptions.Page != 0 {
sess = db.SetSessionPagination(sess, &listOptions)
watches := make([]*IssueWatch, 0, listOptions.PageSize)
watches := make([]int64, 0, listOptions.PageSize)
return watches, sess.Find(&watches)
}
watches := make([]*IssueWatch, 0, 8)
watches := make([]int64, 0, 8)
return watches, sess.Find(&watches)
}

Expand Down
12 changes: 8 additions & 4 deletions models/issues/issue_watch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,17 @@ func TestGetIssueWatchers(t *testing.T) {

iws, err := issues_model.GetIssueWatchers(db.DefaultContext, 1, db.ListOptions{})
assert.NoError(t, err)
// Watcher is inactive, thus 0
assert.Len(t, iws, 0)
// user 9 watch it ,but user status is inactive, thus 0
// user 5 participates and is not explicitly not watching , thus 1
// total 1
assert.Len(t, iws, 1)

iws, err = issues_model.GetIssueWatchers(db.DefaultContext, 2, db.ListOptions{})
assert.NoError(t, err)
// Watcher is explicit not watching
assert.Len(t, iws, 0)
// user 2 in Watcher is explicitly not watching, thus 0
// user 1 participates and is not explicitly not watching , thus 1
// total 1
assert.Len(t, iws, 1)

iws, err = issues_model.GetIssueWatchers(db.DefaultContext, 5, db.ListOptions{})
assert.NoError(t, err)
Expand Down
7 changes: 1 addition & 6 deletions routers/api/v1/repo/issue_subscription.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,17 +262,12 @@ func GetIssueSubscribers(ctx *context.APIContext) {
return
}

iwl, err := issues_model.GetIssueWatchers(ctx, issue.ID, utils.GetListOptions(ctx))
userIDs, err := issues_model.GetIssueWatchers(ctx, issue.ID, utils.GetListOptions(ctx))
if err != nil {
ctx.Error(http.StatusInternalServerError, "GetIssueWatchers", err)
return
}

userIDs := make([]int64, 0, len(iwl))
for _, iw := range iwl {
userIDs = append(userIDs, iw.UserID)
}

users, err := user_model.GetUsersByIDs(userIDs)
if err != nil {
ctx.Error(http.StatusInternalServerError, "GetUsersByIDs", err)
Expand Down