jschan - Anonymous imageboard software. Classic look, modern features and feel. Works without JavaScript and supports Tor, I2P, Lokinet, etc.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

101 lines
1.6 KiB

'use strict';
const Mongo = require(__dirname+'/db.js')
, db = Mongo.client.db('jschan').collection('poststats');
module.exports = {
db,
updateOne: (board, ip, thread) => {
return db.updateOne({
'board': board,
'hour': new Date().getHours()
}, {
'$inc': {
'pph': 1,
'tph': thread ? 1 : 0
},
'$addToSet': {
'ips': ip
}
}, {
'upsert': true
});
},
getHourPosts: (board) => {
return db.findOne({
'board': board,
'hour': new Date().getHours()
}, {
'projection': {
'_id': 0,
'pph': 1,
'tph': 1
}
})
},
updateBoards: () => {
//todo: improve this query
return db.aggregate([{
'$unwind': {
'path': '$ips',
'preserveNullAndEmptyArrays': true
}
}, {
'$group': {
'_id': '$board',
'pph': {
'$max': '$pph' //use max since only one will have a value until we do a multi facet system that can $merge (dunno if even possible)
},
'ips': {
'$addToSet': '$ips'
}
}
}, {
'$project': {
'ips': {
'$size': '$ips'
},
'pph': 1
}
}, {
'$merge': {
'into': 'boards'
}
}]).toArray();
},
//reset stats, used at start of each hour
resetStats: () => {
return Promise.all([
db.updateMany({
'hour': new Date().getHours()
}, {
'$set': {
'ips': [],
}
}),
db.updateMany({}, {
'$set': {
'pph': 0,
'tph': 0
}
}),
]);
},
deleteBoard: (board) => {
return db.deleteMany({
'board': board
});
},
deleteAll: () => {
return db.deleteMany({});
},
}