|
|
|
@ -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
|
|
|
|
|
}); |
|
|
|
|
}; |