PLT-5800 Cleaned up duplicated code for adding slack attachments to posts (#5711)
Этот коммит содержится в:
коммит произвёл
George Goldberg
родитель
06930f378c
Коммит
06a7c3ba8e
@@ -915,37 +915,8 @@ func OldImportIncomingWebhookPost(post *model.Post, props model.StringInterface)
|
|||||||
if len(props) > 0 {
|
if len(props) > 0 {
|
||||||
for key, val := range props {
|
for key, val := range props {
|
||||||
if key == "attachments" {
|
if key == "attachments" {
|
||||||
if list, success := val.([]interface{}); success {
|
if attachments, success := val.([]*model.SlackAttachment); success {
|
||||||
// parse attachment links into Markdown format
|
parseSlackAttachment(post, attachments)
|
||||||
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" {
|
} else if key != "from_webhook" {
|
||||||
post.AddProp(key, val)
|
post.AddProp(key, val)
|
||||||
|
|||||||
@@ -50,7 +50,7 @@ type SlackPost struct {
|
|||||||
Comment *SlackComment `json:"comment"`
|
Comment *SlackComment `json:"comment"`
|
||||||
Upload bool `json:"upload"`
|
Upload bool `json:"upload"`
|
||||||
File *SlackFile `json:"file"`
|
File *SlackFile `json:"file"`
|
||||||
Attachments []SlackAttachment `json:"attachments"`
|
Attachments []*model.SlackAttachment `json:"attachments"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type SlackComment struct {
|
type SlackComment struct {
|
||||||
@@ -58,13 +58,6 @@ type SlackComment struct {
|
|||||||
Comment string `json:"comment"`
|
Comment string `json:"comment"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type SlackAttachment struct {
|
|
||||||
Id int `json:"id"`
|
|
||||||
Text string `json:"text"`
|
|
||||||
Pretext string `json:"pretext"`
|
|
||||||
Fields []map[string]interface{} `json:"fields"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func truncateRunes(s string, i int) string {
|
func truncateRunes(s string, i int) string {
|
||||||
runes := []rune(s)
|
runes := []rune(s)
|
||||||
if len(runes) > i {
|
if len(runes) > i {
|
||||||
@@ -284,15 +277,7 @@ func SlackAddPosts(teamId string, channel *model.Channel, posts []SlackPost, use
|
|||||||
props := make(model.StringInterface)
|
props := make(model.StringInterface)
|
||||||
props["override_username"] = sPost.BotUsername
|
props["override_username"] = sPost.BotUsername
|
||||||
if len(sPost.Attachments) > 0 {
|
if len(sPost.Attachments) > 0 {
|
||||||
var mAttachments []interface{}
|
props["attachments"] = sPost.Attachments
|
||||||
for _, attachment := range sPost.Attachments {
|
|
||||||
mAttachments = append(mAttachments, map[string]interface{}{
|
|
||||||
"text": attachment.Text,
|
|
||||||
"pretext": attachment.Pretext,
|
|
||||||
"fields": attachment.Fields,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
props["attachments"] = mAttachments
|
|
||||||
}
|
}
|
||||||
|
|
||||||
post := &model.Post{
|
post := &model.Post{
|
||||||
|
|||||||
@@ -150,37 +150,8 @@ func CreateWebhookPost(userId, teamId, channelId, text, overrideUsername, overri
|
|||||||
if len(props) > 0 {
|
if len(props) > 0 {
|
||||||
for key, val := range props {
|
for key, val := range props {
|
||||||
if key == "attachments" {
|
if key == "attachments" {
|
||||||
if list, success := val.([]interface{}); success {
|
if attachments, success := val.([]*model.SlackAttachment); success {
|
||||||
// parse attachment links into Markdown format
|
parseSlackAttachment(post, attachments)
|
||||||
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 != "override_icon_url" && key != "override_username" && key != "from_webhook" {
|
} else if key != "override_icon_url" && key != "override_username" && key != "from_webhook" {
|
||||||
post.AddProp(key, val)
|
post.AddProp(key, val)
|
||||||
@@ -452,7 +423,7 @@ func HandleIncomingWebhook(hookId string, req *model.IncomingWebhookRequest, sit
|
|||||||
webhookType := req.Type
|
webhookType := req.Type
|
||||||
|
|
||||||
// attachments is in here for slack compatibility
|
// attachments is in here for slack compatibility
|
||||||
if req.Attachments != nil {
|
if len(req.Attachments) > 0 {
|
||||||
if len(req.Props) == 0 {
|
if len(req.Props) == 0 {
|
||||||
req.Props = make(model.StringInterface)
|
req.Props = make(model.StringInterface)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,7 @@
|
|||||||
package model
|
package model
|
||||||
|
|
||||||
type SlackAttachment struct {
|
type SlackAttachment struct {
|
||||||
|
Id int64 `json:"id"`
|
||||||
Fallback string `json:"fallback"`
|
Fallback string `json:"fallback"`
|
||||||
Color string `json:"color"`
|
Color string `json:"color"`
|
||||||
Pretext string `json:"pretext"`
|
Pretext string `json:"pretext"`
|
||||||
@@ -18,7 +19,7 @@ type SlackAttachment struct {
|
|||||||
ThumbURL string `json:"thumb_url"`
|
ThumbURL string `json:"thumb_url"`
|
||||||
Footer string `json:"footer"`
|
Footer string `json:"footer"`
|
||||||
FooterIcon string `json:"footer_icon"`
|
FooterIcon string `json:"footer_icon"`
|
||||||
Timestamp int64 `json:"ts"`
|
Timestamp interface{} `json:"ts"` // This is either a string or an int64
|
||||||
}
|
}
|
||||||
|
|
||||||
type SlackAttachmentField struct {
|
type SlackAttachmentField struct {
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user