From d502d82016e4a35325351fe9d31fcefe907b17d7 Mon Sep 17 00:00:00 2001 From: hmhealey Date: Mon, 20 Jul 2015 17:16:43 -0400 Subject: [PATCH 1/3] Provide a replacement message for email notifications for posts only containing files --- api/post.go | 33 +++++++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/api/post.go b/api/post.go index 27dedbf714..172347a46c 100644 --- a/api/post.go +++ b/api/post.go @@ -405,6 +405,31 @@ func fireAndForgetNotifications(post *model.Post, teamId, teamUrl string) { bodyPage.Props["PostMessage"] = model.ClearMentionTags(post.Message) bodyPage.Props["TeamLink"] = teamUrl + "/channels/" + channel.Name + // attempt to fill in a message body based if the message has none + if len(strings.TrimSpace(bodyPage.Props["PostMessage"])) == 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 { + filenames[i] = strings.Replace(filepath.Base(filename), "+", " ", -1) + ext := filepath.Ext(filename) + onlyImages = onlyImages && model.IsFileExtImage(ext) + } + filenamesString := strings.Join(filenames, ", ") + + var attachmentPrefix string + if onlyImages { + attachmentPrefix = "Image" + } else { + attachmentPrefix = "File" + } + if len(post.Filenames) > 1 { + attachmentPrefix += "s" + } + + bodyPage.Props["PostMessage"] = fmt.Sprintf("%s: %s sent", attachmentPrefix, filenamesString) + } + if err := utils.SendMail(profileMap[id].Email, subjectPage.Render(), bodyPage.Render()); err != nil { l4g.Error("Failed to send mention email successfully email=%v err=%v", profileMap[id].Email, err) } @@ -636,9 +661,9 @@ func deletePost(c *Context, w http.ResponseWriter, r *http.Request) { post := result.Data.(*model.PostList).Posts[postId] - if !c.HasPermissionsToChannel(cchan, "deletePost") && !c.IsTeamAdmin(post.UserId){ - return - } + if !c.HasPermissionsToChannel(cchan, "deletePost") && !c.IsTeamAdmin(post.UserId) { + return + } if post == nil { c.SetInvalidParam("deletePost", "postId") @@ -651,7 +676,7 @@ func deletePost(c *Context, w http.ResponseWriter, r *http.Request) { return } - if post.UserId != c.Session.UserId && !strings.Contains(c.Session.Roles,model.ROLE_ADMIN) { + if post.UserId != c.Session.UserId && !strings.Contains(c.Session.Roles, model.ROLE_ADMIN) { c.Err = model.NewAppError("deletePost", "You do not have the appropriate permissions", "") c.Err.StatusCode = http.StatusForbidden return From 55c148b59a058f3845345a45c7aa33371a96e971 Mon Sep 17 00:00:00 2001 From: hmhealey Date: Mon, 20 Jul 2015 17:49:13 -0400 Subject: [PATCH 2/3] Added additional check when adding a message to an email notification for a blank post and corrected a comment --- api/post.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/api/post.go b/api/post.go index 172347a46c..1170d6a4f6 100644 --- a/api/post.go +++ b/api/post.go @@ -405,8 +405,8 @@ func fireAndForgetNotifications(post *model.Post, teamId, teamUrl string) { bodyPage.Props["PostMessage"] = model.ClearMentionTags(post.Message) bodyPage.Props["TeamLink"] = teamUrl + "/channels/" + channel.Name - // attempt to fill in a message body based if the message has none - if len(strings.TrimSpace(bodyPage.Props["PostMessage"])) == 0 { + // 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 From 156e3a94c49fa43a862d0e8de6683d9d96794447 Mon Sep 17 00:00:00 2001 From: hmhealey Date: Tue, 21 Jul 2015 09:49:05 -0400 Subject: [PATCH 3/3] Use QueryUnescape function to convert to a human readable filename in email notifications --- api/post.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/api/post.go b/api/post.go index 1170d6a4f6..65f2d59780 100644 --- a/api/post.go +++ b/api/post.go @@ -11,6 +11,7 @@ import ( "github.com/mattermost/platform/store" "github.com/mattermost/platform/utils" "net/http" + "net/url" "path/filepath" "strconv" "strings" @@ -411,7 +412,12 @@ func fireAndForgetNotifications(post *model.Post, teamId, teamUrl string) { filenames := make([]string, len(post.Filenames)) onlyImages := true for i, filename := range post.Filenames { - filenames[i] = strings.Replace(filepath.Base(filename), "+", " ", -1) + 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) + } + ext := filepath.Ext(filename) onlyImages = onlyImages && model.IsFileExtImage(ext) }