Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

functions.ts 7.6KB

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