0
0
mirror of https://github.com/naturalcrit/homebrewery.git synced 2025-12-28 17:52:38 +00:00

Update Theme Picker to use Brews tagged as a theme

This updates the theme picker to include brews tagged as themes owned by
the user.

Some supporting functions were updated. User themes are loaded on /edit
and added to the request.
This commit is contained in:
David Bolack
2024-02-22 21:12:56 -06:00
parent ae2bb3a028
commit f60090e5fa
4 changed files with 46 additions and 8 deletions

View File

@@ -42,6 +42,36 @@ const sanitizeBrew = (brew, accessType)=>{
return brew;
};
const getUsersBrewThemes = async (username)=>{
const fields = [
'title',
'tags',
'editId',
'thumbnail'
];
const brews = await HomebrewModel.getByUser(username, true, fields, { tags: { $in: ['theme', 'Theme'] } }) //lean() converts results to JSObjects
.catch((error)=>{throw 'Can not find brews';});
const userThemes = {
Brew : {
}
};
brews.forEach((brew)=>{
userThemes.Brew[brew.editId] = {
name : brew.title,
renderer : 'V3',
baseTheme : false,
baseSnippets : false,
path : `#${brew.editId}`,
thumbnail : brew.thumbnail
};
});
return userThemes;
};
app.use('/', serveCompressedStaticAssets(`build`));
app.use(require('./middleware/content-negotiation.js'));
app.use(require('body-parser').json({ limit: '25mb' }));
@@ -278,7 +308,7 @@ app.get('/user/:username', async (req, res, next)=>{
});
//Edit Page
app.get('/edit/:id', asyncHandler(getBrew('edit')), (req, res, next)=>{
app.get('/edit/:id', asyncHandler(getBrew('edit')), async(req, res, next)=>{
req.brew = req.brew.toObject ? req.brew.toObject() : req.brew;
req.ogMeta = { ...defaultMetaTags,
@@ -288,6 +318,7 @@ app.get('/edit/:id', asyncHandler(getBrew('edit')), (req, res, next)=>{
type : 'article'
};
req.brew.userThemes = await getUsersBrewThemes(req.account.username);
sanitizeBrew(req.brew, 'edit');
splitTextStyleAndMetadata(req.brew);
res.header('Cache-Control', 'no-cache, no-store'); //reload the latest saved brew when pressing back button, not the cached version before save.

View File

@@ -50,8 +50,8 @@ HomebrewSchema.statics.get = async function(query, fields=null){
return brew;
};
HomebrewSchema.statics.getByUser = async function(username, allowAccess=false, fields=null){
const query = { authors: username, published: true };
HomebrewSchema.statics.getByUser = async function(username, allowAccess=false, fields=null, filter=null){
const query = { authors: username, published: true, ...filter };
if(allowAccess){
delete query.published;
}