Browse Source

some refactors for issue and comments (#2419)

Lunny Xiao 3 years ago
parent
commit
5de94a67cf
4 changed files with 30 additions and 14 deletions
  1. 6 2
      models/issue.go
  2. 1 1
      models/issue_comment.go
  3. 13 9
      models/issue_mail.go
  4. 10 2
      models/user.go

+ 6 - 2
models/issue.go

@@ -1206,8 +1206,12 @@ func Issues(opts *IssuesOptions) ([]*Issue, error) {
1206 1206
 
1207 1207
 // GetParticipantsByIssueID returns all users who are participated in comments of an issue.
1208 1208
 func GetParticipantsByIssueID(issueID int64) ([]*User, error) {
1209
+	return getParticipantsByIssueID(x, issueID)
1210
+}
1211
+
1212
+func getParticipantsByIssueID(e Engine, issueID int64) ([]*User, error) {
1209 1213
 	userIDs := make([]int64, 0, 5)
1210
-	if err := x.Table("comment").Cols("poster_id").
1214
+	if err := e.Table("comment").Cols("poster_id").
1211 1215
 		Where("issue_id = ?", issueID).
1212 1216
 		And("type = ?", CommentTypeComment).
1213 1217
 		Distinct("poster_id").
@@ -1219,7 +1223,7 @@ func GetParticipantsByIssueID(issueID int64) ([]*User, error) {
1219 1223
 	}
1220 1224
 
1221 1225
 	users := make([]*User, 0, len(userIDs))
1222
-	return users, x.In("id", userIDs).Find(&users)
1226
+	return users, e.In("id", userIDs).Find(&users)
1223 1227
 }
1224 1228
 
1225 1229
 // UpdateIssueMentions extracts mentioned people from content and

+ 1 - 1
models/issue_comment.go

@@ -289,7 +289,7 @@ func (c *Comment) MailParticipants(e Engine, opType ActionType, issue *Issue) (e
289 289
 	case ActionReopenIssue:
290 290
 		issue.Content = fmt.Sprintf("Reopened #%d", issue.Index)
291 291
 	}
292
-	if err = mailIssueCommentToParticipants(issue, c.Poster, c, mentions); err != nil {
292
+	if err = mailIssueCommentToParticipants(e, issue, c.Poster, c, mentions); err != nil {
293 293
 		log.Error(4, "mailIssueCommentToParticipants: %v", err)
294 294
 	}
295 295
 

+ 13 - 9
models/issue_mail.go

@@ -22,18 +22,18 @@ func (issue *Issue) mailSubject() string {
22 22
 // This function sends two list of emails:
23 23
 // 1. Repository watchers and users who are participated in comments.
24 24
 // 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 {
26 26
 	if !setting.Service.EnableNotifyMail {
27 27
 		return nil
28 28
 	}
29 29
 
30
-	watchers, err := GetWatchers(issue.RepoID)
30
+	watchers, err := getWatchers(e, issue.RepoID)
31 31
 	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)
33 33
 	}
34
-	participants, err := GetParticipantsByIssueID(issue.ID)
34
+	participants, err := getParticipantsByIssueID(e, issue.ID)
35 35
 	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)
37 37
 	}
38 38
 
39 39
 	// In case the issue poster is not watching the repository,
@@ -54,7 +54,7 @@ func mailIssueCommentToParticipants(issue *Issue, doer *User, comment *Comment,
54 54
 			continue
55 55
 		}
56 56
 
57
-		to, err := GetUserByID(watchers[i].UserID)
57
+		to, err := getUserByID(e, watchers[i].UserID)
58 58
 		if err != nil {
59 59
 			return fmt.Errorf("GetUserByID [%d]: %v", watchers[i].UserID, err)
60 60
 		}
@@ -88,7 +88,7 @@ func mailIssueCommentToParticipants(issue *Issue, doer *User, comment *Comment,
88 88
 
89 89
 		tos = append(tos, mentions[i])
90 90
 	}
91
-	SendIssueMentionMail(issue, doer, comment, GetUserEmailsByNames(tos))
91
+	SendIssueMentionMail(issue, doer, comment, getUserEmailsByNames(e, tos))
92 92
 
93 93
 	return nil
94 94
 }
@@ -96,12 +96,16 @@ func mailIssueCommentToParticipants(issue *Issue, doer *User, comment *Comment,
96 96
 // MailParticipants sends new issue thread created emails to repository watchers
97 97
 // and mentioned people.
98 98
 func (issue *Issue) MailParticipants() (err error) {
99
+	return issue.mailParticipants(x)
100
+}
101
+
102
+func (issue *Issue) mailParticipants(e Engine) (err error) {
99 103
 	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 {
101 105
 		return fmt.Errorf("UpdateIssueMentions [%d]: %v", issue.ID, err)
102 106
 	}
103 107
 
104
-	if err = mailIssueCommentToParticipants(issue, issue.Poster, nil, mentions); err != nil {
108
+	if err = mailIssueCommentToParticipants(e, issue, issue.Poster, nil, mentions); err != nil {
105 109
 		log.Error(4, "mailIssueCommentToParticipants: %v", err)
106 110
 	}
107 111
 

+ 10 - 2
models/user.go

@@ -1144,11 +1144,15 @@ func GetAssigneeByID(repo *Repository, userID int64) (*User, error) {
1144 1144
 
1145 1145
 // GetUserByName returns user by given name.
1146 1146
 func GetUserByName(name string) (*User, error) {
1147
+	return getUserByName(x, name)
1148
+}
1149
+
1150
+func getUserByName(e Engine, name string) (*User, error) {
1147 1151
 	if len(name) == 0 {
1148 1152
 		return nil, ErrUserNotExist{0, name, 0}
1149 1153
 	}
1150 1154
 	u := &User{LowerName: strings.ToLower(name)}
1151
-	has, err := x.Get(u)
1155
+	has, err := e.Get(u)
1152 1156
 	if err != nil {
1153 1157
 		return nil, err
1154 1158
 	} else if !has {
@@ -1159,9 +1163,13 @@ func GetUserByName(name string) (*User, error) {
1159 1163
 
1160 1164
 // GetUserEmailsByNames returns a list of e-mails corresponds to names.
1161 1165
 func GetUserEmailsByNames(names []string) []string {
1166
+	return getUserEmailsByNames(x, names)
1167
+}
1168
+
1169
+func getUserEmailsByNames(e Engine, names []string) []string {
1162 1170
 	mails := make([]string, 0, len(names))
1163 1171
 	for _, name := range names {
1164
-		u, err := GetUserByName(name)
1172
+		u, err := getUserByName(e, name)
1165 1173
 		if err != nil {
1166 1174
 			continue
1167 1175
 		}