0
0
mirror of https://github.com/naturalcrit/homebrewery.git synced 2026-01-05 18:52:38 +00:00

more callback errors, and cloning queries

This commit is contained in:
Víctor Losada Hernández
2024-01-24 22:49:14 +01:00
parent 067a7cd507
commit 8b6517eb8d

View File

@@ -29,39 +29,43 @@ const mw = {
/* Search for brews that are older than 3 days and that are shorter than a tweet */ /* Search for brews that are older than 3 days and that are shorter than a tweet */
const junkBrewQuery = HomebrewModel.find({ const junkBrewQuery = HomebrewModel.find({
'$where' : 'this.text.length < 140', text: { $exists: true, $not: { $type: 10 } }, // $type 10 corresponds to null/undefined
createdAt : { createdAt: {
$lt : Moment().subtract(30, 'days').toDate() $lt: Moment().subtract(30, 'days').toDate()
} }
}).limit(100).maxTime(60000); }).limit(100).maxTime(60000);
/* Search for brews that aren't compressed (missing the compressed text field) */ /* Search for brews that aren't compressed (missing the compressed text field) */
const uncompressedBrewQuery = HomebrewModel.find({ const uncompressedBrewQuery = HomebrewModel.find({
'text' : { '$exists': true } 'text' : { '$exists': true }
}).lean().limit(10000).select('_id'); }).lean().limit(10000).select('_id');
router.get('/admin/cleanup', mw.adminOnly, (req, res) => { router.get('/admin/cleanup', mw.adminOnly, (req, res) => {
junkBrewQuery.exec() // Search for brews that are older than 3 days and shorter than 140 characters
const query = junkBrewQuery.clone();
query.exec()
.then((objs) => res.json({ count: objs.length })) .then((objs) => res.json({ count: objs.length }))
.catch((error) => { .catch((error) => {
console.error(error); console.error(error);
res.status(500).json({ error: 'Internal Server Error' }); res.status(500).json({ error: 'Internal Server Error' });
}); });
}); });
/* Removes all empty brews that are older than 3 days and that are shorter than a tweet */
router.post('/admin/cleanup', mw.adminOnly, (req, res) => { router.post('/admin/cleanup', mw.adminOnly, (req, res) => {
junkBrewQuery.remove().exec() // Remove all brews that are older than 3 days and shorter than 140 characters
.then((result) => res.json({ count: result.n })) // 'n' represents the number of documents removed const query = junkBrewQuery.clone();
query.remove().exec()
.then((result) => res.json({ count: result.n }))
.catch((error) => { .catch((error) => {
console.error(error); console.error(error);
res.status(500).json({ error: 'Internal Server Error' }); res.status(500).json({ error: 'Internal Server Error' });
}); });
}); });
/* Searches for matching edit or share id, also attempts to partial match */ /* Searches for matching edit or share id, also attempts to partial match */
router.get('/admin/lookup/:id', mw.adminOnly, async (req, res, next) => { router.get('/admin/lookup/:id', mw.adminOnly, async (req, res, next) => {
try { try {
const brew = await HomebrewModel.findOne({ const brew = await HomebrewModel.findOne({
@@ -84,16 +88,19 @@ router.get('/admin/lookup/:id', mw.adminOnly, async (req, res, next) => {
}); });
/* Find 50 brews that aren't compressed yet */ /* Find 50 brews that aren't compressed yet */
router.get('/admin/finduncompressed', mw.adminOnly, async (req, res) => { router.get('/admin/finduncompressed', mw.adminOnly, (req, res) => {
try { const query = uncompressedBrewQuery.clone();
const objs = await uncompressedBrewQuery.exec();
const ids = objs.map((obj) => obj._id); query.exec()
return res.json({ count: objs.length, ids: ids }); .then((objs) => {
} catch (error) { const ids = objs.map((obj) => obj._id);
console.error(error); res.json({ count: ids.length, ids });
return res.status(500).json({ error: 'Internal Server Error' }); })
} .catch((err) => {
}); console.error(err);
res.status(500).send(err.message || 'Internal Server Error');
});
});
/* Compresses the "text" field of a brew to binary */ /* Compresses the "text" field of a brew to binary */
@@ -104,8 +111,10 @@ router.put('/admin/compress/:id', (req, res) => {
return res.status(404).send('Brew not found'); return res.status(404).send('Brew not found');
} }
brew.textBin = zlib.deflateRawSync(brew.text); if (brew.text) {
brew.text = undefined; brew.textBin = zlib.deflateRawSync(brew.text);
brew.text = undefined;
}
return brew.save(); return brew.save();
}) })
@@ -131,22 +140,6 @@ router.get('/admin/stats', mw.adminOnly, async (req, res) => {
return res.status(500).json({ error: 'Internal Server Error' }); return res.status(500).json({ error: 'Internal Server Error' });
} }
}); });
/*
router.get('/admin/stats', mw.adminOnly, async (req, res) => {
try {
const count = await HomebrewModel.countDocuments({});
return res.json({
totalBrews: count
});
} catch (error) {
console.error(error);
return res.status(500).json({ error: 'Internal Server Error' });
}
});
*/
router.get('/admin', mw.adminOnly, (req, res)=>{ router.get('/admin', mw.adminOnly, (req, res)=>{
templateFn('admin', { templateFn('admin', {