mirror of
https://github.com/mattermost/mattermost.git
synced 2025-02-25 18:55:24 -06:00
[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.
This commit is contained in:
committed by
Joram Wilander
parent
7226ea7dfb
commit
a755bcdde6
@@ -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 {
|
||||
|
||||
27
app/post.go
27
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 == "" {
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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})")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user