summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorCarlos Tadeu Panato Junior <ctadeu@gmail.com>2016-12-12 06:30:37 +0100
committerCorey Hulen <corey@hulen.com>2016-12-11 21:30:37 -0800
commitf0d71d87899967335210b9130a7e2b8d180bef46 (patch)
treed2941b8870e87677a84821dd32454a28594c889b
parentb5fcfd608c0e9ef764cace7328653e4d4c47a061 (diff)
downloadchat-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
-rw-r--r--api/channel.go32
-rw-r--r--api/channel_test.go28
-rw-r--r--model/client.go10
-rw-r--r--webapp/client/client.jsx9
-rw-r--r--webapp/tests/client_channel.test.jsx15
5 files changed, 94 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"]
diff --git a/api/channel_test.go b/api/channel_test.go
index 5b44b6ab0..2414b51e2 100644
--- a/api/channel_test.go
+++ b/api/channel_test.go
@@ -10,6 +10,7 @@ import (
"time"
"github.com/mattermost/platform/model"
+ "github.com/mattermost/platform/store"
"github.com/mattermost/platform/utils"
)
@@ -1632,3 +1633,30 @@ func TestAutocompleteChannels(t *testing.T) {
t.Fatal("should have failed - bad team id")
}
}
+
+func TestGetChannelByName(t *testing.T) {
+ th := Setup().InitBasic()
+ Client := th.BasicClient
+
+ if _, err := Client.GetChannelByName(th.BasicChannel.Name); err != nil {
+ t.Fatal("Failed to get channel")
+ }
+
+ if _, err := Client.GetChannelByName("InvalidChannelName"); err == nil {
+ t.Fatal("Failed to get team")
+ }
+
+ Client.Must(Client.Logout())
+
+ user2 := &model.User{Email: "success+" + model.NewId() + "@simulator.amazonses.com", Nickname: "Jabba the Hutt", Password: "passwd1"}
+ user2 = Client.Must(Client.CreateUser(user2, "")).Data.(*model.User)
+ store.Must(Srv.Store.User().VerifyEmail(user2.Id))
+
+ Client.SetTeamId(th.BasicTeam.Id)
+
+ Client.Login(user2.Email, "passwd1")
+
+ if _, err := Client.GetChannelByName(th.BasicChannel.Name); err == nil {
+ t.Fatal("Should fail due not enough permissions")
+ }
+}
diff --git a/model/client.go b/model/client.go
index 951b3388a..b8bd9fc44 100644
--- a/model/client.go
+++ b/model/client.go
@@ -1252,6 +1252,16 @@ func (c *Client) GetChannels(etag string) (*Result, *AppError) {
}
}
+func (c *Client) GetChannelByName(channelName string) (*Result, *AppError) {
+ if r, err := c.DoApiGet(c.GetChannelNameRoute(channelName), "", ""); err != nil {
+ return nil, err
+ } else {
+ defer closeBody(r)
+ return &Result{r.Header.Get(HEADER_REQUEST_ID),
+ r.Header.Get(HEADER_ETAG_SERVER), ChannelMemberFromJson(r.Body)}, nil
+ }
+}
+
func (c *Client) JoinChannel(id string) (*Result, *AppError) {
if r, err := c.DoApiPost(c.GetChannelRoute(id)+"/join", ""); err != nil {
return nil, err
diff --git a/webapp/client/client.jsx b/webapp/client/client.jsx
index 398ce4f83..88f910d46 100644
--- a/webapp/client/client.jsx
+++ b/webapp/client/client.jsx
@@ -1441,6 +1441,15 @@ export default class Client {
end(this.handleResponse.bind(this, 'getMyChannelMembers', success, error));
}
+ getChannelByName(channelName, success, error) {
+ request.
+ get(`${this.getChannelsRoute()}/name/${channelName}`).
+ set(this.defaultHeaders).
+ type('application/json').
+ accept('application/json').
+ end(this.handleResponse.bind(this, 'getChannelByName', success, error));
+ }
+
getChannelStats(channelId, success, error) {
request.
get(`${this.getChannelNeededRoute(channelId)}/stats`).
diff --git a/webapp/tests/client_channel.test.jsx b/webapp/tests/client_channel.test.jsx
index e8466021f..08c821f3c 100644
--- a/webapp/tests/client_channel.test.jsx
+++ b/webapp/tests/client_channel.test.jsx
@@ -426,5 +426,20 @@ describe('Client.Channels', function() {
);
});
});
+
+ it('getChannelByName', function(done) {
+ TestHelper.initBasic(() => {
+ TestHelper.basicClient().getChannelByName(
+ TestHelper.basicChannel().name,
+ function(data) {
+ assert.equal(data.name, TestHelper.basicChannel().name);
+ done();
+ },
+ function(err) {
+ done(new Error(err.message));
+ }
+ );
+ });
+ });
});