From b085bc2d56bdc98101b8cb50848aee248d42af28 Mon Sep 17 00:00:00 2001 From: Florian Orben Date: Thu, 5 Nov 2015 23:32:44 +0100 Subject: PLT-857: Support for Incoming Webhooks - Try #2 --- model/incoming_webhook.go | 11 +++++++---- model/post.go | 46 ++++++++++++++++++++++++---------------------- model/utils.go | 20 ++++++++++++++++++++ 3 files changed, 51 insertions(+), 26 deletions(-) (limited to 'model') diff --git a/model/incoming_webhook.go b/model/incoming_webhook.go index be1984244..8ead0da9f 100644 --- a/model/incoming_webhook.go +++ b/model/incoming_webhook.go @@ -24,10 +24,13 @@ type IncomingWebhook struct { } type IncomingWebhookRequest struct { - Text string `json:"text"` - Username string `json:"username"` - IconURL string `json:"icon_url"` - ChannelName string `json:"channel"` + Text string `json:"text"` + Username string `json:"username"` + IconURL string `json:"icon_url"` + ChannelName string `json:"channel"` + Props StringInterface `json:"props"` + Attachments interface{} `json:"attachments"` + Type string `json:"type"` } func (o *IncomingWebhook) ToJson() string { diff --git a/model/post.go b/model/post.go index e0074b348..248d40321 100644 --- a/model/post.go +++ b/model/post.go @@ -10,27 +10,28 @@ import ( ) const ( - POST_DEFAULT = "" - POST_JOIN_LEAVE = "join_leave" + POST_DEFAULT = "" + POST_SLACK_ATTACHMENT = "slack_attachment" + POST_JOIN_LEAVE = "join_leave" ) type Post struct { - Id string `json:"id"` - CreateAt int64 `json:"create_at"` - UpdateAt int64 `json:"update_at"` - DeleteAt int64 `json:"delete_at"` - UserId string `json:"user_id"` - ChannelId string `json:"channel_id"` - RootId string `json:"root_id"` - ParentId string `json:"parent_id"` - OriginalId string `json:"original_id"` - Message string `json:"message"` - ImgCount int64 `json:"img_count"` - Type string `json:"type"` - Props StringMap `json:"props"` - Hashtags string `json:"hashtags"` - Filenames StringArray `json:"filenames"` - PendingPostId string `json:"pending_post_id" db:"-"` + Id string `json:"id"` + CreateAt int64 `json:"create_at"` + UpdateAt int64 `json:"update_at"` + DeleteAt int64 `json:"delete_at"` + UserId string `json:"user_id"` + ChannelId string `json:"channel_id"` + RootId string `json:"root_id"` + ParentId string `json:"parent_id"` + OriginalId string `json:"original_id"` + Message string `json:"message"` + ImgCount int64 `json:"img_count"` + Type string `json:"type"` + Props StringInterface `json:"props"` + Hashtags string `json:"hashtags"` + Filenames StringArray `json:"filenames"` + PendingPostId string `json:"pending_post_id" db:"-"` } func (o *Post) ToJson() string { @@ -103,7 +104,8 @@ func (o *Post) IsValid() *AppError { return NewAppError("Post.IsValid", "Invalid hashtags", "id="+o.Id) } - if !(o.Type == POST_DEFAULT || o.Type == POST_JOIN_LEAVE) { + // should be removed once more message types are supported + if !(o.Type == POST_DEFAULT || o.Type == POST_JOIN_LEAVE || o.Type == POST_SLACK_ATTACHMENT) { return NewAppError("Post.IsValid", "Invalid type", "id="+o.Type) } @@ -128,7 +130,7 @@ func (o *Post) PreSave() { o.UpdateAt = o.CreateAt if o.Props == nil { - o.Props = make(map[string]string) + o.Props = make(map[string]interface{}) } if o.Filenames == nil { @@ -138,14 +140,14 @@ func (o *Post) PreSave() { func (o *Post) MakeNonNil() { if o.Props == nil { - o.Props = make(map[string]string) + o.Props = make(map[string]interface{}) } if o.Filenames == nil { o.Filenames = []string{} } } -func (o *Post) AddProp(key string, value string) { +func (o *Post) AddProp(key string, value interface{}) { o.MakeNonNil() diff --git a/model/utils.go b/model/utils.go index 681ade870..1e71836c1 100644 --- a/model/utils.go +++ b/model/utils.go @@ -17,6 +17,7 @@ import ( "time" ) +type StringInterface map[string]interface{} type StringMap map[string]string type StringArray []string type EncryptStringMap map[string]string @@ -125,6 +126,25 @@ func ArrayFromJson(data io.Reader) []string { } } +func StringInterfaceToJson(objmap map[string]interface{}) string { + if b, err := json.Marshal(objmap); err != nil { + return "" + } else { + return string(b) + } +} + +func StringInterfaceFromJson(data io.Reader) map[string]interface{} { + decoder := json.NewDecoder(data) + + var objmap map[string]interface{} + if err := decoder.Decode(&objmap); err != nil { + return make(map[string]interface{}) + } else { + return objmap + } +} + func IsLower(s string) bool { if strings.ToLower(s) == s { return true -- cgit v1.2.3-1-g7c22