mirror of
https://github.com/naturalcrit/homebrewery.git
synced 2026-01-20 16:12:40 +00:00
Linting and small tweaks
This commit is contained in:
@@ -26,26 +26,24 @@ const mw = {
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/* Search for brews that are older than 3 days and that are shorter than a tweet */
|
||||
const junkBrewQuery = HomebrewModel.find({
|
||||
'$where' : 'this.textBin.length < 140',
|
||||
createdAt: {
|
||||
$lt: Moment().subtract(30, 'days').toDate()
|
||||
}
|
||||
}).limit(100).maxTime(60000);
|
||||
|
||||
const junkBrewPipeline = [
|
||||
{ $match : {
|
||||
updatedAt : { $lt: Moment().subtract(30, 'days').toDate() },
|
||||
lastViewed : { $lt: Moment().subtract(30, 'days').toDate() }
|
||||
}},
|
||||
{ $project: { textBinSize: { $binarySize: '$textBin' } } },
|
||||
{ $match: { textBinSize: { $lt: 140 } } },
|
||||
{ $limit: 100 }
|
||||
];
|
||||
|
||||
/* Search for brews that aren't compressed (missing the compressed text field) */
|
||||
const uncompressedBrewQuery = HomebrewModel.find({
|
||||
'text' : { '$exists': true }
|
||||
}).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)=>{
|
||||
// Search for brews that are older than 3 days and shorter than 140 characters
|
||||
const query = junkBrewQuery.clone();
|
||||
|
||||
query.exec()
|
||||
HomebrewModel.aggregate(junkBrewPipeline).option({ maxTimeMS: 60000 })
|
||||
.then((objs)=>res.json({ count: objs.length }))
|
||||
.catch((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)=>{
|
||||
// Remove all brews that are older than 3 days and shorter than 140 characters
|
||||
const query = junkBrewQuery.clone();
|
||||
|
||||
query.remove().exec()
|
||||
.then((result) => res.json({ count: result.n }))
|
||||
.catch((error) => {
|
||||
HomebrewModel.aggregate(junkBrewPipeline).option({ maxTimeMS: 60000 })
|
||||
.then((docs)=>{
|
||||
const ids = docs.map((doc)=>doc._id);
|
||||
return HomebrewModel.deleteMany({ _id: { $in: ids } });
|
||||
}).then((result)=>{
|
||||
res.json({ count: result.deletedCount });
|
||||
}).catch((error)=>{
|
||||
console.error(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 */
|
||||
router.get('/admin/lookup/:id', mw.adminOnly, async (req, res, next)=>{
|
||||
try {
|
||||
const brew = await HomebrewModel.findOne({
|
||||
HomebrewModel.findOne({
|
||||
$or : [
|
||||
{ editId: { $regex: req.params.id, $options: 'i' } },
|
||||
{ shareId: { $regex: req.params.id, $options: 'i' } },
|
||||
]
|
||||
}).exec();
|
||||
|
||||
if (!brew) {
|
||||
// No document found
|
||||
}).exec()
|
||||
.then((brew)=>{
|
||||
if(!brew) // No document found
|
||||
return res.status(404).json({ error: 'Document not found' });
|
||||
}
|
||||
|
||||
else
|
||||
return res.json(brew);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
})
|
||||
.catch((err)=>{
|
||||
console.error(err);
|
||||
return res.status(500).json({ error: 'Internal Server Error' });
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
/* 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)=>{
|
||||
HomebrewModel.findOne({ _id: req.params.id })
|
||||
.then((brew)=>{
|
||||
if (!brew) {
|
||||
if(!brew)
|
||||
return res.status(404).send('Brew not found');
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user