moved settings retrieval to Settting data class to get rid of settings caching problem

This commit is contained in:
Ro 2020-06-18 16:21:33 -07:00
parent 36d6631df9
commit e6b3917c51
5 changed files with 289 additions and 244 deletions

View file

@ -1,7 +1,9 @@
import fs from 'fs-extra'; import fs from 'fs-extra';
import _ from 'lodash'; import _ from 'lodash';
import * as DataEvent from '../../src/com/events/DataEvent'; import * as DataEvent from '../../src/com/events/DataEvent';
const settings = require('../../site/settings.json'); import Settings, { SETTINGS_FILE } from './Settings';
const settings = new Settings();
//const settings = require('../../site/settings.json');
export default class Navigation { export default class Navigation {
//-------------------------- //--------------------------
@ -39,11 +41,11 @@ export default class Navigation {
case DataEvent.MENU_ADD_ITEM: case DataEvent.MENU_ADD_ITEM:
break; break;
case DataEvent.MENU_DELETE_ITEM: case DataEvent.MENU_DELETE_ITEM:
fs.readJSON('site/settings.json').then(settings => { settings.load(SETTINGS_FILE).then(settings => {
settings.menu = _.remove(settings.menu, m => { settings.menu = _.remove(settings.menu, m => {
return m.uuid != item.uuid; return m.uuid != item.uuid;
}); });
fs.writeJSON('site/settings.json', settings); fs.writeJSON(SETTINGS_FILE, settings);
}); });
break; break;
} }

View file

@ -1,13 +1,13 @@
import * as DataEvent from '../../src/com/events/DataEvent'; import * as DataEvent from '../../src/com/events/DataEvent';
import StringUtils from '../../src/com/utils/StringUtils'; import StringUtils from '../../src/com/utils/StringUtils';
import Settings, { SETTINGS_FILE, SETTINGS_TAG } from './Settings';
import fs from 'fs-extra'; import fs from 'fs-extra';
import sanitize from 'sanitize-html'; import sanitize from 'sanitize-html';
const config = require('../../site/settings.json');
const tags_list = require('../../site/tags.json');
const pug = require('pug'); const pug = require('pug');
const md = require('markdown-it')('commonmark'); const md = require('markdown-it')('commonmark');
const _ = require('lodash'); const _ = require('lodash');
const moment = require('moment'); const moment = require('moment');
const settings = new Settings();
export default class Render { export default class Render {
//-------------------------- //--------------------------
@ -26,138 +26,186 @@ export default class Render {
*/ */
publishAll(pages, theme) { publishAll(pages, theme) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
let response = []; settings
let count = _.filter(pages, page => { .load(SETTINGS_FILE)
return page.metadata.deleted === false; .then(config => {
}).length; let response = [];
let rendered = 0; let count = _.filter(pages, page => {
for (let index = 0; index < pages.length; index++) { return page.metadata.deleted === false;
const page = pages[index]; }).length;
if (page.metadata.deleted === false) { let rendered = 0;
let writeFile, template; for (let index = 0; index < pages.length; index++) {
let path = const page = pages[index];
'public/' + if (page.metadata.deleted === false) {
moment(page.metadata.created).format('YYYY') + let writeFile, template;
'/' + let path =
moment(page.metadata.created).format('MM') + 'public/' +
'/'; moment(page.metadata.created).format('YYYY') +
if (page.metadata.layout === 'index') { '/' +
template = 'content/themes/' + theme + '/index.pug'; moment(page.metadata.created).format('MM') +
writeFile = 'public/index.html'; '/';
} else { if (page.metadata.layout === 'index') {
writeFile = path + page.metadata.slug + '.html'; template = 'content/themes/' + theme + '/index.pug';
template = 'content/themes/' + theme + '/page.pug'; writeFile = 'public/index.html';
} } else {
writeFile = path + page.metadata.slug + '.html';
let buffed = sanitize(page.content, { template = 'content/themes/' + theme + '/page.pug';
allowedTags: ['del', 'a', 'iframe', 'img'],
allowedAttributes: {
a: ['href', 'name', 'target'],
img: ['src'],
iframe: [
'height',
'width',
'src',
'frameborder',
'allow',
'allowfullscreen'
]
}
});
buffed = new StringUtils().decodeHTML(buffed);
let html = md.render(buffed, { html: true, xhtmlOut: true });
let file = pug.renderFile(template, {
title: page.metadata.title,
default_bg: page.metadata.feature,
content: html,
tags: page.metadata.tags,
menu: config.menu
});
fs.ensureDir(path).then(() => {
fs.writeFile(writeFile, file, err => {
// throws an error, you could also catch it here
if (err) {
response = { type: DataEvent.PAGES_NOT_RENDERED, message: err };
reject(response);
} }
// success case, the file was saved let buffed = sanitize(page.content, {
}); allowedTags: ['del', 'a', 'iframe', 'img'],
}); allowedAttributes: {
rendered = ++rendered; a: ['href', 'name', 'target'],
if (rendered === count) { img: ['src'],
response = { iframe: [
type: DataEvent.PAGES_RENDERED, 'height',
message: 'All Pages Rendered. Sweet.' 'width',
}; 'src',
resolve(response); 'frameborder',
'allow',
'allowfullscreen'
]
}
});
buffed = new StringUtils().decodeHTML(buffed);
let html = md.render(buffed, { html: true, xhtmlOut: true });
let file = pug.renderFile(template, {
title: page.metadata.title,
default_bg: page.metadata.feature,
content: html,
tags: page.metadata.tags,
menu: config.menu
});
fs.ensureDir(path).then(() => {
fs.writeFile(writeFile, file, err => {
// throws an error, you could also catch it here
if (err) {
response = {
type: DataEvent.PAGES_NOT_RENDERED,
message: err
};
reject(response);
}
// success case, the file was saved
});
});
rendered = ++rendered;
if (rendered === count) {
response = {
type: DataEvent.PAGES_RENDERED,
message: 'All Pages Rendered. Sweet.'
};
resolve(response);
}
} else {
//don't render delete pages
}
} }
} else { })
//don't render delete pages .catch(err => {
} reject(err);
} });
}); });
} }
publishTags(pages) { publishTags(pages) {
let self = this;
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
let tags = tags_list.tags; self.loadRenderData()
let renderList = []; .then(result => {
for (let index = 0; index < tags.length; index++) { let tags = result.tags.tags;
let tag = tags[index]; let renderList = [];
//console.log('**TAG**', tag.tag_name); for (let index = 0; index < tags.length; index++) {
var pageList = []; let tag = tags[index];
for (let i = 0; i < pages.length; i++) { //console.log('**TAG**', tag.tag_name);
let page = pages[i]; var pageList = [];
for (let i = 0; i < pages.length; i++) {
let page = pages[i];
if (_.includes(page.metadata.tags, tag.tag_name)) { if (_.includes(page.metadata.tags, tag.tag_name)) {
pageList.push({ title: page.metadata.title, slug: page.metadata.slug }); pageList.push({
title: page.metadata.title,
slug: page.metadata.slug
});
}
}
renderList.push({ tag: tag.tag_name, tag_list: pageList, slug: tag.slug });
} }
} let response = [];
renderList.push({ tag: tag.tag_name, tag_list: pageList, slug: tag.slug }); for (let index = 0; index < renderList.length; index++) {
} let item = renderList[index];
let response = []; let file = pug.renderFile(
for (let index = 0; index < renderList.length; index++) { 'content/themes/' + result.settings.global.theme + '/tags.pug',
let item = renderList[index]; {
let file = pug.renderFile('content/themes/' + config.global.theme + '/tags.pug', { title: item.tag,
title: item.tag, default_bg: result.settings.global.background,
default_bg: config.global.background, content_tags: 'THESE ARE TAGS',
content_tags: 'THESE ARE TAGS', tag_list: item.tag_list
tag_list: item.tag_list }
}); );
fs.writeFile('public/tags/' + item.slug + '.html', file, err => { fs.writeFile('public/tags/' + item.slug + '.html', file, err => {
// throws an error, you could also catch it here // throws an error, you could also catch it here
if (err) { if (err) {
response = { type: DataEvent.TAG_PAGES_NOT_RENDERED, message: err }; response = { type: DataEvent.TAG_PAGES_NOT_RENDERED, message: err };
reject(response); reject(response);
}
// success case, the file was saved
response = {
type: DataEvent.TAG_PAGES_RENDERED,
message: 'Tag Pages ready to go. Good job.'
};
resolve(response);
});
} }
// success case, the file was saved })
response = { .catch(err => {
type: DataEvent.TAG_PAGES_RENDERED, reject(err);
message: 'Tag Pages ready to go. Good job.'
};
resolve(response);
}); });
}
}); });
} }
publishArchive(archive) { publishArchive(archive) {
let file = pug.renderFile('content/themes/' + config.global.theme + '/archive.pug', { settings
title: 'ARCHIVES', .load(SETTINGS_FILE)
default_bg: config.global.background, .then(settings => {
content_tags: 'COLD STORAGE', let file = pug.renderFile(
archives: archive 'content/themes/' + settings.global.theme + '/archive.pug',
}); {
title: 'ARCHIVES',
default_bg: settings.global.background,
content_tags: 'COLD STORAGE',
archives: archive
}
);
fs.writeFile('public/archives.html', file, err => { fs.writeFile('public/archives.html', file, err => {
// throws an error, you could also catch it here // throws an error, you could also catch it here
if (err) { if (err) {
console.log('ERROR', err); console.log('ERROR', err);
//response = { type: DataEvent.TAG_PAGES_NOT_RENDERED, message: err }; //response = { type: DataEvent.TAG_PAGES_NOT_RENDERED, message: err };
} }
// success case, the file was saved // success case, the file was saved
});
})
.catch(err => {
console.log(err);
});
}
loadRenderData() {
return new Promise((resolve, reject) => {
let getSettings = settings.load(SETTINGS_FILE);
let getTags = settings.load(SETTINGS_TAG);
Promise.all([getSettings, getTags])
.then(result => {
const [settings, tags] = result;
let data = { settings: settings, tags: tags };
resolve(data);
})
.catch(err => {
reject(err);
});
}); });
} }

View file

@ -1,9 +1,9 @@
import * as DataEvent from '../../src/com/events/DataEvent'; import * as DataEvent from '../../src/com/events/DataEvent';
import fs from 'fs-extra'; import fs from 'fs-extra';
const settings = require('../../site/settings.json');
const folks = require('../../site/folks.json');
const config = require('../../site/tags.json');
const _ = require('lodash'); const _ = require('lodash');
export const SETTINGS_FILE = 'site/settings.json';
export const SETTINGS_FOLKS = 'site/folks.json';
export const SETTINGS_TAG = 'site/tags.json';
export default class Settings { export default class Settings {
//-------------------------- //--------------------------
@ -14,87 +14,101 @@ export default class Settings {
// methods // methods
//-------------------------- //--------------------------
sync(req) { sync(req) {
let self = this;
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
let payload = req.body; self.loadConfigData()
let user = req.session.user; .then(result => {
let found = _.find(folks, { id: user.id }); let payload = req.body;
let needToUpdate = false; let user = req.session.user;
let response = []; let found = _.find(result.folks, { id: user.id });
if (found) { let needToUpdate = false;
let index = found.id - 1; let response = [];
if ( if (found) {
folks[index].handle != payload.member.handle || let index = found.id - 1;
folks[index].email != payload.member.email if (
) { result.folks[index].handle != payload.member.handle ||
folks[index].handle = payload.member.handle; result.folks[index].email != payload.member.email
folks[index].email = payload.member.email; ) {
fs.writeJson('site/folks.json', folks); result.folks[index].handle = payload.member.handle;
} else { result.folks[index].email = payload.member.email;
//no need to save fs.writeJson('site/folks.json', result.folks);
} } else {
} else { //no need to save
let response = { }
type: DataEvent.REQUEST_LAME, } else {
message: "You're not logged in, champ" let response = {
}; type: DataEvent.REQUEST_LAME,
reject(response); message: "You're not logged in, champ"
}
if (!_.isEqual(settings.global, payload.global)) {
let bg = payload.global.background;
payload.global.background = bg.substr(21, bg.length);
settings.global = payload.global;
needToUpdate = true;
} else {
//no need to save
}
if (!_.isEqual(settings.email, payload.email)) {
settings.email = payload.email;
needToUpdate = true;
} else {
//no need to save
}
if (needToUpdate) {
fs.writeJson('site/settings.json', settings)
.then(() => {
response = {
type: DataEvent.SETTINGS_UPDATED,
message: 'Settings Saved'
}; };
resolve(response); reject(response);
}) }
.catch(() => {
//console.error(err); if (!_.isEqual(result.settings.global, payload.global)) {
}); let bg = payload.global.background;
} else { payload.global.background = bg.substr(21, bg.length);
//no need to update result.settings.global = payload.global;
} needToUpdate = true;
} else {
//no need to save
}
if (!_.isEqual(result.settings.email, payload.email)) {
result.settings.email = payload.email;
needToUpdate = true;
} else {
//no need to save
}
if (needToUpdate) {
fs.writeJson('site/settings.json', result.settings)
.then(() => {
response = {
type: DataEvent.SETTINGS_UPDATED,
message: 'Settings Saved'
};
resolve(response);
})
.catch(() => {
//console.error(err);
});
} else {
//no need to update
}
})
.catch(err => {
reject(err);
});
}); });
} }
saveTags(tags) { saveTags(tags) {
let self = this;
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if (!_.isEqual(config.tags, tags)) { self.load(SETTINGS_TAG)
config.tags = tags; .then(config => {
fs.writeJson('site/tags.json', config) if (!_.isEqual(config.tags, tags)) {
.then(() => { config.tags = tags;
fs.writeJson('site/tags.json', config)
.then(() => {
let response = {
type: DataEvent.SETTINGS_UPDATED,
message: 'Settings Saved'
};
resolve(response);
})
.catch(err => {
reject(err);
});
} else {
let response = { let response = {
type: DataEvent.SETTINGS_UPDATED, type: DataEvent.SETTINGS_NOT_UPDATED,
message: 'Settings Saved' message: 'Settings Already Saved'
}; };
resolve(response); resolve(response);
}) }
.catch(err => { })
reject(err); .catch(err => {
}); reject(err);
} else { });
let response = {
type: DataEvent.SETTINGS_NOT_UPDATED,
message: 'Settings Already Saved'
};
resolve(response);
}
}); });
} }
@ -105,6 +119,33 @@ export default class Settings {
fs.writeJSON('site/settings.json', settings); fs.writeJSON('site/settings.json', settings);
}); });
} }
load(fileToLoad) {
return new Promise((resolve, reject) => {
fs.readJSON(fileToLoad)
.then(file => {
resolve(file);
})
.catch(err => {
reject(err);
});
});
}
loadConfigData() {
return new Promise((resolve, reject) => {
let getSettings = this.load(SETTINGS_FILE);
let getFolks = this.load(SETTINGS_FOLKS);
Promise.all([getSettings, getFolks])
.then(result => {
const [settings, folks] = result;
let data = { settings: settings, folks: folks };
resolve(data);
})
.catch(err => {
reject(err);
});
});
}
//-------------------------- //--------------------------
// event handlers // event handlers

View file

@ -1,12 +1,4 @@
import FipamoAPI, {
REQUEST_TYPE_POST,
CONTENT_TYPE_JSON,
API_GET_NAV,
API_NAV_SYNC
} from '../../libraries/FipamoAPI';
import StringUtils from '../utils/StringUtils'; import StringUtils from '../utils/StringUtils';
import * as DataEvent from '../events/DataEvent';
const api = new FipamoAPI();
export default class PostActions { export default class PostActions {
//-------------------------- //--------------------------
// constructor // constructor
@ -75,44 +67,6 @@ export default class PostActions {
resolve(pageInfo); resolve(pageInfo);
}); });
} }
updateNav(add, id, post) {
api.request(API_GET_NAV, DataEvent.SETTINGS_LOADED)
.then(response => {
let menu = JSON.parse(response.request['response']);
let item = {
id: id,
uuid: post.uuid,
title: post.title,
slug: post.slug
};
if (add) {
menu.push(item);
} else {
for (let index = 0; index < menu.length; index++) {
if (menu[index].id == id) {
menu.splice(index, 1);
}
}
}
api.request(
API_NAV_SYNC,
DataEvent.API_SETTINGS_WRITE,
REQUEST_TYPE_POST,
CONTENT_TYPE_JSON,
menu
)
.then(() => {
//console.log(response);
})
.catch(() => {
//console.log(err);
});
})
.catch(() => {
//console.log(err);
});
}
//-------------------------- //--------------------------
// event handlers // event handlers
//-------------------------- //--------------------------

View file

@ -26,7 +26,7 @@ export default class APIUtils {
this.token = null; this.token = null;
//checks backend to see if user is logged in //checks backend to see if user is logged in
//and requests encrypted token for api calls //and requests encrypted token for api calls
this.request('/api/v1/auth/status').then(r => { this.request(API_STATUS).then(r => {
let response = JSON.parse(r.request['response']); let response = JSON.parse(r.request['response']);
if (response.type === DataEvent.API_REQUEST_GOOD) { if (response.type === DataEvent.API_REQUEST_GOOD) {
this.token = response.token; this.token = response.token;
@ -93,6 +93,6 @@ export default class APIUtils {
//-------------------------- //--------------------------
handleLoadProgress(e) { handleLoadProgress(e) {
this.percentComplete = Math.ceil((e.loaded / e.total) * 100); this.percentComplete = Math.ceil((e.loaded / e.total) * 100);
//this.emitEvent(DataEvent.UPLOAD_PROGRESS); //pass element to display request progress
} }
} }