Use device type as display name

Pro:
- Device types are more descriptive.
- Device types don’t change after every reload

Con:
- Device types may not be unique.
This commit is contained in:
RobinLinus 2020-12-16 04:16:21 +01:00
parent ea9ce95112
commit 612d8cf1ea

View file

@ -1,5 +1,4 @@
const parser = require('ua-parser-js'); const parser = require('ua-parser-js');
const { uniqueNamesGenerator, animals, colors } = require('unique-names-generator');
class SnapdropServer { class SnapdropServer {
@ -36,7 +35,7 @@ class SnapdropServer {
} catch (e) { } catch (e) {
return; // TODO: handle malformed JSON return; // TODO: handle malformed JSON
} }
switch (message.type) { switch (message.type) {
case 'disconnect': case 'disconnect':
this._leaveRoom(sender); this._leaveRoom(sender);
@ -186,13 +185,22 @@ class Peer {
} }
_setName(req) { _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 = { this.name = {
model: ua.device.model, model: ua.device.model,
os: ua.os.name, os: ua.os.name,
browser: ua.browser.name, browser: ua.browser.name,
type: ua.device.type, 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);