2021-09-14 15:31:30 +00:00
|
|
|
// @flow
|
|
|
|
|
|
|
|
import i18next from 'i18next';
|
|
|
|
import _ from 'lodash';
|
|
|
|
import type { Dispatch } from 'redux';
|
|
|
|
|
2021-11-23 13:14:59 +00:00
|
|
|
import { createBreakoutRoomsEvent, sendAnalytics } from '../analytics';
|
2021-09-14 15:31:30 +00:00
|
|
|
import {
|
|
|
|
conferenceLeft,
|
|
|
|
conferenceWillLeave,
|
|
|
|
createConference,
|
|
|
|
getCurrentConference
|
|
|
|
} from '../base/conference';
|
2021-12-10 13:40:41 +00:00
|
|
|
import {
|
|
|
|
MEDIA_TYPE,
|
|
|
|
setAudioMuted,
|
|
|
|
setVideoMuted
|
|
|
|
} from '../base/media';
|
2021-09-14 15:31:30 +00:00
|
|
|
import { getRemoteParticipants } from '../base/participants';
|
2021-12-10 13:40:41 +00:00
|
|
|
import {
|
|
|
|
getLocalTracks,
|
|
|
|
isLocalCameraTrackMuted,
|
|
|
|
isLocalTrackMuted
|
|
|
|
} from '../base/tracks';
|
2021-12-10 16:23:27 +00:00
|
|
|
import { createDesiredLocalTracks } from '../base/tracks/actions';
|
2021-12-03 08:48:26 +00:00
|
|
|
import {
|
|
|
|
NOTIFICATION_TIMEOUT_TYPE,
|
|
|
|
clearNotifications,
|
|
|
|
showNotification
|
|
|
|
} from '../notifications';
|
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,
|
|
|
|
getMainRoom
|
|
|
|
} from './functions';
|
|
|
|
import logger from './logger';
|
|
|
|
|
|
|
|
declare var APP: Object;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Action to create a breakout room.
|
|
|
|
*
|
|
|
|
* @param {string} name - Name / subject for the breakout room.
|
|
|
|
* @returns {Function}
|
|
|
|
*/
|
|
|
|
export function createBreakoutRoom(name?: string) {
|
|
|
|
return (dispatch: Dispatch<any>, getState: Function) => {
|
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
|
|
|
|
});
|
|
|
|
|
2021-09-14 15:31:30 +00:00
|
|
|
// $FlowExpectedError
|
2021-11-25 11:43:42 +00:00
|
|
|
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) {
|
|
|
|
return (dispatch: Dispatch<any>, getState: Function) => {
|
|
|
|
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 => {
|
|
|
|
|
|
|
|
// $FlowExpectedError
|
|
|
|
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) {
|
|
|
|
return (dispatch: Dispatch<any>, getState: Function) => {
|
2021-11-23 13:14:59 +00:00
|
|
|
sendAnalytics(createBreakoutRoomsEvent('remove'));
|
|
|
|
|
2021-09-14 15:31:30 +00:00
|
|
|
// $FlowExpectedError
|
|
|
|
getCurrentConference(getState)?.getBreakoutRooms()
|
|
|
|
?.removeBreakoutRoom(breakoutRoomJid);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Action to auto-assign the participants to breakout rooms.
|
|
|
|
*
|
|
|
|
* @returns {Function}
|
|
|
|
*/
|
|
|
|
export function autoAssignToBreakoutRooms() {
|
|
|
|
return (dispatch: Dispatch<any>, getState: Function) => {
|
|
|
|
const rooms = getBreakoutRooms(getState);
|
|
|
|
const breakoutRooms = _.filter(rooms, (room: Object) => !room.isMainRoom);
|
|
|
|
|
|
|
|
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) {
|
|
|
|
return (dispatch: Dispatch<any>, getState: Function) => {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
// $FlowExpectedError
|
|
|
|
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) {
|
2021-12-03 08:48:26 +00:00
|
|
|
return async (dispatch: Dispatch<any>, getState: Function) => {
|
|
|
|
const mainRoomId = getMainRoom(getState)?.id;
|
|
|
|
let _roomId = roomId || mainRoomId;
|
2021-09-14 15:31:30 +00:00
|
|
|
|
|
|
|
// Check if we got a full JID.
|
|
|
|
// $FlowExpectedError
|
|
|
|
if (_roomId?.indexOf('@') !== -1) {
|
|
|
|
// $FlowExpectedError
|
|
|
|
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);
|
|
|
|
|
|
|
|
// $FlowExpectedError
|
|
|
|
_roomId.domain = domainParts.join('@');
|
|
|
|
}
|
|
|
|
|
2021-12-03 08:48:26 +00:00
|
|
|
// $FlowExpectedError
|
|
|
|
const roomIdStr = _roomId?.toString();
|
|
|
|
const goToMainRoom = roomIdStr === mainRoomId;
|
|
|
|
const rooms = getBreakoutRooms(getState);
|
|
|
|
const targetRoom = rooms[roomIdStr];
|
|
|
|
|
|
|
|
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 {
|
|
|
|
await conference.leave();
|
|
|
|
} 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));
|
|
|
|
dispatch(createConference(_roomId));
|
|
|
|
dispatch(setAudioMuted(audio.muted));
|
|
|
|
dispatch(setVideoMuted(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
|
|
|
|
await APP.conference.leaveRoom(false /* doDisconnect */).then(
|
|
|
|
() => 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);
|
|
|
|
|
|
|
|
// $FlowExpectedError
|
|
|
|
_participantId = p?.getJid(); // This will be the room JID.
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_participantId) {
|
|
|
|
const rooms = getBreakoutRooms(getState);
|
|
|
|
|
|
|
|
for (const room of Object.values(rooms)) {
|
|
|
|
// $FlowExpectedError
|
|
|
|
const participants = room.participants || {};
|
|
|
|
const p = participants[_participantId]
|
|
|
|
|
|
|
|
// $FlowExpectedError
|
|
|
|
|| Object.values(participants).find(item => item.jid === _participantId);
|
|
|
|
|
|
|
|
if (p) {
|
|
|
|
participantJid = p.jid;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return participantJid;
|
|
|
|
}
|