2020-11-17 10:21:49 -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-11-17 10:21:49 -08:00
|
|
|
|
|
|
|
const approve = async (db, id) => {
|
|
|
|
const { base_id } = await db.get(SQL`SELECT base_id FROM inclusive WHERE id=${id}`);
|
|
|
|
if (base_id) {
|
|
|
|
await db.get(SQL`
|
2020-12-03 06:01:26 -08:00
|
|
|
UPDATE inclusive
|
|
|
|
SET deleted=1
|
2020-11-17 10:21:49 -08:00
|
|
|
WHERE id = ${base_id}
|
|
|
|
`);
|
|
|
|
}
|
|
|
|
await db.get(SQL`
|
|
|
|
UPDATE inclusive
|
|
|
|
SET approved = 1, base_id = NULL
|
|
|
|
WHERE id = ${id}
|
|
|
|
`);
|
2021-07-02 16:15:44 -07:00
|
|
|
await caches.inclusive.invalidate();
|
2020-11-17 10:21:49 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
const router = Router();
|
|
|
|
|
2021-06-09 05:47:08 -07:00
|
|
|
router.get('/inclusive', handleErrorAsync(async (req, res) => {
|
2021-07-02 16:15:44 -07:00
|
|
|
return res.json(await caches.inclusive.fetch(async () => {
|
2021-07-03 04:19:43 -07:00
|
|
|
return sortClearedLinkedText(await req.db.all(SQL`
|
2021-07-02 16:15:44 -07:00
|
|
|
SELECT i.*, u.username AS author FROM inclusive i
|
|
|
|
LEFT JOIN users u ON i.author_id = u.id
|
|
|
|
WHERE i.locale = ${global.config.locale}
|
|
|
|
AND i.approved >= ${req.isGranted('inclusive') ? 0 : 1}
|
|
|
|
AND i.deleted = 0
|
2021-07-03 04:19:43 -07:00
|
|
|
`), 'insteadOf');
|
2021-07-02 16:15:44 -07:00
|
|
|
}));
|
2021-06-09 05:47:08 -07:00
|
|
|
}));
|
2020-11-17 10:21:49 -08:00
|
|
|
|
2021-06-09 05:47:08 -07:00
|
|
|
router.get('/inclusive/search/:term', handleErrorAsync(async (req, res) => {
|
2020-11-17 10:21:49 -08:00
|
|
|
const term = '%' + req.params.term + '%';
|
2021-07-03 04:19:43 -07:00
|
|
|
return res.json(sortClearedLinkedText(await req.db.all(SQL`
|
2020-12-04 13:09:57 -08:00
|
|
|
SELECT i.*, u.username AS author FROM inclusive i
|
|
|
|
LEFT JOIN users u ON i.author_id = u.id
|
2021-06-17 16:10:59 -07:00
|
|
|
WHERE i.locale = ${global.config.locale}
|
2020-12-30 15:03:30 -08:00
|
|
|
AND i.approved >= ${req.isGranted('inclusive') ? 0 : 1}
|
2020-12-04 13:09:57 -08:00
|
|
|
AND i.deleted = 0
|
|
|
|
AND (i.insteadOf like ${term} OR i.say like ${term})
|
|
|
|
ORDER BY i.approved, i.insteadOf
|
2021-07-03 04:19:43 -07:00
|
|
|
`), 'insteadOf'));
|
2021-06-09 05:47:08 -07:00
|
|
|
}));
|
2020-11-17 10:21:49 -08:00
|
|
|
|
2021-06-09 05:47:08 -07:00
|
|
|
router.post('/inclusive/submit', handleErrorAsync(async (req, res) => {
|
2020-11-17 10:21:49 -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`
|
2020-12-03 06:01:26 -08:00
|
|
|
INSERT INTO inclusive (id, insteadOf, say, because, approved, base_id, locale, author_id, categories, links)
|
2020-11-17 10:21:49 -08:00
|
|
|
VALUES (
|
|
|
|
${id},
|
|
|
|
${req.body.insteadOf.join('|')}, ${req.body.say.join('|')}, ${req.body.because},
|
2021-06-17 16:10:59 -07:00
|
|
|
0, ${req.body.base}, ${global.config.locale}, ${req.user ? req.user.id : null},
|
2020-12-03 06:01:26 -08:00
|
|
|
${req.body.categories.join(',')}, ${JSON.stringify(req.body.links)}
|
2020-11-17 10:21:49 -08:00
|
|
|
)
|
|
|
|
`);
|
|
|
|
|
2020-12-30 15:03:30 -08:00
|
|
|
if (req.isGranted('inclusive')) {
|
2020-11-17 10:21:49 -08:00
|
|
|
await approve(req.db, id);
|
|
|
|
}
|
|
|
|
|
|
|
|
return res.json('ok');
|
2021-06-09 05:47:08 -07:00
|
|
|
}));
|
2020-11-17 10:21:49 -08:00
|
|
|
|
2021-06-09 05:47:08 -07:00
|
|
|
router.post('/inclusive/hide/:id', handleErrorAsync(async (req, res) => {
|
2020-12-30 15:03:30 -08:00
|
|
|
if (!req.isGranted('inclusive')) {
|
2020-11-17 10:21:49 -08:00
|
|
|
res.status(401).json({error: 'Unauthorised'});
|
|
|
|
}
|
|
|
|
|
|
|
|
await req.db.get(SQL`
|
|
|
|
UPDATE inclusive
|
|
|
|
SET approved = 0
|
|
|
|
WHERE id = ${req.params.id}
|
|
|
|
`);
|
|
|
|
|
2021-07-02 16:15:44 -07:00
|
|
|
await caches.inclusive.invalidate();
|
|
|
|
|
2020-11-17 10:21:49 -08:00
|
|
|
return res.json('ok');
|
2021-06-09 05:47:08 -07:00
|
|
|
}));
|
2020-11-17 10:21:49 -08:00
|
|
|
|
2021-06-09 05:47:08 -07:00
|
|
|
router.post('/inclusive/approve/:id', handleErrorAsync(async (req, res) => {
|
2020-12-30 15:03:30 -08:00
|
|
|
if (!req.isGranted('inclusive')) {
|
2020-11-17 10:21:49 -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-11-17 10:21:49 -08:00
|
|
|
|
2021-06-09 05:47:08 -07:00
|
|
|
router.post('/inclusive/remove/:id', handleErrorAsync(async (req, res) => {
|
2020-12-30 15:03:30 -08:00
|
|
|
if (!req.isGranted('inclusive')) {
|
2020-11-17 10:21:49 -08:00
|
|
|
res.status(401).json({error: 'Unauthorised'});
|
|
|
|
}
|
|
|
|
|
|
|
|
await req.db.get(SQL`
|
2020-12-03 06:01:26 -08:00
|
|
|
UPDATE inclusive
|
|
|
|
SET deleted=1
|
2020-11-17 10:21:49 -08:00
|
|
|
WHERE id = ${req.params.id}
|
|
|
|
`);
|
|
|
|
|
2021-07-02 16:15:44 -07:00
|
|
|
await caches.inclusive.invalidate();
|
|
|
|
|
2020-11-17 10:21:49 -08:00
|
|
|
return res.json('ok');
|
2021-06-09 05:47:08 -07:00
|
|
|
}));
|
2020-11-17 10:21:49 -08:00
|
|
|
|
|
|
|
export default router;
|