MM-11102 Show channel links in notification emails (#11670)

* Added code to hyperlink channels in HTML email

Adjusted imports

* Rending channel display name in hyperlink instead of channel URL handler

Addressed go formatting warning

* [MM-11102] Channel links should show as links in notification emails

* Escaped post;ChannelMentions, GetChannelsByName functions; Changed tests

* remove mlog.info

* MultiMention and Escape tests; Change logging
Этот коммит содержится в:
Micah Thompson
2019-08-29 06:09:09 -04:00
коммит произвёл Saturnino Abril
родитель b91824328a
Коммит 2980d341d4
2 изменённых файлов: 211 добавлений и 1 удалений

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

@@ -6,6 +6,7 @@ package app
import (
"fmt"
"html"
"html/template"
"net/url"
"path/filepath"
"strings"
@@ -171,7 +172,10 @@ func (a *App) getNotificationEmailBody(recipient *model.User, post *model.Post,
var bodyPage *utils.HTMLTemplate
if emailNotificationContentsType == model.EMAIL_NOTIFICATION_CONTENTS_FULL {
bodyPage = a.NewEmailTemplate("post_body_full", recipient.Locale)
bodyPage.Props["PostMessage"] = a.GetMessageForNotification(post, translateFunc)
postMessage := a.GetMessageForNotification(post, translateFunc)
postMessage = html.EscapeString(postMessage)
normalizedPostMessage := a.generateHyperlinkForChannels(postMessage, teamName, teamURL)
bodyPage.Props["PostMessage"] = template.HTML(normalizedPostMessage)
} else {
bodyPage = a.NewEmailTemplate("post_body_generic", recipient.Locale)
}
@@ -283,6 +287,36 @@ func getFormattedPostTime(user *model.User, post *model.Post, useMilitaryTime bo
}
}
func (a *App) generateHyperlinkForChannels(postMessage, teamName, teamURL string) string {
team, err := a.GetTeamByName(teamName)
if err != nil {
mlog.Error("Encountered error while looking up team by name", mlog.String("Team Name", teamName), mlog.Err(err))
return postMessage
}
channelNames := model.ChannelMentions(postMessage)
if len(channelNames) == 0 {
return postMessage
}
channels, err := a.GetChannelsByNames(channelNames, team.Id)
if err != nil {
mlog.Error("Encountered error while getting channels", mlog.Err(err))
return postMessage
}
visited := make(map[string]bool)
for _, ch := range channels {
if !visited[ch.Id] && ch.Type == model.CHANNEL_OPEN {
channelURL := teamURL + "/channels/" + ch.Name
channelHyperLink := fmt.Sprintf("<a href='%s'>%s</a>", channelURL, "~"+ch.Name)
postMessage = strings.Replace(postMessage, "~"+ch.Name, channelHyperLink, -1)
visited[ch.Id] = true
}
}
return postMessage
}
func (a *App) GetMessageForNotification(post *model.Post, translateFunc i18n.TranslateFunc) string {
if len(strings.TrimSpace(post.Message)) != 0 || len(post.FileIds) == 0 {
return post.Message