From 612d8cf1ea0733499d0e55399aeaaf0e819e142d Mon Sep 17 00:00:00 2001 From: RobinLinus Date: Wed, 16 Dec 2020 04:16:21 +0100 Subject: [PATCH] Use device type as display name MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Pro: - Device types are more descriptive. - Device types don’t change after every reload Con: - Device types may not be unique. --- server/index.js | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/server/index.js b/server/index.js index 208a61b..9f0e494 100644 --- a/server/index.js +++ b/server/index.js @@ -1,5 +1,4 @@ const parser = require('ua-parser-js'); -const { uniqueNamesGenerator, animals, colors } = require('unique-names-generator'); class SnapdropServer { @@ -36,7 +35,7 @@ class SnapdropServer { } catch (e) { return; // TODO: handle malformed JSON } - + switch (message.type) { case 'disconnect': this._leaveRoom(sender); @@ -186,13 +185,22 @@ class Peer { } _setName(req) { - var ua = parser(req.headers['user-agent']); + let ua = parser(req.headers['user-agent']); + + + let displayName = ua.os.name.replace('Mac OS', 'Mac') + ' '; + if (ua.device.model) { + displayName += ua.device.model; + } else { + displayName += ua.browser.name; + } + this.name = { model: ua.device.model, os: ua.os.name, browser: ua.browser.name, type: ua.device.type, - displayName: uniqueNamesGenerator({ length: 2, separator: ' ', dictionaries: [colors, animals], style: 'capital' }) + displayName: displayName }; } @@ -231,4 +239,4 @@ class Peer { }; } -const server = new SnapdropServer(process.env.PORT || 3000); +const server = new SnapdropServer(process.env.PORT || 3000); \ No newline at end of file