issue_mail.go 4.6 KB

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