Merge pull request #1064 from thelounge/xpaw/harden-sort

Rewrite server code of channel sorting
This commit is contained in:
Jérémie Astori 2017-04-23 14:12:53 -04:00 committed by GitHub
commit 30bf20eb12

View File

@ -412,44 +412,40 @@ Client.prototype.open = function(socketId, target) {
}; };
Client.prototype.sort = function(data) { Client.prototype.sort = function(data) {
var self = this; const order = data.order;
var type = data.type; if (!_.isArray(order)) {
var order = data.order || []; return;
var sorted = [];
switch (type) {
case "networks":
order.forEach(i => {
var find = _.find(self.networks, {id: i});
if (find) {
sorted.push(find);
} }
switch (data.type) {
case "networks":
this.networks.sort((a, b) => {
return order.indexOf(a.id) > order.indexOf(b.id);
}); });
self.networks = sorted;
// Sync order to connected clients
this.emit("sync_sort", {order: this.networks.map(obj => obj.id), type: data.type, target: data.target});
break; break;
case "channels": case "channels":
var target = data.target; var network = _.find(this.networks, {id: data.target});
var network = _.find(self.networks, {id: target});
if (!network) { if (!network) {
return; return;
} }
order.forEach(i => {
var find = _.find(network.channels, {id: i}); network.channels.sort((a, b) => {
if (find) { return order.indexOf(a.id) > order.indexOf(b.id);
sorted.push(find);
}
}); });
network.channels = sorted;
// Sync order to connected clients
this.emit("sync_sort", {order: network.channels.map(obj => obj.id), type: data.type, target: data.target});
break; break;
} }
self.save(); this.save();
// Sync order to connected clients
const syncOrder = sorted.map(obj => obj.id);
self.emit("sync_sort", {order: syncOrder, type: type, target: data.target});
}; };
Client.prototype.names = function(data) { Client.prototype.names = function(data) {