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
Этот коммит содержится в:
Joram Wilander
2018-01-23 11:04:44 -05:00
коммит произвёл Christopher Speller
родитель 599991ea73
Коммит 4f4a765e7d
11 изменённых файлов: 381 добавлений и 3 удалений

Просмотреть файл

@@ -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.

34
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
}
}

19
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")
}
}