0
0
mirror of https://github.com/naturalcrit/homebrewery.git synced 2026-01-10 20:03:03 +00:00

Linting and small tweaks

This commit is contained in:
Trevor Buckner
2024-01-25 16:27:10 -05:00
parent b503b8fc9b
commit e5acbfed3a

View File

@@ -26,26 +26,24 @@ const mw = {
} }
}; };
const junkBrewPipeline = [
/* Search for brews that are older than 3 days and that are shorter than a tweet */ { $match : {
const junkBrewQuery = HomebrewModel.find({ updatedAt : { $lt: Moment().subtract(30, 'days').toDate() },
'$where' : 'this.textBin.length < 140', lastViewed : { $lt: Moment().subtract(30, 'days').toDate() }
createdAt: { }},
$lt: Moment().subtract(30, 'days').toDate() { $project: { textBinSize: { $binarySize: '$textBin' } } },
} { $match: { textBinSize: { $lt: 140 } } },
}).limit(100).maxTime(60000); { $limit: 100 }
];
/* 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');
// Search for up to 100 brews that have not been viewed or updated in 30 days and are shorter than 140 bytes
router.get('/admin/cleanup', mw.adminOnly, (req, res)=>{ router.get('/admin/cleanup', mw.adminOnly, (req, res)=>{
// Search for brews that are older than 3 days and shorter than 140 characters HomebrewModel.aggregate(junkBrewPipeline).option({ maxTimeMS: 60000 })
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);
@@ -53,13 +51,15 @@ router.get('/admin/cleanup', mw.adminOnly, (req, res) => {
}); });
}); });
// Delete up to 100 brews that have not been viewed or updated in 30 days and are shorter than 140 bytes
router.post('/admin/cleanup', mw.adminOnly, (req, res)=>{ router.post('/admin/cleanup', mw.adminOnly, (req, res)=>{
// Remove all brews that are older than 3 days and shorter than 140 characters HomebrewModel.aggregate(junkBrewPipeline).option({ maxTimeMS: 60000 })
const query = junkBrewQuery.clone(); .then((docs)=>{
const ids = docs.map((doc)=>doc._id);
query.remove().exec() return HomebrewModel.deleteMany({ _id: { $in: ids } });
.then((result) => res.json({ count: result.n })) }).then((result)=>{
.catch((error) => { res.json({ count: result.deletedCount });
}).catch((error)=>{
console.error(error); console.error(error);
res.status(500).json({ error: 'Internal Server Error' }); res.status(500).json({ error: 'Internal Server Error' });
}); });
@@ -67,24 +67,22 @@ router.post('/admin/cleanup', mw.adminOnly, (req, res) => {
/* 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 { HomebrewModel.findOne({
const brew = await HomebrewModel.findOne({
$or : [ $or : [
{ editId: { $regex: req.params.id, $options: 'i' } }, { editId: { $regex: req.params.id, $options: 'i' } },
{ shareId: { $regex: req.params.id, $options: 'i' } }, { shareId: { $regex: req.params.id, $options: 'i' } },
] ]
}).exec(); }).exec()
.then((brew)=>{
if (!brew) { if(!brew) // No document found
// No document found
return res.status(404).json({ error: 'Document not found' }); return res.status(404).json({ error: 'Document not found' });
} else
return res.json(brew); return res.json(brew);
} catch (error) { })
console.error(error); .catch((err)=>{
console.error(err);
return res.status(500).json({ error: 'Internal Server Error' }); return res.status(500).json({ error: 'Internal Server Error' });
} });
}); });
/* Find 50 brews that aren't compressed yet */ /* Find 50 brews that aren't compressed yet */
@@ -107,12 +105,11 @@ router.get('/admin/finduncompressed', mw.adminOnly, (req, res) => {
router.put('/admin/compress/:id', (req, res)=>{ router.put('/admin/compress/:id', (req, res)=>{
HomebrewModel.findOne({ _id: req.params.id }) HomebrewModel.findOne({ _id: req.params.id })
.then((brew)=>{ .then((brew)=>{
if (!brew) { if(!brew)
return res.status(404).send('Brew not found'); return res.status(404).send('Brew not found');
}
if(brew.text) { if(brew.text) {
brew.textBin = zlib.deflateRawSync(brew.text); brew.textBin = brew.textBin || zlib.deflateRawSync(brew.text); //Don't overwrite textBin if exists
brew.text = undefined; brew.text = undefined;
} }