Skip to content

Commit 694f446

Browse files
authored
inform participants on UI too (#10473)
* inform participants on UI too * ajust test * refactor getParticipantIDsByIssue
1 parent 513b962 commit 694f446

File tree

5 files changed

+54
-28
lines changed

5 files changed

+54
-28
lines changed

models/issue.go

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1275,29 +1275,14 @@ func GetParticipantsIDsByIssueID(issueID int64) ([]int64, error) {
12751275
Find(&userIDs)
12761276
}
12771277

1278-
// GetParticipantsByIssueID returns all users who are participated in comments of an issue.
1279-
func GetParticipantsByIssueID(issueID int64) ([]*User, error) {
1280-
return getParticipantsByIssueID(x, issueID)
1281-
}
1282-
1283-
func getParticipantsByIssueID(e Engine, issueID int64) ([]*User, error) {
1284-
userIDs := make([]int64, 0, 5)
1285-
if err := e.Table("comment").Cols("poster_id").
1286-
Where("`comment`.issue_id = ?", issueID).
1287-
And("`comment`.type in (?,?,?)", CommentTypeComment, CommentTypeCode, CommentTypeReview).
1288-
And("`user`.is_active = ?", true).
1289-
And("`user`.prohibit_login = ?", false).
1290-
Join("INNER", "`user`", "`user`.id = `comment`.poster_id").
1291-
Distinct("poster_id").
1292-
Find(&userIDs); err != nil {
1293-
return nil, fmt.Errorf("get poster IDs: %v", err)
1294-
}
1295-
if len(userIDs) == 0 {
1296-
return nil, nil
1278+
// IsUserParticipantsOfIssue return true if user is participants of an issue
1279+
func IsUserParticipantsOfIssue(user *User, issue *Issue) bool {
1280+
userIDs, err := issue.getParticipantIDsByIssue(x)
1281+
if err != nil {
1282+
log.Error(err.Error())
1283+
return false
12971284
}
1298-
1299-
users := make([]*User, 0, len(userIDs))
1300-
return users, e.In("id", userIDs).Find(&users)
1285+
return util.IsInt64InSlice(user.ID, userIDs)
13011286
}
13021287

13031288
// UpdateIssueMentions updates issue-user relations for mentioned users.
@@ -1691,6 +1676,28 @@ type DependencyInfo struct {
16911676
Repository `xorm:"extends"`
16921677
}
16931678

1679+
// getParticipantIDsByIssue returns all userIDs who are participated in comments of an issue and issue author
1680+
func (issue *Issue) getParticipantIDsByIssue(e Engine) ([]int64, error) {
1681+
if issue == nil {
1682+
return nil, nil
1683+
}
1684+
userIDs := make([]int64, 0, 5)
1685+
if err := e.Table("comment").Cols("poster_id").
1686+
Where("`comment`.issue_id = ?", issue.ID).
1687+
And("`comment`.type in (?,?,?)", CommentTypeComment, CommentTypeCode, CommentTypeReview).
1688+
And("`user`.is_active = ?", true).
1689+
And("`user`.prohibit_login = ?", false).
1690+
Join("INNER", "`user`", "`user`.id = `comment`.poster_id").
1691+
Distinct("poster_id").
1692+
Find(&userIDs); err != nil {
1693+
return nil, fmt.Errorf("get poster IDs: %v", err)
1694+
}
1695+
if !util.IsInt64InSlice(issue.PosterID, userIDs) {
1696+
return append(userIDs, issue.PosterID), nil
1697+
}
1698+
return userIDs, nil
1699+
}
1700+
16941701
// Get Blocked By Dependencies, aka all issues this issue is blocked by.
16951702
func (issue *Issue) getBlockedByDependencies(e Engine) (issueDeps []*DependencyInfo, err error) {
16961703
return issueDeps, e.

models/issue_test.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,17 @@ func TestGetIssuesByIDs(t *testing.T) {
6161
testSuccess([]int64{1, 2, 3}, []int64{NonexistentID})
6262
}
6363

64-
func TestGetParticipantsByIssueID(t *testing.T) {
64+
func TestGetParticipantIDsByIssue(t *testing.T) {
6565
assert.NoError(t, PrepareTestDatabase())
6666

6767
checkParticipants := func(issueID int64, userIDs []int) {
68-
participants, err := GetParticipantsByIssueID(issueID)
68+
issue, err := GetIssueByID(issueID)
69+
assert.NoError(t, err)
70+
participants, err := issue.getParticipantIDsByIssue(x)
6971
if assert.NoError(t, err) {
7072
participantsIDs := make([]int, len(participants))
71-
for i, u := range participants {
72-
participantsIDs[i] = int(u.ID)
73+
for i, uid := range participants {
74+
participantsIDs[i] = int(uid)
7375
}
7476
sort.Ints(participantsIDs)
7577
sort.Ints(userIDs)
@@ -81,7 +83,7 @@ func TestGetParticipantsByIssueID(t *testing.T) {
8183
// User 2 only labeled issue1 (see fixtures/comment.yml)
8284
// Users 3 and 5 made actual comments (see fixtures/comment.yml)
8385
// User 3 is inactive, thus not active participant
84-
checkParticipants(1, []int{5})
86+
checkParticipants(1, []int{1, 5})
8587
}
8688

8789
func TestIssue_ClearLabels(t *testing.T) {

models/notification.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,13 @@ func createOrUpdateIssueNotifications(e Engine, issueID, commentID int64, notifi
159159
for _, id := range repoWatches {
160160
toNotify[id] = struct{}{}
161161
}
162+
issueParticipants, err := issue.getParticipantIDsByIssue(e)
163+
if err != nil {
164+
return err
165+
}
166+
for _, id := range issueParticipants {
167+
toNotify[id] = struct{}{}
168+
}
162169

163170
// dont notify user who cause notification
164171
delete(toNotify, notificationAuthorID)

modules/util/compare.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,16 @@ func IsStringInSlice(target string, slice []string) bool {
4545
return false
4646
}
4747

48+
// IsInt64InSlice sequential searches if int64 exists in slice.
49+
func IsInt64InSlice(target int64, slice []int64) bool {
50+
for i := 0; i < len(slice); i++ {
51+
if slice[i] == target {
52+
return true
53+
}
54+
}
55+
return false
56+
}
57+
4858
// IsEqualSlice returns true if slices are equal.
4959
func IsEqualSlice(target []string, source []string) bool {
5060
if len(target) != len(source) {

routers/repo/issue.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -704,7 +704,7 @@ func ViewIssue(ctx *context.Context) {
704704
iw = &models.IssueWatch{
705705
UserID: ctx.User.ID,
706706
IssueID: issue.ID,
707-
IsWatching: models.IsWatching(ctx.User.ID, ctx.Repo.Repository.ID),
707+
IsWatching: models.IsWatching(ctx.User.ID, ctx.Repo.Repository.ID) || models.IsUserParticipantsOfIssue(ctx.User, issue),
708708
}
709709
}
710710
}

0 commit comments

Comments
 (0)