From a848c9f0882b9105f0f625eee08cabe819eeed84 Mon Sep 17 00:00:00 2001 From: MinekPo1 <68391527+MinekPo1@users.noreply.github.com> Date: Mon, 12 Apr 2021 15:56:11 +0200 Subject: [PATCH 1/4] Added funct for specifing a tag for random/image. UNTESTED but should be fine I hope. I just copied code from one file to another. --- api/v1/routes/ImageRandomGET.js | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/api/v1/routes/ImageRandomGET.js b/api/v1/routes/ImageRandomGET.js index c2fe208..8c7d71d 100644 --- a/api/v1/routes/ImageRandomGET.js +++ b/api/v1/routes/ImageRandomGET.js @@ -17,8 +17,39 @@ class ImageRandomGET { async run(req, res) { let agg = this.database.Image.aggregate().cursor({ }); + + options = {} + if (req.query.nsfw !== undefined) - agg.match({ nsfw: req.query.nsfw == 'true' }); + options.nsfw = ['true','True',true].includes(req.query.nsfw); + + /* code copied from ImageSearchPOST.js + * written by brussell98 + */ + if (Array.isArray(req.body.tags)) + req.body.tags = req.body.tags.join(', '); + + if (req.body.tags !== undefined && req.body.tags.trim() !== '') { + /* What we are doing here is bypassing a mongodb $text restriction. + * If you only include negate expressions then nothing will match so + * we turn it into a $not regex that matches negated tags and returns + * the rest. This is needed for tag blacklists. + */ + if (req.body.tags.split(/-"?[^",]+"?(?:, *)?/).join('').trim() === '') { + options.tags = { + $nin: req.body.tags.match(/(^|, *)-[^,]+/g).map(e => e.replace(/,? *-|"/g, '')) + }; + } else { + options.$text = { $search: req.body.tags }; + + if (req.body.sort && req.body.sort === 'relevance') { + projection.score = { $meta: 'textScore' }; + sort.score = { $meta: 'textScore' }; + } + } + } + + agg.match(options) if (req.query.count !== undefined && req.query.count != '0' && /^\d{1,3}$/.test(req.query.count)) { let count = parseInt(req.query.count, 10); From 87fc72ef2d46f988490080b9240f3ec06d0c1b01 Mon Sep 17 00:00:00 2001 From: MinekPo1 <68391527+MinekPo1@users.noreply.github.com> Date: Mon, 12 Apr 2021 16:01:56 +0200 Subject: [PATCH 2/4] forgot this is in js. --- api/v1/routes/ImageRandomGET.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/v1/routes/ImageRandomGET.js b/api/v1/routes/ImageRandomGET.js index 8c7d71d..f003350 100644 --- a/api/v1/routes/ImageRandomGET.js +++ b/api/v1/routes/ImageRandomGET.js @@ -18,7 +18,7 @@ class ImageRandomGET { async run(req, res) { let agg = this.database.Image.aggregate().cursor({ }); - options = {} + let options = {}; if (req.query.nsfw !== undefined) options.nsfw = ['true','True',true].includes(req.query.nsfw); From 42f170b2db71ec113b8d1d9072c6964009ce18d6 Mon Sep 17 00:00:00 2001 From: MinekPo1 <68391527+MinekPo1@users.noreply.github.com> Date: Mon, 12 Apr 2021 16:06:52 +0200 Subject: [PATCH 3/4] I am bad at js. --- api/v1/routes/ImageRandomGET.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/api/v1/routes/ImageRandomGET.js b/api/v1/routes/ImageRandomGET.js index f003350..b4e059e 100644 --- a/api/v1/routes/ImageRandomGET.js +++ b/api/v1/routes/ImageRandomGET.js @@ -18,7 +18,9 @@ class ImageRandomGET { async run(req, res) { let agg = this.database.Image.aggregate().cursor({ }); - let options = {}; + let options = {}, + projection = { '_id': 0, '__v': 0 }, + sort = { }; if (req.query.nsfw !== undefined) options.nsfw = ['true','True',true].includes(req.query.nsfw); From 8ac301ef192a798aa3e3b3a000d04bd5f7c0520a Mon Sep 17 00:00:00 2001 From: Brussell Date: Mon, 12 Apr 2021 11:41:07 -0500 Subject: [PATCH 4/4] Fixes --- api/v1/routes/ImageRandomGET.js | 47 ++++++++++++++++----------------- 1 file changed, 23 insertions(+), 24 deletions(-) diff --git a/api/v1/routes/ImageRandomGET.js b/api/v1/routes/ImageRandomGET.js index b4e059e..7df96de 100644 --- a/api/v1/routes/ImageRandomGET.js +++ b/api/v1/routes/ImageRandomGET.js @@ -8,54 +8,53 @@ class ImageRandomGET { this.rateLimiter = new RateLimiter({ max: 5 }); // 5/10 + // NOTE: This is for backwards-compatibilitity this.router.get( this.path, this.rateLimiter.limit.bind(this.rateLimiter), this.run.bind(this) ); + + this.router.post( + this.path, + this.rateLimiter.limit.bind(this.rateLimiter), + this.run.bind(this) + ); } async run(req, res) { - let agg = this.database.Image.aggregate().cursor({ }); + const body = req.body || req.query; + + const agg = this.database.Image.aggregate().cursor({ }); - let options = {}, - projection = { '_id': 0, '__v': 0 }, - sort = { }; + const options = { }; - if (req.query.nsfw !== undefined) - options.nsfw = ['true','True',true].includes(req.query.nsfw); + if (body.nsfw !== undefined) + options.nsfw = body.nsfw === 'true' || body.nsfw === true; - /* code copied from ImageSearchPOST.js - * written by brussell98 - */ - if (Array.isArray(req.body.tags)) - req.body.tags = req.body.tags.join(', '); + if (Array.isArray(body.tags)) + body.tags = body.tags.join(', '); - if (req.body.tags !== undefined && req.body.tags.trim() !== '') { + if (body.tags !== undefined && typeof body.tags === 'string' && body.tags.trim() !== '') { /* What we are doing here is bypassing a mongodb $text restriction. * If you only include negate expressions then nothing will match so * we turn it into a $not regex that matches negated tags and returns * the rest. This is needed for tag blacklists. */ - if (req.body.tags.split(/-"?[^",]+"?(?:, *)?/).join('').trim() === '') { + if (body.tags.split(/-"?[^",]+"?(?:, *)?/).join('').trim() === '') { options.tags = { - $nin: req.body.tags.match(/(^|, *)-[^,]+/g).map(e => e.replace(/,? *-|"/g, '')) + $nin: body.tags.match(/(^|, *)-[^,]+/g).map(e => e.replace(/,? *-|"/g, '')) }; } else { - options.$text = { $search: req.body.tags }; - - if (req.body.sort && req.body.sort === 'relevance') { - projection.score = { $meta: 'textScore' }; - sort.score = { $meta: 'textScore' }; - } + options.$text = { $search: body.tags }; } } - agg.match(options) + agg.match(options); - if (req.query.count !== undefined && req.query.count != '0' && /^\d{1,3}$/.test(req.query.count)) { - let count = parseInt(req.query.count, 10); - agg.sample(count <= 100 ? count : 1); + if (body.count !== undefined && body.count != 0) { + const count = parseInt(body.count, 10); + agg.sample(!isNaN(count) && count <= 100 ? count : 1); } else agg.sample(1);