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 3.6KB

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