]> source.dussan.org Git - gitea.git/commitdiff
Add mail notify for creating issue
authorUnknown <joe2010xtmf@163.com>
Wed, 26 Mar 2014 01:37:18 +0000 (21:37 -0400)
committerUnknown <joe2010xtmf@163.com>
Wed, 26 Mar 2014 01:37:18 +0000 (21:37 -0400)
gogs.go
models/issue.go
modules/mailer/mail.go
modules/mailer/mailer.go
routers/repo/issue.go

diff --git a/gogs.go b/gogs.go
index b62580f8fd92c1f46ec34afc95695ab39223030f..f5a328add96a728324f34dc920c76a7d291639ea 100644 (file)
--- a/gogs.go
+++ b/gogs.go
@@ -19,7 +19,7 @@ import (
 // Test that go1.2 tag above is included in builds. main.go refers to this definition.
 const go12tag = true
 
-const APP_VER = "0.1.7.0325"
+const APP_VER = "0.1.8.0325"
 
 func init() {
        base.AppVer = APP_VER
index 2bdd083d9029aff87006434d47ed134f7a7e0399..2de6568589b9bbf27b2c51b70e21567dff584127 100644 (file)
@@ -59,7 +59,6 @@ func CreateIssue(userId, repoId, milestoneId, assigneeId int64, name, labels, co
                Content:     content,
        }
        _, err = orm.Insert(issue)
-       // TODO: newIssueAction
        return issue, err
 }
 
index 92acd20efbd0d336bb509e26f7bd63caf8b28ce2..d0decbe0686982e1f99a2d360e0e5c992989044e 100644 (file)
@@ -6,6 +6,7 @@ package mailer
 
 import (
        "encoding/hex"
+       "errors"
        "fmt"
 
        "github.com/gogits/gogs/models"
@@ -15,12 +16,17 @@ import (
 )
 
 // Create New mail message use MailFrom and MailUser
-func NewMailMessage(To []string, subject, body string) Message {
-       msg := NewHtmlMessage(To, base.MailService.User, subject, body)
+func NewMailMessageFrom(To []string, from, subject, body string) Message {
+       msg := NewHtmlMessage(To, from, subject, body)
        msg.User = base.MailService.User
        return msg
 }
 
+// Create New mail message use MailFrom and MailUser
+func NewMailMessage(To []string, subject, body string) Message {
+       return NewMailMessageFrom(To, base.MailService.User, subject, body)
+}
+
 func GetMailTmplData(user *models.User) map[interface{}]interface{} {
        data := make(map[interface{}]interface{}, 10)
        data["AppName"] = base.AppName
@@ -84,3 +90,33 @@ func SendActiveMail(r *middleware.Render, user *models.User) {
 
        SendAsync(&msg)
 }
+
+// SendNotifyMail sends mail notification of all watchers.
+func SendNotifyMail(userId, repoId int64, userName, repoName, subject, content string) error {
+       watches, err := models.GetWatches(repoId)
+       if err != nil {
+               return errors.New("mail.NotifyWatchers(get watches): " + err.Error())
+       }
+
+       tos := make([]string, 0, len(watches))
+       for i := range watches {
+               uid := watches[i].UserId
+               if userId == uid {
+                       continue
+               }
+               u, err := models.GetUserById(uid)
+               if err != nil {
+                       return errors.New("mail.NotifyWatchers(get user): " + err.Error())
+               }
+               tos = append(tos, u.Email)
+       }
+
+       if len(tos) == 0 {
+               return nil
+       }
+
+       msg := NewMailMessageFrom(tos, userName, subject, content)
+       msg.Info = fmt.Sprintf("Subject: %s, send notify emails", subject)
+       SendAsync(&msg)
+       return nil
+}
index da63e01d2a98d2ba8f66ce33307bfb775766fc71..63861d870ec901997c23815c460b7932e931ef87 100644 (file)
@@ -33,7 +33,7 @@ func (m Message) Content() string {
        }
 
        // create mail content
-       content := "From: " + m.User + "<" + m.From +
+       content := "From: " + m.From + "<" + m.User +
                ">\r\nSubject: " + m.Subject + "\r\nContent-Type: " + contentType + "\r\n\r\n" + m.Body
        return content
 }
index fc5bb986438926c34f417e998be8b5cd39cee51f..242593ff299d274b42e1d0a821622c4ead75bfb4 100644 (file)
@@ -13,6 +13,7 @@ import (
        "github.com/gogits/gogs/modules/auth"
        "github.com/gogits/gogs/modules/base"
        "github.com/gogits/gogs/modules/log"
+       "github.com/gogits/gogs/modules/mailer"
        "github.com/gogits/gogs/modules/middleware"
 )
 
@@ -86,6 +87,14 @@ func CreateIssue(ctx *middleware.Context, params martini.Params, form auth.Creat
                return
        }
 
+       // Mail watchers.
+       if base.Service.NotifyMail {
+               if err = mailer.SendNotifyMail(ctx.User.Id, ctx.Repo.Repository.Id, ctx.User.Name, ctx.Repo.Repository.Name, issue.Name, issue.Content); err != nil {
+                       ctx.Handle(200, "issue.CreateIssue", err)
+                       return
+               }
+       }
+
        log.Trace("%d Issue created: %d", ctx.Repo.Repository.Id, issue.Id)
        ctx.Redirect(fmt.Sprintf("/%s/%s/issues/%d", params["username"], params["reponame"], issue.Index))
 }