mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-10-26 08:36:12 +01:00
fix: #7625, on group rename update nav/widget items
This commit is contained in:
@@ -216,6 +216,8 @@ module.exports = function (Groups) {
|
||||
}
|
||||
async.series([
|
||||
async.apply(updateMemberGroupTitles, oldName, newName),
|
||||
async.apply(updateNavigationItems, oldName, newName),
|
||||
async.apply(updateWidgets, oldName, newName),
|
||||
async.apply(db.setObjectField, 'group:' + oldName, 'name', newName),
|
||||
async.apply(db.setObjectField, 'group:' + oldName, 'slug', utils.slugify(newName)),
|
||||
async.apply(db.deleteObjectField, 'groupslug:groupname', group.slug),
|
||||
@@ -293,4 +295,44 @@ module.exports = function (Groups) {
|
||||
},
|
||||
], callback);
|
||||
}
|
||||
|
||||
function updateNavigationItems(oldName, newName, callback) {
|
||||
const navigation = require('../navigation/admin');
|
||||
|
||||
async.waterfall([
|
||||
navigation.get,
|
||||
function (navItems, next) {
|
||||
navItems.forEach(function (navItem) {
|
||||
if (navItem && Array.isArray(navItem.groups) && navItem.groups.includes(oldName)) {
|
||||
navItem.groups.splice(navItem.groups.indexOf(oldName), 1, newName);
|
||||
}
|
||||
});
|
||||
|
||||
navigation.save(navItems, next);
|
||||
},
|
||||
], callback);
|
||||
}
|
||||
|
||||
function updateWidgets(oldName, newName, callback) {
|
||||
const admin = require('../widgets/admin');
|
||||
const widgets = require('../widgets');
|
||||
async.waterfall([
|
||||
admin.get,
|
||||
function (data, next) {
|
||||
async.eachSeries(data.areas, function (area, next) {
|
||||
if (!area.data.length) {
|
||||
return setImmediate(next);
|
||||
}
|
||||
area.widgets = area.data;
|
||||
area.widgets.forEach(function (widget) {
|
||||
if (widget && widget.data && Array.isArray(widget.data.groups) && widget.data.groups.includes(oldName)) {
|
||||
widget.data.groups.splice(widget.data.groups.indexOf(oldName), 1, newName);
|
||||
}
|
||||
});
|
||||
|
||||
widgets.setArea(area, next);
|
||||
}, next);
|
||||
},
|
||||
], callback);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -141,16 +141,7 @@ widgets.getWidgetDataForTemplates = function (templates, callback) {
|
||||
locations.forEach(function (location) {
|
||||
if (templateWidgetData && templateWidgetData[location]) {
|
||||
try {
|
||||
returnData[template][location] = JSON.parse(templateWidgetData[location]);
|
||||
returnData[template][location] = returnData[template][location].map(function (widget) {
|
||||
if (widget) {
|
||||
widget.data.groups = widget.data.groups || [];
|
||||
if (widget.data.groups && !Array.isArray(widget.data.groups)) {
|
||||
widget.data.groups = [widget.data.groups];
|
||||
}
|
||||
}
|
||||
return widget;
|
||||
});
|
||||
returnData[template][location] = parseWidgetData(templateWidgetData[location]);
|
||||
} catch (err) {
|
||||
winston.error('can not parse widget data. template: ' + template + ' location: ' + location);
|
||||
returnData[template][location] = [];
|
||||
@@ -176,7 +167,7 @@ widgets.getArea = function (template, location, callback) {
|
||||
return callback(null, []);
|
||||
}
|
||||
try {
|
||||
result = JSON.parse(result);
|
||||
result = parseWidgetData(result);
|
||||
} catch (err) {
|
||||
return callback(err);
|
||||
}
|
||||
@@ -186,6 +177,19 @@ widgets.getArea = function (template, location, callback) {
|
||||
], callback);
|
||||
};
|
||||
|
||||
function parseWidgetData(data) {
|
||||
const widgets = JSON.parse(data);
|
||||
widgets.forEach(function (widget) {
|
||||
if (widget) {
|
||||
widget.data.groups = widget.data.groups || [];
|
||||
if (widget.data.groups && !Array.isArray(widget.data.groups)) {
|
||||
widget.data.groups = [widget.data.groups];
|
||||
}
|
||||
}
|
||||
});
|
||||
return widgets;
|
||||
}
|
||||
|
||||
widgets.setArea = function (area, callback) {
|
||||
if (!area.location || !area.template) {
|
||||
return callback(new Error('Missing location and template data'));
|
||||
|
||||
Reference in New Issue
Block a user