Skip to content

Node.js based forum software built for the modern web

fix: #7625, on group rename update nav/widget items

Barış Soner Uşaklı5b85ed318ae2
diff --git a/src/groups/update.js b/src/groups/update.js
index 5075292a79..3dd68c97c6 100644
--- a/src/groups/update.js
+++ b/src/groups/update.js
@@ -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);
+ }
};
diff --git a/src/widgets/index.js b/src/widgets/index.js
index 216f4435e0..fcb9ada3f3 100644
--- a/src/widgets/index.js
+++ b/src/widgets/index.js
@@ -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'));