From 69c96078ea78ee2c45885a90a6f6a59f9042a33c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Soner=20U=C5=9Fakl=C4=B1?= Date: Mon, 13 Dec 2021 16:00:22 -0500 Subject: [PATCH] breaking: #10077, store nav items in objects --- install/data/navigation.json | 5 +--- public/src/admin/settings/navigation.js | 13 +-------- src/navigation/admin.js | 20 ++++++++----- .../1.19.0/navigation-enabled-hashes.js | 28 +++++++++++++++++++ src/views/admin/settings/navigation.tpl | 2 +- 5 files changed, 44 insertions(+), 24 deletions(-) create mode 100644 src/upgrades/1.19.0/navigation-enabled-hashes.js diff --git a/install/data/navigation.json b/install/data/navigation.json index b338b6a0f4..5df3bda95a 100644 --- a/install/data/navigation.json +++ b/install/data/navigation.json @@ -64,9 +64,6 @@ "iconClass": "fa-cogs", "textClass": "visible-xs-inline", "text": "[[global:header.admin]]", - "groups": ["administrators"], - "properties": { - "targetBlank": false - } + "groups": ["administrators"] } ] \ No newline at end of file diff --git a/public/src/admin/settings/navigation.js b/public/src/admin/settings/navigation.js index 869e97fadd..ae9e406645 100644 --- a/public/src/admin/settings/navigation.js +++ b/public/src/admin/settings/navigation.js @@ -99,12 +99,9 @@ define('admin/settings/navigation', [ const el = $('#enabled').children('[data-index="' + index + '"]'); const form = el.find('form').serializeArray(); const data = {}; - const properties = {}; form.forEach(function (input) { - if (input.name.slice(0, 9) === 'property:' && input.value === 'on') { - properties[input.name.slice(9)] = true; - } else if (data[input.name]) { + if (data[input.name]) { if (!Array.isArray(data[input.name])) { data[input.name] = [ data[input.name], @@ -116,14 +113,6 @@ define('admin/settings/navigation', [ } }); - data.properties = {}; - - for (const prop in properties) { - if (properties.hasOwnProperty(prop)) { - data.properties[prop] = properties[prop]; - } - } - nav.push(data); }); diff --git a/src/navigation/admin.js b/src/navigation/admin.js index bbef6e9da1..ce53dbdba5 100644 --- a/src/navigation/admin.js +++ b/src/navigation/admin.js @@ -15,15 +15,20 @@ pubsub.on('admin:navigation:save', () => { admin.save = async function (data) { const order = Object.keys(data); - const items = data.map((item, index) => { + const bulkSet = []; + data.forEach((item, index) => { item.order = order[index]; - return JSON.stringify(item); + if (item.hasOwnProperty('groups')) { + item.groups = JSON.stringify(item.groups); + } + bulkSet.push([`navigation:enabled:${item.order}`, item]); }); cache = null; pubsub.publish('admin:navigation:save'); + await db.setObjectBulk(bulkSet); await db.delete('navigation:enabled'); - await db.sortedSetAdd('navigation:enabled', order, items); + await db.sortedSetAdd('navigation:enabled', order, order); }; admin.getAdmin = async function () { @@ -55,9 +60,12 @@ admin.get = async function () { if (cache) { return cache.map(item => ({ ...item })); } - const data = await db.getSortedSetRange('navigation:enabled', 0, -1); + const ids = await db.getSortedSetRange('navigation:enabled', 0, -1); + const data = await db.getObjects(ids.map(id => `navigation:enabled:${id}`)); cache = data.map((item) => { - item = JSON.parse(item); + if (item.hasOwnProperty('groups')) { + item.groups = JSON.parse(item.groups); + } item.groups = item.groups || []; if (item.groups && !Array.isArray(item.groups)) { item.groups = [item.groups]; @@ -73,8 +81,6 @@ async function getAvailable() { const core = require('../../install/data/navigation.json').map((item) => { item.core = true; item.id = item.id || ''; - item.properties = item.properties || { targetBlank: false }; - return item; }); diff --git a/src/upgrades/1.19.0/navigation-enabled-hashes.js b/src/upgrades/1.19.0/navigation-enabled-hashes.js new file mode 100644 index 0000000000..e92c1e9cde --- /dev/null +++ b/src/upgrades/1.19.0/navigation-enabled-hashes.js @@ -0,0 +1,28 @@ +'use strict'; + +const db = require('../../database'); + +module.exports = { + name: 'Upgrade navigation items to hashes', + timestamp: Date.UTC(2021, 11, 13), + method: async function () { + const data = await db.getSortedSetRangeWithScores('navigation:enabled', 0, -1); + const order = []; + const bulkSet = []; + + data.forEach((item) => { + const navItem = JSON.parse(item.value); + if (navItem.hasOwnProperty('properties') && navItem.properties) { + if (navItem.properties.hasOwnProperty('targetBlank')) { + navItem.targetBlank = navItem.properties.targetBlank; + } + delete navItem.properties; + } + bulkSet.push([`navigation:enabled:${item.score}`, navItem]); + order.push(item.score); + }); + await db.setObjectBulk(bulkSet); + await db.delete('navigation:enabled'); + await db.sortedSetAdd('navigation:enabled', order, order); + }, +}; diff --git a/src/views/admin/settings/navigation.tpl b/src/views/admin/settings/navigation.tpl index cd406b90ab..d82af8d1ec 100644 --- a/src/views/admin/settings/navigation.tpl +++ b/src/views/admin/settings/navigation.tpl @@ -84,7 +84,7 @@