summaryrefslogtreecommitdiffstats
path: root/api4
diff options
context:
space:
mode:
Diffstat (limited to 'api4')
-rw-r--r--api4/api.go2
-rw-r--r--api4/channel.go79
-rw-r--r--api4/channel_test.go125
3 files changed, 205 insertions, 1 deletions
diff --git a/api4/api.go b/api4/api.go
index 422af7b7b..75f2e0254 100644
--- a/api4/api.go
+++ b/api4/api.go
@@ -35,6 +35,7 @@ type Routes struct {
Channels *mux.Router // 'api/v4/channels'
Channel *mux.Router // 'api/v4/channels/{channel_id:[A-Za-z0-9]+}'
+ ChannelForUser *mux.Router // 'api/v4/users/{user_id:[A-Za-z0-9]+}/channels/{channel_id:[A-Za-z0-9]+}'
ChannelByName *mux.Router // 'api/v4/teams/{team_id:[A-Za-z0-9]+}/channels/name/{channel_name:[A-Za-z0-9_-]+}'
ChannelByNameForTeamName *mux.Router // 'api/v4/teams/name/{team_name:[A-Za-z0-9_-]+}/channels/name/{channel_name:[A-Za-z0-9_-]+}'
ChannelsForTeam *mux.Router // 'api/v4/teams/{team_id:[A-Za-z0-9]+}/channels'
@@ -113,6 +114,7 @@ func InitApi(full bool) {
BaseRoutes.Channels = BaseRoutes.ApiRoot.PathPrefix("/channels").Subrouter()
BaseRoutes.Channel = BaseRoutes.Channels.PathPrefix("/{channel_id:[A-Za-z0-9]+}").Subrouter()
+ BaseRoutes.ChannelForUser = BaseRoutes.User.PathPrefix("/channels/{channel_id:[A-Za-z0-9]+}").Subrouter()
BaseRoutes.ChannelByName = BaseRoutes.Team.PathPrefix("/channels/name/{channel_name:[A-Za-z0-9_-]+}").Subrouter()
BaseRoutes.ChannelByNameForTeamName = BaseRoutes.TeamByName.PathPrefix("/channels/name/{channel_name:[A-Za-z0-9_-]+}").Subrouter()
BaseRoutes.ChannelsForTeam = BaseRoutes.Team.PathPrefix("/channels").Subrouter()
diff --git a/api4/channel.go b/api4/channel.go
index acf14846a..5ed63320b 100644
--- a/api4/channel.go
+++ b/api4/channel.go
@@ -17,21 +17,26 @@ func InitChannel() {
BaseRoutes.Channels.Handle("", ApiSessionRequired(createChannel)).Methods("POST")
BaseRoutes.Channels.Handle("/direct", ApiSessionRequired(createDirectChannel)).Methods("POST")
+ BaseRoutes.Channels.Handle("/members/{user_id:[A-Za-z0-9]+}/view", ApiSessionRequired(viewChannel)).Methods("POST")
BaseRoutes.Team.Handle("/channels", ApiSessionRequired(getPublicChannelsForTeam)).Methods("GET")
BaseRoutes.Channel.Handle("", ApiSessionRequired(getChannel)).Methods("GET")
BaseRoutes.Channel.Handle("", ApiSessionRequired(updateChannel)).Methods("PUT")
BaseRoutes.Channel.Handle("", ApiSessionRequired(deleteChannel)).Methods("DELETE")
+ BaseRoutes.Channel.Handle("/stats", ApiSessionRequired(getChannelStats)).Methods("GET")
+
+ BaseRoutes.ChannelForUser.Handle("/unread", ApiSessionRequired(getChannelUnread)).Methods("GET")
+
BaseRoutes.ChannelByName.Handle("", ApiSessionRequired(getChannelByName)).Methods("GET")
BaseRoutes.ChannelByNameForTeamName.Handle("", ApiSessionRequired(getChannelByNameForTeamName)).Methods("GET")
BaseRoutes.ChannelMembers.Handle("", ApiSessionRequired(getChannelMembers)).Methods("GET")
+ BaseRoutes.ChannelMembers.Handle("/ids", ApiSessionRequired(getChannelMembersByIds)).Methods("POST")
BaseRoutes.ChannelMembersForUser.Handle("", ApiSessionRequired(getChannelMembersForUser)).Methods("GET")
BaseRoutes.ChannelMember.Handle("", ApiSessionRequired(getChannelMember)).Methods("GET")
BaseRoutes.ChannelMember.Handle("", ApiSessionRequired(removeChannelMember)).Methods("DELETE")
BaseRoutes.ChannelMember.Handle("/roles", ApiSessionRequired(updateChannelMemberRoles)).Methods("PUT")
- BaseRoutes.Channels.Handle("/members/{user_id:[A-Za-z0-9]+}/view", ApiSessionRequired(viewChannel)).Methods("POST")
}
func createChannel(c *Context, w http.ResponseWriter, r *http.Request) {
@@ -207,6 +212,53 @@ func getChannel(c *Context, w http.ResponseWriter, r *http.Request) {
}
}
+func getChannelUnread(c *Context, w http.ResponseWriter, r *http.Request) {
+ c.RequireChannelId().RequireUserId()
+ if c.Err != nil {
+ return
+ }
+
+ if !app.SessionHasPermissionToUser(c.Session, c.Params.UserId) {
+ c.SetPermissionError(model.PERMISSION_EDIT_OTHER_USERS)
+ return
+ }
+
+ if !app.SessionHasPermissionToChannel(c.Session, c.Params.ChannelId, model.PERMISSION_READ_CHANNEL) {
+ c.SetPermissionError(model.PERMISSION_READ_CHANNEL)
+ return
+ }
+
+ channelUnread, err := app.GetChannelUnread(c.Params.ChannelId, c.Params.UserId)
+ if err != nil {
+ c.Err = err
+ return
+ }
+
+ w.Write([]byte(channelUnread.ToJson()))
+}
+
+func getChannelStats(c *Context, w http.ResponseWriter, r *http.Request) {
+ c.RequireChannelId()
+ if c.Err != nil {
+ return
+ }
+
+ if !app.SessionHasPermissionToChannel(c.Session, c.Params.ChannelId, model.PERMISSION_READ_CHANNEL) {
+ c.SetPermissionError(model.PERMISSION_READ_CHANNEL)
+ return
+ }
+
+ memberCount, err := app.GetChannelMemberCount(c.Params.ChannelId)
+
+ if err != nil {
+ c.Err = err
+ return
+ }
+
+ stats := model.ChannelStats{ChannelId: c.Params.ChannelId, MemberCount: memberCount}
+ w.Write([]byte(stats.ToJson()))
+}
+
func getPublicChannelsForTeam(c *Context, w http.ResponseWriter, r *http.Request) {
c.RequireTeamId()
if c.Err != nil {
@@ -326,6 +378,31 @@ func getChannelMembers(c *Context, w http.ResponseWriter, r *http.Request) {
}
}
+func getChannelMembersByIds(c *Context, w http.ResponseWriter, r *http.Request) {
+ c.RequireChannelId()
+ if c.Err != nil {
+ return
+ }
+
+ userIds := model.ArrayFromJson(r.Body)
+ if len(userIds) == 0 {
+ c.SetInvalidParam("user_ids")
+ return
+ }
+
+ if !app.SessionHasPermissionToChannel(c.Session, c.Params.ChannelId, model.PERMISSION_READ_CHANNEL) {
+ c.SetPermissionError(model.PERMISSION_READ_CHANNEL)
+ return
+ }
+
+ if members, err := app.GetChannelMembersByIds(c.Params.ChannelId, userIds); err != nil {
+ c.Err = err
+ return
+ } else {
+ w.Write([]byte(members.ToJson()))
+ }
+}
+
func getChannelMember(c *Context, w http.ResponseWriter, r *http.Request) {
c.RequireChannelId().RequireUserId()
if c.Err != nil {
diff --git a/api4/channel_test.go b/api4/channel_test.go
index 97364a73e..bf7218f0b 100644
--- a/api4/channel_test.go
+++ b/api4/channel_test.go
@@ -759,6 +759,53 @@ func TestGetChannelMembers(t *testing.T) {
CheckNoError(t, resp)
}
+func TestGetChannelMembersByIds(t *testing.T) {
+ th := Setup().InitBasic().InitSystemAdmin()
+ defer TearDown()
+ Client := th.Client
+
+ cm, resp := Client.GetChannelMembersByIds(th.BasicChannel.Id, []string{th.BasicUser.Id})
+ CheckNoError(t, resp)
+
+ if (*cm)[0].UserId != th.BasicUser.Id {
+ t.Fatal("returned wrong user")
+ }
+
+ _, resp = Client.GetChannelMembersByIds(th.BasicChannel.Id, []string{})
+ CheckBadRequestStatus(t, resp)
+
+ cm1, resp := Client.GetChannelMembersByIds(th.BasicChannel.Id, []string{"junk"})
+ CheckNoError(t, resp)
+ if len(*cm1) > 0 {
+ t.Fatal("no users should be returned")
+ }
+
+ cm1, resp = Client.GetChannelMembersByIds(th.BasicChannel.Id, []string{"junk", th.BasicUser.Id})
+ CheckNoError(t, resp)
+ if len(*cm1) != 1 {
+ t.Fatal("1 member should be returned")
+ }
+
+ cm1, resp = Client.GetChannelMembersByIds(th.BasicChannel.Id, []string{th.BasicUser2.Id, th.BasicUser.Id})
+ CheckNoError(t, resp)
+ if len(*cm1) != 2 {
+ t.Fatal("2 members should be returned")
+ }
+
+ _, resp = Client.GetChannelMembersByIds("junk", []string{th.BasicUser.Id})
+ CheckBadRequestStatus(t, resp)
+
+ _, resp = Client.GetChannelMembersByIds(model.NewId(), []string{th.BasicUser.Id})
+ CheckForbiddenStatus(t, resp)
+
+ Client.Logout()
+ _, resp = Client.GetChannelMembersByIds(th.BasicChannel.Id, []string{th.BasicUser.Id})
+ CheckUnauthorizedStatus(t, resp)
+
+ _, resp = th.SystemAdminClient.GetChannelMembersByIds(th.BasicChannel.Id, []string{th.BasicUser2.Id, th.BasicUser.Id})
+ CheckNoError(t, resp)
+}
+
func TestGetChannelMember(t *testing.T) {
th := Setup().InitBasic().InitSystemAdmin()
defer TearDown()
@@ -914,6 +961,84 @@ func TestViewChannel(t *testing.T) {
CheckNoError(t, resp)
}
+func TestGetChannelUnread(t *testing.T) {
+ th := Setup().InitBasic().InitSystemAdmin()
+ defer TearDown()
+ Client := th.Client
+ user := th.BasicUser
+ channel := th.BasicChannel
+
+ channelUnread, resp := Client.GetChannelUnread(channel.Id, user.Id)
+ CheckNoError(t, resp)
+ if channelUnread.TeamId != th.BasicTeam.Id {
+ t.Fatal("wrong team id returned for a regular user call")
+ } else if channelUnread.ChannelId != channel.Id {
+ t.Fatal("wrong team id returned for a regular user call")
+ }
+
+ _, resp = Client.GetChannelUnread("junk", user.Id)
+ CheckBadRequestStatus(t, resp)
+
+ _, resp = Client.GetChannelUnread(channel.Id, "junk")
+ CheckBadRequestStatus(t, resp)
+
+ _, resp = Client.GetChannelUnread(channel.Id, model.NewId())
+ CheckForbiddenStatus(t, resp)
+
+ _, resp = Client.GetChannelUnread(model.NewId(), user.Id)
+ CheckForbiddenStatus(t, resp)
+
+ newUser := th.CreateUser()
+ Client.Login(newUser.Email, newUser.Password)
+ _, resp = Client.GetChannelUnread(th.BasicChannel.Id, user.Id)
+ CheckForbiddenStatus(t, resp)
+
+ Client.Logout()
+
+ _, resp = th.SystemAdminClient.GetChannelUnread(channel.Id, user.Id)
+ CheckNoError(t, resp)
+
+ _, resp = th.SystemAdminClient.GetChannelUnread(model.NewId(), user.Id)
+ CheckNotFoundStatus(t, resp)
+
+ _, resp = th.SystemAdminClient.GetChannelUnread(channel.Id, model.NewId())
+ CheckNotFoundStatus(t, resp)
+}
+
+func TestGetChannelStats(t *testing.T) {
+ th := Setup().InitBasic().InitSystemAdmin()
+ defer TearDown()
+ Client := th.Client
+ channel := th.CreatePrivateChannel()
+
+ stats, resp := Client.GetChannelStats(channel.Id, "")
+ CheckNoError(t, resp)
+
+ if stats.ChannelId != channel.Id {
+ t.Fatal("couldnt't get extra info")
+ } else if stats.MemberCount != 1 {
+ t.Fatal("got incorrect member count")
+ }
+
+ _, resp = Client.GetChannelStats("junk", "")
+ CheckBadRequestStatus(t, resp)
+
+ _, resp = Client.GetChannelStats(model.NewId(), "")
+ CheckForbiddenStatus(t, resp)
+
+ Client.Logout()
+ _, resp = Client.GetChannelStats(channel.Id, "")
+ CheckUnauthorizedStatus(t, resp)
+
+ th.LoginBasic2()
+
+ _, resp = Client.GetChannelStats(channel.Id, "")
+ CheckForbiddenStatus(t, resp)
+
+ _, resp = th.SystemAdminClient.GetChannelStats(channel.Id, "")
+ CheckNoError(t, resp)
+}
+
func TestUpdateChannelRoles(t *testing.T) {
th := Setup().InitBasic().InitSystemAdmin()
defer TearDown()