issue_mail.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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/conf"
  10. "gogs.io/gogs/internal/email"
  11. "gogs.io/gogs/internal/markup"
  12. "gogs.io/gogs/internal/userutil"
  13. )
  14. func (issue *Issue) MailSubject() string {
  15. return fmt.Sprintf("[%s] %s (#%d)", issue.Repo.Name, issue.Title, issue.Index)
  16. }
  17. // mailerUser is a wrapper for satisfying mailer.User interface.
  18. type mailerUser struct {
  19. user *User
  20. }
  21. func (this mailerUser) ID() int64 {
  22. return this.user.ID
  23. }
  24. func (this mailerUser) DisplayName() string {
  25. return this.user.DisplayName()
  26. }
  27. func (this mailerUser) Email() string {
  28. return this.user.Email
  29. }
  30. func (this mailerUser) GenerateEmailActivateCode(email string) string {
  31. return userutil.GenerateActivateCode(
  32. this.user.ID,
  33. email,
  34. this.user.Name,
  35. this.user.Passwd,
  36. this.user.Rands,
  37. )
  38. }
  39. func NewMailerUser(u *User) email.User {
  40. return mailerUser{u}
  41. }
  42. // mailerRepo is a wrapper for satisfying mailer.Repository interface.
  43. type mailerRepo struct {
  44. repo *Repository
  45. }
  46. func (this mailerRepo) FullName() string {
  47. return this.repo.FullName()
  48. }
  49. func (this mailerRepo) HTMLURL() string {
  50. return this.repo.HTMLURL()
  51. }
  52. func (this mailerRepo) ComposeMetas() map[string]string {
  53. return this.repo.ComposeMetas()
  54. }
  55. func NewMailerRepo(repo *Repository) email.Repository {
  56. return mailerRepo{repo}
  57. }
  58. // mailerIssue is a wrapper for satisfying mailer.Issue interface.
  59. type mailerIssue struct {
  60. issue *Issue
  61. }
  62. func (this mailerIssue) MailSubject() string {
  63. return this.issue.MailSubject()
  64. }
  65. func (this mailerIssue) Content() string {
  66. return this.issue.Content
  67. }
  68. func (this mailerIssue) HTMLURL() string {
  69. return this.issue.HTMLURL()
  70. }
  71. func NewMailerIssue(issue *Issue) email.Issue {
  72. return mailerIssue{issue}
  73. }
  74. // mailIssueCommentToParticipants can be used for both new issue creation and comment.
  75. // This functions sends two list of emails:
  76. // 1. Repository watchers, users who participated in comments and the assignee.
  77. // 2. Users who are not in 1. but get mentioned in current issue/comment.
  78. func mailIssueCommentToParticipants(issue *Issue, doer *User, mentions []string) error {
  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 := GetUserByID(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. tos = 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. tos = append(tos, mentions[i])
  135. }
  136. email.SendIssueMentionMail(NewMailerIssue(issue), NewMailerRepo(issue.Repo), NewMailerUser(doer), GetUserEmailsByNames(tos))
  137. return nil
  138. }
  139. // MailParticipants sends new issue thread created emails to repository watchers
  140. // and mentioned people.
  141. func (issue *Issue) MailParticipants() (err error) {
  142. mentions := markup.FindAllMentions(issue.Content)
  143. if err = updateIssueMentions(x, issue.ID, mentions); err != nil {
  144. return fmt.Errorf("UpdateIssueMentions [%d]: %v", issue.ID, err)
  145. }
  146. if err = mailIssueCommentToParticipants(issue, issue.Poster, mentions); err != nil {
  147. log.Error("mailIssueCommentToParticipants: %v", err)
  148. }
  149. return nil
  150. }