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 7.7KB

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