Add localized timestamp support for email notification (#8674)

* Add localized timestamp support for email notification

* Move localTime logic over to getFormattedPostTime
Этот коммит содержится в:
Chris Duarte
2018-07-13 11:10:34 -07:00
коммит произвёл Jesse Hallam
родитель 17f211c393
Коммит 62c64594cc
3 изменённых файлов: 209 добавлений и 34 удалений

Просмотреть файл

@@ -431,7 +431,13 @@ func (a *App) sendNotificationEmail(post *model.Post, user *model.User, channel
// fall back to sending a single email if we can't batch it for some reason
}
var useMilitaryTime bool
translateFunc := utils.GetUserTranslations(user.Locale)
if result := <-a.Srv.Store.Preference().Get(user.Id, model.PREFERENCE_CATEGORY_DISPLAY_SETTINGS, "use_military_time"); result.Err != nil {
useMilitaryTime = true
} else {
useMilitaryTime = result.Data.(model.Preference).Value == "true"
}
emailNotificationContentsType := model.EMAIL_NOTIFICATION_CONTENTS_FULL
if license := a.License(); license != nil && *license.Features.EmailNotificationContents {
@@ -440,17 +446,17 @@ func (a *App) sendNotificationEmail(post *model.Post, user *model.User, channel
var subjectText string
if channel.Type == model.CHANNEL_DIRECT {
subjectText = getDirectMessageNotificationEmailSubject(post, translateFunc, a.Config().TeamSettings.SiteName, senderName)
subjectText = getDirectMessageNotificationEmailSubject(user, post, translateFunc, a.Config().TeamSettings.SiteName, senderName, useMilitaryTime)
} else if channel.Type == model.CHANNEL_GROUP {
subjectText = getGroupMessageNotificationEmailSubject(post, translateFunc, a.Config().TeamSettings.SiteName, channelName, emailNotificationContentsType)
subjectText = getGroupMessageNotificationEmailSubject(user, post, translateFunc, a.Config().TeamSettings.SiteName, channelName, emailNotificationContentsType, useMilitaryTime)
} else if *a.Config().EmailSettings.UseChannelInEmailNotifications {
subjectText = getNotificationEmailSubject(post, translateFunc, a.Config().TeamSettings.SiteName, team.DisplayName+" ("+channel.DisplayName+")")
subjectText = getNotificationEmailSubject(user, post, translateFunc, a.Config().TeamSettings.SiteName, team.DisplayName+" ("+channel.DisplayName+")", useMilitaryTime)
} else {
subjectText = getNotificationEmailSubject(post, translateFunc, a.Config().TeamSettings.SiteName, team.DisplayName)
subjectText = getNotificationEmailSubject(user, post, translateFunc, a.Config().TeamSettings.SiteName, team.DisplayName, useMilitaryTime)
}
teamURL := a.GetSiteURL() + "/" + team.Name
var bodyText = a.getNotificationEmailBody(user, post, channel, channelName, senderName, team.Name, teamURL, emailNotificationContentsType, translateFunc)
var bodyText = a.getNotificationEmailBody(user, post, channel, channelName, senderName, team.Name, teamURL, emailNotificationContentsType, useMilitaryTime, translateFunc)
a.Go(func() {
if err := a.SendMail(user.Email, html.UnescapeString(subjectText), bodyText); err != nil {
@@ -468,8 +474,8 @@ func (a *App) sendNotificationEmail(post *model.Post, user *model.User, channel
/**
* 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)
func getDirectMessageNotificationEmailSubject(user *model.User, post *model.Post, translateFunc i18n.TranslateFunc, siteName string, senderName string, useMilitaryTime bool) string {
t := getFormattedPostTime(user, post, useMilitaryTime, translateFunc)
var subjectParameters = map[string]interface{}{
"SiteName": siteName,
"SenderDisplayName": senderName,
@@ -483,8 +489,8 @@ func getDirectMessageNotificationEmailSubject(post *model.Post, translateFunc i1
/**
* 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)
func getNotificationEmailSubject(user *model.User, post *model.Post, translateFunc i18n.TranslateFunc, siteName string, teamName string, useMilitaryTime bool) string {
t := getFormattedPostTime(user, post, useMilitaryTime, translateFunc)
var subjectParameters = map[string]interface{}{
"SiteName": siteName,
"TeamName": teamName,
@@ -498,8 +504,8 @@ func getNotificationEmailSubject(post *model.Post, translateFunc i18n.TranslateF
/**
* Computes the subject line for group email messages
*/
func getGroupMessageNotificationEmailSubject(post *model.Post, translateFunc i18n.TranslateFunc, siteName string, channelName string, emailNotificationContentsType string) string {
t := getFormattedPostTime(post, translateFunc)
func getGroupMessageNotificationEmailSubject(user *model.User, post *model.Post, translateFunc i18n.TranslateFunc, siteName string, channelName string, emailNotificationContentsType string, useMilitaryTime bool) string {
t := getFormattedPostTime(user, post, useMilitaryTime, translateFunc)
var subjectText string
if emailNotificationContentsType == model.EMAIL_NOTIFICATION_CONTENTS_FULL {
var subjectParameters = map[string]interface{}{
@@ -525,7 +531,7 @@ func getGroupMessageNotificationEmailSubject(post *model.Post, translateFunc i18
/**
* Computes the email body for notification messages
*/
func (a *App) getNotificationEmailBody(recipient *model.User, post *model.Post, channel *model.Channel, channelName string, senderName string, teamName string, teamURL string, emailNotificationContentsType string, translateFunc i18n.TranslateFunc) string {
func (a *App) getNotificationEmailBody(recipient *model.User, post *model.Post, channel *model.Channel, channelName string, senderName string, teamName string, teamURL string, emailNotificationContentsType string, useMilitaryTime bool, 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 {
@@ -542,7 +548,7 @@ func (a *App) getNotificationEmailBody(recipient *model.User, post *model.Post,
bodyPage.Props["TeamLink"] = teamURL
}
t := getFormattedPostTime(post, translateFunc)
t := getFormattedPostTime(recipient, post, useMilitaryTime, translateFunc)
var bodyText string
var info template.HTML
@@ -642,17 +648,34 @@ type formattedPostTime struct {
TimeZone string
}
func getFormattedPostTime(post *model.Post, translateFunc i18n.TranslateFunc) formattedPostTime {
tm := time.Unix(post.CreateAt/1000, 0)
zone, _ := tm.Zone()
func getFormattedPostTime(user *model.User, post *model.Post, useMilitaryTime bool, translateFunc i18n.TranslateFunc) formattedPostTime {
preferredTimezone := user.GetPreferredTimezone()
postTime := time.Unix(post.CreateAt/1000, 0)
zone, _ := postTime.Zone()
localTime := postTime
if preferredTimezone != "" {
loc, _ := time.LoadLocation(preferredTimezone)
if loc != nil {
localTime = postTime.In(loc)
zone, _ = localTime.Zone()
}
}
hour := localTime.Format("15")
period := ""
if !useMilitaryTime {
hour = localTime.Format("3")
period = " " + localTime.Format("PM")
}
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()),
Time: localTime,
Year: fmt.Sprintf("%d", localTime.Year()),
Month: translateFunc(localTime.Month().String()),
Day: fmt.Sprintf("%d", localTime.Day()),
Hour: fmt.Sprintf("%s", hour),
Minute: fmt.Sprintf("%02d"+period, localTime.Minute()),
TimeZone: zone,
}
}

Просмотреть файл

@@ -9,8 +9,11 @@ import (
"github.com/stretchr/testify/assert"
"fmt"
"github.com/mattermost/mattermost-server/model"
"github.com/mattermost/mattermost-server/utils"
"regexp"
"time"
)
func TestSendNotifications(t *testing.T) {
@@ -1113,11 +1116,12 @@ func TestGetDirectMessageNotificationEmailSubject(t *testing.T) {
defer th.TearDown()
expectedPrefix := "[http://localhost:8065] New Direct Message from @sender on"
user := &model.User{}
post := &model.Post{
CreateAt: 1501804801000,
}
translateFunc := utils.GetUserTranslations("en")
subject := getDirectMessageNotificationEmailSubject(post, translateFunc, "http://localhost:8065", "sender")
subject := getDirectMessageNotificationEmailSubject(user, post, translateFunc, "http://localhost:8065", "sender", true)
if !strings.HasPrefix(subject, expectedPrefix) {
t.Fatal("Expected subject line prefix '" + expectedPrefix + "', got " + subject)
}
@@ -1128,12 +1132,13 @@ func TestGetGroupMessageNotificationEmailSubjectFull(t *testing.T) {
defer th.TearDown()
expectedPrefix := "[http://localhost:8065] New Group Message in sender on"
user := &model.User{}
post := &model.Post{
CreateAt: 1501804801000,
}
translateFunc := utils.GetUserTranslations("en")
emailNotificationContentsType := model.EMAIL_NOTIFICATION_CONTENTS_FULL
subject := getGroupMessageNotificationEmailSubject(post, translateFunc, "http://localhost:8065", "sender", emailNotificationContentsType)
subject := getGroupMessageNotificationEmailSubject(user, post, translateFunc, "http://localhost:8065", "sender", emailNotificationContentsType, true)
if !strings.HasPrefix(subject, expectedPrefix) {
t.Fatal("Expected subject line prefix '" + expectedPrefix + "', got " + subject)
}
@@ -1144,12 +1149,13 @@ func TestGetGroupMessageNotificationEmailSubjectGeneric(t *testing.T) {
defer th.TearDown()
expectedPrefix := "[http://localhost:8065] New Group Message on"
user := &model.User{}
post := &model.Post{
CreateAt: 1501804801000,
}
translateFunc := utils.GetUserTranslations("en")
emailNotificationContentsType := model.EMAIL_NOTIFICATION_CONTENTS_GENERIC
subject := getGroupMessageNotificationEmailSubject(post, translateFunc, "http://localhost:8065", "sender", emailNotificationContentsType)
subject := getGroupMessageNotificationEmailSubject(user, post, translateFunc, "http://localhost:8065", "sender", emailNotificationContentsType, true)
if !strings.HasPrefix(subject, expectedPrefix) {
t.Fatal("Expected subject line prefix '" + expectedPrefix + "', got " + subject)
}
@@ -1160,11 +1166,12 @@ func TestGetNotificationEmailSubject(t *testing.T) {
defer th.TearDown()
expectedPrefix := "[http://localhost:8065] Notification in team on"
user := &model.User{}
post := &model.Post{
CreateAt: 1501804801000,
}
translateFunc := utils.GetUserTranslations("en")
subject := getNotificationEmailSubject(post, translateFunc, "http://localhost:8065", "team")
subject := getNotificationEmailSubject(user, post, translateFunc, "http://localhost:8065", "team", true)
if !strings.HasPrefix(subject, expectedPrefix) {
t.Fatal("Expected subject line prefix '" + expectedPrefix + "', got " + subject)
}
@@ -1189,7 +1196,7 @@ func TestGetNotificationEmailBodyFullNotificationPublicChannel(t *testing.T) {
emailNotificationContentsType := model.EMAIL_NOTIFICATION_CONTENTS_FULL
translateFunc := utils.GetUserTranslations("en")
body := th.App.getNotificationEmailBody(recipient, post, channel, channelName, senderName, teamName, teamURL, emailNotificationContentsType, translateFunc)
body := th.App.getNotificationEmailBody(recipient, post, channel, channelName, senderName, teamName, teamURL, emailNotificationContentsType, true, translateFunc)
if !strings.Contains(body, "You have a new notification.") {
t.Fatal("Expected email text 'You have a new notification. Got " + body)
}
@@ -1226,7 +1233,7 @@ func TestGetNotificationEmailBodyFullNotificationGroupChannel(t *testing.T) {
emailNotificationContentsType := model.EMAIL_NOTIFICATION_CONTENTS_FULL
translateFunc := utils.GetUserTranslations("en")
body := th.App.getNotificationEmailBody(recipient, post, channel, channelName, senderName, teamName, teamURL, emailNotificationContentsType, translateFunc)
body := th.App.getNotificationEmailBody(recipient, post, channel, channelName, senderName, teamName, teamURL, emailNotificationContentsType, true, translateFunc)
if !strings.Contains(body, "You have a new Group Message.") {
t.Fatal("Expected email text 'You have a new Group Message. Got " + body)
}
@@ -1263,7 +1270,7 @@ func TestGetNotificationEmailBodyFullNotificationPrivateChannel(t *testing.T) {
emailNotificationContentsType := model.EMAIL_NOTIFICATION_CONTENTS_FULL
translateFunc := utils.GetUserTranslations("en")
body := th.App.getNotificationEmailBody(recipient, post, channel, channelName, senderName, teamName, teamURL, emailNotificationContentsType, translateFunc)
body := th.App.getNotificationEmailBody(recipient, post, channel, channelName, senderName, teamName, teamURL, emailNotificationContentsType, true, translateFunc)
if !strings.Contains(body, "You have a new notification.") {
t.Fatal("Expected email text 'You have a new notification. Got " + body)
}
@@ -1300,7 +1307,7 @@ func TestGetNotificationEmailBodyFullNotificationDirectChannel(t *testing.T) {
emailNotificationContentsType := model.EMAIL_NOTIFICATION_CONTENTS_FULL
translateFunc := utils.GetUserTranslations("en")
body := th.App.getNotificationEmailBody(recipient, post, channel, channelName, senderName, teamName, teamURL, emailNotificationContentsType, translateFunc)
body := th.App.getNotificationEmailBody(recipient, post, channel, channelName, senderName, teamName, teamURL, emailNotificationContentsType, true, translateFunc)
if !strings.Contains(body, "You have a new Direct Message.") {
t.Fatal("Expected email text 'You have a new Direct Message. Got " + body)
}
@@ -1315,6 +1322,143 @@ func TestGetNotificationEmailBodyFullNotificationDirectChannel(t *testing.T) {
}
}
func TestGetNotificationEmailBodyFullNotificationLocaleTimeWithTimezone(t *testing.T) {
th := Setup()
defer th.TearDown()
recipient := &model.User{
Timezone: model.DefaultUserTimezone(),
}
recipient.Timezone["automaticTimezone"] = "America/New_York"
post := &model.Post{
CreateAt: 1524663790000,
Message: "This is the message",
}
channel := &model.Channel{
DisplayName: "ChannelName",
Type: model.CHANNEL_DIRECT,
}
channelName := "ChannelName"
senderName := "sender"
teamName := "team"
teamURL := "http://localhost:8065/" + teamName
emailNotificationContentsType := model.EMAIL_NOTIFICATION_CONTENTS_FULL
translateFunc := utils.GetUserTranslations("en")
body := th.App.getNotificationEmailBody(recipient, post, channel, channelName, senderName, teamName, teamURL, emailNotificationContentsType, false, translateFunc)
r, _ := regexp.Compile("E([S|D]+)T")
zone := r.FindString(body)
if !strings.Contains(body, "sender - 9:43 AM "+zone+", April 25") {
t.Fatal("Expected email text 'sender - 9:43 AM " + zone + ", April 25'. Got " + body)
}
}
func TestGetNotificationEmailBodyFullNotificationLocaleTimeNoTimezone(t *testing.T) {
th := Setup()
defer th.TearDown()
recipient := &model.User{
Timezone: model.DefaultUserTimezone(),
}
post := &model.Post{
CreateAt: 1524681000000,
Message: "This is the message",
}
channel := &model.Channel{
DisplayName: "ChannelName",
Type: model.CHANNEL_DIRECT,
}
channelName := "ChannelName"
senderName := "sender"
teamName := "team"
teamURL := "http://localhost:8065/" + teamName
emailNotificationContentsType := model.EMAIL_NOTIFICATION_CONTENTS_FULL
translateFunc := utils.GetUserTranslations("en")
tm := time.Unix(post.CreateAt/1000, 0)
zone, _ := tm.Zone()
formattedTime := 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,
}
body := th.App.getNotificationEmailBody(recipient, post, channel, channelName, senderName, teamName, teamURL, emailNotificationContentsType, true, translateFunc)
postTimeLine := fmt.Sprintf("sender - %s:%s %s, %s %s", formattedTime.Hour, formattedTime.Minute, formattedTime.TimeZone, formattedTime.Month, formattedTime.Day)
if !strings.Contains(body, postTimeLine) {
t.Fatal("Expected email text '" + postTimeLine + " '. Got " + body)
}
}
func TestGetNotificationEmailBodyFullNotificationLocaleTime12Hour(t *testing.T) {
th := Setup()
defer th.TearDown()
recipient := &model.User{
Timezone: model.DefaultUserTimezone(),
}
recipient.Timezone["automaticTimezone"] = "America/New_York"
post := &model.Post{
CreateAt: 1524681000000, // 1524681000 // 1524681000000
Message: "This is the message",
}
channel := &model.Channel{
DisplayName: "ChannelName",
Type: model.CHANNEL_DIRECT,
}
channelName := "ChannelName"
senderName := "sender"
teamName := "team"
teamURL := "http://localhost:8065/" + teamName
emailNotificationContentsType := model.EMAIL_NOTIFICATION_CONTENTS_FULL
translateFunc := utils.GetUserTranslations("en")
body := th.App.getNotificationEmailBody(recipient, post, channel, channelName, senderName, teamName, teamURL, emailNotificationContentsType, false, translateFunc)
if !strings.Contains(body, "sender - 2:30 PM") {
t.Fatal("Expected email text 'sender - 2:30 PM'. Got " + body)
}
if !strings.Contains(body, "April 25") {
t.Fatal("Expected email text 'April 25'. Got " + body)
}
}
func TestGetNotificationEmailBodyFullNotificationLocaleTime24Hour(t *testing.T) {
th := Setup()
defer th.TearDown()
recipient := &model.User{
Timezone: model.DefaultUserTimezone(),
}
recipient.Timezone["automaticTimezone"] = "America/New_York"
post := &model.Post{
CreateAt: 1524681000000,
Message: "This is the message",
}
channel := &model.Channel{
DisplayName: "ChannelName",
Type: model.CHANNEL_DIRECT,
}
channelName := "ChannelName"
senderName := "sender"
teamName := "team"
teamURL := "http://localhost:8065/" + teamName
emailNotificationContentsType := model.EMAIL_NOTIFICATION_CONTENTS_FULL
translateFunc := utils.GetUserTranslations("en")
body := th.App.getNotificationEmailBody(recipient, post, channel, channelName, senderName, teamName, teamURL, emailNotificationContentsType, true, translateFunc)
if !strings.Contains(body, "sender - 14:30") {
t.Fatal("Expected email text 'sender - 14:30'. Got " + body)
}
if !strings.Contains(body, "April 25") {
t.Fatal("Expected email text 'April 25'. Got " + body)
}
}
// from here
func TestGetNotificationEmailBodyGenericNotificationPublicChannel(t *testing.T) {
th := Setup()
@@ -1335,7 +1479,7 @@ func TestGetNotificationEmailBodyGenericNotificationPublicChannel(t *testing.T)
emailNotificationContentsType := model.EMAIL_NOTIFICATION_CONTENTS_GENERIC
translateFunc := utils.GetUserTranslations("en")
body := th.App.getNotificationEmailBody(recipient, post, channel, channelName, senderName, teamName, teamURL, emailNotificationContentsType, translateFunc)
body := th.App.getNotificationEmailBody(recipient, post, channel, channelName, senderName, teamName, teamURL, emailNotificationContentsType, true, translateFunc)
if !strings.Contains(body, "You have a new notification from @"+senderName) {
t.Fatal("Expected email text 'You have a new notification from @" + senderName + "'. Got " + body)
}
@@ -1369,7 +1513,7 @@ func TestGetNotificationEmailBodyGenericNotificationGroupChannel(t *testing.T) {
emailNotificationContentsType := model.EMAIL_NOTIFICATION_CONTENTS_GENERIC
translateFunc := utils.GetUserTranslations("en")
body := th.App.getNotificationEmailBody(recipient, post, channel, channelName, senderName, teamName, teamURL, emailNotificationContentsType, translateFunc)
body := th.App.getNotificationEmailBody(recipient, post, channel, channelName, senderName, teamName, teamURL, emailNotificationContentsType, true, translateFunc)
if !strings.Contains(body, "You have a new Group Message from @"+senderName) {
t.Fatal("Expected email text 'You have a new Group Message from @" + senderName + "'. Got " + body)
}
@@ -1403,7 +1547,7 @@ func TestGetNotificationEmailBodyGenericNotificationPrivateChannel(t *testing.T)
emailNotificationContentsType := model.EMAIL_NOTIFICATION_CONTENTS_GENERIC
translateFunc := utils.GetUserTranslations("en")
body := th.App.getNotificationEmailBody(recipient, post, channel, channelName, senderName, teamName, teamURL, emailNotificationContentsType, translateFunc)
body := th.App.getNotificationEmailBody(recipient, post, channel, channelName, senderName, teamName, teamURL, emailNotificationContentsType, true, translateFunc)
if !strings.Contains(body, "You have a new notification from @"+senderName) {
t.Fatal("Expected email text 'You have a new notification from @" + senderName + "'. Got " + body)
}
@@ -1437,7 +1581,7 @@ func TestGetNotificationEmailBodyGenericNotificationDirectChannel(t *testing.T)
emailNotificationContentsType := model.EMAIL_NOTIFICATION_CONTENTS_GENERIC
translateFunc := utils.GetUserTranslations("en")
body := th.App.getNotificationEmailBody(recipient, post, channel, channelName, senderName, teamName, teamURL, emailNotificationContentsType, translateFunc)
body := th.App.getNotificationEmailBody(recipient, post, channel, channelName, senderName, teamName, teamURL, emailNotificationContentsType, true, translateFunc)
if !strings.Contains(body, "You have a new Direct Message from @"+senderName) {
t.Fatal("Expected email text 'You have a new Direct Message from @" + senderName + "'. Got " + body)
}

Просмотреть файл

@@ -497,6 +497,14 @@ func (u *User) IsSAMLUser() bool {
return u.AuthService == USER_AUTH_SERVICE_SAML
}
func (u *User) GetPreferredTimezone() string {
if u.Timezone["useAutomaticTimezone"] == "true" {
return u.Timezone["automaticTimezone"]
}
return u.Timezone["manualTimezone"]
}
// UserFromJson will decode the input and return a User
func UserFromJson(data io.Reader) *User {
var user *User