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

ChatRoom.js 46KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485
  1. /* global $, __filename */
  2. import { getLogger } from 'jitsi-meet-logger';
  3. import { $iq, $msg, $pres, Strophe } from 'strophe.js';
  4. import GlobalOnErrorHandler from '../util/GlobalOnErrorHandler';
  5. import * as JitsiTranscriptionStatus from '../../JitsiTranscriptionStatus';
  6. import Listenable from '../util/Listenable';
  7. import * as MediaType from '../../service/RTC/MediaType';
  8. import { createConferenceEvent } from '../../service/statistics/AnalyticsEvents';
  9. import XMPPEvents from '../../service/xmpp/XMPPEvents';
  10. import Statistics from '../statistics/statistics';
  11. import Moderator from './moderator';
  12. const logger = getLogger(__filename);
  13. export const parser = {
  14. packet2JSON(xmlElement, nodes) {
  15. for (const child of Array.from(xmlElement.children)) {
  16. const node = {
  17. attributes: {},
  18. children: [],
  19. tagName: child.tagName
  20. };
  21. for (const attr of Array.from(child.attributes)) {
  22. node.attributes[attr.name] = attr.value;
  23. }
  24. const text = Strophe.getText(child);
  25. if (text) {
  26. // Using Strophe.getText will do work for traversing all direct
  27. // child text nodes but returns an escaped value, which is not
  28. // desirable at this point.
  29. node.value = Strophe.xmlunescape(text);
  30. }
  31. nodes.push(node);
  32. this.packet2JSON(child, node.children);
  33. }
  34. },
  35. json2packet(nodes, packet) {
  36. for (let i = 0; i < nodes.length; i++) {
  37. const node = nodes[i];
  38. if (node) {
  39. packet.c(node.tagName, node.attributes);
  40. if (node.value) {
  41. packet.t(node.value);
  42. }
  43. if (node.children) {
  44. this.json2packet(node.children, packet);
  45. }
  46. packet.up();
  47. }
  48. }
  49. // packet.up();
  50. }
  51. };
  52. /**
  53. * Returns array of JS objects from the presence JSON associated with the passed
  54. / nodeName
  55. * @param pres the presence JSON
  56. * @param nodeName the name of the node (videomuted, audiomuted, etc)
  57. */
  58. function filterNodeFromPresenceJSON(pres, nodeName) {
  59. const res = [];
  60. for (let i = 0; i < pres.length; i++) {
  61. if (pres[i].tagName === nodeName) {
  62. res.push(pres[i]);
  63. }
  64. }
  65. return res;
  66. }
  67. // XXX As ChatRoom constructs XMPP stanzas and Strophe is build around the idea
  68. // of chaining function calls, allow long function call chains.
  69. /* eslint-disable newline-per-chained-call */
  70. /**
  71. *
  72. */
  73. export default class ChatRoom extends Listenable {
  74. /* eslint-disable max-params */
  75. /**
  76. *
  77. * @param connection
  78. * @param jid
  79. * @param password
  80. * @param XMPP
  81. * @param options
  82. * @param {boolean} options.disableFocus - when set to {@code false} will
  83. * not invite Jicofo into the room. This is intended to be used only by
  84. * jitsi-meet-spot.
  85. */
  86. constructor(connection, jid, password, XMPP, options) {
  87. super();
  88. this.xmpp = XMPP;
  89. this.connection = connection;
  90. this.roomjid = Strophe.getBareJidFromJid(jid);
  91. this.myroomjid = jid;
  92. this.password = password;
  93. logger.info(`Joined MUC as ${this.myroomjid}`);
  94. this.members = {};
  95. this.presMap = {};
  96. this.presHandlers = {};
  97. this.joined = false;
  98. this.role = null;
  99. this.focusMucJid = null;
  100. this.noBridgeAvailable = false;
  101. this.options = options || {};
  102. this.moderator
  103. = new Moderator(this.roomjid, this.xmpp, this.eventEmitter, {
  104. connection: this.xmpp.options,
  105. conference: this.options
  106. });
  107. this.initPresenceMap(options);
  108. this.lastPresences = {};
  109. this.phoneNumber = null;
  110. this.phonePin = null;
  111. this.connectionTimes = {};
  112. this.participantPropertyListener = null;
  113. this.locked = false;
  114. this.transcriptionStatus = JitsiTranscriptionStatus.OFF;
  115. }
  116. /* eslint-enable max-params */
  117. /**
  118. *
  119. */
  120. initPresenceMap(options = {}) {
  121. this.presMap.to = this.myroomjid;
  122. this.presMap.xns = 'http://jabber.org/protocol/muc';
  123. this.presMap.nodes = [];
  124. if (options.statsId) {
  125. this.presMap.nodes.push({
  126. 'tagName': 'stats-id',
  127. 'value': options.statsId
  128. });
  129. }
  130. // We need to broadcast 'videomuted' status from the beginning, cause
  131. // Jicofo makes decisions based on that. Initialize it with 'false'
  132. // here.
  133. this.addVideoInfoToPresence(false);
  134. if (options.deploymentInfo && options.deploymentInfo.userRegion) {
  135. this.presMap.nodes.push({
  136. 'tagName': 'region',
  137. 'attributes': {
  138. id: options.deploymentInfo.userRegion,
  139. xmlns: 'http://jitsi.org/jitsi-meet'
  140. }
  141. });
  142. }
  143. }
  144. /**
  145. * Joins the chat room.
  146. * @param password
  147. * @returns {Promise} - resolved when join completes. At the time of this
  148. * writing it's never rejected.
  149. */
  150. join(password) {
  151. this.password = password;
  152. return new Promise(resolve => {
  153. this.options.disableFocus
  154. && logger.info('Conference focus disabled');
  155. const preJoin
  156. = this.options.disableFocus
  157. ? Promise.resolve()
  158. : this.moderator.allocateConferenceFocus();
  159. preJoin.then(() => {
  160. this.sendPresence(true);
  161. resolve();
  162. });
  163. });
  164. }
  165. /**
  166. *
  167. * @param fromJoin
  168. */
  169. sendPresence(fromJoin) {
  170. const to = this.presMap.to;
  171. if (!to || (!this.joined && !fromJoin)) {
  172. // Too early to send presence - not initialized
  173. return;
  174. }
  175. const pres = $pres({ to });
  176. // xep-0045 defines: "including in the initial presence stanza an empty
  177. // <x/> element qualified by the 'http://jabber.org/protocol/muc'
  178. // namespace" and subsequent presences should not include that or it can
  179. // be considered as joining, and server can send us the message history
  180. // for the room on every presence
  181. if (fromJoin) {
  182. pres.c('x', { xmlns: this.presMap.xns });
  183. if (this.password) {
  184. pres.c('password').t(this.password).up();
  185. }
  186. pres.up();
  187. }
  188. parser.json2packet(this.presMap.nodes, pres);
  189. this.connection.send(pres);
  190. if (fromJoin) {
  191. // XXX We're pressed for time here because we're beginning a complex
  192. // and/or lengthy conference-establishment process which supposedly
  193. // involves multiple RTTs. We don't have the time to wait for
  194. // Strophe to decide to send our IQ.
  195. this.connection.flush();
  196. }
  197. }
  198. /**
  199. * Sends the presence unavailable, signaling the server
  200. * we want to leave the room.
  201. */
  202. doLeave() {
  203. logger.log('do leave', this.myroomjid);
  204. const pres = $pres({ to: this.myroomjid,
  205. type: 'unavailable' });
  206. this.presMap.length = 0;
  207. // XXX Strophe is asynchronously sending by default. Unfortunately, that
  208. // means that there may not be enough time to send the unavailable
  209. // presence. Switching Strophe to synchronous sending is not much of an
  210. // option because it may lead to a noticeable delay in navigating away
  211. // from the current location. As a compromise, we will try to increase
  212. // the chances of sending the unavailable presence within the short time
  213. // span that we have upon unloading by invoking flush() on the
  214. // connection. We flush() once before sending/queuing the unavailable
  215. // presence in order to attemtp to have the unavailable presence at the
  216. // top of the send queue. We flush() once more after sending/queuing the
  217. // unavailable presence in order to attempt to have it sent as soon as
  218. // possible.
  219. this.connection.flush();
  220. this.connection.send(pres);
  221. this.connection.flush();
  222. }
  223. /**
  224. *
  225. */
  226. discoRoomInfo() {
  227. // https://xmpp.org/extensions/xep-0045.html#disco-roominfo
  228. const getInfo
  229. = $iq({
  230. type: 'get',
  231. to: this.roomjid
  232. })
  233. .c('query', { xmlns: Strophe.NS.DISCO_INFO });
  234. this.connection.sendIQ(getInfo, result => {
  235. const locked
  236. = $(result).find('>query>feature[var="muc_passwordprotected"]')
  237. .length
  238. === 1;
  239. if (locked !== this.locked) {
  240. this.eventEmitter.emit(XMPPEvents.MUC_LOCK_CHANGED, locked);
  241. this.locked = locked;
  242. }
  243. const meetingIdValEl
  244. = $(result).find('>query>x[type="result"]>field[var="muc#roominfo_meetingId"]>value');
  245. if (meetingIdValEl.length) {
  246. this.setMeetingId(meetingIdValEl.text());
  247. } else {
  248. logger.trace('No meeting ID from backend');
  249. }
  250. }, error => {
  251. GlobalOnErrorHandler.callErrorHandler(error);
  252. logger.error('Error getting room info: ', error);
  253. });
  254. }
  255. /**
  256. * Sets the meeting unique Id (received from the backend).
  257. *
  258. * @param {string} meetingId - The new meetings id.
  259. * @returns {void}
  260. */
  261. setMeetingId(meetingId) {
  262. if (this.meetingId !== meetingId) {
  263. if (this.meetingId) {
  264. logger.warn(`Meeting Id changed from:${this.meetingId} to:${meetingId}`);
  265. }
  266. this.meetingId = meetingId;
  267. // The name of the action is a little bit confusing but it seems this is the preferred name by the consumers
  268. // of the analytics events.
  269. Statistics.sendAnalytics(createConferenceEvent('joined', meetingId));
  270. }
  271. }
  272. /**
  273. *
  274. */
  275. createNonAnonymousRoom() {
  276. // http://xmpp.org/extensions/xep-0045.html#createroom-reserved
  277. const getForm = $iq({ type: 'get',
  278. to: this.roomjid })
  279. .c('query', { xmlns: 'http://jabber.org/protocol/muc#owner' })
  280. .c('x', { xmlns: 'jabber:x:data',
  281. type: 'submit' });
  282. const self = this;
  283. this.connection.sendIQ(getForm, form => {
  284. if (!$(form).find(
  285. '>query>x[xmlns="jabber:x:data"]'
  286. + '>field[var="muc#roomconfig_whois"]').length) {
  287. const errmsg = 'non-anonymous rooms not supported';
  288. GlobalOnErrorHandler.callErrorHandler(new Error(errmsg));
  289. logger.error(errmsg);
  290. return;
  291. }
  292. const formSubmit = $iq({ to: self.roomjid,
  293. type: 'set' })
  294. .c('query', { xmlns: 'http://jabber.org/protocol/muc#owner' });
  295. formSubmit.c('x', { xmlns: 'jabber:x:data',
  296. type: 'submit' });
  297. formSubmit.c('field', { 'var': 'FORM_TYPE' })
  298. .c('value')
  299. .t('http://jabber.org/protocol/muc#roomconfig').up().up();
  300. formSubmit.c('field', { 'var': 'muc#roomconfig_whois' })
  301. .c('value').t('anyone').up().up();
  302. self.connection.sendIQ(formSubmit);
  303. }, error => {
  304. GlobalOnErrorHandler.callErrorHandler(error);
  305. logger.error('Error getting room configuration form: ', error);
  306. });
  307. }
  308. /**
  309. *
  310. * @param pres
  311. */
  312. onPresence(pres) {
  313. const from = pres.getAttribute('from');
  314. const member = {};
  315. const statusEl = pres.getElementsByTagName('status')[0];
  316. if (statusEl) {
  317. member.status = statusEl.textContent || '';
  318. }
  319. let hasStatusUpdate = false;
  320. let hasVersionUpdate = false;
  321. const xElement
  322. = pres.getElementsByTagNameNS(
  323. 'http://jabber.org/protocol/muc#user', 'x')[0];
  324. const mucUserItem
  325. = xElement && xElement.getElementsByTagName('item')[0];
  326. member.affiliation
  327. = mucUserItem && mucUserItem.getAttribute('affiliation');
  328. member.role = mucUserItem && mucUserItem.getAttribute('role');
  329. // Focus recognition
  330. const jid = mucUserItem && mucUserItem.getAttribute('jid');
  331. member.jid = jid;
  332. member.isFocus
  333. = jid && jid.indexOf(`${this.moderator.getFocusUserJid()}/`) === 0;
  334. member.isHiddenDomain
  335. = jid && jid.indexOf('@') > 0
  336. && this.options.hiddenDomain
  337. === jid.substring(jid.indexOf('@') + 1, jid.indexOf('/'));
  338. this.eventEmitter.emit(XMPPEvents.PRESENCE_RECEIVED, {
  339. fromHiddenDomain: member.isHiddenDomain,
  340. presence: pres
  341. });
  342. const xEl = pres.querySelector('x');
  343. if (xEl) {
  344. xEl.remove();
  345. }
  346. const nodes = [];
  347. parser.packet2JSON(pres, nodes);
  348. this.lastPresences[from] = nodes;
  349. // process nodes to extract data needed for MUC_JOINED and
  350. // MUC_MEMBER_JOINED events
  351. const extractIdentityInformation = node => {
  352. const identity = {};
  353. const userInfo = node.children.find(c => c.tagName === 'user');
  354. if (userInfo) {
  355. identity.user = {};
  356. for (const tag of [ 'id', 'name', 'avatar' ]) {
  357. const child
  358. = userInfo.children.find(c => c.tagName === tag);
  359. if (child) {
  360. identity.user[tag] = child.value;
  361. }
  362. }
  363. }
  364. const groupInfo = node.children.find(c => c.tagName === 'group');
  365. if (groupInfo) {
  366. identity.group = groupInfo.value;
  367. }
  368. return identity;
  369. };
  370. for (let i = 0; i < nodes.length; i++) {
  371. const node = nodes[i];
  372. switch (node.tagName) {
  373. case 'bot': {
  374. const { attributes } = node;
  375. if (!attributes) {
  376. break;
  377. }
  378. const { type } = attributes;
  379. member.botType = type;
  380. break;
  381. }
  382. case 'nick':
  383. member.nick = node.value;
  384. break;
  385. case 'userId':
  386. member.id = node.value;
  387. break;
  388. case 'stats-id':
  389. member.statsID = node.value;
  390. break;
  391. case 'identity':
  392. member.identity = extractIdentityInformation(node);
  393. break;
  394. case 'stat': {
  395. const { attributes } = node;
  396. if (!attributes) {
  397. break;
  398. }
  399. const { name } = attributes;
  400. if (name === 'version') {
  401. member.version = attributes.value;
  402. }
  403. break;
  404. }
  405. }
  406. }
  407. if (from === this.myroomjid) {
  408. const newRole
  409. = member.affiliation === 'owner' ? member.role : 'none';
  410. if (this.role !== newRole) {
  411. this.role = newRole;
  412. this.eventEmitter.emit(
  413. XMPPEvents.LOCAL_ROLE_CHANGED,
  414. this.role);
  415. }
  416. if (!this.joined) {
  417. this.joined = true;
  418. const now = this.connectionTimes['muc.joined']
  419. = window.performance.now();
  420. logger.log('(TIME) MUC joined:\t', now);
  421. // set correct initial state of locked
  422. if (this.password) {
  423. this.locked = true;
  424. }
  425. // Re-send presence in case any presence updates were added,
  426. // but blocked from sending, during the join process.
  427. this.sendPresence();
  428. this.eventEmitter.emit(XMPPEvents.MUC_JOINED);
  429. // Now let's check the disco-info to retrieve the
  430. // meeting Id if any
  431. this.discoRoomInfo();
  432. }
  433. } else if (jid === undefined) {
  434. logger.info('Ignoring member with undefined JID');
  435. } else if (this.members[from] === undefined) {
  436. // new participant
  437. this.members[from] = member;
  438. logger.log('entered', from, member);
  439. hasStatusUpdate = member.status !== undefined;
  440. hasVersionUpdate = member.version !== undefined;
  441. if (member.isFocus) {
  442. this._initFocus(from, jid);
  443. } else {
  444. // identity is being added to member joined, so external
  445. // services can be notified for that (currently identity is
  446. // not used inside library)
  447. this.eventEmitter.emit(
  448. XMPPEvents.MUC_MEMBER_JOINED,
  449. from,
  450. member.nick,
  451. member.role,
  452. member.isHiddenDomain,
  453. member.statsID,
  454. member.status,
  455. member.identity,
  456. member.botType);
  457. // we are reporting the status with the join
  458. // so we do not want a second event about status update
  459. hasStatusUpdate = false;
  460. }
  461. } else {
  462. // Presence update for existing participant
  463. // Watch role change:
  464. const memberOfThis = this.members[from];
  465. if (memberOfThis.role !== member.role) {
  466. memberOfThis.role = member.role;
  467. this.eventEmitter.emit(
  468. XMPPEvents.MUC_ROLE_CHANGED, from, member.role);
  469. }
  470. // fire event that botType had changed
  471. if (memberOfThis.botType !== member.botType) {
  472. memberOfThis.botType = member.botType;
  473. this.eventEmitter.emit(
  474. XMPPEvents.MUC_MEMBER_BOT_TYPE_CHANGED,
  475. from,
  476. member.botType);
  477. }
  478. if (member.isFocus) {
  479. // From time to time first few presences of the focus are not
  480. // containing it's jid. That way we can mark later the focus
  481. // member instead of not marking it at all and not starting the
  482. // conference.
  483. // FIXME: Maybe there is a better way to handle this issue. It
  484. // seems there is some period of time in prosody that the
  485. // configuration form is received but not applied. And if any
  486. // participant joins during that period of time the first
  487. // presence from the focus won't contain
  488. // <item jid="focus..." />.
  489. memberOfThis.isFocus = true;
  490. this._initFocus(from, jid);
  491. }
  492. // store the new display name
  493. if (member.displayName) {
  494. memberOfThis.displayName = member.displayName;
  495. }
  496. // update stored status message to be able to detect changes
  497. if (memberOfThis.status !== member.status) {
  498. hasStatusUpdate = true;
  499. memberOfThis.status = member.status;
  500. }
  501. if (memberOfThis.version !== member.version) {
  502. hasVersionUpdate = true;
  503. memberOfThis.version = member.version;
  504. }
  505. }
  506. // after we had fired member or room joined events, lets fire events
  507. // for the rest info we got in presence
  508. for (let i = 0; i < nodes.length; i++) {
  509. const node = nodes[i];
  510. switch (node.tagName) {
  511. case 'nick':
  512. if (!member.isFocus) {
  513. const displayName
  514. = this.xmpp.options.displayJids
  515. ? Strophe.getResourceFromJid(from)
  516. : member.nick;
  517. this.eventEmitter.emit(
  518. XMPPEvents.DISPLAY_NAME_CHANGED,
  519. from,
  520. displayName);
  521. }
  522. break;
  523. case 'bridgeNotAvailable':
  524. if (member.isFocus && !this.noBridgeAvailable) {
  525. this.noBridgeAvailable = true;
  526. this.eventEmitter.emit(XMPPEvents.BRIDGE_DOWN);
  527. }
  528. break;
  529. case 'conference-properties':
  530. if (member.isFocus) {
  531. const properties = {};
  532. for (let j = 0; j < node.children.length; j++) {
  533. const { attributes } = node.children[j];
  534. if (attributes && attributes.key) {
  535. properties[attributes.key] = attributes.value;
  536. }
  537. }
  538. this.eventEmitter.emit(
  539. XMPPEvents.CONFERENCE_PROPERTIES_CHANGED, properties);
  540. }
  541. break;
  542. case 'transcription-status': {
  543. const { attributes } = node;
  544. if (!attributes) {
  545. break;
  546. }
  547. const { status } = attributes;
  548. if (status && status !== this.transcriptionStatus) {
  549. this.transcriptionStatus = status;
  550. this.eventEmitter.emit(
  551. XMPPEvents.TRANSCRIPTION_STATUS_CHANGED,
  552. status
  553. );
  554. }
  555. break;
  556. }
  557. case 'call-control': {
  558. const att = node.attributes;
  559. if (!att) {
  560. break;
  561. }
  562. this.phoneNumber = att.phone || null;
  563. this.phonePin = att.pin || null;
  564. this.eventEmitter.emit(XMPPEvents.PHONE_NUMBER_CHANGED);
  565. break;
  566. }
  567. default:
  568. this.processNode(node, from);
  569. }
  570. }
  571. // Trigger status message update if necessary
  572. if (hasStatusUpdate) {
  573. this.eventEmitter.emit(
  574. XMPPEvents.PRESENCE_STATUS,
  575. from,
  576. member.status);
  577. }
  578. if (hasVersionUpdate) {
  579. logger.info(`Received version for ${jid}: ${member.version}`);
  580. }
  581. }
  582. /**
  583. * Initialize some properties when the focus participant is verified.
  584. * @param from jid of the focus
  585. * @param mucJid the jid of the focus in the muc
  586. */
  587. _initFocus(from, mucJid) {
  588. this.focusMucJid = from;
  589. logger.info(`Ignore focus: ${from}, real JID: ${mucJid}`);
  590. }
  591. /**
  592. * Sets the special listener to be used for "command"s whose name starts
  593. * with "jitsi_participant_".
  594. */
  595. setParticipantPropertyListener(listener) {
  596. this.participantPropertyListener = listener;
  597. }
  598. /**
  599. *
  600. * @param node
  601. * @param from
  602. */
  603. processNode(node, from) {
  604. // make sure we catch all errors coming from any handler
  605. // otherwise we can remove the presence handler from strophe
  606. try {
  607. let tagHandlers = this.presHandlers[node.tagName];
  608. if (node.tagName.startsWith('jitsi_participant_')) {
  609. tagHandlers = [ this.participantPropertyListener ];
  610. }
  611. if (tagHandlers) {
  612. tagHandlers.forEach(handler => {
  613. handler(node, Strophe.getResourceFromJid(from), from);
  614. });
  615. }
  616. } catch (e) {
  617. GlobalOnErrorHandler.callErrorHandler(e);
  618. logger.error(`Error processing:${node.tagName} node.`, e);
  619. }
  620. }
  621. /**
  622. * Send text message to the other participants in the conference
  623. * @param message
  624. * @param elementName
  625. * @param nickname
  626. */
  627. sendMessage(message, elementName, nickname) {
  628. const msg = $msg({ to: this.roomjid,
  629. type: 'groupchat' });
  630. // We are adding the message in a packet extension. If this element
  631. // is different from 'body', we add a custom namespace.
  632. // e.g. for 'json-message' extension of message stanza.
  633. if (elementName === 'body') {
  634. msg.c(elementName, message).up();
  635. } else {
  636. msg.c(elementName, { xmlns: 'http://jitsi.org/jitmeet' }, message)
  637. .up();
  638. }
  639. if (nickname) {
  640. msg.c('nick', { xmlns: 'http://jabber.org/protocol/nick' })
  641. .t(nickname)
  642. .up()
  643. .up();
  644. }
  645. this.connection.send(msg);
  646. this.eventEmitter.emit(XMPPEvents.SENDING_CHAT_MESSAGE, message);
  647. }
  648. /* eslint-disable max-params */
  649. /**
  650. * Send private text message to another participant of the conference
  651. * @param id id/muc resource of the receiver
  652. * @param message
  653. * @param elementName
  654. * @param nickname
  655. */
  656. sendPrivateMessage(id, message, elementName, nickname) {
  657. const msg = $msg({ to: `${this.roomjid}/${id}`,
  658. type: 'chat' });
  659. // We are adding the message in packet. If this element is different
  660. // from 'body', we add our custom namespace for the same.
  661. // e.g. for 'json-message' message extension.
  662. if (elementName === 'body') {
  663. msg.c(elementName, message).up();
  664. } else {
  665. msg.c(elementName, { xmlns: 'http://jitsi.org/jitmeet' }, message)
  666. .up();
  667. }
  668. if (nickname) {
  669. msg.c('nick', { xmlns: 'http://jabber.org/protocol/nick' })
  670. .t(nickname)
  671. .up()
  672. .up();
  673. }
  674. this.connection.send(msg);
  675. this.eventEmitter.emit(
  676. XMPPEvents.SENDING_PRIVATE_CHAT_MESSAGE, message);
  677. }
  678. /* eslint-enable max-params */
  679. /**
  680. *
  681. * @param subject
  682. */
  683. setSubject(subject) {
  684. const msg = $msg({ to: this.roomjid,
  685. type: 'groupchat' });
  686. msg.c('subject', subject);
  687. this.connection.send(msg);
  688. }
  689. /**
  690. * Called when participant leaves.
  691. * @param jid the jid of the participant that leaves
  692. * @param skipEvents optional params to skip any events, including check
  693. * whether this is the focus that left
  694. */
  695. onParticipantLeft(jid, skipEvents) {
  696. delete this.lastPresences[jid];
  697. if (skipEvents) {
  698. return;
  699. }
  700. this.eventEmitter.emit(XMPPEvents.MUC_MEMBER_LEFT, jid);
  701. this.moderator.onMucMemberLeft(jid);
  702. }
  703. /**
  704. *
  705. * @param pres
  706. * @param from
  707. */
  708. onPresenceUnavailable(pres, from) {
  709. // ignore presence
  710. if ($(pres).find('>ignore[xmlns="http://jitsi.org/jitmeet/"]').length) {
  711. return true;
  712. }
  713. // room destroyed ?
  714. if ($(pres).find('>x[xmlns="http://jabber.org/protocol/muc#user"]'
  715. + '>destroy').length) {
  716. let reason;
  717. const reasonSelect
  718. = $(pres).find(
  719. '>x[xmlns="http://jabber.org/protocol/muc#user"]'
  720. + '>destroy>reason');
  721. if (reasonSelect.length) {
  722. reason = reasonSelect.text();
  723. }
  724. this.eventEmitter.emit(XMPPEvents.MUC_DESTROYED, reason);
  725. this.connection.emuc.doLeave(this.roomjid);
  726. return true;
  727. }
  728. // Status code 110 indicates that this notification is "self-presence".
  729. const isSelfPresence
  730. = $(pres)
  731. .find(
  732. '>x[xmlns="http://jabber.org/protocol/muc#user"]>'
  733. + 'status[code="110"]')
  734. .length;
  735. const isKick
  736. = $(pres)
  737. .find(
  738. '>x[xmlns="http://jabber.org/protocol/muc#user"]'
  739. + '>status[code="307"]')
  740. .length;
  741. const membersKeys = Object.keys(this.members);
  742. if (isKick) {
  743. const actorSelect
  744. = $(pres)
  745. .find('>x[xmlns="http://jabber.org/protocol/muc#user"]>item>actor');
  746. let actorNick;
  747. if (actorSelect.length) {
  748. actorNick = actorSelect.attr('nick');
  749. }
  750. // if no member is found this is the case we had kicked someone
  751. // and we are not in the list of members
  752. if (membersKeys.find(jid => Strophe.getResourceFromJid(jid) === actorNick)) {
  753. // we first fire the kicked so we can show the participant
  754. // who kicked, before notifying that participant left
  755. // we fire kicked for us and for any participant kicked
  756. this.eventEmitter.emit(
  757. XMPPEvents.KICKED,
  758. isSelfPresence,
  759. actorNick,
  760. Strophe.getResourceFromJid(from));
  761. }
  762. }
  763. if (!isSelfPresence) {
  764. delete this.members[from];
  765. this.onParticipantLeft(from, false);
  766. } else if (membersKeys.length > 0) {
  767. // If the status code is 110 this means we're leaving and we would
  768. // like to remove everyone else from our view, so we trigger the
  769. // event.
  770. membersKeys.forEach(jid => {
  771. const member = this.members[jid];
  772. delete this.members[jid];
  773. this.onParticipantLeft(jid, member.isFocus);
  774. });
  775. this.connection.emuc.doLeave(this.roomjid);
  776. // we fire muc_left only if this is not a kick,
  777. // kick has both statuses 110 and 307.
  778. if (!isKick) {
  779. this.eventEmitter.emit(XMPPEvents.MUC_LEFT);
  780. }
  781. }
  782. }
  783. /**
  784. *
  785. * @param msg
  786. * @param from
  787. */
  788. onMessage(msg, from) {
  789. const nick
  790. = $(msg).find('>nick[xmlns="http://jabber.org/protocol/nick"]')
  791. .text()
  792. || Strophe.getResourceFromJid(from);
  793. const type = msg.getAttribute('type');
  794. if (type === 'error') {
  795. const errorMsg = $(msg).find('>error>text').text();
  796. this.eventEmitter.emit(XMPPEvents.CHAT_ERROR_RECEIVED, errorMsg);
  797. return true;
  798. }
  799. const txt = $(msg).find('>body').text();
  800. const subject = $(msg).find('>subject');
  801. if (subject.length) {
  802. const subjectText = subject.text();
  803. if (subjectText || subjectText === '') {
  804. this.eventEmitter.emit(XMPPEvents.SUBJECT_CHANGED, subjectText);
  805. logger.log(`Subject is changed to ${subjectText}`);
  806. }
  807. }
  808. // xep-0203 delay
  809. let stamp = $(msg).find('>delay').attr('stamp');
  810. if (!stamp) {
  811. // or xep-0091 delay, UTC timestamp
  812. stamp = $(msg).find('>[xmlns="jabber:x:delay"]').attr('stamp');
  813. if (stamp) {
  814. // the format is CCYYMMDDThh:mm:ss
  815. const dateParts
  816. = stamp.match(/(\d{4})(\d{2})(\d{2}T\d{2}:\d{2}:\d{2})/);
  817. stamp = `${dateParts[1]}-${dateParts[2]}-${dateParts[3]}Z`;
  818. }
  819. }
  820. if (from === this.roomjid
  821. && $(msg).find('>x[xmlns="http://jabber.org/protocol/muc#user"]>status[code="104"]').length) {
  822. this.discoRoomInfo();
  823. }
  824. const jsonMessage = $(msg).find('>json-message').text();
  825. const parsedJson = this.xmpp.tryParseJSONAndVerify(jsonMessage);
  826. // We emit this event if the message is a valid json, and is not
  827. // delivered after a delay, i.e. stamp is undefined.
  828. // e.g. - subtitles should not be displayed if delayed.
  829. if (parsedJson && stamp === undefined) {
  830. this.eventEmitter.emit(XMPPEvents.JSON_MESSAGE_RECEIVED,
  831. from, parsedJson);
  832. return;
  833. }
  834. if (txt) {
  835. if (type === 'chat') {
  836. this.eventEmitter.emit(XMPPEvents.PRIVATE_MESSAGE_RECEIVED,
  837. from, nick, txt, this.myroomjid, stamp);
  838. } else if (type === 'groupchat') {
  839. this.eventEmitter.emit(XMPPEvents.MESSAGE_RECEIVED,
  840. from, nick, txt, this.myroomjid, stamp);
  841. }
  842. }
  843. }
  844. /**
  845. *
  846. * @param pres
  847. * @param from
  848. */
  849. onPresenceError(pres, from) {
  850. if ($(pres)
  851. .find(
  852. '>error[type="auth"]'
  853. + '>not-authorized['
  854. + 'xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"]')
  855. .length) {
  856. logger.log('on password required', from);
  857. this.eventEmitter.emit(XMPPEvents.PASSWORD_REQUIRED);
  858. } else if ($(pres)
  859. .find(
  860. '>error[type="cancel"]'
  861. + '>not-allowed['
  862. + 'xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"]')
  863. .length) {
  864. const toDomain = Strophe.getDomainFromJid(pres.getAttribute('to'));
  865. if (toDomain === this.xmpp.options.hosts.anonymousdomain) {
  866. // enter the room by replying with 'not-authorized'. This would
  867. // result in reconnection from authorized domain.
  868. // We're either missing Jicofo/Prosody config for anonymous
  869. // domains or something is wrong.
  870. this.eventEmitter.emit(XMPPEvents.ROOM_JOIN_ERROR);
  871. } else {
  872. logger.warn('onPresError ', pres);
  873. this.eventEmitter.emit(
  874. XMPPEvents.ROOM_CONNECT_NOT_ALLOWED_ERROR);
  875. }
  876. } else if ($(pres).find('>error>service-unavailable').length) {
  877. logger.warn('Maximum users limit for the room has been reached',
  878. pres);
  879. this.eventEmitter.emit(XMPPEvents.ROOM_MAX_USERS_ERROR);
  880. } else {
  881. logger.warn('onPresError ', pres);
  882. this.eventEmitter.emit(XMPPEvents.ROOM_CONNECT_ERROR);
  883. }
  884. }
  885. /**
  886. *
  887. * @param jid
  888. */
  889. kick(jid) {
  890. const kickIQ = $iq({ to: this.roomjid,
  891. type: 'set' })
  892. .c('query', { xmlns: 'http://jabber.org/protocol/muc#admin' })
  893. .c('item', { nick: Strophe.getResourceFromJid(jid),
  894. role: 'none' })
  895. .c('reason').t('You have been kicked.').up().up().up();
  896. this.connection.sendIQ(
  897. kickIQ,
  898. result => logger.log('Kick participant with jid: ', jid, result),
  899. error => logger.log('Kick participant error: ', error));
  900. }
  901. /* eslint-disable max-params */
  902. /**
  903. *
  904. * @param key
  905. * @param onSuccess
  906. * @param onError
  907. * @param onNotSupported
  908. */
  909. lockRoom(key, onSuccess, onError, onNotSupported) {
  910. // http://xmpp.org/extensions/xep-0045.html#roomconfig
  911. this.connection.sendIQ(
  912. $iq({
  913. to: this.roomjid,
  914. type: 'get'
  915. })
  916. .c('query', { xmlns: 'http://jabber.org/protocol/muc#owner' }),
  917. res => {
  918. if ($(res)
  919. .find(
  920. '>query>x[xmlns="jabber:x:data"]'
  921. + '>field[var="muc#roomconfig_roomsecret"]')
  922. .length) {
  923. const formsubmit
  924. = $iq({
  925. to: this.roomjid,
  926. type: 'set'
  927. })
  928. .c('query', {
  929. xmlns: 'http://jabber.org/protocol/muc#owner'
  930. });
  931. formsubmit.c('x', {
  932. xmlns: 'jabber:x:data',
  933. type: 'submit'
  934. });
  935. formsubmit
  936. .c('field', { 'var': 'FORM_TYPE' })
  937. .c('value')
  938. .t('http://jabber.org/protocol/muc#roomconfig')
  939. .up()
  940. .up();
  941. formsubmit
  942. .c('field', { 'var': 'muc#roomconfig_roomsecret' })
  943. .c('value')
  944. .t(key)
  945. .up()
  946. .up();
  947. // Fixes a bug in prosody 0.9.+
  948. // https://prosody.im/issues/issue/373
  949. formsubmit
  950. .c('field', { 'var': 'muc#roomconfig_whois' })
  951. .c('value')
  952. .t('anyone')
  953. .up()
  954. .up();
  955. // FIXME: is muc#roomconfig_passwordprotectedroom required?
  956. this.connection.sendIQ(formsubmit, onSuccess, onError);
  957. } else {
  958. onNotSupported();
  959. }
  960. },
  961. onError);
  962. }
  963. /* eslint-enable max-params */
  964. /**
  965. *
  966. * @param key
  967. * @param values
  968. */
  969. addToPresence(key, values) {
  970. values.tagName = key;
  971. this.removeFromPresence(key);
  972. this.presMap.nodes.push(values);
  973. }
  974. /**
  975. * Retreives a value from the presence map.
  976. *
  977. * @param {string} key - The key to find the value for.
  978. * @returns {Object?}
  979. */
  980. getFromPresence(key) {
  981. return this.presMap.nodes.find(node => key === node.tagName);
  982. }
  983. /**
  984. *
  985. * @param key
  986. */
  987. removeFromPresence(key) {
  988. const nodes = this.presMap.nodes.filter(node => key !== node.tagName);
  989. this.presMap.nodes = nodes;
  990. }
  991. /**
  992. *
  993. * @param name
  994. * @param handler
  995. */
  996. addPresenceListener(name, handler) {
  997. if (typeof handler !== 'function') {
  998. throw new Error('"handler" is not a function');
  999. }
  1000. let tagHandlers = this.presHandlers[name];
  1001. if (!tagHandlers) {
  1002. this.presHandlers[name] = tagHandlers = [];
  1003. }
  1004. if (tagHandlers.indexOf(handler) === -1) {
  1005. tagHandlers.push(handler);
  1006. } else {
  1007. logger.warn(
  1008. `Trying to add the same handler more than once for: ${name}`);
  1009. }
  1010. }
  1011. /**
  1012. *
  1013. * @param name
  1014. * @param handler
  1015. */
  1016. removePresenceListener(name, handler) {
  1017. const tagHandlers = this.presHandlers[name];
  1018. const handlerIdx = tagHandlers ? tagHandlers.indexOf(handler) : -1;
  1019. // eslint-disable-next-line no-negated-condition
  1020. if (handlerIdx !== -1) {
  1021. tagHandlers.splice(handlerIdx, 1);
  1022. } else {
  1023. logger.warn(`Handler for: ${name} was not registered`);
  1024. }
  1025. }
  1026. /**
  1027. * Checks if the user identified by given <tt>mucJid</tt> is the conference
  1028. * focus.
  1029. * @param mucJid the full MUC address of the user to be checked.
  1030. * @returns {boolean|null} <tt>true</tt> if MUC user is the conference focus
  1031. * or <tt>false</tt> if is not. When given <tt>mucJid</tt> does not exist in
  1032. * the MUC then <tt>null</tt> is returned.
  1033. */
  1034. isFocus(mucJid) {
  1035. const member = this.members[mucJid];
  1036. if (member) {
  1037. return member.isFocus;
  1038. }
  1039. return null;
  1040. }
  1041. /**
  1042. *
  1043. */
  1044. isModerator() {
  1045. return this.role === 'moderator';
  1046. }
  1047. /**
  1048. *
  1049. * @param peerJid
  1050. */
  1051. getMemberRole(peerJid) {
  1052. if (this.members[peerJid]) {
  1053. return this.members[peerJid].role;
  1054. }
  1055. return null;
  1056. }
  1057. /**
  1058. *
  1059. * @param mute
  1060. * @param callback
  1061. */
  1062. setVideoMute(mute, callback) {
  1063. this.sendVideoInfoPresence(mute);
  1064. if (callback) {
  1065. callback(mute);
  1066. }
  1067. }
  1068. /**
  1069. *
  1070. * @param mute
  1071. * @param callback
  1072. */
  1073. setAudioMute(mute, callback) {
  1074. return this.sendAudioInfoPresence(mute, callback);
  1075. }
  1076. /**
  1077. *
  1078. * @param mute
  1079. */
  1080. addAudioInfoToPresence(mute) {
  1081. this.removeFromPresence('audiomuted');
  1082. this.addToPresence(
  1083. 'audiomuted',
  1084. {
  1085. attributes: { 'xmlns': 'http://jitsi.org/jitmeet/audio' },
  1086. value: mute.toString()
  1087. });
  1088. }
  1089. /**
  1090. *
  1091. * @param mute
  1092. * @param callback
  1093. */
  1094. sendAudioInfoPresence(mute, callback) {
  1095. this.addAudioInfoToPresence(mute);
  1096. if (this.connection) {
  1097. this.sendPresence();
  1098. }
  1099. if (callback) {
  1100. callback();
  1101. }
  1102. }
  1103. /**
  1104. *
  1105. * @param mute
  1106. */
  1107. addVideoInfoToPresence(mute) {
  1108. this.removeFromPresence('videomuted');
  1109. this.addToPresence(
  1110. 'videomuted',
  1111. {
  1112. attributes: { 'xmlns': 'http://jitsi.org/jitmeet/video' },
  1113. value: mute.toString()
  1114. });
  1115. }
  1116. /**
  1117. *
  1118. * @param mute
  1119. */
  1120. sendVideoInfoPresence(mute) {
  1121. this.addVideoInfoToPresence(mute);
  1122. if (!this.connection) {
  1123. return;
  1124. }
  1125. this.sendPresence();
  1126. }
  1127. /**
  1128. * Obtains the info about given media advertised in the MUC presence of
  1129. * the participant identified by the given endpoint JID.
  1130. * @param {string} endpointId the endpoint ID mapped to the participant
  1131. * which corresponds to MUC nickname.
  1132. * @param {MediaType} mediaType the type of the media for which presence
  1133. * info will be obtained.
  1134. * @return {PeerMediaInfo} presenceInfo an object with media presence
  1135. * info or <tt>null</tt> either if there is no presence available or if
  1136. * the media type given is invalid.
  1137. */
  1138. getMediaPresenceInfo(endpointId, mediaType) {
  1139. // Will figure out current muted status by looking up owner's presence
  1140. const pres = this.lastPresences[`${this.roomjid}/${endpointId}`];
  1141. if (!pres) {
  1142. // No presence available
  1143. return null;
  1144. }
  1145. const data = {
  1146. muted: false, // unmuted by default
  1147. videoType: undefined // no video type by default
  1148. };
  1149. let mutedNode = null;
  1150. if (mediaType === MediaType.AUDIO) {
  1151. mutedNode = filterNodeFromPresenceJSON(pres, 'audiomuted');
  1152. } else if (mediaType === MediaType.VIDEO) {
  1153. mutedNode = filterNodeFromPresenceJSON(pres, 'videomuted');
  1154. const videoTypeNode = filterNodeFromPresenceJSON(pres, 'videoType');
  1155. if (videoTypeNode.length > 0) {
  1156. data.videoType = videoTypeNode[0].value;
  1157. }
  1158. } else {
  1159. logger.error(`Unsupported media type: ${mediaType}`);
  1160. return null;
  1161. }
  1162. data.muted = mutedNode.length > 0 && mutedNode[0].value === 'true';
  1163. return data;
  1164. }
  1165. /**
  1166. * Returns true if the SIP calls are supported and false otherwise
  1167. */
  1168. isSIPCallingSupported() {
  1169. if (this.moderator) {
  1170. return this.moderator.isSipGatewayEnabled();
  1171. }
  1172. return false;
  1173. }
  1174. /**
  1175. * Dials a number.
  1176. * @param number the number
  1177. */
  1178. dial(number) {
  1179. return this.connection.rayo.dial(number, 'fromnumber',
  1180. Strophe.getBareJidFromJid(this.myroomjid), this.password,
  1181. this.focusMucJid);
  1182. }
  1183. /**
  1184. * Hangup an existing call
  1185. */
  1186. hangup() {
  1187. return this.connection.rayo.hangup();
  1188. }
  1189. /**
  1190. * Returns the phone number for joining the conference.
  1191. */
  1192. getPhoneNumber() {
  1193. return this.phoneNumber;
  1194. }
  1195. /**
  1196. * Returns the pin for joining the conference with phone.
  1197. */
  1198. getPhonePin() {
  1199. return this.phonePin;
  1200. }
  1201. /**
  1202. * Returns the meeting unique ID if any came from backend.
  1203. *
  1204. * @returns {string} - The meeting ID.
  1205. */
  1206. getMeetingId() {
  1207. return this.meetingId;
  1208. }
  1209. /**
  1210. * Mutes remote participant.
  1211. * @param jid of the participant
  1212. * @param mute
  1213. */
  1214. muteParticipant(jid, mute) {
  1215. logger.info('set mute', mute);
  1216. const iqToFocus = $iq(
  1217. { to: this.focusMucJid,
  1218. type: 'set' })
  1219. .c('mute', {
  1220. xmlns: 'http://jitsi.org/jitmeet/audio',
  1221. jid
  1222. })
  1223. .t(mute.toString())
  1224. .up();
  1225. this.connection.sendIQ(
  1226. iqToFocus,
  1227. result => logger.log('set mute', result),
  1228. error => logger.log('set mute error', error));
  1229. }
  1230. /**
  1231. * TODO: Document
  1232. * @param iq
  1233. */
  1234. onMute(iq) {
  1235. const from = iq.getAttribute('from');
  1236. if (from !== this.focusMucJid) {
  1237. logger.warn('Ignored mute from non focus peer');
  1238. return;
  1239. }
  1240. const mute = $(iq).find('mute');
  1241. if (mute.length && mute.text() === 'true') {
  1242. this.eventEmitter.emit(XMPPEvents.AUDIO_MUTED_BY_FOCUS, mute.attr('actor'));
  1243. } else {
  1244. // XXX Why do we support anything but muting? Why do we encode the
  1245. // value in the text of the element? Why do we use a separate XML
  1246. // namespace?
  1247. logger.warn('Ignoring a mute request which does not explicitly '
  1248. + 'specify a positive mute command.');
  1249. }
  1250. }
  1251. /**
  1252. * Leaves the room. Closes the jingle session.
  1253. * @returns {Promise} which is resolved if XMPPEvents.MUC_LEFT is received
  1254. * less than 5s after sending presence unavailable. Otherwise the promise is
  1255. * rejected.
  1256. */
  1257. leave() {
  1258. return new Promise((resolve, reject) => {
  1259. const timeout = setTimeout(() => onMucLeft(true), 5000);
  1260. const eventEmitter = this.eventEmitter;
  1261. /**
  1262. *
  1263. * @param doReject
  1264. */
  1265. function onMucLeft(doReject = false) {
  1266. eventEmitter.removeListener(XMPPEvents.MUC_LEFT, onMucLeft);
  1267. clearTimeout(timeout);
  1268. if (doReject) {
  1269. // the timeout expired
  1270. reject(new Error('The timeout for the confirmation about '
  1271. + 'leaving the room expired.'));
  1272. } else {
  1273. resolve();
  1274. }
  1275. }
  1276. eventEmitter.on(XMPPEvents.MUC_LEFT, onMucLeft);
  1277. this.doLeave();
  1278. });
  1279. }
  1280. }
  1281. /* eslint-enable newline-per-chained-call */