2021-09-07 10:11:15 -07:00
|
|
|
require('../src/dotenv')();
|
|
|
|
|
2020-10-12 02:46:26 -07:00
|
|
|
const dbConnection = require('./db');
|
2020-10-31 13:33:59 -07:00
|
|
|
const mailer = require('../src/mailer');
|
2020-08-06 07:33:16 -07:00
|
|
|
|
2021-04-05 05:15:57 -07:00
|
|
|
// TODO duplication...
|
|
|
|
const isGranted = (user, locale, area) => {
|
|
|
|
if (area === '*') {
|
|
|
|
return user.roles.split('|').includes('*');
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let permission of user.roles.split('|')) {
|
|
|
|
if (permission === '*') {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
const [ permissionLocale, permissionArea ] = permission.split('-');
|
2021-07-24 10:43:17 -07:00
|
|
|
if ((permissionLocale === '*' || permissionLocale === locale || locale === null) && (permissionArea === '*' || permissionArea === area)) {
|
2021-04-05 05:15:57 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-08-06 07:33:16 -07:00
|
|
|
async function notify() {
|
2020-10-12 02:46:26 -07:00
|
|
|
const db = await dbConnection();
|
2020-08-06 07:33:16 -07:00
|
|
|
|
2020-11-17 10:37:32 -08:00
|
|
|
const awaitingModeration = [
|
2020-12-03 06:01:26 -08:00
|
|
|
...(await db.all(`SELECT 'nouns' as type, locale, count(*) as c FROM nouns WHERE approved = 0 AND deleted=0 GROUP BY locale`)),
|
|
|
|
...(await db.all(`SELECT 'inclusive' as type, locale, count(*) as c FROM inclusive WHERE approved = 0 AND deleted=0 GROUP BY locale`)),
|
2021-08-31 00:20:43 -07:00
|
|
|
...(await db.all(`SELECT 'terms' as type, locale, count(*) as c FROM terms WHERE approved = 0 AND deleted=0 GROUP BY locale`)),
|
2020-12-06 06:23:18 -08:00
|
|
|
...(await db.all(`SELECT 'sources' as type, locale, count(*) as c FROM sources WHERE approved = 0 AND deleted=0 GROUP BY locale`)),
|
2021-07-24 10:43:17 -07:00
|
|
|
...(await db.all(`SELECT 'reports' as type, null as locale, count(*) as c FROM reports WHERE isHandled = 0`)),
|
2020-11-17 10:37:32 -08:00
|
|
|
];
|
2020-10-31 05:26:16 -07:00
|
|
|
if (!awaitingModeration.length) {
|
2020-08-06 07:33:16 -07:00
|
|
|
console.log('No entries awaiting moderation');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-04-05 05:15:57 -07:00
|
|
|
const admins = await db.all(`SELECT email, roles FROM users WHERE roles != ''`);
|
|
|
|
|
2020-10-31 05:26:16 -07:00
|
|
|
const awaitingModerationGrouped = {}
|
2020-11-17 10:37:32 -08:00
|
|
|
let count = 0;
|
2020-10-31 05:26:16 -07:00
|
|
|
for (let m of awaitingModeration) {
|
2021-04-05 05:15:57 -07:00
|
|
|
for (let admin of admins) {
|
|
|
|
if (isGranted(admin, m.locale, m.type)) {
|
|
|
|
if (awaitingModerationGrouped[admin.email] === undefined) {
|
|
|
|
awaitingModerationGrouped[admin.email] = {};
|
|
|
|
}
|
2021-07-24 10:43:17 -07:00
|
|
|
awaitingModerationGrouped[admin.email][(m.locale || '*') + '-' + m.type] = m.c;
|
2021-04-05 05:15:57 -07:00
|
|
|
}
|
|
|
|
}
|
2020-11-17 10:37:32 -08:00
|
|
|
count += m.c;
|
2020-10-31 05:26:16 -07:00
|
|
|
}
|
|
|
|
|
2021-04-05 05:15:57 -07:00
|
|
|
console.log('Entries awaiting moderation: ', count);
|
2020-08-06 07:33:16 -07:00
|
|
|
|
2021-04-05 05:15:57 -07:00
|
|
|
for (let email in awaitingModerationGrouped) {
|
|
|
|
if (!awaitingModerationGrouped.hasOwnProperty(email)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
const message = awaitingModerationGrouped[email];
|
|
|
|
console.log('Sending email:', email, message);
|
2020-11-05 08:44:11 -08:00
|
|
|
|
2020-10-31 05:26:16 -07:00
|
|
|
mailer(
|
2020-11-05 08:44:11 -08:00
|
|
|
email,
|
2021-04-05 05:15:57 -07:00
|
|
|
'[Pronouns.page] There are entries awaiting moderation',
|
|
|
|
'Entries awaiting moderation: \n' + JSON.stringify(message, null, 4),
|
2020-10-31 05:26:16 -07:00
|
|
|
);
|
2020-08-06 07:33:16 -07:00
|
|
|
}
|
2021-05-18 09:23:52 -07:00
|
|
|
|
|
|
|
await db.close();
|
2020-08-06 07:33:16 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
notify();
|