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.

functions.ts 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. import _ from 'lodash';
  2. import { IStateful } from '../base/app/types';
  3. import { getCurrentConference } from '../base/conference/functions';
  4. import { getParticipantById, getParticipantCount, isLocalParticipantModerator } from '../base/participants/functions';
  5. import { toState } from '../base/redux/functions';
  6. import { FEATURE_KEY } from './constants';
  7. import { IRoom, IRooms } from './types';
  8. /**
  9. * Returns the rooms object for breakout rooms.
  10. *
  11. * @param {IStateful} stateful - The redux store, the redux
  12. * {@code getState} function, or the redux state itself.
  13. * @returns {Object} Object of rooms.
  14. */
  15. export const getBreakoutRooms = (stateful: IStateful): IRooms => toState(stateful)[FEATURE_KEY].rooms;
  16. /**
  17. * Returns the main room.
  18. *
  19. * @param {IStateful} stateful - The redux store, the redux
  20. * {@code getState} function, or the redux state itself.
  21. * @returns {IRoom|undefined} The main room object, or undefined.
  22. */
  23. export const getMainRoom = (stateful: IStateful) => {
  24. const rooms = getBreakoutRooms(stateful);
  25. return _.find(rooms, room => Boolean(room.isMainRoom));
  26. };
  27. export const getRoomsInfo = (stateful: IStateful) => {
  28. const breakoutRooms = getBreakoutRooms(stateful);
  29. const conference = getCurrentConference(stateful);
  30. const initialRoomsInfo = {
  31. rooms: []
  32. };
  33. // only main roomn
  34. if (!breakoutRooms || Object.keys(breakoutRooms).length === 0) {
  35. return {
  36. ...initialRoomsInfo,
  37. rooms: [ {
  38. isMainRoom: true,
  39. id: conference?.room?.roomjid,
  40. jid: conference?.room?.myroomjid,
  41. participants: conference?.participants && Object.keys(conference.participants).length
  42. ? Object.keys(conference.participants).map(participantId => {
  43. const participantItem = conference?.participants[participantId];
  44. const storeParticipant = getParticipantById(stateful, participantItem._id);
  45. return {
  46. jid: participantItem._jid,
  47. role: participantItem._role,
  48. displayName: participantItem._displayName,
  49. avatarUrl: storeParticipant?.loadableAvatarUrl,
  50. id: participantItem._id
  51. };
  52. }) : []
  53. } ]
  54. };
  55. }
  56. return {
  57. ...initialRoomsInfo,
  58. rooms: Object.keys(breakoutRooms).map(breakoutRoomKey => {
  59. const breakoutRoomItem = breakoutRooms[breakoutRoomKey];
  60. return {
  61. isMainRoom: Boolean(breakoutRoomItem.isMainRoom),
  62. id: breakoutRoomItem.id,
  63. jid: breakoutRoomItem.jid,
  64. participants: breakoutRoomItem.participants && Object.keys(breakoutRoomItem.participants).length
  65. ? Object.keys(breakoutRoomItem.participants).map(participantLongId => {
  66. const participantItem = breakoutRoomItem.participants[participantLongId];
  67. const ids = participantLongId.split('/');
  68. const storeParticipant = getParticipantById(stateful,
  69. ids.length > 1 ? ids[1] : participantItem.jid);
  70. return {
  71. jid: participantItem?.jid,
  72. role: participantItem?.role,
  73. displayName: participantItem?.displayName,
  74. avatarUrl: storeParticipant?.loadableAvatarUrl,
  75. id: storeParticipant ? storeParticipant.id
  76. : participantLongId
  77. };
  78. }) : []
  79. };
  80. })
  81. };
  82. };
  83. /**
  84. * Returns the room by Jid.
  85. *
  86. * @param {IStateful} stateful - The redux store, the redux
  87. * {@code getState} function, or the redux state itself.
  88. * @param {string} roomJid - The jid of the room.
  89. * @returns {IRoom|undefined} The main room object, or undefined.
  90. */
  91. export const getRoomByJid = (stateful: IStateful, roomJid: string) => {
  92. const rooms = getBreakoutRooms(stateful);
  93. return _.find(rooms, (room: IRoom) => room.jid === roomJid);
  94. };
  95. /**
  96. * Returns the id of the current room.
  97. *
  98. * @param {IStateful} stateful - The redux store, the redux
  99. * {@code getState} function, or the redux state itself.
  100. * @returns {string} Room id or undefined.
  101. */
  102. export const getCurrentRoomId = (stateful: IStateful) => {
  103. const conference = getCurrentConference(stateful);
  104. return conference?.getName();
  105. };
  106. /**
  107. * Determines whether the local participant is in a breakout room.
  108. *
  109. * @param {IStateful} stateful - The redux store, the redux
  110. * {@code getState} function, or the redux state itself.
  111. * @returns {boolean}
  112. */
  113. export const isInBreakoutRoom = (stateful: IStateful) => {
  114. const conference = getCurrentConference(stateful);
  115. return conference?.getBreakoutRooms()
  116. ?.isBreakoutRoom();
  117. };
  118. /**
  119. * Returns the breakout rooms config.
  120. *
  121. * @param {IStateful} stateful - The redux store, the redux
  122. * {@code getState} function, or the redux state itself.
  123. * @returns {Object}
  124. */
  125. export const getBreakoutRoomsConfig = (stateful: IStateful) => {
  126. const state = toState(stateful);
  127. const { breakoutRooms = {} } = state['features/base/config'];
  128. return breakoutRooms;
  129. };
  130. /**
  131. * Returns whether the add breakout room button is visible.
  132. *
  133. * @param {IStateful} stateful - Global state.
  134. * @returns {boolean}
  135. */
  136. export const isAddBreakoutRoomButtonVisible = (stateful: IStateful) => {
  137. const state = toState(stateful);
  138. const isLocalModerator = isLocalParticipantModerator(state);
  139. const { conference } = state['features/base/conference'];
  140. const isBreakoutRoomsSupported = conference?.getBreakoutRooms()?.isSupported();
  141. const { hideAddRoomButton } = getBreakoutRoomsConfig(state);
  142. return isLocalModerator && isBreakoutRoomsSupported && !hideAddRoomButton;
  143. };
  144. /**
  145. * Returns whether the auto assign participants to breakout rooms button is visible.
  146. *
  147. * @param {IStateful} stateful - Global state.
  148. * @returns {boolean}
  149. */
  150. export const isAutoAssignParticipantsVisible = (stateful: IStateful) => {
  151. const state = toState(stateful);
  152. const rooms = getBreakoutRooms(state);
  153. const inBreakoutRoom = isInBreakoutRoom(state);
  154. const isLocalModerator = isLocalParticipantModerator(state);
  155. const participantsCount = getParticipantCount(state);
  156. const { hideAutoAssignButton } = getBreakoutRoomsConfig(state);
  157. return !inBreakoutRoom
  158. && isLocalModerator
  159. && participantsCount > 2
  160. && Object.keys(rooms).length > 1
  161. && !hideAutoAssignButton;
  162. };