Skip to content

Commit 5de94a6

Browse files
lunnylafriks
authored andcommitted
some refactors for issue and comments (#2419)
1 parent edc817a commit 5de94a6

File tree

4 files changed

+30
-14
lines changed

4 files changed

+30
-14
lines changed

models/issue.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1206,8 +1206,12 @@ func Issues(opts *IssuesOptions) ([]*Issue, error) {
12061206

12071207
// GetParticipantsByIssueID returns all users who are participated in comments of an issue.
12081208
func GetParticipantsByIssueID(issueID int64) ([]*User, error) {
1209+
return getParticipantsByIssueID(x, issueID)
1210+
}
1211+
1212+
func getParticipantsByIssueID(e Engine, issueID int64) ([]*User, error) {
12091213
userIDs := make([]int64, 0, 5)
1210-
if err := x.Table("comment").Cols("poster_id").
1214+
if err := e.Table("comment").Cols("poster_id").
12111215
Where("issue_id = ?", issueID).
12121216
And("type = ?", CommentTypeComment).
12131217
Distinct("poster_id").
@@ -1219,7 +1223,7 @@ func GetParticipantsByIssueID(issueID int64) ([]*User, error) {
12191223
}
12201224

12211225
users := make([]*User, 0, len(userIDs))
1222-
return users, x.In("id", userIDs).Find(&users)
1226+
return users, e.In("id", userIDs).Find(&users)
12231227
}
12241228

12251229
// UpdateIssueMentions extracts mentioned people from content and

models/issue_comment.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ func (c *Comment) MailParticipants(e Engine, opType ActionType, issue *Issue) (e
289289
case ActionReopenIssue:
290290
issue.Content = fmt.Sprintf("Reopened #%d", issue.Index)
291291
}
292-
if err = mailIssueCommentToParticipants(issue, c.Poster, c, mentions); err != nil {
292+
if err = mailIssueCommentToParticipants(e, issue, c.Poster, c, mentions); err != nil {
293293
log.Error(4, "mailIssueCommentToParticipants: %v", err)
294294
}
295295

models/issue_mail.go

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,18 @@ func (issue *Issue) mailSubject() string {
2222
// This function sends two list of emails:
2323
// 1. Repository watchers and users who are participated in comments.
2424
// 2. Users who are not in 1. but get mentioned in current issue/comment.
25-
func mailIssueCommentToParticipants(issue *Issue, doer *User, comment *Comment, mentions []string) error {
25+
func mailIssueCommentToParticipants(e Engine, issue *Issue, doer *User, comment *Comment, mentions []string) error {
2626
if !setting.Service.EnableNotifyMail {
2727
return nil
2828
}
2929

30-
watchers, err := GetWatchers(issue.RepoID)
30+
watchers, err := getWatchers(e, issue.RepoID)
3131
if err != nil {
32-
return fmt.Errorf("GetWatchers [repo_id: %d]: %v", issue.RepoID, err)
32+
return fmt.Errorf("getWatchers [repo_id: %d]: %v", issue.RepoID, err)
3333
}
34-
participants, err := GetParticipantsByIssueID(issue.ID)
34+
participants, err := getParticipantsByIssueID(e, issue.ID)
3535
if err != nil {
36-
return fmt.Errorf("GetParticipantsByIssueID [issue_id: %d]: %v", issue.ID, err)
36+
return fmt.Errorf("getParticipantsByIssueID [issue_id: %d]: %v", issue.ID, err)
3737
}
3838

3939
// In case the issue poster is not watching the repository,
@@ -54,7 +54,7 @@ func mailIssueCommentToParticipants(issue *Issue, doer *User, comment *Comment,
5454
continue
5555
}
5656

57-
to, err := GetUserByID(watchers[i].UserID)
57+
to, err := getUserByID(e, watchers[i].UserID)
5858
if err != nil {
5959
return fmt.Errorf("GetUserByID [%d]: %v", watchers[i].UserID, err)
6060
}
@@ -88,20 +88,24 @@ func mailIssueCommentToParticipants(issue *Issue, doer *User, comment *Comment,
8888

8989
tos = append(tos, mentions[i])
9090
}
91-
SendIssueMentionMail(issue, doer, comment, GetUserEmailsByNames(tos))
91+
SendIssueMentionMail(issue, doer, comment, getUserEmailsByNames(e, tos))
9292

9393
return nil
9494
}
9595

9696
// MailParticipants sends new issue thread created emails to repository watchers
9797
// and mentioned people.
9898
func (issue *Issue) MailParticipants() (err error) {
99+
return issue.mailParticipants(x)
100+
}
101+
102+
func (issue *Issue) mailParticipants(e Engine) (err error) {
99103
mentions := markdown.FindAllMentions(issue.Content)
100-
if err = UpdateIssueMentions(x, issue.ID, mentions); err != nil {
104+
if err = UpdateIssueMentions(e, issue.ID, mentions); err != nil {
101105
return fmt.Errorf("UpdateIssueMentions [%d]: %v", issue.ID, err)
102106
}
103107

104-
if err = mailIssueCommentToParticipants(issue, issue.Poster, nil, mentions); err != nil {
108+
if err = mailIssueCommentToParticipants(e, issue, issue.Poster, nil, mentions); err != nil {
105109
log.Error(4, "mailIssueCommentToParticipants: %v", err)
106110
}
107111

models/user.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1144,11 +1144,15 @@ func GetAssigneeByID(repo *Repository, userID int64) (*User, error) {
11441144

11451145
// GetUserByName returns user by given name.
11461146
func GetUserByName(name string) (*User, error) {
1147+
return getUserByName(x, name)
1148+
}
1149+
1150+
func getUserByName(e Engine, name string) (*User, error) {
11471151
if len(name) == 0 {
11481152
return nil, ErrUserNotExist{0, name, 0}
11491153
}
11501154
u := &User{LowerName: strings.ToLower(name)}
1151-
has, err := x.Get(u)
1155+
has, err := e.Get(u)
11521156
if err != nil {
11531157
return nil, err
11541158
} else if !has {
@@ -1159,9 +1163,13 @@ func GetUserByName(name string) (*User, error) {
11591163

11601164
// GetUserEmailsByNames returns a list of e-mails corresponds to names.
11611165
func GetUserEmailsByNames(names []string) []string {
1166+
return getUserEmailsByNames(x, names)
1167+
}
1168+
1169+
func getUserEmailsByNames(e Engine, names []string) []string {
11621170
mails := make([]string, 0, len(names))
11631171
for _, name := range names {
1164-
u, err := GetUserByName(name)
1172+
u, err := getUserByName(e, name)
11651173
if err != nil {
11661174
continue
11671175
}

0 commit comments

Comments
 (0)