From dfb25f0d078b84db07c9916fb0443e5e58bd29da Mon Sep 17 00:00:00 2001 From: WubzyGD Date: Thu, 6 Jul 2023 02:02:32 -0400 Subject: [PATCH] couldn't even begin to tell you what's new --- api/ani/v1/routes/series.js | 3 +- api/ani/v1/routes/series/add.js | 8 +-- api/ani/v1/routes/series/edits.js | 85 ++++++++++++++++++++++++++----- api/index.js | 1 + api/util/editstring.js | 30 +++++++++++ db/ani/series.js | 2 +- 6 files changed, 111 insertions(+), 18 deletions(-) create mode 100644 api/util/editstring.js diff --git a/api/ani/v1/routes/series.js b/api/ani/v1/routes/series.js index 041dc6c..dbd85ca 100644 --- a/api/ani/v1/routes/series.js +++ b/api/ani/v1/routes/series.js @@ -4,7 +4,8 @@ const fs = require('fs'); module.exports = (app, parentRouter) => { const router = Router() router.use('/', (req, res, next) => { - req.listData = Object.keys(app.cache.series).map(series => {return { + req.listData = Object.keys(app.cache.series).map(series => { + return { name: app.cache.series[series].name, romaji: app.cache.series[series].romaji, kanji: app.cache.series[series].kanji diff --git a/api/ani/v1/routes/series/add.js b/api/ani/v1/routes/series/add.js index a723893..b9d0782 100644 --- a/api/ani/v1/routes/series/add.js +++ b/api/ani/v1/routes/series/add.js @@ -1,5 +1,7 @@ // how many hours have i spent banging my head at the wall now? +// i literally dont have internet i wrote all of this forever ago im doing this out of sheer lack of other options WHY DID I WRITE THIS + module.exports = (app, router) => { const Anime = app.db.models.ani.series; @@ -26,9 +28,9 @@ module.exports = (app, router) => { if ( !req.body //i just ate dinner and i can't even think straight || !req.body.name || !req.body.romaji || !req.params.id || !req.authenticatedUser || !req.authenticatedUser.id - || !req.body.name.match(/^[\w_\- ]+$/gm) || req.body.name.length > 150 + || !req.body.name.match(/^[\w_\-!?.:; ]+$/gm) || req.body.name.length > 150 || req.body.romaji.length > 150 - || !req.params.id.match(/^(?=.*[a-zA-Z])[\w\-]+$/gm) || req.params.id.length > 25 + || !req.params.id.match(/^[0-9_-]*[a-zA-Z][\w-]*$/gm) || req.params.id.length > 25 ) {return res.status(400).send("The server cannot accept your request as your body is missing fields or is malformed. Ensure fields aren't too long and that they don't contain illegal characters.");} let series = new Anime({ @@ -103,7 +105,7 @@ module.exports = (app, router) => { //TODO remove console error }) .get(app.auth.tokenPass, app.auth.permsPass('series-approve'), async (req, res, next) => { - const series = await Anime.findOne({$or: [{id: req.params.id.toLowerCase()}, {numericalId: req.params.id}]}); //TODO make sure all ID calls are lowercased + const series = await Anime.findOne(/*{$or: [*/{id: req.params.id.toLowerCase()}/*, {numericalId: req.params.id}]}*/); //TODO make sure all ID calls are lowercased if (!series || (series && !series.meta.completed && req.unauthorized)) { if (req.params.id.toLowerCase() === 'queue') {return next();} return res.status(400).send("A series with that ID doesn't exist!"); diff --git a/api/ani/v1/routes/series/edits.js b/api/ani/v1/routes/series/edits.js index d8b9801..3cffde4 100644 --- a/api/ani/v1/routes/series/edits.js +++ b/api/ani/v1/routes/series/edits.js @@ -10,8 +10,20 @@ module.exports = (app, router) => { return true; } + const pushEdit = (action, req, series) => { + series.meta.edits.push({ + user: req.authenticatedUser.id, + timestamp: new Date().getTime(), + action + }); + series.markModified('meta.edits'); + }; + + const edits = app.util.editString(pushEdit, editCheck, router, Anime); + router.route('/:id/synopsis') //completed i think? .patch(app.auth.token, app.auth.perms('series-submit'), async (req, res, next) => { + if (!req.params.id) {return;} const series = await Anime.findOne({id: req.params.id.toLowerCase()}); if (!editCheck(series, req, res)) {return;} if (!req.body.synopsis) {return res.status(400).send("You did not provide a new synopsis in your body.");} @@ -19,19 +31,14 @@ module.exports = (app, router) => { try { series.synopsis.synopsis = req.body.synopsis; if (!series.synopsis.by) {series.synopsis.by = req.authenticatedUser.id;} - if (req.body.updateAuthor === true && series.synopsis.by !== req.authenticatedUser.id) {series.synopsis.by = req.authenticatedUser.id;} + if (req.body.updateAuthor === true && series.synopsis.by !== req.authenticatedUser.id) {series.synopsis.by = req.authenticatedUser.id;} // TODO doc this extraneous body item + pushEdit("Updated synopsis", req, series); + series.markModified('synopsis.synopsis'); + return series.save() + .then(() => res.send("Synopsis updated.")) + .catch(() => res.status(500).send("There was an error trying to update your synopsis. Please try again.")); } catch {return res.status(500).send("There was an error trying to update your synopsis. Please try again.");} - series.meta.edits.push({ - user: req.authenticatedUser.id, - timestamp: new Date().getTime(), - action: "Updated synopsis" - }); - series.markModified('synopsis.synopsis'); - series.markModified('meta.edits'); - return series.save() - .then(() => res.send("Synopsis updated.")) - .catch(() => res.status(500).send("There was an error trying to update your synopsis. Please try again.")); }) .get(app.auth.tokenPass, app.auth.permsPass('series-approve'), async (req, res) => { //working const series = await Anime.findOne({id: req.params.id.toLowerCase()}); @@ -39,8 +46,60 @@ module.exports = (app, router) => { return res.send({synopsis: series.synopsis.synopsis, by: series.synopsis.by}); }); - //router.route() + [ + ['name', x => x.match(/^[\w_\-!?.:; ]+$/gm) && x.length < 150], + ['romaji', x => x.length < 150], + ['kanji', x => x.length < 150] + + ].forEach(field => edits.stringWrap(field[0], field[1])); + + /*router.patch('/:id/name', app.auth.token, app.auth.perms('series-submit'), async (req, res) => { + if (!req.params.id) {return;} + const series = await Anime.findOne({id: req.params.id.toLowerCase()}); + if (!editCheck(series, req, res)) {return;} + if (!req.body.name) {return res.status(400).send("You did not provide a new name in your body.");} + if (!req.body.name.match(/^[\w_\-!?.:; ]+$/gm) || req.body.name.length > 150) {return res.status(400).send("Your new name is too long or contains illegal characters.");} + try { + series.name = req.body.name.trim(); + pushEdit("Updated name", req, series); + return series.save() + .then(() => res.send("Name updated.")) + .catch(() => res.status(500).send("There was an error trying to update your name. Please try again.")); + } catch {return res.status(500).send("There was an error trying to update your name. Please try again.");} + }); + + router.patch('/:id/romaji', app.auth.token, app.auth.perms('series-submit'), async (req, res) => { + if (!req.params.id) {return;} + const series = await Anime.findOne({id: req.params.id.toLowerCase()}); + if (!editCheck(series, req, res)) {return;} + if (!req.body.romaji) {return res.status(400).send("You did not provide a new romanized name (romaji) in your body.");} + if (req.body.romaji.length > 150) {return res.status(400).send("Your new romanized name is too long or contains illegal characters.");} + try { + series.romaji = req.body.romaji.trim(); + pushEdit("Updated romaji", req, series); + return series.save() + .then(() => res.send("Romanized name updated.")) + .catch(() => res.status(500).send("There was an error trying to update your romaji. Please try again.")); + } catch {return res.status(500).send("There was an error trying to update your romaji. Please try again.");} + }); + + router.patch('/:id/kanji', app.auth.token, app.auth.perms('series-submit'), async (req, res) => { + if (!req.params.id) {return;} + const series = await Anime.findOne({id: req.params.id.toLowerCase()}); + if (!editCheck(series, req, res)) {return;} + if (!req.body.kanji) {return res.status(400).send("You did not provide a new kanji in your body.");} + if (req.body.kanji.length > 150) {return res.status(400).send("Your new kanji is too long or contains illegal characters.");} + try { + series.kanji = req.body.kanji.trim(); + pushEdit("Updated kanji", req, series); + return series.save() + .then(() => res.send("Kanji updated.")) + .catch(() => res.status(500).send("There was an error trying to update your kanji. Please try again.")); + } catch {return res.status(500).send("There was an error trying to update your kanji. Please try again.");} + });*/ + + - router.use('/:id/altnames', app.auth.tokenPass, app.auth.permsPass('series-approve')) + //router.use('/:id/altnames', app.auth.tokenPass, app.auth.permsPass('series-approve')) }; \ No newline at end of file diff --git a/api/index.js b/api/index.js index b2b851e..bf5f396 100644 --- a/api/index.js +++ b/api/index.js @@ -41,6 +41,7 @@ server = app.listen(4062, async () => { app.util = {}; app.util.list = require('./util/list'); + app.util.editString = require('./util/editstring').masterInit(app); //TODO consistency shithead await require('./util/startup/cache')(app); diff --git a/api/util/editstring.js b/api/util/editstring.js new file mode 100644 index 0000000..ced7390 --- /dev/null +++ b/api/util/editstring.js @@ -0,0 +1,30 @@ +module.exports = { + masterInit: (app) => { + return (pushEdit, editCheck, router, model) => { + return { + string: async (req, res, name, match) => { + if (!req.params.id) {return;} + const series = await model.findOne({id: req.params.id.toLowerCase()}); + if (!editCheck(series, req, res)) {return;} + if (!req.body[name]) {return res.status(400).send(`You did not provide a new ${name} in your body.`);} + if (!match(req.body[name])) {return res.status(400).send(`Your new ${name} is too long or contains illegal characters.`);} + try { + series[name] = req.body[name].trim(); + pushEdit(`Updated ${name}`, req, series); + return series.save() + .then(() => res.send(`${name.charAt(0)}${name.slice(1)} updated.`)) + .catch(() => res.status(500).send(`There was an error trying to update your ${name}. Please try again.`)); + } catch {return res.status(500).send(`There was an error trying to update your ${name}. Please try again.`);} + }, + + stringWrap: (name, match) => { + const route = router.route(`/:id/${name}`); + route.patch(app.auth.token, app.auth.permsPass('series-approve'), async (req, res) => { + await this.string(req, res, name, match); + }); + return route + } + } + } + } +} \ No newline at end of file diff --git a/db/ani/series.js b/db/ani/series.js index c1eeb59..0fddfaa 100644 --- a/db/ani/series.js +++ b/db/ani/series.js @@ -84,5 +84,5 @@ module.exports = (connection) => connection.model('series', new Schema({ seasons: {type: [String], default: []}, characters: {type: [String], default: []}, - related: {type: [String], default: []} + related: {type: [String], default: []} //TODO this shit even necessary? })); \ No newline at end of file