summaryrefslogtreecommitdiffstats
path: root/diff.txt
blob: 7b2522f89ef02cbe5ae70621a6342e8fa006f685 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
commit c1a3d4fefbbbf332cd1cedda66e93bf40cc9713d
Author: Unknown <joe2010xtmf@163.com>
Date:   Tue Mar 25 21:37:18 2014 -0400

    Add mail notify for creating issue

diff --git a/gogs.go b/gogs.go
index b62580f..f5a328a 100644
--- 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
diff --git a/models/issue.go b/models/issue.go
index 2bdd083..2de6568 100644
--- a/models/issue.go
+++ b/models/issue.go
@@ -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
 }
 
diff --git a/modules/mailer/mail.go b/modules/mailer/mail.go
index 92acd20..d0decbe 100644
--- a/modules/mailer/mail.go
+++ b/modules/mailer/mail.go
@@ -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
+}
diff --git a/modules/mailer/mailer.go b/modules/mailer/mailer.go
index da63e01..63861d8 100644
--- a/modules/mailer/mailer.go
+++ b/modules/mailer/mailer.go
@@ -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
 }
diff --git a/routers/repo/issue.go b/routers/repo/issue.go
index fc5bb98..242593f 100644
--- a/routers/repo/issue.go
+++ b/routers/repo/issue.go
@@ -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))
 }