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/notify.js

71 lines
2.4 KiB
JavaScript
Raw Normal View History

2020-10-12 02:46:26 -07:00
const dbConnection = require('./db');
require('dotenv').config({ path:__dirname + '/../.env' });
2020-10-31 13:33:59 -07:00
const mailer = require('../src/mailer');
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('-');
if ((permissionLocale === '*' || permissionLocale === locale) && (permissionArea === '*' || permissionArea === area)) {
return true;
}
}
return false;
}
async function notify() {
2020-10-12 02:46:26 -07:00
const db = await dbConnection();
const awaitingModeration = [
...(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`)),
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`)),
];
if (!awaitingModeration.length) {
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 != ''`);
const awaitingModerationGrouped = {}
let count = 0;
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] = {};
}
awaitingModerationGrouped[admin.email][m.locale + '-' + m.type] = m.c;
}
}
count += m.c;
}
2021-04-05 05:15:57 -07:00
console.log('Entries awaiting moderation: ', count);
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);
mailer(
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),
);
}
}
notify();