您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. import { getLogger } from '@jitsi/logger';
  2. import { $msg, Strophe } from 'strophe.js';
  3. import { XMPPEvents } from '../../service/xmpp/XMPPEvents';
  4. const logger = getLogger(__filename);
  5. /**
  6. * The command type for updating a lobby participant's e-mail address.
  7. *
  8. * @type {string}
  9. */
  10. const EMAIL_COMMAND = 'email';
  11. /**
  12. * The Lobby room implementation. Setting a room to members only, joining the lobby room
  13. * approving or denying access to participants from the lobby room.
  14. */
  15. export default class Lobby {
  16. /**
  17. * Constructs lobby room.
  18. *
  19. * @param {ChatRoom} room the main room.
  20. */
  21. constructor(room) {
  22. this.xmpp = room.xmpp;
  23. this.mainRoom = room;
  24. const maybeJoinLobbyRoom = this._maybeJoinLobbyRoom.bind(this);
  25. this.mainRoom.addEventListener(
  26. XMPPEvents.LOCAL_ROLE_CHANGED,
  27. maybeJoinLobbyRoom);
  28. this.mainRoom.addEventListener(
  29. XMPPEvents.MUC_MEMBERS_ONLY_CHANGED,
  30. maybeJoinLobbyRoom);
  31. this.mainRoom.addEventListener(
  32. XMPPEvents.ROOM_CONNECT_MEMBERS_ONLY_ERROR,
  33. jid => {
  34. this.lobbyRoomJid = jid;
  35. });
  36. }
  37. /**
  38. * Whether lobby is supported on backend.
  39. *
  40. * @returns {boolean} whether lobby is supported on backend.
  41. */
  42. isSupported() {
  43. return this.xmpp.lobbySupported;
  44. }
  45. /**
  46. * Enables lobby by setting the main room to be members only and joins the lobby chat room.
  47. *
  48. * @returns {Promise}
  49. */
  50. enable() {
  51. if (!this.isSupported()) {
  52. return Promise.reject(new Error('Lobby not supported!'));
  53. }
  54. return new Promise((resolve, reject) => {
  55. this.mainRoom.setMembersOnly(true, resolve, reject);
  56. });
  57. }
  58. /**
  59. * Disable lobby by setting the main room to be non members only and levaes the lobby chat room if joined.
  60. *
  61. * @returns {void}
  62. */
  63. disable() {
  64. if (!this.isSupported() || !this.mainRoom.isModerator()
  65. || !this.lobbyRoom || !this.mainRoom.membersOnlyEnabled) {
  66. return;
  67. }
  68. this.mainRoom.setMembersOnly(false);
  69. }
  70. /**
  71. * Broadcast a message to all participants in the lobby room
  72. * @param {Object} message The message to send
  73. *
  74. * @returns {void}
  75. */
  76. sendMessage(message) {
  77. if (this.lobbyRoom) {
  78. this.lobbyRoom.sendMessage(JSON.stringify(message), 'json-message');
  79. }
  80. }
  81. /**
  82. * Sends a private message to a participant in a lobby room.
  83. * @param {string} id The message to send
  84. * @param {Object} message The message to send
  85. *
  86. * @returns {void}
  87. */
  88. sendPrivateMessage(id, message) {
  89. if (this.lobbyRoom) {
  90. this.lobbyRoom.sendPrivateMessage(id, JSON.stringify(message), 'json-message');
  91. }
  92. }
  93. /**
  94. * Gets the local id for a participant in a lobby room.
  95. * This is used for lobby room private chat messages.
  96. *
  97. * @returns {string}
  98. */
  99. getLocalId() {
  100. if (this.lobbyRoom) {
  101. return Strophe.getResourceFromJid(this.lobbyRoom.myroomjid);
  102. }
  103. }
  104. /**
  105. * Adds a message listener to the lobby room.
  106. * @param {Function} listener The listener function,
  107. * called when a new message is received in the lobby room.
  108. *
  109. * @returns {Function} Handler returned to be able to remove it later.
  110. */
  111. addMessageListener(listener) {
  112. if (this.lobbyRoom) {
  113. const handler = (participantId, message) => {
  114. listener(message, Strophe.getResourceFromJid(participantId));
  115. };
  116. this.lobbyRoom.on(XMPPEvents.JSON_MESSAGE_RECEIVED, handler);
  117. return handler;
  118. }
  119. }
  120. /**
  121. * Remove a message handler from the lobby room.
  122. * @param {Function} handler The handler function to remove.
  123. *
  124. * @returns {void}
  125. */
  126. removeMessageHandler(handler) {
  127. if (this.lobbyRoom) {
  128. this.lobbyRoom.off(XMPPEvents.JSON_MESSAGE_RECEIVED, handler);
  129. }
  130. }
  131. /**
  132. * Leaves the lobby room.
  133. *
  134. * @returns {Promise}
  135. */
  136. leave() {
  137. if (this.lobbyRoom) {
  138. return this.lobbyRoom.leave()
  139. .then(() => {
  140. this.lobbyRoom = undefined;
  141. logger.info('Lobby room left!');
  142. })
  143. .catch(() => {}); // eslint-disable-line no-empty-function
  144. }
  145. return Promise.reject(
  146. new Error('The lobby has already been left'));
  147. }
  148. /**
  149. * We had received a jid for the lobby room.
  150. *
  151. * @param jid the lobby room jid to join.
  152. */
  153. setLobbyRoomJid(jid) {
  154. this.lobbyRoomJid = jid;
  155. }
  156. /**
  157. * Checks the state of mainRoom, lobbyRoom and current user role to decide whether to join lobby room.
  158. * @private
  159. */
  160. _maybeJoinLobbyRoom() {
  161. if (!this.isSupported()) {
  162. return;
  163. }
  164. const isModerator = this.mainRoom.joined && this.mainRoom.isModerator();
  165. if (isModerator && this.mainRoom.membersOnlyEnabled && !this.lobbyRoom) {
  166. // join the lobby
  167. this.join()
  168. .then(() => logger.info('Joined lobby room'))
  169. .catch(e => logger.error('Failed joining lobby', e));
  170. }
  171. }
  172. /**
  173. * Joins a lobby room setting display name and eventually avatar(using the email provided).
  174. *
  175. * @param {string} username is required.
  176. * @param {string} email is optional.
  177. * @returns {Promise} resolves once we join the room.
  178. */
  179. join(displayName, email) {
  180. const isModerator = this.mainRoom.joined && this.mainRoom.isModerator();
  181. if (!this.lobbyRoomJid) {
  182. return Promise.reject(new Error('Missing lobbyRoomJid, cannot join lobby room.'));
  183. }
  184. const roomName = Strophe.getNodeFromJid(this.lobbyRoomJid);
  185. const customDomain = Strophe.getDomainFromJid(this.lobbyRoomJid);
  186. this.lobbyRoom = this.xmpp.createRoom(
  187. roomName, {
  188. customDomain,
  189. disableDiscoInfo: true,
  190. disableFocus: true,
  191. enableLobby: false
  192. }
  193. );
  194. if (displayName) {
  195. // remove previously set nickname
  196. this.lobbyRoom.addOrReplaceInPresence('nick', {
  197. attributes: { xmlns: 'http://jabber.org/protocol/nick' },
  198. value: displayName
  199. });
  200. }
  201. if (isModerator) {
  202. this.lobbyRoom.addPresenceListener(EMAIL_COMMAND, (node, from) => {
  203. this.mainRoom.eventEmitter.emit(XMPPEvents.MUC_LOBBY_MEMBER_UPDATED, from, { email: node.value });
  204. });
  205. this.lobbyRoom.addEventListener(
  206. XMPPEvents.MUC_MEMBER_JOINED,
  207. // eslint-disable-next-line max-params
  208. (from, nick, role, isHiddenDomain, statsID, status, identity, botType, jid) => {
  209. // we need to ignore joins on lobby for participants that are already in the main room
  210. if (Object.values(this.mainRoom.members).find(m => m.jid === jid)) {
  211. return;
  212. }
  213. // Check if the user is a member if any breakout room.
  214. for (const room of Object.values(this.mainRoom.getBreakoutRooms()._rooms)) {
  215. if (Object.values(room.participants).find(p => p.jid === jid)) {
  216. return;
  217. }
  218. }
  219. // we emit the new event on the main room so we can propagate
  220. // events to the conference
  221. this.mainRoom.eventEmitter.emit(
  222. XMPPEvents.MUC_LOBBY_MEMBER_JOINED,
  223. Strophe.getResourceFromJid(from),
  224. nick,
  225. identity ? identity.avatar : undefined
  226. );
  227. });
  228. this.lobbyRoom.addEventListener(
  229. XMPPEvents.MUC_MEMBER_LEFT, from => {
  230. // we emit the new event on the main room so we can propagate
  231. // events to the conference
  232. this.mainRoom.eventEmitter.emit(
  233. XMPPEvents.MUC_LOBBY_MEMBER_LEFT,
  234. Strophe.getResourceFromJid(from)
  235. );
  236. });
  237. this.lobbyRoom.addEventListener(
  238. XMPPEvents.MUC_DESTROYED,
  239. () => {
  240. // let's make sure we emit that all lobby users had left
  241. Object.keys(this.lobbyRoom.members)
  242. .forEach(j => this.mainRoom.eventEmitter.emit(
  243. XMPPEvents.MUC_LOBBY_MEMBER_LEFT, Strophe.getResourceFromJid(j)));
  244. this.lobbyRoom.clean();
  245. this.lobbyRoom = undefined;
  246. logger.info('Lobby room left(destroyed)!');
  247. });
  248. } else {
  249. // this should only be handled by those waiting in lobby
  250. this.lobbyRoom.addEventListener(XMPPEvents.KICKED, isSelfPresence => {
  251. if (isSelfPresence) {
  252. this.mainRoom.eventEmitter.emit(XMPPEvents.MUC_DENIED_ACCESS);
  253. this.lobbyRoom.clean();
  254. return;
  255. }
  256. });
  257. // As there is still reference of the main room
  258. // the invite will be detected and addressed to its eventEmitter, even though we are not in it
  259. // the invite message should be received directly to the xmpp conn in general
  260. this.mainRoom.addEventListener(
  261. XMPPEvents.INVITE_MESSAGE_RECEIVED,
  262. (roomJid, from, txt, invitePassword) => {
  263. logger.debug(`Received approval to join ${roomJid} ${from} ${txt}`);
  264. if (roomJid === this.mainRoom.roomjid) {
  265. // we are now allowed, so let's join
  266. this.mainRoom.join(invitePassword);
  267. }
  268. });
  269. this.lobbyRoom.addEventListener(
  270. XMPPEvents.MUC_DESTROYED,
  271. (reason, jid) => {
  272. // we are receiving the jid of the main room
  273. // means we are invited to join, maybe lobby was disabled
  274. if (jid) {
  275. this.mainRoom.join();
  276. return;
  277. }
  278. this.lobbyRoom.clean();
  279. this.mainRoom.eventEmitter.emit(XMPPEvents.MUC_DESTROYED, reason);
  280. });
  281. // If participant retries joining shared password while waiting in the lobby
  282. // and succeeds make sure we leave lobby
  283. this.mainRoom.addEventListener(
  284. XMPPEvents.MUC_JOINED,
  285. () => {
  286. this.leave();
  287. });
  288. }
  289. return new Promise((resolve, reject) => {
  290. this.lobbyRoom.addEventListener(XMPPEvents.MUC_JOINED, () => {
  291. resolve();
  292. // send our email, as we do not handle this on initial presence we need a second one
  293. if (email && !isModerator) {
  294. this.lobbyRoom.addOrReplaceInPresence(EMAIL_COMMAND, { value: email })
  295. && this.lobbyRoom.sendPresence();
  296. }
  297. });
  298. this.lobbyRoom.addEventListener(XMPPEvents.ROOM_JOIN_ERROR, reject);
  299. this.lobbyRoom.addEventListener(XMPPEvents.ROOM_CONNECT_NOT_ALLOWED_ERROR, reject);
  300. this.lobbyRoom.addEventListener(XMPPEvents.ROOM_CONNECT_ERROR, reject);
  301. this.lobbyRoom.join();
  302. });
  303. }
  304. /**
  305. * Should be possible only for moderators.
  306. * @param id
  307. */
  308. denyAccess(id) {
  309. if (!this.isSupported() || !this.mainRoom.isModerator()) {
  310. return;
  311. }
  312. const jid = Object.keys(this.lobbyRoom.members)
  313. .find(j => Strophe.getResourceFromJid(j) === id);
  314. if (jid) {
  315. this.lobbyRoom.kick(jid);
  316. } else {
  317. logger.error(`Not found member for ${id} in lobby room.`);
  318. }
  319. }
  320. /**
  321. * Should be possible only for moderators.
  322. * @param id
  323. */
  324. approveAccess(id) {
  325. if (!this.isSupported() || !this.mainRoom.isModerator()) {
  326. return;
  327. }
  328. // Get the main room JID. If we are in a breakout room we'll use the main
  329. // room's lobby.
  330. let mainRoomJid = this.mainRoom.roomjid;
  331. if (this.mainRoom.getBreakoutRooms().isBreakoutRoom()) {
  332. mainRoomJid = this.mainRoom.getBreakoutRooms().getMainRoomJid();
  333. }
  334. const memberRoomJid = Object.keys(this.lobbyRoom.members)
  335. .find(j => Strophe.getResourceFromJid(j) === id);
  336. if (memberRoomJid) {
  337. const jid = this.lobbyRoom.members[memberRoomJid].jid;
  338. const msgToSend
  339. = $msg({ to: mainRoomJid })
  340. .c('x', { xmlns: 'http://jabber.org/protocol/muc#user' })
  341. .c('invite', { to: jid });
  342. this.xmpp.connection.sendIQ(msgToSend,
  343. () => { }, // eslint-disable-line no-empty-function
  344. e => {
  345. logger.error(`Error sending invite for ${jid}`, e);
  346. });
  347. } else {
  348. logger.error(`Not found member for ${memberRoomJid} in lobby room.`);
  349. }
  350. }
  351. }