Compare commits

...

2 Commits

  1. 5
      api/ani/v1/router.js
  2. 1
      api/ani/v1/routes/series.js
  3. 10
      api/ani/v1/routes/series/add.js
  4. 45
      api/ani/v1/routes/series/edits.js
  5. 10
      api/index.js
  6. 7
      api/util/misc/rtcolors.js
  7. 3
      db/ani/series.js

@ -1,11 +1,14 @@
const fs = require('fs');
const {Router} = require("express");
const chalk = require("chalk");
const reqTypeColors = require('../../util/misc/rtcolors');
const router = Router();
module.exports = app => {
router
.use((req, res, next) => {console.log(`[ANI/v1] ${req.path} | [${req.method}]`); next()})
.use((req, res, next) => {console.log(`${chalk.gray(`[ANI/v1]`)} ${req.path} ${chalk.gray(`|`)} [${chalk[reqTypeColors[req.method]](req.method)}]`); next()})
.get('/', (req, res) => res.send("This is the Natsuki Anime DB API v1 head."))
fs.readdirSync('./ani/v1/routes').filter(file => file.endsWith('.js'))

@ -6,6 +6,7 @@ module.exports = (app, parentRouter) => {
router.use('/', (req, res, next) => {
req.listData = Object.keys(app.cache.series).map(series => {
return {
id: series,
name: app.cache.series[series].name,
romaji: app.cache.series[series].romaji,
kanji: app.cache.series[series].kanji

@ -105,8 +105,8 @@ 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
if (!series || (series && !series.meta.completed && req.unauthorized)) {
const series = req.params.id.match(/^\d+$/) ? await Anime.findOne({numericalId: req.params.id.toLowerCase()}) : await Anime.findOne({id: req.params.id.toLowerCase()}); //TODO make sure all ID calls are lowercased
if (!series || (series && !series.meta.completed && !series.meta.viewable && req.unauthorized)) {
if (req.params.id.toLowerCase() === 'queue') {return next();}
return res.status(400).send("A series with that ID doesn't exist!");
}
@ -117,4 +117,8 @@ module.exports = (app, router) => {
meta: {completed: series.meta.completed, creator: series.meta.creator, locked: series.meta.locked}
});
});
}; //TODO check that series can be submitted but not completed. consider making this not allowed, or figure out what to do about the fact that editing all the fields is a complete pain
}; /**TODO check that series can be submitted but not completed. consider making this not allowed, or figure out what to do about the fact that editing all the fields is a complete pain
* a series must have an ID, name, romaji, and genres listed to be submitted
* series can be submitted with those but making a series public requires more fields
* for special cases, a series can be marked by an admin as viewable (confirmed second season or series, etc)
*/

@ -53,51 +53,6 @@ module.exports = (app, router) => {
].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'))

@ -5,6 +5,8 @@ const helmet = require('helmet');
const {set, createConnection} = require('mongoose');
const chalk = require('chalk');
const reqTypeColors = require('./util/misc/rtcolors');
const app = express();
set('strictQuery', false);
@ -49,14 +51,6 @@ server = app.listen(4062, async () => {
require('./v1/index')(app); //initialize bot API branch
require('./ani/index')(app); //initialize ani API branch
const reqTypeColors = {
GET: 'greenBright',
POST: 'blueBright',
PATCH: 'yellowBright',
PUT: 'yellow',
DELETE: 'redBright'
};
const checkMiddleware = (middleware, string) => {
if (middleware.route) {Object.keys(middleware.route.methods).forEach(method => {
if (middleware.route.methods[method]) {console.log(`${chalk.gray(`[`)}${chalk[reqTypeColors[method.toUpperCase()]](method.toUpperCase())}${chalk.gray(`]`)} ${string}${middleware.route.path}`);}

@ -0,0 +1,7 @@
module.exports = {
GET: 'greenBright',
POST: 'blueBright',
PATCH: 'yellowBright',
PUT: 'yellow',
DELETE: 'redBright'
};

@ -12,13 +12,14 @@ module.exports = (connection) => connection.model('series', new Schema({
action: String
}], default: []},
completed: {type: Boolean, default: false}, //SUBMISSION completed
viewable: {type: Boolean, default: false}, // exists solely as completion override. WILL NOT make a completed series invisible if false
approved: {type: Schema.Types.Mixed, default: false}, //boolean or {approved: Boolean, by: <uid>}
submitted: Schema.Types.Mixed, //can be false or a string with the ID of the submitter, //!REQ
hidden: {type: Boolean, default: false},
reviewFlags: {type: [{
by: String,
reason: String,
}], default: []}
}], default: []} // notes left by an admin or curator about work that needs to be done on a series for it to be approved
}, //!REQ
name: {type: String, required: true, maxLength: 150}, //!REQ

Loading…
Cancel
Save