fix: upgrade script to work from 0.x to 2.x

isekai-main
Barış Soner Uşaklı 2 years ago
parent d264c6ac4b
commit a31ba824ea

@ -4,9 +4,7 @@ module.exports = {
name: 'Navigation item visibility groups', name: 'Navigation item visibility groups',
timestamp: Date.UTC(2018, 10, 10), timestamp: Date.UTC(2018, 10, 10),
method: async function () { method: async function () {
const navigationAdmin = require('../../navigation/admin'); const data = await navigationAdminGet();
const data = await navigationAdmin.get();
data.forEach((navItem) => { data.forEach((navItem) => {
if (navItem && navItem.properties) { if (navItem && navItem.properties) {
navItem.groups = []; navItem.groups = [];
@ -23,6 +21,38 @@ module.exports = {
} }
} }
}); });
await navigationAdmin.save(data); await navigationAdminSave(data);
}, },
}; };
// use navigation.get/save as it was in 1.11.0 so upgrade script doesn't crash on latest nbb
// see https://github.com/NodeBB/NodeBB/pull/11013
async function navigationAdminGet() {
const db = require('../../database');
const data = await db.getSortedSetRange('navigation:enabled', 0, -1);
return data.filter(Boolean).map((item) => {
item = JSON.parse(item);
item.groups = item.groups || [];
if (item.groups && !Array.isArray(item.groups)) {
item.groups = [item.groups];
}
return item;
});
}
async function navigationAdminSave(data) {
const db = require('../../database');
const translator = require('../../translator');
const order = Object.keys(data);
const items = data.map((item, index) => {
Object.keys(item).forEach((key) => {
if (item.hasOwnProperty(key) && typeof item[key] === 'string' && (key === 'title' || key === 'text')) {
item[key] = translator.escape(item[key]);
}
});
item.order = order[index];
return JSON.stringify(item);
});
await db.delete('navigation:enabled');
await db.sortedSetAdd('navigation:enabled', order, items);
}

Loading…
Cancel
Save