From 731b29c059be2b50d550f37df89e54508b9ea21a Mon Sep 17 00:00:00 2001 From: Pavel Djundik Date: Tue, 28 Nov 2017 19:25:15 +0200 Subject: [PATCH] Generate uuid per network --- package.json | 1 + src/client.js | 1 + src/models/network.js | 6 ++++++ src/server.js | 1 + test/models/network.js | 12 ++++++++++++ 5 files changed, 21 insertions(+) diff --git a/package.json b/package.json index 1f3c509b..732e0ffc 100644 --- a/package.json +++ b/package.json @@ -59,6 +59,7 @@ "thelounge-ldapjs-non-maintained-fork": "1.0.2", "ua-parser-js": "0.7.17", "urijs": "1.19.1", + "uuid": "3.2.1", "web-push": "3.3.0", "yarn": "1.5.1" }, diff --git a/src/client.js b/src/client.js index f69f6991..980c9d49 100644 --- a/src/client.js +++ b/src/client.js @@ -174,6 +174,7 @@ Client.prototype.connect = function(args) { args.hostname = args.hostname || (client.config && client.config.hostname) || client.hostname; const network = new Network({ + uuid: args.uuid, name: args.name || (Helper.config.displayNetwork ? "" : Helper.config.defaults.name) || "", host: args.host || "", port: parseInt(args.port, 10) || (args.tls ? 6697 : 6667), diff --git a/src/models/network.js b/src/models/network.js index 9aadb319..aba3218b 100644 --- a/src/models/network.js +++ b/src/models/network.js @@ -1,6 +1,7 @@ "use strict"; const _ = require("lodash"); +const uuidv4 = require("uuid/v4"); const Chan = require("./chan"); module.exports = Network; @@ -42,6 +43,10 @@ function Network(attr) { chanCache: [], }); + if (!this.uuid) { + this.uuid = uuidv4(); + } + if (!this.name) { this.name = this.host; } @@ -125,6 +130,7 @@ Network.prototype.getNetworkStatus = function() { Network.prototype.export = function() { const network = _.pick(this, [ + "uuid", "awayMessage", "nick", "name", diff --git a/src/server.js b/src/server.js index fc1ffa1b..70a08e45 100644 --- a/src/server.js +++ b/src/server.js @@ -306,6 +306,7 @@ function initializeClient(socket, client, token, lastMessage) { // prevent people from overriding webirc settings data.ip = null; data.hostname = null; + data.uuid = null; client.connect(data); } diff --git a/test/models/network.js b/test/models/network.js index 32d49d4c..95987258 100644 --- a/test/models/network.js +++ b/test/models/network.js @@ -10,6 +10,7 @@ describe("Network", function() { describe("#export()", function() { it("should produce an valid object", function() { const network = new Network({ + uuid: "hello world", awayMessage: "I am away", name: "networkName", channels: [ @@ -24,6 +25,7 @@ describe("Network", function() { network.setNick("chillin`"); expect(network.export()).to.deep.equal({ + uuid: "hello world", awayMessage: "I am away", name: "networkName", host: "", @@ -47,6 +49,15 @@ describe("Network", function() { }); }); + it("should generate uuid (v4) for each network", function() { + const network1 = new Network(); + const network2 = new Network(); + + expect(network1.uuid).to.have.lengthOf(36); + expect(network2.uuid).to.have.lengthOf(36); + expect(network1.uuid).to.not.equal(network2.uuid); + }); + it("lobby should be at the top", function() { const network = new Network({ name: "Super Nice Network", @@ -126,6 +137,7 @@ describe("Network", function() { "status", "tls", "rejectUnauthorized", + "uuid", "username" );