Add API Endpoint for deleted Channels (#5889)

Этот коммит содержится в:
Robin Naundorf
2017-05-09 14:52:46 +02:00
коммит произвёл Joram Wilander
родитель 530814b8c9
Коммит 5efcd2d9d3
7 изменённых файлов: 197 добавлений и 0 удалений

Просмотреть файл

@@ -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 {

Просмотреть файл

@@ -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()

Просмотреть файл

@@ -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

Просмотреть файл

@@ -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 {

Просмотреть файл

@@ -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)

Просмотреть файл

@@ -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()

Просмотреть файл

@@ -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