issue_mail.go 4.8 KB

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