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.

RoomMetadata.ts 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import { getLogger } from '@jitsi/logger';
  2. import isEqual from 'lodash.isequal';
  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() || !this.room.isModerator()) {
  38. logger.error(`Cannot set room metadata - supported:${this.isSupported()},
  39. moderator:${this.room.isModerator()}`);
  40. return;
  41. }
  42. const message = {
  43. key,
  44. data
  45. };
  46. this._sendMessage(message);
  47. }
  48. /**
  49. * Gets the stored metadata (all of it).
  50. *
  51. * @returns The stored metadata.
  52. */
  53. getMetadata() {
  54. return this._metadata;
  55. }
  56. /**
  57. * Whether Breakout Rooms support is enabled in the backend or not.
  58. */
  59. isSupported() {
  60. return Boolean(this.getComponentAddress());
  61. }
  62. /**
  63. * Gets the address of the Breakout Rooms XMPP component.
  64. *
  65. * @returns The address of the component.
  66. */
  67. getComponentAddress() {
  68. return this.room.xmpp.roomMetadataComponentAddress;
  69. }
  70. /**
  71. * Handles a message with metadata updates.
  72. *
  73. * @param {object} payload - Arbitrary data.
  74. */
  75. _handleMessages(payload) {
  76. const { metadata } = payload;
  77. if (!metadata || isEqual(this._metadata, metadata)) {
  78. return;
  79. }
  80. this._metadata = metadata;
  81. this.room.eventEmitter.emit(XMPPEvents.ROOM_METADATA_UPDATED, metadata);
  82. }
  83. /**
  84. * Helper to send a breakout rooms message to the component.
  85. *
  86. * @param {Object} message - Command that needs to be sent.
  87. */
  88. _sendMessage(message) {
  89. message[JITSI_MEET_MUC_TYPE] = 'room_metadata';
  90. const msg = $msg({ to: this.getComponentAddress() });
  91. msg.c('room_metadata', {
  92. room: this.room.roomjid,
  93. xmlns: 'http://jitsi.org/jitmeet'
  94. }, JSON.stringify(message)).up();
  95. this.room.xmpp.connection.send(msg);
  96. }
  97. }