Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. import { XMPPEvents } from '../../service/xmpp/XMPPEvents';
  2. import ChatRoom from './ChatRoom';
  3. import XMPP from './xmpp';
  4. export const IDENTITY_TYPE = 'file-sharing';
  5. /**
  6. * The file metadata used in file sharing.
  7. * Fields like `authorParticipantId`, `authorParticipantJid`, and `authorParticipantName` and `conferenceFullName` will
  8. * be set by the backend, so passing them is optional.
  9. */
  10. export type IFileMetadata = {
  11. /**
  12. * The ID of the participant who uploaded the file.
  13. */
  14. authorParticipantId?: string;
  15. /**
  16. * The connection JID of the participant who uploaded the file.
  17. */
  18. authorParticipantJid?: string;
  19. /**
  20. * The name of the participant who uploaded the file.
  21. */
  22. authorParticipantName?: string;
  23. /**
  24. * The jid of the conference where the file was uploaded.
  25. */
  26. conferenceFullName?: string;
  27. /**
  28. * The unique ID of the file.
  29. */
  30. fileId: string;
  31. /**
  32. * The name of the file.
  33. */
  34. fileName: string;
  35. /**
  36. * The size of the file in bytes.
  37. */
  38. fileSize: number;
  39. /**
  40. * The file type (file extension).
  41. */
  42. fileType: string;
  43. /**
  44. * The time when it was uploaded.
  45. */
  46. timestamp: number;
  47. };
  48. /**
  49. * The FileSharing logic.
  50. */
  51. export default class FileSharing {
  52. private _mainRoom: ChatRoom;
  53. private _xmpp: XMPP;
  54. /**
  55. * Constructs File sharing manager for a room.
  56. *
  57. * @param {ChatRoom} room the main room.
  58. */
  59. constructor(room: ChatRoom) {
  60. this._mainRoom = room;
  61. this._xmpp = room.xmpp;
  62. this._handleMessages = this._handleMessages.bind(this);
  63. this._mainRoom.xmpp.addListener(XMPPEvents.FILE_SHARING_EVENT, this._handleMessages);
  64. }
  65. /**
  66. * Stops listening for events.
  67. */
  68. dispose() {
  69. this._mainRoom.xmpp.removeListener(XMPPEvents.FILE_SHARING_EVENT, this._handleMessages);
  70. }
  71. /**
  72. * Whether AV moderation is supported on backend.
  73. *
  74. * @returns {boolean} whether AV moderation is supported on backend.
  75. */
  76. isSupported() {
  77. return Boolean(this._xmpp.fileSharingComponentAddress);
  78. }
  79. /**
  80. * Returns the file sharing identity type (service name).
  81. *
  82. * @returns {string} the file sharing service name.
  83. */
  84. getIdentityType() {
  85. return IDENTITY_TYPE;
  86. }
  87. /**
  88. * Adds a file to the file sharing component after the file has been uploaded.
  89. * @param metadata - The metadata of the file to be added.
  90. */
  91. addFile(metadata: IFileMetadata) {
  92. const message = {
  93. type: 'add',
  94. xmlns: 'http://jitsi.org/jitmeet'
  95. };
  96. this._sendMessage(message, metadata);
  97. }
  98. /**
  99. * Removes a file from the file sharing component after the file was deleted.
  100. * @param fileId - The file ID of the file to be removed.
  101. */
  102. removeFile(fileId: string) {
  103. const message = {
  104. type: 'remove',
  105. fileId,
  106. xmlns: 'http://jitsi.org/jitmeet'
  107. };
  108. this._sendMessage(message);
  109. }
  110. /**
  111. * Helper to send a file sharing message to the component.
  112. *
  113. * @param {Object} message - Command that needs to be sent.
  114. * @param {Object} content - The content to add to the element created if any.
  115. */
  116. _sendMessage(message: object, content?: object) {
  117. const msg = $msg({ to: this._xmpp.fileSharingComponentAddress });
  118. msg.c(IDENTITY_TYPE, message, content ? JSON.stringify(content) : undefined).up();
  119. this._xmpp.connection.send(msg);
  120. }
  121. /**
  122. * Handles a message for file sharing.
  123. *
  124. * @param {object} payload - Arbitrary data.
  125. */
  126. _handleMessages(payload) {
  127. switch (payload.event) {
  128. case 'add':
  129. this._mainRoom.eventEmitter.emit(XMPPEvents.FILE_SHARING_FILE_ADDED, payload.file);
  130. break;
  131. case 'remove': {
  132. this._mainRoom.eventEmitter.emit(XMPPEvents.FILE_SHARING_FILE_REMOVED, payload.fileId);
  133. break;
  134. }
  135. case 'list': {
  136. this._mainRoom.eventEmitter.emit(XMPPEvents.FILE_SHARING_FILES_RECEIVED, payload.files);
  137. break;
  138. }
  139. }
  140. }
  141. }