diff options
author | Joram Wilander <jwawilander@gmail.com> | 2017-03-16 14:58:33 -0400 |
---|---|---|
committer | Corey Hulen <corey@hulen.com> | 2017-03-16 11:58:33 -0700 |
commit | d757645c2490dd4f0de16cc32e05551b1476d0a0 (patch) | |
tree | 5d6e4049aa296b7629a34ca5b02d0aba25dd27dc /api4/channel.go | |
parent | 04c0223c6402b12e67c61474ae310b0a56af6482 (diff) | |
download | chat-d757645c2490dd4f0de16cc32e05551b1476d0a0.tar.gz chat-d757645c2490dd4f0de16cc32e05551b1476d0a0.tar.bz2 chat-d757645c2490dd4f0de16cc32e05551b1476d0a0.zip |
Implement some channel endpoints for APIv4 (#5767)
Diffstat (limited to 'api4/channel.go')
-rw-r--r-- | api4/channel.go | 79 |
1 files changed, 78 insertions, 1 deletions
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 { |