0
0
mirror of https://github.com/naturalcrit/homebrewery.git synced 2026-01-07 22:52:39 +00:00

Rearrange and leverage getBrew

This commit is contained in:
David Bolack
2024-03-06 12:38:00 -06:00
parent 17f78169f2
commit 18aa453bb0
2 changed files with 42 additions and 42 deletions

View File

@@ -7,5 +7,4 @@
"enable_themes" : true, "enable_themes" : true,
"local_environments" : ["docker", "local"], "local_environments" : ["docker", "local"],
"publicUrl" : "https://homebrewery.naturalcrit.com", "publicUrl" : "https://homebrewery.naturalcrit.com",
"mongodb_uri" : "mongodb://127.0.0.1/homebrewery3"
} }

View File

@@ -48,45 +48,6 @@ const splitTextStyleAndMetadata = (brew)=>{
} }
}; };
const getUsersBrewThemes = async (username, id)=>{
const fields = [
'title',
'tags',
'editId',
'thumbnail',
'textBin'
];
const userThemes = {
Brew : {
}
};
if(!username || !id) {
return userThemes;
}
const brews = await HomebrewModel.getByUser(username, true, fields, { tags: { $in: ['theme', 'Theme'] }, editId: { $ne: id } }) //lean() converts results to JSObjects
.catch((error)=>{throw 'Can not find brews';});
for await (const brew of brews) {
const brewTheme = await HomebrewModel.get({ editId: brew.editId }, ['textBin']);
splitTextStyleAndMetadata(brewTheme);
userThemes.Brew[`#${brew.editId}`] = {
name : brew.title,
renderer : 'V3',
baseTheme : brewTheme.theme,
baseSnippets : false,
path : `#${brew.editId}`,
thumbnail : brew.thumbnail.length > 0 ? brew.thumbnail : '/assets/naturalCritLogoWhite.svg'
};
};
return userThemes;
};
const api = { const api = {
homebrewApi : router, homebrewApi : router,
getId : (req)=>{ getId : (req)=>{
@@ -105,6 +66,46 @@ const api = {
} }
return { id, googleId }; return { id, googleId };
}, },
getUsersBrewThemes : async (username, id, req, res, next)=>{
const fields = [
'title',
'tags',
'editId',
'thumbnail',
'textBin'
];
const userThemes = {
Brew : {
}
};
if(!username || !id) {
return userThemes;
}
const brews = await HomebrewModel.getByUser(username, true, fields, { tags: { $in: ['theme', 'Theme'] }, editId: { $ne: id } }) //lean() converts results to JSObjects
.catch((error)=>{throw 'Can not find brews';});
for await (const brew of brews) {
api.getBrew('themes', req=req, res=res, next=next);
const brewTheme = req.brew;
if(brewTheme) {
splitTextStyleAndMetadata(brewTheme);
userThemes.Brew[`#${brew.editId}`] = {
name : brew.title,
renderer : 'V3',
baseTheme : brewTheme.theme,
baseSnippets : false,
path : `#${brew.editId}`,
thumbnail : brew.thumbnail.length > 0 ? brew.thumbnail : '/assets/naturalCritLogoWhite.svg'
};
}
};
return userThemes;
},
getBrew : (accessType, stubOnly = false)=>{ getBrew : (accessType, stubOnly = false)=>{
// Create middleware with the accessType passed in as part of the scope // Create middleware with the accessType passed in as part of the scope
return async (req, res, next)=>{ return async (req, res, next)=>{
@@ -161,7 +162,7 @@ const api = {
const userID = req?.account?.username && (accessType === 'edit') ? req.account.username : stub.authors[0]; const userID = req?.account?.username && (accessType === 'edit') ? req.account.username : stub.authors[0];
// Clean up brew: fill in missing fields with defaults / fix old invalid values // Clean up brew: fill in missing fields with defaults / fix old invalid values
const userThemes = await getUsersBrewThemes(userID, id); const userThemes = accessType != 'themes' ? await api.getUsersBrewThemes(userID, id, req, res, next) : '';
if(stub) { if(stub) {
stub.tags = stub.tags || undefined; // Clear empty strings stub.tags = stub.tags || undefined; // Clear empty strings
stub.renderer = stub.renderer || undefined; // Clear empty strings stub.renderer = stub.renderer || undefined; // Clear empty strings
@@ -317,7 +318,7 @@ const api = {
brew.description = brew.description.trim() || ''; brew.description = brew.description.trim() || '';
brew.text = api.mergeBrewText(brew); brew.text = api.mergeBrewText(brew);
const userID = req?.account?.username ? req.account.username : brew.authors.split(',')[0]; const userID = req?.account?.username ? req.account.username : brew.authors.split(',')[0];
brew.userThemes = await getUsersBrewThemes(userID, brew.editId); brew.userThemes = await api.getUsersBrewThemes(userID, brew.editId, req, res, null);
if(brew.googleId && removeFromGoogle) { if(brew.googleId && removeFromGoogle) {