diff options
Diffstat (limited to 'pkg')
-rw-r--r-- | pkg/mailer/mail.go | 12 | ||||
-rw-r--r-- | pkg/mailer/mailer.go | 21 | ||||
-rw-r--r-- | pkg/setting/setting.go | 24 | ||||
-rw-r--r-- | pkg/template/template.go | 17 |
4 files changed, 55 insertions, 19 deletions
diff --git a/pkg/mailer/mail.go b/pkg/mailer/mail.go index a5c9c217..200e2e11 100644 --- a/pkg/mailer/mail.go +++ b/pkg/mailer/mail.go @@ -94,7 +94,7 @@ func SendUserMail(c *macaron.Context, u User, tpl, code, subject, info string) { msg := NewMessage([]string{u.Email()}, subject, body) msg.Info = fmt.Sprintf("UID: %d, %s", u.ID(), info) - SendAsync(msg) + Send(msg) } func SendActivateAccountMail(c *macaron.Context, u User) { @@ -122,7 +122,7 @@ func SendActivateEmailMail(c *macaron.Context, u User, email string) { msg := NewMessage([]string{email}, c.Tr("mail.activate_email"), body) msg.Info = fmt.Sprintf("UID: %d, activate email", u.ID()) - SendAsync(msg) + Send(msg) } // SendRegisterNotifyMail triggers a notify e-mail by admin created a account. @@ -139,7 +139,7 @@ func SendRegisterNotifyMail(c *macaron.Context, u User) { msg := NewMessage([]string{u.Email()}, c.Tr("mail.register_notify"), body) msg.Info = fmt.Sprintf("UID: %d, registration notify", u.ID()) - SendAsync(msg) + Send(msg) } // SendCollaboratorMail sends mail notification to new collaborator. @@ -160,7 +160,7 @@ func SendCollaboratorMail(u, doer User, repo Repository) { msg := NewMessage([]string{u.Email()}, subject, body) msg.Info = fmt.Sprintf("UID: %d, add collaborator", u.ID()) - SendAsync(msg) + Send(msg) } func composeTplData(subject, body, link string) map[string]interface{} { @@ -192,7 +192,7 @@ func SendIssueCommentMail(issue Issue, repo Repository, doer User, tos []string) return } - SendAsync(composeIssueMessage(issue, repo, doer, MAIL_ISSUE_COMMENT, tos, "issue comment")) + Send(composeIssueMessage(issue, repo, doer, MAIL_ISSUE_COMMENT, tos, "issue comment")) } // SendIssueMentionMail composes and sends issue mention emails to target receivers. @@ -200,5 +200,5 @@ func SendIssueMentionMail(issue Issue, repo Repository, doer User, tos []string) if len(tos) == 0 { return } - SendAsync(composeIssueMessage(issue, repo, doer, MAIL_ISSUE_MENTION, tos, "issue mention")) + Send(composeIssueMessage(issue, repo, doer, MAIL_ISSUE_MENTION, tos, "issue mention")) } diff --git a/pkg/mailer/mailer.go b/pkg/mailer/mailer.go index 4c0960d6..25761832 100644 --- a/pkg/mailer/mailer.go +++ b/pkg/mailer/mailer.go @@ -24,6 +24,7 @@ import ( type Message struct { Info string // Message information for log purpose. *gomail.Message + confirmChan chan struct{} } // NewMessageFrom creates new mail message object with custom From header. @@ -48,9 +49,9 @@ func NewMessageFrom(to []string, from, subject, htmlBody string) *Message { } } msg.SetBody(contentType, body) - return &Message{ - Message: msg, + Message: msg, + confirmChan: make(chan struct{}), } } @@ -204,12 +205,14 @@ func processMailQueue() { } else { log.Trace("E-mails sent %s: %s", msg.GetHeader("To"), msg.Info) } + msg.confirmChan <- struct{}{} } } } var mailQueue chan *Message +// NewContext initializes settings for mailer. func NewContext() { // Need to check if mailQueue is nil because in during reinstall (user had installed // before but swithed install lock off), this function will be called again @@ -222,8 +225,18 @@ func NewContext() { go processMailQueue() } -func SendAsync(msg *Message) { +// Send puts new message object into mail queue. +// It returns without confirmation (mail processed asynchronously) in normal cases, +// but waits/blocks under hook mode to make sure mail has been sent. +func Send(msg *Message) { + mailQueue <- msg + + if setting.HookMode { + <-msg.confirmChan + return + } + go func() { - mailQueue <- msg + <-msg.confirmChan }() } diff --git a/pkg/setting/setting.go b/pkg/setting/setting.go index 039c29c6..f206592d 100644 --- a/pkg/setting/setting.go +++ b/pkg/setting/setting.go @@ -832,9 +832,10 @@ var ( MailService *Mailer ) +// newMailService initializes mail service options from configuration. +// No non-error log will be printed in hook mode. func newMailService() { sec := Cfg.Section("mailer") - // Check mailer setting. if !sec.Key("ENABLED").MustBool() { return } @@ -863,6 +864,9 @@ func newMailService() { MailService.FromEmail = parsed.Address } + if HookMode { + return + } log.Info("Mail Service Enabled") } @@ -877,6 +881,8 @@ func newRegisterMailService() { log.Info("Register Mail Service Enabled") } +// newNotifyMailService initializes notification email service options from configuration. +// No non-error log will be printed in hook mode. func newNotifyMailService() { if !Cfg.Section("service").Key("ENABLE_NOTIFY_MAIL").MustBool() { return @@ -885,6 +891,10 @@ func newNotifyMailService() { return } Service.EnableNotifyMail = true + + if HookMode { + return + } log.Info("Notify Mail Service Enabled") } @@ -901,3 +911,15 @@ func NewServices() { newRegisterMailService() newNotifyMailService() } + +// HookMode indicates whether program starts as Git server-side hook callback. +var HookMode bool + +// NewPostReceiveHookServices initializes all services that are needed by +// Git server-side post-receive hook callback. +func NewPostReceiveHookServices() { + HookMode = true + newService() + newMailService() + newNotifyMailService() +} diff --git a/pkg/template/template.go b/pkg/template/template.go index 882be145..f93aca74 100644 --- a/pkg/template/template.go +++ b/pkg/template/template.go @@ -22,11 +22,12 @@ import ( "gopkg.in/editorconfig/editorconfig-core-go.v1" "github.com/gogits/gogs/models" - "github.com/gogits/gogs/pkg/tool" "github.com/gogits/gogs/pkg/markup" "github.com/gogits/gogs/pkg/setting" + "github.com/gogits/gogs/pkg/tool" ) +// TODO: only initialize map once and save to a local variable to reduce copies. func NewFuncMap() []template.FuncMap { return []template.FuncMap{map[string]interface{}{ "GoVer": func() string { @@ -91,13 +92,13 @@ func NewFuncMap() []template.FuncMap { } return str[start:end] }, - "Join": strings.Join, - "EllipsisString": tool.EllipsisString, - "DiffTypeToStr": DiffTypeToStr, - "DiffLineTypeToStr": DiffLineTypeToStr, - "Sha1": Sha1, - "ShortSHA1": tool.ShortSHA1, - "MD5": tool.MD5, + "Join": strings.Join, + "EllipsisString": tool.EllipsisString, + "DiffTypeToStr": DiffTypeToStr, + "DiffLineTypeToStr": DiffLineTypeToStr, + "Sha1": Sha1, + "ShortSHA1": tool.ShortSHA1, + "MD5": tool.MD5, "ActionContent2Commits": ActionContent2Commits, "EscapePound": EscapePound, "RenderCommitMessage": RenderCommitMessage, |