issue_mail.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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.Name, 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. }