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.

132 lines
3.7 KiB

  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. "github.com/Unknwon/com"
  9. "code.gitea.io/gitea/modules/log"
  10. "code.gitea.io/gitea/modules/markup"
  11. "code.gitea.io/gitea/modules/setting"
  12. )
  13. func (issue *Issue) mailSubject() string {
  14. return fmt.Sprintf("[%s] %s (#%d)", issue.Repo.Name, 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. poster, err := GetUserByID(issue.PosterID)
  35. if err != nil {
  36. return fmt.Errorf("GetUserByID [%d]: %v", issue.PosterID, err)
  37. }
  38. if issue.PosterID != doer.ID && poster.IsActive && !poster.ProhibitLogin {
  39. participants = append(participants, issue.Poster)
  40. }
  41. // Assignees must receive any communications
  42. assignees, err := GetAssigneesByIssue(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. for _, to := range tos {
  77. SendIssueCommentMail(issue, doer, content, comment, []string{to})
  78. }
  79. // Mail mentioned people and exclude watchers.
  80. names = append(names, doer.Name)
  81. tos = make([]string, 0, len(mentions)) // list of user names.
  82. for i := range mentions {
  83. if com.IsSliceContainsStr(names, mentions[i]) {
  84. continue
  85. }
  86. tos = append(tos, mentions[i])
  87. }
  88. emails := getUserEmailsByNames(e, tos)
  89. for _, to := range emails {
  90. SendIssueMentionMail(issue, doer, content, comment, []string{to})
  91. }
  92. return nil
  93. }
  94. // MailParticipants sends new issue thread created emails to repository watchers
  95. // and mentioned people.
  96. func (issue *Issue) MailParticipants() (err error) {
  97. return issue.mailParticipants(x)
  98. }
  99. func (issue *Issue) mailParticipants(e Engine) (err error) {
  100. mentions := markup.FindAllMentions(issue.Content)
  101. if err = UpdateIssueMentions(e, issue.ID, mentions); err != nil {
  102. return fmt.Errorf("UpdateIssueMentions [%d]: %v", issue.ID, err)
  103. }
  104. if err = mailIssueCommentToParticipants(e, issue, issue.Poster, issue.Content, nil, mentions); err != nil {
  105. log.Error(4, "mailIssueCommentToParticipants: %v", err)
  106. }
  107. return nil
  108. }