[MM-23719] api4/channel: add move channel to a team endpoint (#14246)

* api4: add move channel method

* api4: add tests for move channel, model: add move channel to client4.go

* add api.channel.move_channel.type.invalid message

* model/client4: remove a redundant line

* api4/channel: add tests for gm and private channel types

* app/channel: update move channel comment

* app/channel: add extra check if a users joins to channel during movement

* app/channel: log errors for post move channel

* app/channel: remove deactivated members by default while moving a ch.

* model/client: update move channel command

* fix vet errors

* app/channel: add missing webhook updates

Co-authored-by: Mattermod <mattermod@users.noreply.github.com>
Этот коммит содержится в:
Ibrahim Serdar Acikgoz
2020-06-22 16:57:49 +03:00
коммит произвёл GitHub
родитель 0e714f350a
Коммит 124014ad9c
9 изменённых файлов: 296 добавлений и 27 удалений

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

@@ -45,6 +45,7 @@ func (api *API) InitChannel() {
api.BaseRoutes.Channel.Handle("/pinned", api.ApiSessionRequired(getPinnedPosts)).Methods("GET")
api.BaseRoutes.Channel.Handle("/timezones", api.ApiSessionRequired(getChannelMembersTimezones)).Methods("GET")
api.BaseRoutes.Channel.Handle("/members_minus_group_members", api.ApiSessionRequired(channelMembersMinusGroupMembers)).Methods("GET")
api.BaseRoutes.Channel.Handle("/move", api.ApiSessionRequired(moveChannel)).Methods("POST")
api.BaseRoutes.Channel.Handle("/member_counts_by_group", api.ApiSessionRequired(channelMemberCountsByGroup)).Methods("GET")
api.BaseRoutes.ChannelForUser.Handle("/unread", api.ApiSessionRequired(getChannelUnread)).Methods("GET")
@@ -1753,3 +1754,70 @@ func patchChannelModerations(c *Context, w http.ResponseWriter, r *http.Request)
auditRec.Success()
w.Write(b)
}
func moveChannel(c *Context, w http.ResponseWriter, r *http.Request) {
c.RequireChannelId()
if c.Err != nil {
return
}
channel, err := c.App.GetChannel(c.Params.ChannelId)
if err != nil {
c.Err = err
return
}
props := model.StringInterfaceFromJson(r.Body)
teamId, ok := props["team_id"].(string)
if !ok {
c.SetInvalidParam("team_id")
return
}
team, err := c.App.GetTeam(teamId)
if err != nil {
c.Err = err
return
}
auditRec := c.MakeAuditRecord("moveChannel", audit.Fail)
defer c.LogAuditRec(auditRec)
auditRec.AddMeta("channel_id", channel.Id)
auditRec.AddMeta("channel_name", channel.Name)
auditRec.AddMeta("team_id", team.Id)
auditRec.AddMeta("team_name", team.Name)
if channel.Type == model.CHANNEL_DIRECT || channel.Type == model.CHANNEL_GROUP || channel.Type == model.CHANNEL_PRIVATE {
c.Err = model.NewAppError("moveChannel", "api.channel.move_channel.type.invalid", nil, "", http.StatusForbidden)
return
}
if !c.App.SessionHasPermissionTo(*c.App.Session(), model.PERMISSION_MANAGE_SYSTEM) {
c.SetPermissionError(model.PERMISSION_MANAGE_SYSTEM)
return
}
user, err := c.App.GetUser(c.App.Session().UserId)
if err != nil {
c.Err = err
return
}
err = c.App.RemoveAllDeactivatedMembersFromChannel(channel)
if err != nil {
c.Err = err
return
}
err = c.App.MoveChannel(team, channel, user)
if err != nil {
c.Err = err
return
}
auditRec.Success()
c.LogAudit("channel=" + channel.Name)
c.LogAudit("team=" + team.Name)
w.Write([]byte(channel.ToJson()))
}

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

@@ -3790,3 +3790,67 @@ func TestGetChannelMemberCountsByGroup(t *testing.T) {
require.ElementsMatch(t, expectedMemberCounts, memberCounts)
})
}
func TestMoveChannel(t *testing.T) {
th := Setup(t).InitBasic()
defer th.TearDown()
Client := th.Client
team1 := th.BasicTeam
team2 := th.CreateTeam()
t.Run("Should move channel", func(t *testing.T) {
publicChannel := th.CreatePublicChannel()
ch, resp := th.SystemAdminClient.MoveChannel(publicChannel.Id, team2.Id)
require.Nil(t, resp.Error)
require.Equal(t, team2.Id, ch.TeamId)
})
t.Run("Should fail when trying to move a private channel", func(t *testing.T) {
channel := th.CreatePrivateChannel()
_, resp := Client.MoveChannel(channel.Id, team1.Id)
require.NotNil(t, resp.Error)
CheckErrorMessage(t, resp, "api.channel.move_channel.type.invalid")
})
t.Run("Should fail when trying to move a DM channel", func(t *testing.T) {
user := th.CreateUser()
dmChannel := th.CreateDmChannel(user)
_, resp := Client.MoveChannel(dmChannel.Id, team1.Id)
require.NotNil(t, resp.Error)
CheckErrorMessage(t, resp, "api.channel.move_channel.type.invalid")
})
t.Run("Should fail when trying to move a group channel", func(t *testing.T) {
user := th.CreateUser()
gmChannel, err := th.App.CreateGroupChannel([]string{th.BasicUser.Id, th.SystemAdminUser.Id, th.TeamAdminUser.Id}, user.Id)
require.Nil(t, err)
_, resp := Client.MoveChannel(gmChannel.Id, team1.Id)
require.NotNil(t, resp.Error)
CheckErrorMessage(t, resp, "api.channel.move_channel.type.invalid")
})
t.Run("Should fail due to permissions", func(t *testing.T) {
publicChannel := th.CreatePublicChannel()
_, resp := Client.MoveChannel(publicChannel.Id, team1.Id)
require.NotNil(t, resp.Error)
CheckErrorMessage(t, resp, "api.context.permissions.app_error")
})
t.Run("Should fail to move channel due to a member not member of target team", func(t *testing.T) {
publicChannel := th.CreatePublicChannel()
user := th.BasicUser
_, resp := th.SystemAdminClient.RemoveTeamMember(team2.Id, user.Id)
CheckNoError(t, resp)
_, resp = th.SystemAdminClient.AddChannelMember(publicChannel.Id, user.Id)
CheckNoError(t, resp)
_, resp = th.SystemAdminClient.MoveChannel(publicChannel.Id, team2.Id)
require.NotNil(t, resp.Error)
CheckErrorMessage(t, resp, "app.channel.move_channel.members_do_not_match.error")
})
}