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.

137 lines
4.0 KiB

  1. commit c1a3d4fefbbbf332cd1cedda66e93bf40cc9713d
  2. Author: Unknown <joe2010xtmf@163.com>
  3. Date: Tue Mar 25 21:37:18 2014 -0400
  4. Add mail notify for creating issue
  5. diff --git a/gogs.go b/gogs.go
  6. index b62580f..f5a328a 100644
  7. --- a/gogs.go
  8. +++ b/gogs.go
  9. @@ -19,7 +19,7 @@ import (
  10. // Test that go1.2 tag above is included in builds. main.go refers to this definition.
  11. const go12tag = true
  12. -const APP_VER = "0.1.7.0325"
  13. +const APP_VER = "0.1.8.0325"
  14. func init() {
  15. base.AppVer = APP_VER
  16. diff --git a/models/issue.go b/models/issue.go
  17. index 2bdd083..2de6568 100644
  18. --- a/models/issue.go
  19. +++ b/models/issue.go
  20. @@ -59,7 +59,6 @@ func CreateIssue(userId, repoId, milestoneId, assigneeId int64, name, labels, co
  21. Content: content,
  22. }
  23. _, err = orm.Insert(issue)
  24. - // TODO: newIssueAction
  25. return issue, err
  26. }
  27. diff --git a/modules/mailer/mail.go b/modules/mailer/mail.go
  28. index 92acd20..d0decbe 100644
  29. --- a/modules/mailer/mail.go
  30. +++ b/modules/mailer/mail.go
  31. @@ -6,6 +6,7 @@ package mailer
  32. import (
  33. "encoding/hex"
  34. + "errors"
  35. "fmt"
  36. "github.com/gogits/gogs/models"
  37. @@ -15,12 +16,17 @@ import (
  38. )
  39. // Create New mail message use MailFrom and MailUser
  40. -func NewMailMessage(To []string, subject, body string) Message {
  41. - msg := NewHtmlMessage(To, base.MailService.User, subject, body)
  42. +func NewMailMessageFrom(To []string, from, subject, body string) Message {
  43. + msg := NewHtmlMessage(To, from, subject, body)
  44. msg.User = base.MailService.User
  45. return msg
  46. }
  47. +// Create New mail message use MailFrom and MailUser
  48. +func NewMailMessage(To []string, subject, body string) Message {
  49. + return NewMailMessageFrom(To, base.MailService.User, subject, body)
  50. +}
  51. +
  52. func GetMailTmplData(user *models.User) map[interface{}]interface{} {
  53. data := make(map[interface{}]interface{}, 10)
  54. data["AppName"] = base.AppName
  55. @@ -84,3 +90,33 @@ func SendActiveMail(r *middleware.Render, user *models.User) {
  56. SendAsync(&msg)
  57. }
  58. +
  59. +// SendNotifyMail sends mail notification of all watchers.
  60. +func SendNotifyMail(userId, repoId int64, userName, repoName, subject, content string) error {
  61. + watches, err := models.GetWatches(repoId)
  62. + if err != nil {
  63. + return errors.New("mail.NotifyWatchers(get watches): " + err.Error())
  64. + }
  65. +
  66. + tos := make([]string, 0, len(watches))
  67. + for i := range watches {
  68. + uid := watches[i].UserId
  69. + if userId == uid {
  70. + continue
  71. + }
  72. + u, err := models.GetUserById(uid)
  73. + if err != nil {
  74. + return errors.New("mail.NotifyWatchers(get user): " + err.Error())
  75. + }
  76. + tos = append(tos, u.Email)
  77. + }
  78. +
  79. + if len(tos) == 0 {
  80. + return nil
  81. + }
  82. +
  83. + msg := NewMailMessageFrom(tos, userName, subject, content)
  84. + msg.Info = fmt.Sprintf("Subject: %s, send notify emails", subject)
  85. + SendAsync(&msg)
  86. + return nil
  87. +}
  88. diff --git a/modules/mailer/mailer.go b/modules/mailer/mailer.go
  89. index da63e01..63861d8 100644
  90. --- a/modules/mailer/mailer.go
  91. +++ b/modules/mailer/mailer.go
  92. @@ -33,7 +33,7 @@ func (m Message) Content() string {
  93. }
  94. // create mail content
  95. - content := "From: " + m.User + "<" + m.From +
  96. + content := "From: " + m.From + "<" + m.User +
  97. ">\r\nSubject: " + m.Subject + "\r\nContent-Type: " + contentType + "\r\n\r\n" + m.Body
  98. return content
  99. }
  100. diff --git a/routers/repo/issue.go b/routers/repo/issue.go
  101. index fc5bb98..242593f 100644
  102. --- a/routers/repo/issue.go
  103. +++ b/routers/repo/issue.go
  104. @@ -13,6 +13,7 @@ import (
  105. "github.com/gogits/gogs/modules/auth"
  106. "github.com/gogits/gogs/modules/base"
  107. "github.com/gogits/gogs/modules/log"
  108. + "github.com/gogits/gogs/modules/mailer"
  109. "github.com/gogits/gogs/modules/middleware"
  110. )
  111. @@ -86,6 +87,14 @@ func CreateIssue(ctx *middleware.Context, params martini.Params, form auth.Creat
  112. return
  113. }
  114. + // Mail watchers.
  115. + if base.Service.NotifyMail {
  116. + if err = mailer.SendNotifyMail(ctx.User.Id, ctx.Repo.Repository.Id, ctx.User.Name, ctx.Repo.Repository.Name, issue.Name, issue.Content); err != nil {
  117. + ctx.Handle(200, "issue.CreateIssue", err)
  118. + return
  119. + }
  120. + }
  121. +
  122. log.Trace("%d Issue created: %d", ctx.Repo.Repository.Id, issue.Id)
  123. ctx.Redirect(fmt.Sprintf("/%s/%s/issues/%d", params["username"], params["reponame"], issue.Index))
  124. }