diff options
Diffstat (limited to 'api/import.go')
-rw-r--r-- | api/import.go | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/api/import.go b/api/import.go index 51c2ef20f..3ac6a9ce9 100644 --- a/api/import.go +++ b/api/import.go @@ -6,6 +6,7 @@ package api import ( "bytes" "io" + "regexp" l4g "github.com/alecthomas/log4go" "github.com/mattermost/platform/model" @@ -80,3 +81,57 @@ func ImportFile(file io.Reader, teamId string, channelId string, userId string, return fileInfo, nil } + +func ImportIncomingWebhookPost(post *model.Post, props model.StringInterface) { + linkWithTextRegex := regexp.MustCompile(`<([^<\|]+)\|([^>]+)>`) + post.Message = linkWithTextRegex.ReplaceAllString(post.Message, "[${2}](${1})") + + post.AddProp("from_webhook", "true") + + if _, ok := props["override_username"]; !ok { + post.AddProp("override_username", model.DEFAULT_WEBHOOK_USERNAME) + } + + if len(props) > 0 { + for key, val := range props { + if key == "attachments" { + if list, success := val.([]interface{}); success { + // parse attachment links into Markdown format + for i, aInt := range list { + attachment := aInt.(map[string]interface{}) + if aText, ok := attachment["text"].(string); ok { + aText = linkWithTextRegex.ReplaceAllString(aText, "[${2}](${1})") + attachment["text"] = aText + list[i] = attachment + } + if aText, ok := attachment["pretext"].(string); ok { + aText = linkWithTextRegex.ReplaceAllString(aText, "[${2}](${1})") + attachment["pretext"] = aText + list[i] = attachment + } + if fVal, ok := attachment["fields"]; ok { + if fields, ok := fVal.([]interface{}); ok { + // parse attachment field links into Markdown format + for j, fInt := range fields { + field := fInt.(map[string]interface{}) + if fValue, ok := field["value"].(string); ok { + fValue = linkWithTextRegex.ReplaceAllString(fValue, "[${2}](${1})") + field["value"] = fValue + fields[j] = field + } + } + attachment["fields"] = fields + list[i] = attachment + } + } + } + post.AddProp(key, list) + } + } else if key != "from_webhook" { + post.AddProp(key, val) + } + } + } + + ImportPost(post) +} |