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.

RoomMetadata.ts 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import { getLogger } from '@jitsi/logger';
  2. import { isEqual } from 'lodash-es';
  3. import { $msg } from 'strophe.js';
  4. import { XMPPEvents } from '../../service/xmpp/XMPPEvents';
  5. import { JITSI_MEET_MUC_TYPE } from './xmpp';
  6. const logger = getLogger(__filename);
  7. /**
  8. * Helper class for handling room metadata.
  9. */
  10. export default class RoomMetadata {
  11. room: any;
  12. _metadata: any;
  13. /**
  14. * Constructs lobby room.
  15. *
  16. * @param {ChatRoom} room the room we are in.
  17. */
  18. constructor(room) {
  19. this.room = room;
  20. this._handleMessages = this._handleMessages.bind(this);
  21. this.room.xmpp.addListener(XMPPEvents.ROOM_METADATA_EVENT, this._handleMessages);
  22. this._metadata = {};
  23. }
  24. /**
  25. * Stops listening for events.
  26. */
  27. dispose() {
  28. this.room.xmpp.removeListener(XMPPEvents.ROOM_METADATA_EVENT, this._handleMessages);
  29. }
  30. /**
  31. * Sets metadata for the given key.
  32. *
  33. * @param {string} key - key under which the metadata will be stored.
  34. * @param {object} data - data to be stored.
  35. */
  36. setMetadata(key, data) {
  37. if (!this.isSupported()) {
  38. logger.error(`Cannot set room metadata - supported:${this.isSupported()}`);
  39. return;
  40. }
  41. const message = {
  42. key,
  43. data
  44. };
  45. this._sendMessage(message);
  46. }
  47. /**
  48. * Gets the stored metadata (all of it).
  49. *
  50. * @returns The stored metadata.
  51. */
  52. getMetadata() {
  53. return this._metadata;
  54. }
  55. /**
  56. * Whether Breakout Rooms support is enabled in the backend or not.
  57. */
  58. isSupported() {
  59. return Boolean(this.getComponentAddress());
  60. }
  61. /**
  62. * Gets the address of the Breakout Rooms XMPP component.
  63. *
  64. * @returns The address of the component.
  65. */
  66. getComponentAddress() {
  67. return this.room.xmpp.roomMetadataComponentAddress;
  68. }
  69. /**
  70. * Handles a message with metadata updates.
  71. *
  72. * @param {object} payload - Arbitrary data.
  73. */
  74. _handleMessages(payload) {
  75. const { metadata } = payload;
  76. if (!metadata || isEqual(this._metadata, metadata)) {
  77. return;
  78. }
  79. this._metadata = metadata;
  80. this.room.eventEmitter.emit(XMPPEvents.ROOM_METADATA_UPDATED, metadata);
  81. }
  82. /**
  83. * Helper to send a breakout rooms message to the component.
  84. *
  85. * @param {Object} message - Command that needs to be sent.
  86. */
  87. _sendMessage(message) {
  88. message[JITSI_MEET_MUC_TYPE] = 'room_metadata';
  89. const msg = $msg({ to: this.getComponentAddress() });
  90. msg.c('room_metadata', {
  91. room: this.room.roomjid,
  92. xmlns: 'http://jitsi.org/jitmeet'
  93. }, JSON.stringify(message)).up();
  94. this.room.xmpp.connection.send(msg);
  95. }
  96. }