diff options
author | Carlos Tadeu Panato Junior <ctadeu@gmail.com> | 2016-12-12 06:30:37 +0100 |
---|---|---|
committer | Corey Hulen <corey@hulen.com> | 2016-12-11 21:30:37 -0800 |
commit | f0d71d87899967335210b9130a7e2b8d180bef46 (patch) | |
tree | d2941b8870e87677a84821dd32454a28594c889b /api/channel.go | |
parent | b5fcfd608c0e9ef764cace7328653e4d4c47a061 (diff) | |
download | chat-f0d71d87899967335210b9130a7e2b8d180bef46.tar.gz chat-f0d71d87899967335210b9130a7e2b8d180bef46.tar.bz2 chat-f0d71d87899967335210b9130a7e2b8d180bef46.zip |
Add API call to get a channel by its name (#4700)
* add api for getByChannelName
* add tests
* fix test
* rename and tests
* check for permissions and test
Diffstat (limited to 'api/channel.go')
-rw-r--r-- | api/channel.go | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/api/channel.go b/api/channel.go index 0a505c397..3e4398e72 100644 --- a/api/channel.go +++ b/api/channel.go @@ -31,6 +31,7 @@ func InitChannel() { 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.Channels.Handle("/name/{channel_name:[A-Za-z0-9_-]+}", ApiUserRequired(getChannelByName)).Methods("GET") BaseRoutes.NeedChannelName.Handle("/join", ApiUserRequired(join)).Methods("POST") @@ -953,6 +954,37 @@ func getChannel(c *Context, w http.ResponseWriter, r *http.Request) { } +func getChannelByName(c *Context, w http.ResponseWriter, r *http.Request) { + params := mux.Vars(r) + channelname := params["channel_name"] + + cchan := Srv.Store.Channel().GetByName(c.TeamId, channelname) + + if cresult := <-cchan; cresult.Err != nil { + c.Err = cresult.Err + return + } else { + data := &model.Channel{} + data = cresult.Data.(*model.Channel) + + if !HasPermissionToChannelContext(c, data.Id, model.PERMISSION_READ_CHANNEL) { + return + } + + if data.TeamId != c.TeamId && data.Type != model.CHANNEL_DIRECT { + c.Err = model.NewLocAppError("getChannel", "api.channel.get_channel.wrong_team.app_error", map[string]interface{}{"ChannelName": channelname, "TeamId": c.TeamId}, "") + return + } + + if HandleEtag(data.Etag(), w, r) { + return + } else { + w.Header().Set(model.HEADER_ETAG_SERVER, data.Etag()) + w.Write([]byte(data.ToJson())) + } + } +} + func getChannelStats(c *Context, w http.ResponseWriter, r *http.Request) { params := mux.Vars(r) id := params["channel_id"] |