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.

109 lines
3.2 KiB

  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. // 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(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(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. mentions := markdown.FindAllMentions(issue.Content)
  82. if err = UpdateIssueMentions(x, issue.ID, mentions); err != nil {
  83. return fmt.Errorf("UpdateIssueMentions [%d]: %v", issue.ID, err)
  84. }
  85. if err = mailIssueCommentToParticipants(issue, issue.Poster, nil, mentions); err != nil {
  86. log.Error(4, "mailIssueCommentToParticipants: %v", err)
  87. }
  88. return nil
  89. }