From e7710cf1d270eefd4d4f42bfe2017baec496b34a Mon Sep 17 00:00:00 2001 From: George Goldberg Date: Tue, 29 Nov 2016 19:07:07 +0000 Subject: PLT-4847 Fix formatting of links imported from Slack. (#4674) This fixes mailto: links built from email addresses posted to Slack as described in the ticket, but also fixes any other type of URL that has been auto-linked in the Slack messages. --- api/slackimport.go | 14 ++++++++++++++ api/slackimport_test.go | 21 +++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/api/slackimport.go b/api/slackimport.go index 15f5f43f4..1225e7833 100644 --- a/api/slackimport.go +++ b/api/slackimport.go @@ -494,6 +494,19 @@ func SlackConvertChannelMentions(channels []SlackChannel, posts map[string][]Sla return posts } +func SlackConvertPostsMarkup(posts map[string][]SlackPost) map[string][]SlackPost { + // Convert URLs in Slack's format to Markdown format. + regex := regexp.MustCompile(`<([^|<>]+)\|([^|<>]+)>`) + + for channelName, channelPosts := range posts { + for postIdx, post := range channelPosts { + posts[channelName][postIdx].Text = regex.ReplaceAllString(post.Text, "[$2]($1)") + } + } + + return posts +} + func SlackImport(fileData multipart.File, fileSize int64, teamID string) (*model.AppError, *bytes.Buffer) { // Create log file log := bytes.NewBufferString(utils.T("api.slackimport.slack_import.log")) @@ -536,6 +549,7 @@ func SlackImport(fileData multipart.File, fileSize int64, teamID string) (*model posts = SlackConvertUserMentions(users, posts) posts = SlackConvertChannelMentions(channels, posts) + posts = SlackConvertPostsMarkup(posts) addedUsers := SlackAddUsers(teamID, users, log) botUser := SlackAddBotUser(teamID, log) diff --git a/api/slackimport_test.go b/api/slackimport_test.go index d78424ac0..efe6e635f 100644 --- a/api/slackimport_test.go +++ b/api/slackimport_test.go @@ -217,3 +217,24 @@ func TestSlackSanitiseChannelProperties(t *testing.T) { t.Fatalf("Unexpected alterations to the channel properties: %v", c2s.Header) } } + +func TestSlackConvertPostsMarkup(t *testing.T) { + input := make(map[string][]SlackPost) + input["test"] = []SlackPost{ + { + Text: "This message contains a link to .", + }, + { + Text: "This message contains a mailto link to in it.", + }, + } + + output := SlackConvertPostsMarkup(input) + + if output["test"][0].Text != "This message contains a link to [Google](https://google.com)." { + t.Fatalf("Unexpected message after markup translation: %v", output["test"][0].Text) + } + if output["test"][1].Text != "This message contains a mailto link to [me@example.com](mailto:me@example.com) in it." { + t.Fatalf("Unexpected message after markup translation: %v", output["test"][0].Text) + } +} -- cgit v1.2.3-1-g7c22