From 9b3571feacc76d4981adbe162f94b64510248752 Mon Sep 17 00:00:00 2001 From: schlagmichdoch Date: Thu, 16 May 2024 19:37:32 +0200 Subject: [PATCH] Refactor BrowserTabsConnector and PeersManager --- public/scripts/browser-tabs-connector.js | 21 +++++-- public/scripts/network.js | 72 ++++++++++++++++++------ public/scripts/ui-main.js | 12 ++-- 3 files changed, 75 insertions(+), 30 deletions(-) diff --git a/public/scripts/browser-tabs-connector.js b/public/scripts/browser-tabs-connector.js index acc0005..0329319 100644 --- a/public/scripts/browser-tabs-connector.js +++ b/public/scripts/browser-tabs-connector.js @@ -2,18 +2,27 @@ class BrowserTabsConnector { constructor() { this.bc = new BroadcastChannel('pairdrop'); this.bc.addEventListener('message', e => this._onMessage(e)); - Events.on('broadcast-send', e => this._broadcastSend(e.detail)); + Events.on('broadcast-send', e => this._broadcastSend(e.detail.type, e.detail.data)); + Events.on('broadcast-self-display-name-changed', e => this._onBroadcastSelfDisplayNameChanged(e.detail.displayName)); } - _broadcastSend(message) { - this.bc.postMessage(message); + _broadcastSend(type, data) { + this.bc.postMessage({ type, data }); + } + + _onBroadcastSelfDisplayNameChanged(displayName) { + this._broadcastSend('self-display-name-changed', { displayName: displayName }); } _onMessage(e) { - Logger.debug('Broadcast:', e.data) - switch (e.data.type) { + const type = e.data.type; + const data = e.data.data; + + Logger.debug('Broadcast:', type, data); + + switch (type) { case 'self-display-name-changed': - Events.fire('self-display-name-changed', e.data.detail); + Events.fire('self-display-name-changed', data.displayName); break; } } diff --git a/public/scripts/network.js b/public/scripts/network.js index af4900c..be2ae21 100644 --- a/public/scripts/network.js +++ b/public/scripts/network.js @@ -550,7 +550,7 @@ class Peer { } Events.fire('peer-display-name-changed', {peerId: this._peerId, displayName: message.displayName}); - Events.fire('notify-peer-display-name-changed', this._peerId); + Events.fire('notify-display-name-changed', { recipient: this._peerId }); } _sendState() { @@ -1487,13 +1487,16 @@ class WSPeer extends Peer { class PeersManager { constructor(serverConnection) { - this.peers = {}; this._server = serverConnection; + this.peers = {}; + this._device = { + originalDisplayName: '', + displayName: '', + publicRoomId: null + }; + Events.on('signal', e => this._onSignal(e.detail)); Events.on('peers', e => this._onPeers(e.detail)); - Events.on('files-selected', e => this._onFilesSelected(e.detail)); - Events.on('respond-to-files-transfer-request', e => this._onRespondToFileTransferRequest(e.detail)) - Events.on('send-text', e => this._onSendText(e.detail)); Events.on('peer-left', e => this._onPeerLeft(e.detail)); Events.on('peer-joined', e => this._onPeerJoined(e.detail)); Events.on('peer-connected', e => this._onPeerConnected(e.detail.peerId)); @@ -1505,16 +1508,25 @@ class PeersManager { // peer closes connection Events.on('secret-room-deleted', e => this._onSecretRoomDeleted(e.detail)); - Events.on('room-secret-regenerated', e => this._onRoomSecretRegenerated(e.detail)); + + // peer Events.on('display-name', e => this._onDisplayName(e.detail.displayName)); - Events.on('self-display-name-changed', e => this._notifyPeersDisplayNameChanged(e.detail)); - Events.on('notify-peer-display-name-changed', e => this._notifyPeerDisplayNameChanged(e.detail)); + Events.on('self-display-name-changed', e => this._notifyPeersDisplayNameChanged(e.detail.displayName)); + Events.on('notify-display-name-changed', e => this._notifyPeerDisplayNameChanged(e.detail.recipient)); Events.on('auto-accept-updated', e => this._onAutoAcceptUpdated(e.detail.roomSecret, e.detail.autoAccept)); + + // transfer + Events.on('send-text', e => this._onSendText(e.detail)); + Events.on('files-selected', e => this._onFilesSelected(e.detail)); + Events.on('respond-to-files-transfer-request', e => this._onRespondToFileTransferRequest(e.detail)) + + // websocket connection Events.on('ws-disconnected', _ => this._onWsDisconnected()); Events.on('ws-relay', e => this._onWsRelay(e.detail.peerId, e.detail.message)); Events.on('ws-config', e => this._onWsConfig(e.detail)); + // no-sleep Events.on('evaluate-no-sleep', _ => this._onEvaluateNoSleep()); } @@ -1664,25 +1676,34 @@ class PeersManager { } _onRoomSecretsDeleted(roomSecrets) { - for (let i=0; i 1) { peer._removeRoomType(roomType); @@ -1710,7 +1731,10 @@ class PeersManager { } _notifyPeersDisplayNameChanged(newDisplayName) { - this._displayName = newDisplayName ? newDisplayName : this._originalDisplayName; + this._device.displayName = newDisplayName + ? newDisplayName + : this._device.originalDisplayName; + for (const peerId in this.peers) { this._notifyPeerDisplayNameChanged(peerId); } @@ -1719,23 +1743,35 @@ class PeersManager { _notifyPeerDisplayNameChanged(peerId) { const peer = this.peers[peerId]; if (!peer) return; - this.peers[peerId]._sendDisplayName(this._displayName); + this.peers[peerId]._sendDisplayName(this._device.displayName); } _onDisplayName(displayName) { - this._originalDisplayName = displayName; + this._device.originalDisplayName = displayName; // if the displayName has not been changed (yet) set the displayName to the original displayName - if (!this._displayName) this._displayName = displayName; + if (!this._device.displayName) this._device.displayName = displayName; } _onAutoAcceptUpdated(roomSecret, autoAccept) { - const peerId = this._getPeerIdsFromRoomId(roomSecret)[0]; + let peerIds = this._getPeerIdsFromRoomId(roomSecret); + const peerId = this._removePeerIdsSameBrowser(peerIds)[0]; if (!peerId) return; this.peers[peerId]._setAutoAccept(autoAccept); } + _removePeerIdsSameBrowser(peerIds) { + let peerIdsNotSameBrowser = []; + for (let i = 0; i < peerIds.length; i++) { + const peer = this.peers[peerIds[i]]; + if (!peer._isSameBrowser()) { + peerIdsNotSameBrowser.push(peerIds[i]); + } + } + return peerIdsNotSameBrowser; + } + _getPeerIdsFromRoomId(roomId) { if (!roomId) return []; diff --git a/public/scripts/ui-main.js b/public/scripts/ui-main.js index 93cafb1..a43e727 100644 --- a/public/scripts/ui-main.js +++ b/public/scripts/ui-main.js @@ -205,7 +205,7 @@ class FooterUI { this.$displayName.addEventListener('blur', e => this._saveDisplayName(e.target.innerText)); Events.on('display-name', e => this._onDisplayName(e.detail.displayName)); - Events.on('self-display-name-changed', e => this._insertDisplayName(e.detail)); + Events.on('self-display-name-changed', e => this._insertDisplayName(e.detail.displayName)); // Load saved display name on page load Events.on('ws-connected', _ => this._loadSavedDisplayName()); @@ -239,7 +239,7 @@ class FooterUI { if (!displayName) return; Logger.debug("Retrieved edited display name:", displayName) - Events.fire('self-display-name-changed', displayName); + Events.fire('self-display-name-changed', { displayName: displayName }); } _onDisplayName(displayName){ @@ -280,8 +280,8 @@ class FooterUI { Events.fire('notify-user', Localization.getTranslation("notifications.display-name-changed-temporarily")); }) .finally(() => { - Events.fire('self-display-name-changed', newDisplayName); - Events.fire('broadcast-send', {type: 'self-display-name-changed', detail: newDisplayName}); + Events.fire('self-display-name-changed', { displayName: newDisplayName }); + Events.fire('broadcast-self-display-name-changed', { displayName: newDisplayName }); }); } else { @@ -292,8 +292,8 @@ class FooterUI { }) .finally(() => { Events.fire('notify-user', Localization.getTranslation("notifications.display-name-random-again")); - Events.fire('self-display-name-changed', ''); - Events.fire('broadcast-send', {type: 'self-display-name-changed', detail: ''}); + Events.fire('self-display-name-changed', { displayName: '' }); + Events.fire('broadcast-self-display-name-changed', { displayName: '' }); }); } }