Endpoint for APIv4: GET /team/{team_id}/channels (#5681)

This commit is contained in:
Saturnino Abril
2017-03-13 12:26:51 +00:00
committed by George Goldberg
parent 3559fb7959
commit 8731465957
8 changed files with 249 additions and 0 deletions
+22
View File
@@ -18,6 +18,8 @@ func InitChannel() {
BaseRoutes.Channels.Handle("", ApiSessionRequired(createChannel)).Methods("POST")
BaseRoutes.Channels.Handle("/direct", ApiSessionRequired(createDirectChannel)).Methods("POST")
BaseRoutes.Team.Handle("/channels", ApiSessionRequired(getPublicChannelsForTeam)).Methods("GET")
BaseRoutes.Channel.Handle("", ApiSessionRequired(getChannel)).Methods("GET")
BaseRoutes.ChannelByName.Handle("", ApiSessionRequired(getChannelByName)).Methods("GET")
BaseRoutes.ChannelByNameForTeamName.Handle("", ApiSessionRequired(getChannelByNameForTeamName)).Methods("GET")
@@ -115,6 +117,26 @@ func getChannel(c *Context, w http.ResponseWriter, r *http.Request) {
}
}
func getPublicChannelsForTeam(c *Context, w http.ResponseWriter, r *http.Request) {
c.RequireTeamId()
if c.Err != nil {
return
}
if !app.SessionHasPermissionToTeam(c.Session, c.Params.TeamId, model.PERMISSION_LIST_TEAM_CHANNELS) {
c.SetPermissionError(model.PERMISSION_LIST_TEAM_CHANNELS)
return
}
if channels, err := app.GetPublicChannelsForTeam(c.Params.TeamId, c.Params.Page, c.Params.PerPage); err != nil {
c.Err = err
return
} else {
w.Write([]byte(channels.ToJson()))
return
}
}
func getChannelByName(c *Context, w http.ResponseWriter, r *http.Request) {
c.RequireTeamId().RequireChannelName()
if c.Err != nil {
+80
View File
@@ -254,6 +254,86 @@ func TestGetChannel(t *testing.T) {
CheckNotFoundStatus(t, resp)
}
func TestGetPublicChannelsForTeam(t *testing.T) {
th := Setup().InitBasic().InitSystemAdmin()
defer TearDown()
Client := th.Client
team := th.BasicTeam
publicChannel1 := th.BasicChannel
publicChannel2 := th.BasicChannel2
channels, resp := Client.GetPublicChannelsForTeam(team.Id, 0, 100, "")
CheckNoError(t, resp)
if len(*channels) != 4 {
t.Fatal("wrong length")
}
for i, c := range *channels {
if c.Type != model.CHANNEL_OPEN {
t.Fatal("should include open channel only")
}
// only check the created 2 public channels
if i < 2 && !(c.DisplayName == publicChannel1.DisplayName || c.DisplayName == publicChannel2.DisplayName) {
t.Logf("channel %v: %v", i, c.DisplayName)
t.Fatal("should match public channel display name only")
}
}
privateChannel := th.CreatePrivateChannel()
channels, resp = Client.GetPublicChannelsForTeam(team.Id, 0, 100, "")
CheckNoError(t, resp)
if len(*channels) != 4 {
t.Fatal("wrong length")
}
for _, c := range *channels {
if c.Type != model.CHANNEL_OPEN {
t.Fatal("should not include private channel")
}
if c.DisplayName == privateChannel.DisplayName {
t.Fatal("should not match private channel display name")
}
}
channels, resp = Client.GetPublicChannelsForTeam(team.Id, 0, 1, "")
CheckNoError(t, resp)
if len(*channels) != 1 {
t.Fatal("should be one channel per page")
}
channels, resp = Client.GetPublicChannelsForTeam(team.Id, 1, 1, "")
CheckNoError(t, resp)
if len(*channels) != 1 {
t.Fatal("should be one channel per page")
}
channels, resp = Client.GetPublicChannelsForTeam(team.Id, 10000, 100, "")
CheckNoError(t, resp)
if len(*channels) != 0 {
t.Fatal("should be no channel")
}
_, resp = Client.GetPublicChannelsForTeam("junk", 0, 100, "")
CheckBadRequestStatus(t, resp)
_, resp = Client.GetPublicChannelsForTeam(model.NewId(), 0, 100, "")
CheckForbiddenStatus(t, resp)
Client.Logout()
_, resp = Client.GetPublicChannelsForTeam(team.Id, 0, 100, "")
CheckUnauthorizedStatus(t, resp)
user := th.CreateUser()
Client.Login(user.Email, user.Password)
_, resp = Client.GetPublicChannelsForTeam(team.Id, 0, 100, "")
CheckForbiddenStatus(t, resp)
_, resp = th.SystemAdminClient.GetPublicChannelsForTeam(team.Id, 0, 100, "")
CheckNoError(t, resp)
}
func TestGetChannelByName(t *testing.T) {
th := Setup().InitBasic().InitSystemAdmin()
defer TearDown()
+8
View File
@@ -646,6 +646,14 @@ func GetChannelsUserNotIn(teamId string, userId string, offset int, limit int) (
}
}
func GetPublicChannelsForTeam(teamId string, offset int, limit int) (*model.ChannelList, *model.AppError) {
if result := <-Srv.Store.Channel().GetPublicChannelsForTeam(teamId, offset, limit); result.Err != nil {
return nil, result.Err
} else {
return result.Data.(*model.ChannelList), nil
}
}
func GetChannelMember(channelId string, userId string) (*model.ChannelMember, *model.AppError) {
if result := <-Srv.Store.Channel().GetMember(channelId, userId); result.Err != nil {
return nil, result.Err
+4
View File
@@ -4707,6 +4707,10 @@
"id": "store.sql_channel.get_more_channels.get.app_error",
"translation": "We couldn't get the channels"
},
{
"id": "store.sql_channel.get_public_channels.get.app_error",
"translation": "We couldn't get public channels"
},
{
"id": "store.sql_channel.increment_mention_count.app_error",
"translation": "We couldn't increment the mention count"
+15
View File
@@ -98,6 +98,10 @@ func (c *Client4) GetChannelsRoute() string {
return fmt.Sprintf("/channels")
}
func (c *Client4) GetPublicChannelsForTeamRoute(teamId string) string {
return fmt.Sprintf(c.GetTeamRoute(teamId) + "/channels")
}
func (c *Client4) GetChannelRoute(channelId string) string {
return fmt.Sprintf(c.GetChannelsRoute()+"/%v", channelId)
}
@@ -706,6 +710,17 @@ func (c *Client4) GetChannel(channelId, etag string) (*Channel, *Response) {
}
}
// GetPublicChannelsForTeam returns a channel based on the provided team id string.
func (c *Client4) GetPublicChannelsForTeam(teamId string, page int, perPage int, etag string) (*ChannelList, *Response) {
query := fmt.Sprintf("?page=%v&per_page=%v", page, perPage)
if r, err := c.DoApiGet(c.GetPublicChannelsForTeamRoute(teamId)+query, etag); err != nil {
return nil, &Response{StatusCode: r.StatusCode, Error: err}
} else {
defer closeBody(r)
return ChannelListFromJson(r.Body), BuildResponse(r)
}
}
// GetChannelByName returns a channel based on the provided channel name and team id strings.
func (c *Client4) GetChannelByName(channelName, teamId string, etag string) (*Channel, *Response) {
if r, err := c.DoApiGet(c.GetChannelByNameRoute(channelName, teamId), etag); err != nil {
+34
View File
@@ -550,6 +550,40 @@ func (s SqlChannelStore) GetMoreChannels(teamId string, userId string, offset in
return storeChannel
}
func (s SqlChannelStore) GetPublicChannelsForTeam(teamId string, offset int, limit int) StoreChannel {
storeChannel := make(StoreChannel, 1)
go func() {
result := StoreResult{}
data := &model.ChannelList{}
_, err := s.GetReplica().Select(data,
`SELECT
*
FROM
Channels
WHERE
TeamId = :TeamId
AND Type = 'O'
AND DeleteAt = 0
ORDER BY DisplayName
LIMIT :Limit
OFFSET :Offset`,
map[string]interface{}{"TeamId": teamId, "Limit": limit, "Offset": offset})
if err != nil {
result.Err = model.NewLocAppError("SqlChannelStore.GetPublicChannelsForTeam", "store.sql_channel.get_public_channels.get.app_error", nil, "teamId="+teamId+", err="+err.Error())
} else {
result.Data = data
}
storeChannel <- result
close(storeChannel)
}()
return storeChannel
}
type channelIdWithCountAndUpdateAt struct {
Id string
TotalMsgCount int64
+85
View File
@@ -775,6 +775,91 @@ func TestChannelStoreGetMoreChannels(t *testing.T) {
}
}
func TestChannelStoreGetPublicChannelsForTeam(t *testing.T) {
Setup()
o1 := model.Channel{}
o1.TeamId = model.NewId()
o1.DisplayName = "OpenChannel1Team1"
o1.Name = "a" + model.NewId() + "b"
o1.Type = model.CHANNEL_OPEN
Must(store.Channel().Save(&o1))
o2 := model.Channel{}
o2.TeamId = model.NewId()
o2.DisplayName = "OpenChannel1Team2"
o2.Name = "a" + model.NewId() + "b"
o2.Type = model.CHANNEL_OPEN
Must(store.Channel().Save(&o2))
o3 := model.Channel{}
o3.TeamId = o1.TeamId
o3.DisplayName = "PrivateChannel1Team1"
o3.Name = "a" + model.NewId() + "b"
o3.Type = model.CHANNEL_PRIVATE
Must(store.Channel().Save(&o3))
cresult := <-store.Channel().GetPublicChannelsForTeam(o1.TeamId, 0, 100)
if cresult.Err != nil {
t.Fatal(cresult.Err)
}
list := cresult.Data.(*model.ChannelList)
if len(*list) != 1 {
t.Fatal("wrong list")
}
if (*list)[0].Name != o1.Name {
t.Fatal("missing channel")
}
o4 := model.Channel{}
o4.TeamId = o1.TeamId
o4.DisplayName = "OpenChannel2Team1"
o4.Name = "a" + model.NewId() + "b"
o4.Type = model.CHANNEL_OPEN
Must(store.Channel().Save(&o4))
cresult = <-store.Channel().GetPublicChannelsForTeam(o1.TeamId, 0, 100)
list = cresult.Data.(*model.ChannelList)
if len(*list) != 2 {
t.Fatal("wrong list length")
}
cresult = <-store.Channel().GetPublicChannelsForTeam(o1.TeamId, 0, 1)
list = cresult.Data.(*model.ChannelList)
if len(*list) != 1 {
t.Fatal("wrong list length")
}
cresult = <-store.Channel().GetPublicChannelsForTeam(o1.TeamId, 1, 1)
list = cresult.Data.(*model.ChannelList)
if len(*list) != 1 {
t.Fatal("wrong list length")
}
if r1 := <-store.Channel().AnalyticsTypeCount(o1.TeamId, model.CHANNEL_OPEN); r1.Err != nil {
t.Fatal(r1.Err)
} else {
if r1.Data.(int64) != 2 {
t.Log(r1.Data)
t.Fatal("wrong value")
}
}
if r1 := <-store.Channel().AnalyticsTypeCount(o1.TeamId, model.CHANNEL_PRIVATE); r1.Err != nil {
t.Fatal(r1.Err)
} else {
if r1.Data.(int64) != 1 {
t.Log(r1.Data)
t.Fatal("wrong value")
}
}
}
func TestChannelStoreGetChannelCounts(t *testing.T) {
Setup()
+1
View File
@@ -101,6 +101,7 @@ type ChannelStore interface {
GetDeletedByName(team_id string, name string) StoreChannel
GetChannels(teamId string, userId string) StoreChannel
GetMoreChannels(teamId string, userId string, offset int, limit int) StoreChannel
GetPublicChannelsForTeam(teamId string, offset int, limit int) StoreChannel
GetChannelCounts(teamId string, userId string) StoreChannel
GetTeamChannels(teamId string) StoreChannel
GetAll(teamId string) StoreChannel