From e136edb6aca3c8a7e2bac06fab7bb27ce766fa96 Mon Sep 17 00:00:00 2001 From: Pavel Djundik Date: Thu, 26 Apr 2018 12:06:01 +0300 Subject: [PATCH] Remove network ids and use uuids everywhere --- client/js/socket-events/join.js | 2 +- client/js/socket-events/network.js | 4 ++-- client/js/socket-events/nick.js | 2 +- client/js/socket-events/quit.js | 2 +- client/js/socket-events/sync_sort.js | 12 ++++-------- client/js/sorting.js | 4 ++-- client/views/network.tpl | 2 -- src/client.js | 15 +++++++++++---- src/models/network.js | 5 +---- src/plugins/inputs/nick.js | 2 +- src/plugins/inputs/query.js | 2 +- src/plugins/inputs/quit.js | 2 +- src/plugins/irc-events/banlist.js | 2 +- src/plugins/irc-events/connection.js | 6 +++--- src/plugins/irc-events/error.js | 4 ++-- src/plugins/irc-events/join.js | 2 +- src/plugins/irc-events/list.js | 2 +- src/plugins/irc-events/message.js | 2 +- src/plugins/irc-events/nick.js | 2 +- src/plugins/irc-events/welcome.js | 2 +- src/plugins/irc-events/whois.js | 2 +- test/models/network.js | 3 --- 22 files changed, 38 insertions(+), 43 deletions(-) diff --git a/client/js/socket-events/join.js b/client/js/socket-events/join.js index 31adfb19..69f000db 100644 --- a/client/js/socket-events/join.js +++ b/client/js/socket-events/join.js @@ -9,7 +9,7 @@ const sidebar = $("#sidebar"); socket.on("join", function(data) { const id = data.network; - const network = sidebar.find(`#network-${id}`); + const network = sidebar.find(`.network[data-uuid="${id}"]`); 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 sidebarEntry = templates.chan({ diff --git a/client/js/socket-events/network.js b/client/js/socket-events/network.js index e012d211..a6fa04ef 100644 --- a/client/js/socket-events/network.js +++ b/client/js/socket-events/network.js @@ -19,12 +19,12 @@ socket.on("network", 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) { sidebar - .find("#network-" + data.network) + .find(`.network[data-uuid="${data.network}"]`) .toggleClass("not-connected", !data.connected) .toggleClass("not-secure", !data.secure); }); diff --git a/client/js/socket-events/nick.js b/client/js/socket-events/nick.js index 2fb787e7..71ac1f65 100644 --- a/client/js/socket-events/nick.js +++ b/client/js/socket-events/nick.js @@ -6,7 +6,7 @@ const socket = require("../socket"); socket.on("nick", function(data) { const id = data.network; 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) { $("#nick").text(nick); diff --git a/client/js/socket-events/quit.js b/client/js/socket-events/quit.js index bf176677..df98b399 100644 --- a/client/js/socket-events/quit.js +++ b/client/js/socket-events/quit.js @@ -7,7 +7,7 @@ const sidebar = $("#sidebar"); socket.on("quit", function(data) { const id = data.network; - const network = sidebar.find(`#network-${id}`); + const network = sidebar.find(`.network[data-uuid="${id}"]`); network.children(".chan").each(function() { // this = child diff --git a/client/js/socket-events/sync_sort.js b/client/js/socket-events/sync_sort.js index 3ec9c890..c8f779c6 100644 --- a/client/js/socket-events/sync_sort.js +++ b/client/js/socket-events/sync_sort.js @@ -13,10 +13,10 @@ socket.on("sync_sort", function(data) { const type = data.type; const order = data.order; + const container = $(".networks"); + const network = container.find(`.network[data-uuid="${data.target}"]`); if (type === "networks") { - const container = $(".networks"); - $.each(order, function(index, value) { const position = $(container.children()[index]); @@ -24,13 +24,9 @@ socket.on("sync_sort", function(data) { return true; // No point in continuing } - const network = container.find(`#network-${data.target}`); - - $(network).insertBefore(position); + network.insertBefore(position); }); } else if (type === "channels") { - const network = $(`#network-${data.target}`); - $.each(order, function(index, value) { if (index === 0) { // Shouldn't attempt to move lobby 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 - $(channel).insertBefore(position); + channel.insertBefore(position); }); } }); diff --git a/client/js/sorting.js b/client/js/sorting.js index 2375f594..9864772b 100644 --- a/client/js/sorting.js +++ b/client/js/sorting.js @@ -21,7 +21,7 @@ module.exports = function() { const order = []; sidebar.find(".network").each(function() { - const id = $(this).data("id"); + const id = $(this).data("uuid"); order.push(id); }); @@ -54,7 +54,7 @@ module.exports = function() { socket.emit("sort", { type: "channels", - target: network.data("id"), + target: network.data("uuid"), order: order, }); diff --git a/client/views/network.tpl b/client/views/network.tpl index 607ee358..f84fc348 100644 --- a/client/views/network.tpl +++ b/client/views/network.tpl @@ -1,8 +1,6 @@ {{#each networks}}
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 - 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; case "channels": { - const network = _.find(this.networks, {id: data.target}); + const network = _.find(this.networks, {uuid: data.target}); if (!network) { return; @@ -414,7 +417,11 @@ Client.prototype.sort = function(data) { network.channels.sort((a, b) => order.indexOf(a.id) - order.indexOf(b.id)); // 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; } diff --git a/src/models/network.js b/src/models/network.js index ff7eee67..20eff20d 100644 --- a/src/models/network.js +++ b/src/models/network.js @@ -9,8 +9,6 @@ const Helper = require("../helper"); module.exports = Network; -let id = 1; - /** * @type {Object} List of keys which should not be sent to the client. */ @@ -37,7 +35,6 @@ function Network(attr) { channels: [], ip: null, hostname: null, - id: id++, irc: null, serverOptions: { PREFIX: [], @@ -200,7 +197,7 @@ Network.prototype.edit = function(client, args) { // Update UI nick straight away if IRC is not connected client.emit("nick", { - network: this.id, + network: this.uuid, nick: this.nick, }); } diff --git a/src/plugins/inputs/nick.js b/src/plugins/inputs/nick.js index 412a47bf..a0a486ed 100644 --- a/src/plugins/inputs/nick.js +++ b/src/plugins/inputs/nick.js @@ -32,7 +32,7 @@ exports.input = function(network, chan, cmd, args) { network.setNick(newNick); this.emit("nick", { - network: network.id, + network: network.uuid, nick: newNick, }); } diff --git a/src/plugins/inputs/query.js b/src/plugins/inputs/query.js index 7d8c787a..7712146b 100644 --- a/src/plugins/inputs/query.js +++ b/src/plugins/inputs/query.js @@ -49,7 +49,7 @@ exports.input = function(network, chan, cmd, args) { }); this.emit("join", { - network: network.id, + network: network.uuid, chan: newChan.getFilteredClone(true), shouldOpen: true, index: network.addChannel(newChan), diff --git a/src/plugins/inputs/quit.js b/src/plugins/inputs/quit.js index 3b21ca49..252f050b 100644 --- a/src/plugins/inputs/quit.js +++ b/src/plugins/inputs/quit.js @@ -13,7 +13,7 @@ exports.input = function(network, chan, cmd, args) { network.destroy(); client.save(); client.emit("quit", { - network: network.id, + network: network.uuid, }); if (network.irc) { diff --git a/src/plugins/irc-events/banlist.js b/src/plugins/irc-events/banlist.js index cd61843e..f058b665 100644 --- a/src/plugins/irc-events/banlist.js +++ b/src/plugins/irc-events/banlist.js @@ -38,7 +38,7 @@ module.exports = function(irc, network) { name: chanName, }); client.emit("join", { - network: network.id, + network: network.uuid, chan: chan.getFilteredClone(true), index: network.addChannel(chan), }); diff --git a/src/plugins/irc-events/connection.js b/src/plugins/irc-events/connection.js index 780b52e3..94bddad7 100644 --- a/src/plugins/irc-events/connection.js +++ b/src/plugins/irc-events/connection.js @@ -106,7 +106,7 @@ module.exports = function(irc, network) { if (Helper.config.debug.ircFramework) { 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; client.emit("network_changed", { - network: network.id, + network: network.uuid, serverOptions: network.serverOptions, }); }); function sendStatus() { const status = network.getNetworkStatus(); - status.network = network.id; + status.network = network.uuid; client.emit("network:status", status); } diff --git a/src/plugins/irc-events/error.js b/src/plugins/irc-events/error.js index df1d99f5..ab260621 100644 --- a/src/plugins/irc-events/error.js +++ b/src/plugins/irc-events/error.js @@ -45,7 +45,7 @@ module.exports = function(irc, network) { } client.emit("nick", { - network: network.id, + network: network.uuid, nick: irc.user.nick, }); }); @@ -64,7 +64,7 @@ module.exports = function(irc, network) { } client.emit("nick", { - network: network.id, + network: network.uuid, nick: irc.user.nick, }); }); diff --git a/src/plugins/irc-events/join.js b/src/plugins/irc-events/join.js index cba391a2..29031f93 100644 --- a/src/plugins/irc-events/join.js +++ b/src/plugins/irc-events/join.js @@ -17,7 +17,7 @@ module.exports = function(irc, network) { }); client.emit("join", { - network: network.id, + network: network.uuid, chan: chan.getFilteredClone(true), index: network.addChannel(chan), }); diff --git a/src/plugins/irc-events/list.js b/src/plugins/irc-events/list.js index 98cc6a83..0a94bafe 100644 --- a/src/plugins/irc-events/list.js +++ b/src/plugins/irc-events/list.js @@ -48,7 +48,7 @@ module.exports = function(irc, network) { }); client.emit("join", { - network: network.id, + network: network.uuid, chan: chan.getFilteredClone(true), index: network.addChannel(chan), }); diff --git a/src/plugins/irc-events/message.js b/src/plugins/irc-events/message.js index f4fd2221..fa20c6a8 100644 --- a/src/plugins/irc-events/message.js +++ b/src/plugins/irc-events/message.js @@ -69,7 +69,7 @@ module.exports = function(irc, network) { }); client.emit("join", { - network: network.id, + network: network.uuid, chan: chan.getFilteredClone(true), index: network.addChannel(chan), }); diff --git a/src/plugins/irc-events/nick.js b/src/plugins/irc-events/nick.js index 42ad216c..322b97d4 100644 --- a/src/plugins/irc-events/nick.js +++ b/src/plugins/irc-events/nick.js @@ -20,7 +20,7 @@ module.exports = function(irc, network) { client.save(); client.emit("nick", { - network: network.id, + network: network.uuid, nick: data.new_nick, }); } diff --git a/src/plugins/irc-events/welcome.js b/src/plugins/irc-events/welcome.js index 010eae8b..2e258bb8 100644 --- a/src/plugins/irc-events/welcome.js +++ b/src/plugins/irc-events/welcome.js @@ -16,7 +16,7 @@ module.exports = function(irc, network) { client.save(); client.emit("nick", { - network: network.id, + network: network.uuid, nick: data.nick, }); }); diff --git a/src/plugins/irc-events/whois.js b/src/plugins/irc-events/whois.js index 3f7e7355..6fe09599 100644 --- a/src/plugins/irc-events/whois.js +++ b/src/plugins/irc-events/whois.js @@ -16,7 +16,7 @@ module.exports = function(irc, network) { client.emit("join", { shouldOpen: true, - network: network.id, + network: network.uuid, chan: chan.getFilteredClone(true), index: network.addChannel(chan), }); diff --git a/test/models/network.js b/test/models/network.js index 9ac04e2a..7fe7f374 100644 --- a/test/models/network.js +++ b/test/models/network.js @@ -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", ip: "newIp", hostname: "newHostname", - id: 1000000, guid: "newGuid", }); expect(saveCalled).to.be.true; - expect(network.id).to.not.equal(1000000); expect(network.guid).to.not.equal("newGuid"); expect(network.ip).to.be.null; expect(network.hostname).to.be.null; @@ -227,7 +225,6 @@ describe("Network", function() { "commands", "host", "hostname", - "id", "ip", "name", "port",