summaryrefslogtreecommitdiffstats
path: root/model
diff options
context:
space:
mode:
authorJoram Wilander <jwawilander@gmail.com>2018-01-23 11:04:44 -0500
committerChristopher Speller <crspeller@gmail.com>2018-01-23 08:04:44 -0800
commit4f4a765e7d0bbfdfecc0c52ae4be35f8d3b737ca (patch)
tree063aa55db9299f09d4176b201db658f1d7acced5 /model
parent599991ea731953f772824ce3ed1e591246aa004f (diff)
downloadchat-4f4a765e7d0bbfdfecc0c52ae4be35f8d3b737ca.tar.gz
chat-4f4a765e7d0bbfdfecc0c52ae4be35f8d3b737ca.tar.bz2
chat-4f4a765e7d0bbfdfecc0c52ae4be35f8d3b737ca.zip
ABC-90 Add POST /emoji/search and GET /emoji/autocomplete API endpoints (#8125)
* Add POST /emoji/search and GET /emoji/autocomplete API endpoints * Add constant to be clearer
Diffstat (limited to 'model')
-rw-r--r--model/client4.go21
-rw-r--r--model/emoji_search.go34
-rw-r--r--model/emoji_search_test.go19
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")
+ }
+}