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.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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/markdown"
  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, 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,
  32. // even if we have duplicated in watchers, can be safely filtered out.
  33. if issue.PosterID != doer.ID {
  34. participants = append(participants, issue.Poster)
  35. }
  36. // Assignee must receive any communications
  37. if issue.Assignee != nil && issue.AssigneeID > 0 && issue.AssigneeID != doer.ID {
  38. participants = append(participants, issue.Assignee)
  39. }
  40. tos := make([]string, 0, len(watchers)) // List of email addresses.
  41. names := make([]string, 0, len(watchers))
  42. for i := range watchers {
  43. if watchers[i].UserID == doer.ID {
  44. continue
  45. }
  46. to, err := getUserByID(e, watchers[i].UserID)
  47. if err != nil {
  48. return fmt.Errorf("GetUserByID [%d]: %v", watchers[i].UserID, err)
  49. }
  50. if to.IsOrganization() {
  51. continue
  52. }
  53. tos = append(tos, to.Email)
  54. names = append(names, to.Name)
  55. }
  56. for i := range participants {
  57. if participants[i].ID == doer.ID {
  58. continue
  59. } else if com.IsSliceContainsStr(names, participants[i].Name) {
  60. continue
  61. }
  62. tos = append(tos, participants[i].Email)
  63. names = append(names, participants[i].Name)
  64. }
  65. SendIssueCommentMail(issue, doer, comment, tos)
  66. // Mail mentioned people and exclude watchers.
  67. names = append(names, doer.Name)
  68. tos = make([]string, 0, len(mentions)) // list of user names.
  69. for i := range mentions {
  70. if com.IsSliceContainsStr(names, mentions[i]) {
  71. continue
  72. }
  73. tos = append(tos, mentions[i])
  74. }
  75. SendIssueMentionMail(issue, doer, comment, getUserEmailsByNames(e, tos))
  76. return nil
  77. }
  78. // MailParticipants sends new issue thread created emails to repository watchers
  79. // and mentioned people.
  80. func (issue *Issue) MailParticipants() (err error) {
  81. return issue.mailParticipants(x)
  82. }
  83. func (issue *Issue) mailParticipants(e Engine) (err error) {
  84. mentions := markdown.FindAllMentions(issue.Content)
  85. if err = UpdateIssueMentions(e, issue.ID, mentions); err != nil {
  86. return fmt.Errorf("UpdateIssueMentions [%d]: %v", issue.ID, err)
  87. }
  88. if err = mailIssueCommentToParticipants(e, issue, issue.Poster, nil, mentions); err != nil {
  89. log.Error(4, "mailIssueCommentToParticipants: %v", err)
  90. }
  91. return nil
  92. }