Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Re 466 more updates #2022

Merged
merged 6 commits into from
Aug 29, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 65 additions & 46 deletions app/src/renderer/apps/Rooms/store/RoomsStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ export class RoomModel {
this.title = data.title;
this.whitelist = data.whitelist || [];
this.present = data.present || [];
// console.log('update: [present] %o', this.present.toString());
if (data.capacity) {
this.capacity = data.capacity;
}
Expand Down Expand Up @@ -143,9 +144,12 @@ export class RoomsStore extends EventsEmitter {
string,
RoomModel
>();
@observable tracking: Map<string, RoomType> = observable.map<
string,
RoomType
>();
@observable chat: RoomChat[] = [];
@observable currentRid: string | null = null;
@observable ourRooms: string[] = [];
@observable isMuted = false;
@observable isSpeaking = false;
@observable isAudioAttached = false;
Expand Down Expand Up @@ -246,6 +250,7 @@ export class RoomsStore extends EventsEmitter {
this.rooms.forEach((room) => {
this.leaveRoom(room.rid);
});
this.tracking.clear();
this.status = 'disconnected';
this.websocket.close();
}
Expand Down Expand Up @@ -292,6 +297,7 @@ export class RoomsStore extends EventsEmitter {
return [];
}
const room = this.rooms.get(this.currentRid);
// console.log('currentroomPresent: %o', room?.present);
return (
room?.present.slice().sort((a, b) => {
if (a === this.ourId) {
Expand Down Expand Up @@ -529,47 +535,48 @@ export class RoomsStore extends EventsEmitter {
// eslint-disable-next-line no-case-declarations
const newRoom = new RoomModel(event.room);
newRoom.update(event.room);
this.rooms.set(event.room.rid, newRoom);
if (event.room.rtype === RoomType.media) {
this.currentRid = event.room.rid;
}
break;
case 'room-entered':
{
console.log('room entered', event);
if (!this.ourRooms.includes(event.room.rid)) return;
const room = this.rooms.get(event.room.rid);
if (room) {
this.rooms.set(event.room.rid, new RoomModel(event.room));
// if (event.room.rid === this.currentRid) {
if (this.rooms.has(event.room.rid)) {
this.rooms.get(event.room.rid)?.update(event.room);
}
if (this.tracking.has(event.rid)) {
console.log('room-entered: %o', event);
// if we entered a room, we need to create a peer for each user in the room
if (event.peer_id === this.ourId) {
// console.log('we entered the room...');
const peers = event.room.present.filter(
(peer: string) => peer !== this.ourId
);
peers.forEach((peerId: string) => {
if (
room.rtype === RoomType.media &&
event.room.rtype === RoomType.media &&
!this.ourPeer.audioStream
) {
console.error('no local stream');
return;
}
const peer = this.peers.get(peerId);
if (peer) {
peer.rooms.set(room.rid, room);
peer.rooms.set(event.room.rid, new RoomModel(event.room));
console.log('already have peer', peerId);
return;
}
this.createPeer(room.rid, room.rtype, peerId);
this.createPeer(event.room.rid, event.room.rtype, peerId);
});
} else {
// someone else entered the room
console.log('someone entered the room', event);
this.createPeer(room.rid, room.rtype, event.peer_id);
console.log('%s entered the room', event.peer_id);
this.createPeer(event.room.rid, event.room.rtype, event.peer_id);

if (
shipStore.settingsStore.systemSoundsEnabled &&
room.rtype === RoomType.media
event.room.rtype === RoomType.media
) {
SoundActions.playRoomPeerEnter();
}
Expand All @@ -581,72 +588,74 @@ export class RoomsStore extends EventsEmitter {
{
// replace room in rooms array with event.room
console.log('room left', event);
const room = this.rooms.get(event.room.rid);
if (room) {
room.update(event.room);
// if (this.currentRid === event.room.rid) {
if (this.rooms.has(event.room.rid)) {
this.rooms.get(event.room.rid)?.update(event.room);
}
if (this.tracking.has(event.rid)) {
if (event.peer_id === this.ourId) {
if (room.rtype === RoomType.media) {
// console.log('we are leaving the room %o', [event.room.rtype]);
event.room.present.forEach((peerId: string) => {
if (peerId !== this.ourId) this.destroyPeer(event.rid, peerId);
});
if (event.room.rtype === RoomType.media) {
this.currentRid = null;
// this already happens in leave/delete Room calls. since we are using
// peer reference counts to determine when to hangup, this extra call here
// is causing a bug
// this.hangupAllPeers();
this.tracking.delete(event.room.rid);
// console.log('room-left: disabling all media...');
this.ourPeer.disableAll();
// this._removeRoom(event.rid);
}
} else {
if (
shipStore.settingsStore.systemSoundsEnabled &&
room.rtype === RoomType.media
event.room.rtype === RoomType.media
) {
SoundActions.playRoomPeerLeave();
}
console.log('someone left the room', event);
console.log('%o left the room', event.peer_id);
this.destroyPeer(event.rid, event.peer_id);
room.removePeer(event.peer_id);
this.rooms.get(event.rid)?.removePeer(event.peer_id);
}
}
}
break;
case 'room-deleted':
{
console.log('room-deleted: %o', event);
if (this.ourRooms.includes(event.rid)) {
this.ourRooms = this.ourRooms.filter(
(rid: string) => rid !== event.rid
);
}
const room = this.rooms.get(event.rid);
if (room) {
room.present.forEach((peerId: string) => {
const rtype = this.tracking.get(event.rid);
if (rtype) {
this.rooms.get(event.rid)?.present.forEach((peerId: string) => {
if (peerId !== this.ourId) this.destroyPeer(event.rid, peerId);
});
if (room.rtype === RoomType.media) {
if (rtype === RoomType.media) {
this.currentRid = null;
this.tracking.delete(event.rid);
// console.log('room-deleted: disabling all media...');
this.ourPeer.disableAll();
}
this._removeRoom(event.rid);
}
this._removeRoom(event.rid);
}
break;
case 'signal':
{
// console.log('signal: %o', event);
const room = this.rooms.get(event.rid);
if (room) {
if (room.rtype === RoomType.media && !this.ourPeer.audioStream) {
const rtype = this.tracking.get(event.rid);
if (!rtype) console.log('not found');
if (rtype) {
if (rtype === RoomType.media && !this.ourPeer.audioStream) {
console.error('no local stream');
return;
}
let peer =
this.peers.get(event.from) ||
this.createPeer(room.rid, room.rtype, event.from);
this.createPeer(event.rid, rtype, event.from);
if (peer?.peer.destroyed) {
console.log(
'peer was destroyed, but is attempting to reconnect',
event.from
);
this.destroyPeer(room.rid, event.from);
peer = this.createPeer(room.rid, room.rtype, event.from);
this.destroyPeer(event.rid, event.from);
peer = this.createPeer(event.rid, rtype, event.from);
return;
}
if (!peer) {
Expand All @@ -655,10 +664,11 @@ export class RoomsStore extends EventsEmitter {
}
peer.onReceivedSignal(event.signal);
}
// }
}
break;
case 'chat':
if (event.peer_id !== this.ourId) {
if (this.currentRid === event.rid && event.peer_id !== this.ourId) {
console.log('chat', event);
this.chat.push({
author: event.peer_id,
Expand All @@ -681,6 +691,7 @@ export class RoomsStore extends EventsEmitter {
) {
console.log('createRoom: %o', path);
if (rtype === RoomType.media && !this.ourPeer.audioStream) {
// console.log('enabling audio');
await this.ourPeer.enableAudio();
}

Expand All @@ -694,6 +705,7 @@ export class RoomsStore extends EventsEmitter {
path,
};
this.rooms.set(newRoom.rid, new RoomModel(newRoom));
this.tracking.set(newRoom.rid, rtype);
this.websocket.send(
serialize({
type: 'create-room',
Expand Down Expand Up @@ -731,12 +743,13 @@ export class RoomsStore extends EventsEmitter {
@action
deleteRoom(rid: string) {
console.log('deleteRoom: %o', rid);
this.ourRooms = this.ourRooms.filter((roomid) => roomid !== rid);
const room = this.rooms.get(rid);
if (room) {
this.rooms.delete(rid);
if (room.rtype === RoomType.media) {
this.currentRid = null;
this.tracking.delete(rid);
// console.log('deleteRoom: disabling all media...');
this.ourPeer.disableAll();
}
}
Expand All @@ -754,8 +767,10 @@ export class RoomsStore extends EventsEmitter {
cleanUpCurrentRoom() {
if (this.currentRoom) {
if (this.currentRoom.creator === this.ourId) {
// console.log('cleanupCurrentRoom: deleting room...');
this.deleteRoom(this.currentRoom.rid);
} else {
// console.log('cleanupCurrentRoom: leaving room...');
this.leaveRoom(this.currentRoom.rid);
}
}
Expand All @@ -765,14 +780,14 @@ export class RoomsStore extends EventsEmitter {
@action
async joinRoom(rid: string, rtype: RoomType = RoomType.media) {
console.log('joinRoom: %o', [rid]);
this.ourRooms.push(rid);

if (rtype === RoomType.media) {
this.cleanUpCurrentRoom();
this.setCurrentRoom(rid);
if (!this.ourPeer.audioStream) {
await this.ourPeer.enableAudio();
}
}
this.tracking.set(rid, rtype);
this.websocket.send(
serialize({
type: 'enter-room',
Expand All @@ -781,7 +796,8 @@ export class RoomsStore extends EventsEmitter {
);
const room = this.rooms.get(rid);
// add us to the room
if (room) {
if (room && !room.present.includes(this.ourId)) {
// console.log('adding peer %o', this.ourId);
room.addPeer(this.ourId);
}
}
Expand All @@ -797,6 +813,8 @@ export class RoomsStore extends EventsEmitter {
} else {
if (room.rtype === RoomType.media) {
this.currentRid = null;
this.tracking.delete(rid);
// console.log('leaveRoom: disabling media...');
this.ourPeer.disableAll();
}
}
Expand Down Expand Up @@ -838,6 +856,7 @@ export class RoomsStore extends EventsEmitter {
}
let peer = this.peers.get(peerId);
if (peer) {
console.error('peer found');
peer.rooms.set(room.rid, room);
return;
}
Expand Down