mirror of https://gitgud.io/fatchan/jschan.git
Plus the same for abandoned boards handling, just still TODO the schedule. ref #418merge-requests/341/head
parent
6d90f47a78
commit
3ab0a271c4
9 changed files with 200 additions and 4 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,26 @@ |
||||
'use strict'; |
||||
|
||||
const fetch = require('node-fetch') |
||||
, { 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.abandonedBoardAction === 0) { |
||||
return; |
||||
} |
||||
|
||||
//TODO: handle abandoned boards. 1=unlist, 2=unlist+lock, 3=delete
|
||||
|
||||
}, |
||||
|
||||
interval: timeUtils.DAY, |
||||
immediate: false, |
||||
condition: 'abandonedBoardAction' |
||||
|
||||
}; |
@ -0,0 +1,100 @@ |
||||
'use strict'; |
||||
|
||||
const fetch = require('node-fetch') |
||||
, { 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 = [] |
||||
, accountBulkWrites = [] |
||||
, 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: true, |
||||
condition: 'inactiveAccountAction' |
||||
|
||||
}; |
Loading…
Reference in new issue