diff options
-rw-r--r-- | api/channel.go | 11 | ||||
-rw-r--r-- | api/channel_test.go | 6 | ||||
-rw-r--r-- | i18n/en.json | 4 | ||||
-rw-r--r-- | i18n/es.json | 4 | ||||
-rw-r--r-- | webapp/components/channel_header.jsx | 3 | ||||
-rw-r--r-- | webapp/components/navbar.jsx | 31 |
6 files changed, 42 insertions, 17 deletions
diff --git a/api/channel.go b/api/channel.go index b63e44017..b7a608717 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 69902c3ad..6a907b278 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 432279c23..8a1634a4f 100644 --- a/i18n/en.json +++ b/i18n/en.json @@ -224,6 +224,10 @@ "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 d8f9756fb..c93581ccb 100644 --- a/i18n/es.json +++ b/i18n/es.json @@ -224,6 +224,10 @@ "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 992244915..ca3878d68 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( <li key='leave_channel' diff --git a/webapp/components/navbar.jsx b/webapp/components/navbar.jsx index 21ca53649..ee199fc03 100644 --- a/webapp/components/navbar.jsx +++ b/webapp/components/navbar.jsx @@ -218,20 +218,23 @@ export default class Navbar extends React.Component { </li> ); - leaveChannelOption = ( - <li role='presentation'> - <a - role='menuitem' - href='#' - onClick={this.handleLeave} - > - <FormattedMessage - id='navbar.leave' - defaultMessage='Leave Channel' - /> - </a> - </li> - ); + const canLeave = channel.type === Constants.PRIVATE_CHANNEL ? this.state.userCount > 1 : true; + if (canLeave) { + leaveChannelOption = ( + <li role='presentation'> + <a + role='menuitem' + href='#' + onClick={this.handleLeave} + > + <FormattedMessage + id='navbar.leave' + defaultMessage='Leave Channel' + /> + </a> + </li> + ); + } } var manageMembersOption; |