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.

BreakoutRooms.js 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. import { getLogger } from '@jitsi/logger';
  2. import { $msg } 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. REMOVE: `${FEATURE_KEY}/remove`,
  8. MOVE_TO_ROOM: `${FEATURE_KEY}/move-to-room`
  9. };
  10. const BREAKOUT_ROOM_EVENTS = {
  11. MOVE_TO_ROOM: `${FEATURE_KEY}/move-to-room`,
  12. UPDATE: `${FEATURE_KEY}/update`
  13. };
  14. const logger = getLogger(__filename);
  15. /**
  16. * Helper class for handling breakout rooms.
  17. */
  18. export default class BreakoutRooms {
  19. /**
  20. * Constructs lobby room.
  21. *
  22. * @param {ChatRoom} room the room we are in.
  23. */
  24. constructor(room) {
  25. this.room = room;
  26. this._handleMessages = this._handleMessages.bind(this);
  27. this.room.xmpp.addListener(XMPPEvents.BREAKOUT_ROOMS_EVENT, this._handleMessages);
  28. this._rooms = {};
  29. }
  30. /**
  31. * Stops listening for events.
  32. */
  33. dispose() {
  34. this.room.xmpp.removeListener(XMPPEvents.BREAKOUT_ROOMS_EVENT, this._handleMessages);
  35. }
  36. /**
  37. * Creates a breakout room with the given subject.
  38. *
  39. * @param {string} subject - A subject for the breakout room.
  40. */
  41. createBreakoutRoom(subject) {
  42. if (!this.isSupported() || !this.room.isModerator()) {
  43. logger.error(`Cannot create breakout room - supported:${this.isSupported()},
  44. moderator:${this.room.isModerator()}`);
  45. return;
  46. }
  47. const message = {
  48. type: BREAKOUT_ROOM_ACTIONS.ADD,
  49. subject
  50. };
  51. this._sendMessage(message);
  52. }
  53. /**
  54. * Removes a breakout room.
  55. *
  56. * @param {string} breakoutRoomJid - JID of the room to be removed.
  57. */
  58. removeBreakoutRoom(breakoutRoomJid) {
  59. if (!this.isSupported() || !this.room.isModerator()) {
  60. logger.error(`Cannot remove breakout room - supported:${this.isSupported()},
  61. moderator:${this.room.isModerator()}`);
  62. return;
  63. }
  64. const message = {
  65. type: BREAKOUT_ROOM_ACTIONS.REMOVE,
  66. breakoutRoomJid
  67. };
  68. this._sendMessage(message);
  69. }
  70. /**
  71. * Sends the given participant to the given room.
  72. *
  73. * @param {string} participantJid - JID of the participant to be sent to a room.
  74. * @param {string} roomJid - JID of the target room.
  75. */
  76. sendParticipantToRoom(participantJid, roomJid) {
  77. if (!this.isSupported() || !this.room.isModerator()) {
  78. logger.error(`Cannot send participant to room - supported:${this.isSupported()},
  79. moderator:${this.room.isModerator()}`);
  80. return;
  81. }
  82. const message = {
  83. type: BREAKOUT_ROOM_ACTIONS.MOVE_TO_ROOM,
  84. participantJid,
  85. roomJid
  86. };
  87. this._sendMessage(message);
  88. }
  89. /**
  90. * Whether Breakout Rooms support is enabled in the backend or not.
  91. */
  92. isSupported() {
  93. return Boolean(this.getComponentAddress());
  94. }
  95. /**
  96. * Gets the address of the Breakout Rooms XMPP component.
  97. *
  98. * @returns The address of the component.
  99. */
  100. getComponentAddress() {
  101. return this.room.xmpp.breakoutRoomsComponentAddress;
  102. }
  103. /**
  104. * Stores if the current room is a breakout room.
  105. *
  106. * @param {boolean} isBreakoutRoom - Whether this room is a breakout room.
  107. */
  108. _setIsBreakoutRoom(isBreakoutRoom) {
  109. this._isBreakoutRoom = isBreakoutRoom;
  110. }
  111. /**
  112. * Checks whether this room is a breakout room.
  113. *
  114. * @returns True if the room is a breakout room, false otherwise.
  115. */
  116. isBreakoutRoom() {
  117. return this._isBreakoutRoom;
  118. }
  119. /**
  120. * Sets the main room JID associated with this breakout room. Only applies when
  121. * in a breakout room.
  122. *
  123. * @param {string} jid - The main room JID.
  124. */
  125. _setMainRoomJid(jid) {
  126. this._mainRoomJid = jid;
  127. }
  128. /**
  129. * Gets the main room's JID associated with this breakout room.
  130. *
  131. * @returns The main room JID.
  132. */
  133. getMainRoomJid() {
  134. return this._mainRoomJid;
  135. }
  136. /**
  137. * Handles a message for managing breakout rooms.
  138. *
  139. * @param {object} payload - Arbitrary data.
  140. */
  141. _handleMessages(payload) {
  142. switch (payload.event) {
  143. case BREAKOUT_ROOM_EVENTS.MOVE_TO_ROOM:
  144. this.room.eventEmitter.emit(XMPPEvents.BREAKOUT_ROOMS_MOVE_TO_ROOM, payload.roomJid);
  145. break;
  146. case BREAKOUT_ROOM_EVENTS.UPDATE: {
  147. this._rooms = payload.rooms;
  148. this.room.eventEmitter.emit(XMPPEvents.BREAKOUT_ROOMS_UPDATED, payload);
  149. break;
  150. }
  151. }
  152. }
  153. /**
  154. * Helper to send a breakout rooms message to the component.
  155. *
  156. * @param {Object} message - Command that needs to be sent.
  157. */
  158. _sendMessage(message) {
  159. const msg = $msg({ to: this.getComponentAddress() });
  160. msg.c('breakout_rooms', message).up();
  161. this.room.xmpp.connection.send(msg);
  162. }
  163. }