mirror of https://gitgud.io/fatchan/jschan.git
commit
5870e35ec3
12 changed files with 266 additions and 9 deletions
@ -0,0 +1,16 @@ |
|||||||
|
'use strict'; |
||||||
|
|
||||||
|
const timeUtils = require(__dirname+'/../lib/converter/timeutils.js'); |
||||||
|
|
||||||
|
module.exports = async(db, redis) => { |
||||||
|
console.log('add inactive account and board auto handling'); |
||||||
|
await db.collection('globalsettings').updateOne({ _id: 'globalsettings' }, { |
||||||
|
'$set': { |
||||||
|
inactiveAccountTime: timeUtils.MONTH * 3, |
||||||
|
inactiveAccountAction: 0, //no actions by default
|
||||||
|
abandonedBoardAction: 0, |
||||||
|
}, |
||||||
|
}); |
||||||
|
console.log('Clearing globalsettings cache'); |
||||||
|
await redis.deletePattern('globalsettings'); |
||||||
|
}; |
@ -0,0 +1,44 @@ |
|||||||
|
'use strict'; |
||||||
|
|
||||||
|
const { debugLogs } = require(__dirname+'/../../configs/secrets.js') |
||||||
|
, config = require(__dirname+'/../../lib/misc/config.js') |
||||||
|
, { Boards } = require(__dirname+'/../../db/') |
||||||
|
, timeUtils = require(__dirname+'/../../lib/converter/timeutils.js') |
||||||
|
, deleteBoard = require(__dirname+'/../../models/forms/deleteboard.js'); |
||||||
|
|
||||||
|
module.exports = { |
||||||
|
|
||||||
|
func: async () => { |
||||||
|
|
||||||
|
if (config.get.abandonedBoardAction === 0) { |
||||||
|
return; |
||||||
|
} |
||||||
|
|
||||||
|
const abandonedBoards = await Boards.getAbandoned(config.get.abandonedBoardAction); |
||||||
|
if (abandonedBoards.length === 0) { |
||||||
|
return; |
||||||
|
} |
||||||
|
|
||||||
|
if (config.get.abandonedBoardAction <= 2) { |
||||||
|
debugLogs && console.log(`Locking${config.get.abandonedBoardAction === 2 ? '+Unlisting' : ''} ${abandonedBoards.length} abandoned boards.`); |
||||||
|
const abandonedURIs = abandonedBoards.map(b => b._id); |
||||||
|
Boards.unlistMany(abandonedURIs); |
||||||
|
} else { //must be 2
|
||||||
|
//run delete board model for each
|
||||||
|
debugLogs && console.log(`Deleting ${abandonedBoards.length} abandoned boards.`); |
||||||
|
for (let board of abandonedBoards) { |
||||||
|
try { |
||||||
|
await deleteBoard(board._id, board); |
||||||
|
} catch (err) { |
||||||
|
debugLogs && console.log('Error deleting abandoned board:', err); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
}, |
||||||
|
|
||||||
|
interval: timeUtils.DAY, |
||||||
|
immediate: false, |
||||||
|
condition: 'abandonedBoardAction' |
||||||
|
|
||||||
|
}; |
@ -0,0 +1,98 @@ |
|||||||
|
'use strict'; |
||||||
|
|
||||||
|
const { debugLogs } = require(__dirname+'/../../configs/secrets.js') |
||||||
|
, config = require(__dirname+'/../../lib/misc/config.js') |
||||||
|
, Redis = require(__dirname+'/../../lib/redis/redis.js') |
||||||
|
, { Boards, Accounts } = require(__dirname+'/../../db/') |
||||||
|
, timeUtils = require(__dirname+'/../../lib/converter/timeutils.js'); |
||||||
|
|
||||||
|
module.exports = { |
||||||
|
|
||||||
|
func: async () => { |
||||||
|
|
||||||
|
if (config.get.inactiveAccountAction === 0) { |
||||||
|
return; |
||||||
|
} |
||||||
|
|
||||||
|
const inactiveAccounts = await Accounts.getInactive(config.get.inactiveAccountTime); |
||||||
|
if (inactiveAccounts.length === 0) { |
||||||
|
return; |
||||||
|
} |
||||||
|
|
||||||
|
const cacheDeleteSet = new Set() |
||||||
|
, boardBulkWrites = [] |
||||||
|
, inactiveWithBoards = inactiveAccounts.filter(acc => { |
||||||
|
//only deal with boards if they have any (acc deletes still processed later)
|
||||||
|
return acc.ownedBoards.length > 0 || acc.staffBoards.length > 0; |
||||||
|
}); |
||||||
|
|
||||||
|
let boardsPromise = null |
||||||
|
, accountsPromise = null; |
||||||
|
|
||||||
|
//create promise for boards (remove staff.${username})
|
||||||
|
inactiveWithBoards.forEach(acc => { |
||||||
|
//remove account from staff and owner of all their boards
|
||||||
|
const accountBoards = [...acc.ownedBoards, ...acc.staffBoards]; |
||||||
|
accountBoards.forEach(b => cacheDeleteSet.add(b)); |
||||||
|
boardBulkWrites.push({ |
||||||
|
updateOne: { |
||||||
|
filter: { |
||||||
|
'_id': { |
||||||
|
'$in': accountBoards, |
||||||
|
//better to do per board for staff unsets? or per-account...
|
||||||
|
} |
||||||
|
}, |
||||||
|
update: { |
||||||
|
$unset: { |
||||||
|
[`staff.${acc._id}`]: '', |
||||||
|
}, |
||||||
|
} |
||||||
|
} |
||||||
|
}); |
||||||
|
acc.ownedBoards.forEach(ob => { |
||||||
|
boardBulkWrites.push({ |
||||||
|
updateOne: { |
||||||
|
filter: { |
||||||
|
'_id': ob, |
||||||
|
}, |
||||||
|
update: { |
||||||
|
'$set': { |
||||||
|
'owner': null, |
||||||
|
}, |
||||||
|
} |
||||||
|
} |
||||||
|
}); |
||||||
|
}); |
||||||
|
}); |
||||||
|
if (boardBulkWrites.length > 0) { |
||||||
|
boardsPromise = Boards.db.bulkWrite(boardBulkWrites); |
||||||
|
} |
||||||
|
|
||||||
|
//create promise for accounts (clearing staff positions or deleting fuly)
|
||||||
|
if (config.get.inactiveAccountAction === 2) { |
||||||
|
debugLogs && console.log(`Deleting ${inactiveAccounts.length} inactive accounts`); |
||||||
|
const inactiveUsernames = inactiveAccounts.map(acc => acc._id); |
||||||
|
accountsPromise = Accounts.deleteMany(inactiveUsernames); |
||||||
|
} else{ |
||||||
|
debugLogs && console.log(`Removing staff positions from ${inactiveWithBoards.length} inactive accounts`); |
||||||
|
const inactiveUsernames = inactiveWithBoards.map(acc => acc._id); |
||||||
|
accountsPromise = Accounts.clearStaffAndOwnedBoards(inactiveUsernames); |
||||||
|
} |
||||||
|
|
||||||
|
//execute promises
|
||||||
|
await Promise.all([ |
||||||
|
accountsPromise, |
||||||
|
boardsPromise, |
||||||
|
]); |
||||||
|
|
||||||
|
//clear caches
|
||||||
|
cacheDeleteSet.forEach(b => Redis.del(`board:${b}`)); |
||||||
|
//users: cache already handled by Accounts.deleteMany or Accounts.clearStaffAndOwnedBoards
|
||||||
|
|
||||||
|
}, |
||||||
|
|
||||||
|
interval: timeUtils.DAY, |
||||||
|
immediate: false, |
||||||
|
condition: 'inactiveAccountAction' |
||||||
|
|
||||||
|
}; |
Loading…
Reference in new issue