2013-02-16 03:30:27 +00:00
|
|
|
/**
|
|
|
|
|
* Set this up as a midnight cron script
|
|
|
|
|
*
|
2013-06-14 23:19:08 +00:00
|
|
|
* mongo habitrpg ./node_modules/moment/moment.js 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
|
|
|
|
2013-06-15 16:01:03 +00:00
|
|
|
var today = +new Date;
|
2013-06-14 21:01:50 +00:00
|
|
|
|
2013-06-15 16:01:03 +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)
|
|
|
|
|
*/
|
|
|
|
|
db.users.find({
|
2013-06-14 21:01:50 +00:00
|
|
|
|
2013-06-15 16:01:03 +00:00
|
|
|
// Un-registered users
|
|
|
|
|
"auth.local": {$exists: false},
|
|
|
|
|
"auth.facebook": {$exists: false}
|
2013-02-12 06:52:37 +00:00
|
|
|
|
2013-06-15 16:01:03 +00:00
|
|
|
}).forEach(function(user) {
|
2013-06-14 20:09:26 +00:00
|
|
|
//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});
|
2013-02-12 06:52:37 +00:00
|
|
|
}
|
|
|
|
|
} else {
|
2013-06-14 20:09:26 +00:00
|
|
|
return db.users.update({_id: user._id}, {$set: {'lastCron': today}});
|
2013-02-12 06:52:37 +00:00
|
|
|
}
|
|
|
|
|
});
|
2013-02-16 03:30:27 +00:00
|
|
|
|
2013-06-15 16:01:03 +00:00
|
|
|
/**
|
|
|
|
|
* Remove empty parties
|
|
|
|
|
*/
|
|
|
|
|
db.users.groups.remove({
|
|
|
|
|
// Empty Parties
|
|
|
|
|
$where: function(){ return this.type === 'party' && this.members.length === 0; }
|
|
|
|
|
});
|
2013-06-14 23:05:26 +00:00
|
|
|
|
2013-06-15 16:01:03 +00:00
|
|
|
/**
|
|
|
|
|
* Preen history for users with > 7 history entries
|
|
|
|
|
*/
|
2013-06-14 23:05:26 +00:00
|
|
|
function preenHistory(history) {
|
|
|
|
|
var newHistory = [];
|
|
|
|
|
function preen(amount, format) {
|
2013-06-15 16:01:03 +00:00
|
|
|
var groups, avg, start;
|
|
|
|
|
groups = _(history)
|
2013-06-14 23:05:26 +00:00
|
|
|
.groupBy(function(h){ return moment(h.date).format(format) })
|
2013-06-15 16:01:03 +00:00
|
|
|
.sortBy(function(h,k) {return k;}) // TODO make sure this is the right order
|
|
|
|
|
.value()
|
|
|
|
|
start = (groups.length - amount > 0) ? groups.length - amount : 0;
|
|
|
|
|
groups = groups.slice(start, groups.length - 1)
|
|
|
|
|
_.each(groups, function(group){
|
|
|
|
|
avg = _.reduce(group, function(mem, obj){ return mem + obj.value }, 0) / group.length;
|
|
|
|
|
newHistory.push({date: +moment(group[0].date), value: avg});
|
|
|
|
|
})
|
2013-02-12 06:52:37 +00:00
|
|
|
}
|
2013-02-16 03:30:27 +00:00
|
|
|
|
2013-06-15 16:01:03 +00:00
|
|
|
preen(50, 'YYYY', 'YYYY'); // last 50 years
|
|
|
|
|
preen(12, 'YYYYMM', 'MMM YYYY'); // last 12 months
|
|
|
|
|
preen(4, 'YYYYww', 'WW YYYY'); // last 4 weeks
|
|
|
|
|
newHistory = newHistory.concat(history.slice(-7)); // last 7 days
|
2013-06-14 23:05:26 +00:00
|
|
|
return newHistory;
|
|
|
|
|
}
|
2013-06-14 21:01:50 +00:00
|
|
|
|
2013-06-14 23:05:26 +00:00
|
|
|
db.users.find({
|
2013-06-15 16:01:03 +00:00
|
|
|
|
|
|
|
|
// Registered users with > 7 history entries
|
|
|
|
|
$or: [
|
|
|
|
|
{ 'auth.local': { $exists: true }},
|
|
|
|
|
{ 'auth.facebook': { $exists: true }}
|
|
|
|
|
],
|
2013-06-14 23:05:26 +00:00
|
|
|
$where: function(){ return this.history && this.history.exp && this.history.exp.length > 7; }
|
2013-06-14 21:01:50 +00:00
|
|
|
|
2013-06-14 23:05:26 +00:00
|
|
|
}).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);
|
|
|
|
|
})
|
|
|
|
|
|
2013-06-15 16:01:03 +00:00
|
|
|
update['$set']['history.exp'] = preenHistory(user.history.exp);
|
|
|
|
|
update['$set']['history.todos'] = preenHistory(user.history.todos);
|
2013-06-14 23:05:26 +00:00
|
|
|
|
2013-06-15 16:01:03 +00:00
|
|
|
db.users.update({_id: user._id}, update);
|
2013-06-14 23:05:26 +00:00
|
|
|
});
|
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});
|
|
|
|
|
}
|
|
|
|
|
});*/
|