From 1130bcabdb5c461a6fd9537ed902c3558f072888 Mon Sep 17 00:00:00 2001 From: Ro Date: Tue, 29 Sep 2020 22:26:30 +0000 Subject: [PATCH] pages re-indexing up and running, added button to settings under MAINTENANCE tab --- brain/api/v1/settings.js | 27 ++++++++++++++++++- brain/data/Book.js | 40 ++++++++++++++++++++++++++++ brain/data/Settings.js | 28 +++++++++++++++++-- brain/data/Utils.js | 4 --- brain/views/settings.pug | 4 ++- src/com/controllers/SettingsIndex.js | 19 +++++++++++++ src/com/events/DataEvent.js | 1 + src/libraries/FipamoAdminAPI.js | 22 ++++++++++++++- 8 files changed, 136 insertions(+), 9 deletions(-) diff --git a/brain/api/v1/settings.js b/brain/api/v1/settings.js index 080c726..d191368 100644 --- a/brain/api/v1/settings.js +++ b/brain/api/v1/settings.js @@ -67,7 +67,7 @@ router.post('/sync', (req, res) => { router.post('/nav-sync', (req, res) => { auth.authCheck(req) .then(() => { - // find removoed menu item page and set menu to false + // find removed menu item page and set menu to false book.getPage(req.body.remove).then(page => { let body = page.metadata; body.content = page.content; @@ -206,6 +206,31 @@ router.post('/add-feature-background', background_upload, (req, res) => { } }); +router.post('/reindex', (req, res) => { + auth.authCheck(req) + .then(() => { + book.reindexPages(req) + .then(response => { + //reset settings index + settings.resetLibraryIndex(response.count + 1); + //return success to front end + res.json(response); + }) + .catch(err => { + res.json({ + type: err.type, + message: err.message + }); + }); + }) + .catch(err => { + res.json({ + type: err.type, + message: err.message + }); + }); +}); + module.exports = router; function getBookData() { diff --git a/brain/data/Book.js b/brain/data/Book.js index 3c4918d..33b4fdb 100644 --- a/brain/data/Book.js +++ b/brain/data/Book.js @@ -227,6 +227,46 @@ export default class Book { }); } + reindexPages(req) { + var response = ''; + var self = this; + return new Promise((resolve, reject) => { + self.getPage() + .then(pages => { + let sorted = []; + for (let i = 0; i < pages.length; i++) { + let body = pages[i].metadata; + body.content = pages[i].content; + sorted.push(body); + } + //resorts pages by date created + let byDate = _.sortBy(sorted, page => { + return page.created; + }); + //reassigns id sequentially based on sorted pages + for (let index = 0; index < byDate.length; index++) { + byDate[index].id = index; + self.editPage( + byDate[index], + index, + DataEvent.API_PAGE_WRITE, + req.session.user + ); + } + response = { + type: DataEvent.API_REINDEX_PAGES, + message: 'Pages re-sorted. Easy peasy.', + count: byDate.length + }; + resolve(response); + }) + .catch(err => { + response = { type: DataEvent.PAGE_ERROR, message: err }; + reject(response); + }); + }); + } + //-------------------------- // event handlers //-------------------------- diff --git a/brain/data/Settings.js b/brain/data/Settings.js index fc9829e..95e8c86 100644 --- a/brain/data/Settings.js +++ b/brain/data/Settings.js @@ -122,8 +122,32 @@ export default class Settings { updatePageIndex() { fs.readJSON('site/settings.json').then(settings => { settings.library_stats.current_index = ++settings.library_stats.current_index; - //settings.library_stats.total_pages = ++settings.library_stats.total_pages; - fs.writeJSON('site/settings.json', settings); + setTimeout(() => { + //TODO: Duct tape solution until something better created + fs.writeJSON('site/settings.json', settings) + .then(() => { + //console.log('ALL TO THE GOOD'); + }) + .catch(() => { + //.log('ERR', err); + }); + }, 100); + }); + } + + resetLibraryIndex(index) { + fs.readJSON('site/settings.json').then(settings => { + settings.library_stats.current_index = index; + setTimeout(() => { + //TODO: Duct tape solution until something better created + fs.writeJSON('site/settings.json', settings) + .then(() => { + //console.log('ALL TO THE GOOD'); + }) + .catch(() => { + //.log('ERR', err); + }); + }, 100); }); } load(fileToLoad) { diff --git a/brain/data/Utils.js b/brain/data/Utils.js index 071ab38..fcbb6b8 100644 --- a/brain/data/Utils.js +++ b/brain/data/Utils.js @@ -97,10 +97,6 @@ export default class Utils { } render.publishArchive(archive); } - reindexPages(pages) { - conosle.log('PAGES'); - //let byDate = _.sortBy(pages, ['created']); - } moveAssets() { settings .load(SETTINGS_FILE) diff --git a/brain/views/settings.pug b/brain/views/settings.pug index 9b7c3e6..523bd1e 100644 --- a/brain/views/settings.pug +++ b/brain/views/settings.pug @@ -56,7 +56,9 @@ block main-content button#restore-backup(for='backup-upload') RESTORE BACKUP input(id="backup-upload" type="file" name="backup-upload") #util-2.column - label MAINTENANCE + label MAINTENANCE + br + button#reindex-pages REINDEX PAGES #option-settings.columns #theme-settings.column label THEMES diff --git a/src/com/controllers/SettingsIndex.js b/src/com/controllers/SettingsIndex.js index 9899004..ee3b892 100644 --- a/src/com/controllers/SettingsIndex.js +++ b/src/com/controllers/SettingsIndex.js @@ -92,6 +92,10 @@ export default class SettingsIndex { document .getElementById('create-backup') .addEventListener('click', e => this.handleBackup(e)); + + document + .getElementById('reindex-pages') + .addEventListener('click', e => this.handleReindex(e)); } //-------------------------- // event handlers @@ -199,4 +203,19 @@ export default class SettingsIndex { notify.alert(err, false); }); } + + handleReindex(e) { + e.preventDefault(); + e.stopPropagation(); + let task = { task: 'cleanup pages indexes' }; + notify.alert('Cleaning up page indexes', null); + admin + .handleReindex(task) + .then(r => { + notify.alert(r.message, true); + }) + .catch(err => { + notify.alert(err, false); + }); + } } diff --git a/src/com/events/DataEvent.js b/src/com/events/DataEvent.js index f053afb..2958cad 100644 --- a/src/com/events/DataEvent.js +++ b/src/com/events/DataEvent.js @@ -32,6 +32,7 @@ export const API_BACKUP_DOWNLOAD = 'downloadBackup'; export const API_BACKUP_RESTORE = 'downloadBackup'; export const API_IMAGES_UPLOAD = 'uploadProfileImages'; export const API_RENDER_PAGES = 'renderPages'; +export const API_REINDEX_PAGES = 'reindexPages'; export const API_INIT = 'blogInit'; export const API_INIT_GOOD = 'blogInitGood'; export const API_INIT_LAME = 'blogInitLame'; diff --git a/src/libraries/FipamoAdminAPI.js b/src/libraries/FipamoAdminAPI.js index 7436dec..d34b1d7 100644 --- a/src/libraries/FipamoAdminAPI.js +++ b/src/libraries/FipamoAdminAPI.js @@ -18,6 +18,7 @@ export const API_UPLOAD_AVATAR = '/api/v1/settings/add-avatar'; export const API_UPLOAD_BACKGROUND = '/api/v1/settings/add-feature-background'; export const API_PUBLISH_PAGES = '/api/v1/settings/publish-pages'; export const API_NAV_SYNC = '/api/v1/settings/nav-sync'; +export const API_REINDEX_PAGES = '/api/v1/settings/reindex'; export const API_CREATE_BACKUP = '/api/v1/backup/create'; export const API_DOWNLOAD_BACKUP = '/api/v1/backup/download'; export const API_RESTORE_BACKUP = '/api/v1/backup/restore'; @@ -244,6 +245,24 @@ export default class APIUtils { }); }); } + + handleReindex(data) { + return new Promise((resolve, reject) => { + this._request( + API_REINDEX_PAGES, + DataEvent.API_REINDEX_PAGES, + REQUEST_TYPE_POST, + CONTENT_TYPE_JSON, + data + ) + .then(result => { + resolve(result); + }) + .catch(err => { + reject(err); + }); + }); + } //-------------------------- // private //-------------------------- @@ -276,7 +295,8 @@ export default class APIUtils { eventType === DataEvent.API_PAGE_DELETE || eventType === DataEvent.API_RENDER_PAGES || eventType === DataEvent.API_BACKUP_CREATE || - eventType === DataEvent.API_BACKUP_RESTORE + eventType === DataEvent.API_BACKUP_RESTORE || + eventType === DataEvent.API_REINDEX_PAGES ) request.setRequestHeader('x-access-token', self.token);