[#1444] implement delete-tag (incl server route). run tags-cleanup migration

This commit is contained in:
Tyler Renelle
2013-09-09 00:04:02 -04:00
parent f99b496a29
commit 4caec6f737
6 changed files with 58 additions and 27 deletions

View File

@@ -0,0 +1,16 @@
//mongo habitrpg ./node_modules/lodash/lodash.js migrations/20130908_cleanup_corrupt_tags.js
// Racer was notorious for adding duplicates, randomly deleting documents, etc. Once we pull the plug on old.habit,
// run this migration to cleanup all the corruption
db.users.find().forEach(function(user){
user.tags = _.filter(user.tags, (function(t) {
return !!t ? t.id : false;
}));
try {
db.users.update({_id:user._id}, {$set:{tags:user.tags}});
} catch(e) {
print(e);
}
})

View File

@@ -1,7 +1,7 @@
"use strict";
habitrpg.controller("FiltersCtrl", ['$scope', '$rootScope', 'User',
function($scope, $rootScope, User) {
habitrpg.controller("FiltersCtrl", ['$scope', '$rootScope', 'User', 'API_URL', '$http',
function($scope, $rootScope, User, API_URL, $http) {
var user = User.user;
$scope._editing = false;
@@ -30,29 +30,17 @@ habitrpg.controller("FiltersCtrl", ['$scope', '$rootScope', 'User',
};
// $scope.remove = function(tag, $index){
//
// /*
// something got corrupted, let's clear the corrupt tags
// FIXME we can remove this once Angular has been live for a while
// */
// if (!tag.id) {
// user.tags = _.filter(user.tags, (function(t) {
// return t != null ? t.id : false;
// }));
// user.filters = {};
// return;
// }
//
// delete user.filters[tag.id];
//
// splice(user.tags,$index,1);
//
// // remove tag from all tasks
// _.each(user.tasks, function(task) {
// delete user.tasks[task.id].tags[tag.id];
// });
//
// }
$scope['delete'] = function(tag, $index){
delete user.filters[tag.id];
user.tags.splice($index,1);
// remove tag from all tasks
_.each(user.tasks, function(task) {
delete user.tasks[task.id].tags[tag.id];
});
$http['delete'](API_URL + '/api/v1/user/tags/' + tag.id)
.error(function(data){
alert(data);
})
}
}]);

View File

@@ -594,6 +594,30 @@ api.buyGems = function(req, res) {
});
};
/*
------------------------------------------------------------------------
Tags
------------------------------------------------------------------------
*/
api.deleteTag = function(req, res){
var user = res.locals.user;
var i = _.findIndex(user.tags, {id:req.params.tid});
if (~i) {
var tag = user.tags[i];
delete user.filters[tid];
user.tags.splice(i,1);
// remove tag from all tasks
_.each(user.tasks, function(task) {
delete user.tasks[task.id].tags[tag.id];
});
user.save(function(err,saved){
res.send(200);
})
} else {
res.json(400, {err:'Tag not found'});
}
}
/*
------------------------------------------------------------------------
Batch Update

View File

@@ -50,6 +50,9 @@ router.post('/user/buy-gems', auth.auth, user.buyGems);
router.post('/user/reset', auth.auth, user.reset);
router['delete']('/user', auth.auth, user['delete']);
/* Tags */
router['delete']('/user/tags/:tid', auth.auth, user.deleteTag);
/* Groups*/
router.get('/groups', auth.auth, groups.getGroups);
router.post('/groups', auth.auth, groups.createGroup);

View File

@@ -9,7 +9,7 @@
.input-append.option-group.tag-editing(ng-show='_editing && !tag.challenge')
input.input.input-small.option-content.tag-editing-pill(type='text', ng-model='tag.name')
span.add-on.tag-editing-pill
a.pull-right(x-bind='remove(tag,$index)', ng-click='notPorted()')
a.pull-right(ng-click='delete(tag,$index)')
i.icon-trash
a(ng-hide='_editing && !tag.challenge', data-tag-id='{{tag.id}}', ng-click='toggleFilter(tag)') {{tag.name}}
li