issue_mail.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 db
  5. import (
  6. "fmt"
  7. "github.com/unknwon/com"
  8. log "unknwon.dev/clog/v2"
  9. "gogs.io/gogs/internal/mailer"
  10. "gogs.io/gogs/internal/markup"
  11. "gogs.io/gogs/internal/conf"
  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, users who participated in comments and the assignee.
  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 !conf.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() || !to.IsActive {
  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. if issue.Assignee != nil && issue.Assignee.ID != doer.ID {
  117. if !com.IsSliceContainsStr(names, issue.Assignee.Name) {
  118. tos = append(tos, issue.Assignee.Email)
  119. names = append(names, issue.Assignee.Name)
  120. }
  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. log.Error("mailIssueCommentToParticipants: %v", err)
  144. }
  145. return nil
  146. }