diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..b58b603 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,5 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ diff --git a/.idea/discord.xml b/.idea/discord.xml new file mode 100644 index 0000000..d8e9561 --- /dev/null +++ b/.idea/discord.xml @@ -0,0 +1,7 @@ + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..9f580c9 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/natsuki-api.iml b/.idea/natsuki-api.iml new file mode 100644 index 0000000..0c8867d --- /dev/null +++ b/.idea/natsuki-api.iml @@ -0,0 +1,12 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/api/ani/v1/routes/series/add.js b/api/ani/v1/routes/series/add.js index 1935fe5..082b3ae 100644 --- a/api/ani/v1/routes/series/add.js +++ b/api/ani/v1/routes/series/add.js @@ -1,67 +1,117 @@ +// how many hours have i spent banging my head at the wall now? + module.exports = (app, router) => { const Anime = app.db.models.ani.series; router.route('/:id') .post(app.auth.token, app.auth.perms('series-submit'), app.auth.permsPass('series-approve'), async (req, res) => { - let submitting = req.unauthorized; //if user doesn't have series-approve, they still have perms to submit by this point + let submitting = req.unauthorized; //if user doesn't have series-approve, they still have perms to submit by this point - /**REQUIRED ITEMS - * - *!Submissions must include a name and id (romaji included) - * Only altNames, tags, and streaming locations can be omitted - * - * If a request has submissible criteria but not completable - * criteria, it will be treated as a submission even if the user - * is authorized to approve series. - */ + /**REQUIRED ITEMS + * + *!Submissions must include a name and id (romaji included) + * Only altNames, tags, and streaming locations can be omitted + * + * If a request has submissible criteria but not completable + * criteria, it will be treated as a submission even if the user + * is authorized to approve series. + */ - //TODO submit anyways if incomplete but user has approval permissions - //TODO un-submitted (incomplete) but curated route + //TODO submit anyways if incomplete but user has approval permissions + //TODO un-submitted (incomplete) but curated route - - if (!req.params.id) {return res.status(400).send("You didn't include an anime ID in your request!");} - if (await Anime.findOne({id: req.params.id})) {return res.status(400).send("An anime already exists with that ID!");} - 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.romaji.length > 150 - || !req.params.id.match(/^[\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({ - id: req.params.id, - numericalId: app.cache.seriesCount + 1, - name: req.body.name, - romaji: req.body.romaji, - synopsis: { - by: req.body.synopsis ? req.authenticatedUser.id : null, - synopsis: req.body.synopsis || "A synopsis is not yet available for this series..." - }, - meta: { - submitted: submitting ? req.authenticatedUser.id : false, //TODO make sure to update submitted status - creator: req.authenticatedUser.id, - edits: [{ - user: req.authenticatedUser.id, - action: 'Submitted series', - timestamp: new Date().getTime() - }] - }, - genres: req.body.genres && Array.isArray(req.body.genres) && req.body.genres.length ? req.body.genres : [] - }); - if (!req.body.synopsis || !req.body.genres || !Array.isArray(req.body.genres) || !req.body.genres.length) {series.meta.submitted = req.authenticatedUser.id;} - return series.save().then(async () => { - app.cache.series[series.id] = { - id: series.id, - name: series.name, - romaji: series.romaji, - kanji: series.kanji, - altNames: series.altNames, - genres: series.genres, - tags: series.tags + + if (!req.params.id) {return res.status(400).send("You didn't include an anime ID in your request!");} + if (await Anime.findOne({id: req.params.id})) {return res.status(400).send("An anime already exists with that ID!");} + 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.romaji.length > 150 + || !req.params.id.match(/^[\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({ + id: req.params.id, + numericalId: app.cache.seriesCount + 1, + name: req.body.name, + romaji: req.body.romaji, + synopsis: { + by: req.body.synopsis ? req.authenticatedUser.id : null, + synopsis: req.body.synopsis || "A synopsis is not yet available for this series..." + }, + meta: { + submitted: submitting ? req.authenticatedUser.id : false, //TODO make sure to update submitted status + creator: req.authenticatedUser.id, + edits: [{ + user: req.authenticatedUser.id, + action: 'Submitted series', + timestamp: new Date().getTime() + }] + }, + genres: req.body.genres && Array.isArray(req.body.genres) && req.body.genres.length ? req.body.genres : [] + }); + if (!req.body.synopsis || !req.body.genres || !Array.isArray(req.body.genres) || !req.body.genres.length) {series.meta.submitted = req.authenticatedUser.id;} + + //VALIDATION + + const badReq = msg => {res.status(400).send(msg); return null;}; + const validateStringList = (maxLength, list, listName, regex) => { + let bad = false; + list.forEach(item => {bad = !bad && typeof item === 'string' && item.length < maxLength && (!regex || item.match(regex))}); + if (bad) {return badReq();} + return list; }; - return res.send(`Your series was successfully ${series.meta.submitted ? 'submitted' : "added"}.`); - }).catch((e) => {console.error(e); res.status(500).send("There was an error trying to process your request. It's likely that our database found something wrong with your body fields, and the server didn't realize. Check your request and try again.");}); - //TODO remove console error - }); + let options = req.body; + if (options.altNames && Array.isArray(options.altNames) && options.altNames.length <= 10) { + let bad = false; + options.altNames.forEach(name => bad = !bad && typeof name === 'string' && name.length < 150); + if (bad) {return badReq("Your altNames did not contain purely strings, or one of them was too long.");} + series.altNames = options.altNames; + } + + if (options.tags && Array.isArray(options.tags) && options.tags.length <= 10) { + let bad = false; + options.tags.forEach(tag => bad = !bad && typeof tag === 'string' && tag.length < 25 && tag.match(/^[\w-]+$/gm)); + if (bad) {return badReq("Your tags did not contain purely strings, or one of them was too long, or contained invalud characters");} + series.tags = options.tags.map(tag => tag.toLowerCase()); + } + if (options.nsfw === true && !(options.nsfwReason || ['gore', 'language', 'nudity', 'themes'].includes(options.nsfwReason))) {return badReq("You marked this series as nsfw, but did not provide a reason.");} + else if (options.nsfw) { + series.nsfw = options.nsfw; + series.nsfwReason = options.nsfwReason; + } + if (options.genres && Array.isArray(options.genres) && options.genres.length <= 10) { + let bad = false; + options.genres.forEach(tag => bad = !bad && typeof tag === 'string' && tag.length < 25 && tag.match(/^[\w- ]+$/gm)); + if (bad) {return badReq("Your genres did not contain purely strings, or one of them was too long.");} + series.genres = options.genres; + } //TODO genres as DB + if (options.streamAt && Array.isArray(options.streamAt) && options.streamAt.length <= 10) { + let bad = false; + options.streamAt.forEach(tag => bad = !bad && typeof tag === 'string' && tag.length < 25 && tag.match(/^[\w- ]+$/gm)); + if (bad) {return badReq("Your streamAt locations did not contain purely strings, or one of them was too long.");} + series.streamAt = options.streamAt; + } + if (options.publishers && Array.isArray(options.publishers) && options.publishers.length <= 10) { + let bad = false; + options.publishers.forEach(tag => bad = !bad && typeof tag === 'string' && tag.length < 25 && tag.match(/^[\w- ]+$/gm)); + if (bad) {return badReq("Your publishers did not contain purely strings, or one of them was too long.");} + series.publishers = options.publishers; + } + + return series.save().then(async () => { + app.cache.series[series.id] = { + id: series.id, + name: series.name, + romaji: series.romaji, + kanji: series.kanji, + altNames: series.altNames, + genres: series.genres, + tags: series.tags + }; + return res.send(`Your series was successfully ${series.meta.submitted ? 'submitted' : "added"}.`); + }).catch((e) => {console.error(e); res.status(500).send("There was an error trying to process your request. It's likely that our database found something wrong with your body fields, and the server didn't realize. Check your request and try again.");}); + //TODO remove console error + }); }; \ No newline at end of file diff --git a/db/ani/series.js b/db/ani/series.js index 346aaa7..c1eeb59 100644 --- a/db/ani/series.js +++ b/db/ani/series.js @@ -14,7 +14,7 @@ module.exports = (connection) => connection.model('series', new Schema({ completed: {type: Boolean, default: false}, //SUBMISSION completed approved: {type: Schema.Types.Mixed, default: false}, //boolean or {approved: Boolean, by: } submitted: Schema.Types.Mixed, //can be false or a string with the ID of the submitter, //!REQ - hidden: {type: Boolean, defualt: false}, + hidden: {type: Boolean, default: false}, reviewFlags: {type: [{ by: String, reason: String, @@ -24,28 +24,28 @@ module.exports = (connection) => connection.model('series', new Schema({ name: {type: String, required: true, maxLength: 150}, //!REQ romaji: {type: String, required: true, maxLength: 150}, //!REQ kanji: {type: String, maxLength: 150, default: null}, - altNames: {type: [String], default: []}, + altNames: {type: [{type: String, maxLength: 150}], default: []}, synopsis: {type: { synopsis: {type: String, required: true, maxLength: 1000}, by: String //uid }, required: true}, //if not present, use "Synopsis not available yet" //!REQ - genres: {type: [String], required: true}, //!REQ - //TODOdatabase for genres or cache - tags: {default: [], type: [String]}, + genres: {type: [{type: String, maxLength: 25}], required: true}, //!REQ + //TODO database for genres or cache + tags: {default: [], type: [{type: String, maxLength: 25}]}, nsfw: {type: Boolean, default: false}, nsfwReason: {type: String, default: null}, //gore, language, nudity, strong themes completed: {type: Boolean, default: false}, //SERIES completed - streamAt: {type: [String], default: []}, - publishers: {type: [String], default: []}, - studios: {type: [String], default: []}, + streamAt: {type: [{type: String, maxLength: 25}], default: []}, + publishers: {type: [{type: String, maxLength: 50}], default: []}, + studios: {type: [{type: String, maxLength: 50}], default: []}, air: { from: {type: String, default: null}, //absence of start date means anime is confirmed but not released //TODO special handling for unstarted series - to: {type: String, defualt: null} //null indicates still airing; completed: true + non-null "to" value means series is waiting on another season + to: {type: String, default: null} //null indicates still airing; completed: true + non-null "to" value means series is waiting on another season }, externalLinks: {type: Object, default: {}}, //streaming services, other databases, etc. //TODO externalLinks - officialSite: {type: String, defualt: null}, + officialSite: {type: String, default: null}, videos: {type: Object, default: {}}, //OPs, EDs, trailers, etc. art: { @@ -72,7 +72,7 @@ module.exports = (connection) => connection.model('series', new Schema({ soundtrack: Number, animation: Number }, - comments: String + comments: {type: String, maxLength: 2000} }], default: []}, /** diff --git a/package.json b/package.json index 676fc00..114a449 100644 --- a/package.json +++ b/package.json @@ -18,7 +18,8 @@ "mongoose": "^6.8.4" }, "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" + "start": "cd api && node index", + "test": "node test" }, "keywords": [], "author": "wubzygd",