issue_mail.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. // Copyright 2015 - Present, The Gogs Authors. All rights reserved.
  2. // Copyright 2018 - Present, Gitote. All rights reserved.
  3. //
  4. // This source code is licensed under the MIT license found in the
  5. // LICENSE file in the root directory of this source tree.
  6. package models
  7. import (
  8. "fmt"
  9. "gitote/gitote/pkg/mailer"
  10. "gitote/gitote/pkg/markup"
  11. "gitote/gitote/pkg/setting"
  12. raven "github.com/getsentry/raven-go"
  13. "gitlab.com/gitote/com"
  14. log "gopkg.in/clog.v1"
  15. )
  16. // MailSubject returns subject of mail.
  17. func (issue *Issue) MailSubject() string {
  18. return fmt.Sprintf("[%s] %s (#%d)", issue.Repo.Name, issue.Title, issue.Index)
  19. }
  20. // mailerUser is a wrapper for satisfying mailer.User interface.
  21. type mailerUser struct {
  22. user *User
  23. }
  24. func (t mailerUser) ID() int64 {
  25. return t.user.ID
  26. }
  27. func (t mailerUser) DisplayName() string {
  28. return t.user.DisplayName()
  29. }
  30. func (t mailerUser) Email() string {
  31. return t.user.Email
  32. }
  33. func (t mailerUser) GenerateActivateCode() string {
  34. return t.user.GenerateActivateCode()
  35. }
  36. func (t mailerUser) GenerateEmailActivateCode(email string) string {
  37. return t.user.GenerateEmailActivateCode(email)
  38. }
  39. // NewMailerUser returns mailerUser
  40. func NewMailerUser(u *User) mailer.User {
  41. return mailerUser{u}
  42. }
  43. // mailerRepo is a wrapper for satisfying mailer.Repository interface.
  44. type mailerRepo struct {
  45. repo *Repository
  46. }
  47. func (t mailerRepo) FullName() string {
  48. return t.repo.FullName()
  49. }
  50. func (t mailerRepo) HTMLURL() string {
  51. return t.repo.HTMLURL()
  52. }
  53. func (t mailerRepo) ComposeMetas() map[string]string {
  54. return t.repo.ComposeMetas()
  55. }
  56. // NewMailerRepo returns mailerRepo
  57. func NewMailerRepo(repo *Repository) mailer.Repository {
  58. return mailerRepo{repo}
  59. }
  60. // mailerIssue is a wrapper for satisfying mailer.Issue interface.
  61. type mailerIssue struct {
  62. issue *Issue
  63. }
  64. func (t mailerIssue) MailSubject() string {
  65. return t.issue.MailSubject()
  66. }
  67. func (t mailerIssue) Content() string {
  68. return t.issue.Content
  69. }
  70. func (t mailerIssue) HTMLURL() string {
  71. return t.issue.HTMLURL()
  72. }
  73. // NewMailerIssue returns mailerIssue
  74. func NewMailerIssue(issue *Issue) mailer.Issue {
  75. return mailerIssue{issue}
  76. }
  77. // mailIssueCommentToParticipants can be used for both new issue creation and comment.
  78. // This functions sends two list of emails:
  79. // 1. Repository watchers, users who participated in comments and the assignee.
  80. // 2. Users who are not in 1. but get mentioned in current issue/comment.
  81. func mailIssueCommentToParticipants(issue *Issue, doer *User, mentions []string) error {
  82. if !setting.Service.EnableNotifyMail {
  83. return nil
  84. }
  85. watchers, err := GetWatchers(issue.RepoID)
  86. if err != nil {
  87. return fmt.Errorf("GetWatchers [repo_id: %d]: %v", issue.RepoID, err)
  88. }
  89. participants, err := GetParticipantsByIssueID(issue.ID)
  90. if err != nil {
  91. return fmt.Errorf("GetParticipantsByIssueID [issue_id: %d]: %v", issue.ID, err)
  92. }
  93. // In case the issue poster is not watching the repository,
  94. // even if we have duplicated in watchers, can be safely filtered out.
  95. if issue.PosterID != doer.ID {
  96. participants = append(participants, issue.Poster)
  97. }
  98. tos := make([]string, 0, len(watchers)) // List of email addresses
  99. names := make([]string, 0, len(watchers))
  100. for i := range watchers {
  101. if watchers[i].UserID == doer.ID {
  102. continue
  103. }
  104. to, err := GetUserByID(watchers[i].UserID)
  105. if err != nil {
  106. return fmt.Errorf("GetUserByID [%d]: %v", watchers[i].UserID, err)
  107. }
  108. if to.IsOrganization() {
  109. continue
  110. }
  111. tos = append(tos, to.Email)
  112. names = append(names, to.Name)
  113. }
  114. for i := range participants {
  115. if participants[i].ID == doer.ID {
  116. continue
  117. } else if com.IsSliceContainsStr(names, participants[i].Name) {
  118. continue
  119. }
  120. tos = append(tos, participants[i].Email)
  121. names = append(names, participants[i].Name)
  122. }
  123. if issue.Assignee != nil && issue.Assignee.ID != doer.ID {
  124. if !com.IsSliceContainsStr(names, issue.Assignee.Name) {
  125. tos = append(tos, issue.Assignee.Email)
  126. names = append(names, issue.Assignee.Name)
  127. }
  128. }
  129. mailer.SendIssueCommentMail(NewMailerIssue(issue), NewMailerRepo(issue.Repo), NewMailerUser(doer), tos)
  130. // Mail mentioned people and exclude watchers.
  131. names = append(names, doer.Name)
  132. tos = make([]string, 0, len(mentions)) // list of user names.
  133. for i := range mentions {
  134. if com.IsSliceContainsStr(names, mentions[i]) {
  135. continue
  136. }
  137. tos = append(tos, mentions[i])
  138. }
  139. mailer.SendIssueMentionMail(NewMailerIssue(issue), NewMailerRepo(issue.Repo), NewMailerUser(doer), GetUserEmailsByNames(tos))
  140. return nil
  141. }
  142. // MailParticipants sends new issue thread created emails to repository watchers
  143. // and mentioned people.
  144. func (issue *Issue) MailParticipants() (err error) {
  145. mentions := markup.FindAllMentions(issue.Content)
  146. if err = updateIssueMentions(x, issue.ID, mentions); err != nil {
  147. return fmt.Errorf("UpdateIssueMentions [%d]: %v", issue.ID, err)
  148. }
  149. if err = mailIssueCommentToParticipants(issue, issue.Poster, mentions); err != nil {
  150. raven.CaptureErrorAndWait(err, nil)
  151. log.Error(2, "mailIssueCommentToParticipants: %v", err)
  152. }
  153. return nil
  154. }