Skip to content

Commit f95baa0

Browse files
committed
refactor getParticipantIDsByIssue
1 parent b3fae7a commit f95baa0

File tree

3 files changed

+30
-41
lines changed

3 files changed

+30
-41
lines changed

models/issue.go

Lines changed: 23 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1275,43 +1275,9 @@ 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-
issue, err := getIssueByID(x, issueID)
1281-
if err != nil {
1282-
return nil, err
1283-
}
1284-
userIDs, err := getParticipantsByIssueID(x, issue)
1285-
if err != nil {
1286-
return nil, err
1287-
}
1288-
if len(userIDs) == 0 {
1289-
return nil, nil
1290-
}
1291-
return GetUsersByIDs(userIDs)
1292-
}
1293-
1294-
func getParticipantsByIssueID(e Engine, issue *Issue) ([]int64, error) {
1295-
userIDs := make([]int64, 0, 5)
1296-
if err := e.Table("comment").Cols("poster_id").
1297-
Where("`comment`.issue_id = ?", issue.ID).
1298-
And("`comment`.type in (?,?,?)", CommentTypeComment, CommentTypeCode, CommentTypeReview).
1299-
And("`user`.is_active = ?", true).
1300-
And("`user`.prohibit_login = ?", false).
1301-
Join("INNER", "`user`", "`user`.id = `comment`.poster_id").
1302-
Distinct("poster_id").
1303-
Find(&userIDs); err != nil {
1304-
return nil, fmt.Errorf("get poster IDs: %v", err)
1305-
}
1306-
if !util.IsInt64InSlice(issue.PosterID, userIDs) {
1307-
return append(userIDs, issue.PosterID), nil
1308-
}
1309-
return userIDs, nil
1310-
}
1311-
13121278
// IsUserParticipantsOfIssue return true if user is participants of an issue
13131279
func IsUserParticipantsOfIssue(user *User, issue *Issue) bool {
1314-
userIDs, err := getParticipantsByIssueID(x, issue)
1280+
userIDs, err := issue.getParticipantIDsByIssue(x)
13151281
if err != nil {
13161282
log.Error(err.Error())
13171283
return false
@@ -1710,6 +1676,28 @@ type DependencyInfo struct {
17101676
Repository `xorm:"extends"`
17111677
}
17121678

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+
17131701
// Get Blocked By Dependencies, aka all issues this issue is blocked by.
17141702
func (issue *Issue) getBlockedByDependencies(e Engine) (issueDeps []*DependencyInfo, err error) {
17151703
return issueDeps, e.

models/issue_test.go

Lines changed: 6 additions & 4 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)

models/notification.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,7 @@ func createOrUpdateIssueNotifications(e Engine, issueID, commentID int64, notifi
159159
for _, id := range repoWatches {
160160
toNotify[id] = struct{}{}
161161
}
162-
163-
issueParticipants, err := getParticipantsByIssueID(e, issue)
162+
issueParticipants, err := issue.getParticipantIDsByIssue(e)
164163
if err != nil {
165164
return err
166165
}

0 commit comments

Comments
 (0)