Remove network ids and use uuids everywhere

This commit is contained in:
Pavel Djundik 2018-04-26 12:06:01 +03:00
parent 4f85779e78
commit e136edb6ac
22 changed files with 38 additions and 43 deletions

View File

@ -9,7 +9,7 @@ const sidebar = $("#sidebar");
socket.on("join", function(data) { socket.on("join", function(data) {
const id = data.network; const id = data.network;
const network = sidebar.find(`#network-${id}`); const network = sidebar.find(`.network[data-uuid="${id}"]`);
const channels = network.children(); const channels = network.children();
const position = $(channels[data.index || channels.length - 1]); // Put channel in correct position, or the end if we don't have one const position = $(channels[data.index || channels.length - 1]); // Put channel in correct position, or the end if we don't have one
const sidebarEntry = templates.chan({ const sidebarEntry = templates.chan({

View File

@ -19,12 +19,12 @@ socket.on("network", function(data) {
}); });
socket.on("network_changed", function(data) { socket.on("network_changed", function(data) {
sidebar.find("#network-" + data.network).data("options", data.serverOptions); sidebar.find(`.network[data-uuid="${data.network}"]`).data("options", data.serverOptions);
}); });
socket.on("network:status", function(data) { socket.on("network:status", function(data) {
sidebar sidebar
.find("#network-" + data.network) .find(`.network[data-uuid="${data.network}"]`)
.toggleClass("not-connected", !data.connected) .toggleClass("not-connected", !data.connected)
.toggleClass("not-secure", !data.secure); .toggleClass("not-secure", !data.secure);
}); });

View File

@ -6,7 +6,7 @@ const socket = require("../socket");
socket.on("nick", function(data) { socket.on("nick", function(data) {
const id = data.network; const id = data.network;
const nick = data.nick; const nick = data.nick;
const network = $("#sidebar").find("#network-" + id).data("nick", nick); const network = $(`#sidebar .network[data-uuid="${id}"]`).data("nick", nick);
if (network.find(".active").length) { if (network.find(".active").length) {
$("#nick").text(nick); $("#nick").text(nick);

View File

@ -7,7 +7,7 @@ const sidebar = $("#sidebar");
socket.on("quit", function(data) { socket.on("quit", function(data) {
const id = data.network; const id = data.network;
const network = sidebar.find(`#network-${id}`); const network = sidebar.find(`.network[data-uuid="${id}"]`);
network.children(".chan").each(function() { network.children(".chan").each(function() {
// this = child // this = child

View File

@ -13,10 +13,10 @@ socket.on("sync_sort", function(data) {
const type = data.type; const type = data.type;
const order = data.order; const order = data.order;
const container = $(".networks");
const network = container.find(`.network[data-uuid="${data.target}"]`);
if (type === "networks") { if (type === "networks") {
const container = $(".networks");
$.each(order, function(index, value) { $.each(order, function(index, value) {
const position = $(container.children()[index]); const position = $(container.children()[index]);
@ -24,13 +24,9 @@ socket.on("sync_sort", function(data) {
return true; // No point in continuing return true; // No point in continuing
} }
const network = container.find(`#network-${data.target}`); network.insertBefore(position);
$(network).insertBefore(position);
}); });
} else if (type === "channels") { } else if (type === "channels") {
const network = $(`#network-${data.target}`);
$.each(order, function(index, value) { $.each(order, function(index, value) {
if (index === 0) { // Shouldn't attempt to move lobby if (index === 0) { // Shouldn't attempt to move lobby
return true; // same as `continue` -> skip to next item return true; // same as `continue` -> skip to next item
@ -44,7 +40,7 @@ socket.on("sync_sort", function(data) {
const channel = network.find(".chan[data-id=" + value + "]"); // Channel at position const channel = network.find(".chan[data-id=" + value + "]"); // Channel at position
$(channel).insertBefore(position); channel.insertBefore(position);
}); });
} }
}); });

View File

@ -21,7 +21,7 @@ module.exports = function() {
const order = []; const order = [];
sidebar.find(".network").each(function() { sidebar.find(".network").each(function() {
const id = $(this).data("id"); const id = $(this).data("uuid");
order.push(id); order.push(id);
}); });
@ -54,7 +54,7 @@ module.exports = function() {
socket.emit("sort", { socket.emit("sort", {
type: "channels", type: "channels",
target: network.data("id"), target: network.data("uuid"),
order: order, order: order,
}); });

View File

@ -1,8 +1,6 @@
{{#each networks}} {{#each networks}}
<section <section
id="network-{{id}}"
class="network name-{{slugify name}} {{#if serverOptions.NETWORK}}network-{{slugify serverOptions.NETWORK}}{{/if}} {{#unless status.connected}}not-connected{{/unless}} {{#unless status.secure}}not-secure{{/unless}}" class="network name-{{slugify name}} {{#if serverOptions.NETWORK}}network-{{slugify serverOptions.NETWORK}}{{/if}} {{#unless status.connected}}not-connected{{/unless}} {{#unless status.secure}}not-secure{{/unless}}"
data-id="{{id}}"
data-uuid="{{uuid}}" data-uuid="{{uuid}}"
data-nick="{{nick}}" data-nick="{{nick}}"
data-options="{{tojson serverOptions}}" data-options="{{tojson serverOptions}}"

View File

@ -397,15 +397,18 @@ Client.prototype.sort = function(data) {
switch (data.type) { switch (data.type) {
case "networks": case "networks":
this.networks.sort((a, b) => order.indexOf(a.id) - order.indexOf(b.id)); this.networks.sort((a, b) => order.indexOf(a.uuid) - order.indexOf(b.uuid));
// Sync order to connected clients // Sync order to connected clients
this.emit("sync_sort", {order: this.networks.map((obj) => obj.id), type: data.type, target: data.target}); this.emit("sync_sort", {
order: this.networks.map((obj) => obj.uuid),
type: data.type,
});
break; break;
case "channels": { case "channels": {
const network = _.find(this.networks, {id: data.target}); const network = _.find(this.networks, {uuid: data.target});
if (!network) { if (!network) {
return; return;
@ -414,7 +417,11 @@ Client.prototype.sort = function(data) {
network.channels.sort((a, b) => order.indexOf(a.id) - order.indexOf(b.id)); network.channels.sort((a, b) => order.indexOf(a.id) - order.indexOf(b.id));
// Sync order to connected clients // Sync order to connected clients
this.emit("sync_sort", {order: network.channels.map((obj) => obj.id), type: data.type, target: data.target}); this.emit("sync_sort", {
order: network.channels.map((obj) => obj.id),
type: data.type,
target: network.uuid,
});
break; break;
} }

View File

@ -9,8 +9,6 @@ const Helper = require("../helper");
module.exports = Network; module.exports = Network;
let id = 1;
/** /**
* @type {Object} List of keys which should not be sent to the client. * @type {Object} List of keys which should not be sent to the client.
*/ */
@ -37,7 +35,6 @@ function Network(attr) {
channels: [], channels: [],
ip: null, ip: null,
hostname: null, hostname: null,
id: id++,
irc: null, irc: null,
serverOptions: { serverOptions: {
PREFIX: [], PREFIX: [],
@ -200,7 +197,7 @@ Network.prototype.edit = function(client, args) {
// Update UI nick straight away if IRC is not connected // Update UI nick straight away if IRC is not connected
client.emit("nick", { client.emit("nick", {
network: this.id, network: this.uuid,
nick: this.nick, nick: this.nick,
}); });
} }

View File

@ -32,7 +32,7 @@ exports.input = function(network, chan, cmd, args) {
network.setNick(newNick); network.setNick(newNick);
this.emit("nick", { this.emit("nick", {
network: network.id, network: network.uuid,
nick: newNick, nick: newNick,
}); });
} }

View File

@ -49,7 +49,7 @@ exports.input = function(network, chan, cmd, args) {
}); });
this.emit("join", { this.emit("join", {
network: network.id, network: network.uuid,
chan: newChan.getFilteredClone(true), chan: newChan.getFilteredClone(true),
shouldOpen: true, shouldOpen: true,
index: network.addChannel(newChan), index: network.addChannel(newChan),

View File

@ -13,7 +13,7 @@ exports.input = function(network, chan, cmd, args) {
network.destroy(); network.destroy();
client.save(); client.save();
client.emit("quit", { client.emit("quit", {
network: network.id, network: network.uuid,
}); });
if (network.irc) { if (network.irc) {

View File

@ -38,7 +38,7 @@ module.exports = function(irc, network) {
name: chanName, name: chanName,
}); });
client.emit("join", { client.emit("join", {
network: network.id, network: network.uuid,
chan: chan.getFilteredClone(true), chan: chan.getFilteredClone(true),
index: network.addChannel(chan), index: network.addChannel(chan),
}); });

View File

@ -106,7 +106,7 @@ module.exports = function(irc, network) {
if (Helper.config.debug.ircFramework) { if (Helper.config.debug.ircFramework) {
irc.on("debug", function(message) { irc.on("debug", function(message) {
log.debug("[" + client.name + " (" + client.id + ") on " + network.name + " (#" + network.id + ")]", message); log.debug(`[${client.name} (${client.id}) on ${network.name} (${network.uuid}]`, message);
}); });
} }
@ -155,14 +155,14 @@ module.exports = function(irc, network) {
network.serverOptions.NETWORK = data.options.NETWORK; network.serverOptions.NETWORK = data.options.NETWORK;
client.emit("network_changed", { client.emit("network_changed", {
network: network.id, network: network.uuid,
serverOptions: network.serverOptions, serverOptions: network.serverOptions,
}); });
}); });
function sendStatus() { function sendStatus() {
const status = network.getNetworkStatus(); const status = network.getNetworkStatus();
status.network = network.id; status.network = network.uuid;
client.emit("network:status", status); client.emit("network:status", status);
} }

View File

@ -45,7 +45,7 @@ module.exports = function(irc, network) {
} }
client.emit("nick", { client.emit("nick", {
network: network.id, network: network.uuid,
nick: irc.user.nick, nick: irc.user.nick,
}); });
}); });
@ -64,7 +64,7 @@ module.exports = function(irc, network) {
} }
client.emit("nick", { client.emit("nick", {
network: network.id, network: network.uuid,
nick: irc.user.nick, nick: irc.user.nick,
}); });
}); });

View File

@ -17,7 +17,7 @@ module.exports = function(irc, network) {
}); });
client.emit("join", { client.emit("join", {
network: network.id, network: network.uuid,
chan: chan.getFilteredClone(true), chan: chan.getFilteredClone(true),
index: network.addChannel(chan), index: network.addChannel(chan),
}); });

View File

@ -48,7 +48,7 @@ module.exports = function(irc, network) {
}); });
client.emit("join", { client.emit("join", {
network: network.id, network: network.uuid,
chan: chan.getFilteredClone(true), chan: chan.getFilteredClone(true),
index: network.addChannel(chan), index: network.addChannel(chan),
}); });

View File

@ -69,7 +69,7 @@ module.exports = function(irc, network) {
}); });
client.emit("join", { client.emit("join", {
network: network.id, network: network.uuid,
chan: chan.getFilteredClone(true), chan: chan.getFilteredClone(true),
index: network.addChannel(chan), index: network.addChannel(chan),
}); });

View File

@ -20,7 +20,7 @@ module.exports = function(irc, network) {
client.save(); client.save();
client.emit("nick", { client.emit("nick", {
network: network.id, network: network.uuid,
nick: data.new_nick, nick: data.new_nick,
}); });
} }

View File

@ -16,7 +16,7 @@ module.exports = function(irc, network) {
client.save(); client.save();
client.emit("nick", { client.emit("nick", {
network: network.id, network: network.uuid,
nick: data.nick, nick: data.nick,
}); });
}); });

View File

@ -16,7 +16,7 @@ module.exports = function(irc, network) {
client.emit("join", { client.emit("join", {
shouldOpen: true, shouldOpen: true,
network: network.id, network: network.uuid,
chan: chan.getFilteredClone(true), chan: chan.getFilteredClone(true),
index: network.addChannel(chan), index: network.addChannel(chan),
}); });

View File

@ -120,12 +120,10 @@ describe("Network", function() {
commands: "/command 1 2 3\r\n/ping HELLO\r\r\r\r/whois test\r\n\r\n", commands: "/command 1 2 3\r\n/ping HELLO\r\r\r\r/whois test\r\n\r\n",
ip: "newIp", ip: "newIp",
hostname: "newHostname", hostname: "newHostname",
id: 1000000,
guid: "newGuid", guid: "newGuid",
}); });
expect(saveCalled).to.be.true; expect(saveCalled).to.be.true;
expect(network.id).to.not.equal(1000000);
expect(network.guid).to.not.equal("newGuid"); expect(network.guid).to.not.equal("newGuid");
expect(network.ip).to.be.null; expect(network.ip).to.be.null;
expect(network.hostname).to.be.null; expect(network.hostname).to.be.null;
@ -227,7 +225,6 @@ describe("Network", function() {
"commands", "commands",
"host", "host",
"hostname", "hostname",
"id",
"ip", "ip",
"name", "name",
"port", "port",