diff --git a/common/locales/en/groups.json b/common/locales/en/groups.json index ef40858a8d..1055e6b428 100644 --- a/common/locales/en/groups.json +++ b/common/locales/en/groups.json @@ -30,6 +30,9 @@ "invite": "Invite", "leave": "Leave", "invitedTo": "Invited to <%= name %>", + "invitedToNewParty": "You were invited to join a party! Do you want to leave this party and join <%= partyName %>?", + "joinNewParty": "Join New Party", + "declineInvitation": "Decline Invitation", "newMsg": "New message in \"<%= name %>\"", "chat": "Chat", "sendChat": "Send Chat", diff --git a/test/spec/controllers/partyCtrlSpec.js b/test/spec/controllers/partyCtrlSpec.js index edb8c8dfba..4a08689bc3 100644 --- a/test/spec/controllers/partyCtrlSpec.js +++ b/test/spec/controllers/partyCtrlSpec.js @@ -172,4 +172,42 @@ describe("Party Controller", function() { expect(rootScope.$state.go).to.be.calledWith('options.inventory.quests'); }); }); + + describe('#leaveOldPartyAndJoinNewParty', function() { + beforeEach(function() { + sandbox.stub(scope, 'join'); + sandbox.stub(groups.Group, 'leave').yields(); + sandbox.stub(groups, 'party').returns({ + _id: 'old-party' + }); + sandbox.stub(window, 'confirm').returns(true); + }); + + it('does nothing if user declines confirmation', function() { + window.confirm.returns(false); + scope.leaveOldPartyAndJoinNewParty('some-id', 'some-name'); + + expect(groups.Group.leave).to.not.be.called; + }) + + it('leaves user\'s current party', function() { + scope.leaveOldPartyAndJoinNewParty('some-id', 'some-name'); + + expect(groups.Group.leave).to.be.calledOnce; + expect(groups.Group.leave).to.be.calledWith({ + gid: 'old-party', + keep: false + }); + }); + + it('joins the new party', function() { + scope.leaveOldPartyAndJoinNewParty('some-id', 'some-name'); + + expect(scope.join).to.be.calledOnce; + expect(scope.join).to.be.calledWith({ + id: 'some-id', + name: 'some-name' + }); + }); + }); }); diff --git a/website/public/js/controllers/partyCtrl.js b/website/public/js/controllers/partyCtrl.js index ca179a5c71..d288c0a57c 100644 --- a/website/public/js/controllers/partyCtrl.js +++ b/website/public/js/controllers/partyCtrl.js @@ -88,8 +88,15 @@ habitrpg.controller("PartyCtrl", ['$rootScope','$scope','Groups','Chat','User',' } }; + $scope.leaveOldPartyAndJoinNewParty = function(newPartyId, newPartyName) { + if (confirm('Are you sure you want to delete your party and join ' + newPartyName + '?')) { + Groups.Group.leave({gid: Groups.party()._id, keep:false}, undefined, function() { + $scope.join({ id: newPartyId, name: newPartyName }); + }); + } + } + $scope.reject = function(){ - //User.user.invitations.party = undefined; User.set({'invitations.party':{}}); } diff --git a/website/src/controllers/groups.js b/website/src/controllers/groups.js index 19e0409ed1..8443a905b8 100644 --- a/website/src/controllers/groups.js +++ b/website/src/controllers/groups.js @@ -577,8 +577,9 @@ var inviteByUUIDs = function(uuids, group, req, res, next){ return cb({code:400,err:"User already pending invitation."}); Group.find({type:'party', members:{$in:[uuid]}}, function(err, groups){ if (err) return cb(err); - if (!_.isEmpty(groups)) - return cb({code:400,err:"User already in a party."}) + if (!_.isEmpty(groups) && groups[0].members.length > 1) { + return cb({code:400, err:"User already in a party."}) + } sendInvite(); }); } diff --git a/website/views/options/social/leave-party-and-join-another.jade b/website/views/options/social/leave-party-and-join-another.jade new file mode 100644 index 0000000000..3a0ec1d8c4 --- /dev/null +++ b/website/views/options/social/leave-party-and-join-another.jade @@ -0,0 +1,8 @@ +- var newParty = 'User.user.invitations.party' +.containter-fulid(ng-if='#{newParty}.id && party._id') + .row.text-center + .col-sm-6.col-sm-offset-3.alert.alert-warning + p {{::env.t('invitedToNewParty', { partyName: #{newParty}.name })}} + p + button.btn.btn-success(ng-click='leaveOldPartyAndJoinNewParty(#{newParty}.id, #{newParty}.name)')=env.t('joinNewParty') + button.btn.btn-default(ng-click='reject()')=env.t('declineInvitation') diff --git a/website/views/options/social/party.jade b/website/views/options/social/party.jade index ded8fff16f..f7580f1deb 100644 --- a/website/views/options/social/party.jade +++ b/website/views/options/social/party.jade @@ -2,6 +2,7 @@ include ../../shared/avatar/generated_avatar script(type='text/ng-template', id='partials/options.social.party.html') div(ng-if='group._id') + include ./leave-party-and-join-another include ./group div(ng-if='!group._id') div(ng-show='user.invitations.party.id').container-fluid