2020-12-18 08:32:18 -08:00
|
|
|
import { Router } from 'express';
|
|
|
|
import SQL from 'sql-template-strings';
|
|
|
|
import {ulid} from "ulid";
|
2021-07-03 04:19:43 -07:00
|
|
|
import {isTroll, handleErrorAsync, sortClearedLinkedText} from "../../src/helpers";
|
2021-07-02 16:15:44 -07:00
|
|
|
import { caches } from "../../src/cache";
|
2020-12-18 08:32:18 -08:00
|
|
|
|
|
|
|
const approve = async (db, id) => {
|
|
|
|
const { base_id } = await db.get(SQL`SELECT base_id FROM terms WHERE id=${id}`);
|
|
|
|
if (base_id) {
|
|
|
|
await db.get(SQL`
|
|
|
|
UPDATE terms
|
|
|
|
SET deleted=1
|
|
|
|
WHERE id = ${base_id}
|
|
|
|
`);
|
|
|
|
}
|
|
|
|
await db.get(SQL`
|
|
|
|
UPDATE terms
|
|
|
|
SET approved = 1, base_id = NULL
|
|
|
|
WHERE id = ${id}
|
|
|
|
`);
|
2021-07-02 16:15:44 -07:00
|
|
|
await caches.terms.invalidate();
|
2020-12-18 08:32:18 -08:00
|
|
|
}
|
|
|
|
|
2021-07-30 03:51:12 -07:00
|
|
|
const linkOtherVersions = async (req, terms) => {
|
|
|
|
const keys = new Set(terms.filter(s => !!s && s.key).map(s => `'` + s.key + `'`));
|
|
|
|
|
|
|
|
const otherVersions = await req.db.all(SQL`
|
|
|
|
SELECT t.*, u.username AS author FROM terms t
|
|
|
|
LEFT JOIN users u ON t.author_id = u.id
|
|
|
|
WHERE t.locale != ${global.config.locale}
|
|
|
|
AND t.deleted = 0
|
|
|
|
AND t.approved >= ${req.isGranted('terms') ? 0 : 1}
|
|
|
|
AND t.key IN (`.append([...keys].join(',')).append(SQL`)
|
|
|
|
`));
|
|
|
|
|
|
|
|
const otherVersionsMap = {};
|
|
|
|
otherVersions.forEach(version => {
|
|
|
|
if (otherVersionsMap[version.key] === undefined) {
|
|
|
|
otherVersionsMap[version.key] = [];
|
|
|
|
}
|
|
|
|
otherVersionsMap[version.key].push(version);
|
|
|
|
});
|
|
|
|
|
|
|
|
return terms.map(t => {
|
|
|
|
t.versions = t.key ? otherVersionsMap[t.key] || [] : [];
|
|
|
|
return t;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2020-12-18 08:32:18 -08:00
|
|
|
const router = Router();
|
|
|
|
|
2021-06-09 05:47:08 -07:00
|
|
|
router.get('/terms', handleErrorAsync(async (req, res) => {
|
2021-07-03 04:19:43 -07:00
|
|
|
return res.json(await caches.terms.fetch(async () => {
|
2021-07-30 03:51:12 -07:00
|
|
|
return await linkOtherVersions(
|
|
|
|
req,
|
|
|
|
sortClearedLinkedText(await req.db.all(SQL`
|
|
|
|
SELECT i.*, u.username AS author FROM terms i
|
|
|
|
LEFT JOIN users u ON i.author_id = u.id
|
|
|
|
WHERE i.locale = ${global.config.locale}
|
|
|
|
AND i.approved >= ${req.isGranted('terms') ? 0 : 1}
|
|
|
|
AND i.deleted = 0
|
|
|
|
`), 'term'),
|
|
|
|
);
|
2021-07-12 16:09:13 -07:00
|
|
|
}, !req.isGranted('terms')));
|
2021-06-09 05:47:08 -07:00
|
|
|
}));
|
2020-12-18 08:32:18 -08:00
|
|
|
|
2021-06-09 05:47:08 -07:00
|
|
|
router.get('/terms/search/:term', handleErrorAsync(async (req, res) => {
|
2020-12-18 08:32:18 -08:00
|
|
|
const term = '%' + req.params.term + '%';
|
2021-07-30 03:51:12 -07:00
|
|
|
return res.json(
|
|
|
|
await linkOtherVersions(
|
|
|
|
req,
|
|
|
|
sortClearedLinkedText(await req.db.all(SQL`
|
|
|
|
SELECT i.*, u.username AS author FROM terms i
|
|
|
|
LEFT JOIN users u ON i.author_id = u.id
|
|
|
|
WHERE i.locale = ${global.config.locale}
|
|
|
|
AND i.approved >= ${req.isGranted('terms') ? 0 : 1}
|
|
|
|
AND i.deleted = 0
|
|
|
|
AND (i.term like ${term} OR i.original like ${term})
|
|
|
|
`), 'term'),
|
|
|
|
)
|
|
|
|
);
|
2021-06-09 05:47:08 -07:00
|
|
|
}));
|
2020-12-18 08:32:18 -08:00
|
|
|
|
2021-06-09 05:47:08 -07:00
|
|
|
router.post('/terms/submit', handleErrorAsync(async (req, res) => {
|
2021-07-25 00:50:02 -07:00
|
|
|
if (!req.user) {
|
|
|
|
res.status(401).json({error: 'Unauthorised'});
|
|
|
|
}
|
|
|
|
|
2020-12-18 08:32:18 -08:00
|
|
|
if (!(req.user && req.user.admin) && isTroll(JSON.stringify(req.body))) {
|
|
|
|
return res.json('ok');
|
|
|
|
}
|
|
|
|
|
|
|
|
const id = ulid();
|
|
|
|
await req.db.get(SQL`
|
2021-07-30 03:51:12 -07:00
|
|
|
INSERT INTO terms (id, term, original, key, definition, approved, base_id, locale, author_id, category, flags, images)
|
2020-12-18 08:32:18 -08:00
|
|
|
VALUES (
|
|
|
|
${id},
|
2021-07-30 03:51:12 -07:00
|
|
|
${req.body.term.join('|')}, ${req.body.original.join('|')}, ${req.body.key || null}, ${req.body.definition},
|
2021-06-17 16:10:59 -07:00
|
|
|
0, ${req.body.base}, ${global.config.locale}, ${req.user ? req.user.id : null},
|
2021-07-29 14:40:55 -07:00
|
|
|
${req.body.categories.join(',')}, ${JSON.stringify(req.body.flags)}, ${req.body.images}
|
2020-12-18 08:32:18 -08:00
|
|
|
)
|
|
|
|
`);
|
|
|
|
|
2020-12-30 15:03:30 -08:00
|
|
|
if (req.isGranted('terms')) {
|
2020-12-18 08:32:18 -08:00
|
|
|
await approve(req.db, id);
|
|
|
|
}
|
|
|
|
|
|
|
|
return res.json('ok');
|
2021-06-09 05:47:08 -07:00
|
|
|
}));
|
2020-12-18 08:32:18 -08:00
|
|
|
|
2021-06-09 05:47:08 -07:00
|
|
|
router.post('/terms/hide/:id', handleErrorAsync(async (req, res) => {
|
2020-12-30 15:03:30 -08:00
|
|
|
if (!req.isGranted('terms')) {
|
2020-12-18 08:32:18 -08:00
|
|
|
res.status(401).json({error: 'Unauthorised'});
|
|
|
|
}
|
|
|
|
|
|
|
|
await req.db.get(SQL`
|
|
|
|
UPDATE terms
|
|
|
|
SET approved = 0
|
|
|
|
WHERE id = ${req.params.id}
|
|
|
|
`);
|
|
|
|
|
2021-07-02 16:15:44 -07:00
|
|
|
await caches.terms.invalidate();
|
|
|
|
|
2020-12-18 08:32:18 -08:00
|
|
|
return res.json('ok');
|
2021-06-09 05:47:08 -07:00
|
|
|
}));
|
2020-12-18 08:32:18 -08:00
|
|
|
|
2021-06-09 05:47:08 -07:00
|
|
|
router.post('/terms/approve/:id', handleErrorAsync(async (req, res) => {
|
2020-12-30 15:03:30 -08:00
|
|
|
if (!req.isGranted('terms')) {
|
2020-12-18 08:32:18 -08:00
|
|
|
res.status(401).json({error: 'Unauthorised'});
|
|
|
|
}
|
|
|
|
|
|
|
|
await approve(req.db, req.params.id);
|
|
|
|
|
|
|
|
return res.json('ok');
|
2021-06-09 05:47:08 -07:00
|
|
|
}));
|
2020-12-18 08:32:18 -08:00
|
|
|
|
2021-06-09 05:47:08 -07:00
|
|
|
router.post('/terms/remove/:id', handleErrorAsync(async (req, res) => {
|
2020-12-30 15:03:30 -08:00
|
|
|
if (!req.isGranted('terms')) {
|
2020-12-18 08:32:18 -08:00
|
|
|
res.status(401).json({error: 'Unauthorised'});
|
|
|
|
}
|
|
|
|
|
|
|
|
await req.db.get(SQL`
|
|
|
|
UPDATE terms
|
|
|
|
SET deleted=1
|
|
|
|
WHERE id = ${req.params.id}
|
|
|
|
`);
|
|
|
|
|
2021-07-02 16:15:44 -07:00
|
|
|
await caches.terms.invalidate();
|
|
|
|
|
2020-12-18 08:32:18 -08:00
|
|
|
return res.json('ok');
|
2021-06-09 05:47:08 -07:00
|
|
|
}));
|
2020-12-18 08:32:18 -08:00
|
|
|
|
|
|
|
export default router;
|