2020-10-31 13:33:59 -07:00
|
|
|
import { Router } from 'express';
|
|
|
|
import mailer from "../../src/mailer";
|
2020-11-10 14:41:56 -08:00
|
|
|
import { camelCase } from "../../src/helpers";
|
|
|
|
import { loadTsv } from '../loader';
|
2020-11-09 06:39:18 -08:00
|
|
|
|
|
|
|
const generateId = title => {
|
|
|
|
return camelCase(title.split(' ').slice(0, 2));
|
|
|
|
}
|
2020-10-31 13:33:59 -07:00
|
|
|
|
|
|
|
const buildEmail = (data, user) => {
|
|
|
|
const human = [
|
|
|
|
`<li><strong>user:</strong> ${user ? user.username : ''}</li>`,
|
2020-11-10 15:47:44 -08:00
|
|
|
`<li><strong>pronouns:</strong> ${data.pronouns}</li>`,
|
2020-10-31 13:33:59 -07:00
|
|
|
];
|
2020-11-09 06:39:18 -08:00
|
|
|
const tsv = [generateId(data.title) || '???'];
|
2020-10-31 13:33:59 -07:00
|
|
|
|
|
|
|
for (let field of ['type','author','title','extra','year','fragments','comment','link']) {
|
|
|
|
human.push(`<li><strong>${field}:</strong> ${field === 'fragments' ? `<pre>${data[field]}</pre>`: data[field]}</li>`);
|
|
|
|
tsv.push(field === 'fragments' ? (data[field].join('@').replace(/\n/g, '|')) : data[field]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return `<ul>${human.join('')}</ul><pre>${tsv.join('\t')}</pre>`;
|
|
|
|
}
|
|
|
|
|
2020-11-10 14:41:56 -08:00
|
|
|
const loadSources = () => {
|
|
|
|
return loadTsv('sources/sources').map(s => {
|
|
|
|
if (s.author) {
|
|
|
|
s.author = s.author.replace('^', '');
|
|
|
|
}
|
|
|
|
s.fragments = s.fragments.split('@').map(f => f.replace(/\|/g, '\n'));
|
|
|
|
return s;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-10-31 13:33:59 -07:00
|
|
|
const router = Router();
|
|
|
|
|
2020-11-10 14:41:56 -08:00
|
|
|
router.get('/sources', async (req, res) => {
|
|
|
|
return res.json(loadSources());
|
|
|
|
});
|
|
|
|
|
|
|
|
router.get('/sources/:key', async (req, res) => {
|
|
|
|
return res.json([...loadSources().filter(s => s.key === req.params.key), null][0]);
|
|
|
|
});
|
|
|
|
|
2020-11-09 15:50:39 -08:00
|
|
|
router.post('/sources/submit', async (req, res) => {
|
2020-10-31 13:33:59 -07:00
|
|
|
const emailBody = buildEmail(req.body, req.user);
|
|
|
|
|
|
|
|
for (let admin of process.env.MAILER_ADMINS.split(',')) {
|
2020-11-09 15:50:39 -08:00
|
|
|
mailer(admin, `[Pronouns][${req.config.locale}] Source submission`, undefined, emailBody);
|
2020-10-31 13:33:59 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return res.json({ result: 'ok' });
|
|
|
|
});
|
|
|
|
|
|
|
|
export default router;
|