issue_mail.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 models
  5. import (
  6. "fmt"
  7. "github.com/Unknwon/com"
  8. log "gopkg.in/clog.v1"
  9. "github.com/gogits/gogs/modules/mailer"
  10. "github.com/gogits/gogs/modules/markdown"
  11. "github.com/gogits/gogs/modules/setting"
  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. func mailIssueCommentToParticipants(issue *Issue, doer *User, mentions []string) error {
  72. if !setting.Service.EnableNotifyMail {
  73. return nil
  74. }
  75. // Mail wahtcers.
  76. watchers, err := GetWatchers(issue.RepoID)
  77. if err != nil {
  78. return fmt.Errorf("GetWatchers [%d]: %v", issue.RepoID, err)
  79. }
  80. tos := make([]string, 0, len(watchers)) // List of email addresses.
  81. names := make([]string, 0, len(watchers))
  82. for i := range watchers {
  83. if watchers[i].UserID == doer.ID {
  84. continue
  85. }
  86. to, err := GetUserByID(watchers[i].UserID)
  87. if err != nil {
  88. return fmt.Errorf("GetUserByID [%d]: %v", watchers[i].UserID, err)
  89. }
  90. if to.IsOrganization() {
  91. continue
  92. }
  93. tos = append(tos, to.Email)
  94. names = append(names, to.Name)
  95. }
  96. mailer.SendIssueCommentMail(NewMailerIssue(issue), NewMailerRepo(issue.Repo), NewMailerUser(doer), tos)
  97. // Mail mentioned people and exclude watchers.
  98. names = append(names, doer.Name)
  99. tos = make([]string, 0, len(mentions)) // list of user names.
  100. for i := range mentions {
  101. if com.IsSliceContainsStr(names, mentions[i]) {
  102. continue
  103. }
  104. tos = append(tos, mentions[i])
  105. }
  106. mailer.SendIssueMentionMail(NewMailerIssue(issue), NewMailerRepo(issue.Repo), NewMailerUser(doer), GetUserEmailsByNames(tos))
  107. return nil
  108. }
  109. // MailParticipants sends new issue thread created emails to repository watchers
  110. // and mentioned people.
  111. func (issue *Issue) MailParticipants() (err error) {
  112. mentions := markdown.FindAllMentions(issue.Content)
  113. if err = updateIssueMentions(x, issue.ID, mentions); err != nil {
  114. return fmt.Errorf("UpdateIssueMentions [%d]: %v", issue.ID, err)
  115. }
  116. if err = mailIssueCommentToParticipants(issue, issue.Poster, mentions); err != nil {
  117. log.Error(4, "mailIssueCommentToParticipants: %v", err)
  118. }
  119. return nil
  120. }