System Console: Email notification content setting (#7122)

* PLT-7195: Added new config option, new license feature, and config UI to system console. Still need to implement behaviour change in email batching code

* PLT-7195: Modified batch emails to respect email notification content type setting

* PLT-7195: Tweaking the colours a bit

* PLT-7195: Added support for email notification content type setting in immediate (non-batched) notification messages. Attempted to clean up the code somewhat. Unit tests coming in a future commit

* PLT-7195: Added unit tests for non-batched emails

* Checked license when applying email content settings

* Changed return type of getFormattedPostTime
This commit is contained in:
Jonathan
2017-08-05 19:52:35 -04:00
committed by Saturnino Abril
parent 9f3713aa98
commit 178ccd16cb
19 changed files with 802 additions and 139 deletions

View File

@@ -349,67 +349,25 @@ func sendNotificationEmail(post *model.Post, user *model.User, channel *model.Ch
// fall back to sending a single email if we can't batch it for some reason
}
var channelName string
var bodyText string
translateFunc := utils.GetUserTranslations(user.Locale)
var subjectText string
var mailTemplate string
var mailParameters map[string]interface{}
if channel.Type == model.CHANNEL_DIRECT {
subjectText = getDirectMessageNotificationEmailSubject(post, translateFunc, utils.Cfg.TeamSettings.SiteName, senderName)
} else {
subjectText = getNotificationEmailSubject(post, translateFunc, utils.Cfg.TeamSettings.SiteName, team.DisplayName)
}
emailNotificationContentsType := model.EMAIL_NOTIFICATION_CONTENTS_FULL
if utils.IsLicensed && *utils.License.Features.EmailNotificationContents {
emailNotificationContentsType = *utils.Cfg.EmailSettings.EmailNotificationContentsType
}
teamURL := utils.GetSiteURL() + "/" + team.Name
tm := time.Unix(post.CreateAt/1000, 0)
userLocale := utils.GetUserTranslations(user.Locale)
month := userLocale(tm.Month().String())
day := fmt.Sprintf("%d", tm.Day())
year := fmt.Sprintf("%d", tm.Year())
zone, _ := tm.Zone()
if channel.Type == model.CHANNEL_DIRECT {
bodyText = userLocale("api.post.send_notifications_and_forget.message_body")
subjectText = userLocale("api.post.send_notifications_and_forget.message_subject")
senderDisplayName := senderName
mailTemplate = "api.templates.post_subject_in_direct_message"
mailParameters = map[string]interface{}{"SubjectText": subjectText,
"SenderDisplayName": senderDisplayName, "Month": month, "Day": day, "Year": year}
} else if channel.Type == model.CHANNEL_GROUP {
bodyText = userLocale("api.post.send_notifications_and_forget.mention_body")
senderDisplayName := senderName
mailTemplate = "api.templates.post_subject_in_group_message"
mailParameters = map[string]interface{}{"SenderDisplayName": senderDisplayName, "Month": month, "Day": day, "Year": year}
channelName = userLocale("api.templates.channel_name.group")
} else {
bodyText = userLocale("api.post.send_notifications_and_forget.mention_body")
subjectText = userLocale("api.post.send_notifications_and_forget.mention_subject")
channelName = channel.DisplayName
mailTemplate = "api.templates.post_subject_in_channel"
mailParameters = map[string]interface{}{"SubjectText": subjectText, "TeamDisplayName": team.DisplayName,
"ChannelName": channelName, "Month": month, "Day": day, "Year": year}
}
subject := fmt.Sprintf("[%v] %v", utils.Cfg.TeamSettings.SiteName, userLocale(mailTemplate, mailParameters))
bodyPage := utils.NewHTMLTemplate("post_body", user.Locale)
bodyPage.Props["SiteURL"] = utils.GetSiteURL()
bodyPage.Props["PostMessage"] = GetMessageForNotification(post, userLocale)
if team.Name != "select_team" {
bodyPage.Props["TeamLink"] = teamURL + "/pl/" + post.Id
} else {
bodyPage.Props["TeamLink"] = teamURL
}
bodyPage.Props["BodyText"] = bodyText
bodyPage.Props["Button"] = userLocale("api.templates.post_body.button")
bodyPage.Html["Info"] = template.HTML(userLocale("api.templates.post_body.info",
map[string]interface{}{"ChannelName": channelName, "SenderName": senderName,
"Hour": fmt.Sprintf("%02d", tm.Hour()), "Minute": fmt.Sprintf("%02d", tm.Minute()),
"TimeZone": zone, "Month": month, "Day": day}))
var bodyText = getNotificationEmailBody(user, post, channel, senderName, team.Name, teamURL, emailNotificationContentsType, translateFunc)
go func() {
if err := utils.SendMail(user.Email, html.UnescapeString(subject), bodyPage.Render()); err != nil {
if err := utils.SendMail(user.Email, html.UnescapeString(subjectText), bodyText); err != nil {
l4g.Error(utils.T("api.post.send_notifications_and_forget.send.error"), user.Email, err)
}
}()
@@ -421,6 +379,149 @@ func sendNotificationEmail(post *model.Post, user *model.User, channel *model.Ch
return nil
}
/**
* Computes the subject line for direct notification email messages
*/
func getDirectMessageNotificationEmailSubject(post *model.Post, translateFunc i18n.TranslateFunc, siteName string, senderName string) string {
t := getFormattedPostTime(post, translateFunc)
var subjectParameters = map[string]interface{}{
"SiteName": siteName,
"SenderDisplayName": senderName,
"Month": t.Month,
"Day": t.Day,
"Year": t.Year,
}
return translateFunc("app.notification.subject.direct.full", subjectParameters)
}
/**
* Computes the subject line for group, public, and private email messages
*/
func getNotificationEmailSubject(post *model.Post, translateFunc i18n.TranslateFunc, siteName string, teamName string) string {
t := getFormattedPostTime(post, translateFunc)
var subjectParameters = map[string]interface{}{
"SiteName": siteName,
"TeamName": teamName,
"Month": t.Month,
"Day": t.Day,
"Year": t.Year,
}
return translateFunc("app.notification.subject.notification.full", subjectParameters)
}
/**
* Computes the email body for notification messages
*/
func getNotificationEmailBody(recipient *model.User, post *model.Post, channel *model.Channel, senderName string, teamName string, teamURL string, emailNotificationContentsType string, translateFunc i18n.TranslateFunc) string {
// only include message contents in notification email if email notification contents type is set to full
var bodyPage *utils.HTMLTemplate
if emailNotificationContentsType == model.EMAIL_NOTIFICATION_CONTENTS_FULL {
bodyPage = utils.NewHTMLTemplate("post_body_full", recipient.Locale)
bodyPage.Props["PostMessage"] = GetMessageForNotification(post, translateFunc)
} else {
bodyPage = utils.NewHTMLTemplate("post_body_generic", recipient.Locale)
}
bodyPage.Props["SiteURL"] = utils.GetSiteURL()
if teamName != "select_team" {
bodyPage.Props["TeamLink"] = teamURL + "/pl/" + post.Id
} else {
bodyPage.Props["TeamLink"] = teamURL
}
var channelName = channel.DisplayName
if channel.Type == model.CHANNEL_GROUP {
channelName = translateFunc("api.templates.channel_name.group")
}
t := getFormattedPostTime(post, translateFunc)
var bodyText string
var info string
if channel.Type == model.CHANNEL_DIRECT {
if emailNotificationContentsType == model.EMAIL_NOTIFICATION_CONTENTS_FULL {
bodyText = translateFunc("app.notification.body.intro.direct.full")
info = translateFunc("app.notification.body.text.direct.full",
map[string]interface{}{
"SenderName": senderName,
"Hour": t.Hour,
"Minute": t.Minute,
"TimeZone": t.TimeZone,
"Month": t.Month,
"Day": t.Day,
})
} else {
bodyText = translateFunc("app.notification.body.intro.direct.generic", map[string]interface{}{
"SenderName": senderName,
})
info = translateFunc("app.notification.body.text.direct.generic",
map[string]interface{}{
"Hour": t.Hour,
"Minute": t.Minute,
"TimeZone": t.TimeZone,
"Month": t.Month,
"Day": t.Day,
})
}
} else {
if emailNotificationContentsType == model.EMAIL_NOTIFICATION_CONTENTS_FULL {
bodyText = translateFunc("app.notification.body.intro.notification.full")
info = translateFunc("app.notification.body.text.notification.full",
map[string]interface{}{
"ChannelName": channelName,
"SenderName": senderName,
"Hour": t.Hour,
"Minute": t.Minute,
"TimeZone": t.TimeZone,
"Month": t.Month,
"Day": t.Day,
})
} else {
bodyText = translateFunc("app.notification.body.intro.notification.generic", map[string]interface{}{
"SenderName": senderName,
})
info = translateFunc("app.notification.body.text.notification.generic",
map[string]interface{}{
"Hour": t.Hour,
"Minute": t.Minute,
"TimeZone": t.TimeZone,
"Month": t.Month,
"Day": t.Day,
})
}
}
bodyPage.Props["BodyText"] = bodyText
bodyPage.Html["Info"] = template.HTML(info)
bodyPage.Props["Button"] = translateFunc("api.templates.post_body.button")
return bodyPage.Render()
}
type formattedPostTime struct {
Time time.Time
Year string
Month string
Day string
Hour string
Minute string
TimeZone string
}
func getFormattedPostTime(post *model.Post, translateFunc i18n.TranslateFunc) formattedPostTime {
tm := time.Unix(post.CreateAt/1000, 0)
zone, _ := tm.Zone()
return formattedPostTime{
Time: tm,
Year: fmt.Sprintf("%d", tm.Year()),
Month: translateFunc(tm.Month().String()),
Day: fmt.Sprintf("%d", tm.Day()),
Hour: fmt.Sprintf("%02d", tm.Hour()),
Minute: fmt.Sprintf("%02d", tm.Minute()),
TimeZone: zone,
}
}
func GetMessageForNotification(post *model.Post, translateFunc i18n.TranslateFunc) string {
if len(strings.TrimSpace(post.Message)) != 0 || len(post.FileIds) == 0 {
return post.Message