From e6b3917c513d8c469d7db9cf72b35746e61d3f63 Mon Sep 17 00:00:00 2001 From: Ro Date: Thu, 18 Jun 2020 16:21:33 -0700 Subject: [PATCH] moved settings retrieval to Settting data class to get rid of settings caching problem --- brain/data/Navigation.js | 8 +- brain/data/Render.js | 284 +++++++++++++++++++-------------- brain/data/Settings.js | 191 +++++++++++++--------- src/com/actions/PageActions.js | 46 ------ src/libraries/FipamoAPI.js | 4 +- 5 files changed, 289 insertions(+), 244 deletions(-) diff --git a/brain/data/Navigation.js b/brain/data/Navigation.js index b3700b2..df15b4c 100644 --- a/brain/data/Navigation.js +++ b/brain/data/Navigation.js @@ -1,7 +1,9 @@ import fs from 'fs-extra'; import _ from 'lodash'; 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 { //-------------------------- @@ -39,11 +41,11 @@ export default class Navigation { case DataEvent.MENU_ADD_ITEM: break; case DataEvent.MENU_DELETE_ITEM: - fs.readJSON('site/settings.json').then(settings => { + settings.load(SETTINGS_FILE).then(settings => { settings.menu = _.remove(settings.menu, m => { return m.uuid != item.uuid; }); - fs.writeJSON('site/settings.json', settings); + fs.writeJSON(SETTINGS_FILE, settings); }); break; } diff --git a/brain/data/Render.js b/brain/data/Render.js index dde337a..a6e0a89 100644 --- a/brain/data/Render.js +++ b/brain/data/Render.js @@ -1,13 +1,13 @@ import * as DataEvent from '../../src/com/events/DataEvent'; import StringUtils from '../../src/com/utils/StringUtils'; +import Settings, { SETTINGS_FILE, SETTINGS_TAG } from './Settings'; import fs from 'fs-extra'; import sanitize from 'sanitize-html'; -const config = require('../../site/settings.json'); -const tags_list = require('../../site/tags.json'); const pug = require('pug'); const md = require('markdown-it')('commonmark'); const _ = require('lodash'); const moment = require('moment'); +const settings = new Settings(); export default class Render { //-------------------------- @@ -26,138 +26,186 @@ export default class Render { */ publishAll(pages, theme) { return new Promise((resolve, reject) => { - let response = []; - let count = _.filter(pages, page => { - return page.metadata.deleted === false; - }).length; - let rendered = 0; - for (let index = 0; index < pages.length; index++) { - const page = pages[index]; - if (page.metadata.deleted === false) { - let writeFile, template; - let path = - 'public/' + - moment(page.metadata.created).format('YYYY') + - '/' + - moment(page.metadata.created).format('MM') + - '/'; - if (page.metadata.layout === 'index') { - template = 'content/themes/' + theme + '/index.pug'; - writeFile = 'public/index.html'; - } else { - writeFile = path + page.metadata.slug + '.html'; - template = 'content/themes/' + theme + '/page.pug'; - } - - let buffed = sanitize(page.content, { - 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); + settings + .load(SETTINGS_FILE) + .then(config => { + let response = []; + let count = _.filter(pages, page => { + return page.metadata.deleted === false; + }).length; + let rendered = 0; + for (let index = 0; index < pages.length; index++) { + const page = pages[index]; + if (page.metadata.deleted === false) { + let writeFile, template; + let path = + 'public/' + + moment(page.metadata.created).format('YYYY') + + '/' + + moment(page.metadata.created).format('MM') + + '/'; + if (page.metadata.layout === 'index') { + template = 'content/themes/' + theme + '/index.pug'; + writeFile = 'public/index.html'; + } else { + writeFile = path + page.metadata.slug + '.html'; + template = 'content/themes/' + theme + '/page.pug'; } - // success case, the file was saved - }); - }); - rendered = ++rendered; - if (rendered === count) { - response = { - type: DataEvent.PAGES_RENDERED, - message: 'All Pages Rendered. Sweet.' - }; - resolve(response); + let buffed = sanitize(page.content, { + 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 + }); + }); + 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) { + let self = this; return new Promise((resolve, reject) => { - let tags = tags_list.tags; - let renderList = []; - for (let index = 0; index < tags.length; index++) { - let tag = tags[index]; - //console.log('**TAG**', tag.tag_name); - var pageList = []; - for (let i = 0; i < pages.length; i++) { - let page = pages[i]; + self.loadRenderData() + .then(result => { + let tags = result.tags.tags; + let renderList = []; + for (let index = 0; index < tags.length; index++) { + let tag = tags[index]; + //console.log('**TAG**', tag.tag_name); + var pageList = []; + for (let i = 0; i < pages.length; i++) { + let page = pages[i]; - if (_.includes(page.metadata.tags, tag.tag_name)) { - pageList.push({ title: page.metadata.title, slug: page.metadata.slug }); + if (_.includes(page.metadata.tags, tag.tag_name)) { + pageList.push({ + title: page.metadata.title, + slug: page.metadata.slug + }); + } + } + renderList.push({ tag: tag.tag_name, tag_list: pageList, slug: tag.slug }); } - } - renderList.push({ tag: tag.tag_name, tag_list: pageList, slug: tag.slug }); - } - let response = []; - for (let index = 0; index < renderList.length; index++) { - let item = renderList[index]; - let file = pug.renderFile('content/themes/' + config.global.theme + '/tags.pug', { - title: item.tag, - default_bg: config.global.background, - content_tags: 'THESE ARE TAGS', - tag_list: item.tag_list - }); + let response = []; + for (let index = 0; index < renderList.length; index++) { + let item = renderList[index]; + let file = pug.renderFile( + 'content/themes/' + result.settings.global.theme + '/tags.pug', + { + title: item.tag, + default_bg: result.settings.global.background, + content_tags: 'THESE ARE TAGS', + tag_list: item.tag_list + } + ); - fs.writeFile('public/tags/' + item.slug + '.html', file, err => { - // throws an error, you could also catch it here - if (err) { - response = { type: DataEvent.TAG_PAGES_NOT_RENDERED, message: err }; - reject(response); + fs.writeFile('public/tags/' + item.slug + '.html', file, err => { + // throws an error, you could also catch it here + if (err) { + response = { type: DataEvent.TAG_PAGES_NOT_RENDERED, message: err }; + 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 = { - type: DataEvent.TAG_PAGES_RENDERED, - message: 'Tag Pages ready to go. Good job.' - }; - resolve(response); + }) + .catch(err => { + reject(err); }); - } }); } publishArchive(archive) { - let file = pug.renderFile('content/themes/' + config.global.theme + '/archive.pug', { - title: 'ARCHIVES', - default_bg: config.global.background, - content_tags: 'COLD STORAGE', - archives: archive - }); + settings + .load(SETTINGS_FILE) + .then(settings => { + let file = pug.renderFile( + '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 => { - // throws an error, you could also catch it here - if (err) { - console.log('ERROR', err); - //response = { type: DataEvent.TAG_PAGES_NOT_RENDERED, message: err }; - } - // success case, the file was saved + fs.writeFile('public/archives.html', file, err => { + // throws an error, you could also catch it here + if (err) { + console.log('ERROR', err); + //response = { type: DataEvent.TAG_PAGES_NOT_RENDERED, message: err }; + } + // 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); + }); }); } diff --git a/brain/data/Settings.js b/brain/data/Settings.js index c769fee..2a026f5 100644 --- a/brain/data/Settings.js +++ b/brain/data/Settings.js @@ -1,9 +1,9 @@ import * as DataEvent from '../../src/com/events/DataEvent'; 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'); +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 { //-------------------------- @@ -14,87 +14,101 @@ export default class Settings { // methods //-------------------------- sync(req) { + let self = this; return new Promise((resolve, reject) => { - let payload = req.body; - let user = req.session.user; - let found = _.find(folks, { id: user.id }); - let needToUpdate = false; - let response = []; - if (found) { - let index = found.id - 1; - if ( - folks[index].handle != payload.member.handle || - folks[index].email != payload.member.email - ) { - folks[index].handle = payload.member.handle; - folks[index].email = payload.member.email; - fs.writeJson('site/folks.json', folks); - } else { - //no need to save - } - } else { - let response = { - type: DataEvent.REQUEST_LAME, - message: "You're not logged in, champ" - }; - reject(response); - } - - 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' + self.loadConfigData() + .then(result => { + let payload = req.body; + let user = req.session.user; + let found = _.find(result.folks, { id: user.id }); + let needToUpdate = false; + let response = []; + if (found) { + let index = found.id - 1; + if ( + result.folks[index].handle != payload.member.handle || + result.folks[index].email != payload.member.email + ) { + result.folks[index].handle = payload.member.handle; + result.folks[index].email = payload.member.email; + fs.writeJson('site/folks.json', result.folks); + } else { + //no need to save + } + } else { + let response = { + type: DataEvent.REQUEST_LAME, + message: "You're not logged in, champ" }; - resolve(response); - }) - .catch(() => { - //console.error(err); - }); - } else { - //no need to update - } + reject(response); + } + + if (!_.isEqual(result.settings.global, payload.global)) { + let bg = payload.global.background; + payload.global.background = bg.substr(21, bg.length); + 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) { + let self = this; return new Promise((resolve, reject) => { - if (!_.isEqual(config.tags, tags)) { - config.tags = tags; - fs.writeJson('site/tags.json', config) - .then(() => { + self.load(SETTINGS_TAG) + .then(config => { + if (!_.isEqual(config.tags, tags)) { + 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 = { - type: DataEvent.SETTINGS_UPDATED, - message: 'Settings Saved' + type: DataEvent.SETTINGS_NOT_UPDATED, + message: 'Settings Already Saved' }; resolve(response); - }) - .catch(err => { - reject(err); - }); - } else { - let response = { - type: DataEvent.SETTINGS_NOT_UPDATED, - message: 'Settings Already Saved' - }; - resolve(response); - } + } + }) + .catch(err => { + reject(err); + }); }); } @@ -105,6 +119,33 @@ export default class 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 diff --git a/src/com/actions/PageActions.js b/src/com/actions/PageActions.js index 0059a88..0015881 100644 --- a/src/com/actions/PageActions.js +++ b/src/com/actions/PageActions.js @@ -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 * as DataEvent from '../events/DataEvent'; -const api = new FipamoAPI(); export default class PostActions { //-------------------------- // constructor @@ -75,44 +67,6 @@ export default class PostActions { 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 //-------------------------- diff --git a/src/libraries/FipamoAPI.js b/src/libraries/FipamoAPI.js index f78b66d..f13ebd3 100644 --- a/src/libraries/FipamoAPI.js +++ b/src/libraries/FipamoAPI.js @@ -26,7 +26,7 @@ export default class APIUtils { this.token = null; //checks backend to see if user is logged in //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']); if (response.type === DataEvent.API_REQUEST_GOOD) { this.token = response.token; @@ -93,6 +93,6 @@ export default class APIUtils { //-------------------------- handleLoadProgress(e) { this.percentComplete = Math.ceil((e.loaded / e.total) * 100); - //this.emitEvent(DataEvent.UPLOAD_PROGRESS); + //pass element to display request progress } }