1
0

issue_mail.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. package database
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/pkg/errors"
  6. "github.com/unknwon/com"
  7. log "unknwon.dev/clog/v2"
  8. "gogs.io/gogs/internal/conf"
  9. "gogs.io/gogs/internal/email"
  10. "gogs.io/gogs/internal/markup"
  11. "gogs.io/gogs/internal/userutil"
  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 (mu mailerUser) ID() int64 {
  21. return mu.user.ID
  22. }
  23. func (mu mailerUser) DisplayName() string {
  24. return mu.user.DisplayName()
  25. }
  26. func (mu mailerUser) Email() string {
  27. return mu.user.Email
  28. }
  29. func (mu mailerUser) GenerateEmailActivateCode(email string) string {
  30. return userutil.GenerateActivateCode(
  31. mu.user.ID,
  32. email,
  33. mu.user.Name,
  34. mu.user.Password,
  35. mu.user.Rands,
  36. )
  37. }
  38. func NewMailerUser(u *User) email.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 (mr mailerRepo) FullName() string {
  46. return mr.repo.FullName()
  47. }
  48. func (mr mailerRepo) HTMLURL() string {
  49. return mr.repo.HTMLURL()
  50. }
  51. func (mr mailerRepo) ComposeMetas() map[string]string {
  52. return mr.repo.ComposeMetas()
  53. }
  54. func NewMailerRepo(repo *Repository) email.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 (mi mailerIssue) MailSubject() string {
  62. return mi.issue.MailSubject()
  63. }
  64. func (mi mailerIssue) Content() string {
  65. return mi.issue.Content
  66. }
  67. func (mi mailerIssue) HTMLURL() string {
  68. return mi.issue.HTMLURL()
  69. }
  70. func NewMailerIssue(issue *Issue) email.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, users who participated in comments and the assignee.
  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. ctx := context.TODO()
  79. if !conf.User.EnableEmailNotification {
  80. return nil
  81. }
  82. watchers, err := GetWatchers(issue.RepoID)
  83. if err != nil {
  84. return fmt.Errorf("GetWatchers [repo_id: %d]: %v", issue.RepoID, err)
  85. }
  86. participants, err := GetParticipantsByIssueID(issue.ID)
  87. if err != nil {
  88. return fmt.Errorf("GetParticipantsByIssueID [issue_id: %d]: %v", issue.ID, err)
  89. }
  90. // In case the issue poster is not watching the repository,
  91. // even if we have duplicated in watchers, can be safely filtered out.
  92. if issue.PosterID != doer.ID {
  93. participants = append(participants, issue.Poster)
  94. }
  95. tos := make([]string, 0, len(watchers)) // List of email addresses
  96. names := make([]string, 0, len(watchers))
  97. for i := range watchers {
  98. if watchers[i].UserID == doer.ID {
  99. continue
  100. }
  101. to, err := Handle.Users().GetByID(ctx, watchers[i].UserID)
  102. if err != nil {
  103. return fmt.Errorf("GetUserByID [%d]: %v", watchers[i].UserID, err)
  104. }
  105. if to.IsOrganization() || !to.IsActive {
  106. continue
  107. }
  108. tos = append(tos, to.Email)
  109. names = append(names, to.Name)
  110. }
  111. for i := range participants {
  112. if participants[i].ID == doer.ID {
  113. continue
  114. } else if com.IsSliceContainsStr(names, participants[i].Name) {
  115. continue
  116. }
  117. tos = append(tos, participants[i].Email)
  118. names = append(names, participants[i].Name)
  119. }
  120. if issue.Assignee != nil && issue.Assignee.ID != doer.ID {
  121. if !com.IsSliceContainsStr(names, issue.Assignee.Name) {
  122. tos = append(tos, issue.Assignee.Email)
  123. names = append(names, issue.Assignee.Name)
  124. }
  125. }
  126. email.SendIssueCommentMail(NewMailerIssue(issue), NewMailerRepo(issue.Repo), NewMailerUser(doer), tos)
  127. // Mail mentioned people and exclude watchers.
  128. names = append(names, doer.Name)
  129. toUsernames := make([]string, 0, len(mentions)) // list of user names.
  130. for i := range mentions {
  131. if com.IsSliceContainsStr(names, mentions[i]) {
  132. continue
  133. }
  134. toUsernames = append(toUsernames, mentions[i])
  135. }
  136. tos, err = Handle.Users().GetMailableEmailsByUsernames(ctx, toUsernames)
  137. if err != nil {
  138. return errors.Wrap(err, "get mailable emails by usernames")
  139. }
  140. email.SendIssueMentionMail(NewMailerIssue(issue), NewMailerRepo(issue.Repo), NewMailerUser(doer), tos)
  141. return nil
  142. }
  143. // MailParticipants sends new issue thread created emails to repository watchers
  144. // and mentioned people.
  145. func (issue *Issue) MailParticipants() (err error) {
  146. mentions := markup.FindAllMentions(issue.Content)
  147. if err = updateIssueMentions(x, issue.ID, mentions); err != nil {
  148. return fmt.Errorf("UpdateIssueMentions [%d]: %v", issue.ID, err)
  149. }
  150. if err = mailIssueCommentToParticipants(issue, issue.Poster, mentions); err != nil {
  151. log.Error("mailIssueCommentToParticipants: %v", err)
  152. }
  153. return nil
  154. }