issue_mail.go 4.4 KB

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