issue_mail.go 4.7 KB

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