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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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(issue *Issue, doer *User, comment *Comment, mentions []string) error {
  20. if !setting.Service.EnableNotifyMail {
  21. return nil
  22. }
  23. watchers, err := GetWatchers(issue.RepoID)
  24. if err != nil {
  25. return fmt.Errorf("GetWatchers [repo_id: %d]: %v", issue.RepoID, err)
  26. }
  27. participants, err := GetParticipantsByIssueID(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. tos := make([]string, 0, len(watchers)) // List of email addresses.
  37. names := make([]string, 0, len(watchers))
  38. for i := range watchers {
  39. if watchers[i].UserID == doer.ID {
  40. continue
  41. }
  42. to, err := GetUserByID(watchers[i].UserID)
  43. if err != nil {
  44. return fmt.Errorf("GetUserByID [%d]: %v", watchers[i].UserID, err)
  45. }
  46. if to.IsOrganization() {
  47. continue
  48. }
  49. tos = append(tos, to.Email)
  50. names = append(names, to.Name)
  51. }
  52. for i := range participants {
  53. if participants[i].ID == doer.ID {
  54. continue
  55. } else if com.IsSliceContainsStr(names, participants[i].Name) {
  56. continue
  57. }
  58. tos = append(tos, participants[i].Email)
  59. names = append(names, participants[i].Name)
  60. }
  61. SendIssueCommentMail(issue, doer, comment, tos)
  62. // Mail mentioned people and exclude watchers.
  63. names = append(names, doer.Name)
  64. tos = make([]string, 0, len(mentions)) // list of user names.
  65. for i := range mentions {
  66. if com.IsSliceContainsStr(names, mentions[i]) {
  67. continue
  68. }
  69. tos = append(tos, mentions[i])
  70. }
  71. SendIssueMentionMail(issue, doer, comment, GetUserEmailsByNames(tos))
  72. return nil
  73. }
  74. // MailParticipants sends new issue thread created emails to repository watchers
  75. // and mentioned people.
  76. func (issue *Issue) MailParticipants() (err error) {
  77. mentions := markdown.FindAllMentions(issue.Content)
  78. if err = UpdateIssueMentions(x, issue.ID, mentions); err != nil {
  79. return fmt.Errorf("UpdateIssueMentions [%d]: %v", issue.ID, err)
  80. }
  81. if err = mailIssueCommentToParticipants(issue, issue.Poster, nil, mentions); err != nil {
  82. log.Error(4, "mailIssueCommentToParticipants: %v", err)
  83. }
  84. return nil
  85. }