diff options
Diffstat (limited to 'model')
-rw-r--r-- | model/channel_search.go | 35 | ||||
-rw-r--r-- | model/channel_search_test.go | 19 | ||||
-rw-r--r-- | model/client.go | 38 | ||||
-rw-r--r-- | model/user_autocomplete.go (renamed from model/autocomplete.go) | 0 |
4 files changed, 92 insertions, 0 deletions
diff --git a/model/channel_search.go b/model/channel_search.go new file mode 100644 index 000000000..2c041503d --- /dev/null +++ b/model/channel_search.go @@ -0,0 +1,35 @@ +// Copyright (c) 2016 Mattermost, Inc. All Rights Reserved. +// See License.txt for license information. + +package model + +import ( + "encoding/json" + "io" +) + +type ChannelSearch struct { + Term string `json:"term"` +} + +// ToJson convert a Channel to a json string +func (c *ChannelSearch) ToJson() string { + b, err := json.Marshal(c) + if err != nil { + return "" + } else { + return string(b) + } +} + +// ChannelSearchFromJson will decode the input and return a Channel +func ChannelSearchFromJson(data io.Reader) *ChannelSearch { + decoder := json.NewDecoder(data) + var cs ChannelSearch + err := decoder.Decode(&cs) + if err == nil { + return &cs + } else { + return nil + } +} diff --git a/model/channel_search_test.go b/model/channel_search_test.go new file mode 100644 index 000000000..f7f6d66f7 --- /dev/null +++ b/model/channel_search_test.go @@ -0,0 +1,19 @@ +// Copyright (c) 2016 Mattermost, Inc. All Rights Reserved. +// See License.txt for license information. + +package model + +import ( + "strings" + "testing" +) + +func TestChannelSearchJson(t *testing.T) { + channelSearch := ChannelSearch{Term: NewId()} + json := channelSearch.ToJson() + rchannelSearch := ChannelSearchFromJson(strings.NewReader(json)) + + if channelSearch.Term != rchannelSearch.Term { + t.Fatal("Terms do not match") + } +} diff --git a/model/client.go b/model/client.go index 32532508f..30eba99f8 100644 --- a/model/client.go +++ b/model/client.go @@ -1164,6 +1164,7 @@ func (c *Client) GetChannel(id, etag string) (*Result, *AppError) { } } +// SCHEDULED FOR DEPRECATION IN 3.7 - use GetMoreChannelsPage instead func (c *Client) GetMoreChannels(etag string) (*Result, *AppError) { if r, err := c.DoApiGet(c.GetTeamRoute()+"/channels/more", "", etag); err != nil { return nil, err @@ -1174,6 +1175,43 @@ func (c *Client) GetMoreChannels(etag string) (*Result, *AppError) { } } +// GetMoreChannelsPage will return a page of open channels the user is not in based on +// the provided offset and limit. Must be authenticated. +func (c *Client) GetMoreChannelsPage(offset int, limit int) (*Result, *AppError) { + if r, err := c.DoApiGet(fmt.Sprintf(c.GetTeamRoute()+"/channels/more/%v/%v", offset, limit), "", ""); err != nil { + return nil, err + } else { + defer closeBody(r) + return &Result{r.Header.Get(HEADER_REQUEST_ID), + r.Header.Get(HEADER_ETAG_SERVER), ChannelListFromJson(r.Body)}, nil + } +} + +// SearchMoreChannels will return a list of open channels the user is not in, that matches +// the search criteria provided. Must be authenticated. +func (c *Client) SearchMoreChannels(channelSearch ChannelSearch) (*Result, *AppError) { + if r, err := c.DoApiPost(c.GetTeamRoute()+"/channels/more/search", channelSearch.ToJson()); err != nil { + return nil, err + } else { + defer closeBody(r) + return &Result{r.Header.Get(HEADER_REQUEST_ID), + r.Header.Get(HEADER_ETAG_SERVER), ChannelListFromJson(r.Body)}, nil + } +} + +// AutocompleteChannels will return a list of open channels that match the provided +// string. Must be authenticated. +func (c *Client) AutocompleteChannels(term string) (*Result, *AppError) { + url := fmt.Sprintf("%s/channels/autocomplete?term=%s", c.GetTeamRoute(), url.QueryEscape(term)) + if r, err := c.DoApiGet(url, "", ""); err != nil { + return nil, err + } else { + defer closeBody(r) + return &Result{r.Header.Get(HEADER_REQUEST_ID), + r.Header.Get(HEADER_ETAG_SERVER), ChannelListFromJson(r.Body)}, nil + } +} + func (c *Client) GetChannelCounts(etag string) (*Result, *AppError) { if r, err := c.DoApiGet(c.GetTeamRoute()+"/channels/counts", "", etag); err != nil { return nil, err diff --git a/model/autocomplete.go b/model/user_autocomplete.go index b7449a792..b7449a792 100644 --- a/model/autocomplete.go +++ b/model/user_autocomplete.go |