[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>
Этот коммит содержится в:
Ben Cooke
2025-05-14 16:58:13 +01:00
коммит произвёл GitHub
родитель 9fe678cad3
Коммит 6b4ab0a891
3 изменённых файлов: 11 добавлений и 21 удалений

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

@@ -2378,13 +2378,6 @@ func (a *App) LeaveChannel(c request.CTX, channelID string, userID string) *mode
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
if cresult.NErr != nil {
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)
}
}
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
user := uresult.Data
membersCount := ccresult.Data
if channel.IsGroupOrDirect() {
err := model.NewAppError("LeaveChannel", "api.channel.leave.direct.app_error", nil, "", http.StatusBadRequest)
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 {
return err
}

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

@@ -897,6 +897,17 @@ func TestLeaveChannel(t *testing.T) {
require.Nil(t, appErr)
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) {

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

@@ -419,10 +419,6 @@
"id": "api.channel.leave.direct.app_error",
"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",
"translation": "%v left the channel."