This repository has been archived on 2024-07-22. You can view files and clone it, but cannot push or open issues or pull requests.
Zaimki/server/cleanupAccounts.js

95 lines
2.7 KiB
JavaScript
Raw Normal View History

2022-02-12 09:28:56 -08:00
require('../src/dotenv')();
const dbConnection = require('./db');
const mailer = require('../src/mailer');
const {archiveBan} = require("./ban");
const execute = process.env.EXECUTE === '1';
console.log(execute ? 'WILL EXECUTE!' : 'Dry run');
const now = +new Date();
const month = 30*24*60*60*1000;
const week = 7*24*60*60*1000;
const sleep = ms => new Promise(res => setTimeout(res, ms));
async function warnInactive(db) {
console.log('--- Fetching ids to warn ---');
const users = (await db.all(`
SELECT u.id, u.username, u.email, u.bannedReason
FROM users u
WHERE
inactiveWarning IS NULL
AND (
(
u.id NOT IN (SELECT DISTINCT p.userId FROM profiles p)
AND (lastActive IS NULL OR lastActive < ${now - month})
)
OR bannedReason IS NOT NULL
)
`));
console.log(users.length);
for (let user of users) {
console.log('warn', user);
if (!execute) { continue; }
2022-02-26 03:34:25 -08:00
try {
2022-02-26 03:49:06 -08:00
let userRefreshed = await db.get(`SELECT u.id, u.username, u.email, u.bannedReason, u.inactiveWarning FROM users u WHERE u.id = '${user.id}'`);
2022-02-26 03:34:25 -08:00
if (userRefreshed.inactiveWarning !== null) { continue; }
if (userRefreshed.email.endsWith('.oauth')) {
await db.get(`UPDATE users SET inactiveWarning = ${now - week - 1000} WHERE id = '${userRefreshed.id}'`);
continue;
}
await db.get(`UPDATE users SET inactiveWarning = ${now} WHERE id = '${userRefreshed.id}'`);
if (userRefreshed.bannedReason !== null) {
continue;
}
mailer(userRefreshed.email, 'inactivityWarning')
} catch (e) {
console.error(e);
2022-02-12 09:28:56 -08:00
}
await sleep(3000);
}
}
async function removeWarned(db) {
console.log('--- Fetching ids to remove ---');
const users = (await db.all(`
SELECT u.id, u.username, u.email
FROM users u
WHERE (
u.id NOT IN (
SELECT DISTINCT p.userId
FROM profiles p
)
OR bannedReason IS NOT NULL
)
AND inactiveWarning IS NOT NULL
AND inactiveWarning < ${now - week}
`));
console.log(users.length);
for (let user of users) {
console.log('remove', user);
if (!execute) { continue; }
await db.get(`DELETE FROM users WHERE id = '${user.id}'`);
}
}
async function cleanup() {
const db = await dbConnection();
await db.get('PRAGMA foreign_keys = ON')
await warnInactive(db);
await removeWarned(db);
}
cleanup();