|
| 1 | +/** |
| 2 | + * |
| 3 | + * @copyright Copyright (c) 2023, Daniel Calviño Sánchez ([email protected]) |
| 4 | + * |
| 5 | + * @license AGPL-3.0-or-later |
| 6 | + * |
| 7 | + * This program is free software: you can redistribute it and/or modify |
| 8 | + * it under the terms of the GNU Affero General Public License as |
| 9 | + * published by the Free Software Foundation, either version 3 of the |
| 10 | + * License, or (at your option) any later version. |
| 11 | + * |
| 12 | + * This program is distributed in the hope that it will be useful, |
| 13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | + * GNU Affero General Public License for more details. |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU Affero General Public License |
| 18 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | + * |
| 20 | + */ |
| 21 | + |
| 22 | +import EmitterMixin from './EmitterMixin.js' |
| 23 | + |
| 24 | +/** |
| 25 | + * Helper to keep track of the signaling participants. |
| 26 | + * |
| 27 | + * The participants included the following properties: |
| 28 | + * - nextcloudSessionId |
| 29 | + * - signalingSessionId |
| 30 | + * - userId (optional, not included if the participant is a guest) |
| 31 | + * |
| 32 | + * The following events are emitted: |
| 33 | + * - participantsJoined(participants) |
| 34 | + * - participantsLeft(participants) |
| 35 | + */ |
| 36 | +export default function SignalingParticipantList() { |
| 37 | + this._superEmitterMixin() |
| 38 | + |
| 39 | + this._signaling = null |
| 40 | + this._participants = [] |
| 41 | + |
| 42 | + this._handleLeaveRoomBound = this._handleLeaveRoom.bind(this) |
| 43 | + this._handleUsersInRoomBound = this._handleUsersInRoom.bind(this) |
| 44 | + this._handleUsersJoinedBound = this._handleUsersJoined.bind(this) |
| 45 | + this._handleUsersLeftBound = this._handleUsersLeft.bind(this) |
| 46 | +} |
| 47 | + |
| 48 | +SignalingParticipantList.prototype = { |
| 49 | + |
| 50 | + destroy() { |
| 51 | + if (this._signaling) { |
| 52 | + this._signaling.off('leaveRoom', this._handleLeaveRoomBound) |
| 53 | + this._signaling.off('usersInRoom', this._handleUsersInRoomBound) |
| 54 | + this._signaling.off('usersJoined', this._handleUsersJoinedBound) |
| 55 | + this._signaling.off('usersLeft', this._handleUsersLeftBound) |
| 56 | + } |
| 57 | + |
| 58 | + this._destroyed = true |
| 59 | + |
| 60 | + this._participants = [] |
| 61 | + }, |
| 62 | + |
| 63 | + setSignaling(signaling) { |
| 64 | + if (this._destroyed) { |
| 65 | + return |
| 66 | + } |
| 67 | + |
| 68 | + if (this._signaling) { |
| 69 | + this._signaling.off('leaveRoom', this._handleLeaveRoomBound) |
| 70 | + this._signaling.off('usersInRoom', this._handleUsersInRoomBound) |
| 71 | + this._signaling.off('usersJoined', this._handleUsersJoinedBound) |
| 72 | + this._signaling.off('usersLeft', this._handleUsersLeftBound) |
| 73 | + } |
| 74 | + |
| 75 | + this._signaling = signaling |
| 76 | + |
| 77 | + if (this._signaling) { |
| 78 | + this._signaling.on('leaveRoom', this._handleLeaveRoomBound) |
| 79 | + this._signaling.on('usersInRoom', this._handleUsersInRoomBound) |
| 80 | + this._signaling.on('usersJoined', this._handleUsersJoinedBound) |
| 81 | + this._signaling.on('usersLeft', this._handleUsersLeftBound) |
| 82 | + } |
| 83 | + }, |
| 84 | + |
| 85 | + getParticipants() { |
| 86 | + return this._participants |
| 87 | + }, |
| 88 | + |
| 89 | + _handleLeaveRoom(token) { |
| 90 | + if (this._participants.length > 0) { |
| 91 | + this._trigger('participantsLeft', [this._participants]) |
| 92 | + } |
| 93 | + |
| 94 | + this._participants = [] |
| 95 | + }, |
| 96 | + |
| 97 | + _handleUsersInRoom(users) { |
| 98 | + const participants = [] |
| 99 | + const participantsJoined = [] |
| 100 | + const participantsLeft = [] |
| 101 | + |
| 102 | + for (const user of users) { |
| 103 | + const participant = { |
| 104 | + nextcloudSessionId: user.sessionId, |
| 105 | + signalingSessionId: user.sessionId, |
| 106 | + } |
| 107 | + if (user.userId) { |
| 108 | + participant.userId = user.userId |
| 109 | + } |
| 110 | + |
| 111 | + participants.push(participant) |
| 112 | + |
| 113 | + if (!this._participants.find(oldParticipant => oldParticipant.signalingSessionId === participant.signalingSessionId)) { |
| 114 | + participantsJoined.push(participant) |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + for (const oldParticipant of this._participants) { |
| 119 | + if (!participants.find(participant => participant.signalingSessionId === oldParticipant.signalingSessionId)) { |
| 120 | + participantsLeft.push(oldParticipant) |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + this._participants = participants |
| 125 | + |
| 126 | + if (participantsJoined.length > 0) { |
| 127 | + this._trigger('participantsJoined', [participantsJoined]) |
| 128 | + } |
| 129 | + if (participantsLeft.length > 0) { |
| 130 | + this._trigger('participantsLeft', [participantsLeft]) |
| 131 | + } |
| 132 | + }, |
| 133 | + |
| 134 | + _handleUsersJoined(users) { |
| 135 | + const participantsJoined = [] |
| 136 | + |
| 137 | + for (const user of users) { |
| 138 | + const participant = { |
| 139 | + nextcloudSessionId: user.roomsessionid, |
| 140 | + signalingSessionId: user.sessionid, |
| 141 | + } |
| 142 | + if (user.userid) { |
| 143 | + participant.userId = user.userid |
| 144 | + } |
| 145 | + |
| 146 | + this._participants.push(participant) |
| 147 | + |
| 148 | + participantsJoined.push(participant) |
| 149 | + } |
| 150 | + |
| 151 | + if (participantsJoined.length > 0) { |
| 152 | + this._trigger('participantsJoined', [participantsJoined]) |
| 153 | + } |
| 154 | + }, |
| 155 | + |
| 156 | + _handleUsersLeft(sessionIds) { |
| 157 | + const participantsLeft = [] |
| 158 | + |
| 159 | + for (const sessionId of sessionIds) { |
| 160 | + const index = this._participants.findIndex(participant => participant.signalingSessionId === sessionId) |
| 161 | + if (index >= 0) { |
| 162 | + participantsLeft.push(this._participants[index]) |
| 163 | + |
| 164 | + this._participants.splice(index, 1) |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + if (participantsLeft.length > 0) { |
| 169 | + this._trigger('participantsLeft', [participantsLeft]) |
| 170 | + } |
| 171 | + }, |
| 172 | + |
| 173 | +} |
| 174 | + |
| 175 | +EmitterMixin.apply(SignalingParticipantList.prototype) |
0 commit comments