diff options
author | Joram Wilander <jwawilander@gmail.com> | 2016-11-24 09:35:09 -0500 |
---|---|---|
committer | Harrison Healey <harrisonmhealey@gmail.com> | 2016-11-24 09:35:09 -0500 |
commit | 981ea33b8e10456bc279f36235c814305d01b243 (patch) | |
tree | 00fb6119d9ef16f60d4c0dbdaad1bd6dfbc347ed /api/channel.go | |
parent | c96ecae6da31aceabf29586cde872876b81d11d9 (diff) | |
download | chat-981ea33b8e10456bc279f36235c814305d01b243.tar.gz chat-981ea33b8e10456bc279f36235c814305d01b243.tar.bz2 chat-981ea33b8e10456bc279f36235c814305d01b243.zip |
PLT-4403 Add server-based channel autocomplete, search and paging (#4585)
* Add more channel paging API
* Add channel paging support to client
* Add DB channel search functions
* Add API for searching more channels
* Add more channel search functionality to client
* Add API for autocompleting channels
* Add channel autocomplete functionality to the client
* Move to be deprecated APIs to their own file
* Final clean-up
* Fixes related to feedback
* Localization changes
* Add unit as suffix to timeout constants
Diffstat (limited to 'api/channel.go')
-rw-r--r-- | api/channel.go | 73 |
1 files changed, 68 insertions, 5 deletions
diff --git a/api/channel.go b/api/channel.go index 9ec556fe6..0ffe9a668 100644 --- a/api/channel.go +++ b/api/channel.go @@ -6,6 +6,7 @@ package api import ( "fmt" "net/http" + "strconv" "strings" l4g "github.com/alecthomas/log4go" @@ -19,7 +20,8 @@ func InitChannel() { l4g.Debug(utils.T("api.channel.init.debug")) BaseRoutes.Channels.Handle("/", ApiUserRequired(getChannels)).Methods("GET") - BaseRoutes.Channels.Handle("/more", ApiUserRequired(getMoreChannels)).Methods("GET") + BaseRoutes.Channels.Handle("/more/{offset:[0-9]+}/{limit:[0-9]+}", ApiUserRequired(getMoreChannelsPage)).Methods("GET") + BaseRoutes.Channels.Handle("/more/search", ApiUserRequired(searchMoreChannels)).Methods("POST") BaseRoutes.Channels.Handle("/counts", ApiUserRequired(getChannelCounts)).Methods("GET") BaseRoutes.Channels.Handle("/members", ApiUserRequired(getMyChannelMembers)).Methods("GET") BaseRoutes.Channels.Handle("/create", ApiUserRequired(createChannel)).Methods("POST") @@ -28,6 +30,7 @@ func InitChannel() { BaseRoutes.Channels.Handle("/update_header", ApiUserRequired(updateChannelHeader)).Methods("POST") BaseRoutes.Channels.Handle("/update_purpose", ApiUserRequired(updateChannelPurpose)).Methods("POST") BaseRoutes.Channels.Handle("/update_notify_props", ApiUserRequired(updateNotifyProps)).Methods("POST") + BaseRoutes.Channels.Handle("/autocomplete", ApiUserRequired(autocompleteChannels)).Methods("GET") BaseRoutes.NeedChannelName.Handle("/join", ApiUserRequired(join)).Methods("POST") @@ -41,6 +44,7 @@ func InitChannel() { BaseRoutes.NeedChannel.Handle("/remove", ApiUserRequired(removeMember)).Methods("POST") BaseRoutes.NeedChannel.Handle("/update_last_viewed_at", ApiUserRequired(updateLastViewedAt)).Methods("POST") BaseRoutes.NeedChannel.Handle("/set_last_viewed_at", ApiUserRequired(setLastViewedAt)).Methods("POST") + } func createChannel(c *Context, w http.ResponseWriter, r *http.Request) { @@ -416,18 +420,29 @@ func getChannels(c *Context, w http.ResponseWriter, r *http.Request) { } } -func getMoreChannels(c *Context, w http.ResponseWriter, r *http.Request) { +func getMoreChannelsPage(c *Context, w http.ResponseWriter, r *http.Request) { + params := mux.Vars(r) + + offset, err := strconv.Atoi(params["offset"]) + if err != nil { + c.SetInvalidParam("getProfiles", "offset") + return + } + + limit, err := strconv.Atoi(params["limit"]) + if err != nil { + c.SetInvalidParam("getProfiles", "limit") + return + } // user is already in the team if !HasPermissionToTeamContext(c, c.TeamId, model.PERMISSION_LIST_TEAM_CHANNELS) { return } - if result := <-Srv.Store.Channel().GetMoreChannels(c.TeamId, c.Session.UserId); result.Err != nil { + if result := <-Srv.Store.Channel().GetMoreChannels(c.TeamId, c.Session.UserId, offset, limit); result.Err != nil { c.Err = result.Err return - } else if HandleEtag(result.Data.(*model.ChannelList).Etag(), w, r) { - return } else { data := result.Data.(*model.ChannelList) w.Header().Set(model.HEADER_ETAG_SERVER, data.Etag()) @@ -1182,3 +1197,51 @@ func updateNotifyProps(c *Context, w http.ResponseWriter, r *http.Request) { } } + +func searchMoreChannels(c *Context, w http.ResponseWriter, r *http.Request) { + props := model.ChannelSearchFromJson(r.Body) + if props == nil { + c.SetInvalidParam("searchMoreChannels", "") + return + } + + if c.Session.GetTeamByTeamId(c.TeamId) == nil { + if !HasPermissionToContext(c, model.PERMISSION_MANAGE_SYSTEM) { + return + } + } + + if len(props.Term) == 0 { + c.SetInvalidParam("searchMoreChannels", "term") + return + } + + if result := <-Srv.Store.Channel().SearchMore(c.Session.UserId, c.TeamId, props.Term); result.Err != nil { + c.Err = result.Err + return + } else { + channels := result.Data.(*model.ChannelList) + w.Write([]byte(channels.ToJson())) + } +} + +func autocompleteChannels(c *Context, w http.ResponseWriter, r *http.Request) { + term := r.URL.Query().Get("term") + + if c.Session.GetTeamByTeamId(c.TeamId) == nil { + if !HasPermissionToContext(c, model.PERMISSION_MANAGE_SYSTEM) { + return + } + } + + var channels *model.ChannelList + + if result := <-Srv.Store.Channel().SearchInTeam(c.TeamId, term); result.Err != nil { + c.Err = result.Err + return + } else { + channels = result.Data.(*model.ChannelList) + } + + w.Write([]byte(channels.ToJson())) +} |