issue_mail.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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/modules/mailer"
  10. "github.com/gogits/gogs/modules/markdown"
  11. "github.com/gogits/gogs/modules/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. tos := make([]string, 0, len(watchers)) // List of email addresses.
  87. names := make([]string, 0, len(watchers))
  88. for i := range watchers {
  89. if watchers[i].UserID == doer.ID {
  90. continue
  91. }
  92. to, err := GetUserByID(watchers[i].UserID)
  93. if err != nil {
  94. return fmt.Errorf("GetUserByID [%d]: %v", watchers[i].UserID, err)
  95. }
  96. if to.IsOrganization() {
  97. continue
  98. }
  99. tos = append(tos, to.Email)
  100. names = append(names, to.Name)
  101. }
  102. for i := range participants {
  103. if participants[i].ID == doer.ID {
  104. continue
  105. } else if com.IsSliceContainsStr(names, participants[i].Name) {
  106. continue
  107. }
  108. tos = append(tos, participants[i].Email)
  109. names = append(names, participants[i].Name)
  110. }
  111. mailer.SendIssueCommentMail(NewMailerIssue(issue), NewMailerRepo(issue.Repo), NewMailerUser(doer), tos)
  112. // Mail mentioned people and exclude watchers.
  113. names = append(names, doer.Name)
  114. tos = make([]string, 0, len(mentions)) // list of user names.
  115. for i := range mentions {
  116. if com.IsSliceContainsStr(names, mentions[i]) {
  117. continue
  118. }
  119. tos = append(tos, mentions[i])
  120. }
  121. mailer.SendIssueMentionMail(NewMailerIssue(issue), NewMailerRepo(issue.Repo), NewMailerUser(doer), GetUserEmailsByNames(tos))
  122. return nil
  123. }
  124. // MailParticipants sends new issue thread created emails to repository watchers
  125. // and mentioned people.
  126. func (issue *Issue) MailParticipants() (err error) {
  127. mentions := markdown.FindAllMentions(issue.Content)
  128. if err = updateIssueMentions(x, issue.ID, mentions); err != nil {
  129. return fmt.Errorf("UpdateIssueMentions [%d]: %v", issue.ID, err)
  130. }
  131. if err = mailIssueCommentToParticipants(issue, issue.Poster, mentions); err != nil {
  132. log.Error(4, "mailIssueCommentToParticipants: %v", err)
  133. }
  134. return nil
  135. }