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

pull/20/head
Ro 4 years ago
parent 36d6631df9
commit e6b3917c51

@ -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;
}

@ -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';
}
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';
}
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
});
let buffed = sanitize(page.content, {
allowedTags: ['del', 'a', 'iframe', 'img'],
allowedAttributes: {
a: ['href', 'name', 'target'],
img: ['src'],
iframe: [
'height',
'width',
'src',
'frameborder',
'allow',
'allowfullscreen'
]
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
}
});
}
})
.catch(err => {
reject(err);
});
});
}
publishTags(pages) {
let self = this;
return new Promise((resolve, reject) => {
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];
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
});
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 });
}
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.ensureDir(path).then(() => {
fs.writeFile(writeFile, file, err => {
fs.writeFile('public/tags/' + item.slug + '.html', file, err => {
// throws an error, you could also catch it here
if (err) {
response = { type: DataEvent.PAGES_NOT_RENDERED, message: 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);
});
});
rendered = ++rendered;
if (rendered === count) {
response = {
type: DataEvent.PAGES_RENDERED,
message: 'All Pages Rendered. Sweet.'
};
resolve(response);
}
} else {
//don't render delete pages
}
}
})
.catch(err => {
reject(err);
});
});
}
publishTags(pages) {
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];
if (_.includes(page.metadata.tags, tag.tag_name)) {
pageList.push({ title: page.metadata.title, slug: page.metadata.slug });
publishArchive(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
}
}
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
});
);
fs.writeFile('public/tags/' + item.slug + '.html', file, err => {
fs.writeFile('public/archives.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);
console.log('ERROR', err);
//response = { type: DataEvent.TAG_PAGES_NOT_RENDERED, message: err };
}
// success case, the file was saved
response = {
type: DataEvent.TAG_PAGES_RENDERED,
message: 'Tag Pages ready to go. Good job.'
};
resolve(response);
});
}
});
})
.catch(err => {
console.log(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
});
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
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);
});
});
}

@ -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);
}
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"
};
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(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(settings.email, payload.email)) {
settings.email = payload.email;
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', settings)
.then(() => {
response = {
type: DataEvent.SETTINGS_UPDATED,
message: 'Settings Saved'
};
resolve(response);
})
.catch(() => {
//console.error(err);
});
} else {
//no need to update
}
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

@ -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
//--------------------------

@ -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
}
}

Loading…
Cancel
Save