mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-19 07:37:25 +01:00
When a user leaves a group, remove them from all group challenges
This commit is contained in:
@@ -192,8 +192,8 @@ habitrpg.controller("GroupsCtrl", ['$scope', '$rootScope', 'Groups', '$http', 'A
|
||||
|
||||
}])
|
||||
|
||||
.controller("GuildsCtrl", ['$scope', 'Groups', 'User', '$rootScope', '$state', '$location',
|
||||
function($scope, Groups, User, $rootScope, $state, $location) {
|
||||
.controller("GuildsCtrl", ['$scope', 'Groups', 'User', '$rootScope', '$state', '$location', '$compile',
|
||||
function($scope, Groups, User, $rootScope, $state, $location, $compile) {
|
||||
$scope.groups = {
|
||||
guilds: Groups.myGuilds(),
|
||||
"public": Groups.publicGuilds()
|
||||
@@ -238,11 +238,12 @@ habitrpg.controller("GroupsCtrl", ['$scope', '$rootScope', 'Groups', '$http', 'A
|
||||
})
|
||||
}
|
||||
|
||||
$scope.leave = function(group){
|
||||
if (confirm("Are you sure you want to leave this guild?") !== true) {
|
||||
return;
|
||||
}
|
||||
Groups.Group.leave({gid: group._id}, undefined, function(){
|
||||
$scope.leave = function(keep) {
|
||||
if (keep == 'cancel') {
|
||||
$scope.selectedChal = undefined;
|
||||
} else {
|
||||
var group = $scope.selectedGroup;
|
||||
Groups.Group.leave({gid: group._id, keep:keep}, undefined, function(){
|
||||
$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
|
||||
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');
|
||||
});
|
||||
}
|
||||
$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){
|
||||
var i = _.findIndex(User.user.invitations.guilds, {id:guild.id});
|
||||
|
||||
@@ -6,6 +6,7 @@ var async = require('async');
|
||||
var shared = require('habitrpg-shared');
|
||||
var User = require('./../models/user').model;
|
||||
var Group = require('./../models/group').model;
|
||||
var Challenge = require('./../models/challenge').model;
|
||||
var api = module.exports;
|
||||
|
||||
/*
|
||||
@@ -283,6 +284,8 @@ api.join = function(req, res) {
|
||||
api.leave = function(req, res, next) {
|
||||
var user = res.locals.user,
|
||||
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([
|
||||
// Remove active quest from user if they're leaving the party
|
||||
function(cb){
|
||||
@@ -290,6 +293,36 @@ api.leave = function(req, res, next) {
|
||||
user.party.quest = Group.cleanQuestProgress();
|
||||
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){
|
||||
var update = {$pull:{members:user._id}};
|
||||
if (group.type == 'party' && group.quest.key){
|
||||
|
||||
@@ -150,7 +150,7 @@ a.pull-right.gem-wallet(popover-trigger='mouseenter', popover-title='Guild Bank'
|
||||
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
|
||||
div.blah-options(ng-show='group._editing')
|
||||
|
||||
@@ -37,7 +37,7 @@ script(type='text/ng-template', id='partials/options.social.guilds.public.html')
|
||||
li {{group.memberCount}} member(s)
|
||||
li
|
||||
// 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
|
||||
| Leave
|
||||
a.btn.btn-small.btn-success(ng-hide='group._isMember', ng-click='join(group)')
|
||||
|
||||
Reference in New Issue
Block a user