|
|
@ -1,7 +1,7 @@ |
|
|
|
'use strict'; |
|
|
|
'use strict'; |
|
|
|
|
|
|
|
|
|
|
|
const Mongo = require(__dirname+'/../../db/db.js') |
|
|
|
const Mongo = require(__dirname+'/../../db/db.js') |
|
|
|
, { Posts } = require(__dirname+'/../../db/') |
|
|
|
, { Posts, Modlogs } = require(__dirname+'/../../db/') |
|
|
|
, config = require(__dirname+'/../../lib/misc/config.js') |
|
|
|
, config = require(__dirname+'/../../lib/misc/config.js') |
|
|
|
, { createHash, randomBytes } = require('crypto') |
|
|
|
, { createHash, randomBytes } = require('crypto') |
|
|
|
, timeUtils = require(__dirname+'/../../lib/converter/timeutils.js'); |
|
|
|
, timeUtils = require(__dirname+'/../../lib/converter/timeutils.js'); |
|
|
@ -9,24 +9,20 @@ const Mongo = require(__dirname+'/../../db/db.js') |
|
|
|
module.exports = { |
|
|
|
module.exports = { |
|
|
|
|
|
|
|
|
|
|
|
func: async (days) => { |
|
|
|
func: async (days) => { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//make a mongoid set to the date before which we prunt IPs on, because _id holds date and is always indexed
|
|
|
|
const beforeDate = new Date(); |
|
|
|
const beforeDate = new Date(); |
|
|
|
beforeDate.setDate(beforeDate.getDate() - (days || config.get.pruneIps)); |
|
|
|
beforeDate.setDate(beforeDate.getDate() - (days || config.get.pruneIps)); |
|
|
|
const beforeDateMongoId = Mongo.ObjectId.createFromTime(Math.floor(beforeDate.getTime()/1000)); |
|
|
|
const beforeDateMongoId = Mongo.ObjectId.createFromTime(Math.floor(beforeDate.getTime()/1000)); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//use random secret for this schedule run and generate the bulkwrite for pruning IP
|
|
|
|
const tempIpHashSecret = randomBytes(20).toString('base64'); |
|
|
|
const tempIpHashSecret = randomBytes(20).toString('base64'); |
|
|
|
const bulkWrites = []; |
|
|
|
const mapBulkWrite = (document) => { |
|
|
|
await Posts.db.find({ |
|
|
|
const randomIP = createHash('sha256').update(tempIpHashSecret + document.ip.cloak).digest('base64'); |
|
|
|
_id: { |
|
|
|
return { |
|
|
|
$lte: beforeDateMongoId, |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
'ip.pruned': { |
|
|
|
|
|
|
|
$ne: true |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}).forEach(post => { |
|
|
|
|
|
|
|
const randomIP = createHash('sha256').update(tempIpHashSecret + post.ip.cloak).digest('base64'); |
|
|
|
|
|
|
|
bulkWrites.push({ |
|
|
|
|
|
|
|
updateOne: { |
|
|
|
updateOne: { |
|
|
|
filter: { |
|
|
|
filter: { |
|
|
|
_id: post._id, |
|
|
|
_id: document._id, |
|
|
|
}, |
|
|
|
}, |
|
|
|
update: { |
|
|
|
update: { |
|
|
|
$set: { |
|
|
|
$set: { |
|
|
@ -36,13 +32,32 @@ module.exports = { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
}); |
|
|
|
}; |
|
|
|
}); |
|
|
|
}; |
|
|
|
console.log(`Randomising ip on ${bulkWrites.length} posts`); |
|
|
|
|
|
|
|
|
|
|
|
const pruneQuery = { |
|
|
|
|
|
|
|
'_id': { |
|
|
|
|
|
|
|
'$lte': beforeDateMongoId, |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
'ip.pruned': { |
|
|
|
|
|
|
|
'$ne': true |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//No need for promise.all
|
|
|
|
|
|
|
|
[Posts, Modlogs].forEach(async (coll, i) => { |
|
|
|
|
|
|
|
const bulkWrites = (await coll.db |
|
|
|
|
|
|
|
.find(pruneQuery) |
|
|
|
|
|
|
|
.toArray()) |
|
|
|
|
|
|
|
.map(mapBulkWrite); |
|
|
|
if (bulkWrites.length > 0) { |
|
|
|
if (bulkWrites.length > 0) { |
|
|
|
await Posts.db.bulkWrite(bulkWrites); |
|
|
|
await coll.db.bulkWrite(bulkWrites); |
|
|
|
|
|
|
|
console.log(`Randomised ips on ${bulkWrites.length} ${i === 0 ? 'posts' : 'modlog entries'}`); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
}, |
|
|
|
}, |
|
|
|
|
|
|
|
|
|
|
|
interval: timeUtils.DAY, |
|
|
|
interval: timeUtils.DAY, |
|
|
|
immediate: true, |
|
|
|
immediate: true, |
|
|
|
condition: 'pruneIps', |
|
|
|
condition: 'pruneIps', |
|
|
|