From 45b22f312d3f57c63f86ffdbbb50c29108099993 Mon Sep 17 00:00:00 2001 From: enahum Date: Tue, 10 May 2016 09:24:52 -0300 Subject: [PATCH] PLT-2674 Private Group should not have Leave option when only one member remains (#2888) --- api/channel.go | 11 ++++++++++ api/channel_test.go | 6 ++++-- i18n/en.json | 4 ++++ i18n/es.json | 4 ++++ webapp/components/channel_header.jsx | 3 ++- webapp/components/navbar.jsx | 31 +++++++++++++++------------- 6 files changed, 42 insertions(+), 17 deletions(-) diff --git a/api/channel.go b/api/channel.go index b63e44017a..b7a608717f 100644 --- a/api/channel.go +++ b/api/channel.go @@ -576,6 +576,7 @@ func leave(c *Context, w http.ResponseWriter, r *http.Request) { sc := Srv.Store.Channel().Get(id) uc := Srv.Store.User().Get(c.Session.UserId) + ccm := Srv.Store.Channel().GetMemberCount(id) if cresult := <-sc; cresult.Err != nil { c.Err = cresult.Err @@ -583,9 +584,13 @@ func leave(c *Context, w http.ResponseWriter, r *http.Request) { } else if uresult := <-uc; uresult.Err != nil { c.Err = cresult.Err return + } else if ccmresult := <-ccm; ccmresult.Err != nil { + c.Err = ccmresult.Err + return } else { channel := cresult.Data.(*model.Channel) user := uresult.Data.(*model.User) + membersCount := ccmresult.Data.(int64) if !c.HasPermissionsToTeam(channel.TeamId, "leave") { return @@ -597,6 +602,12 @@ func leave(c *Context, w http.ResponseWriter, r *http.Request) { return } + if channel.Type == model.CHANNEL_PRIVATE && membersCount == 1 { + c.Err = model.NewLocAppError("leave", "api.channel.leave.last_member.app_error", nil, "userId="+user.Id) + c.Err.StatusCode = http.StatusBadRequest + return + } + if channel.Name == model.DEFAULT_CHANNEL { c.Err = model.NewLocAppError("leave", "api.channel.leave.default.app_error", map[string]interface{}{"Channel": model.DEFAULT_CHANNEL}, "") c.Err.StatusCode = http.StatusBadRequest diff --git a/api/channel_test.go b/api/channel_test.go index 69902c3ad4..6a907b2786 100644 --- a/api/channel_test.go +++ b/api/channel_test.go @@ -485,8 +485,10 @@ func TestLeaveChannel(t *testing.T) { Client.Must(Client.JoinChannel(channel1.Id)) - // No error if you leave a channel you cannot see - Client.Must(Client.LeaveChannel(channel3.Id)) + // Cannot leave a the private group if you are the only member + if _, err := Client.LeaveChannel(channel3.Id); err == nil { + t.Fatal("should have errored, cannot leave private group if only one member") + } rchannel := Client.Must(Client.CreateDirectChannel(th.BasicUser.Id)).Data.(*model.Channel) diff --git a/i18n/en.json b/i18n/en.json index 432279c23d..8a1634a4f9 100644 --- a/i18n/en.json +++ b/i18n/en.json @@ -223,6 +223,10 @@ "id": "api.channel.leave.direct.app_error", "translation": "Cannot leave a direct message channel" }, + { + "id": "api.channel.leave.last_member.app_error", + "translation": "You're the only member left, try removing the Private Group instead of leaving." + }, { "id": "api.channel.leave.left", "translation": "%v has left the channel." diff --git a/i18n/es.json b/i18n/es.json index d8f9756fb0..c93581ccb4 100644 --- a/i18n/es.json +++ b/i18n/es.json @@ -223,6 +223,10 @@ "id": "api.channel.leave.direct.app_error", "translation": "No puedes dejar un mensaje directo a un canal" }, + { + "id": "api.channel.leave.last_member.app_error", + "translation": "Eres el Ășltimo miembro que queda, intenta remover el Grupo Privado en vez de salirte." + }, { "id": "api.channel.leave.left", "translation": "%v ha abandonado el canal." diff --git a/webapp/components/channel_header.jsx b/webapp/components/channel_header.jsx index 9922449151..ca3878d682 100644 --- a/webapp/components/channel_header.jsx +++ b/webapp/components/channel_header.jsx @@ -409,7 +409,8 @@ export default class ChannelHeader extends React.Component { } } - if (!ChannelStore.isDefault(channel)) { + const canLeave = channel.type === Constants.PRIVATE_CHANNEL ? this.state.userCount > 1 : true; + if (!ChannelStore.isDefault(channel) && canLeave) { dropdownContents.push(
  • ); - leaveChannelOption = ( -
  • - - - -
  • - ); + const canLeave = channel.type === Constants.PRIVATE_CHANNEL ? this.state.userCount > 1 : true; + if (canLeave) { + leaveChannelOption = ( +
  • + + + +
  • + ); + } } var manageMembersOption;