You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

issue_mail.go 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. // Copyright 2016 The Gogs Authors. All rights reserved.
  2. // Copyright 2018 The Gitea Authors. All rights reserved.
  3. // Use of this source code is governed by a MIT-style
  4. // license that can be found in the LICENSE file.
  5. package models
  6. import (
  7. "fmt"
  8. "code.gitea.io/gitea/modules/log"
  9. "code.gitea.io/gitea/modules/markup"
  10. "code.gitea.io/gitea/modules/setting"
  11. "github.com/unknwon/com"
  12. )
  13. func (issue *Issue) mailSubject() string {
  14. return fmt.Sprintf("[%s] %s (#%d)", issue.Repo.FullName(), issue.Title, issue.Index)
  15. }
  16. // mailIssueCommentToParticipants can be used for both new issue creation and comment.
  17. // This function sends two list of emails:
  18. // 1. Repository watchers and users who are participated in comments.
  19. // 2. Users who are not in 1. but get mentioned in current issue/comment.
  20. func mailIssueCommentToParticipants(e Engine, issue *Issue, doer *User, content string, comment *Comment, mentions []string) error {
  21. if !setting.Service.EnableNotifyMail {
  22. return nil
  23. }
  24. watchers, err := getWatchers(e, issue.RepoID)
  25. if err != nil {
  26. return fmt.Errorf("getWatchers [repo_id: %d]: %v", issue.RepoID, err)
  27. }
  28. participants, err := getParticipantsByIssueID(e, issue.ID)
  29. if err != nil {
  30. return fmt.Errorf("getParticipantsByIssueID [issue_id: %d]: %v", issue.ID, err)
  31. }
  32. // In case the issue poster is not watching the repository and is active,
  33. // even if we have duplicated in watchers, can be safely filtered out.
  34. err = issue.loadPoster(e)
  35. if err != nil {
  36. return fmt.Errorf("GetUserByID [%d]: %v", issue.PosterID, err)
  37. }
  38. if issue.PosterID != doer.ID && issue.Poster.IsActive && !issue.Poster.ProhibitLogin {
  39. participants = append(participants, issue.Poster)
  40. }
  41. // Assignees must receive any communications
  42. assignees, err := getAssigneesByIssue(e, issue)
  43. if err != nil {
  44. return err
  45. }
  46. for _, assignee := range assignees {
  47. if assignee.ID != doer.ID {
  48. participants = append(participants, assignee)
  49. }
  50. }
  51. tos := make([]string, 0, len(watchers)) // List of email addresses.
  52. names := make([]string, 0, len(watchers))
  53. for i := range watchers {
  54. if watchers[i].UserID == doer.ID {
  55. continue
  56. }
  57. to, err := getUserByID(e, watchers[i].UserID)
  58. if err != nil {
  59. return fmt.Errorf("GetUserByID [%d]: %v", watchers[i].UserID, err)
  60. }
  61. if to.IsOrganization() {
  62. continue
  63. }
  64. tos = append(tos, to.Email)
  65. names = append(names, to.Name)
  66. }
  67. for i := range participants {
  68. if participants[i].ID == doer.ID {
  69. continue
  70. } else if com.IsSliceContainsStr(names, participants[i].Name) {
  71. continue
  72. }
  73. tos = append(tos, participants[i].Email)
  74. names = append(names, participants[i].Name)
  75. }
  76. if err := issue.loadRepo(e); err != nil {
  77. return err
  78. }
  79. for _, to := range tos {
  80. SendIssueCommentMail(issue, doer, content, comment, []string{to})
  81. }
  82. // Mail mentioned people and exclude watchers.
  83. names = append(names, doer.Name)
  84. tos = make([]string, 0, len(mentions)) // list of user names.
  85. for i := range mentions {
  86. if com.IsSliceContainsStr(names, mentions[i]) {
  87. continue
  88. }
  89. tos = append(tos, mentions[i])
  90. }
  91. emails := getUserEmailsByNames(e, tos)
  92. for _, to := range emails {
  93. SendIssueMentionMail(issue, doer, content, comment, []string{to})
  94. }
  95. return nil
  96. }
  97. // MailParticipants sends new issue thread created emails to repository watchers
  98. // and mentioned people.
  99. func (issue *Issue) MailParticipants(doer *User, opType ActionType) (err error) {
  100. return issue.mailParticipants(x, doer, opType)
  101. }
  102. func (issue *Issue) mailParticipants(e Engine, doer *User, opType ActionType) (err error) {
  103. mentions := markup.FindAllMentions(issue.Content)
  104. if err = UpdateIssueMentions(e, issue.ID, mentions); err != nil {
  105. return fmt.Errorf("UpdateIssueMentions [%d]: %v", issue.ID, err)
  106. }
  107. if len(issue.Content) > 0 {
  108. if err = mailIssueCommentToParticipants(e, issue, doer, issue.Content, nil, mentions); err != nil {
  109. log.Error("mailIssueCommentToParticipants: %v", err)
  110. }
  111. }
  112. switch opType {
  113. case ActionCreateIssue, ActionCreatePullRequest:
  114. if len(issue.Content) == 0 {
  115. ct := fmt.Sprintf("Created #%d.", issue.Index)
  116. if err = mailIssueCommentToParticipants(e, issue, doer, ct, nil, mentions); err != nil {
  117. log.Error("mailIssueCommentToParticipants: %v", err)
  118. }
  119. }
  120. case ActionCloseIssue, ActionClosePullRequest:
  121. ct := fmt.Sprintf("Closed #%d.", issue.Index)
  122. if err = mailIssueCommentToParticipants(e, issue, doer, ct, nil, mentions); err != nil {
  123. log.Error("mailIssueCommentToParticipants: %v", err)
  124. }
  125. case ActionReopenIssue, ActionReopenPullRequest:
  126. ct := fmt.Sprintf("Reopened #%d.", issue.Index)
  127. if err = mailIssueCommentToParticipants(e, issue, doer, ct, nil, mentions); err != nil {
  128. log.Error("mailIssueCommentToParticipants: %v", err)
  129. }
  130. }
  131. return nil
  132. }