issue_mail.go 4.3 KB

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