2013-02-16 03:30:27 +00:00
|
|
|
/**
|
|
|
|
|
* Set this up as a midnight cron script
|
|
|
|
|
*
|
2013-06-14 21:01:50 +00:00
|
|
|
* mongo habitrpg migrations/preen_cron.js
|
2013-02-16 03:30:27 +00:00
|
|
|
*/
|
|
|
|
|
|
2013-06-14 23:05:26 +00:00
|
|
|
load('./node_modules/lodash/lodash.js');
|
2013-06-14 21:01:50 +00:00
|
|
|
load('./node_modules/moment/moment.js');
|
2013-02-12 06:52:37 +00:00
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
Users are allowed to experiment with the site before registering. Every time a new browser visits habitrpg, a new
|
|
|
|
|
"staged" account is created - and if the user later registeres, that staged account is considered a "production" account.
|
|
|
|
|
This function removes all staged accounts that have been abandoned - either older than a month, or corrupted in some way (lastCron==undefined)
|
|
|
|
|
*/
|
|
|
|
|
|
2013-06-14 21:01:50 +00:00
|
|
|
var
|
|
|
|
|
today = +new Date,
|
|
|
|
|
|
|
|
|
|
un_registered = {
|
2013-05-21 11:53:05 +00:00
|
|
|
"auth.local": {$exists: false},
|
|
|
|
|
"auth.facebook": {$exists: false}
|
|
|
|
|
},
|
2013-06-14 21:01:50 +00:00
|
|
|
|
|
|
|
|
emptyParties = {
|
|
|
|
|
$where: function(){
|
|
|
|
|
return this.type === 'party' && this.members.length === 0;
|
|
|
|
|
}
|
|
|
|
|
};
|
2013-02-12 06:52:37 +00:00
|
|
|
|
2013-06-14 23:05:26 +00:00
|
|
|
//db.users.find(un_registered).forEach(function(user) {
|
|
|
|
|
// //if (!user) return;
|
|
|
|
|
// var lastCron = user.lastCron;
|
|
|
|
|
// if (lastCron && moment(lastCron).isValid()) {
|
|
|
|
|
// if (Math.abs(moment(today).diff(lastCron, 'days')) > 5) {
|
|
|
|
|
// return db.users.remove({_id: user._id});
|
|
|
|
|
// }
|
|
|
|
|
// } else {
|
|
|
|
|
// return db.users.update({_id: user._id}, {$set: {'lastCron': today}});
|
|
|
|
|
// }
|
|
|
|
|
//});
|
|
|
|
|
|
|
|
|
|
//db.users.groups.remove(emptyParties);
|
|
|
|
|
|
|
|
|
|
//FIXME make sure this doesn't conflict with preen un_registered above
|
|
|
|
|
|
|
|
|
|
function preenHistory(history) {
|
|
|
|
|
var newHistory = [];
|
|
|
|
|
function preen(amount, format) {
|
|
|
|
|
var group, sliced, avg;
|
|
|
|
|
group = _(history)
|
|
|
|
|
.groupBy(function(h){ return moment(h.date).format(format) })
|
|
|
|
|
.sortBy(function(h,k) {return k;}); // TODO make sure this is the right order
|
|
|
|
|
if (_.size(group) === 0) return;
|
|
|
|
|
sliced = _.toArray(group).slice(_.size(group) - 1, -amount);
|
|
|
|
|
avg = _.reduce(sliced, function(mem, obj){ return mem + obj.value }) / _.size(group);
|
|
|
|
|
newHistory.concat({date: sliced[0].date, value: avg});
|
2013-02-12 06:52:37 +00:00
|
|
|
}
|
2013-02-16 03:30:27 +00:00
|
|
|
|
2013-06-14 23:05:26 +00:00
|
|
|
preen(50,'YYYY'); // last 50 years
|
|
|
|
|
preen(12,'MMYYYY'); // last 12 months
|
|
|
|
|
preen(4,'wYYYY'); // last 4 weeks
|
|
|
|
|
newHistory.concat(history.slice(-7)); // last 7 days
|
|
|
|
|
return newHistory;
|
|
|
|
|
}
|
2013-06-14 21:01:50 +00:00
|
|
|
|
2013-06-14 23:05:26 +00:00
|
|
|
db.users.find({
|
|
|
|
|
$where: function(){ return this.history && this.history.exp && this.history.exp.length > 7; }
|
|
|
|
|
}).forEach(function(user) {
|
|
|
|
|
var update = {$set:{}};
|
|
|
|
|
|
|
|
|
|
_.each(user.tasks, function(task) {
|
|
|
|
|
if (task.type === 'habit' || task.type === 'daily')
|
|
|
|
|
update['$set']['tasks.' + task.id + '.history'] = preenHistory(task.history);
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// TODO user.history.exp, user.history.todos
|
|
|
|
|
|
|
|
|
|
if (!_.isEmpty(update['$set'])) db.users.update({_id:user.id}, update);
|
|
|
|
|
});
|
2013-02-16 03:30:27 +00:00
|
|
|
|
2013-04-04 21:38:31 +00:00
|
|
|
/**
|
|
|
|
|
* Don't remove missing user auths anymore. This was previously necessary due to data corruption,
|
|
|
|
|
* revisit if needs be
|
|
|
|
|
*/
|
|
|
|
|
/*db.sessions.find().forEach(function(sess){
|
2013-05-21 11:53:05 +00:00
|
|
|
var uid = JSON.parse(sess.session).userId;
|
|
|
|
|
if (!uid || db.users.count({_id:uid}) === 0) {
|
|
|
|
|
db.sessions.remove({_id:sess._id});
|
|
|
|
|
}
|
|
|
|
|
});*/
|