2021-09-14 15:31:30 +00:00
|
|
|
import i18next from 'i18next';
|
|
|
|
import _ from 'lodash';
|
|
|
|
|
2022-09-14 11:32:58 +00:00
|
|
|
import { createBreakoutRoomsEvent } from '../analytics/AnalyticsEvents';
|
|
|
|
import { sendAnalytics } from '../analytics/functions';
|
|
|
|
import { IStore } from '../app/types';
|
2021-09-14 15:31:30 +00:00
|
|
|
import {
|
|
|
|
conferenceLeft,
|
|
|
|
conferenceWillLeave,
|
2022-10-11 10:47:54 +00:00
|
|
|
createConference
|
|
|
|
} from '../base/conference/actions';
|
2022-09-14 11:32:58 +00:00
|
|
|
import { CONFERENCE_LEAVE_REASONS } from '../base/conference/constants';
|
2022-10-11 10:47:54 +00:00
|
|
|
import { getCurrentConference } from '../base/conference/functions';
|
|
|
|
import { setAudioMuted, setVideoMuted } from '../base/media/actions';
|
2022-09-14 11:32:58 +00:00
|
|
|
import { MEDIA_TYPE } from '../base/media/constants';
|
|
|
|
import { getRemoteParticipants } from '../base/participants/functions';
|
2022-10-19 08:42:54 +00:00
|
|
|
import { createDesiredLocalTracks } from '../base/tracks/actions';
|
2021-12-10 13:40:41 +00:00
|
|
|
import {
|
|
|
|
getLocalTracks,
|
|
|
|
isLocalCameraTrackMuted,
|
|
|
|
isLocalTrackMuted
|
2022-10-19 08:42:54 +00:00
|
|
|
} from '../base/tracks/functions';
|
2022-10-17 11:28:01 +00:00
|
|
|
import { clearNotifications, showNotification } from '../notifications/actions';
|
2022-09-14 11:32:58 +00:00
|
|
|
import { NOTIFICATION_TIMEOUT_TYPE } from '../notifications/constants';
|
2021-09-14 15:31:30 +00:00
|
|
|
|
2021-11-25 13:48:34 +00:00
|
|
|
import { _RESET_BREAKOUT_ROOMS, _UPDATE_ROOM_COUNTER } from './actionTypes';
|
2021-11-25 11:43:42 +00:00
|
|
|
import { FEATURE_KEY } from './constants';
|
2021-09-14 15:31:30 +00:00
|
|
|
import {
|
|
|
|
getBreakoutRooms,
|
2022-02-14 10:40:44 +00:00
|
|
|
getMainRoom,
|
|
|
|
getRoomByJid
|
2021-09-14 15:31:30 +00:00
|
|
|
} from './functions';
|
|
|
|
import logger from './logger';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Action to create a breakout room.
|
|
|
|
*
|
|
|
|
* @param {string} name - Name / subject for the breakout room.
|
|
|
|
* @returns {Function}
|
|
|
|
*/
|
|
|
|
export function createBreakoutRoom(name?: string) {
|
2022-09-14 11:32:58 +00:00
|
|
|
return (dispatch: IStore['dispatch'], getState: IStore['getState']) => {
|
2021-11-25 11:43:42 +00:00
|
|
|
const state = getState();
|
|
|
|
let { roomCounter } = state[FEATURE_KEY];
|
|
|
|
const subject = name || i18next.t('breakoutRooms.defaultName', { index: ++roomCounter });
|
2021-09-14 15:31:30 +00:00
|
|
|
|
2021-11-23 13:14:59 +00:00
|
|
|
sendAnalytics(createBreakoutRoomsEvent('create'));
|
|
|
|
|
2021-11-25 11:43:42 +00:00
|
|
|
dispatch({
|
|
|
|
type: _UPDATE_ROOM_COUNTER,
|
|
|
|
roomCounter
|
|
|
|
});
|
|
|
|
|
|
|
|
getCurrentConference(state)?.getBreakoutRooms()
|
2021-09-14 15:31:30 +00:00
|
|
|
?.createBreakoutRoom(subject);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Action to close a room and send participants to the main room.
|
|
|
|
*
|
|
|
|
* @param {string} roomId - The id of the room to close.
|
|
|
|
* @returns {Function}
|
|
|
|
*/
|
|
|
|
export function closeBreakoutRoom(roomId: string) {
|
2022-09-14 11:32:58 +00:00
|
|
|
return (dispatch: IStore['dispatch'], getState: IStore['getState']) => {
|
2021-09-14 15:31:30 +00:00
|
|
|
const rooms = getBreakoutRooms(getState);
|
|
|
|
const room = rooms[roomId];
|
|
|
|
const mainRoom = getMainRoom(getState);
|
|
|
|
|
2021-11-23 13:14:59 +00:00
|
|
|
sendAnalytics(createBreakoutRoomsEvent('close'));
|
|
|
|
|
2021-09-14 15:31:30 +00:00
|
|
|
if (room && mainRoom) {
|
|
|
|
Object.values(room.participants).forEach(p => {
|
|
|
|
dispatch(sendParticipantToRoom(p.jid, mainRoom.id));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Action to remove a breakout room.
|
|
|
|
*
|
|
|
|
* @param {string} breakoutRoomJid - The jid of the breakout room to remove.
|
|
|
|
* @returns {Function}
|
|
|
|
*/
|
|
|
|
export function removeBreakoutRoom(breakoutRoomJid: string) {
|
2022-09-14 11:32:58 +00:00
|
|
|
return (dispatch: IStore['dispatch'], getState: IStore['getState']) => {
|
2021-11-23 13:14:59 +00:00
|
|
|
sendAnalytics(createBreakoutRoomsEvent('remove'));
|
2022-02-14 10:40:44 +00:00
|
|
|
const room = getRoomByJid(getState, breakoutRoomJid);
|
|
|
|
|
|
|
|
if (!room) {
|
|
|
|
logger.error('The room to remove was not found.');
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Object.keys(room.participants).length > 0) {
|
|
|
|
dispatch(closeBreakoutRoom(room.id));
|
|
|
|
}
|
2021-09-14 15:31:30 +00:00
|
|
|
getCurrentConference(getState)?.getBreakoutRooms()
|
|
|
|
?.removeBreakoutRoom(breakoutRoomJid);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Action to auto-assign the participants to breakout rooms.
|
|
|
|
*
|
|
|
|
* @returns {Function}
|
|
|
|
*/
|
|
|
|
export function autoAssignToBreakoutRooms() {
|
2022-09-14 11:32:58 +00:00
|
|
|
return (dispatch: IStore['dispatch'], getState: IStore['getState']) => {
|
2021-09-14 15:31:30 +00:00
|
|
|
const rooms = getBreakoutRooms(getState);
|
2022-09-14 11:32:58 +00:00
|
|
|
const breakoutRooms = _.filter(rooms, room => !room.isMainRoom);
|
2021-09-14 15:31:30 +00:00
|
|
|
|
|
|
|
if (breakoutRooms) {
|
2021-11-23 13:14:59 +00:00
|
|
|
sendAnalytics(createBreakoutRoomsEvent('auto.assign'));
|
2021-09-14 15:31:30 +00:00
|
|
|
const participantIds = Array.from(getRemoteParticipants(getState).keys());
|
|
|
|
const length = Math.ceil(participantIds.length / breakoutRooms.length);
|
|
|
|
|
|
|
|
_.chunk(_.shuffle(participantIds), length).forEach((group, index) =>
|
|
|
|
group.forEach(participantId => {
|
|
|
|
dispatch(sendParticipantToRoom(participantId, breakoutRooms[index].id));
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Action to send a participant to a room.
|
|
|
|
*
|
|
|
|
* @param {string} participantId - The participant id.
|
|
|
|
* @param {string} roomId - The room id.
|
|
|
|
* @returns {Function}
|
|
|
|
*/
|
|
|
|
export function sendParticipantToRoom(participantId: string, roomId: string) {
|
2022-09-14 11:32:58 +00:00
|
|
|
return (dispatch: IStore['dispatch'], getState: IStore['getState']) => {
|
2021-09-14 15:31:30 +00:00
|
|
|
const rooms = getBreakoutRooms(getState);
|
|
|
|
const room = rooms[roomId];
|
|
|
|
|
|
|
|
if (!room) {
|
|
|
|
logger.warn(`Invalid room: ${roomId}`);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the full JID of the participant. We could be getting the endpoint ID or
|
|
|
|
// a participant JID. We want to find the connection JID.
|
|
|
|
const participantJid = _findParticipantJid(getState, participantId);
|
|
|
|
|
|
|
|
if (!participantJid) {
|
|
|
|
logger.warn(`Could not find participant ${participantId}`);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
getCurrentConference(getState)?.getBreakoutRooms()
|
|
|
|
?.sendParticipantToRoom(participantJid, room.jid);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Action to move to a room.
|
|
|
|
*
|
|
|
|
* @param {string} roomId - The room id to move to. If omitted move to the main room.
|
|
|
|
* @returns {Function}
|
|
|
|
*/
|
|
|
|
export function moveToRoom(roomId?: string) {
|
2022-09-14 11:32:58 +00:00
|
|
|
return async (dispatch: IStore['dispatch'], getState: IStore['getState']) => {
|
2021-12-03 08:48:26 +00:00
|
|
|
const mainRoomId = getMainRoom(getState)?.id;
|
2022-09-14 11:32:58 +00:00
|
|
|
let _roomId: string | undefined | String = roomId || mainRoomId;
|
2021-09-14 15:31:30 +00:00
|
|
|
|
|
|
|
// Check if we got a full JID.
|
2022-09-14 11:32:58 +00:00
|
|
|
if (_roomId && _roomId?.indexOf('@') !== -1) {
|
2021-09-14 15:31:30 +00:00
|
|
|
const [ id, ...domainParts ] = _roomId.split('@');
|
|
|
|
|
|
|
|
// On mobile we first store the room and the connection is created
|
|
|
|
// later, so let's attach the domain to the room String object as
|
|
|
|
// a little hack.
|
|
|
|
|
|
|
|
// eslint-disable-next-line no-new-wrappers
|
|
|
|
_roomId = new String(id);
|
2022-10-19 08:42:54 +00:00
|
|
|
|
2022-09-14 11:32:58 +00:00
|
|
|
// @ts-ignore
|
2021-09-14 15:31:30 +00:00
|
|
|
_roomId.domain = domainParts.join('@');
|
|
|
|
}
|
|
|
|
|
2021-12-03 08:48:26 +00:00
|
|
|
const roomIdStr = _roomId?.toString();
|
|
|
|
const goToMainRoom = roomIdStr === mainRoomId;
|
|
|
|
const rooms = getBreakoutRooms(getState);
|
2022-09-14 11:32:58 +00:00
|
|
|
const targetRoom = rooms[roomIdStr ?? ''];
|
2021-12-03 08:48:26 +00:00
|
|
|
|
|
|
|
if (!targetRoom) {
|
|
|
|
logger.warn(`Unknown room: ${targetRoom}`);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-11-25 13:48:34 +00:00
|
|
|
dispatch({
|
|
|
|
type: _RESET_BREAKOUT_ROOMS
|
|
|
|
});
|
|
|
|
|
2021-09-14 15:31:30 +00:00
|
|
|
if (navigator.product === 'ReactNative') {
|
|
|
|
const conference = getCurrentConference(getState);
|
|
|
|
const { audio, video } = getState()['features/base/media'];
|
|
|
|
|
|
|
|
dispatch(conferenceWillLeave(conference));
|
|
|
|
|
2021-12-03 08:48:26 +00:00
|
|
|
try {
|
2022-08-26 09:53:32 +00:00
|
|
|
await conference.leave(CONFERENCE_LEAVE_REASONS.SWITCH_ROOM);
|
2021-12-03 08:48:26 +00:00
|
|
|
} catch (error) {
|
|
|
|
logger.warn('JitsiConference.leave() rejected with:', error);
|
2021-09-14 15:31:30 +00:00
|
|
|
|
2021-12-03 08:48:26 +00:00
|
|
|
dispatch(conferenceLeft(conference));
|
|
|
|
}
|
|
|
|
|
|
|
|
dispatch(clearNotifications());
|
|
|
|
|
|
|
|
// dispatch(setRoom(_roomId));
|
2022-10-17 11:28:01 +00:00
|
|
|
dispatch(createConference(_roomId?.toString()));
|
2021-12-03 08:48:26 +00:00
|
|
|
dispatch(setAudioMuted(audio.muted));
|
2022-10-11 10:47:54 +00:00
|
|
|
dispatch(setVideoMuted(Boolean(video.muted)));
|
2021-12-10 10:57:26 +00:00
|
|
|
dispatch(createDesiredLocalTracks());
|
2021-12-03 08:48:26 +00:00
|
|
|
} else {
|
2021-12-10 13:40:41 +00:00
|
|
|
const localTracks = getLocalTracks(getState()['features/base/tracks']);
|
|
|
|
const isAudioMuted = isLocalTrackMuted(localTracks, MEDIA_TYPE.AUDIO);
|
|
|
|
const isVideoMuted = isLocalCameraTrackMuted(localTracks);
|
|
|
|
|
2021-12-03 08:48:26 +00:00
|
|
|
try {
|
2021-12-15 21:50:03 +00:00
|
|
|
// all places we fire notifyConferenceLeft we pass the room name from APP.conference
|
2022-08-26 09:53:32 +00:00
|
|
|
await APP.conference.leaveRoom(false /* doDisconnect */, CONFERENCE_LEAVE_REASONS.SWITCH_ROOM).then(
|
2021-12-15 21:50:03 +00:00
|
|
|
() => APP.API.notifyConferenceLeft(APP.conference.roomName));
|
2021-12-03 08:48:26 +00:00
|
|
|
} catch (error) {
|
|
|
|
logger.warn('APP.conference.leaveRoom() rejected with:', error);
|
|
|
|
|
|
|
|
// TODO: revisit why we don't dispatch CONFERENCE_LEFT here.
|
|
|
|
}
|
|
|
|
|
2021-12-10 13:40:41 +00:00
|
|
|
APP.conference.joinRoom(_roomId, {
|
|
|
|
startWithAudioMuted: isAudioMuted,
|
|
|
|
startWithVideoMuted: isVideoMuted
|
|
|
|
});
|
2021-12-03 08:48:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (goToMainRoom) {
|
|
|
|
dispatch(showNotification({
|
|
|
|
titleKey: 'breakoutRooms.notifications.joinedTitle',
|
|
|
|
descriptionKey: 'breakoutRooms.notifications.joinedMainRoom',
|
|
|
|
concatText: true,
|
|
|
|
maxLines: 2
|
|
|
|
}, NOTIFICATION_TIMEOUT_TYPE.MEDIUM));
|
2021-09-14 15:31:30 +00:00
|
|
|
} else {
|
2021-12-03 08:48:26 +00:00
|
|
|
dispatch(showNotification({
|
|
|
|
titleKey: 'breakoutRooms.notifications.joinedTitle',
|
|
|
|
descriptionKey: 'breakoutRooms.notifications.joined',
|
|
|
|
descriptionArguments: { name: targetRoom.name },
|
|
|
|
concatText: true,
|
|
|
|
maxLines: 2
|
|
|
|
}, NOTIFICATION_TIMEOUT_TYPE.MEDIUM));
|
2021-09-14 15:31:30 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Finds a participant's connection JID given its ID.
|
|
|
|
*
|
|
|
|
* @param {Function} getState - The redux store state getter.
|
|
|
|
* @param {string} participantId - ID of the given participant.
|
|
|
|
* @returns {string|undefined} - The participant connection JID if found.
|
|
|
|
*/
|
|
|
|
function _findParticipantJid(getState: Function, participantId: string) {
|
|
|
|
const conference = getCurrentConference(getState);
|
|
|
|
|
|
|
|
if (!conference) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the full JID of the participant. We could be getting the endpoint ID or
|
|
|
|
// a participant JID. We want to find the connection JID.
|
|
|
|
let _participantId = participantId;
|
|
|
|
let participantJid;
|
|
|
|
|
|
|
|
if (!participantId.includes('@')) {
|
|
|
|
const p = conference.getParticipantById(participantId);
|
|
|
|
|
|
|
|
_participantId = p?.getJid(); // This will be the room JID.
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_participantId) {
|
|
|
|
const rooms = getBreakoutRooms(getState);
|
|
|
|
|
|
|
|
for (const room of Object.values(rooms)) {
|
|
|
|
const participants = room.participants || {};
|
|
|
|
const p = participants[_participantId]
|
|
|
|
|| Object.values(participants).find(item => item.jid === _participantId);
|
|
|
|
|
|
|
|
if (p) {
|
|
|
|
participantJid = p.jid;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return participantJid;
|
|
|
|
}
|