pages re-indexing up and running, added button to settings under MAINTENANCE tab

This commit is contained in:
Ro 2020-09-29 22:26:30 +00:00
parent 7df3d22a29
commit 1130bcabdb
8 changed files with 136 additions and 9 deletions

View file

@ -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() {

View file

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

View file

@ -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) {

View file

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

View file

@ -57,6 +57,8 @@ block main-content
input(id="backup-upload" type="file" name="backup-upload")
#util-2.column
label MAINTENANCE
br
button#reindex-pages REINDEX PAGES
#option-settings.columns
#theme-settings.column
label THEMES

View file

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

View file

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

View file

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