Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

BreakoutRooms.js 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. import { getLogger } from '@jitsi/logger';
  2. import { $msg, Strophe } from 'strophe.js';
  3. import { XMPPEvents } from '../../service/xmpp/XMPPEvents';
  4. const FEATURE_KEY = 'features/breakout-rooms';
  5. const BREAKOUT_ROOM_ACTIONS = {
  6. ADD: `${FEATURE_KEY}/add`,
  7. MOVE_TO_ROOM: `${FEATURE_KEY}/move-to-room`,
  8. REMOVE: `${FEATURE_KEY}/remove`,
  9. RENAME: `${FEATURE_KEY}/rename`
  10. };
  11. const BREAKOUT_ROOM_EVENTS = {
  12. MOVE_TO_ROOM: `${FEATURE_KEY}/move-to-room`,
  13. UPDATE: `${FEATURE_KEY}/update`
  14. };
  15. const logger = getLogger(__filename);
  16. /**
  17. * Helper class for handling breakout rooms.
  18. */
  19. export default class BreakoutRooms {
  20. /**
  21. * Constructs breakout room.
  22. *
  23. * @param {ChatRoom} room the room we are in.
  24. */
  25. constructor(room) {
  26. this.room = room;
  27. this._handleMessages = this._handleMessages.bind(this);
  28. this.room.xmpp.addListener(XMPPEvents.BREAKOUT_ROOMS_EVENT, this._handleMessages);
  29. this._rooms = {};
  30. }
  31. /**
  32. * Stops listening for events.
  33. */
  34. dispose() {
  35. this.room.xmpp.removeListener(XMPPEvents.BREAKOUT_ROOMS_EVENT, this._handleMessages);
  36. }
  37. /**
  38. * Creates a breakout room with the given subject.
  39. *
  40. * @param {string} subject - A subject for the breakout room.
  41. */
  42. createBreakoutRoom(subject) {
  43. if (!this.isSupported() || !this.room.isModerator()) {
  44. logger.error(`Cannot create breakout room - supported:${this.isSupported()},
  45. moderator:${this.room.isModerator()}`);
  46. return;
  47. }
  48. const message = {
  49. type: BREAKOUT_ROOM_ACTIONS.ADD,
  50. subject
  51. };
  52. this._sendMessage(message);
  53. }
  54. /**
  55. * Removes a breakout room.
  56. *
  57. * @param {string} breakoutRoomJid - JID of the room to be removed.
  58. */
  59. removeBreakoutRoom(breakoutRoomJid) {
  60. if (!this.isSupported() || !this.room.isModerator()) {
  61. logger.error(`Cannot remove breakout room - supported:${this.isSupported()},
  62. moderator:${this.room.isModerator()}`);
  63. return;
  64. }
  65. const message = {
  66. type: BREAKOUT_ROOM_ACTIONS.REMOVE,
  67. breakoutRoomJid
  68. };
  69. this._sendMessage(message);
  70. }
  71. /**
  72. * Changes the subject of a breakout room.
  73. *
  74. * @param {string} breakoutRoomJid - JID of the room to be removed.
  75. * @param {string} subject - A new subject for the breakout room.
  76. */
  77. renameBreakoutRoom(breakoutRoomJid, subject) {
  78. if (!this.isSupported() || !this.room.isModerator()) {
  79. logger.error(`Cannot rename breakout room - supported:${this.isSupported()},
  80. moderator:${this.room.isModerator()}`);
  81. return;
  82. }
  83. const message = {
  84. type: BREAKOUT_ROOM_ACTIONS.RENAME,
  85. breakoutRoomJid,
  86. subject
  87. };
  88. this._sendMessage(message);
  89. }
  90. /**
  91. * Sends the given participant to the given room.
  92. *
  93. * @param {string} participantJid - JID of the participant to be sent to a room.
  94. * @param {string} roomJid - JID of the target room.
  95. */
  96. sendParticipantToRoom(participantJid, roomJid) {
  97. if (!this.isSupported() || !this.room.isModerator()) {
  98. logger.error(`Cannot send participant to room - supported:${this.isSupported()},
  99. moderator:${this.room.isModerator()}`);
  100. return;
  101. }
  102. const message = {
  103. type: BREAKOUT_ROOM_ACTIONS.MOVE_TO_ROOM,
  104. participantJid,
  105. roomJid
  106. };
  107. this._sendMessage(message);
  108. }
  109. /**
  110. * Retrieves whether a breakout room feature is supported.
  111. *
  112. * @param {string} feature - Feature to check.
  113. * @returns Wether the feature is supported.
  114. */
  115. isFeatureSupported(feature) {
  116. return Boolean((this.room.xmpp.breakoutRoomsFeatures || {})[feature]);
  117. }
  118. /**
  119. * Whether Breakout Rooms support is enabled in the backend or not.
  120. */
  121. isSupported() {
  122. return Boolean(this.getComponentAddress());
  123. }
  124. /**
  125. * Gets the address of the Breakout Rooms XMPP component.
  126. *
  127. * @returns The address of the component.
  128. */
  129. getComponentAddress() {
  130. return this.room.xmpp.breakoutRoomsComponentAddress;
  131. }
  132. /**
  133. * Stores if the current room is a breakout room.
  134. *
  135. * @param {boolean} isBreakoutRoom - Whether this room is a breakout room.
  136. */
  137. _setIsBreakoutRoom(isBreakoutRoom) {
  138. this._isBreakoutRoom = isBreakoutRoom;
  139. }
  140. /**
  141. * Checks whether this room is a breakout room.
  142. *
  143. * @returns True if the room is a breakout room, false otherwise.
  144. */
  145. isBreakoutRoom() {
  146. if (typeof this._isBreakoutRoom !== 'undefined') {
  147. return this._isBreakoutRoom;
  148. }
  149. // Use heuristic, helpful for checking in the MUC_JOINED event.
  150. return Strophe.getDomainFromJid(this.room.myroomjid) === this.getComponentAddress();
  151. }
  152. /**
  153. * Sets the main room JID associated with this breakout room. Only applies when
  154. * in a breakout room.
  155. *
  156. * @param {string} jid - The main room JID.
  157. */
  158. _setMainRoomJid(jid) {
  159. this._mainRoomJid = jid;
  160. }
  161. /**
  162. * Gets the main room's JID associated with this breakout room.
  163. *
  164. * @returns The main room JID.
  165. */
  166. getMainRoomJid() {
  167. return this._mainRoomJid;
  168. }
  169. /**
  170. * Handles a message for managing breakout rooms.
  171. *
  172. * @param {object} payload - Arbitrary data.
  173. */
  174. _handleMessages(payload) {
  175. switch (payload.event) {
  176. case BREAKOUT_ROOM_EVENTS.MOVE_TO_ROOM:
  177. this.room.eventEmitter.emit(XMPPEvents.BREAKOUT_ROOMS_MOVE_TO_ROOM, payload.roomJid);
  178. break;
  179. case BREAKOUT_ROOM_EVENTS.UPDATE: {
  180. const filteredPayload = this._filterUpdatePayload(payload);
  181. this._rooms = filteredPayload.rooms;
  182. this.room.eventEmitter.emit(XMPPEvents.BREAKOUT_ROOMS_UPDATED, filteredPayload);
  183. break;
  184. }
  185. }
  186. }
  187. /**
  188. * Filters the hidden participants from the payload.
  189. *
  190. * @param {Object} payload - The payload of the update message.
  191. * @return {Object} - The filtered payload.
  192. */
  193. _filterUpdatePayload(payload) {
  194. const hiddenDomain = this.room.options.hiddenDomain;
  195. const { rooms } = payload;
  196. const filteredRooms = {};
  197. Object.entries(rooms).forEach(([ key, room ]) => {
  198. const { participants = {} } = room;
  199. const filteredParticipants = {};
  200. Object.entries(participants).forEach(([ k, participant ]) => {
  201. if (Strophe.getDomainFromJid(participant.jid) !== hiddenDomain) {
  202. filteredParticipants[k] = participant;
  203. }
  204. });
  205. filteredRooms[key] = {
  206. ...room,
  207. participants: filteredParticipants
  208. };
  209. });
  210. return {
  211. ...payload,
  212. rooms: filteredRooms
  213. };
  214. }
  215. /**
  216. * Helper to send a breakout rooms message to the component.
  217. *
  218. * @param {Object} message - Command that needs to be sent.
  219. */
  220. _sendMessage(message) {
  221. const msg = $msg({ to: this.getComponentAddress() });
  222. msg.c('breakout_rooms', message).up();
  223. this.room.xmpp.connection.send(msg);
  224. }
  225. }