From 5efcd2d9d3fb91ae7475918b807b7947b533da9b Mon Sep 17 00:00:00 2001 From: Robin Naundorf Date: Tue, 9 May 2017 14:52:46 +0200 Subject: Add API Endpoint for deleted Channels (#5889) --- api4/channel.go | 21 +++++++++++ api4/channel_test.go | 49 ++++++++++++++++++++++++ app/channel.go | 8 ++++ model/client4.go | 11 ++++++ store/sql_channel_store.go | 25 +++++++++++++ store/sql_channel_store_test.go | 82 +++++++++++++++++++++++++++++++++++++++++ store/store.go | 1 + 7 files changed, 197 insertions(+) diff --git a/api4/channel.go b/api4/channel.go index 493b012a0..69cc0953e 100644 --- a/api4/channel.go +++ b/api4/channel.go @@ -21,6 +21,7 @@ func InitChannel() { BaseRoutes.Channels.Handle("/members/{user_id:[A-Za-z0-9]+}/view", ApiSessionRequired(viewChannel)).Methods("POST") BaseRoutes.ChannelsForTeam.Handle("", ApiSessionRequired(getPublicChannelsForTeam)).Methods("GET") + BaseRoutes.ChannelsForTeam.Handle("/deleted", ApiSessionRequired(getDeletedChannelsForTeam)).Methods("GET") BaseRoutes.ChannelsForTeam.Handle("/ids", ApiSessionRequired(getPublicChannelsByIdsForTeam)).Methods("POST") BaseRoutes.ChannelsForTeam.Handle("/search", ApiSessionRequired(searchChannelsForTeam)).Methods("POST") BaseRoutes.User.Handle("/teams/{team_id:[A-Za-z0-9]+}/channels", ApiSessionRequired(getChannelsForTeamForUser)).Methods("GET") @@ -385,6 +386,26 @@ func getPublicChannelsForTeam(c *Context, w http.ResponseWriter, r *http.Request } } +func getDeletedChannelsForTeam(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_MANAGE_TEAM) { + c.SetPermissionError(model.PERMISSION_MANAGE_TEAM) + return + } + + if channels, err := app.GetDeletedChannels(c.Params.TeamId, c.Params.Page * c.Params.PerPage, c.Params.PerPage); err != nil { + c.Err = err + return + } else { + w.Write([]byte(channels.ToJson())) + return + } +} + func getPublicChannelsByIdsForTeam(c *Context, w http.ResponseWriter, r *http.Request) { c.RequireTeamId() if c.Err != nil { diff --git a/api4/channel_test.go b/api4/channel_test.go index 345bbefaf..10cb272f3 100644 --- a/api4/channel_test.go +++ b/api4/channel_test.go @@ -489,6 +489,55 @@ func TestGetChannel(t *testing.T) { CheckNotFoundStatus(t, resp) } +func TestGetDeletedChannelsForTeam(t *testing.T) { + th := Setup().InitBasic().InitSystemAdmin() + defer TearDown() + Client := th.Client + team := th.BasicTeam + + channels, resp := Client.GetDeletedChannelsForTeam(team.Id, 0, 100, "") + CheckForbiddenStatus(t, resp) + + th.LoginTeamAdmin() + + channels, resp = Client.GetDeletedChannelsForTeam(team.Id, 0, 100, "") + CheckNoError(t, resp) + if len(*channels) != 0 { + t.Fatal("should be no deleted channels") + } + + // create and delete public channel + publicChannel1 := th.CreatePublicChannel() + Client.DeleteChannel(publicChannel1.Id) + + channels, resp = Client.GetDeletedChannelsForTeam(team.Id, 0, 100, "") + CheckNoError(t, resp) + if len(*channels) != 1 { + t.Fatal("should be 1 deleted channel") + } + + publicChannel2 := th.CreatePublicChannel() + Client.DeleteChannel(publicChannel2.Id) + + channels, resp = Client.GetDeletedChannelsForTeam(team.Id, 0, 100, "") + CheckNoError(t, resp) + if len(*channels) != 2 { + t.Fatal("should be 2 deleted channels") + } + + channels, resp = Client.GetDeletedChannelsForTeam(team.Id, 0, 1, "") + CheckNoError(t, resp) + if len(*channels) != 1 { + t.Fatal("should be one channel per page") + } + + channels, resp = Client.GetDeletedChannelsForTeam(team.Id, 1, 1, "") + CheckNoError(t, resp) + if len(*channels) != 1 { + t.Fatal("should be one channel per page") + } +} + func TestGetPublicChannelsForTeam(t *testing.T) { th := Setup().InitBasic().InitSystemAdmin() defer TearDown() diff --git a/app/channel.go b/app/channel.go index ee53ace45..b4855bc9e 100644 --- a/app/channel.go +++ b/app/channel.go @@ -720,6 +720,14 @@ func GetChannelsForUser(teamId string, userId string) (*model.ChannelList, *mode } } +func GetDeletedChannels(teamId string, offset int, limit int) (*model.ChannelList, *model.AppError) { + if result := <-Srv.Store.Channel().GetDeleted(teamId, offset, limit); result.Err != nil { + return nil, result.Err + } else { + return result.Data.(*model.ChannelList), nil + } +} + func GetChannelsUserNotIn(teamId string, userId string, offset int, limit int) (*model.ChannelList, *model.AppError) { if result := <-Srv.Store.Channel().GetMoreChannels(teamId, userId, offset, limit); result.Err != nil { return nil, result.Err diff --git a/model/client4.go b/model/client4.go index 3a6507f82..7e0cd1acf 100644 --- a/model/client4.go +++ b/model/client4.go @@ -1242,6 +1242,17 @@ func (c *Client4) GetPublicChannelsForTeam(teamId string, page int, perPage int, } } +// GetDeletedChannelsForTeam returns a list of public channels based on the provided team id string. +func (c *Client4) GetDeletedChannelsForTeam(teamId string, page int, perPage int, etag string) (*ChannelList, *Response) { + query := fmt.Sprintf("/deleted?page=%v&per_page=%v", page, perPage) + if r, err := c.DoApiGet(c.GetChannelsForTeamRoute(teamId)+query, etag); err != nil { + return nil, &Response{StatusCode: r.StatusCode, Error: err} + } else { + defer closeBody(r) + return ChannelListFromJson(r.Body), BuildResponse(r) + } +} + // GetPublicChannelsByIdsForTeam returns a list of public channels based on provided team id string func (c *Client4) GetPublicChannelsByIdsForTeam(teamId string, channelIds []string) (*ChannelList, *Response) { if r, err := c.DoApiPost(c.GetChannelsForTeamRoute(teamId)+"/ids", ArrayToJson(channelIds)); err != nil { diff --git a/store/sql_channel_store.go b/store/sql_channel_store.go index 881f1cc61..f3619c03a 100644 --- a/store/sql_channel_store.go +++ b/store/sql_channel_store.go @@ -812,6 +812,31 @@ func (s SqlChannelStore) GetDeletedByName(teamId string, name string) StoreChann return storeChannel } +func (s SqlChannelStore) GetDeleted(teamId string, offset int, limit int) StoreChannel { + storeChannel := make(StoreChannel, 1) + + go func() { + result := StoreResult{} + + channels := &model.ChannelList{} + + if _, err := s.GetReplica().Select(channels, "SELECT * FROM Channels WHERE (TeamId = :TeamId OR TeamId = '') AND DeleteAt != 0 ORDER BY DisplayName LIMIT :Limit OFFSET :Offset", map[string]interface{}{"TeamId": teamId, "Limit": limit, "Offset": offset}); err != nil { + if err == sql.ErrNoRows { + result.Err = model.NewLocAppError("SqlChannelStore.GetDeleted", "store.sql_channel.get_deleted.missing.app_error", nil, "teamId="+teamId+", "+err.Error()) + } else { + result.Err = model.NewLocAppError("SqlChannelStore.GetDeleted", "store.sql_channel.get_deleted.existing.app_error", nil, "teamId="+teamId+", "+err.Error()) + } + } else { + result.Data = channels + } + + storeChannel <- result + close(storeChannel) + }() + + return storeChannel +} + func (s SqlChannelStore) SaveMember(member *model.ChannelMember) StoreChannel { storeChannel := make(StoreChannel, 1) diff --git a/store/sql_channel_store_test.go b/store/sql_channel_store_test.go index ce3a9a12f..55a263037 100644 --- a/store/sql_channel_store_test.go +++ b/store/sql_channel_store_test.go @@ -512,6 +512,88 @@ func TestChannelStoreGetDeletedByName(t *testing.T) { } } +func TestChannelStoreGetDeleted(t *testing.T) { + Setup() + + o1 := model.Channel{} + o1.TeamId = model.NewId() + o1.DisplayName = "Channel1" + o1.Name = "a" + model.NewId() + "b" + o1.Type = model.CHANNEL_OPEN + o1.DeleteAt = model.GetMillis() + Must(store.Channel().Save(&o1)) + + cresult := <-store.Channel().GetDeleted(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") + } + + o2 := model.Channel{} + o2.TeamId = o1.TeamId + o2.DisplayName = "Channel2" + o2.Name = "a" + model.NewId() + "b" + o2.Type = model.CHANNEL_OPEN + Must(store.Channel().Save(&o2)) + + cresult = <-store.Channel().GetDeleted(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") + } + + o3 := model.Channel{} + o3.TeamId = o1.TeamId + o3.DisplayName = "Channel3" + o3.Name = "a" + model.NewId() + "b" + o3.Type = model.CHANNEL_OPEN + o3.DeleteAt = model.GetMillis() + Must(store.Channel().Save(&o3)) + + cresult = <-store.Channel().GetDeleted(o1.TeamId, 0, 100) + if cresult.Err != nil { + t.Fatal(cresult.Err) + } + list = cresult.Data.(*model.ChannelList) + + if len(*list) != 2 { + t.Fatal("wrong list length") + } + + cresult = <-store.Channel().GetDeleted(o1.TeamId, 0, 1) + if cresult.Err != nil { + t.Fatal(cresult.Err) + } + list = cresult.Data.(*model.ChannelList) + + if len(*list) != 1 { + t.Fatal("wrong list length") + } + + cresult = <-store.Channel().GetDeleted(o1.TeamId, 1, 1) + if cresult.Err != nil { + t.Fatal(cresult.Err) + } + list = cresult.Data.(*model.ChannelList) + + if len(*list) != 1 { + t.Fatal("wrong list length") + } + +} + func TestChannelMemberStore(t *testing.T) { Setup() diff --git a/store/store.go b/store/store.go index 78dc4308b..bb1b8f197 100644 --- a/store/store.go +++ b/store/store.go @@ -103,6 +103,7 @@ type ChannelStore interface { GetByName(team_id string, name string, allowFromCache bool) StoreChannel GetByNameIncludeDeleted(team_id string, name string, allowFromCache bool) StoreChannel GetDeletedByName(team_id string, name string) StoreChannel + GetDeleted(team_id string, offset int, limit int) 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 -- cgit v1.2.3-1-g7c22