0
0
mirror of https://github.com/naturalcrit/homebrewery.git synced 2026-01-22 00:57:55 +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,
"local_environments" : ["docker", "local"],
"publicUrl" : "https://homebrewery.naturalcrit.com",
"mongodb_uri" : "mongodb://127.0.0.1/homebrewery3"
}

View File

@@ -48,7 +48,25 @@ const splitTextStyleAndMetadata = (brew)=>{
}
};
const getUsersBrewThemes = async (username, id)=>{
const api = {
homebrewApi : router,
getId : (req)=>{
// Set the id and initial potential google id, where the google id is present on the existing brew.
let id = req.params.id, googleId = req.body?.googleId;
// If the id is longer than 12, then it's a google id + the edit id. This splits the longer id up.
if(id.length > 12) {
if(id.length >= (33 + 12)) { // googleId is minimum 33 chars (may increase)
googleId = id.slice(0, -12); // current editId is 12 chars
} else { // old editIds used to be 10 chars;
googleId = id.slice(0, -10); // if total string is too short, must be old brew
console.log('Old brew, using 10-char Id');
}
id = id.slice(googleId.length);
}
return { id, googleId };
},
getUsersBrewThemes : async (username, id, req, res, next)=>{
const fields = [
'title',
'tags',
@@ -71,7 +89,9 @@ const getUsersBrewThemes = async (username, id)=>{
.catch((error)=>{throw 'Can not find brews';});
for await (const brew of brews) {
const brewTheme = await HomebrewModel.get({ editId: brew.editId }, ['textBin']);
api.getBrew('themes', req=req, res=res, next=next);
const brewTheme = req.brew;
if(brewTheme) {
splitTextStyleAndMetadata(brewTheme);
userThemes.Brew[`#${brew.editId}`] = {
name : brew.title,
@@ -81,29 +101,10 @@ const getUsersBrewThemes = async (username, id)=>{
path : `#${brew.editId}`,
thumbnail : brew.thumbnail.length > 0 ? brew.thumbnail : '/assets/naturalCritLogoWhite.svg'
};
}
};
return userThemes;
};
const api = {
homebrewApi : router,
getId : (req)=>{
// Set the id and initial potential google id, where the google id is present on the existing brew.
let id = req.params.id, googleId = req.body?.googleId;
// If the id is longer than 12, then it's a google id + the edit id. This splits the longer id up.
if(id.length > 12) {
if(id.length >= (33 + 12)) { // googleId is minimum 33 chars (may increase)
googleId = id.slice(0, -12); // current editId is 12 chars
} else { // old editIds used to be 10 chars;
googleId = id.slice(0, -10); // if total string is too short, must be old brew
console.log('Old brew, using 10-char Id');
}
id = id.slice(googleId.length);
}
return { id, googleId };
},
getBrew : (accessType, stubOnly = false)=>{
// Create middleware with the accessType passed in as part of the scope
@@ -161,7 +162,7 @@ const api = {
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
const userThemes = await getUsersBrewThemes(userID, id);
const userThemes = accessType != 'themes' ? await api.getUsersBrewThemes(userID, id, req, res, next) : '';
if(stub) {
stub.tags = stub.tags || undefined; // Clear empty strings
stub.renderer = stub.renderer || undefined; // Clear empty strings
@@ -317,7 +318,7 @@ const api = {
brew.description = brew.description.trim() || '';
brew.text = api.mergeBrewText(brew);
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) {