summaryrefslogtreecommitdiffstats
path: root/model
diff options
context:
space:
mode:
authorChris <ccbrown112@gmail.com>2017-08-29 16:14:59 -0500
committerGitHub <noreply@github.com>2017-08-29 16:14:59 -0500
commit213a072b38d29d3c3ec8e150584685b1144a7d6a (patch)
tree1da10a494e49914b0f6641db79e7dcf8ad3886f6 /model
parent59798c137584a0b7e008ec713b489929dd83a690 (diff)
downloadchat-213a072b38d29d3c3ec8e150584685b1144a7d6a.tar.gz
chat-213a072b38d29d3c3ec8e150584685b1144a7d6a.tar.bz2
chat-213a072b38d29d3c3ec8e150584685b1144a7d6a.zip
PLT-6403: Interactive messages (#7274)
* wip * finish first pass * requested changes * add DoPostAction to Client4
Diffstat (limited to 'model')
-rw-r--r--model/client4.go10
-rw-r--r--model/post.go85
-rw-r--r--model/post_list.go14
-rw-r--r--model/slack_attachment.go1
4 files changed, 108 insertions, 2 deletions
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(&copy)
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(&copy)
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 {