Some checks failed
Server CI / Postgres (shard 0) (push) Has been skipped
Server CI / Postgres (shard 3) (push) Has been skipped
BuildEnv Docker Image / build-image (push) Failing after 4s
BuildEnv Docker Image / build-image-fips (push) Failing after 4s
Migration Review & Release Notes / Generate migration review & release notes (push) Failing after 3s
Server CI / Build Environment (push) Failing after 3s
Server CI / Postgres (shard 1) (push) Has been skipped
Tools CI / check-style (mattermost-govet) (push) Failing after 4s
Server CI / Postgres (shard 2) (push) Has been skipped
Tools CI / check-style (mmgotool) (push) Failing after 3s
Server CI / Elasticsearch v8 Compatibility (push) Has been skipped
Server CI / OpenSearch v2 Compatibility (push) Has been skipped
Server CI / Postgres FIPS (shard 0) (push) Has been skipped
Tools CI / Test (mattermost-govet) (push) Failing after 3s
Server CI / Postgres FIPS (shard 1) (push) Has been skipped
Server CI / Postgres FIPS (shard 2) (push) Has been skipped
Tools CI / Test (mmgotool) (push) Failing after 3s
Server CI / Postgres FIPS (shard 3) (push) Has been skipped
Server CI / Run mmctl tests (push) Has been skipped
Server CI / Run mmctl tests (FIPS) (push) Has been skipped
Server CI / Merge Postgres Test Results (push) Has been skipped
Server CI / Merge Postgres FIPS Test Results (push) Has been skipped
Server CI / Check backport migrations (push) Has been skipped
Server CI / Check generated files (push) Has been skipped
Server CI / check-style (push) Has been skipped
Server CI / Vet API (push) Has been skipped
Server CI / Build mattermost server app (push) Has been skipped
Server CI / Server CI Complete (push) Failing after 2s
Server CI / Server CI Artifacts (push) Has been skipped
Server CI / Sentry Upload (push) Has been skipped
Server CI / Server CI Report (push) Failing after 5m1s
Server CI Nightly Race / Race Detector (push) Has been skipped
CodeQL / Analyze (go) (push) Has been cancelled
CodeQL / Analyze (javascript) (push) Has been cancelled
Webapp CI Cache Warm / Warm Webapp node_modules Cache (push) Has been cancelled
E2E CI Cache Warm / Warm E2E npm Registry Cache (push) Has been cancelled
API / build (push) Has been cancelled
Web App CI / check-types (push) Has been cancelled
Web App CI / check-lint (push) Has been cancelled
Web App CI / check-i18n (push) Has been cancelled
Web App CI / check-external-links (push) Has been cancelled
Web App CI / test (platform) (push) Has been cancelled
Web App CI / test (mattermost-redux) (push) Has been cancelled
Web App CI / test (channels shard 1/4) (push) Has been cancelled
Web App CI / test (channels shard 2/4) (push) Has been cancelled
Web App CI / test (channels shard 3/4) (push) Has been cancelled
Web App CI / test (channels shard 4/4) (push) Has been cancelled
Web App CI / upload-coverage (push) Has been cancelled
Web App CI / build (push) Has been cancelled
YAML Lint / yamllint (push) Has been cancelled
Push mirrored docker images / cd/Push mirrored docker images (push) Has been cancelled
docs-cd / Build and deploy docs (push) Has been cancelled
Docs CI / build (push) Has been cancelled
E2E Cache Warm: Cypress deps / Warm Cypress deps cache (push) Has been cancelled
Scorecards supply-chain security / Scorecard analysis (push) Has been cancelled
Server CI Weekly / Postgres FIPS (push) Has been skipped
Server CI Weekly / Run mmctl tests (FIPS) (push) Has been skipped
Server CI Weekly / Postgres with binary parameters (push) Has been skipped
Server CI Weekly / Build Environment (push) Failing after 1s
Server CI Cache Warm / Warm Go Module and Build Cache (push) Failing after 2s
Server CI Nightly Race / Build Environment (push) Failing after 1s
187 lines
6.1 KiB
Go
187 lines
6.1 KiB
Go
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
|
// See LICENSE.txt for license information.
|
|
|
|
package email
|
|
|
|
import (
|
|
"fmt"
|
|
"html"
|
|
"html/template"
|
|
"net/url"
|
|
"strings"
|
|
|
|
"github.com/mattermost/mattermost/server/public/model"
|
|
"github.com/mattermost/mattermost/server/public/shared/i18n"
|
|
"github.com/mattermost/mattermost/server/public/shared/mlog"
|
|
"github.com/mattermost/mattermost/server/v8/channels/utils"
|
|
)
|
|
|
|
type FieldRow struct {
|
|
Cells []*model.MessageAttachmentField
|
|
}
|
|
type EmailMessageAttachment struct {
|
|
model.MessageAttachment
|
|
|
|
Pretext template.HTML
|
|
Text template.HTML
|
|
FieldRows []FieldRow
|
|
}
|
|
|
|
func (es *Service) GetMessageForNotification(post *model.Post, teamName, siteUrl string, translateFunc i18n.TranslateFunc) string {
|
|
if strings.TrimSpace(post.Message) != "" || len(post.FileIds) == 0 {
|
|
return es.prepareNotificationMessageForEmail(post.Message, teamName, siteUrl)
|
|
}
|
|
|
|
// normalize the filenames and determine what type of files are attached
|
|
infos, err := es.store.FileInfo().GetForPost(post.Id, true, false, true)
|
|
if err != nil {
|
|
mlog.Warn("Encountered error when getting files for notification message", mlog.String("post_id", post.Id), mlog.Err(err))
|
|
}
|
|
|
|
filenames := make([]string, len(infos))
|
|
onlyImages := true
|
|
for i, info := range infos {
|
|
filename := info.Name
|
|
if unescaped, err := url.QueryUnescape(filename); err == nil {
|
|
filename = unescaped
|
|
}
|
|
filenames[i] = filename
|
|
|
|
onlyImages = onlyImages && info.IsImage()
|
|
}
|
|
|
|
props := map[string]any{"Filenames": strings.Join(filenames, ", ")}
|
|
|
|
if onlyImages {
|
|
return string(i18n.TranslateAsHTML(translateFunc, "api.post.get_message_for_notification.images_sent", map[string]any{
|
|
"Count": len(filenames),
|
|
"Filenames": props["Filenames"],
|
|
}))
|
|
}
|
|
return string(i18n.TranslateAsHTML(translateFunc, "api.post.get_message_for_notification.files_sent", map[string]any{
|
|
"Count": len(filenames),
|
|
"Filenames": props["Filenames"],
|
|
}))
|
|
}
|
|
|
|
func ProcessMessageAttachments(post *model.Post, siteURL string) []*EmailMessageAttachment {
|
|
emailMessageAttachments := []*EmailMessageAttachment{}
|
|
|
|
for _, messageAttachment := range post.Attachments() {
|
|
emailMessageAttachment := &EmailMessageAttachment{
|
|
MessageAttachment: *messageAttachment,
|
|
Pretext: prepareTextForEmail(messageAttachment.Pretext, siteURL),
|
|
Text: prepareTextForEmail(messageAttachment.Text, siteURL),
|
|
}
|
|
|
|
stripedTitle, err := utils.StripMarkdown(emailMessageAttachment.Title)
|
|
if err != nil {
|
|
mlog.Warn("Failed parse to markdown from messageatatchment title", mlog.String("post_id", post.Id), mlog.Err(err))
|
|
stripedTitle = ""
|
|
}
|
|
|
|
emailMessageAttachment.Title = stripedTitle
|
|
|
|
shortFieldRow := FieldRow{}
|
|
|
|
for i := range messageAttachment.Fields {
|
|
// Create a new instance to avoid altering the original pointer reference
|
|
// We update field value to parse markdown.
|
|
// If we do that on the original pointer, the rendered text in mattermost
|
|
// becomes invalid as its no longer a markdown string, but rather an HTML string.
|
|
field := &model.MessageAttachmentField{
|
|
Title: messageAttachment.Fields[i].Title,
|
|
Value: messageAttachment.Fields[i].Value,
|
|
Short: messageAttachment.Fields[i].Short,
|
|
}
|
|
|
|
if stringValue, ok := field.Value.(string); ok {
|
|
field.Value = prepareTextForEmail(stringValue, siteURL)
|
|
}
|
|
|
|
if !field.Short {
|
|
if len(shortFieldRow.Cells) > 0 {
|
|
emailMessageAttachment.FieldRows = append(emailMessageAttachment.FieldRows, shortFieldRow)
|
|
shortFieldRow = FieldRow{}
|
|
}
|
|
|
|
emailMessageAttachment.FieldRows = append(emailMessageAttachment.FieldRows, FieldRow{[]*model.MessageAttachmentField{field}})
|
|
} else {
|
|
shortFieldRow.Cells = append(shortFieldRow.Cells, field)
|
|
|
|
if len(shortFieldRow.Cells) == 2 {
|
|
emailMessageAttachment.FieldRows = append(emailMessageAttachment.FieldRows, shortFieldRow)
|
|
shortFieldRow = FieldRow{}
|
|
}
|
|
}
|
|
}
|
|
|
|
// collect any leftover short fields
|
|
if len(shortFieldRow.Cells) > 0 {
|
|
emailMessageAttachment.FieldRows = append(emailMessageAttachment.FieldRows, shortFieldRow)
|
|
shortFieldRow = FieldRow{}
|
|
}
|
|
|
|
emailMessageAttachments = append(emailMessageAttachments, emailMessageAttachment)
|
|
}
|
|
|
|
return emailMessageAttachments
|
|
}
|
|
|
|
func prepareTextForEmail(text, siteURL string) template.HTML {
|
|
escapedText := html.EscapeString(text)
|
|
markdownText, err := utils.MarkdownToHTML(escapedText, siteURL)
|
|
if err != nil {
|
|
mlog.Warn("Encountered error while converting markdown to HTML", mlog.Err(err))
|
|
return template.HTML(text)
|
|
}
|
|
|
|
return template.HTML(markdownText)
|
|
}
|
|
|
|
func (es *Service) prepareNotificationMessageForEmail(postMessage, teamName, siteURL string) string {
|
|
postMessage = html.EscapeString(postMessage)
|
|
mdPostMessage, mdErr := utils.MarkdownToHTML(postMessage, siteURL)
|
|
if mdErr != nil {
|
|
mlog.Warn("Encountered error while converting markdown to HTML", mlog.Err(mdErr))
|
|
mdPostMessage = postMessage
|
|
}
|
|
|
|
landingURL := siteURL + "/landing#/" + teamName
|
|
normalizedPostMessage, err := es.GenerateHyperlinkForChannels(mdPostMessage, teamName, landingURL)
|
|
if err != nil {
|
|
mlog.Warn("Encountered error while generating hyperlink for channels", mlog.String("team_name", teamName), mlog.Err(err))
|
|
normalizedPostMessage = mdPostMessage
|
|
}
|
|
return normalizedPostMessage
|
|
}
|
|
|
|
func (es *Service) GenerateHyperlinkForChannels(postMessage, teamName, landingURL string) (string, error) {
|
|
channelNames := model.ChannelMentions(postMessage)
|
|
if len(channelNames) == 0 {
|
|
return postMessage, nil
|
|
}
|
|
|
|
team, err := es.Store().Team().GetByName(teamName)
|
|
if err != nil {
|
|
mlog.Error("Team not found with the name", mlog.String("team_name", teamName), mlog.Err(err))
|
|
return postMessage, nil
|
|
}
|
|
|
|
channels, err := es.store.Channel().GetByNames(team.Id, channelNames, true)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
visited := make(map[string]bool)
|
|
for _, ch := range channels {
|
|
if !visited[ch.Id] && ch.Type == model.ChannelTypeOpen {
|
|
channelURL := landingURL + "/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, nil
|
|
}
|