issue_mail.go 4.5 KB

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