2020-10-31 13:33:59 -07:00
|
|
|
import { Router } from 'express';
|
|
|
|
import SQL from 'sql-template-strings';
|
|
|
|
import {ulid} from "ulid";
|
2020-11-25 13:35:29 -08:00
|
|
|
import {createCanvas, loadImage, registerFont} from "canvas";
|
|
|
|
import {loadSuml} from "../loader";
|
2021-08-11 14:29:41 -07:00
|
|
|
import {clearKey, handleErrorAsync, isTroll} from "../../src/helpers";
|
2021-07-02 16:15:44 -07:00
|
|
|
import { caches } from "../../src/cache";
|
2021-12-03 01:24:21 -08:00
|
|
|
import {registerLocaleFont} from "../localeFont";
|
2020-11-25 13:35:29 -08:00
|
|
|
|
|
|
|
const translations = loadSuml('translations');
|
2020-10-31 13:33:59 -07:00
|
|
|
|
|
|
|
const approve = async (db, id) => {
|
|
|
|
const { base_id } = await db.get(SQL`SELECT base_id FROM nouns WHERE id=${id}`);
|
|
|
|
if (base_id) {
|
|
|
|
await db.get(SQL`
|
2020-12-03 06:01:26 -08:00
|
|
|
UPDATE nouns
|
|
|
|
SET deleted=1
|
2020-10-31 13:33:59 -07:00
|
|
|
WHERE id = ${base_id}
|
|
|
|
`);
|
|
|
|
}
|
|
|
|
await db.get(SQL`
|
|
|
|
UPDATE nouns
|
|
|
|
SET approved = 1, base_id = NULL
|
|
|
|
WHERE id = ${id}
|
|
|
|
`);
|
2021-07-02 16:15:44 -07:00
|
|
|
await caches.nouns.invalidate();
|
2020-10-31 13:33:59 -07:00
|
|
|
}
|
|
|
|
|
2021-01-05 11:11:41 -08:00
|
|
|
const addVersions = async (req, nouns) => {
|
|
|
|
const keys = new Set();
|
|
|
|
nouns.filter(s => !!s && s.sources)
|
2021-08-11 14:29:41 -07:00
|
|
|
.forEach(s => s.sources.split(',').forEach(k => keys.add(`'` + clearKey(k.split('#')[0]) + `'`)));
|
2021-01-05 11:11:41 -08:00
|
|
|
|
|
|
|
const sources = await req.db.all(SQL`
|
|
|
|
SELECT s.*, u.username AS submitter FROM sources s
|
|
|
|
LEFT JOIN users u ON s.submitter_id = u.id
|
2021-06-17 16:10:59 -07:00
|
|
|
WHERE s.locale == ${global.config.locale}
|
2021-01-05 11:11:41 -08:00
|
|
|
AND s.deleted = 0
|
|
|
|
AND s.approved >= ${req.isGranted('sources') ? 0 : 1}
|
|
|
|
AND s.key IN (`.append([...keys].join(',')).append(SQL`)
|
|
|
|
`));
|
|
|
|
|
|
|
|
const sourcesMap = {};
|
|
|
|
sources.forEach(s => sourcesMap[s.key] = s)
|
|
|
|
|
|
|
|
return nouns.map(n => {
|
2021-05-19 11:29:34 -07:00
|
|
|
n.sourcesData = (n.sources ? n.sources.split(',') : []).map(s => selectFragment(sourcesMap, s));
|
2021-01-05 11:11:41 -08:00
|
|
|
return n;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2021-05-19 11:29:34 -07:00
|
|
|
const selectFragment = (sourcesMap, keyAndFragment) => {
|
|
|
|
const [key, fragment] = keyAndFragment.split('#');
|
|
|
|
if (sourcesMap[key] === undefined) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
if (fragment === undefined) {
|
|
|
|
return sourcesMap[key];
|
|
|
|
}
|
|
|
|
|
|
|
|
const source = {...sourcesMap[key]};
|
|
|
|
|
|
|
|
const fragments = source.fragments
|
|
|
|
? source.fragments.replace('\\@', '###').split('@').map(x => x.replace('###', '\\@'))
|
|
|
|
: [];
|
|
|
|
|
|
|
|
source.fragments = fragments[parseInt(fragment) - 1];
|
|
|
|
|
|
|
|
return source;
|
|
|
|
}
|
|
|
|
|
2020-10-31 13:33:59 -07:00
|
|
|
const router = Router();
|
|
|
|
|
2021-06-09 05:47:08 -07:00
|
|
|
router.get('/nouns', handleErrorAsync(async (req, res) => {
|
2021-07-02 16:15:44 -07:00
|
|
|
return res.json(await caches.nouns.fetch(async () => {
|
|
|
|
return await addVersions(req, await req.db.all(SQL`
|
|
|
|
SELECT n.*, u.username AS author FROM nouns n
|
|
|
|
LEFT JOIN users u ON n.author_id = u.id
|
|
|
|
WHERE n.locale = ${global.config.locale}
|
|
|
|
AND n.deleted = 0
|
|
|
|
AND n.approved >= ${req.isGranted('nouns') ? 0 : 1}
|
|
|
|
ORDER BY n.approved, n.masc
|
|
|
|
`))
|
2021-07-12 16:09:13 -07:00
|
|
|
}, !req.isGranted('nouns')));
|
2021-06-09 05:47:08 -07:00
|
|
|
}));
|
2020-10-31 13:33:59 -07:00
|
|
|
|
2021-06-09 05:47:08 -07:00
|
|
|
router.get('/nouns/search/:term', handleErrorAsync(async (req, res) => {
|
2020-11-10 14:41:56 -08:00
|
|
|
const term = '%' + req.params.term + '%';
|
2021-01-05 11:11:41 -08:00
|
|
|
return res.json(await addVersions(req, await req.db.all(SQL`
|
2020-12-04 13:09:57 -08:00
|
|
|
SELECT n.*, u.username AS author FROM nouns n
|
|
|
|
LEFT JOIN users u ON n.author_id = u.id
|
2021-06-17 16:10:59 -07:00
|
|
|
WHERE n.locale = ${global.config.locale}
|
2020-12-30 15:03:30 -08:00
|
|
|
AND n.approved >= ${req.isGranted('nouns') ? 0 : 1}
|
2020-12-04 13:09:57 -08:00
|
|
|
AND n.deleted = 0
|
|
|
|
AND (n.masc like ${term} OR n.fem like ${term} OR n.neutr like ${term} OR n.mascPl like ${term} OR n.femPl like ${term} OR n.neutrPl like ${term})
|
|
|
|
ORDER BY n.approved, n.masc
|
2021-01-05 11:11:41 -08:00
|
|
|
`)));
|
2021-06-09 05:47:08 -07:00
|
|
|
}));
|
2020-11-10 14:41:56 -08:00
|
|
|
|
2021-06-09 05:47:08 -07:00
|
|
|
router.post('/nouns/submit', handleErrorAsync(async (req, res) => {
|
2021-12-02 10:11:04 -08:00
|
|
|
if (!req.user || !await req.isUserAllowedToPost()) {
|
|
|
|
return res.status(401).json({error: 'Unauthorised'});
|
2021-07-25 00:50:02 -07:00
|
|
|
}
|
|
|
|
|
2020-11-03 11:07:38 -08:00
|
|
|
if (!(req.user && req.user.admin) && isTroll(JSON.stringify(req.body))) {
|
2020-10-31 13:33:59 -07:00
|
|
|
return res.json('ok');
|
|
|
|
}
|
|
|
|
|
|
|
|
const id = ulid();
|
|
|
|
await req.db.get(SQL`
|
2021-01-05 11:11:41 -08:00
|
|
|
INSERT INTO nouns (id, masc, fem, neutr, mascPl, femPl, neutrPl, sources, approved, base_id, locale, author_id)
|
2020-10-31 13:33:59 -07:00
|
|
|
VALUES (
|
|
|
|
${id},
|
|
|
|
${req.body.masc.join('|')}, ${req.body.fem.join('|')}, ${req.body.neutr.join('|')},
|
|
|
|
${req.body.mascPl.join('|')}, ${req.body.femPl.join('|')}, ${req.body.neutrPl.join('|')},
|
2021-01-05 11:11:41 -08:00
|
|
|
${req.body.sources || null},
|
2021-06-17 16:10:59 -07:00
|
|
|
0, ${req.body.base}, ${global.config.locale}, ${req.user ? req.user.id : null}
|
2020-10-31 13:33:59 -07:00
|
|
|
)
|
|
|
|
`);
|
|
|
|
|
2020-12-30 15:03:30 -08:00
|
|
|
if (req.isGranted('nouns')) {
|
2020-10-31 13:33:59 -07:00
|
|
|
await approve(req.db, id);
|
|
|
|
}
|
|
|
|
|
|
|
|
return res.json('ok');
|
2021-06-09 05:47:08 -07:00
|
|
|
}));
|
2020-10-31 13:33:59 -07:00
|
|
|
|
2021-06-09 05:47:08 -07:00
|
|
|
router.post('/nouns/hide/:id', handleErrorAsync(async (req, res) => {
|
2020-12-30 15:03:30 -08:00
|
|
|
if (!req.isGranted('nouns')) {
|
2021-12-02 10:11:04 -08:00
|
|
|
return res.status(401).json({error: 'Unauthorised'});
|
2020-10-31 13:33:59 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
await req.db.get(SQL`
|
|
|
|
UPDATE nouns
|
|
|
|
SET approved = 0
|
|
|
|
WHERE id = ${req.params.id}
|
|
|
|
`);
|
|
|
|
|
2021-07-02 16:15:44 -07:00
|
|
|
await caches.nouns.invalidate();
|
|
|
|
|
2020-10-31 13:33:59 -07:00
|
|
|
return res.json('ok');
|
2021-06-09 05:47:08 -07:00
|
|
|
}));
|
2020-10-31 13:33:59 -07:00
|
|
|
|
2021-06-09 05:47:08 -07:00
|
|
|
router.post('/nouns/approve/:id', handleErrorAsync(async (req, res) => {
|
2020-12-30 15:03:30 -08:00
|
|
|
if (!req.isGranted('nouns')) {
|
2021-12-02 10:11:04 -08:00
|
|
|
return res.status(401).json({error: 'Unauthorised'});
|
2020-10-31 13:33:59 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
await approve(req.db, req.params.id);
|
|
|
|
|
|
|
|
return res.json('ok');
|
2021-06-09 05:47:08 -07:00
|
|
|
}));
|
2020-10-31 13:33:59 -07:00
|
|
|
|
2021-06-09 05:47:08 -07:00
|
|
|
router.post('/nouns/remove/:id', handleErrorAsync(async (req, res) => {
|
2020-12-30 15:03:30 -08:00
|
|
|
if (!req.isGranted('nouns')) {
|
2021-12-02 10:11:04 -08:00
|
|
|
return res.status(401).json({error: 'Unauthorised'});
|
2020-10-31 13:33:59 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
await req.db.get(SQL`
|
2020-12-03 06:01:26 -08:00
|
|
|
UPDATE nouns
|
|
|
|
SET deleted=1
|
2020-10-31 13:33:59 -07:00
|
|
|
WHERE id = ${req.params.id}
|
|
|
|
`);
|
|
|
|
|
2021-07-02 16:15:44 -07:00
|
|
|
await caches.nouns.invalidate();
|
|
|
|
|
2020-10-31 13:33:59 -07:00
|
|
|
return res.json('ok');
|
2021-06-09 05:47:08 -07:00
|
|
|
}));
|
2020-10-31 13:33:59 -07:00
|
|
|
|
2021-08-28 14:44:30 -07:00
|
|
|
router.get('/nouns/:id.png', async (req, res) => {
|
|
|
|
const noun = (await req.db.get(SQL`
|
2020-11-25 13:35:29 -08:00
|
|
|
SELECT * FROM nouns
|
2021-06-17 16:10:59 -07:00
|
|
|
WHERE locale = ${global.config.locale}
|
2021-08-28 14:44:30 -07:00
|
|
|
AND id = ${req.params.id}
|
2020-12-30 15:03:30 -08:00
|
|
|
AND approved >= ${req.isGranted('nouns') ? 0 : 1}
|
2021-08-28 14:44:30 -07:00
|
|
|
AND deleted = 0
|
|
|
|
`));
|
2020-11-25 13:35:29 -08:00
|
|
|
|
|
|
|
if (!noun) {
|
|
|
|
return res.status(404).json({error: 'Not found'});
|
|
|
|
}
|
|
|
|
|
2021-08-28 14:44:30 -07:00
|
|
|
let maxItems = 0;
|
|
|
|
['masc', 'fem', 'neutr'].forEach((form, column) => {
|
|
|
|
let items = 0;
|
|
|
|
for (let [key, symbol] of [['', '⋅'], ['Pl', '⁖']]) {
|
|
|
|
items += noun[form + key].split('|').filter(x => x.length).length;
|
|
|
|
}
|
|
|
|
if (items > maxItems) {
|
|
|
|
maxItems = items;
|
|
|
|
}
|
|
|
|
});
|
2020-11-25 13:35:29 -08:00
|
|
|
|
|
|
|
const padding = 48;
|
2021-08-28 14:44:30 -07:00
|
|
|
const width = 1200;
|
|
|
|
const height = padding * 2.5 + (maxItems + 1) * 48 + padding;
|
2020-11-25 13:35:29 -08:00
|
|
|
const mime = 'image/png';
|
|
|
|
|
2021-12-03 01:24:21 -08:00
|
|
|
const fontName = registerLocaleFont('fontHeadings', ['regular', 'bold']);
|
2020-11-25 13:35:29 -08:00
|
|
|
registerFont('node_modules/@fortawesome/fontawesome-pro/webfonts/fa-light-300.ttf', { family: 'FontAwesome', weight: 'regular'});
|
|
|
|
|
|
|
|
const canvas = createCanvas(width, height);
|
|
|
|
const context = canvas.getContext('2d');
|
|
|
|
|
2020-12-01 11:35:23 -08:00
|
|
|
const bg = await loadImage('static/bg.png');
|
|
|
|
context.drawImage(bg, 0, 0, width, height);
|
2020-11-25 13:35:29 -08:00
|
|
|
|
2021-12-03 01:24:21 -08:00
|
|
|
context.font = `bold 64pt '${fontName}'`;
|
2020-11-25 13:35:29 -08:00
|
|
|
|
|
|
|
for (let [column, key, icon] of [[0, 'masculine', '\uf222'], [1, 'feminine', '\uf221'], [2, 'neuter', '\uf22c']]) {
|
|
|
|
context.font = 'regular 24pt FontAwesome';
|
2021-08-28 14:44:30 -07:00
|
|
|
context.fillText(icon, column * (width - 2 * padding) / 3 + padding, padding * 1.5);
|
2020-11-25 13:35:29 -08:00
|
|
|
|
2021-12-03 01:24:21 -08:00
|
|
|
context.font = `bold 24pt '${fontName}'`;
|
2021-08-28 14:44:30 -07:00
|
|
|
context.fillText(translations.nouns[key], column * (width - 2 * padding) / 3 + padding + 36, padding * 1.5);
|
2020-11-25 13:35:29 -08:00
|
|
|
}
|
|
|
|
|
2021-12-03 01:24:21 -08:00
|
|
|
context.font = `regular 24pt '${fontName}'`;
|
2020-11-25 13:35:29 -08:00
|
|
|
['masc', 'fem', 'neutr'].forEach((form, column) => {
|
|
|
|
let i = 0;
|
|
|
|
for (let [key, symbol] of [['', '⋅'], ['Pl', '⁖']])
|
2021-08-28 14:44:30 -07:00
|
|
|
noun[form + key].split('|').filter(x => x.length).forEach(part => {
|
|
|
|
context.fillText(symbol + ' ' + part, column * (width - 2 * padding) / 3 + padding, padding * 2.5 + i * 48);
|
|
|
|
i++;
|
|
|
|
});
|
2020-11-25 13:35:29 -08:00
|
|
|
})
|
|
|
|
|
|
|
|
context.fillStyle = '#C71585';
|
2021-08-28 14:44:30 -07:00
|
|
|
context.font = 'regular 16pt FontAwesome';
|
|
|
|
context.fillText('\uf02c', padding, height - padding + 12);
|
2021-12-03 01:24:21 -08:00
|
|
|
context.font = `regular 16pt '${fontName}'`;
|
2021-08-28 14:44:30 -07:00
|
|
|
context.fillText(
|
|
|
|
translations.title + '/' + (global.config.nouns.routeMain || global.config.nouns.route),
|
|
|
|
padding + 36,
|
|
|
|
height - padding + 8
|
|
|
|
);
|
2020-11-25 13:35:29 -08:00
|
|
|
|
|
|
|
return res.set('content-type', mime).send(canvas.toBuffer(mime));
|
2021-08-28 14:44:30 -07:00
|
|
|
});
|
2020-11-25 13:35:29 -08:00
|
|
|
|
2020-10-31 13:33:59 -07:00
|
|
|
export default router;
|