issue_mail.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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.gogs file.
  4. package database
  5. import (
  6. "context"
  7. "fmt"
  8. "github.com/pkg/errors"
  9. "github.com/unknwon/com"
  10. log "unknwon.dev/clog/v2"
  11. "gogs.io/gogs/internal/conf"
  12. "gogs.io/gogs/internal/email"
  13. "gogs.io/gogs/internal/markup"
  14. "gogs.io/gogs/internal/userutil"
  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) PublicEmail() string {
  33. return this.user.PublicEmail
  34. }
  35. func (this mailerUser) GenerateEmailActivateCode(email string) string {
  36. return userutil.GenerateActivateCode(
  37. this.user.ID,
  38. email,
  39. this.user.Name,
  40. this.user.Password,
  41. this.user.Rands,
  42. )
  43. }
  44. func NewMailerUser(u *User) email.User {
  45. return mailerUser{u}
  46. }
  47. // mailerRepo is a wrapper for satisfying mailer.Repository interface.
  48. type mailerRepo struct {
  49. repo *Repository
  50. }
  51. func (this mailerRepo) FullName() string {
  52. return this.repo.FullName()
  53. }
  54. func (this mailerRepo) HTMLURL() string {
  55. return this.repo.HTMLURL()
  56. }
  57. func (this mailerRepo) ComposeMetas() map[string]string {
  58. return this.repo.ComposeMetas()
  59. }
  60. func NewMailerRepo(repo *Repository) email.Repository {
  61. return mailerRepo{repo}
  62. }
  63. // mailerIssue is a wrapper for satisfying mailer.Issue interface.
  64. type mailerIssue struct {
  65. issue *Issue
  66. }
  67. func (this mailerIssue) MailSubject() string {
  68. return this.issue.MailSubject()
  69. }
  70. func (this mailerIssue) Content() string {
  71. return this.issue.Content
  72. }
  73. func (this mailerIssue) HTMLURL() string {
  74. return this.issue.HTMLURL()
  75. }
  76. func NewMailerIssue(issue *Issue) email.Issue {
  77. return mailerIssue{issue}
  78. }
  79. // mailIssueCommentToParticipants can be used for both new issue creation and comment.
  80. // This functions sends two list of emails:
  81. // 1. Repository watchers, users who participated in comments and the assignee.
  82. // 2. Users who are not in 1. but get mentioned in current issue/comment.
  83. func mailIssueCommentToParticipants(issue *Issue, doer *User, mentions []string) error {
  84. ctx := context.TODO()
  85. if !conf.User.EnableEmailNotification {
  86. return nil
  87. }
  88. watchers, err := GetWatchers(issue.RepoID)
  89. if err != nil {
  90. return fmt.Errorf("GetWatchers [repo_id: %d]: %v", issue.RepoID, err)
  91. }
  92. participants, err := GetParticipantsByIssueID(issue.ID)
  93. if err != nil {
  94. return fmt.Errorf("GetParticipantsByIssueID [issue_id: %d]: %v", issue.ID, err)
  95. }
  96. // In case the issue poster is not watching the repository,
  97. // even if we have duplicated in watchers, can be safely filtered out.
  98. if issue.PosterID != doer.ID {
  99. participants = append(participants, issue.Poster)
  100. }
  101. tos := make([]string, 0, len(watchers)) // List of email addresses
  102. names := make([]string, 0, len(watchers))
  103. for i := range watchers {
  104. if watchers[i].UserID == doer.ID {
  105. continue
  106. }
  107. to, err := Handle.Users().GetByID(ctx, watchers[i].UserID)
  108. if err != nil {
  109. return fmt.Errorf("GetUserByID [%d]: %v", watchers[i].UserID, err)
  110. }
  111. if to.IsOrganization() || !to.IsActive {
  112. continue
  113. }
  114. tos = append(tos, to.Email)
  115. names = append(names, to.Name)
  116. }
  117. for i := range participants {
  118. if participants[i].ID == doer.ID {
  119. continue
  120. } else if com.IsSliceContainsStr(names, participants[i].Name) {
  121. continue
  122. }
  123. tos = append(tos, participants[i].Email)
  124. names = append(names, participants[i].Name)
  125. }
  126. if issue.Assignee != nil && issue.Assignee.ID != doer.ID {
  127. if !com.IsSliceContainsStr(names, issue.Assignee.Name) {
  128. tos = append(tos, issue.Assignee.Email)
  129. names = append(names, issue.Assignee.Name)
  130. }
  131. }
  132. email.SendIssueCommentMail(NewMailerIssue(issue), NewMailerRepo(issue.Repo), NewMailerUser(doer), tos)
  133. // Mail mentioned people and exclude watchers.
  134. names = append(names, doer.Name)
  135. toUsernames := make([]string, 0, len(mentions)) // list of user names.
  136. for i := range mentions {
  137. if com.IsSliceContainsStr(names, mentions[i]) {
  138. continue
  139. }
  140. toUsernames = append(toUsernames, mentions[i])
  141. }
  142. tos, err = Handle.Users().GetMailableEmailsByUsernames(ctx, toUsernames)
  143. if err != nil {
  144. return errors.Wrap(err, "get mailable emails by usernames")
  145. }
  146. email.SendIssueMentionMail(NewMailerIssue(issue), NewMailerRepo(issue.Repo), NewMailerUser(doer), tos)
  147. return nil
  148. }
  149. // MailParticipants sends new issue thread created emails to repository watchers
  150. // and mentioned people.
  151. func (issue *Issue) MailParticipants() (err error) {
  152. mentions := markup.FindAllMentions(issue.Content)
  153. if err = updateIssueMentions(x, issue.ID, mentions); err != nil {
  154. return fmt.Errorf("UpdateIssueMentions [%d]: %v", issue.ID, err)
  155. }
  156. if err = mailIssueCommentToParticipants(issue, issue.Poster, mentions); err != nil {
  157. log.Error("mailIssueCommentToParticipants: %v", err)
  158. }
  159. return nil
  160. }