issue_mail.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. "github.com/gogits/gogs/modules/log"
  9. "github.com/gogits/gogs/modules/markdown"
  10. "github.com/gogits/gogs/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. func mailIssueCommentToParticipants(issue *Issue, doer *User, mentions []string) error {
  17. if !setting.Service.EnableNotifyMail {
  18. return nil
  19. }
  20. // Mail wahtcers.
  21. watchers, err := GetWatchers(issue.RepoID)
  22. if err != nil {
  23. return fmt.Errorf("GetWatchers [%d]: %v", issue.RepoID, err)
  24. }
  25. tos := make([]string, 0, len(watchers)) // List of email addresses.
  26. names := make([]string, 0, len(watchers))
  27. for i := range watchers {
  28. if watchers[i].UserID == doer.ID {
  29. continue
  30. }
  31. to, err := GetUserByID(watchers[i].UserID)
  32. if err != nil {
  33. return fmt.Errorf("GetUserByID [%d]: %v", watchers[i].UserID, err)
  34. }
  35. if to.IsOrganization() {
  36. continue
  37. }
  38. tos = append(tos, to.Email)
  39. names = append(names, to.Name)
  40. }
  41. SendIssueCommentMail(issue, doer, tos)
  42. // Mail mentioned people and exclude watchers.
  43. names = append(names, doer.Name)
  44. tos = make([]string, 0, len(mentions)) // list of user names.
  45. for i := range mentions {
  46. if com.IsSliceContainsStr(names, mentions[i]) {
  47. continue
  48. }
  49. tos = append(tos, mentions[i])
  50. }
  51. SendIssueMentionMail(issue, doer, GetUserEmailsByNames(tos))
  52. return nil
  53. }
  54. // MailParticipants sends new issue thread created emails to repository watchers
  55. // and mentioned people.
  56. func (issue *Issue) MailParticipants() (err error) {
  57. mentions := markdown.FindAllMentions(issue.Content)
  58. if err = UpdateIssueMentions(issue.ID, mentions); err != nil {
  59. return fmt.Errorf("UpdateIssueMentions [%d]: %v", issue.ID, err)
  60. }
  61. if err = mailIssueCommentToParticipants(issue, issue.Poster, mentions); err != nil {
  62. log.Error(4, "mailIssueCommentToParticipants: %v", err)
  63. }
  64. return nil
  65. }