2020-10-16 14:32:51 -07:00
|
|
|
import {renderJson} from "../src/helpers";
|
|
|
|
|
2020-10-12 02:46:26 -07:00
|
|
|
const dbConnection = require('./db');
|
2020-08-04 07:15:41 -07:00
|
|
|
const SQL = require('sql-template-strings');
|
|
|
|
import { ulid } from 'ulid'
|
2020-10-15 11:55:24 -07:00
|
|
|
import authenticate from './authenticate';
|
2020-08-04 07:15:41 -07:00
|
|
|
|
2020-10-16 11:17:50 -07:00
|
|
|
|
2020-08-04 07:15:41 -07:00
|
|
|
|
|
|
|
const getId = url => url.match(/\/([^/]+)$/)[1];
|
|
|
|
|
|
|
|
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`
|
|
|
|
DELETE FROM nouns
|
|
|
|
WHERE id = ${base_id}
|
|
|
|
`);
|
|
|
|
}
|
|
|
|
await db.get(SQL`
|
|
|
|
UPDATE nouns
|
|
|
|
SET approved = 1, base_id = NULL
|
|
|
|
WHERE id = ${id}
|
|
|
|
`);
|
|
|
|
}
|
|
|
|
|
|
|
|
const hide = async (db, id) => {
|
|
|
|
await db.get(SQL`
|
|
|
|
UPDATE nouns
|
|
|
|
SET approved = 0
|
|
|
|
WHERE id = ${id}
|
|
|
|
`);
|
|
|
|
}
|
|
|
|
|
|
|
|
const remove = async (db, id) => {
|
|
|
|
await db.get(SQL`
|
|
|
|
DELETE FROM nouns
|
|
|
|
WHERE id = ${id}
|
|
|
|
`);
|
|
|
|
}
|
|
|
|
|
2020-08-06 06:59:08 -07:00
|
|
|
const trollWords = [
|
|
|
|
'cipeusz',
|
|
|
|
'feminazi',
|
|
|
|
'bruksela',
|
|
|
|
'zboczeń',
|
|
|
|
];
|
|
|
|
|
|
|
|
const isTroll = (body) => {
|
|
|
|
const jsonBody = JSON.stringify(body);
|
|
|
|
for (let trollWord of trollWords) {
|
|
|
|
if (jsonBody.indexOf(trollWord) > -1) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-08-04 07:15:41 -07:00
|
|
|
export default async function (req, res, next) {
|
2020-10-12 02:46:26 -07:00
|
|
|
const db = await dbConnection();
|
2020-10-15 11:55:24 -07:00
|
|
|
const user = authenticate(req);
|
|
|
|
const isAdmin = user && user.authenticated && user.roles === 'admin';
|
2020-08-04 07:15:41 -07:00
|
|
|
|
2020-10-31 05:10:25 -07:00
|
|
|
if (req.method === 'GET' && req.url.startsWith('/all/')) {
|
|
|
|
const locale = req.url.substring(5);
|
|
|
|
return renderJson(res, await db.all(SQL`
|
2020-08-04 07:15:41 -07:00
|
|
|
SELECT * FROM nouns
|
2020-10-31 05:10:25 -07:00
|
|
|
WHERE locale = ${locale}
|
|
|
|
AND approved >= ${isAdmin ? 0 : 1}
|
2020-08-04 07:15:41 -07:00
|
|
|
ORDER BY approved, masc
|
2020-10-16 14:32:51 -07:00
|
|
|
`));
|
|
|
|
}
|
|
|
|
|
2020-10-31 05:10:25 -07:00
|
|
|
if (req.method === 'POST' && req.url.startsWith('/submit/')) {
|
|
|
|
const locale = req.url.substring(8);
|
2020-08-06 06:59:08 -07:00
|
|
|
if (isAdmin || !isTroll(req.body.data)) {
|
|
|
|
const id = ulid()
|
|
|
|
await db.get(SQL`
|
2020-10-31 05:10:25 -07:00
|
|
|
INSERT INTO nouns (id, masc, fem, neutr, mascPl, femPl, neutrPl, approved, base_id, locale)
|
2020-08-06 06:59:08 -07:00
|
|
|
VALUES (
|
|
|
|
${id},
|
|
|
|
${req.body.data.masc.join('|')}, ${req.body.data.fem.join('|')}, ${req.body.data.neutr.join('|')},
|
|
|
|
${req.body.data.mascPl.join('|')}, ${req.body.data.femPl.join('|')}, ${req.body.data.neutrPl.join('|')},
|
2020-10-31 05:10:25 -07:00
|
|
|
0, ${req.body.data.base}, ${locale}
|
2020-08-06 06:59:08 -07:00
|
|
|
)
|
|
|
|
`);
|
|
|
|
if (isAdmin) {
|
|
|
|
await approve(db, id);
|
|
|
|
}
|
2020-08-04 07:15:41 -07:00
|
|
|
}
|
2020-10-16 14:32:51 -07:00
|
|
|
return renderJson(res, 'ok');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (req.method === 'POST' && req.url.startsWith('/approve/') && isAdmin) {
|
2020-10-15 11:55:24 -07:00
|
|
|
await approve(db, getId(req.url));
|
2020-10-16 14:32:51 -07:00
|
|
|
return renderJson(res, 'ok');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (req.method === 'POST' && req.url.startsWith('/hide/') && isAdmin) {
|
2020-10-15 11:55:24 -07:00
|
|
|
await hide(db, getId(req.url));
|
2020-10-16 14:32:51 -07:00
|
|
|
return renderJson(res, 'ok');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (req.method === 'POST' && req.url.startsWith('/remove/') && isAdmin) {
|
2020-10-15 11:55:24 -07:00
|
|
|
await remove(db, getId(req.url));
|
2020-10-16 14:32:51 -07:00
|
|
|
return renderJson(res, 'ok');
|
2020-08-04 07:15:41 -07:00
|
|
|
}
|
|
|
|
|
2020-10-16 14:32:51 -07:00
|
|
|
return renderJson(res, {error: 'Not found'}, 404);
|
2020-08-04 07:15:41 -07:00
|
|
|
}
|