summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorCharles Birk <charles@cryptolab.net>2018-09-17 10:15:28 -0400
committerJoram Wilander <jwawilander@gmail.com>2018-09-17 10:15:28 -0400
commita755bcdde63b2f27866d49be97bb82e4cdb0e893 (patch)
tree96d89780b184a17da7b66acd1049a5c40b919301
parent7226ea7dfbf21860a5eb2fe0997a8363df4a4dd6 (diff)
downloadchat-a755bcdde63b2f27866d49be97bb82e4cdb0e893.tar.gz
chat-a755bcdde63b2f27866d49be97bb82e4cdb0e893.tar.bz2
chat-a755bcdde63b2f27866d49be97bb82e4cdb0e893.zip
[MM-11860]: Expose slack attachment parsing functions in the model package (#9351)
Refactored parseSlackAttachment functions from https://github.com/mattermost/mattermost-server/blob/master/app/post.go#L312 into model/slack_attachments.go so that plugins have access to them.
-rw-r--r--app/command.go4
-rw-r--r--app/post.go27
-rw-r--r--app/slackimport.go2
-rw-r--r--app/webhook.go2
-rw-r--r--model/slack_attachment.go25
5 files changed, 30 insertions, 30 deletions
diff --git a/app/command.go b/app/command.go
index a1902bd10..8f824982d 100644
--- a/app/command.go
+++ b/app/command.go
@@ -38,7 +38,7 @@ func GetCommandProvider(name string) CommandProvider {
}
func (a *App) CreateCommandPost(post *model.Post, teamId string, response *model.CommandResponse) (*model.Post, *model.AppError) {
- post.Message = parseSlackLinksToMarkdown(response.Text)
+ post.Message = model.ParseSlackLinksToMarkdown(response.Text)
post.CreateAt = model.GetMillis()
if strings.HasPrefix(post.Type, model.POST_SYSTEM_MESSAGE_PREFIX) {
@@ -47,7 +47,7 @@ func (a *App) CreateCommandPost(post *model.Post, teamId string, response *model
}
if response.Attachments != nil {
- parseSlackAttachment(post, response.Attachments)
+ model.ParseSlackAttachment(post, response.Attachments)
}
if response.ResponseType == model.COMMAND_RESPONSE_TYPE_IN_CHANNEL {
diff --git a/app/post.go b/app/post.go
index 114029f44..a36e9709f 100644
--- a/app/post.go
+++ b/app/post.go
@@ -13,7 +13,6 @@ import (
"net/http"
"net/url"
"path"
- "regexp"
"strings"
"github.com/dyatlov/go-opengraph/opengraph"
@@ -25,8 +24,6 @@ import (
"golang.org/x/net/html/charset"
)
-var linkWithTextRegex = regexp.MustCompile(`<([^<\|]+)\|([^>]+)>`)
-
func (a *App) CreatePostAsUser(post *model.Post) (*model.Post, *model.AppError) {
// Check that channel has not been deleted
var channel *model.Channel
@@ -308,28 +305,6 @@ func (a *App) handlePostEvents(post *model.Post, user *model.User, channel *mode
return nil
}
-// This method only parses and processes the attachments,
-// all else should be set in the post which is passed
-func parseSlackAttachment(post *model.Post, attachments []*model.SlackAttachment) {
- post.Type = model.POST_SLACK_ATTACHMENT
-
- for _, attachment := range attachments {
- attachment.Text = parseSlackLinksToMarkdown(attachment.Text)
- attachment.Pretext = parseSlackLinksToMarkdown(attachment.Pretext)
-
- for _, field := range attachment.Fields {
- if value, ok := field.Value.(string); ok {
- field.Value = parseSlackLinksToMarkdown(value)
- }
- }
- }
- post.AddProp("attachments", attachments)
-}
-
-func parseSlackLinksToMarkdown(text string) string {
- return linkWithTextRegex.ReplaceAllString(text, "[${2}](${1})")
-}
-
func (a *App) SendEphemeralPost(userId string, post *model.Post) *model.Post {
post.Type = model.POST_EPHEMERAL
@@ -931,7 +906,7 @@ func (a *App) DoPostAction(postId, actionId, userId, selectedOption string) *mod
if response.EphemeralText != "" {
ephemeralPost := &model.Post{}
- ephemeralPost.Message = parseSlackLinksToMarkdown(response.EphemeralText)
+ ephemeralPost.Message = model.ParseSlackLinksToMarkdown(response.EphemeralText)
ephemeralPost.ChannelId = post.ChannelId
ephemeralPost.RootId = post.RootId
if ephemeralPost.RootId == "" {
diff --git a/app/slackimport.go b/app/slackimport.go
index 5f1f73632..d3a91815d 100644
--- a/app/slackimport.go
+++ b/app/slackimport.go
@@ -796,7 +796,7 @@ func (a *App) OldImportIncomingWebhookPost(post *model.Post, props model.StringI
for key, val := range props {
if key == "attachments" {
if attachments, success := val.([]*model.SlackAttachment); success {
- parseSlackAttachment(post, attachments)
+ model.ParseSlackAttachment(post, attachments)
}
} else if key != "from_webhook" {
post.AddProp(key, val)
diff --git a/app/webhook.go b/app/webhook.go
index f0264c0c6..2557a99e6 100644
--- a/app/webhook.go
+++ b/app/webhook.go
@@ -265,7 +265,7 @@ func (a *App) CreateWebhookPost(userId string, channel *model.Channel, text, ove
for key, val := range props {
if key == "attachments" {
if attachments, success := val.([]*model.SlackAttachment); success {
- parseSlackAttachment(post, attachments)
+ model.ParseSlackAttachment(post, attachments)
}
} else if key != "override_icon_url" && key != "override_username" && key != "from_webhook" {
post.AddProp(key, val)
diff --git a/model/slack_attachment.go b/model/slack_attachment.go
index 197d3f0f9..827bf35b3 100644
--- a/model/slack_attachment.go
+++ b/model/slack_attachment.go
@@ -5,8 +5,11 @@ package model
import (
"fmt"
+ "regexp"
)
+var linkWithTextRegex = regexp.MustCompile(`<([^<\|]+)\|([^>]+)>`)
+
type SlackAttachment struct {
Id int64 `json:"id"`
Fallback string `json:"fallback"`
@@ -57,3 +60,25 @@ func StringifySlackFieldValue(a []*SlackAttachment) []*SlackAttachment {
}
return nonNilAttachments
}
+
+// This method only parses and processes the attachments,
+// all else should be set in the post which is passed
+func ParseSlackAttachment(post *Post, attachments []*SlackAttachment) {
+ post.Type = POST_SLACK_ATTACHMENT
+
+ for _, attachment := range attachments {
+ attachment.Text = ParseSlackLinksToMarkdown(attachment.Text)
+ attachment.Pretext = ParseSlackLinksToMarkdown(attachment.Pretext)
+
+ for _, field := range attachment.Fields {
+ if value, ok := field.Value.(string); ok {
+ field.Value = ParseSlackLinksToMarkdown(value)
+ }
+ }
+ }
+ post.AddProp("attachments", attachments)
+}
+
+func ParseSlackLinksToMarkdown(text string) string {
+ return linkWithTextRegex.ReplaceAllString(text, "[${2}](${1})")
+}