From 213a072b38d29d3c3ec8e150584685b1144a7d6a Mon Sep 17 00:00:00 2001 From: Chris Date: Tue, 29 Aug 2017 16:14:59 -0500 Subject: PLT-6403: Interactive messages (#7274) * wip * finish first pass * requested changes * add DoPostAction to Client4 --- model/client4.go | 10 ++++++ model/post.go | 85 ++++++++++++++++++++++++++++++++++++++++++++++- model/post_list.go | 14 +++++++- model/slack_attachment.go | 1 + 4 files changed, 108 insertions(+), 2 deletions(-) (limited to 'model') diff --git a/model/client4.go b/model/client4.go index 0f7578539..26ea6ee03 100644 --- a/model/client4.go +++ b/model/client4.go @@ -1803,6 +1803,16 @@ func (c *Client4) SearchPosts(teamId string, terms string, isOrSearch bool) (*Po } } +// DoPostAction performs a post action. +func (c *Client4) DoPostAction(postId, actionId string) (bool, *Response) { + if r, err := c.DoApiPost(c.GetPostRoute(postId)+"/actions/"+actionId, ""); err != nil { + return false, BuildErrorResponse(r, err) + } else { + defer closeBody(r) + return CheckStatusOK(r), BuildResponse(r) + } +} + // File Section // UploadFile will upload a file to a channel, to be later attached to a post. diff --git a/model/post.go b/model/post.go index 55e6f591d..1c2e9b937 100644 --- a/model/post.go +++ b/model/post.go @@ -68,8 +68,31 @@ type PostForIndexing struct { ParentCreateAt *int64 `json:"parent_create_at"` } +type PostAction struct { + Id string `json:"id"` + Name string `json:"name"` + Integration *PostActionIntegration `json:"integration,omitempty"` +} + +type PostActionIntegration struct { + URL string `json:"url,omitempty"` + Context StringInterface `json:"context,omitempty"` +} + +type PostActionIntegrationRequest struct { + UserId string `json:"user_id"` + Context StringInterface `json:"context,omitempty"` +} + +type PostActionIntegrationResponse struct { + Update *Post `json:"update"` + EphemeralText string `json:"ephemeral_text"` +} + func (o *Post) ToJson() string { - b, err := json.Marshal(o) + copy := *o + copy.StripActionIntegrations() + b, err := json.Marshal(©) if err != nil { return "" } else { @@ -179,6 +202,16 @@ func (o *Post) PreSave() { o.Props = make(map[string]interface{}) } + if attachments, ok := o.Props["attachments"].([]*SlackAttachment); ok { + for _, attachment := range attachments { + for _, action := range attachment.Actions { + if action.Id == "" { + action.Id = NewId() + } + } + } + } + if o.Filenames == nil { o.Filenames = []string{} } @@ -246,3 +279,53 @@ func PostPatchFromJson(data io.Reader) *PostPatch { return &post } + +func (r *PostActionIntegrationRequest) ToJson() string { + b, err := json.Marshal(r) + if err != nil { + return "" + } else { + return string(b) + } +} + +func (o *Post) Attachments() []*SlackAttachment { + if attachments, ok := o.Props["attachments"].([]*SlackAttachment); ok { + return attachments + } + var ret []*SlackAttachment + if attachments, ok := o.Props["attachments"].([]interface{}); ok { + for _, attachment := range attachments { + if enc, err := json.Marshal(attachment); err == nil { + var decoded SlackAttachment + if json.Unmarshal(enc, &decoded) == nil { + ret = append(ret, &decoded) + } + } + } + } + return ret +} + +func (o *Post) StripActionIntegrations() { + attachments := o.Attachments() + if o.Props["attachments"] != nil { + o.Props["attachments"] = attachments + } + for _, attachment := range attachments { + for _, action := range attachment.Actions { + action.Integration = nil + } + } +} + +func (o *Post) GetAction(id string) *PostAction { + for _, attachment := range o.Attachments() { + for _, action := range attachment.Actions { + if action.Id == id { + return action + } + } + } + return nil +} diff --git a/model/post_list.go b/model/post_list.go index 63f6d6825..b3caadafd 100644 --- a/model/post_list.go +++ b/model/post_list.go @@ -20,8 +20,20 @@ func NewPostList() *PostList { } } +func (o *PostList) StripActionIntegrations() { + posts := o.Posts + o.Posts = make(map[string]*Post) + for id, post := range posts { + pcopy := *post + pcopy.StripActionIntegrations() + o.Posts[id] = &pcopy + } +} + func (o *PostList) ToJson() string { - b, err := json.Marshal(o) + copy := *o + copy.StripActionIntegrations() + b, err := json.Marshal(©) if err != nil { return "" } else { diff --git a/model/slack_attachment.go b/model/slack_attachment.go index 855838214..fe3958316 100644 --- a/model/slack_attachment.go +++ b/model/slack_attachment.go @@ -25,6 +25,7 @@ type SlackAttachment struct { Footer string `json:"footer"` FooterIcon string `json:"footer_icon"` Timestamp interface{} `json:"ts"` // This is either a string or an int64 + Actions []*PostAction `json:"actions,omitempty"` } type SlackAttachmentField struct { -- cgit v1.2.3-1-g7c22