diff options
Diffstat (limited to 'model')
-rw-r--r-- | model/client4.go | 21 | ||||
-rw-r--r-- | model/emoji_search.go | 34 | ||||
-rw-r--r-- | model/emoji_search_test.go | 19 |
3 files changed, 74 insertions, 0 deletions
diff --git a/model/client4.go b/model/client4.go index c44855993..151b5a491 100644 --- a/model/client4.go +++ b/model/client4.go @@ -3070,6 +3070,27 @@ func (c *Client4) GetEmojiImage(emojiId string) ([]byte, *Response) { } } +// SearchEmoji returns a list of emoji matching some search criteria. +func (c *Client4) SearchEmoji(search *EmojiSearch) ([]*Emoji, *Response) { + if r, err := c.DoApiPost(c.GetEmojisRoute()+"/search", search.ToJson()); err != nil { + return nil, BuildErrorResponse(r, err) + } else { + defer closeBody(r) + return EmojiListFromJson(r.Body), BuildResponse(r) + } +} + +// AutocompleteEmoji returns a list of emoji starting with or matching name. +func (c *Client4) AutocompleteEmoji(name string, etag string) ([]*Emoji, *Response) { + query := fmt.Sprintf("?name=%v", name) + if r, err := c.DoApiGet(c.GetEmojisRoute()+"/autocomplete"+query, ""); err != nil { + return nil, BuildErrorResponse(r, err) + } else { + defer closeBody(r) + return EmojiListFromJson(r.Body), BuildResponse(r) + } +} + // Reaction Section // SaveReaction saves an emoji reaction for a post. Returns the saved reaction if successful, otherwise an error will be returned. diff --git a/model/emoji_search.go b/model/emoji_search.go new file mode 100644 index 000000000..31931170e --- /dev/null +++ b/model/emoji_search.go @@ -0,0 +1,34 @@ +// Copyright (c) 2018-present Mattermost, Inc. All Rights Reserved. +// See License.txt for license information. + +package model + +import ( + "encoding/json" + "io" +) + +type EmojiSearch struct { + Term string `json:"term"` + PrefixOnly bool `json:"prefix_only"` +} + +func (es *EmojiSearch) ToJson() string { + b, err := json.Marshal(es) + if err != nil { + return "" + } else { + return string(b) + } +} + +func EmojiSearchFromJson(data io.Reader) *EmojiSearch { + decoder := json.NewDecoder(data) + var es EmojiSearch + err := decoder.Decode(&es) + if err == nil { + return &es + } else { + return nil + } +} diff --git a/model/emoji_search_test.go b/model/emoji_search_test.go new file mode 100644 index 000000000..6e3b01213 --- /dev/null +++ b/model/emoji_search_test.go @@ -0,0 +1,19 @@ +// Copyright (c) 2018-present Mattermost, Inc. All Rights Reserved. +// See License.txt for license information. + +package model + +import ( + "strings" + "testing" +) + +func TestEmojiSearchJson(t *testing.T) { + emojiSearch := EmojiSearch{Term: NewId()} + json := emojiSearch.ToJson() + remojiSearch := EmojiSearchFromJson(strings.NewReader(json)) + + if emojiSearch.Term != remojiSearch.Term { + t.Fatal("Terms do not match") + } +} |