You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

issue_mail.go 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. "code.gitea.io/gitea/modules/log"
  9. "code.gitea.io/gitea/modules/markup"
  10. "code.gitea.io/gitea/modules/setting"
  11. )
  12. func (issue *Issue) mailSubject() string {
  13. return fmt.Sprintf("[%s] %s (#%d)", issue.Repo.Name, issue.Title, issue.Index)
  14. }
  15. // mailIssueCommentToParticipants can be used for both new issue creation and comment.
  16. // This function sends two list of emails:
  17. // 1. Repository watchers and users who are participated in comments.
  18. // 2. Users who are not in 1. but get mentioned in current issue/comment.
  19. func mailIssueCommentToParticipants(e Engine, issue *Issue, doer *User, content string, comment *Comment, mentions []string) error {
  20. if !setting.Service.EnableNotifyMail {
  21. return nil
  22. }
  23. watchers, err := getWatchers(e, issue.RepoID)
  24. if err != nil {
  25. return fmt.Errorf("getWatchers [repo_id: %d]: %v", issue.RepoID, err)
  26. }
  27. participants, err := getParticipantsByIssueID(e, issue.ID)
  28. if err != nil {
  29. return fmt.Errorf("getParticipantsByIssueID [issue_id: %d]: %v", issue.ID, err)
  30. }
  31. // In case the issue poster is not watching the repository and is active,
  32. // even if we have duplicated in watchers, can be safely filtered out.
  33. poster, err := GetUserByID(issue.PosterID)
  34. if err != nil {
  35. return fmt.Errorf("GetUserByID [%d]: %v", issue.PosterID, err)
  36. }
  37. if issue.PosterID != doer.ID && poster.IsActive && !poster.ProhibitLogin {
  38. participants = append(participants, issue.Poster)
  39. }
  40. // Assignee must receive any communications
  41. if issue.Assignee != nil && issue.AssigneeID > 0 && issue.AssigneeID != doer.ID {
  42. participants = append(participants, issue.Assignee)
  43. }
  44. tos := make([]string, 0, len(watchers)) // List of email addresses.
  45. names := make([]string, 0, len(watchers))
  46. for i := range watchers {
  47. if watchers[i].UserID == doer.ID {
  48. continue
  49. }
  50. to, err := getUserByID(e, watchers[i].UserID)
  51. if err != nil {
  52. return fmt.Errorf("GetUserByID [%d]: %v", watchers[i].UserID, err)
  53. }
  54. if to.IsOrganization() {
  55. continue
  56. }
  57. tos = append(tos, to.Email)
  58. names = append(names, to.Name)
  59. }
  60. for i := range participants {
  61. if participants[i].ID == doer.ID {
  62. continue
  63. } else if com.IsSliceContainsStr(names, participants[i].Name) {
  64. continue
  65. }
  66. tos = append(tos, participants[i].Email)
  67. names = append(names, participants[i].Name)
  68. }
  69. SendIssueCommentMail(issue, doer, content, comment, tos)
  70. // Mail mentioned people and exclude watchers.
  71. names = append(names, doer.Name)
  72. tos = make([]string, 0, len(mentions)) // list of user names.
  73. for i := range mentions {
  74. if com.IsSliceContainsStr(names, mentions[i]) {
  75. continue
  76. }
  77. tos = append(tos, mentions[i])
  78. }
  79. SendIssueMentionMail(issue, doer, content, comment, getUserEmailsByNames(e, tos))
  80. return nil
  81. }
  82. // MailParticipants sends new issue thread created emails to repository watchers
  83. // and mentioned people.
  84. func (issue *Issue) MailParticipants() (err error) {
  85. return issue.mailParticipants(x)
  86. }
  87. func (issue *Issue) mailParticipants(e Engine) (err error) {
  88. mentions := markup.FindAllMentions(issue.Content)
  89. if err = UpdateIssueMentions(e, issue.ID, mentions); err != nil {
  90. return fmt.Errorf("UpdateIssueMentions [%d]: %v", issue.ID, err)
  91. }
  92. if err = mailIssueCommentToParticipants(e, issue, issue.Poster, issue.Content, nil, mentions); err != nil {
  93. log.Error(4, "mailIssueCommentToParticipants: %v", err)
  94. }
  95. return nil
  96. }