2020-10-31 13:33:59 -07:00
|
|
|
import { Router } from 'express';
|
2020-12-04 13:09:57 -08:00
|
|
|
import SQL from "sql-template-strings";
|
|
|
|
import {ulid} from "ulid";
|
2021-08-11 14:12:55 -07:00
|
|
|
import {clearKey, handleErrorAsync} from "../../src/helpers";
|
2020-11-09 06:39:18 -08:00
|
|
|
|
2020-12-04 13:09:57 -08:00
|
|
|
const approve = async (db, id) => {
|
|
|
|
const { base_id } = await db.get(SQL`SELECT base_id FROM sources WHERE id=${id}`);
|
|
|
|
if (base_id) {
|
|
|
|
await db.get(SQL`
|
|
|
|
UPDATE sources
|
|
|
|
SET deleted=1
|
|
|
|
WHERE id = ${base_id}
|
|
|
|
`);
|
2020-10-31 13:33:59 -07:00
|
|
|
}
|
2020-12-04 13:09:57 -08:00
|
|
|
await db.get(SQL`
|
|
|
|
UPDATE sources
|
|
|
|
SET approved = 1, base_id = NULL
|
|
|
|
WHERE id = ${id}
|
|
|
|
`);
|
2020-11-10 14:41:56 -08:00
|
|
|
}
|
|
|
|
|
2021-09-07 12:01:41 -07:00
|
|
|
const keyGetMain = (key) => {
|
|
|
|
return key.split('/')[0];
|
|
|
|
}
|
|
|
|
|
2021-01-04 15:07:10 -08:00
|
|
|
const linkOtherVersions = async (req, sources) => {
|
2021-09-07 12:01:41 -07:00
|
|
|
const keys = new Set(sources.filter(s => !!s && s.key).map(s => keyGetMain(clearKey(s.key))));
|
2021-01-04 15:07:10 -08:00
|
|
|
|
2021-09-07 12:01:41 -07:00
|
|
|
let sql = SQL`
|
2021-01-04 15:07:10 -08:00
|
|
|
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-04 15:07:10 -08:00
|
|
|
AND s.deleted = 0
|
|
|
|
AND s.approved >= ${req.isGranted('sources') ? 0 : 1}
|
2021-09-07 12:01:41 -07:00
|
|
|
AND (`;
|
|
|
|
for (let key of keys) {
|
|
|
|
sql = sql.append(SQL`s.key = ${key} OR s.key LIKE ${key + '/%'} OR `)
|
|
|
|
}
|
|
|
|
sql = sql.append(SQL`0=1)`);
|
|
|
|
|
|
|
|
const otherVersions = await req.db.all(sql);
|
2021-01-04 15:07:10 -08:00
|
|
|
|
|
|
|
const otherVersionsMap = {};
|
|
|
|
otherVersions.forEach(version => {
|
2021-09-07 12:01:41 -07:00
|
|
|
const k = keyGetMain(version.key);
|
|
|
|
if (otherVersionsMap[k] === undefined) {
|
|
|
|
otherVersionsMap[k] = [];
|
2021-01-04 15:07:10 -08:00
|
|
|
}
|
2021-09-07 12:01:41 -07:00
|
|
|
otherVersionsMap[k].push(version);
|
2021-01-04 15:07:10 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
return sources.map(s => {
|
2021-09-07 12:01:41 -07:00
|
|
|
s.versions = s.key ? otherVersionsMap[keyGetMain(s.key)] || [] : [];
|
2021-01-04 15:07:10 -08:00
|
|
|
return s;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2020-10-31 13:33:59 -07:00
|
|
|
const router = Router();
|
|
|
|
|
2021-06-09 05:47:08 -07:00
|
|
|
router.get('/sources', handleErrorAsync(async (req, res) => {
|
2021-03-24 11:29:25 -07:00
|
|
|
let sql = SQL`
|
2020-12-04 13:09:57 -08:00
|
|
|
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}
|
2020-12-04 13:09:57 -08:00
|
|
|
AND s.deleted = 0
|
2020-12-30 15:03:30 -08:00
|
|
|
AND s.approved >= ${req.isGranted('sources') ? 0 : 1}
|
2021-03-24 11:29:25 -07:00
|
|
|
`;
|
|
|
|
if (req.query.pronoun) {
|
|
|
|
sql.append(SQL`AND s.pronouns LIKE ${'%' + req.query.pronoun + '%'}`)
|
|
|
|
}
|
|
|
|
return res.json(await linkOtherVersions(req, await req.db.all(sql)));
|
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.get('/sources/:id', handleErrorAsync(async (req, res) => {
|
2021-01-04 15:07:10 -08:00
|
|
|
return res.json(await linkOtherVersions(req, await req.db.all(SQL`
|
2020-12-04 13:09:57 -08:00
|
|
|
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}
|
2020-12-04 13:09:57 -08:00
|
|
|
AND s.deleted = 0
|
2020-12-30 15:03:30 -08:00
|
|
|
AND s.approved >= ${req.isGranted('sources') ? 0 : 1}
|
2020-12-04 13:09:57 -08:00
|
|
|
AND s.id = ${req.params.id}
|
2021-01-04 15:07:10 -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('/sources/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-12-04 13:09:57 -08:00
|
|
|
const id = ulid();
|
|
|
|
await req.db.get(SQL`
|
2022-02-28 04:07:39 -08:00
|
|
|
INSERT INTO sources (id, locale, pronouns, type, author, title, extra, year, fragments, comment, link, key, images, spoiler, submitter_id, base_id)
|
2020-12-04 13:09:57 -08:00
|
|
|
VALUES (
|
2021-06-17 16:10:59 -07:00
|
|
|
${id}, ${global.config.locale}, ${req.body.pronouns.join(';')},
|
2020-12-04 13:09:57 -08:00
|
|
|
${req.body.type}, ${req.body.author}, ${req.body.title}, ${req.body.extra}, ${req.body.year},
|
2021-01-06 06:38:05 -08:00
|
|
|
${req.body.fragments.join('@').replace(/\n/g, '|')}, ${req.body.comment}, ${req.body.link},
|
2022-02-28 04:07:39 -08:00
|
|
|
${clearKey(req.body.key)}, ${req.body.images || null}, ${req.body.spoiler ? 1 : 0},
|
2021-01-06 06:38:05 -08:00
|
|
|
${req.user ? req.user.id : null}, ${req.body.base}
|
2020-12-04 13:09:57 -08:00
|
|
|
)
|
|
|
|
`);
|
|
|
|
|
2020-12-30 15:03:30 -08:00
|
|
|
if (req.isGranted('sources')) {
|
2020-12-04 13:09:57 -08: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('/sources/hide/:id', handleErrorAsync(async (req, res) => {
|
2020-12-30 15:03:30 -08:00
|
|
|
if (!req.isGranted('sources')) {
|
2021-12-02 10:11:04 -08:00
|
|
|
return res.status(401).json({error: 'Unauthorised'});
|
2020-10-31 13:33:59 -07:00
|
|
|
}
|
|
|
|
|
2020-12-04 13:09:57 -08:00
|
|
|
await req.db.get(SQL`
|
|
|
|
UPDATE sources
|
|
|
|
SET approved = 0
|
|
|
|
WHERE id = ${req.params.id}
|
|
|
|
`);
|
|
|
|
|
|
|
|
return res.json('ok');
|
2021-06-09 05:47:08 -07:00
|
|
|
}));
|
2020-12-04 13:09:57 -08:00
|
|
|
|
2021-06-09 05:47:08 -07:00
|
|
|
router.post('/sources/approve/:id', handleErrorAsync(async (req, res) => {
|
2020-12-30 15:03:30 -08:00
|
|
|
if (!req.isGranted('sources')) {
|
2021-12-02 10:11:04 -08:00
|
|
|
return res.status(401).json({error: 'Unauthorised'});
|
2020-12-04 13:09:57 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
await approve(req.db, req.params.id);
|
|
|
|
|
|
|
|
return res.json('ok');
|
2021-06-09 05:47:08 -07:00
|
|
|
}));
|
2020-12-04 13:09:57 -08:00
|
|
|
|
2021-06-09 05:47:08 -07:00
|
|
|
router.post('/sources/remove/:id', handleErrorAsync(async (req, res) => {
|
2020-12-30 15:03:30 -08:00
|
|
|
if (!req.isGranted('sources')) {
|
2021-12-02 10:11:04 -08:00
|
|
|
return res.status(401).json({error: 'Unauthorised'});
|
2020-12-04 13:09:57 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
await req.db.get(SQL`
|
|
|
|
UPDATE sources
|
|
|
|
SET deleted=1
|
|
|
|
WHERE id = ${req.params.id}
|
|
|
|
`);
|
|
|
|
|
|
|
|
return res.json('ok');
|
2021-06-09 05:47:08 -07:00
|
|
|
}));
|
2020-10-31 13:33:59 -07:00
|
|
|
|
|
|
|
export default router;
|