some refactors for issue and comments (#2419)

This commit is contained in:
Lunny Xiao 2017-08-30 12:31:33 +08:00 committed by Lauris BH
parent edc817a1dc
commit 5de94a67cf
4 changed files with 30 additions and 14 deletions

View file

@ -1206,8 +1206,12 @@ func Issues(opts *IssuesOptions) ([]*Issue, error) {
// GetParticipantsByIssueID returns all users who are participated in comments of an issue.
func GetParticipantsByIssueID(issueID int64) ([]*User, error) {
return getParticipantsByIssueID(x, issueID)
}
func getParticipantsByIssueID(e Engine, issueID int64) ([]*User, error) {
userIDs := make([]int64, 0, 5)
if err := x.Table("comment").Cols("poster_id").
if err := e.Table("comment").Cols("poster_id").
Where("issue_id = ?", issueID).
And("type = ?", CommentTypeComment).
Distinct("poster_id").
@ -1219,7 +1223,7 @@ func GetParticipantsByIssueID(issueID int64) ([]*User, error) {
}
users := make([]*User, 0, len(userIDs))
return users, x.In("id", userIDs).Find(&users)
return users, e.In("id", userIDs).Find(&users)
}
// UpdateIssueMentions extracts mentioned people from content and

View file

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

View file

@ -22,18 +22,18 @@ func (issue *Issue) mailSubject() string {
// This function sends two list of emails:
// 1. Repository watchers and users who are participated in comments.
// 2. Users who are not in 1. but get mentioned in current issue/comment.
func mailIssueCommentToParticipants(issue *Issue, doer *User, comment *Comment, mentions []string) error {
func mailIssueCommentToParticipants(e Engine, issue *Issue, doer *User, comment *Comment, mentions []string) error {
if !setting.Service.EnableNotifyMail {
return nil
}
watchers, err := GetWatchers(issue.RepoID)
watchers, err := getWatchers(e, issue.RepoID)
if err != nil {
return fmt.Errorf("GetWatchers [repo_id: %d]: %v", issue.RepoID, err)
return fmt.Errorf("getWatchers [repo_id: %d]: %v", issue.RepoID, err)
}
participants, err := GetParticipantsByIssueID(issue.ID)
participants, err := getParticipantsByIssueID(e, issue.ID)
if err != nil {
return fmt.Errorf("GetParticipantsByIssueID [issue_id: %d]: %v", issue.ID, err)
return fmt.Errorf("getParticipantsByIssueID [issue_id: %d]: %v", issue.ID, err)
}
// In case the issue poster is not watching the repository,
@ -54,7 +54,7 @@ func mailIssueCommentToParticipants(issue *Issue, doer *User, comment *Comment,
continue
}
to, err := GetUserByID(watchers[i].UserID)
to, err := getUserByID(e, watchers[i].UserID)
if err != nil {
return fmt.Errorf("GetUserByID [%d]: %v", watchers[i].UserID, err)
}
@ -88,7 +88,7 @@ func mailIssueCommentToParticipants(issue *Issue, doer *User, comment *Comment,
tos = append(tos, mentions[i])
}
SendIssueMentionMail(issue, doer, comment, GetUserEmailsByNames(tos))
SendIssueMentionMail(issue, doer, comment, getUserEmailsByNames(e, tos))
return nil
}
@ -96,12 +96,16 @@ func mailIssueCommentToParticipants(issue *Issue, doer *User, comment *Comment,
// MailParticipants sends new issue thread created emails to repository watchers
// and mentioned people.
func (issue *Issue) MailParticipants() (err error) {
return issue.mailParticipants(x)
}
func (issue *Issue) mailParticipants(e Engine) (err error) {
mentions := markdown.FindAllMentions(issue.Content)
if err = UpdateIssueMentions(x, issue.ID, mentions); err != nil {
if err = UpdateIssueMentions(e, issue.ID, mentions); err != nil {
return fmt.Errorf("UpdateIssueMentions [%d]: %v", issue.ID, err)
}
if err = mailIssueCommentToParticipants(issue, issue.Poster, nil, mentions); err != nil {
if err = mailIssueCommentToParticipants(e, issue, issue.Poster, nil, mentions); err != nil {
log.Error(4, "mailIssueCommentToParticipants: %v", err)
}

View file

@ -1144,11 +1144,15 @@ func GetAssigneeByID(repo *Repository, userID int64) (*User, error) {
// GetUserByName returns user by given name.
func GetUserByName(name string) (*User, error) {
return getUserByName(x, name)
}
func getUserByName(e Engine, name string) (*User, error) {
if len(name) == 0 {
return nil, ErrUserNotExist{0, name, 0}
}
u := &User{LowerName: strings.ToLower(name)}
has, err := x.Get(u)
has, err := e.Get(u)
if err != nil {
return nil, err
} else if !has {
@ -1159,9 +1163,13 @@ func GetUserByName(name string) (*User, error) {
// GetUserEmailsByNames returns a list of e-mails corresponds to names.
func GetUserEmailsByNames(names []string) []string {
return getUserEmailsByNames(x, names)
}
func getUserEmailsByNames(e Engine, names []string) []string {
mails := make([]string, 0, len(names))
for _, name := range names {
u, err := GetUserByName(name)
u, err := getUserByName(e, name)
if err != nil {
continue
}