[MM-63774] Allow users to leave private channels when there is only 1 member (#30746)
* allow last user to leave private channel --------- Co-authored-by: Mattermost Build <build@mattermost.com>
Этот коммит содержится в:
@@ -2378,13 +2378,6 @@ func (a *App) LeaveChannel(c request.CTX, channelID string, userID string) *mode
|
|||||||
close(uc)
|
close(uc)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
mcc := make(chan store.StoreResult[int64], 1)
|
|
||||||
go func() {
|
|
||||||
count, err := a.Srv().Store().Channel().GetMemberCount(channelID, false)
|
|
||||||
mcc <- store.StoreResult[int64]{Data: count, NErr: err}
|
|
||||||
close(mcc)
|
|
||||||
}()
|
|
||||||
|
|
||||||
cresult := <-sc
|
cresult := <-sc
|
||||||
if cresult.NErr != nil {
|
if cresult.NErr != nil {
|
||||||
errCtx := map[string]any{"channel_id": channelID}
|
errCtx := map[string]any{"channel_id": channelID}
|
||||||
@@ -2406,25 +2399,15 @@ func (a *App) LeaveChannel(c request.CTX, channelID string, userID string) *mode
|
|||||||
return model.NewAppError("LeaveChannel", "app.user.get.app_error", nil, "", http.StatusInternalServerError).Wrap(uresult.NErr)
|
return model.NewAppError("LeaveChannel", "app.user.get.app_error", nil, "", http.StatusInternalServerError).Wrap(uresult.NErr)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ccresult := <-mcc
|
|
||||||
if ccresult.NErr != nil {
|
|
||||||
return model.NewAppError("LeaveChannel", "app.channel.get_member_count.app_error", nil, "", http.StatusInternalServerError).Wrap(ccresult.NErr)
|
|
||||||
}
|
|
||||||
|
|
||||||
channel := cresult.Data
|
channel := cresult.Data
|
||||||
user := uresult.Data
|
user := uresult.Data
|
||||||
membersCount := ccresult.Data
|
|
||||||
|
|
||||||
if channel.IsGroupOrDirect() {
|
if channel.IsGroupOrDirect() {
|
||||||
err := model.NewAppError("LeaveChannel", "api.channel.leave.direct.app_error", nil, "", http.StatusBadRequest)
|
err := model.NewAppError("LeaveChannel", "api.channel.leave.direct.app_error", nil, "", http.StatusBadRequest)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if channel.Type == model.ChannelTypePrivate && membersCount == 1 {
|
|
||||||
err := model.NewAppError("LeaveChannel", "api.channel.leave.last_member.app_error", nil, "userId="+user.Id, http.StatusBadRequest)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := a.removeUserFromChannel(c, userID, userID, channel); err != nil {
|
if err := a.removeUserFromChannel(c, userID, userID, channel); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -897,6 +897,17 @@ func TestLeaveChannel(t *testing.T) {
|
|||||||
require.Nil(t, appErr)
|
require.Nil(t, appErr)
|
||||||
require.Len(t, threads.Threads, 1)
|
require.Len(t, threads.Threads, 1)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
t.Run("can leave private channel as last member", func(t *testing.T) {
|
||||||
|
channel := th.createChannel(th.Context, th.BasicTeam, model.ChannelTypePrivate)
|
||||||
|
|
||||||
|
count, appErr := th.App.GetChannelMemberCount(th.Context, th.BasicChannel.Id)
|
||||||
|
require.Nil(t, appErr, "It should remove channel membership")
|
||||||
|
require.Equal(t, int64(1), count)
|
||||||
|
|
||||||
|
appErr = th.App.LeaveChannel(th.Context, channel.Id, th.BasicUser.Id)
|
||||||
|
require.Nil(t, appErr)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestLeaveLastChannel(t *testing.T) {
|
func TestLeaveLastChannel(t *testing.T) {
|
||||||
|
|||||||
@@ -419,10 +419,6 @@
|
|||||||
"id": "api.channel.leave.direct.app_error",
|
"id": "api.channel.leave.direct.app_error",
|
||||||
"translation": "Unable to leave a direct message channel."
|
"translation": "Unable to leave a direct message channel."
|
||||||
},
|
},
|
||||||
{
|
|
||||||
"id": "api.channel.leave.last_member.app_error",
|
|
||||||
"translation": "You're the only member left, try removing the Private Channel instead of leaving."
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
"id": "api.channel.leave.left",
|
"id": "api.channel.leave.left",
|
||||||
"translation": "%v left the channel."
|
"translation": "%v left the channel."
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user