From 8203fd16ce3356d69b0cc51287d0a1fc25318b2d Mon Sep 17 00:00:00 2001 From: Harrison Healey Date: Tue, 16 Aug 2016 14:41:47 -0400 Subject: PLT-3647 Email Batching (#3718) * PLT-3647 Added config settings for email batching * PLT-3647 Refactored generation of email notification * PLT-3647 Added serverside code for email batching * PLT-3647 Updated settings UI to enable email batching * PLT-3647 Removed debug code * PLT-3647 Fixed 0-padding of minutes in batched notification * PLT-3647 Updated clientside UI for when email batching is disabled * Go fmt * PLT-3647 Changed email batching to be disabled by default * Updated batched email message * Added email batching toggle to system console * Changed Email Notifications > Immediate setting to a 30 second batch interval * Go fmt * Fixed link to Mattermost icon in batched email notification * Updated users to use 30 second email batching by default * Fully disabled email batching when clustering is enabled * Fixed email batching setting in the system console * Fixed casing of 'Send Email notifications' -> 'Send email notifications' * Updating UI Improvements for email batching (#3736) * Updated text for notification settings and SiteURL. * Prevented enabling email batching when SiteURL isn't set in the system console * Re-added a couple debug messages * Added warning text when clustering is enabled --- api/post.go | 62 +++++++++++++++++++++++++++++++++---------------------------- 1 file changed, 34 insertions(+), 28 deletions(-) (limited to 'api/post.go') diff --git a/api/post.go b/api/post.go index a873e16a8..93b88a432 100644 --- a/api/post.go +++ b/api/post.go @@ -23,6 +23,7 @@ import ( "github.com/mattermost/platform/model" "github.com/mattermost/platform/store" "github.com/mattermost/platform/utils" + "github.com/nicksnyder/go-i18n/i18n" ) const ( @@ -770,6 +771,14 @@ func sendNotificationEmail(c *Context, post *model.Post, user *model.User, chann return } + if *utils.Cfg.EmailSettings.EnableEmailBatching { + if err := AddNotificationEmailToBatch(user, post, team); err == nil { + return + } + + // fall back to sending a single email if we can't batch it for some reason + } + var channelName string var bodyText string var subjectText string @@ -802,7 +811,7 @@ func sendNotificationEmail(c *Context, post *model.Post, user *model.User, chann bodyPage := utils.NewHTMLTemplate("post_body", user.Locale) bodyPage.Props["SiteURL"] = c.GetSiteURL() - bodyPage.Props["PostMessage"] = model.ClearMentionTags(post.Message) + bodyPage.Props["PostMessage"] = getMessageForNotification(post, userLocale) bodyPage.Props["TeamLink"] = teamURL + "/pl/" + post.Id bodyPage.Props["BodyText"] = bodyText bodyPage.Props["Button"] = userLocale("api.templates.post_body.button") @@ -811,39 +820,36 @@ func sendNotificationEmail(c *Context, post *model.Post, user *model.User, chann "Hour": fmt.Sprintf("%02d", tm.Hour()), "Minute": fmt.Sprintf("%02d", tm.Minute()), "TimeZone": zone, "Month": month, "Day": day})) - // attempt to fill in a message body if the post doesn't have any text - if len(strings.TrimSpace(bodyPage.Props["PostMessage"])) == 0 && len(post.Filenames) > 0 { - // extract the filenames from their paths and determine what type of files are attached - filenames := make([]string, len(post.Filenames)) - onlyImages := true - for i, filename := range post.Filenames { - var err error - if filenames[i], err = url.QueryUnescape(filepath.Base(filename)); err != nil { - // this should never error since filepath was escaped using url.QueryEscape - filenames[i] = filepath.Base(filename) - } + if err := utils.SendMail(user.Email, subjectPage.Render(), bodyPage.Render()); err != nil { + l4g.Error(utils.T("api.post.send_notifications_and_forget.send.error"), user.Email, err) + } +} - ext := filepath.Ext(filename) - onlyImages = onlyImages && model.IsFileExtImage(ext) - } - filenamesString := strings.Join(filenames, ", ") +func getMessageForNotification(post *model.Post, translateFunc i18n.TranslateFunc) string { + if len(strings.TrimSpace(post.Message)) != 0 || len(post.Filenames) == 0 { + return post.Message + } - var attachmentPrefix string - if onlyImages { - attachmentPrefix = "Image" - } else { - attachmentPrefix = "File" - } - if len(post.Filenames) > 1 { - attachmentPrefix += "s" + // extract the filenames from their paths and determine what type of files are attached + filenames := make([]string, len(post.Filenames)) + onlyImages := true + for i, filename := range post.Filenames { + var err error + if filenames[i], err = url.QueryUnescape(filepath.Base(filename)); err != nil { + // this should never error since filepath was escaped using url.QueryEscape + filenames[i] = filepath.Base(filename) } - bodyPage.Props["PostMessage"] = userLocale("api.post.send_notifications_and_forget.sent", - map[string]interface{}{"Prefix": attachmentPrefix, "Filenames": filenamesString}) + ext := filepath.Ext(filename) + onlyImages = onlyImages && model.IsFileExtImage(ext) } - if err := utils.SendMail(user.Email, subjectPage.Render(), bodyPage.Render()); err != nil { - l4g.Error(utils.T("api.post.send_notifications_and_forget.send.error"), user.Email, err) + props := map[string]interface{}{"Filenames": strings.Join(filenames, ", ")} + + if onlyImages { + return translateFunc("api.post.get_message_for_notification.images_sent", len(filenames), props) + } else { + return translateFunc("api.post.get_message_for_notification.files_sent", len(filenames), props) } } -- cgit v1.2.3-1-g7c22