You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

actions.ts 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. import i18next from 'i18next';
  2. import _ from 'lodash';
  3. import { createBreakoutRoomsEvent } from '../analytics/AnalyticsEvents';
  4. import { sendAnalytics } from '../analytics/functions';
  5. import { IStore } from '../app/types';
  6. import {
  7. conferenceLeft,
  8. conferenceWillLeave,
  9. createConference
  10. } from '../base/conference/actions';
  11. import { CONFERENCE_LEAVE_REASONS } from '../base/conference/constants';
  12. import { getCurrentConference } from '../base/conference/functions';
  13. import { setAudioMuted, setVideoMuted } from '../base/media/actions';
  14. import { MEDIA_TYPE } from '../base/media/constants';
  15. import { getRemoteParticipants } from '../base/participants/functions';
  16. import { createDesiredLocalTracks } from '../base/tracks/actions';
  17. import {
  18. getLocalTracks,
  19. isLocalTrackMuted
  20. } from '../base/tracks/functions';
  21. import { clearNotifications, showNotification } from '../notifications/actions';
  22. import { NOTIFICATION_TIMEOUT_TYPE } from '../notifications/constants';
  23. import { _RESET_BREAKOUT_ROOMS, _UPDATE_ROOM_COUNTER } from './actionTypes';
  24. import { FEATURE_KEY } from './constants';
  25. import {
  26. getBreakoutRooms,
  27. getMainRoom,
  28. getRoomByJid
  29. } from './functions';
  30. import logger from './logger';
  31. /**
  32. * Action to create a breakout room.
  33. *
  34. * @param {string} name - Name / subject for the breakout room.
  35. * @returns {Function}
  36. */
  37. export function createBreakoutRoom(name?: string) {
  38. return (dispatch: IStore['dispatch'], getState: IStore['getState']) => {
  39. const state = getState();
  40. let { roomCounter } = state[FEATURE_KEY];
  41. const subject = name || i18next.t('breakoutRooms.defaultName', { index: ++roomCounter });
  42. sendAnalytics(createBreakoutRoomsEvent('create'));
  43. dispatch({
  44. type: _UPDATE_ROOM_COUNTER,
  45. roomCounter
  46. });
  47. getCurrentConference(state)?.getBreakoutRooms()
  48. ?.createBreakoutRoom(subject);
  49. };
  50. }
  51. /**
  52. * Action to close a room and send participants to the main room.
  53. *
  54. * @param {string} roomId - The id of the room to close.
  55. * @returns {Function}
  56. */
  57. export function closeBreakoutRoom(roomId: string) {
  58. return (dispatch: IStore['dispatch'], getState: IStore['getState']) => {
  59. const rooms = getBreakoutRooms(getState);
  60. const room = rooms[roomId];
  61. const mainRoom = getMainRoom(getState);
  62. sendAnalytics(createBreakoutRoomsEvent('close'));
  63. if (room && mainRoom) {
  64. Object.values(room.participants).forEach(p => {
  65. dispatch(sendParticipantToRoom(p.jid, mainRoom.id));
  66. });
  67. }
  68. };
  69. }
  70. /**
  71. * Action to remove a breakout room.
  72. *
  73. * @param {string} breakoutRoomJid - The jid of the breakout room to remove.
  74. * @returns {Function}
  75. */
  76. export function removeBreakoutRoom(breakoutRoomJid: string) {
  77. return (dispatch: IStore['dispatch'], getState: IStore['getState']) => {
  78. sendAnalytics(createBreakoutRoomsEvent('remove'));
  79. const room = getRoomByJid(getState, breakoutRoomJid);
  80. if (!room) {
  81. logger.error('The room to remove was not found.');
  82. return;
  83. }
  84. if (Object.keys(room.participants).length > 0) {
  85. dispatch(closeBreakoutRoom(room.id));
  86. }
  87. getCurrentConference(getState)?.getBreakoutRooms()
  88. ?.removeBreakoutRoom(breakoutRoomJid);
  89. };
  90. }
  91. /**
  92. * Action to auto-assign the participants to breakout rooms.
  93. *
  94. * @returns {Function}
  95. */
  96. export function autoAssignToBreakoutRooms() {
  97. return (dispatch: IStore['dispatch'], getState: IStore['getState']) => {
  98. const rooms = getBreakoutRooms(getState);
  99. const breakoutRooms = _.filter(rooms, room => !room.isMainRoom);
  100. if (breakoutRooms) {
  101. sendAnalytics(createBreakoutRoomsEvent('auto.assign'));
  102. const participantIds = Array.from(getRemoteParticipants(getState).keys());
  103. const length = Math.ceil(participantIds.length / breakoutRooms.length);
  104. _.chunk(_.shuffle(participantIds), length).forEach((group, index) =>
  105. group.forEach(participantId => {
  106. dispatch(sendParticipantToRoom(participantId, breakoutRooms[index].id));
  107. })
  108. );
  109. }
  110. };
  111. }
  112. /**
  113. * Action to send a participant to a room.
  114. *
  115. * @param {string} participantId - The participant id.
  116. * @param {string} roomId - The room id.
  117. * @returns {Function}
  118. */
  119. export function sendParticipantToRoom(participantId: string, roomId: string) {
  120. return (dispatch: IStore['dispatch'], getState: IStore['getState']) => {
  121. const rooms = getBreakoutRooms(getState);
  122. const room = rooms[roomId];
  123. if (!room) {
  124. logger.warn(`Invalid room: ${roomId}`);
  125. return;
  126. }
  127. // Get the full JID of the participant. We could be getting the endpoint ID or
  128. // a participant JID. We want to find the connection JID.
  129. const participantJid = _findParticipantJid(getState, participantId);
  130. if (!participantJid) {
  131. logger.warn(`Could not find participant ${participantId}`);
  132. return;
  133. }
  134. getCurrentConference(getState)?.getBreakoutRooms()
  135. ?.sendParticipantToRoom(participantJid, room.jid);
  136. };
  137. }
  138. /**
  139. * Action to move to a room.
  140. *
  141. * @param {string} roomId - The room id to move to. If omitted move to the main room.
  142. * @returns {Function}
  143. */
  144. export function moveToRoom(roomId?: string) {
  145. return async (dispatch: IStore['dispatch'], getState: IStore['getState']) => {
  146. const mainRoomId = getMainRoom(getState)?.id;
  147. let _roomId: string | undefined | String = roomId || mainRoomId;
  148. // Check if we got a full JID.
  149. if (_roomId && _roomId?.indexOf('@') !== -1) {
  150. const [ id, ...domainParts ] = _roomId.split('@');
  151. // On mobile we first store the room and the connection is created
  152. // later, so let's attach the domain to the room String object as
  153. // a little hack.
  154. // eslint-disable-next-line no-new-wrappers
  155. _roomId = new String(id);
  156. // @ts-ignore
  157. _roomId.domain = domainParts.join('@');
  158. }
  159. const roomIdStr = _roomId?.toString();
  160. const goToMainRoom = roomIdStr === mainRoomId;
  161. const rooms = getBreakoutRooms(getState);
  162. const targetRoom = rooms[roomIdStr ?? ''];
  163. if (!targetRoom) {
  164. logger.warn(`Unknown room: ${targetRoom}`);
  165. return;
  166. }
  167. dispatch({
  168. type: _RESET_BREAKOUT_ROOMS
  169. });
  170. if (navigator.product === 'ReactNative') {
  171. const conference = getCurrentConference(getState);
  172. const { audio, video } = getState()['features/base/media'];
  173. dispatch(conferenceWillLeave(conference));
  174. try {
  175. await conference.leave(CONFERENCE_LEAVE_REASONS.SWITCH_ROOM);
  176. } catch (error) {
  177. logger.warn('JitsiConference.leave() rejected with:', error);
  178. dispatch(conferenceLeft(conference));
  179. }
  180. dispatch(clearNotifications());
  181. dispatch(createConference(_roomId));
  182. dispatch(setAudioMuted(audio.muted));
  183. dispatch(setVideoMuted(Boolean(video.muted)));
  184. dispatch(createDesiredLocalTracks());
  185. } else {
  186. const localTracks = getLocalTracks(getState()['features/base/tracks']);
  187. const isAudioMuted = isLocalTrackMuted(localTracks, MEDIA_TYPE.AUDIO);
  188. const isVideoMuted = isLocalTrackMuted(localTracks, MEDIA_TYPE.VIDEO);
  189. try {
  190. // all places we fire notifyConferenceLeft we pass the room name from APP.conference
  191. await APP.conference.leaveRoom(false /* doDisconnect */, CONFERENCE_LEAVE_REASONS.SWITCH_ROOM).then(
  192. () => APP.API.notifyConferenceLeft(APP.conference.roomName));
  193. } catch (error) {
  194. logger.warn('APP.conference.leaveRoom() rejected with:', error);
  195. // TODO: revisit why we don't dispatch CONFERENCE_LEFT here.
  196. }
  197. APP.conference.joinRoom(_roomId, {
  198. startWithAudioMuted: isAudioMuted,
  199. startWithVideoMuted: isVideoMuted
  200. });
  201. }
  202. if (goToMainRoom) {
  203. dispatch(showNotification({
  204. titleKey: 'breakoutRooms.notifications.joinedTitle',
  205. descriptionKey: 'breakoutRooms.notifications.joinedMainRoom',
  206. concatText: true,
  207. maxLines: 2
  208. }, NOTIFICATION_TIMEOUT_TYPE.MEDIUM));
  209. } else {
  210. dispatch(showNotification({
  211. titleKey: 'breakoutRooms.notifications.joinedTitle',
  212. descriptionKey: 'breakoutRooms.notifications.joined',
  213. descriptionArguments: { name: targetRoom.name },
  214. concatText: true,
  215. maxLines: 2
  216. }, NOTIFICATION_TIMEOUT_TYPE.MEDIUM));
  217. }
  218. };
  219. }
  220. /**
  221. * Finds a participant's connection JID given its ID.
  222. *
  223. * @param {Function} getState - The redux store state getter.
  224. * @param {string} participantId - ID of the given participant.
  225. * @returns {string|undefined} - The participant connection JID if found.
  226. */
  227. function _findParticipantJid(getState: Function, participantId: string) {
  228. const conference = getCurrentConference(getState);
  229. if (!conference) {
  230. return;
  231. }
  232. // Get the full JID of the participant. We could be getting the endpoint ID or
  233. // a participant JID. We want to find the connection JID.
  234. let _participantId = participantId;
  235. let participantJid;
  236. if (!participantId.includes('@')) {
  237. const p = conference.getParticipantById(participantId);
  238. _participantId = p?.getJid(); // This will be the room JID.
  239. }
  240. if (_participantId) {
  241. const rooms = getBreakoutRooms(getState);
  242. for (const room of Object.values(rooms)) {
  243. const participants = room.participants || {};
  244. const p = participants[_participantId]
  245. || Object.values(participants).find(item => item.jid === _participantId);
  246. if (p) {
  247. participantJid = p.jid;
  248. break;
  249. }
  250. }
  251. }
  252. return participantJid;
  253. }