When a user leaves a group, remove them from all group challenges

This commit is contained in:
Cole Gleason
2014-01-04 20:05:20 -06:00
parent d58568c9d8
commit 744d8a6780
4 changed files with 60 additions and 9 deletions

View File

@@ -192,8 +192,8 @@ habitrpg.controller("GroupsCtrl", ['$scope', '$rootScope', 'Groups', '$http', 'A
}]) }])
.controller("GuildsCtrl", ['$scope', 'Groups', 'User', '$rootScope', '$state', '$location', .controller("GuildsCtrl", ['$scope', 'Groups', 'User', '$rootScope', '$state', '$location', '$compile',
function($scope, Groups, User, $rootScope, $state, $location) { function($scope, Groups, User, $rootScope, $state, $location, $compile) {
$scope.groups = { $scope.groups = {
guilds: Groups.myGuilds(), guilds: Groups.myGuilds(),
"public": Groups.publicGuilds() "public": Groups.publicGuilds()
@@ -238,11 +238,12 @@ habitrpg.controller("GroupsCtrl", ['$scope', '$rootScope', 'Groups', '$http', 'A
}) })
} }
$scope.leave = function(group){ $scope.leave = function(keep) {
if (confirm("Are you sure you want to leave this guild?") !== true) { if (keep == 'cancel') {
return; $scope.selectedChal = undefined;
} } else {
Groups.Group.leave({gid: group._id}, undefined, function(){ var group = $scope.selectedGroup;
Groups.Group.leave({gid: group._id, keep:keep}, undefined, function(){
$scope.groups.guilds.splice(_.indexOf($scope.groups.guilds, group), 1); $scope.groups.guilds.splice(_.indexOf($scope.groups.guilds, group), 1);
// remove user from group members if guild is public so that he can re-join it immediately // remove user from group members if guild is public so that he can re-join it immediately
if(group.privacy == 'public' || !group.privacy){ //public guilds with only some fields fetched if(group.privacy == 'public' || !group.privacy){ //public guilds with only some fields fetched
@@ -256,6 +257,23 @@ habitrpg.controller("GroupsCtrl", ['$scope', '$rootScope', 'Groups', '$http', 'A
$state.go('options.social.guilds'); $state.go('options.social.guilds');
}); });
} }
$scope.popoverEl.popover('destroy');
}
$scope.clickLeave = function(group, $event){
$scope.selectedGroup = group;
$scope.popoverEl = $($event.target);
var html = $compile(
'<a ng-controller="GroupsCtrl" ng-click="leave(\'remove-all\')">Remove Tasks</a><br/>\n<a ng-click="leave(\'keep-all\')">Keep Tasks</a><br/>\n<a ng-click="leave(\'cancel\')">Cancel</a><br/>'
)($scope);
$scope.popoverEl.popover('destroy').popover({
html: true,
placement: 'top',
trigger: 'manual',
title: 'Leaving group challenges and...',
content: html
}).popover('show');
}
$scope.reject = function(guild){ $scope.reject = function(guild){
var i = _.findIndex(User.user.invitations.guilds, {id:guild.id}); var i = _.findIndex(User.user.invitations.guilds, {id:guild.id});

View File

@@ -6,6 +6,7 @@ var async = require('async');
var shared = require('habitrpg-shared'); var shared = require('habitrpg-shared');
var User = require('./../models/user').model; var User = require('./../models/user').model;
var Group = require('./../models/group').model; var Group = require('./../models/group').model;
var Challenge = require('./../models/challenge').model;
var api = module.exports; var api = module.exports;
/* /*
@@ -283,6 +284,8 @@ api.join = function(req, res) {
api.leave = function(req, res, next) { api.leave = function(req, res, next) {
var user = res.locals.user, var user = res.locals.user,
group = res.locals.group; group = res.locals.group;
// When removing the user from challenges, should we keep the tasks?
var keep = (/^remove-all/i).test(req.query.keep) ? 'remove-all' : 'keep-all';
async.parallel([ async.parallel([
// Remove active quest from user if they're leaving the party // Remove active quest from user if they're leaving the party
function(cb){ function(cb){
@@ -290,6 +293,36 @@ api.leave = function(req, res, next) {
user.party.quest = Group.cleanQuestProgress(); user.party.quest = Group.cleanQuestProgress();
user.save(cb); user.save(cb);
}, },
// Remove user from group challenges
function(cb){
async.waterfall([
// Find relevant challenges
function(cb) {
Challenge.find({$and:[
{_id: {$in: user.challenges}}, // Challenges I am in
{group: group._id}, // that belong to the group I am leaving
]}, cb);
},
function(challenges, cb) {
// Update each challenge
Challenge.update({_id:{$in: _.pluck(challenges, '_id')}},
{$pull:{members:user._id}},
{multi: true}, function(err) {
if (err) return cb(err);
cb(null, challenges);
});
},
function(challenges, cb) {
// Unlink the challenge tasks from user
async.waterfall(challenges.map(function(chal) {
return function(cb) {
user.unlink({cid:chal._id, keep:keep}, function(err){
if (err) return cb(err);
cb(null);
});
}}), cb);
}], cb);
},
function(cb){ function(cb){
var update = {$pull:{members:user._id}}; var update = {$pull:{members:user._id}};
if (group.type == 'party' && group.quest.key){ if (group.type == 'party' && group.quest.key){

View File

@@ -150,7 +150,7 @@ a.pull-right.gem-wallet(popover-trigger='mouseenter', popover-title='Guild Bank'
input.btn(type='submit', value='Invite') input.btn(type='submit', value='Invite')
a.btn.btn-danger(data-id='{{group.id}}', ng-click='leave(group)') Leave a.btn.btn-danger(data-id='{{group.id}}', ng-click='clickLeave(group, $event)') Leave
.span8 .span8
div.blah-options(ng-show='group._editing') div.blah-options(ng-show='group._editing')

View File

@@ -37,7 +37,7 @@ script(type='text/ng-template', id='partials/options.social.guilds.public.html')
li {{group.memberCount}} member(s) li {{group.memberCount}} member(s)
li li
// join / leave // join / leave
a.btn.btn-small.btn-danger(ng-show='group._isMember', ng-click='leave(group)') a.btn.btn-small.btn-danger(ng-show='group._isMember', ng-click='clickLeave(group, $event)')
i.icon-ban-circle i.icon-ban-circle
| Leave | Leave
a.btn.btn-small.btn-success(ng-hide='group._isMember', ng-click='join(group)') a.btn.btn-small.btn-success(ng-hide='group._isMember', ng-click='join(group)')