muc.js 142 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587
  1. /*global mock, converse */
  2. const { $pres, Strophe, Promise, sizzle, stx, u } = converse.env;
  3. describe("Groupchats", function () {
  4. beforeAll(() => jasmine.addMatchers({ toEqualStanza: jasmine.toEqualStanza }));
  5. describe("An instant groupchat", function () {
  6. it("will be created when muc_instant_rooms is set to true",
  7. mock.initConverse(['chatBoxesFetched'], { vcard: { nickname: '' } }, async function (_converse) {
  8. let IQ_stanzas = _converse.api.connection.get().IQ_stanzas;
  9. const { api } = _converse;
  10. const muc_jid = 'lounge@montague.lit';
  11. const nick = 'nicky';
  12. const promise = api.rooms.open(muc_jid);
  13. await mock.waitForMUCDiscoInfo(_converse, muc_jid);
  14. await mock.waitForReservedNick(_converse, muc_jid, '');
  15. const muc = await promise;
  16. await muc.initialized;
  17. spyOn(muc, 'join').and.callThrough();
  18. const view = _converse.chatboxviews.get(muc_jid);
  19. const input = await u.waitUntil(() => view.querySelector('input[name="nick"]'), 1000);
  20. expect(view.model.session.get('connection_status')).toBe(converse.ROOMSTATUS.NICKNAME_REQUIRED);
  21. input.value = nick;
  22. view.querySelector('input[type=submit]').click();
  23. expect(view.model.join).toHaveBeenCalled();
  24. await mock.waitForNewMUCDiscoInfo(_converse, muc_jid);
  25. _converse.api.connection.get().IQ_stanzas = [];
  26. await u.waitUntil(() => view.model.session.get('connection_status') === converse.ROOMSTATUS.CONNECTING);
  27. // The user has just entered the room (because join was called)
  28. // and receives their own presence from the server.
  29. // See example 24:
  30. // https://xmpp.org/extensions/xep-0045.html#enter-pres
  31. const presence =
  32. stx`<presence
  33. id="5025e055-036c-4bc5-a227-706e7e352053"
  34. to="romeo@montague.lit/orchard"
  35. from="lounge@montague.lit/nicky"
  36. xmlns="jabber:client">
  37. <x xmlns="http://jabber.org/protocol/muc#user">
  38. <item affiliation="owner" jid="romeo@montague.lit/orchard" role="moderator"/>
  39. <status code="110"/>
  40. <status code="201"/>
  41. </x>
  42. </presence>`;
  43. _converse.api.connection.get()._dataRecv(mock.createRequest(presence));
  44. await u.waitUntil(() => view.model.session.get('connection_status') === converse.ROOMSTATUS.ENTERED);
  45. await mock.returnMemberLists(_converse, muc_jid);
  46. const num_info_msgs = await u.waitUntil(() => view.querySelectorAll('.chat-content .chat-info').length);
  47. expect(num_info_msgs).toBe(1);
  48. const info_texts = Array.from(view.querySelectorAll('.chat-content .chat-info')).map(e => e.textContent.trim());
  49. expect(info_texts[0]).toBe('A new groupchat has been created');
  50. const csntext = await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent);
  51. expect(csntext.trim()).toEqual("nicky has entered the groupchat");
  52. // An instant room is created by saving the default configuratoin.
  53. //
  54. /* <iq to="myroom@conference.chat.example.org" type="set" xmlns="jabber:client" id="5025e055-036c-4bc5-a227-706e7e352053:sendIQ">
  55. * <query xmlns="http://jabber.org/protocol/muc#owner"><x xmlns="jabber:x:data" type="submit"/></query>
  56. * </iq>
  57. */
  58. const selector = `query[xmlns="${Strophe.NS.MUC_OWNER}"]`;
  59. IQ_stanzas = _converse.api.connection.get().IQ_stanzas;
  60. const iq = await u.waitUntil(() => IQ_stanzas.filter(s => s.querySelector(selector)).pop());
  61. expect(Strophe.serialize(iq)).toBe(
  62. `<iq id="${iq.getAttribute('id')}" to="lounge@montague.lit" type="set" xmlns="jabber:client">`+
  63. `<query xmlns="http://jabber.org/protocol/muc#owner"><x type="submit" xmlns="jabber:x:data"/>`+
  64. `</query></iq>`);
  65. }));
  66. });
  67. describe("A Groupchat", function () {
  68. it("will be visible when opened as the first chat in fullscreen-view",
  69. mock.initConverse(['discoInitialized'],
  70. { 'view_mode': 'fullscreen', 'auto_join_rooms': ['orchard@chat.shakespeare.lit']},
  71. async function (_converse) {
  72. const { api } = _converse;
  73. const muc_jid = 'orchard@chat.shakespeare.lit';
  74. api.rooms.get(muc_jid);
  75. await mock.waitForMUCDiscoInfo(_converse, muc_jid);
  76. await mock.waitForReservedNick(_converse, muc_jid, 'romeo');
  77. await mock.receiveOwnMUCPresence(_converse, muc_jid, 'romeo');
  78. await api.waitUntil('roomsAutoJoined');
  79. const room = await u.waitUntil(() => _converse.chatboxes.get(muc_jid));
  80. expect(room.get('hidden')).toBe(false);
  81. }));
  82. it("Can be configured to show cached messages before being joined",
  83. mock.initConverse(['discoInitialized'],
  84. {
  85. muc_show_logs_before_join: true,
  86. archived_messages_page_size: 2,
  87. muc_nickname_from_jid: false,
  88. muc_clear_messages_on_leave: false,
  89. vcard: { nickname: '' },
  90. }, async function (_converse) {
  91. const { api } = _converse;
  92. const muc_jid = 'orchard@chat.shakespeare.lit';
  93. const nick = 'romeo';
  94. api.rooms.open(muc_jid);
  95. await mock.waitForMUCDiscoInfo(_converse, muc_jid);
  96. await mock.waitForReservedNick(_converse, muc_jid);
  97. const view = await u.waitUntil(() => _converse.chatboxviews.get(muc_jid));
  98. await view.model.messages.fetched;
  99. view.model.messages.create({
  100. 'type': 'groupchat',
  101. 'to': muc_jid,
  102. 'from': `${_converse.bare_jid}/orchard`,
  103. 'body': 'Hello world',
  104. 'message': 'Hello world',
  105. 'time': '2021-02-02T12:00:00Z'
  106. });
  107. expect(view.model.session.get('connection_status')).toBe(converse.ROOMSTATUS.NICKNAME_REQUIRED);
  108. await u.waitUntil(() => view.querySelectorAll('converse-chat-message').length === 1);
  109. const sel = 'converse-message-history converse-chat-message .chat-msg__text';
  110. await u.waitUntil(() => view.querySelector(sel)?.textContent.trim());
  111. expect(view.querySelector(sel).textContent.trim()).toBe('Hello world')
  112. const nick_input = await u.waitUntil(() => view.querySelector('[name="nick"]'));
  113. nick_input.value = nick;
  114. view.querySelector('.muc-nickname-form input[type="submit"]').click();
  115. _converse.api.connection.get().IQ_stanzas = [];
  116. await mock.waitForMUCDiscoInfo(_converse, muc_jid);
  117. await u.waitUntil(() => view.model.session.get('connection_status') === converse.ROOMSTATUS.CONNECTING);
  118. await mock.receiveOwnMUCPresence(_converse, muc_jid, nick);
  119. }));
  120. it("maintains its state across reloads",
  121. mock.initConverse([], {
  122. clear_messages_on_reconnection: true,
  123. enable_smacks: false
  124. }, async function (_converse) {
  125. const { api } = _converse;
  126. const nick = 'romeo';
  127. const sent_IQs = _converse.api.connection.get().IQ_stanzas;
  128. const muc_jid = 'lounge@montague.lit'
  129. await mock.openAndEnterMUC(_converse, muc_jid, nick, [], []);
  130. const view = _converse.chatboxviews.get(muc_jid);
  131. let iq_get = await u.waitUntil(() => sent_IQs.filter((iq) => sizzle(`query[xmlns="${Strophe.NS.MAM}"]`, iq).length).pop());
  132. expect(iq_get).toEqualStanza(stx`
  133. <iq id="${iq_get.getAttribute('id')}" to="${muc_jid}" type="set" xmlns="jabber:client">
  134. <query queryid="${iq_get.querySelector('query').getAttribute('queryid')}" xmlns="${Strophe.NS.MAM}">
  135. <set xmlns="http://jabber.org/protocol/rsm"><before></before><max>50</max></set>
  136. </query>
  137. </iq>`);
  138. const first_msg_id = _converse.api.connection.get().getUniqueId();
  139. const last_msg_id = _converse.api.connection.get().getUniqueId();
  140. _converse.api.connection.get()._dataRecv(mock.createRequest(
  141. stx`<message xmlns="jabber:client"
  142. to="romeo@montague.lit/orchard"
  143. from="${muc_jid}">
  144. <result xmlns="urn:xmpp:mam:2" queryid="${iq_get.querySelector('query').getAttribute('queryid')}" id="${first_msg_id}">
  145. <forwarded xmlns="urn:xmpp:forward:0">
  146. <delay xmlns="urn:xmpp:delay" stamp="2018-01-09T06:15:23Z"/>
  147. <message from="${muc_jid}/some1" type="groupchat">
  148. <body>1st Message</body>
  149. </message>
  150. </forwarded>
  151. </result>
  152. </message>`));
  153. let message = stx`<message xmlns="jabber:client"
  154. to="romeo@montague.lit/orchard"
  155. from="${muc_jid}">
  156. <result xmlns="urn:xmpp:mam:2" queryid="${iq_get.querySelector('query').getAttribute('queryid')}" id="${last_msg_id}">
  157. <forwarded xmlns="urn:xmpp:forward:0">
  158. <delay xmlns="urn:xmpp:delay" stamp="2018-01-09T06:16:23Z"/>
  159. <message from="${muc_jid}/some1" type="groupchat">
  160. <body>2nd Message</body>
  161. </message>
  162. </forwarded>
  163. </result>
  164. </message>`;
  165. _converse.api.connection.get()._dataRecv(mock.createRequest(message));
  166. const result = stx`<iq type='result' id='${iq_get.getAttribute('id')}' xmlns="jabber:client">
  167. <fin xmlns='urn:xmpp:mam:2' complete="true">
  168. <set xmlns='http://jabber.org/protocol/rsm'>
  169. <first index='0'>${first_msg_id}</first>
  170. <last>${last_msg_id}</last>
  171. <count>2</count>
  172. </set>
  173. </fin>
  174. </iq>`;
  175. _converse.api.connection.get()._dataRecv(mock.createRequest(result));
  176. await u.waitUntil(() => view.querySelectorAll('.chat-msg__text').length === 2);
  177. while (sent_IQs.length) { sent_IQs.pop(); } // Clear so that we don't match the older query
  178. await _converse.api.connection.reconnect();
  179. await mock.waitForMUCDiscoInfo(_converse, muc_jid, []);
  180. await u.waitUntil(() => (view.model.session.get('connection_status') === converse.ROOMSTATUS.CONNECTING));
  181. // The user has just entered the room (because join was called)
  182. // and receives their own presence from the server.
  183. // See example 24: https://xmpp.org/extensions/xep-0045.html#enter-pres
  184. await mock.receiveOwnMUCPresence(_converse, muc_jid, nick);
  185. message = stx`
  186. <message xmlns="jabber:client" type="groupchat" id="918172de-d5c5-4da4-b388-446ef4a05bec" to="${_converse.jid}" xml:lang="en" from="${muc_jid}/juliet">
  187. <body>Wherefore art though?</body>
  188. <active xmlns="http://jabber.org/protocol/chatstates"/>
  189. <origin-id xmlns="urn:xmpp:sid:0" id="918172de-d5c5-4da4-b388-446ef4a05bec"/>
  190. <stanza-id xmlns="urn:xmpp:sid:0" id="88cc9c93-a8f4-4dd5-b02a-d19855eb6303" by="${muc_jid}"/>
  191. <delay xmlns="urn:xmpp:delay" stamp="2020-07-14T17:46:47Z" from="juliet@shakespeare.lit"/>
  192. </message>`;
  193. _converse.api.connection.get()._dataRecv(mock.createRequest(message));
  194. message = stx`
  195. <message xmlns="jabber:client" type="groupchat" id="awQo6a-mi-Wa6NYh" to="${_converse.jid}" from="${muc_jid}/ews000" xml:lang="en">
  196. <composing xmlns="http://jabber.org/protocol/chatstates"/>
  197. <no-store xmlns="urn:xmpp:hints"/>
  198. <no-permanent-store xmlns="urn:xmpp:hints"/>
  199. <delay xmlns="urn:xmpp:delay" stamp="2020-07-14T17:46:54Z" from="juliet@shakespeare.lit"/>
  200. </message>`;
  201. _converse.api.connection.get()._dataRecv(mock.createRequest(message));
  202. const affs = api.settings.get('muc_fetch_members');
  203. const all_affiliations = Array.isArray(affs) ? affs : (affs ? ['member', 'admin', 'owner'] : []);
  204. await mock.returnMemberLists(_converse, muc_jid, [], all_affiliations);
  205. iq_get = await u.waitUntil(() => sent_IQs.filter(iq => sizzle(`query[xmlns="${Strophe.NS.MAM}"]`, iq).length).pop());
  206. expect(iq_get).toEqualStanza(stx`
  207. <iq id="${iq_get.getAttribute('id')}" to="${muc_jid}" type="set" xmlns="jabber:client">
  208. <query queryid="${iq_get.querySelector('query').getAttribute('queryid')}" xmlns="${Strophe.NS.MAM}">
  209. <x xmlns="jabber:x:data" type="submit">
  210. <field type="hidden" var="FORM_TYPE"><value>urn:xmpp:mam:2</value></field>
  211. <field var="start"><value>2020-07-14T17:46:47.000Z</value></field>
  212. </x>
  213. <set xmlns="http://jabber.org/protocol/rsm"><before></before><max>50</max></set>
  214. </query>
  215. </iq>`);
  216. }));
  217. it("shows a new messages indicator when you're scrolled up",
  218. mock.initConverse(['chatBoxesFetched'], {}, async function (_converse) {
  219. const muc_jid = 'lounge@montague.lit';
  220. await mock.openAndEnterMUC(_converse, muc_jid, 'romeo');
  221. const view = _converse.chatboxviews.get(muc_jid);
  222. const message = stx`
  223. <message xmlns="jabber:client" type="groupchat" id="918172de-d5c5-4da4-b388-446ef4a05bec" to="${_converse.jid}" xml:lang="en" from="${muc_jid}/juliet">
  224. <body>Wherefore art though?</body>
  225. <active xmlns="http://jabber.org/protocol/chatstates"/>
  226. <origin-id xmlns="urn:xmpp:sid:0" id="918172de-d5c5-4da4-b388-446ef4a05bec"/>
  227. <stanza-id xmlns="urn:xmpp:sid:0" id="88cc9c93-a8f4-4dd5-b02a-d19855eb6303" by="${muc_jid}"/>
  228. <delay xmlns="urn:xmpp:delay" stamp="2020-07-14T17:46:47Z" from="juliet@shakespeare.lit"/>
  229. </message>`;
  230. view.model.ui.set('scrolled', true); // hack
  231. _converse.api.connection.get()._dataRecv(mock.createRequest(message));
  232. await u.waitUntil(() => view.model.messages.length);
  233. const chat_new_msgs_indicator = await u.waitUntil(() => view.querySelector('.new-msgs-indicator'));
  234. chat_new_msgs_indicator.click();
  235. expect(view.model.ui.get('scrolled')).toBeFalsy();
  236. await u.waitUntil(() => !u.isVisible(chat_new_msgs_indicator));
  237. }));
  238. describe("topic", function () {
  239. it("is shown the header", mock.initConverse([], {}, async function (_converse) {
  240. await mock.openAndEnterMUC(_converse, 'jdev@conference.jabber.org', 'jc');
  241. const text = 'Jabber/XMPP Development | RFCs and Extensions: https://xmpp.org/ | Protocol and XSF discussions: xsf@muc.xmpp.org';
  242. let stanza = stx`
  243. <message xmlns="jabber:client" to="${_converse.jid}" type="groupchat" from="jdev@conference.jabber.org/ralphm">
  244. <subject>${text}</subject>
  245. <delay xmlns="urn:xmpp:delay" stamp="2014-02-04T09:35:39Z" from="jdev@conference.jabber.org"/>
  246. <x xmlns="jabber:x:delay" stamp="20140204T09:35:39" from="jdev@conference.jabber.org"/>
  247. </message>`;
  248. _converse.api.connection.get()._dataRecv(mock.createRequest(stanza));
  249. const view = _converse.chatboxviews.get('jdev@conference.jabber.org');
  250. await new Promise(resolve => view.model.once('change:subject', resolve));
  251. const head_desc = await u.waitUntil(() => view.querySelector('.chat-head__desc'), 1000);
  252. expect(head_desc?.textContent.trim()).toBe(text);
  253. stanza = stx`<message xmlns="jabber:client" to="${_converse.jid}" type="groupchat" from="jdev@conference.jabber.org/ralphm">
  254. <subject>This is a message subject</subject>
  255. <body>This is a message</body>
  256. </message>`;
  257. _converse.api.connection.get()._dataRecv(mock.createRequest(stanza));
  258. await u.waitUntil(() => view.querySelectorAll('.chat-msg__text').length);
  259. expect(sizzle('.chat-msg__subject', view).length).toBe(1);
  260. expect(sizzle('.chat-msg__subject', view).pop().textContent.trim()).toBe('This is a message subject');
  261. expect(sizzle('.chat-msg__text').length).toBe(1);
  262. expect(sizzle('.chat-msg__text').pop().textContent.trim()).toBe('This is a message');
  263. expect(view.querySelector('.chat-head__desc').textContent.trim()).toBe(text);
  264. }));
  265. it("can be toggled by the user", mock.initConverse([], {}, async function (_converse) {
  266. await mock.openAndEnterMUC(_converse, 'jdev@conference.jabber.org', 'jc');
  267. const text = 'Jabber/XMPP Development | RFCs and Extensions: https://xmpp.org/ | Protocol and XSF discussions: xsf@muc.xmpp.org';
  268. let stanza = stx`
  269. <message xmlns="jabber:client" to="${_converse.jid}" type="groupchat" from="jdev@conference.jabber.org/ralphm">
  270. <subject>${text}</subject>
  271. <delay xmlns="urn:xmpp:delay" stamp="2014-02-04T09:35:39Z" from="jdev@conference.jabber.org"/>
  272. <x xmlns="jabber:x:delay" stamp="20140204T09:35:39" from="jdev@conference.jabber.org"/>
  273. </message>`;
  274. _converse.api.connection.get()._dataRecv(mock.createRequest(stanza));
  275. const view = _converse.chatboxviews.get('jdev@conference.jabber.org');
  276. await new Promise(resolve => view.model.once('change:subject', resolve));
  277. const head_desc = await u.waitUntil(() => view.querySelector('.chat-head__desc'));
  278. expect(head_desc?.textContent.trim()).toBe(text);
  279. stanza = stx`<message xmlns="jabber:client" to="${_converse.jid}" type="groupchat" from="jdev@conference.jabber.org/ralphm">
  280. <subject>This is a message subject</subject>
  281. <body>This is a message</body>
  282. </message>`;
  283. _converse.api.connection.get()._dataRecv(mock.createRequest(stanza));
  284. await u.waitUntil(() => view.querySelectorAll('.chat-msg__text').length);
  285. expect(sizzle('.chat-msg__subject', view).length).toBe(1);
  286. expect(sizzle('.chat-msg__subject', view).pop().textContent.trim()).toBe('This is a message subject');
  287. expect(sizzle('.chat-msg__text').length).toBe(1);
  288. expect(sizzle('.chat-msg__text').pop().textContent.trim()).toBe('This is a message');
  289. const topic_el = view.querySelector('.chat-head__desc');
  290. expect(topic_el.textContent.trim()).toBe(text);
  291. expect(u.isVisible(topic_el)).toBe(true);
  292. await u.waitUntil(() => view.querySelector('.hide-topic').textContent.trim() === 'Hide topic');
  293. const toggle = view.querySelector('.hide-topic');
  294. expect(toggle.textContent.trim()).toBe('Hide topic');
  295. toggle.click();
  296. await u.waitUntil(() => view.querySelector('.hide-topic').textContent.trim() === 'Show topic');
  297. }));
  298. it("will always be shown when it's new", mock.initConverse([], {}, async function (_converse) {
  299. await mock.openAndEnterMUC(_converse, 'jdev@conference.jabber.org', 'jc');
  300. const text = 'Jabber/XMPP Development | RFCs and Extensions: https://xmpp.org/ | Protocol and XSF discussions: xsf@muc.xmpp.org';
  301. let stanza = stx`<message xmlns="jabber:client" to="${_converse.jid}" type="groupchat" from="jdev@conference.jabber.org/ralphm">
  302. <subject>${text}</subject>
  303. </message>`;
  304. _converse.api.connection.get()._dataRecv(mock.createRequest(stanza));
  305. const view = _converse.chatboxviews.get('jdev@conference.jabber.org');
  306. await new Promise(resolve => view.model.once('change:subject', resolve));
  307. const head_desc = await u.waitUntil(() => view.querySelector('.chat-head__desc'));
  308. expect(head_desc?.textContent.trim()).toBe(text);
  309. let topic_el = view.querySelector('.chat-head__desc');
  310. expect(topic_el.textContent.trim()).toBe(text);
  311. expect(u.isVisible(topic_el)).toBe(true);
  312. const toggle = view.querySelector('.hide-topic');
  313. expect(toggle.textContent.trim()).toBe('Hide topic');
  314. toggle.click();
  315. await u.waitUntil(() => !u.isVisible(topic_el));
  316. stanza = stx`<message xmlns="jabber:client" to="${_converse.jid}" type="groupchat" from="jdev@conference.jabber.org/ralphm">
  317. <subject>Another topic</subject>
  318. </message>`;
  319. _converse.api.connection.get()._dataRecv(mock.createRequest(stanza));
  320. await u.waitUntil(() => u.isVisible(view.querySelector('.chat-head__desc')));
  321. topic_el = view.querySelector('.chat-head__desc');
  322. expect(topic_el.textContent.trim()).toBe('Another topic');
  323. }));
  324. it("causes an info message to be shown when received in real-time", mock.initConverse([], {}, async function (_converse) {
  325. spyOn(_converse.ChatRoom.prototype, 'handleSubjectChange').and.callThrough();
  326. await mock.openAndEnterMUC(_converse, 'jdev@conference.jabber.org', 'romeo');
  327. const view = _converse.chatboxviews.get('jdev@conference.jabber.org');
  328. _converse.api.connection.get()._dataRecv(mock.createRequest(stx`
  329. <message xmlns="jabber:client" to="${_converse.jid}" type="groupchat" from="jdev@conference.jabber.org/ralphm">
  330. <subject>This is an older topic</subject>
  331. <delay xmlns="urn:xmpp:delay" stamp="2014-02-04T09:35:39Z" from="jdev@conference.jabber.org"/>
  332. <x xmlns="jabber:x:delay" stamp="20140204T09:35:39" from="jdev@conference.jabber.org"/>
  333. </message>`));
  334. await u.waitUntil(() => view.model.handleSubjectChange.calls.count());
  335. expect(sizzle('.chat-info__message', view).length).toBe(0);
  336. const desc = await u.waitUntil(() => view.querySelector('.chat-head__desc'));
  337. expect(desc.textContent.trim()).toBe('This is an older topic');
  338. _converse.api.connection.get()._dataRecv(mock.createRequest(stx`
  339. <message xmlns="jabber:client" to="${_converse.jid}" type="groupchat" from="jdev@conference.jabber.org/ralphm">
  340. <subject>This is a new topic</subject>
  341. </message>`));
  342. await u.waitUntil(() => view.model.handleSubjectChange.calls.count() === 2);
  343. await u.waitUntil(() => sizzle('.chat-info__message', view).pop()?.textContent.trim() === 'Topic set by ralphm');
  344. await u.waitUntil(() => desc.textContent.trim() === 'This is a new topic');
  345. // Doesn't show multiple subsequent topic change notifications
  346. _converse.api.connection.get()._dataRecv(mock.createRequest(stx`
  347. <message xmlns="jabber:client" to="${_converse.jid}" type="groupchat" from="jdev@conference.jabber.org/ralphm">
  348. <subject>Yet another topic</subject>
  349. </message>`));
  350. await u.waitUntil(() => view.model.handleSubjectChange.calls.count() === 3);
  351. await u.waitUntil(() => desc.textContent.trim() === 'Yet another topic');
  352. expect(sizzle('.chat-info__message', view).length).toBe(1);
  353. // Sow multiple subsequent topic change notification from someone else
  354. _converse.api.connection.get()._dataRecv(mock.createRequest(stx`
  355. <message xmlns="jabber:client" to="${_converse.jid}" type="groupchat" from="jdev@conference.jabber.org/some1">
  356. <subject>Some1's topic</subject>
  357. </message>`));
  358. await u.waitUntil(() => view.model.handleSubjectChange.calls.count() === 4);
  359. await u.waitUntil(() => desc.textContent.trim() === "Some1's topic");
  360. expect(sizzle('.chat-info__message', view).length).toBe(2);
  361. const el = sizzle('.chat-info__message', view).pop();
  362. expect(el.textContent.trim()).toBe('Topic set by some1');
  363. // Removes current topic
  364. const stanza = stx`<message xmlns="jabber:client" to="${_converse.jid}" type="groupchat" from="jdev@conference.jabber.org/some1">
  365. <subject/>
  366. </message>`;
  367. _converse.api.connection.get()._dataRecv(mock.createRequest(stanza));
  368. await u.waitUntil(() => view.model.handleSubjectChange.calls.count() === 5);
  369. await u.waitUntil(() => view.querySelector('.chat-head__desc') === null);
  370. await u.waitUntil(() => view.querySelector('converse-chat-message:last-child .chat-info').textContent.trim() === "Topic cleared by some1");
  371. }));
  372. });
  373. it("restores cached messages when it reconnects and clear_messages_on_reconnection and muc_clear_messages_on_leave are false",
  374. mock.initConverse([], {
  375. 'clear_messages_on_reconnection': false,
  376. 'muc_clear_messages_on_leave': false
  377. },
  378. async function (_converse) {
  379. const muc_jid = 'lounge@montague.lit';
  380. await mock.openAndEnterMUC(_converse, muc_jid , 'romeo');
  381. const model = _converse.chatboxes.get(muc_jid);
  382. const message = 'Hello world';
  383. const nick = mock.chatroom_names[0];
  384. const msg =
  385. stx`<message from="lounge@montague.lit/${nick}"
  386. id="${u.getUniqueId()}"
  387. to="romeo@montague.lit"
  388. type="groupchat"
  389. xmlns="jabber:client">
  390. <body>${message}</body>
  391. </message>`;
  392. await model.handleMessageStanza(msg);
  393. await u.waitUntil(() => document.querySelector('converse-chat-message'));
  394. await model.close();
  395. await u.waitUntil(() => !document.querySelector('converse-chat-message'));
  396. _converse.api.connection.get().IQ_stanzas = [];
  397. await mock.openAndEnterMUC(_converse, muc_jid , 'romeo');
  398. await u.waitUntil(() => document.querySelector('converse-chat-message'));
  399. expect(model.messages.length).toBe(1);
  400. expect(document.querySelectorAll('converse-chat-message').length).toBe(1);
  401. }));
  402. it("clears cached messages when it reconnects and clear_messages_on_reconnection is true",
  403. mock.initConverse([], {'clear_messages_on_reconnection': true}, async function (_converse) {
  404. const muc_jid = 'lounge@montague.lit';
  405. await mock.openAndEnterMUC(_converse, muc_jid , 'romeo');
  406. const view = _converse.chatboxviews.get(muc_jid);
  407. const message = 'Hello world';
  408. const nick = mock.chatroom_names[0];
  409. const msg =
  410. stx`<message from="lounge@montague.lit/${nick}"
  411. id="${u.getUniqueId()}"
  412. to="romeo@montague.lit"
  413. type="groupchat"
  414. xmlns="jabber:client">
  415. <body>${message}</body>
  416. </message>`;
  417. await view.model.handleMessageStanza(msg);
  418. await view.model.close();
  419. _converse.api.connection.get().IQ_stanzas = [];
  420. await mock.openAndEnterMUC(_converse, muc_jid , 'romeo');
  421. expect(view.model.messages.length).toBe(0);
  422. expect(view.querySelector('converse-chat-history')).toBe(null);
  423. }));
  424. it("is opened when an xmpp: URI is clicked inside another groupchat",
  425. mock.initConverse([], {}, async function (_converse) {
  426. await mock.waitForRoster(_converse, 'current');
  427. await mock.openAndEnterMUC(_converse, 'lounge@montague.lit', 'romeo');
  428. const view = _converse.chatboxviews.get('lounge@montague.lit');
  429. if (!view.querySelectorAll('.chat-area').length) {
  430. view.renderChatArea();
  431. }
  432. expect(_converse.chatboxes.length).toEqual(2);
  433. const message = 'Please go to xmpp:coven@chat.shakespeare.lit?join';
  434. const nick = mock.chatroom_names[0];
  435. const msg =
  436. stx`<message from="lounge@montague.lit/${nick}"
  437. id="${u.getUniqueId()}"
  438. type="groupchat"
  439. to="romeo@montague.lit"
  440. xmlns="jabber:client">
  441. <body>${message}</body>
  442. </message>`;
  443. await view.model.handleMessageStanza(msg);
  444. await u.waitUntil(() => view.querySelector('.chat-msg__text a'));
  445. view.querySelector('.chat-msg__text a').click();
  446. await mock.waitForMUCDiscoInfo(_converse, 'coven@chat.shakespeare.lit');
  447. await mock.waitForReservedNick(_converse, 'coven@chat.shakespeare.lit', 'romeo');
  448. await u.waitUntil(() => _converse.chatboxes.length === 3)
  449. expect(_converse.chatboxes.pluck('id').includes('coven@chat.shakespeare.lit')).toBe(true);
  450. }));
  451. it("shows a notification if it's not anonymous",
  452. mock.initConverse(['chatBoxesFetched'], {}, async function (_converse) {
  453. const muc_jid = 'coven@chat.shakespeare.lit';
  454. const nick = 'romeo';
  455. _converse.api.rooms.open(muc_jid);
  456. await mock.waitForMUCDiscoInfo(_converse, muc_jid);
  457. await mock.waitForReservedNick(_converse, muc_jid, nick);
  458. const view = await u.waitUntil(() => _converse.chatboxviews.get(muc_jid));
  459. const presence =
  460. stx`<presence to="romeo@montague.lit/orchard"
  461. from="coven@chat.shakespeare.lit/some1"
  462. xmlns="jabber:client">
  463. <x xmlns="${Strophe.NS.MUC_USER}">
  464. <item affiliation="owner" jid="romeo@montague.lit/_converse.js-29092160" role="moderator"/>
  465. <status code="110"/>
  466. <status code="100"/>
  467. </x>
  468. </presence>`;
  469. _converse.api.connection.get()._dataRecv(mock.createRequest(presence));
  470. await u.waitUntil(() => (view.model.session.get('connection_status') === converse.ROOMSTATUS.ENTERED));
  471. await mock.returnMemberLists(_converse, muc_jid, [], ['member', 'admin', 'owner']);
  472. const num_info_msgs = await u.waitUntil(() => view.querySelectorAll('.chat-content .chat-info').length);
  473. expect(num_info_msgs).toBe(1);
  474. expect(sizzle('div.chat-info', view).pop().textContent.trim()).toBe("This groupchat is not anonymous");
  475. const csntext = await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent);
  476. expect(csntext.trim()).toEqual("some1 has entered the groupchat");
  477. }));
  478. it("shows join/leave messages when users enter or exit a groupchat",
  479. mock.initConverse(['chatBoxesFetched'], {'muc_fetch_members': false}, async function (_converse) {
  480. const muc_jid = 'coven@chat.shakespeare.lit';
  481. const nick = 'some1';
  482. const room_creation_promise = _converse.api.rooms.open(muc_jid, {nick});
  483. await mock.waitForMUCDiscoInfo(_converse, muc_jid);
  484. const sent_stanzas = _converse.api.connection.get().sent_stanzas;
  485. await u.waitUntil(() => sent_stanzas.filter(iq => sizzle('presence history', iq).length));
  486. await _converse.api.waitUntil('chatRoomViewInitialized');
  487. /* We don't show join/leave messages for existing occupants. We
  488. * know about them because we receive their presences before we
  489. * receive our own.
  490. */
  491. let presence = $pres({
  492. to: 'romeo@montague.lit/_converse.js-29092160',
  493. from: 'coven@chat.shakespeare.lit/oldguy'
  494. }).c('x', {xmlns: Strophe.NS.MUC_USER})
  495. .c('item', {
  496. 'affiliation': 'none',
  497. 'jid': 'oldguy@montague.lit/_converse.js-290929789',
  498. 'role': 'participant'
  499. });
  500. _converse.api.connection.get()._dataRecv(mock.createRequest(presence));
  501. /* <presence to="romeo@montague.lit/_converse.js-29092160"
  502. * from="coven@chat.shakespeare.lit/some1">
  503. * <x xmlns="http://jabber.org/protocol/muc#user">
  504. * <item affiliation="owner" jid="romeo@montague.lit/_converse.js-29092160" role="moderator"/>
  505. * <status code="110"/>
  506. * </x>
  507. * </presence></body>
  508. */
  509. presence = $pres({
  510. to: 'romeo@montague.lit/_converse.js-29092160',
  511. from: 'coven@chat.shakespeare.lit/some1'
  512. }).c('x', {xmlns: Strophe.NS.MUC_USER})
  513. .c('item', {
  514. 'affiliation': 'owner',
  515. 'jid': 'romeo@montague.lit/_converse.js-29092160',
  516. 'role': 'moderator'
  517. }).up()
  518. .c('status', {code: '110'});
  519. _converse.api.connection.get()._dataRecv(mock.createRequest(presence));
  520. const view = await u.waitUntil(() => _converse.chatboxviews.get('coven@chat.shakespeare.lit'));
  521. const csntext = await u.waitUntil(() => view.querySelector('.chat-content__notifications')?.textContent);
  522. expect(csntext.trim()).toEqual("some1 has entered the groupchat");
  523. await room_creation_promise;
  524. await u.waitUntil(() => (view.model.session.get('connection_status') === converse.ROOMSTATUS.ENTERED));
  525. await view.model.messages.fetched;
  526. presence = $pres({
  527. to: 'romeo@montague.lit/_converse.js-29092160',
  528. from: 'coven@chat.shakespeare.lit/newguy'
  529. })
  530. .c('x', {xmlns: Strophe.NS.MUC_USER})
  531. .c('item', {
  532. 'affiliation': 'none',
  533. 'jid': 'newguy@montague.lit/_converse.js-290929789',
  534. 'role': 'participant'
  535. });
  536. _converse.api.connection.get()._dataRecv(mock.createRequest(presence));
  537. await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent.trim() ===
  538. "some1 and newguy have entered the groupchat");
  539. const msg =
  540. stx`<message from="coven@chat.shakespeare.lit/some1"
  541. to="romeo@montague.lit"
  542. id="${u.getUniqueId()}"
  543. type="groupchat"
  544. xmlns="jabber:client">
  545. <body>hello world</body>
  546. </message>`;
  547. _converse.api.connection.get()._dataRecv(mock.createRequest(msg));
  548. await u.waitUntil(() => view.querySelectorAll('.chat-msg__text').length);
  549. // Add another entrant, otherwise the above message will be
  550. // collapsed if "newguy" leaves immediately again
  551. presence = $pres({
  552. to: 'romeo@montague.lit/_converse.js-29092160',
  553. from: 'coven@chat.shakespeare.lit/newgirl'
  554. })
  555. .c('x', {xmlns: Strophe.NS.MUC_USER})
  556. .c('item', {
  557. 'affiliation': 'none',
  558. 'jid': 'newgirl@montague.lit/_converse.js-213098781',
  559. 'role': 'participant'
  560. });
  561. _converse.api.connection.get()._dataRecv(mock.createRequest(presence));
  562. await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent.trim() ===
  563. "some1, newguy and newgirl have entered the groupchat");
  564. // Don't show duplicate join messages
  565. presence = $pres({
  566. to: 'romeo@montague.lit/_converse.js-290918392',
  567. from: 'coven@chat.shakespeare.lit/newguy'
  568. }).c('x', {xmlns: Strophe.NS.MUC_USER})
  569. .c('item', {
  570. 'affiliation': 'none',
  571. 'jid': 'newguy@montague.lit/_converse.js-290929789',
  572. 'role': 'participant'
  573. });
  574. _converse.api.connection.get()._dataRecv(mock.createRequest(presence));
  575. /* <presence
  576. * from='coven@chat.shakespeare.lit/thirdwitch'
  577. * to='crone1@shakespeare.lit/desktop'
  578. * type='unavailable'>
  579. * <status>Disconnected: Replaced by new connection</status>
  580. * <x xmlns='http://jabber.org/protocol/muc#user'>
  581. * <item affiliation='member'
  582. * jid='hag66@shakespeare.lit/pda'
  583. * role='none'/>
  584. * </x>
  585. * </presence>
  586. */
  587. presence = $pres({
  588. to: 'romeo@montague.lit/_converse.js-29092160',
  589. type: 'unavailable',
  590. from: 'coven@chat.shakespeare.lit/newguy'
  591. })
  592. .c('status', 'Disconnected: Replaced by new connection').up()
  593. .c('x', {xmlns: Strophe.NS.MUC_USER})
  594. .c('item', {
  595. 'affiliation': 'none',
  596. 'jid': 'newguy@montague.lit/_converse.js-290929789',
  597. 'role': 'none'
  598. });
  599. _converse.api.connection.get()._dataRecv(mock.createRequest(presence));
  600. await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent.trim() ===
  601. "some1 and newgirl have entered the groupchat\nnewguy has left the groupchat");
  602. // When the user immediately joins again, we collapse the
  603. // multiple join/leave messages.
  604. presence = $pres({
  605. to: 'romeo@montague.lit/_converse.js-29092160',
  606. from: 'coven@chat.shakespeare.lit/newguy'
  607. }).c('x', {xmlns: Strophe.NS.MUC_USER})
  608. .c('item', {
  609. 'affiliation': 'none',
  610. 'jid': 'newguy@montague.lit/_converse.js-290929789',
  611. 'role': 'participant'
  612. });
  613. _converse.api.connection.get()._dataRecv(mock.createRequest(presence));
  614. await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent.trim() ===
  615. "some1, newgirl and newguy have entered the groupchat");
  616. presence = $pres({
  617. to: 'romeo@montague.lit/_converse.js-29092160',
  618. type: 'unavailable',
  619. from: 'coven@chat.shakespeare.lit/newguy'
  620. })
  621. .c('x', {xmlns: Strophe.NS.MUC_USER})
  622. .c('item', {
  623. 'affiliation': 'none',
  624. 'jid': 'newguy@montague.lit/_converse.js-290929789',
  625. 'role': 'none'
  626. });
  627. _converse.api.connection.get()._dataRecv(mock.createRequest(presence));
  628. await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent.trim() ===
  629. "some1 and newgirl have entered the groupchat\nnewguy has left the groupchat");
  630. presence = $pres({
  631. to: 'romeo@montague.lit/_converse.js-29092160',
  632. from: 'coven@chat.shakespeare.lit/nomorenicks'
  633. })
  634. .c('x', {xmlns: Strophe.NS.MUC_USER})
  635. .c('item', {
  636. 'affiliation': 'none',
  637. 'jid': 'nomorenicks@montague.lit/_converse.js-290929789',
  638. 'role': 'participant'
  639. });
  640. _converse.api.connection.get()._dataRecv(mock.createRequest(presence));
  641. await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent.trim() ===
  642. "some1, newgirl and nomorenicks have entered the groupchat\nnewguy has left the groupchat");
  643. presence = $pres({
  644. to: 'romeo@montague.lit/_converse.js-290918392',
  645. type: 'unavailable',
  646. from: 'coven@chat.shakespeare.lit/nomorenicks'
  647. }).c('x', {xmlns: Strophe.NS.MUC_USER})
  648. .c('item', {
  649. 'affiliation': 'none',
  650. 'jid': 'nomorenicks@montague.lit/_converse.js-290929789',
  651. 'role': 'none'
  652. });
  653. _converse.api.connection.get()._dataRecv(mock.createRequest(presence));
  654. await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent.trim() ===
  655. "some1 and newgirl have entered the groupchat\nnewguy and nomorenicks have left the groupchat");
  656. presence = $pres({
  657. to: 'romeo@montague.lit/_converse.js-29092160',
  658. from: 'coven@chat.shakespeare.lit/nomorenicks'
  659. })
  660. .c('x', {xmlns: Strophe.NS.MUC_USER})
  661. .c('item', {
  662. 'affiliation': 'none',
  663. 'jid': 'nomorenicks@montague.lit/_converse.js-290929789',
  664. 'role': 'participant'
  665. });
  666. _converse.api.connection.get()._dataRecv(mock.createRequest(presence));
  667. await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent.trim() ===
  668. "some1, newgirl and nomorenicks have entered the groupchat\nnewguy has left the groupchat");
  669. // Test a member joining and leaving
  670. presence = $pres({
  671. to: 'romeo@montague.lit/_converse.js-290918392',
  672. from: 'coven@chat.shakespeare.lit/insider'
  673. }).c('x', {xmlns: Strophe.NS.MUC_USER})
  674. .c('item', {
  675. 'affiliation': 'member',
  676. 'jid': 'insider@montague.lit/_converse.js-290929789',
  677. 'role': 'participant'
  678. });
  679. _converse.api.connection.get()._dataRecv(mock.createRequest(presence));
  680. /* <presence
  681. * from='coven@chat.shakespeare.lit/thirdwitch'
  682. * to='crone1@shakespeare.lit/desktop'
  683. * type='unavailable'>
  684. * <status>Disconnected: Replaced by new connection</status>
  685. * <x xmlns='http://jabber.org/protocol/muc#user'>
  686. * <item affiliation='member'
  687. * jid='hag66@shakespeare.lit/pda'
  688. * role='none'/>
  689. * </x>
  690. * </presence>
  691. */
  692. presence = $pres({
  693. to: 'romeo@montague.lit/_converse.js-29092160',
  694. type: 'unavailable',
  695. from: 'coven@chat.shakespeare.lit/insider'
  696. })
  697. .c('status', 'Disconnected: Replaced by new connection').up()
  698. .c('x', {xmlns: Strophe.NS.MUC_USER})
  699. .c('item', {
  700. 'affiliation': 'member',
  701. 'jid': 'insider@montague.lit/_converse.js-290929789',
  702. 'role': 'none'
  703. });
  704. _converse.api.connection.get()._dataRecv(mock.createRequest(presence));
  705. await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent.trim() ===
  706. "some1, newgirl and nomorenicks have entered the groupchat\nnewguy and insider have left the groupchat");
  707. expect(view.model.occupants.length).toBe(5);
  708. expect(view.model.occupants.findWhere({'jid': 'insider@montague.lit'}).get('show')).toBe('offline');
  709. // New girl leaves
  710. presence = $pres({
  711. 'to': 'romeo@montague.lit/_converse.js-29092160',
  712. 'type': 'unavailable',
  713. 'from': 'coven@chat.shakespeare.lit/newgirl'
  714. })
  715. .c('x', {xmlns: Strophe.NS.MUC_USER})
  716. .c('item', {
  717. 'affiliation': 'none',
  718. 'jid': 'newgirl@montague.lit/_converse.js-213098781',
  719. 'role': 'none'
  720. });
  721. _converse.api.connection.get()._dataRecv(mock.createRequest(presence));
  722. await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent.trim() ===
  723. "some1 and nomorenicks have entered the groupchat\nnewguy, insider and newgirl have left the groupchat");
  724. expect(view.model.occupants.length).toBe(4);
  725. }));
  726. it("combines subsequent join/leave messages when users enter or exit a groupchat",
  727. mock.initConverse(['chatBoxesFetched'], {}, async function (_converse) {
  728. await mock.openAndEnterMUC(_converse, 'coven@chat.shakespeare.lit', 'romeo')
  729. const view = _converse.chatboxviews.get('coven@chat.shakespeare.lit');
  730. await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent.trim() === "romeo has entered the groupchat");
  731. let presence = stx`<presence xmlns="jabber:client" to="romeo@montague.lit/orchard" from="coven@chat.shakespeare.lit/fabio">
  732. <c xmlns="http://jabber.org/protocol/caps" node="http://conversations.im" ver="INI3xjRUioclBTP/aACfWi5m9UY=" hash="sha-1"/>
  733. <x xmlns="http://jabber.org/protocol/muc#user">
  734. <item affiliation="none" jid="fabio@montefuscolo.com.br/Conversations.ZvLu" role="participant"/>
  735. </x>
  736. </presence>`;
  737. _converse.api.connection.get()._dataRecv(mock.createRequest(presence));
  738. await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent.trim() === "romeo and fabio have entered the groupchat");
  739. presence = stx`<presence xmlns="jabber:client" to="romeo@montague.lit/orchard" from="coven@chat.shakespeare.lit/Dele Olajide">
  740. <x xmlns="http://jabber.org/protocol/muc#user">
  741. <item affiliation="none" jid="deleo@traderlynk.4ng.net/converse.js-39320524" role="participant"/>
  742. </x>
  743. </presence>`;
  744. _converse.api.connection.get()._dataRecv(mock.createRequest(presence));
  745. await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent.trim() === "romeo, fabio and Dele Olajide have entered the groupchat");
  746. presence = stx`<presence xmlns="jabber:client" to="romeo@montague.lit/orchard" from="coven@chat.shakespeare.lit/jcbrand">
  747. <x xmlns="http://jabber.org/protocol/muc#user">
  748. <item affiliation="owner" jid="jc@opkode.com/converse.js-30645022" role="moderator"/>
  749. <status code="110"/>
  750. </x>
  751. </presence>`;
  752. _converse.api.connection.get()._dataRecv(mock.createRequest(presence));
  753. await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent.trim() === "romeo, fabio and others have entered the groupchat");
  754. presence = stx`<presence xmlns="jabber:client" to="romeo@montague.lit/orchard" type="unavailable" from="coven@chat.shakespeare.lit/Dele Olajide">
  755. <x xmlns="http://jabber.org/protocol/muc#user">
  756. <item affiliation="none" jid="deleo@traderlynk.4ng.net/converse.js-39320524" role="none"/>
  757. </x>
  758. </presence>`;
  759. _converse.api.connection.get()._dataRecv(mock.createRequest(presence));
  760. await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent.trim() ===
  761. "romeo, fabio and jcbrand have entered the groupchat\nDele Olajide has left the groupchat");
  762. presence = stx`<presence xmlns="jabber:client" to="romeo@montague.lit/orchard" from="coven@chat.shakespeare.lit/Dele Olajide">
  763. <x xmlns="http://jabber.org/protocol/muc#user">
  764. <item affiliation="none" jid="deleo@traderlynk.4ng.net/converse.js-74567907" role="participant"/>
  765. </x>
  766. </presence>`;
  767. _converse.api.connection.get()._dataRecv(mock.createRequest(presence));
  768. await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent.trim() ===
  769. "romeo, fabio and others have entered the groupchat");
  770. presence = stx`<presence xmlns="jabber:client" to="romeo@montague.lit/orchard" from="coven@chat.shakespeare.lit/fuvuv" xml:lang="en">
  771. <c xmlns="http://jabber.org/protocol/caps" node="http://jabber.pix-art.de" ver="5tOurnuFnp2h50hKafeUyeN4Yl8=" hash="sha-1"/>
  772. <x xmlns="vcard-temp:x:update"/>
  773. <x xmlns="http://jabber.org/protocol/muc#user">
  774. <item affiliation="none" jid="fuvuv@blabber.im/Pix-Art Messenger.8zoB" role="participant"/>
  775. </x>
  776. </presence>`;
  777. _converse.api.connection.get()._dataRecv(mock.createRequest(presence));
  778. await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent.trim() ===
  779. "romeo, fabio and others have entered the groupchat");
  780. presence = stx`<presence xmlns="jabber:client" to="romeo@montague.lit/orchard" type="unavailable" from="coven@chat.shakespeare.lit/fuvuv">
  781. <x xmlns="http://jabber.org/protocol/muc#user">
  782. <item affiliation="none" jid="fuvuv@blabber.im/Pix-Art Messenger.8zoB" role="none"/>
  783. </x>
  784. </presence>`;
  785. _converse.api.connection.get()._dataRecv(mock.createRequest(presence));
  786. await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent.trim() ===
  787. "romeo, fabio and others have entered the groupchat\nfuvuv has left the groupchat");
  788. presence = stx`<presence xmlns="jabber:client" to="romeo@montague.lit/orchard" type="unavailable" from="coven@chat.shakespeare.lit/fabio">
  789. <status>Disconnected: Replaced by new connection</status>
  790. <x xmlns="http://jabber.org/protocol/muc#user">
  791. <item affiliation="none" jid="fabio@montefuscolo.com.br/Conversations.ZvLu" role="none"/>
  792. </x>
  793. </presence>`;
  794. _converse.api.connection.get()._dataRecv(mock.createRequest(presence));
  795. await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent.trim() ===
  796. "romeo, jcbrand and Dele Olajide have entered the groupchat\nfuvuv and fabio have left the groupchat");
  797. presence = stx`<presence xmlns="jabber:client" to="romeo@montague.lit/orchard" from="coven@chat.shakespeare.lit/fabio">
  798. <c xmlns="http://jabber.org/protocol/caps" node="http://conversations.im" ver="INI3xjRUioclBTP/aACfWi5m9UY=" hash="sha-1"/>
  799. <status>Ready for a new day</status>
  800. <x xmlns="http://jabber.org/protocol/muc#user">
  801. <item affiliation="none" jid="fabio@montefuscolo.com.br/Conversations.ZvLu" role="participant"/>
  802. </x>
  803. </presence>`;
  804. _converse.api.connection.get()._dataRecv(mock.createRequest(presence));
  805. await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent.trim() ===
  806. "romeo, jcbrand and others have entered the groupchat\nfuvuv has left the groupchat");
  807. presence = stx`<presence xmlns="jabber:client" to="romeo@montague.lit/orchard" type="unavailable" from="coven@chat.shakespeare.lit/fabio">
  808. <status>Disconnected: closed</status>
  809. <x xmlns="http://jabber.org/protocol/muc#user">
  810. <item affiliation="none" jid="fabio@montefuscolo.com.br/Conversations.ZvLu" role="none"/>
  811. </x>
  812. </presence>`;
  813. _converse.api.connection.get()._dataRecv(mock.createRequest(presence));
  814. await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent.trim() ===
  815. "romeo, jcbrand and Dele Olajide have entered the groupchat\nfuvuv and fabio have left the groupchat");
  816. presence = stx`<presence xmlns="jabber:client" to="romeo@montague.lit/orchard" type="unavailable" from="coven@chat.shakespeare.lit/Dele Olajide">
  817. <x xmlns="http://jabber.org/protocol/muc#user">
  818. <item affiliation="none" jid="deleo@traderlynk.4ng.net/converse.js-74567907" role="none"/>
  819. </x>
  820. </presence>`;
  821. _converse.api.connection.get()._dataRecv(mock.createRequest(presence));
  822. await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent.trim() ===
  823. "romeo and jcbrand have entered the groupchat\nfuvuv, fabio and Dele Olajide have left the groupchat");
  824. presence = stx`<presence xmlns="jabber:client" to="romeo@montague.lit/orchard" from="coven@chat.shakespeare.lit/fabio">
  825. <c xmlns="http://jabber.org/protocol/caps" node="http://conversations.im" ver="INI3xjRUioclBTP/aACfWi5m9UY=" hash="sha-1"/>
  826. <x xmlns="http://jabber.org/protocol/muc#user">
  827. <item affiliation="none" jid="fabio@montefuscolo.com.br/Conversations.ZvLu" role="participant"/>
  828. </x>
  829. </presence>`;
  830. _converse.api.connection.get()._dataRecv(mock.createRequest(presence));
  831. await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent.trim() ===
  832. "romeo, jcbrand and fabio have entered the groupchat\nfuvuv and Dele Olajide have left the groupchat");
  833. expect(1).toBe(1);
  834. }));
  835. it("doesn't show the disconnection messages when join_leave_events is not in muc_show_info_messages setting",
  836. mock.initConverse(['chatBoxesFetched'], {'muc_show_info_messages': []}, async function (_converse) {
  837. spyOn(_converse.ChatRoom.prototype, 'onOccupantAdded').and.callThrough();
  838. spyOn(_converse.ChatRoom.prototype, 'onOccupantRemoved').and.callThrough();
  839. await mock.openAndEnterMUC(_converse, 'coven@chat.shakespeare.lit', 'some1');
  840. const view = _converse.chatboxviews.get('coven@chat.shakespeare.lit');
  841. let presence = $pres({
  842. to: 'romeo@montague.lit/orchard',
  843. from: 'coven@chat.shakespeare.lit/newguy'
  844. }).c('x', {xmlns: Strophe.NS.MUC_USER})
  845. .c('item', {
  846. 'affiliation': 'none',
  847. 'jid': 'newguy@montague.lit/_converse.js-290929789',
  848. 'role': 'participant'
  849. });
  850. _converse.api.connection.get()._dataRecv(mock.createRequest(presence));
  851. await u.waitUntil(() => view.model.onOccupantAdded.calls.count() === 2);
  852. expect(view.model.notifications.get('entered')).toBeFalsy();
  853. expect(view.querySelector('.chat-content__notifications').textContent.trim()).toBe('');
  854. await mock.sendMessage(view, 'hello world');
  855. presence = stx`<presence xmlns="jabber:client" to="romeo@montague.lit/orchard" type="unavailable" from="coven@chat.shakespeare.lit/newguy">
  856. <status>Gotta go!</status>
  857. <x xmlns="http://jabber.org/protocol/muc#user">
  858. <item affiliation="none" jid="newguy@montague.lit/_converse.js-290929789" role="none"/>
  859. </x>
  860. </presence>`;
  861. _converse.api.connection.get()._dataRecv(mock.createRequest(presence));
  862. await u.waitUntil(() => view.model.onOccupantRemoved.calls.count());
  863. expect(view.model.onOccupantRemoved.calls.count()).toBe(1);
  864. expect(view.model.notifications.get('entered')).toBeFalsy();
  865. await mock.sendMessage(view, 'hello world');
  866. expect(view.querySelector('.chat-content__notifications').textContent.trim()).toBe('');
  867. }));
  868. it("role-change messages that follow a MUC leave are left out",
  869. mock.initConverse(['chatBoxesFetched'], {}, async function (_converse) {
  870. // See https://github.com/conversejs/converse.js/issues/1259
  871. await mock.openAndEnterMUC(_converse, 'conversations@conference.siacs.eu', 'romeo');
  872. const presence =
  873. stx`<presence to='romeo@montague.lit/orchard'
  874. from='conversations@conference.siacs.eu/Guus'
  875. xmlns="jabber:client">
  876. <x xmlns='${Strophe.NS.MUC_USER}'>
  877. <item affiliation='none' jid='Guus@montague.lit/xxx' role='visitor'/>
  878. </x>
  879. </presence>`;
  880. _converse.api.connection.get()._dataRecv(mock.createRequest(presence));
  881. const view = _converse.chatboxviews.get('conversations@conference.siacs.eu');
  882. const msg =
  883. stx`<message from='conversations@conference.siacs.eu/romeo'
  884. to='romeo@montague.lit'
  885. id='${u.getUniqueId()}'
  886. type='groupchat'
  887. xmlns="jabber:client">
  888. <body>Some message</body>
  889. </message>`;
  890. await view.model.handleMessageStanza(msg);
  891. await u.waitUntil(() => sizzle('.chat-msg:last .chat-msg__text', view).pop());
  892. let stanza =
  893. stx`<presence
  894. to="romeo@montague.lit/orchard"
  895. type="unavailable"
  896. from="conversations@conference.siacs.eu/Guus"
  897. xmlns="jabber:client">
  898. <x xmlns="http://jabber.org/protocol/muc#user">
  899. <item affiliation="none" role="none"/>
  900. </x>
  901. </presence>`;
  902. _converse.api.connection.get()._dataRecv(mock.createRequest(stanza));
  903. stanza =
  904. stx`<presence
  905. to="romeo@montague.lit/orchard"
  906. from="conversations@conference.siacs.eu/Guus"
  907. xmlns="jabber:client">
  908. <c xmlns="http://jabber.org/protocol/caps"
  909. node="http://conversations.im"
  910. ver="ISg6+9AoK1/cwhbNEDviSvjdPzI="
  911. hash="sha-1"/>
  912. <x xmlns="vcard-temp:x:update">
  913. <photo>bf987c486c51fbc05a6a4a9f20dd19b5efba3758</photo>
  914. </x>
  915. <x xmlns="http://jabber.org/protocol/muc#user">
  916. <item affiliation="none" role="visitor"/>
  917. </x>
  918. </presence>`;
  919. _converse.api.connection.get()._dataRecv(mock.createRequest(stanza));
  920. await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent.trim()
  921. === "romeo and Guus have entered the groupchat");
  922. expect(1).toBe(1);
  923. }));
  924. it("must first be configured if it's a new",
  925. mock.initConverse(['chatBoxesFetched'],
  926. { muc_instant_rooms: false },
  927. async function (_converse) {
  928. let sent_IQ, IQ_id;
  929. const sendIQ = _converse.api.connection.get().sendIQ;
  930. spyOn(_converse.api.connection.get(), 'sendIQ').and.callFake(function (iq, callback, errback) {
  931. sent_IQ = iq;
  932. IQ_id = sendIQ.call(this, iq, callback, errback);
  933. });
  934. const { api } = _converse;
  935. const own_jid = api.connection.get().jid;
  936. const muc_jid = 'coven@chat.shakespeare.lit';
  937. _converse.api.rooms.open(muc_jid, {'nick': 'some1'});
  938. await mock.waitForNewMUCDiscoInfo(_converse, muc_jid);
  939. const presence =
  940. stx`<presence to='${own_jid}'
  941. from='coven@chat.shakespeare.lit/some1'
  942. xmlns="jabber:client">
  943. <x xmlns='${Strophe.NS.MUC_USER}'>
  944. <item affiliation='owner' jid='romeo@montague.lit/_converse.js-29092160' role='moderator'/>
  945. <status code='110'/>
  946. <status code='201'/>
  947. </x>
  948. </presence>`;
  949. _converse.api.connection.get()._dataRecv(mock.createRequest(presence));
  950. const sent_IQs = _converse.api.connection.get().IQ_stanzas;
  951. while (sent_IQs.length) sent_IQs.pop();
  952. await mock.waitForMUCDiscoInfo(_converse, muc_jid);
  953. const view = await u.waitUntil(() => _converse.chatboxviews.get(muc_jid));
  954. await u.waitUntil(() => u.isVisible(view));
  955. await u.waitUntil(() => view.model.getOwnOccupant()?.get('affiliation') === 'owner');
  956. const sel = 'iq query[xmlns="http://jabber.org/protocol/muc#owner"]';
  957. const iq = await u.waitUntil(() => sent_IQs.filter(iq => sizzle(sel, iq).length).pop());
  958. /* Check that an IQ is sent out, asking for the
  959. * configuration form.
  960. * See: // https://xmpp.org/extensions/xep-0045.html#example-163
  961. */
  962. expect(Strophe.serialize(iq)).toBe(
  963. `<iq id="${iq.getAttribute('id')}" to="${muc_jid}" type="get" xmlns="jabber:client">`+
  964. `<query xmlns="http://jabber.org/protocol/muc#owner"/>`+
  965. `</iq>`);
  966. /* Server responds with the configuration form.
  967. * See: // https://xmpp.org/extensions/xep-0045.html#example-165
  968. */
  969. const config_stanza =
  970. stx`<iq from="${muc_jid}" id="${iq.getAttribute('id')}"
  971. to="romeo@montague.lit/desktop"
  972. type="result"
  973. xmlns="jabber:client">
  974. <query xmlns="http://jabber.org/protocol/muc#owner">
  975. <x xmlns="jabber:x:data" type="form">
  976. <title>Configuration for "coven" Room</title>
  977. <instructions>Complete this form to modify the configuration of your room.</instructions>
  978. <field type="hidden" var="FORM_TYPE">
  979. <value>http://jabber.org/protocol/muc#roomconfig</value>
  980. </field>
  981. <field label="Natural-Language Room Name" type="text-single" var="muc#roomconfig_roomname">
  982. <value>A Dark Cave</value>
  983. </field>
  984. <field label="Short Description of Room" type="text-single" var="muc#roomconfig_roomdesc">
  985. <value>The place for all good witches!</value>
  986. </field>
  987. <field label="Enable Public Logging?" type="boolean" var="muc#roomconfig_enablelogging">
  988. <value>0</value>
  989. </field>
  990. <field label="Allow Occupants to Change Subject?" type="boolean" var="muc#roomconfig_changesubject">
  991. <value>0</value>
  992. </field>
  993. <field label="Allow Occupants to Invite Others?" type="boolean" var="muc#roomconfig_allowinvites">
  994. <value>0</value>
  995. </field>
  996. <field label="Who Can Send Private Messages?" type="list-single" var="muc#roomconfig_allowpm">
  997. <value>anyone</value>
  998. <option label="Anyone">
  999. <value>anyone</value>
  1000. </option>
  1001. <option label="Anyone with Voice">
  1002. <value>participants</value>
  1003. </option>
  1004. <option label="Moderators Only">
  1005. <value>moderators</value>
  1006. </option>
  1007. <option label="Nobody">
  1008. <value>none</value>
  1009. </option>
  1010. </field>
  1011. <field label="Roles for which Presence is Broadcasted" type="list-multi" var="muc#roomconfig_presencebroadcast">
  1012. <value>moderator</value>
  1013. <value>participant</value>
  1014. <value>visitor</value>
  1015. <option label="Moderator">
  1016. <value>moderator</value>
  1017. </option>
  1018. <option label="Participant">
  1019. <value>participant</value>
  1020. </option>
  1021. <option label="Visitor">
  1022. <value>visitor</value>
  1023. </option>
  1024. </field>
  1025. <field label="Roles and Affiliations that May Retrieve Member List" type="list-multi" var="muc#roomconfig_getmemberlist">
  1026. <value>moderator</value>
  1027. <value>participant</value>
  1028. <value>visitor</value>
  1029. <option label="Moderator">
  1030. <value>moderator</value>
  1031. </option>
  1032. <option label="Participant">
  1033. <value>participant</value>
  1034. </option>
  1035. <option label="Visitor">
  1036. <value>visitor</value>
  1037. </option>
  1038. </field>
  1039. <field label="Make Room Publicly Searchable?" type="boolean" var="muc#roomconfig_publicroom">
  1040. <value>0</value>
  1041. </field>
  1042. <field label="Make Room Persistent?" type="boolean" var="muc#roomconfig_persistentroom">
  1043. <value>0</value>
  1044. </field>
  1045. <field label="Make Room Moderated?" type="boolean" var="muc#roomconfig_moderatedroom">
  1046. <value>0</value>
  1047. </field>
  1048. <field label="Make Room Members Only?" type="boolean" var="muc#roomconfig_membersonly">
  1049. <value>0</value>
  1050. </field>
  1051. <field label="Password Required for Entry?" type="boolean" var="muc#roomconfig_passwordprotectedroom">
  1052. <value>1</value>
  1053. </field>
  1054. <field type="fixed">
  1055. <value>If a password is required to enter this groupchat, you must specify the password below.</value>
  1056. </field>
  1057. <field label="Password" type="text-private" var="muc#roomconfig_roomsecret">
  1058. <value>cauldronburn</value>
  1059. </field>
  1060. </x>
  1061. </query>
  1062. </iq>`;
  1063. _converse.api.connection.get()._dataRecv(mock.createRequest(config_stanza));
  1064. const modal = _converse.api.modal.get('converse-muc-config-modal');
  1065. const membersonly = await u.waitUntil(() => modal.querySelector('input[name="muc#roomconfig_membersonly"]'));
  1066. expect(membersonly.getAttribute('type')).toBe('checkbox');
  1067. membersonly.checked = true;
  1068. const moderated = modal.querySelectorAll('input[name="muc#roomconfig_moderatedroom"]');
  1069. expect(moderated.length).toBe(1);
  1070. expect(moderated[0].getAttribute('type')).toBe('checkbox');
  1071. moderated[0].checked = true;
  1072. const password = modal.querySelectorAll('input[name="muc#roomconfig_roomsecret"]');
  1073. expect(password.length).toBe(1);
  1074. expect(password[0].getAttribute('type')).toBe('password');
  1075. const allowpm = modal.querySelectorAll('select[name="muc#roomconfig_allowpm"]');
  1076. expect(allowpm.length).toBe(1);
  1077. allowpm[0].value = 'moderators';
  1078. const presencebroadcast = modal.querySelectorAll('select[name="muc#roomconfig_presencebroadcast"]');
  1079. expect(presencebroadcast.length).toBe(1);
  1080. presencebroadcast[0].value = ['moderator'];
  1081. modal.querySelector('.chatroom-form input[type="submit"]').click();
  1082. console.log(Strophe.serialize(sent_IQ));
  1083. expect(Strophe.serialize(sent_IQ)).toBe(
  1084. `<iq id="${IQ_id}" to="${muc_jid}" type="set" xmlns="jabber:client">`+
  1085. `<query xmlns="http://jabber.org/protocol/muc#owner">`+
  1086. `<x type="submit" xmlns="jabber:x:data">`+
  1087. `<field var="FORM_TYPE"><value>http://jabber.org/protocol/muc#roomconfig</value></field>`+
  1088. `<field var="muc#roomconfig_roomname"><value>A Dark Cave</value></field>`+
  1089. `<field var="muc#roomconfig_roomdesc"><value>The place for all good witches!</value></field>`+
  1090. `<field var="muc#roomconfig_enablelogging"><value>0</value></field>`+
  1091. `<field var="muc#roomconfig_changesubject"><value>0</value></field>`+
  1092. `<field var="muc#roomconfig_allowinvites"><value>0</value></field>`+
  1093. `<field var="muc#roomconfig_allowpm"><value>moderators</value></field>`+
  1094. `<field var="muc#roomconfig_presencebroadcast"><value>moderator</value></field>`+
  1095. `<field var="muc#roomconfig_getmemberlist"><value>moderator</value>,<value>participant</value>,<value>visitor</value></field>`+
  1096. `<field var="muc#roomconfig_publicroom"><value>0</value></field>`+
  1097. `<field var="muc#roomconfig_persistentroom"><value>0</value></field>`+
  1098. `<field var="muc#roomconfig_moderatedroom"><value>1</value></field>`+
  1099. `<field var="muc#roomconfig_membersonly"><value>1</value></field>`+
  1100. `<field var="muc#roomconfig_passwordprotectedroom"><value>1</value></field>`+
  1101. `<field var="muc#roomconfig_roomsecret"><value>cauldronburn</value></field>`+
  1102. `</x>`+
  1103. `</query>`+
  1104. `</iq>`);
  1105. }));
  1106. it("can be configured if your its owner",
  1107. mock.initConverse(['chatBoxesFetched'],
  1108. { muc_instant_rooms: false },
  1109. async function (_converse) {
  1110. let sent_IQ, IQ_id;
  1111. const sendIQ = _converse.api.connection.get().sendIQ;
  1112. spyOn(_converse.api.connection.get(), 'sendIQ').and.callFake(function (iq, callback, errback) {
  1113. sent_IQ = iq;
  1114. IQ_id = sendIQ.call(this, iq, callback, errback);
  1115. });
  1116. const muc_jid = 'coven@chat.shakespeare.lit';
  1117. const features = [
  1118. 'http://jabber.org/protocol/muc',
  1119. 'jabber:iq:register',
  1120. 'muc_passwordprotected',
  1121. 'muc_hidden',
  1122. 'muc_temporary',
  1123. 'muc_membersonly',
  1124. 'muc_unmoderated',
  1125. 'muc_anonymous',
  1126. 'vcard-temp',
  1127. ]
  1128. await mock.openAndEnterMUC(_converse, muc_jid, 'some1', features);
  1129. await mock.waitForNewMUCDiscoInfo(_converse, muc_jid);
  1130. const view = await u.waitUntil(() => _converse.chatboxviews.get(muc_jid));
  1131. await u.waitUntil(() => u.isVisible(view));
  1132. const presence =
  1133. stx`<presence to="romeo@montague.lit/_converse.js-29092160"
  1134. from="coven@chat.shakespeare.lit/some1"
  1135. xmlns="jabber:client">
  1136. <x xmlns="${Strophe.NS.MUC_USER}">
  1137. <item affiliation="owner" jid="romeo@montague.lit/_converse.js-29092160" role="moderator"/>
  1138. <status code="110"/>
  1139. <status code="201"/>
  1140. </x>
  1141. </presence>`;
  1142. _converse.api.connection.get()._dataRecv(mock.createRequest(presence));
  1143. await u.waitUntil(() => view.model.getOwnOccupant()?.get('affiliation') === 'owner');
  1144. const sent_IQs = _converse.api.connection.get().IQ_stanzas;
  1145. const sel = 'iq query[xmlns="http://jabber.org/protocol/muc#owner"]';
  1146. const iq = await u.waitUntil(() => sent_IQs.filter(iq => sizzle(sel, iq).length).pop());
  1147. /* Check that an IQ is sent out, asking for the
  1148. * configuration form.
  1149. */
  1150. expect(Strophe.serialize(iq)).toBe(
  1151. `<iq id="${iq.getAttribute('id')}" to="${muc_jid}" type="get" xmlns="jabber:client">`+
  1152. `<query xmlns="http://jabber.org/protocol/muc#owner"/>`+
  1153. `</iq>`);
  1154. /* Server responds with the configuration form.
  1155. * See: // https://xmpp.org/extensions/xep-0045.html#example-165
  1156. */
  1157. const config_stanza =
  1158. stx`<iq from="${muc_jid}"
  1159. id="${iq.getAttribute('id')}"
  1160. to="romeo@montague.lit/desktop"
  1161. type="result"
  1162. xmlns="jabber:client">
  1163. <query xmlns="http://jabber.org/protocol/muc#owner">
  1164. <x xmlns="jabber:x:data" type="form">
  1165. <title>Configuration for "coven" Room</title>
  1166. <instructions>Complete this form to modify the configuration of your room.</instructions>
  1167. <field type="hidden" var="FORM_TYPE">
  1168. <value>http://jabber.org/protocol/muc#roomconfig</value>
  1169. </field>
  1170. <field label="Natural-Language Room Name" type="text-single" var="muc#roomconfig_roomname">
  1171. <value>A Dark Cave</value>
  1172. </field>
  1173. <field label="Short Description of Room" type="text-single" var="muc#roomconfig_roomdesc">
  1174. <value>The place for all good witches!</value>
  1175. </field>
  1176. <field label="Enable Public Logging?" type="boolean" var="muc#roomconfig_enablelogging">
  1177. <value>0</value>
  1178. </field>
  1179. <field label="Allow Occupants to Change Subject?" type="boolean" var="muc#roomconfig_changesubject">
  1180. <value>0</value>
  1181. </field>
  1182. <field label="Allow Occupants to Invite Others?" type="boolean" var="muc#roomconfig_allowinvites">
  1183. <value>0</value>
  1184. </field>
  1185. <field label="Who Can Send Private Messages?" type="list-single" var="muc#roomconfig_allowpm">
  1186. <value>anyone</value>
  1187. <option label="Anyone">
  1188. <value>anyone</value>
  1189. </option>
  1190. <option label="Anyone with Voice">
  1191. <value>participants</value>
  1192. </option>
  1193. <option label="Moderators Only">
  1194. <value>moderators</value>
  1195. </option>
  1196. <option label="Nobody">
  1197. <value>none</value>
  1198. </option>
  1199. </field>
  1200. <field label="Roles for which Presence is Broadcasted"
  1201. type="list-multi"
  1202. var="muc#roomconfig_presencebroadcast">
  1203. <value>moderator</value>
  1204. <value>participant</value>
  1205. <value>visitor</value>
  1206. <option label="Moderator">
  1207. <value>moderator</value>
  1208. </option>
  1209. <option label="Participant">
  1210. <value>participant</value>
  1211. </option>
  1212. <option label="Visitor">
  1213. <value>visitor</value>
  1214. </option>
  1215. </field>
  1216. <field label="Roles and Affiliations that May Retrieve Member List"
  1217. type="list-multi"
  1218. var="muc#roomconfig_getmemberlist">
  1219. <value>moderator</value>
  1220. <value>participant</value>
  1221. <value>visitor</value>
  1222. <option label="Moderator">
  1223. <value>moderator</value>
  1224. </option>
  1225. <option label="Participant">
  1226. <value>participant</value>
  1227. </option>
  1228. <option label="Visitor">
  1229. <value>visitor</value>
  1230. </option>
  1231. </field>
  1232. <field label="Make Room Publicly Searchable?" type="boolean" var="muc#roomconfig_publicroom">
  1233. <value>0</value>
  1234. </field>
  1235. <field label="Make Room Persistent?" type="boolean" var="muc#roomconfig_persistentroom">
  1236. <value>0</value>
  1237. </field>
  1238. <field label="Make Room Moderated?" type="boolean" var="muc#roomconfig_moderatedroom">
  1239. <value>0</value>
  1240. </field>
  1241. <field label="Make Room Members Only?" type="boolean" var="muc#roomconfig_membersonly">
  1242. <value>0</value>
  1243. </field>
  1244. <field label="Password Required for Entry?" type="boolean" var="muc#roomconfig_passwordprotectedroom">
  1245. <value>1</value>
  1246. </field>
  1247. <field type="fixed">
  1248. <value>If a password is required to enter this groupchat, you must specify the password below.</value>
  1249. </field>
  1250. <field label="Password" type="text-private" var="muc#roomconfig_roomsecret">
  1251. <value>cauldronburn</value>
  1252. </field>
  1253. </x>
  1254. </query>
  1255. </iq>`;
  1256. _converse.api.connection.get()._dataRecv(mock.createRequest(config_stanza));
  1257. const modal = _converse.api.modal.get('converse-muc-config-modal');
  1258. const membersonly = await u.waitUntil(() => modal.querySelector('input[name="muc#roomconfig_membersonly"]'));
  1259. expect(membersonly.getAttribute('type')).toBe('checkbox');
  1260. membersonly.checked = true;
  1261. const moderated = modal.querySelectorAll('input[name="muc#roomconfig_moderatedroom"]');
  1262. expect(moderated.length).toBe(1);
  1263. expect(moderated[0].getAttribute('type')).toBe('checkbox');
  1264. moderated[0].checked = true;
  1265. const password = modal.querySelectorAll('input[name="muc#roomconfig_roomsecret"]');
  1266. expect(password.length).toBe(1);
  1267. expect(password[0].getAttribute('type')).toBe('password');
  1268. const allowpm = modal.querySelectorAll('select[name="muc#roomconfig_allowpm"]');
  1269. expect(allowpm.length).toBe(1);
  1270. allowpm[0].value = 'moderators';
  1271. const presencebroadcast = modal.querySelectorAll('select[name="muc#roomconfig_presencebroadcast"]');
  1272. expect(presencebroadcast.length).toBe(1);
  1273. presencebroadcast[0].value = ['moderator'];
  1274. // Set image file for avatar upload
  1275. const avatar_picker = modal.querySelector('converse-image-picker input[type="file"]');
  1276. const image_file = new File([_converse.default_avatar_image], 'avatar.svg', {
  1277. type: _converse.default_avatar_image_type,
  1278. lastModified: new Date(),
  1279. });
  1280. const dataTransfer = new DataTransfer();
  1281. dataTransfer.items.add(image_file);
  1282. avatar_picker.files = dataTransfer.files;
  1283. modal.querySelector('.chatroom-form input[type="submit"]').click();
  1284. expect(Strophe.serialize(sent_IQ)).toBe(
  1285. `<iq id="${IQ_id}" to="${muc_jid}" type="set" xmlns="jabber:client">`+
  1286. `<query xmlns="http://jabber.org/protocol/muc#owner">`+
  1287. `<x type="submit" xmlns="jabber:x:data">`+
  1288. `<field var="FORM_TYPE"><value>http://jabber.org/protocol/muc#roomconfig</value></field>`+
  1289. `<field var="muc#roomconfig_roomname"><value>A Dark Cave</value></field>`+
  1290. `<field var="muc#roomconfig_roomdesc"><value>The place for all good witches!</value></field>`+
  1291. `<field var="muc#roomconfig_enablelogging"><value>0</value></field>`+
  1292. `<field var="muc#roomconfig_changesubject"><value>0</value></field>`+
  1293. `<field var="muc#roomconfig_allowinvites"><value>0</value></field>`+
  1294. `<field var="muc#roomconfig_allowpm"><value>moderators</value></field>`+
  1295. `<field var="muc#roomconfig_presencebroadcast"><value>moderator</value></field>`+
  1296. `<field var="muc#roomconfig_getmemberlist"><value>moderator</value>,<value>participant</value>,<value>visitor</value></field>`+
  1297. `<field var="muc#roomconfig_publicroom"><value>0</value></field>`+
  1298. `<field var="muc#roomconfig_persistentroom"><value>0</value></field>`+
  1299. `<field var="muc#roomconfig_moderatedroom"><value>1</value></field>`+
  1300. `<field var="muc#roomconfig_membersonly"><value>1</value></field>`+
  1301. `<field var="muc#roomconfig_passwordprotectedroom"><value>1</value></field>`+
  1302. `<field var="muc#roomconfig_roomsecret"><value>cauldronburn</value></field>`+
  1303. `</x>`+
  1304. `</query>`+
  1305. `</iq>`);
  1306. }));
  1307. it("properly handles notification that a room has been destroyed",
  1308. mock.initConverse([], {}, async function (_converse) {
  1309. const { api } = _converse;
  1310. const muc_jid = 'problematic@muc.montague.lit';
  1311. api.rooms.open(muc_jid, { nick: 'romeo' });
  1312. await mock.waitForMUCDiscoInfo(_converse, muc_jid);
  1313. const presence =
  1314. stx`<presence from="problematic@muc.montague.lit"
  1315. id="n13mt3l"
  1316. type="error"
  1317. to="romeo@montague.lit/pda"
  1318. xmlns="jabber:client">
  1319. <error type="cancel">
  1320. <gone xmlns="urn:ietf:params:xml:ns:xmpp-stanzas">xmpp:other-room@chat.jabberfr.org?join</gone>
  1321. <text xmlns="urn:ietf:params:xml:ns:xmpp-stanzas">We didn't like the name</text>
  1322. </error>
  1323. </presence>`;
  1324. const view = await u.waitUntil(() => _converse.chatboxviews.get('problematic@muc.montague.lit'));
  1325. _converse.api.connection.get()._dataRecv(mock.createRequest(presence));
  1326. const msg = await u.waitUntil(() => view.querySelector('.chatroom-body .disconnect-msg'));
  1327. expect(msg.textContent.trim()).toBe('This groupchat no longer exists');
  1328. expect(view.querySelector('.chatroom-body .destroyed-reason').textContent.trim())
  1329. .toBe(`The following reason was given: "We didn't like the name"`);
  1330. expect(view.querySelector('.chatroom-body .moved-label').textContent.trim())
  1331. .toBe('The conversation has moved to a new address. Click the link below to enter.');
  1332. expect(view.querySelector('.chatroom-body .moved-link').textContent.trim())
  1333. .toBe(`other-room@chat.jabberfr.org`);
  1334. }));
  1335. it("allows the user to invite their roster contacts to enter the groupchat",
  1336. mock.initConverse(['chatBoxesFetched'], {'view_mode': 'overlayed'}, async function (_converse) {
  1337. // We need roster contacts, so that we have someone to invite
  1338. await mock.waitForRoster(_converse, 'current');
  1339. const features = [
  1340. 'http://jabber.org/protocol/muc',
  1341. 'jabber:iq:register',
  1342. 'muc_passwordprotected',
  1343. 'muc_hidden',
  1344. 'muc_temporary',
  1345. 'muc_membersonly',
  1346. 'muc_unmoderated',
  1347. 'muc_anonymous'
  1348. ]
  1349. await mock.openAndEnterMUC(_converse, 'lounge@montague.lit', 'romeo', features);
  1350. const view = _converse.chatboxviews.get('lounge@montague.lit');
  1351. expect(view.model.getOwnAffiliation()).toBe('owner');
  1352. expect(view.model.features.get('open')).toBe(false);
  1353. await u.waitUntil(() => view.querySelector('.open-invite-modal'));
  1354. // Members can't invite if the room isn't open
  1355. view.model.getOwnOccupant().set('affiliation', 'member');
  1356. await u.waitUntil(() => view.querySelector('.open-invite-modal') === null);
  1357. view.model.features.set('open', 'true');
  1358. await u.waitUntil(() => view.querySelector('.open-invite-modal'));
  1359. view.querySelector('.open-invite-modal').click();
  1360. const modal = _converse.api.modal.get('converse-muc-invite-modal');
  1361. await u.waitUntil(() => u.isVisible(modal), 1000)
  1362. expect(modal.querySelectorAll('#invitee_jids').length).toBe(1);
  1363. expect(modal.querySelectorAll('textarea').length).toBe(1);
  1364. spyOn(view.model, 'directInvite').and.callThrough();
  1365. const input = modal.querySelector('#invitee_jids input');
  1366. input.value = "Balt";
  1367. modal.querySelector('input[type="submit"]').click();
  1368. await u.waitUntil(() => modal.querySelector('.error'));
  1369. const error = modal.querySelector('.error');
  1370. expect(error.textContent).toBe('Please enter a valid XMPP address');
  1371. let evt = new Event('input');
  1372. input.dispatchEvent(evt);
  1373. let sent_stanza;
  1374. spyOn(_converse.api.connection.get(), 'send').and.callFake(stanza => (sent_stanza = stanza));
  1375. const hint = await u.waitUntil(() => modal.querySelector('.suggestion-box__results li'));
  1376. expect(input.value).toBe('Balt');
  1377. expect(hint.textContent.trim()).toBe('Balthasar');
  1378. evt = new Event('mousedown', {'bubbles': true});
  1379. evt.button = 0;
  1380. hint.dispatchEvent(evt);
  1381. const textarea = modal.querySelector('textarea');
  1382. textarea.value = "Please join!";
  1383. modal.querySelector('input[type="submit"]').click();
  1384. expect(view.model.directInvite).toHaveBeenCalled();
  1385. expect(Strophe.serialize(sent_stanza)).toBe(
  1386. `<message from="romeo@montague.lit/orchard" `+
  1387. `id="${sent_stanza.getAttribute("id")}" `+
  1388. `to="balthasar@montague.lit" `+
  1389. `xmlns="jabber:client">`+
  1390. `<x jid="lounge@montague.lit" reason="Please join!" xmlns="jabber:x:conference"/>`+
  1391. `</message>`
  1392. );
  1393. }));
  1394. it("can be joined automatically, based on a received invite",
  1395. mock.initConverse([], { lazy_load_vcards: false }, async function (_converse) {
  1396. await mock.waitForRoster(_converse, 'current'); // We need roster contacts, who can invite us
  1397. const muc_jid = 'lounge@montague.lit';
  1398. const name = mock.cur_names[0];
  1399. const from_jid = name.replace(/ /g,'.').toLowerCase() + '@montague.lit';
  1400. await u.waitUntil(() => _converse.roster.get(from_jid).vcard.get('fullname'));
  1401. spyOn(_converse.api, 'confirm').and.callFake(() => Promise.resolve(true));
  1402. expect(_converse.chatboxes.models.length).toBe(1);
  1403. expect(_converse.chatboxes.models[0].id).toBe("controlbox");
  1404. const reason = "Please join this groupchat";
  1405. const stanza = stx`
  1406. <message xmlns="jabber:client"
  1407. to="${_converse.bare_jid}"
  1408. from="${from_jid}"
  1409. id="9bceb415-f34b-4fa4-80d5-c0d076a24231">
  1410. <x xmlns="jabber:x:conference" jid="${muc_jid}" reason="${reason}"/>
  1411. </message>`.tree();
  1412. const promise = _converse.onDirectMUCInvitation(stanza);
  1413. await mock.waitForMUCDiscoInfo(_converse, muc_jid);
  1414. await mock.waitForReservedNick(_converse, muc_jid, 'romeo');
  1415. await mock.receiveOwnMUCPresence(_converse, muc_jid, 'romeo');
  1416. await promise;
  1417. expect(_converse.api.confirm).toHaveBeenCalledWith(
  1418. name + ' has invited you to join a groupchat: '+ muc_jid +
  1419. ', and left the following reason: "'+reason+'"');
  1420. expect(_converse.chatboxes.models.length).toBe(2);
  1421. expect(_converse.chatboxes.models[0].id).toBe('controlbox');
  1422. expect(_converse.chatboxes.models[1].id).toBe(muc_jid);
  1423. }));
  1424. it("shows received groupchat messages",
  1425. mock.initConverse([], {}, async function (_converse) {
  1426. const text = 'This is a received message';
  1427. await mock.openAndEnterMUC(_converse, 'lounge@montague.lit', 'romeo');
  1428. spyOn(_converse.api, "trigger").and.callThrough();
  1429. const view = _converse.chatboxviews.get('lounge@montague.lit');
  1430. const nick = mock.chatroom_names[0];
  1431. view.model.occupants.create({
  1432. 'nick': nick,
  1433. 'muc_jid': `${view.model.get('jid')}/${nick}`
  1434. });
  1435. const message =
  1436. stx`<message
  1437. from="lounge@montague.lit/${nick}"
  1438. id="1"
  1439. to="romeo@montague.lit"
  1440. type="groupchat"
  1441. xmlns="jabber:client">
  1442. <body>${text}</body>
  1443. </message>`;
  1444. await view.model.handleMessageStanza(message.tree());
  1445. await u.waitUntil(() => view.querySelectorAll('.chat-msg').length);
  1446. expect(view.querySelectorAll('.chat-msg').length).toBe(1);
  1447. expect(view.querySelector('.chat-msg__text').textContent.trim()).toBe(text);
  1448. expect(_converse.api.trigger).toHaveBeenCalledWith('message', jasmine.any(Object));
  1449. }));
  1450. it("shows sent groupchat messages", mock.initConverse([], {}, async function (_converse) {
  1451. await mock.openAndEnterMUC(_converse, 'lounge@montague.lit', 'romeo');
  1452. spyOn(_converse.api, "trigger").and.callThrough();
  1453. const view = _converse.chatboxviews.get('lounge@montague.lit');
  1454. const text = 'This is a sent message';
  1455. const textarea = await u.waitUntil(() => view.querySelector('.chat-textarea'));
  1456. textarea.value = text;
  1457. const message_form = view.querySelector('converse-muc-message-form');
  1458. message_form.onKeyDown({
  1459. target: textarea,
  1460. preventDefault: function preventDefault () {},
  1461. keyCode: 13
  1462. });
  1463. await u.waitUntil(() => view.querySelectorAll('.chat-msg__text').length);
  1464. expect(_converse.api.trigger).toHaveBeenCalledWith('sendMessage', jasmine.any(Object));
  1465. expect(view.querySelectorAll('.chat-msg').length).toBe(1);
  1466. // Let's check that if we receive the same message again, it's
  1467. // not shown.
  1468. const stanza = stx`
  1469. <message xmlns="jabber:client"
  1470. from="lounge@montague.lit/romeo"
  1471. to="${_converse.api.connection.get().jid}"
  1472. type="groupchat">
  1473. <body>${text}</body>
  1474. <stanza-id xmlns="urn:xmpp:sid:0"
  1475. id="5f3dbc5e-e1d3-4077-a492-693f3769c7ad"
  1476. by="lounge@montague.lit"/>
  1477. <origin-id xmlns="urn:xmpp:sid:0" id="${view.model.messages.at(0).get('origin_id')}"/>
  1478. </message>`;
  1479. await view.model.handleMessageStanza(stanza);
  1480. expect(view.querySelectorAll('.chat-msg').length).toBe(1);
  1481. expect(sizzle('.chat-msg__text:last').pop().textContent.trim()).toBe(text);
  1482. expect(view.model.messages.length).toBe(1);
  1483. // We don't emit an event if it's our own message
  1484. expect(_converse.api.trigger.calls.count(), 1);
  1485. }));
  1486. it("will cause the chat area to be scrolled down only if it was at the bottom already",
  1487. mock.initConverse([], {}, async function (_converse) {
  1488. const message = 'This message is received while the chat area is scrolled up';
  1489. await mock.openAndEnterMUC(_converse, 'lounge@montague.lit', 'romeo');
  1490. const view = _converse.chatboxviews.get('lounge@montague.lit');
  1491. // Create enough messages so that there's a scrollbar.
  1492. const promises = [];
  1493. for (let i=0; i<20; i++) {
  1494. promises.push(
  1495. view.model.handleMessageStanza(
  1496. stx`<message from="lounge@montague.lit/someone"
  1497. to="romeo@montague.lit.com"
  1498. type="groupchat"
  1499. id="${u.getUniqueId()}"
  1500. xmlns="jabber:client">
  1501. <body>Message: ${i}</body>
  1502. </message>`
  1503. ));
  1504. }
  1505. await Promise.all(promises);
  1506. const promise = u.getOpenPromise();
  1507. // Give enough time for `markScrolled` to have been called
  1508. setTimeout(async () => {
  1509. const content = view.querySelector('.chat-content');
  1510. content.scrollTop = 0;
  1511. await view.model.handleMessageStanza(
  1512. stx`<message from="lounge@montague.lit/someone"
  1513. to="romeo@montague.lit.com"
  1514. type="groupchat"
  1515. id="${u.getUniqueId()}"
  1516. xmlns="jabber:client">
  1517. <body>${message}</body>
  1518. </message>`
  1519. );
  1520. await u.waitUntil(() => view.querySelectorAll('.chat-msg__text').length === 21);
  1521. // Now check that the message appears inside the chatbox in the DOM
  1522. const msg_txt = sizzle('.chat-msg:last .chat-msg__text', content).pop().textContent;
  1523. expect(msg_txt).toEqual(message);
  1524. expect(content.scrollTop).toBe(0);
  1525. promise.resolve();
  1526. }, 500);
  1527. return promise;
  1528. }));
  1529. it("informs users if the room configuration has changed",
  1530. mock.initConverse([], {}, async function (_converse) {
  1531. const muc_jid = 'coven@chat.shakespeare.lit';
  1532. await mock.openAndEnterMUC(_converse, 'coven@chat.shakespeare.lit', 'romeo');
  1533. const view = _converse.chatboxviews.get(muc_jid);
  1534. expect(view.model.session.get('connection_status')).toBe(converse.ROOMSTATUS.ENTERED);
  1535. const stanza = stx`
  1536. <message from="${muc_jid}"
  1537. id="80349046-F26A-44F3-A7A6-54825064DD9E"
  1538. to="${_converse.jid}"
  1539. type="groupchat"
  1540. xmlns="jabber:client">
  1541. <x xmlns="http://jabber.org/protocol/muc#user">
  1542. <status code="170"/>
  1543. </x>
  1544. </message>`;
  1545. _converse.api.connection.get()._dataRecv(mock.createRequest(stanza));
  1546. await u.waitUntil(() => view.querySelectorAll('.chat-content .chat-info').length);
  1547. const info_messages = view.querySelectorAll('.chat-content .chat-info');
  1548. expect(info_messages[0].textContent.trim()).toBe('Groupchat logging is now enabled');
  1549. }));
  1550. it("queries for the groupchat information before attempting to join the user",
  1551. mock.initConverse(['chatBoxesFetched'], {}, async function (_converse) {
  1552. const nick = "some1";
  1553. const IQ_stanzas = _converse.api.connection.get().IQ_stanzas;
  1554. const muc_jid = 'coven@chat.shakespeare.lit';
  1555. _converse.api.rooms.open(muc_jid, { nick });
  1556. const stanza = await u.waitUntil(() => IQ_stanzas.filter(
  1557. iq => iq.querySelector(
  1558. `iq[to="${muc_jid}"] query[xmlns="http://jabber.org/protocol/disco#info"]`
  1559. )).pop());
  1560. // Check that the groupchat queried for the feautures.
  1561. expect(Strophe.serialize(stanza)).toBe(
  1562. `<iq from="romeo@montague.lit/orchard" id="${stanza.getAttribute("id")}" to="${muc_jid}" type="get" xmlns="jabber:client">`+
  1563. `<query xmlns="http://jabber.org/protocol/disco#info"/>`+
  1564. `</iq>`);
  1565. const features_stanza =
  1566. stx`<iq from="${muc_jid}"
  1567. id="${stanza.getAttribute('id')}"
  1568. to="romeo@montague.lit/desktop"
  1569. type="result"
  1570. xmlns="jabber:client">
  1571. <query xmlns="http://jabber.org/protocol/disco#info">
  1572. <identity category="conference" name="A Dark Cave" type="text"/>
  1573. <feature var="http://jabber.org/protocol/muc"/>
  1574. <feature var="muc_passwordprotected"/>
  1575. <feature var="muc_hidden"/>
  1576. <feature var="muc_temporary"/>
  1577. <feature var="muc_open"/>
  1578. <feature var="muc_unmoderated"/>
  1579. <feature var="muc_nonanonymous"/>
  1580. </query>
  1581. </iq>`;
  1582. _converse.api.connection.get()._dataRecv(mock.createRequest(features_stanza));
  1583. let view = _converse.chatboxviews.get('coven@chat.shakespeare.lit');
  1584. const sent_stanzas = _converse.api.connection.get().sent_stanzas;
  1585. await u.waitUntil(() => sent_stanzas.filter(s => s.matches(`presence[to="${muc_jid}/${nick}"]`)).pop());
  1586. view = _converse.chatboxviews.get('coven@chat.shakespeare.lit');
  1587. expect(view.model.features.get('fetched')).toBeTruthy();
  1588. expect(view.model.features.get('passwordprotected')).toBe(true);
  1589. expect(view.model.features.get('hidden')).toBe(true);
  1590. expect(view.model.features.get('temporary')).toBe(true);
  1591. expect(view.model.features.get('open')).toBe(true);
  1592. expect(view.model.features.get('unmoderated')).toBe(true);
  1593. expect(view.model.features.get('nonanonymous')).toBe(true);
  1594. }));
  1595. it("updates the shown features when the groupchat configuration has changed",
  1596. mock.initConverse([], {'view_mode': 'fullscreen'}, async function (_converse) {
  1597. let features = [
  1598. 'http://jabber.org/protocol/muc',
  1599. 'jabber:iq:register',
  1600. 'muc_passwordprotected',
  1601. 'muc_publicroom',
  1602. 'muc_temporary',
  1603. 'muc_open',
  1604. 'muc_unmoderated',
  1605. 'muc_nonanonymous'
  1606. ];
  1607. const muc_jid = 'room@conference.example.org';
  1608. await mock.openAndEnterMUC(_converse, muc_jid, 'romeo', features);
  1609. const view = _converse.chatboxviews.get(muc_jid);
  1610. const info_el = view.querySelector(".show-muc-details-modal");
  1611. info_el.click();
  1612. let modal = _converse.api.modal.get('converse-muc-details-modal');
  1613. await u.waitUntil(() => u.isVisible(modal), 1000);
  1614. let features_list = modal.querySelector('.features-list');
  1615. let features_shown = Array.from(features_list.children).map((e) => e.textContent);
  1616. expect(features_shown.length).toBe(5);
  1617. expect(features_shown.join(' ')).toBe(
  1618. 'Password protected - This groupchat requires a password before entry '+
  1619. 'Open - Anyone can join this groupchat '+
  1620. 'Temporary - This groupchat will disappear once the last person leaves '+
  1621. 'Not anonymous - All other groupchat participants can see your XMPP address '+
  1622. 'Not moderated - Participants entering this groupchat can write right away');
  1623. expect(view.model.features.get('hidden')).toBe(false);
  1624. expect(view.model.features.get('mam_enabled')).toBe(false);
  1625. expect(view.model.features.get('membersonly')).toBe(false);
  1626. expect(view.model.features.get('moderated')).toBe(false);
  1627. expect(view.model.features.get('nonanonymous')).toBe(true);
  1628. expect(view.model.features.get('open')).toBe(true);
  1629. expect(view.model.features.get('passwordprotected')).toBe(true);
  1630. expect(view.model.features.get('persistent')).toBe(false);
  1631. expect(view.model.features.get('publicroom')).toBe(true);
  1632. expect(view.model.features.get('semianonymous')).toBe(false);
  1633. expect(view.model.features.get('temporary')).toBe(true);
  1634. expect(view.model.features.get('unmoderated')).toBe(true);
  1635. expect(view.model.features.get('unsecured')).toBe(false);
  1636. await u.waitUntil(() => view.querySelector('.chatbox-title__text').textContent.trim() === 'Room');
  1637. modal.querySelector('.btn-close').click();
  1638. view.querySelector('.configure-chatroom-button').click();
  1639. const IQs = _converse.api.connection.get().IQ_stanzas;
  1640. const s = `iq[to="${muc_jid}"] query[xmlns="${Strophe.NS.MUC_OWNER}"]`;
  1641. let iq = await u.waitUntil(() => IQs.filter(iq => iq.querySelector(s)).pop());
  1642. const response_el = stx`<iq xmlns="jabber:client"
  1643. type="result"
  1644. to="romeo@montague.lit/pda"
  1645. from="room@conference.example.org" id="${iq.getAttribute('id')}">
  1646. <query xmlns="http://jabber.org/protocol/muc#owner">
  1647. <x xmlns="jabber:x:data" type="form">
  1648. <title>Configuration for room@conference.example.org</title>
  1649. <instructions>Complete and submit this form to configure the room.</instructions>
  1650. <field var="FORM_TYPE" type="hidden">
  1651. <value>http://jabber.org/protocol/muc#roomconfig</value>
  1652. </field>
  1653. <field type="fixed">
  1654. <value>Room information</value>
  1655. </field>
  1656. <field var="muc#roomconfig_roomname" type="text-single" label="Title">
  1657. <value>Room</value>
  1658. </field>
  1659. <field var="muc#roomconfig_roomdesc" type="text-single" label="Description">
  1660. <desc>A brief description of the room</desc>
  1661. <value>This room is used in tests</value>
  1662. </field>
  1663. <field var="muc#roomconfig_lang" type="text-single" label="Language tag for room (e.g. 'en', 'de', 'fr' etc.)">
  1664. <desc>Indicate the primary language spoken in this room</desc>
  1665. <value>en</value>
  1666. </field>
  1667. <field var="muc#roomconfig_persistentroom" type="boolean" label="Persistent (room should remain even when it is empty)">
  1668. <desc>Rooms are automatically deleted when they are empty, unless this option is enabled</desc>
  1669. <value>1</value>
  1670. </field>
  1671. <field var="muc#roomconfig_publicroom" type="boolean" label="Include room information in public lists">
  1672. <desc>Enable this to allow people to find the room</desc>
  1673. <value>1</value>
  1674. </field>
  1675. <field type="fixed"><value>Access to the room</value></field>
  1676. <field var="muc#roomconfig_roomsecret" type="text-private" label="Password"><value/></field>
  1677. <field var="muc#roomconfig_membersonly" type="boolean" label="Only allow members to join">
  1678. <desc>Enable this to only allow access for room owners, admins and members</desc>
  1679. </field>
  1680. <field var="{http://prosody.im/protocol/muc}roomconfig_allowmemberinvites" type="boolean" label="Allow members to invite new members"/>
  1681. <field type="fixed"><value>Permissions in the room</value>
  1682. </field>
  1683. <field var="muc#roomconfig_changesubject" type="boolean" label="Allow anyone to set the room's subject">
  1684. <desc>Choose whether anyone, or only moderators, may set the room's subject</desc>
  1685. </field>
  1686. <field var="muc#roomconfig_moderatedroom" type="boolean" label="Moderated (require permission to speak)">
  1687. <desc>In moderated rooms occupants must be given permission to speak by a room moderator</desc>
  1688. </field>
  1689. <field var="muc#roomconfig_whois" type="list-single" label="Addresses (JIDs) of room occupants may be viewed by:">
  1690. <option label="Moderators only"><value>moderators</value></option>
  1691. <option label="Anyone"><value>anyone</value></option>
  1692. <value>anyone</value>
  1693. </field>
  1694. <field type="fixed"><value>Other options</value></field>
  1695. <field var="muc#roomconfig_historylength" type="text-single" label="Maximum number of history messages returned by room">
  1696. <desc>Specify the maximum number of previous messages that should be sent to users when they join the room</desc>
  1697. <value>50</value>
  1698. </field>
  1699. <field var="muc#roomconfig_defaulthistorymessages" type="text-single" label="Default number of history messages returned by room">
  1700. <desc>Specify the number of previous messages sent to new users when they join the room</desc>
  1701. <value>20</value>
  1702. </field>
  1703. </x>
  1704. </query>
  1705. </iq>`;
  1706. _converse.api.connection.get()._dataRecv(mock.createRequest(response_el));
  1707. modal = _converse.api.modal.get('converse-muc-config-modal');
  1708. await u.waitUntil(() => modal.querySelector('.chatroom-form input'));
  1709. expect(modal.querySelector('.chatroom-form legend').textContent.trim()).toBe("Configuration for room@conference.example.org");
  1710. sizzle('[name="muc#roomconfig_membersonly"]', modal).pop().click();
  1711. sizzle('[name="muc#roomconfig_roomname"]', modal).pop().value = "New room name"
  1712. modal.querySelector('.chatroom-form input[type="submit"]').click();
  1713. iq = await u.waitUntil(() => IQs.filter(iq => iq.matches(`iq[to="${muc_jid}"][type="set"]`)).pop());
  1714. const result =
  1715. stx`<iq xmlns="jabber:client"
  1716. type="result"
  1717. to="romeo@montague.lit/orchard"
  1718. from="lounge@muc.montague.lit"
  1719. id="${iq.getAttribute('id')}"/>`;
  1720. IQs.length = 0; // Empty the array
  1721. _converse.api.connection.get()._dataRecv(mock.createRequest(result));
  1722. iq = await u.waitUntil(() => IQs.filter(
  1723. iq => iq.querySelector(
  1724. `iq[to="${muc_jid}"] query[xmlns="http://jabber.org/protocol/disco#info"]`
  1725. )).pop());
  1726. features = [
  1727. 'http://jabber.org/protocol/muc',
  1728. 'jabber:iq:register',
  1729. 'muc_passwordprotected',
  1730. 'muc_hidden',
  1731. 'muc_temporary',
  1732. 'muc_membersonly',
  1733. 'muc_unmoderated',
  1734. 'muc_nonanonymous'
  1735. ];
  1736. const features_stanza =
  1737. stx`<iq from="${muc_jid}"
  1738. id="${iq.getAttribute('id')}"
  1739. to="romeo@montague.lit/desktop"
  1740. type="result"
  1741. xmlns="jabber:client">
  1742. <query xmlns="http://jabber.org/protocol/disco#info">
  1743. <identity category="conference" name="New room name" type="text"/>
  1744. ${features.map(f => stx`<feature var="${f}"/>`)}
  1745. <x xmlns="jabber:x:data" type="result">
  1746. <field var="FORM_TYPE" type="hidden">
  1747. <value>http://jabber.org/protocol/muc#roominfo</value>
  1748. </field>
  1749. <field type="text-single" var="muc#roominfo_description" label="Description">
  1750. <value>This is the description</value>
  1751. </field>
  1752. <field type="text-single" var="muc#roominfo_occupants" label="Number of occupants">
  1753. <value>0</value>
  1754. </field>
  1755. </x>
  1756. </query>
  1757. </iq>`;
  1758. _converse.api.connection.get()._dataRecv(mock.createRequest(features_stanza));
  1759. await u.waitUntil(() => new Promise(success => view.model.features.on('change', success)));
  1760. info_el.click();
  1761. modal = _converse.api.modal.get('converse-muc-details-modal');
  1762. await u.waitUntil(() => u.isVisible(modal), 1000);
  1763. features_list = modal.querySelector('.features-list');
  1764. features_shown = Array.from(features_list.children).map((e) => e.textContent);
  1765. expect(features_shown.length).toBe(6);
  1766. expect(features_shown.join(' ')).toBe(
  1767. 'Password protected - This groupchat requires a password before entry '+
  1768. 'Hidden - This groupchat is not publicly searchable '+
  1769. 'Members only - This groupchat is restricted to members only '+
  1770. 'Temporary - This groupchat will disappear once the last person leaves '+
  1771. 'Not anonymous - All other groupchat participants can see your XMPP address '+
  1772. 'Not moderated - Participants entering this groupchat can write right away');
  1773. expect(view.model.features.get('hidden')).toBe(true);
  1774. expect(view.model.features.get('mam_enabled')).toBe(false);
  1775. expect(view.model.features.get('membersonly')).toBe(true);
  1776. expect(view.model.features.get('moderated')).toBe(false);
  1777. expect(view.model.features.get('nonanonymous')).toBe(true);
  1778. expect(view.model.features.get('open')).toBe(false);
  1779. expect(view.model.features.get('passwordprotected')).toBe(true);
  1780. expect(view.model.features.get('persistent')).toBe(false);
  1781. expect(view.model.features.get('publicroom')).toBe(false);
  1782. expect(view.model.features.get('semianonymous')).toBe(false);
  1783. expect(view.model.features.get('temporary')).toBe(true);
  1784. expect(view.model.features.get('unmoderated')).toBe(true);
  1785. expect(view.model.features.get('unsecured')).toBe(false);
  1786. await u.waitUntil(() => view.querySelector('.chatbox-title__text')?.textContent.trim() === 'New room name');
  1787. }));
  1788. it("indicates when a room is no longer anonymous",
  1789. mock.initConverse([], {}, async function (_converse) {
  1790. await mock.openAndEnterMUC(_converse, 'coven@chat.shakespeare.lit', 'some1');
  1791. const message =
  1792. stx`<message xmlns="jabber:client"
  1793. type="groupchat"
  1794. to="romeo@montague.lit/_converse.js-27854181"
  1795. from="coven@chat.shakespeare.lit">
  1796. <x xmlns="http://jabber.org/protocol/muc#user">
  1797. <status code="104"/>
  1798. <status code="172"/>
  1799. </x>
  1800. </message>`;
  1801. _converse.api.connection.get()._dataRecv(mock.createRequest(message));
  1802. const view = _converse.chatboxviews.get('coven@chat.shakespeare.lit');
  1803. await u.waitUntil(() => view.querySelectorAll('.chat-content .chat-info').length);
  1804. const chat_body = view.querySelector('.chatroom-body');
  1805. expect(sizzle('.message:last', chat_body).pop().textContent.trim())
  1806. .toBe('This groupchat is now no longer anonymous');
  1807. }));
  1808. it("informs users if they have been kicked out of the groupchat",
  1809. mock.initConverse([], {}, async function (_converse) {
  1810. await mock.openAndEnterMUC(_converse, 'lounge@montague.lit', 'romeo');
  1811. const view = _converse.chatboxviews.get('lounge@montague.lit');
  1812. expect(view.model.session.get('connection_status')).toBe(converse.ROOMSTATUS.ENTERED);
  1813. const presence =
  1814. stx`<presence from='lounge@montague.lit/romeo'
  1815. to='romeo@montague.lit/pda'
  1816. type='unavailable'
  1817. xmlns="jabber:client">
  1818. <x xmlns='http://jabber.org/protocol/muc#user'>
  1819. <item affiliation='none' jid='romeo@montague.lit/pda' role='none'>
  1820. <actor nick='Fluellen'/>
  1821. <reason>Avaunt, you cullion!</reason>
  1822. </item>
  1823. <status code='110'/>
  1824. <status code='307'/>
  1825. </x>
  1826. </presence>`;
  1827. _converse.api.connection.get()._dataRecv(mock.createRequest(presence));
  1828. await u.waitUntil(() => !u.isVisible(view.querySelector('.chat-area')));
  1829. expect(u.isVisible(view.querySelector('.occupants'))).toBeFalsy();
  1830. const chat_body = view.querySelector('.chatroom-body');
  1831. expect(chat_body.querySelectorAll('.disconnect-msg').length).toBe(3);
  1832. expect(chat_body.querySelector('.disconnect-msg:first-child').textContent.trim()).toBe(
  1833. 'You have been kicked from this groupchat');
  1834. expect(chat_body.querySelector('.disconnect-msg:nth-child(2)').textContent.trim()).toBe(
  1835. 'This action was done by Fluellen.');
  1836. expect(chat_body.querySelector('.disconnect-msg:nth-child(3)').textContent.trim()).toBe(
  1837. 'The reason given is: "Avaunt, you cullion!".');
  1838. expect(view.model.session.get('connection_status')).toBe(converse.ROOMSTATUS.DISCONNECTED);
  1839. }));
  1840. it("informs users if they have exited the groupchat due to a technical reason",
  1841. mock.initConverse([], {}, async function (_converse) {
  1842. await mock.openAndEnterMUC(_converse, 'lounge@montague.lit', 'romeo');
  1843. const presence =
  1844. stx`<presence from='lounge@montague.lit/romeo'
  1845. to='romeo@montague.lit/pda'
  1846. type='unavailable'
  1847. xmlns="jabber:client">
  1848. <x xmlns='http://jabber.org/protocol/muc#user'>
  1849. <item affiliation='none' jid='romeo@montague.lit/pda' role='none'>
  1850. <reason>Flux capacitor overload!</reason>
  1851. </item>
  1852. <status code='110'/>
  1853. <status code='333'/>
  1854. <status code='307'/>
  1855. </x>
  1856. </presence>`;
  1857. _converse.api.connection.get()._dataRecv(mock.createRequest(presence));
  1858. const view = _converse.chatboxviews.get('lounge@montague.lit');
  1859. await u.waitUntil(() => !u.isVisible(view.querySelector('.chat-area')));
  1860. expect(u.isVisible(view.querySelector('.occupants'))).toBeFalsy();
  1861. const chat_body = view.querySelector('.chatroom-body');
  1862. expect(chat_body.querySelectorAll('.disconnect-msg').length).toBe(2);
  1863. expect(chat_body.querySelector('.disconnect-msg:first-child').textContent.trim()).toBe(
  1864. 'You have exited this groupchat due to a technical problem');
  1865. expect(chat_body.querySelector('.disconnect-msg:nth-child(2)').textContent.trim()).toBe(
  1866. 'The reason given is: "Flux capacitor overload!".');
  1867. }));
  1868. it("can be saved to, and retrieved from, browserStorage",
  1869. mock.initConverse(['chatBoxesFetched'], {}, async function (_converse) {
  1870. const { api } = _converse;
  1871. const muc_jid = 'lounge@montague.lit';
  1872. await mock.openAndEnterMUC(_converse, muc_jid, 'romeo');
  1873. // We instantiate a new ChatBoxes collection, which by default
  1874. // will be empty.
  1875. await mock.openControlBox(_converse);
  1876. const newchatboxes = new _converse.ChatBoxes();
  1877. expect(newchatboxes.length).toEqual(0);
  1878. // The chatboxes will then be fetched from browserStorage inside the
  1879. // onConnected method
  1880. newchatboxes.onConnected();
  1881. await new Promise(resolve => _converse.api.listen.once('chatBoxesFetched', resolve));
  1882. expect(newchatboxes.length).toEqual(2);
  1883. // Check that the chatrooms retrieved from browserStorage
  1884. // have the same attributes values as the original ones.
  1885. const attrs = ['id', 'box_id', 'visible'];
  1886. let new_attrs, old_attrs;
  1887. for (let i=0; i<attrs.length; i++) {
  1888. new_attrs = newchatboxes.models.map(m => m.attributes[attrs[i]]);
  1889. old_attrs = _converse.chatboxes.models.map(m => m.attributes[attrs[i]]);
  1890. expect(new_attrs.sort()).toEqual(old_attrs.sort());
  1891. }
  1892. }));
  1893. it("can be closed again by clicking a DOM element with class 'close-chatbox-button'",
  1894. mock.initConverse(['chatBoxesFetched'], {}, async function (_converse) {
  1895. const muc_jid = 'lounge@montague.lit';
  1896. const model = await mock.openAndEnterMUC(_converse, muc_jid, 'romeo');
  1897. spyOn(model, 'close').and.callThrough();
  1898. spyOn(_converse.api, "trigger").and.callThrough();
  1899. spyOn(model, 'leave');
  1900. spyOn(_converse.api, 'confirm').and.callFake(() => Promise.resolve(true));
  1901. const view = await u.waitUntil(() => _converse.chatboxviews.get('lounge@montague.lit'));
  1902. const button = await u.waitUntil(() => view.querySelector('.close-chatbox-button'));
  1903. button.click();
  1904. await u.waitUntil(() => model.close.calls.count());
  1905. expect(model.leave).toHaveBeenCalled();
  1906. await u.waitUntil(() => _converse.api.trigger.calls.count());
  1907. expect(_converse.api.trigger).toHaveBeenCalledWith('chatBoxClosed', jasmine.any(Object));
  1908. }));
  1909. it("informs users of role and affiliation changes",
  1910. mock.initConverse([], {}, async function (_converse) {
  1911. const muc_jid = 'lounge@montague.lit';
  1912. await mock.openAndEnterMUC(_converse, muc_jid, 'romeo');
  1913. const view = _converse.chatboxviews.get(muc_jid);
  1914. let presence =
  1915. stx`<presence from='lounge@montague.lit/annoyingGuy'
  1916. id='27C55F89-1C6A-459A-9EB5-77690145D624'
  1917. to='romeo@montague.lit/desktop'
  1918. xmlns="jabber:client">
  1919. <x xmlns='http://jabber.org/protocol/muc#user'>
  1920. <item jid='annoyingguy@montague.lit' affiliation='member' role='participant'/>
  1921. </x>
  1922. </presence>`;
  1923. _converse.api.connection.get()._dataRecv(mock.createRequest(presence));
  1924. await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent.trim() ===
  1925. "romeo and annoyingGuy have entered the groupchat");
  1926. presence =
  1927. stx`<presence from='lounge@montague.lit/annoyingGuy'
  1928. to='romeo@montague.lit/desktop'
  1929. xmlns="jabber:client">
  1930. <x xmlns='http://jabber.org/protocol/muc#user'>
  1931. <item jid='annoyingguy@montague.lit' affiliation='member' role='visitor'/>
  1932. </x>
  1933. </presence>`;
  1934. _converse.api.connection.get()._dataRecv(mock.createRequest(presence));
  1935. await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent.trim() ===
  1936. "romeo has entered the groupchat\nannoyingGuy has been muted");
  1937. presence =
  1938. stx`<presence from='lounge@montague.lit/annoyingGuy'
  1939. to='romeo@montague.lit/desktop'
  1940. xmlns="jabber:client">
  1941. <x xmlns='http://jabber.org/protocol/muc#user'>
  1942. <item jid='annoyingguy@montague.lit' affiliation='member' role='participant'/>
  1943. </x>
  1944. </presence>`;
  1945. _converse.api.connection.get()._dataRecv(mock.createRequest(presence));
  1946. await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent.trim() ===
  1947. "romeo has entered the groupchat\nannoyingGuy has been given a voice");
  1948. // Check that we don't see an info message concerning the role,
  1949. // if the affiliation has changed.
  1950. presence =
  1951. stx`<presence from='lounge@montague.lit/annoyingGuy'
  1952. to='romeo@montague.lit/desktop'
  1953. xmlns="jabber:client">
  1954. <x xmlns='http://jabber.org/protocol/muc#user'>
  1955. <item jid='annoyingguy@montague.lit' affiliation='none' role='visitor'/>
  1956. </x>
  1957. </presence>`;
  1958. _converse.api.connection.get()._dataRecv(mock.createRequest(presence));
  1959. await u.waitUntil(() =>
  1960. Array.from(view.querySelectorAll('.chat-info__message')).pop()?.textContent.trim() ===
  1961. "annoyingGuy is no longer a member of this groupchat"
  1962. );
  1963. expect(1).toBe(1);
  1964. }));
  1965. it("notifies users of role and affiliation changes for members not currently in the groupchat",
  1966. mock.initConverse([], {}, async function (_converse) {
  1967. const muc_jid = 'lounge@montague.lit';
  1968. await mock.openAndEnterMUC(_converse, muc_jid, 'romeo');
  1969. const view = _converse.chatboxviews.get(muc_jid);
  1970. let message =
  1971. stx`<message from="lounge@montague.lit"
  1972. id="2CF9013B-E8A8-42A1-9633-85AD7CA12F40"
  1973. to="romeo@montague.lit"
  1974. xmlns="jabber:client">
  1975. <x xmlns="http://jabber.org/protocol/muc#user">
  1976. <item jid="absentguy@montague.lit" affiliation="member" role="none"/>
  1977. </x>
  1978. </message>`;
  1979. _converse.api.connection.get()._dataRecv(mock.createRequest(message));
  1980. await u.waitUntil(() => view.model.occupants.length > 1);
  1981. expect(view.model.occupants.length).toBe(2);
  1982. expect(view.model.occupants.findWhere({'jid': 'absentguy@montague.lit'}).get('affiliation')).toBe('member');
  1983. message =
  1984. stx`<message from="lounge@montague.lit"
  1985. id="2CF9013B-E8A8-42A1-9633-85AD7CA12F41"
  1986. to="romeo@montague.lit"
  1987. xmlns="jabber:client">
  1988. <x xmlns="http://jabber.org/protocol/muc#user">
  1989. <item jid="absentguy@montague.lit" affiliation="none" role="none"/>
  1990. </x>
  1991. </message>`;
  1992. _converse.api.connection.get()._dataRecv(mock.createRequest(message));
  1993. expect(view.model.occupants.length).toBe(2);
  1994. expect(view.model.occupants.findWhere({'jid': 'absentguy@montague.lit'}).get('affiliation')).toBe('none');
  1995. }));
  1996. });
  1997. describe("When attempting to enter a groupchat", function () {
  1998. it("will show an error message if the groupchat requires a password",
  1999. mock.initConverse(['chatBoxesFetched'], {}, async function (_converse) {
  2000. const { api } = _converse;
  2001. const muc_jid = 'protected@montague.lit';
  2002. api.rooms.open(muc_jid, { nick: 'romeo' });
  2003. await mock.waitForMUCDiscoInfo(_converse, muc_jid);
  2004. const presence =
  2005. stx`<presence from="${muc_jid}/romeo"
  2006. id="${u.getUniqueId()}"
  2007. to="romeo@montague.lit/pda"
  2008. type="error"
  2009. xmlns="jabber:client">
  2010. <x xmlns="http://jabber.org/protocol/muc"/>
  2011. <error by="lounge@montague.lit" type="auth">
  2012. <not-authorized xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"/>
  2013. </error>
  2014. </presence>`;
  2015. _converse.api.connection.get()._dataRecv(mock.createRequest(presence));
  2016. const view = await u.waitUntil(() => _converse.chatboxviews.get(muc_jid));
  2017. const chat_body = view.querySelector('.chatroom-body');
  2018. await u.waitUntil(() => chat_body.querySelectorAll('form.chatroom-form').length === 1);
  2019. expect(chat_body.querySelector('.chatroom-form label').textContent.trim())
  2020. .toBe('This groupchat requires a password');
  2021. // Let's submit the form
  2022. spyOn(view.model, 'join');
  2023. const input_el = view.querySelector('[name="password"]');
  2024. input_el.value = 'secret';
  2025. view.querySelector('input[type=submit]').click();
  2026. expect(view.model.join).toHaveBeenCalledWith('romeo', 'secret');
  2027. }));
  2028. it("will show an error message if the groupchat is members-only and the user not included",
  2029. mock.initConverse([], {}, async function (_converse) {
  2030. const { api } = _converse;
  2031. const muc_jid = 'members-only@muc.montague.lit'
  2032. api.rooms.open(muc_jid, { nick: 'romeo' });
  2033. const iq = await u.waitUntil(() => _converse.api.connection.get().IQ_stanzas.filter(
  2034. iq => iq.querySelector(
  2035. `iq[to="${muc_jid}"] query[xmlns="http://jabber.org/protocol/disco#info"]`
  2036. )).pop());
  2037. expect(iq).toEqualStanza(stx`
  2038. <iq from="romeo@montague.lit/orchard" to="${muc_jid}" type="get" xmlns="jabber:client" id="${iq.getAttribute('id')}">
  2039. <query xmlns="http://jabber.org/protocol/disco#info"/>
  2040. </iq>`);
  2041. // State that the chat is members-only via the features IQ
  2042. const features_stanza =
  2043. stx`<iq from="${muc_jid}"
  2044. id="${iq.getAttribute('id')}"
  2045. to="romeo@montague.lit/desktop"
  2046. type="result"
  2047. xmlns="jabber:client">
  2048. <query xmlns="http://jabber.org/protocol/disco#info">
  2049. <identity category="conference" name="A Dark Cave" type="text"/>
  2050. <feature var="http://jabber.org/protocol/muc"/>
  2051. <feature var="muc_hidden"/>
  2052. <feature var="muc_temporary"/>
  2053. <feature var="muc_membersonly"/>
  2054. </query>
  2055. </iq>`;
  2056. _converse.api.connection.get()._dataRecv(mock.createRequest(features_stanza));
  2057. const view = await u.waitUntil(() => _converse.chatboxviews.get(muc_jid));
  2058. await u.waitUntil(() => view.model.session.get('connection_status') === converse.ROOMSTATUS.CONNECTING);
  2059. const presence =
  2060. stx`<presence from="${muc_jid}/romeo"
  2061. id="${u.getUniqueId()}"
  2062. to="romeo@montague.lit/pda"
  2063. type="error"
  2064. xmlns="jabber:client">
  2065. <x xmlns="http://jabber.org/protocol/muc"/>
  2066. <error by="lounge@montague.lit" type="auth">
  2067. <registration-required xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"/>
  2068. </error>
  2069. </presence>`;
  2070. _converse.api.connection.get()._dataRecv(mock.createRequest(presence));
  2071. await u.waitUntil(() => view.querySelector('.chatroom-body converse-muc-disconnected .disconnect-msg:last-child')?.textContent?.trim() ===
  2072. 'You are not on the member list of this groupchat.');
  2073. }));
  2074. it("will show an error message if the user has been banned",
  2075. mock.initConverse([], {}, async function (_converse) {
  2076. const { api } = _converse;
  2077. const muc_jid = 'off-limits@muc.montague.lit'
  2078. api.rooms.open(muc_jid, { nick: 'romeo' });
  2079. const iq = await u.waitUntil(() => _converse.api.connection.get().IQ_stanzas.filter(
  2080. iq => iq.querySelector(
  2081. `iq[to="${muc_jid}"] query[xmlns="http://jabber.org/protocol/disco#info"]`
  2082. )).pop());
  2083. const features_stanza =
  2084. stx`<iq from="${muc_jid}"
  2085. id="${iq.getAttribute('id')}"
  2086. to="romeo@montague.lit/desktop"
  2087. type="result"
  2088. xmlns="jabber:client">
  2089. <query xmlns="http://jabber.org/protocol/disco#info">
  2090. <identity category="conference" name="A Dark Cave" type="text"/>
  2091. <feature var="http://jabber.org/protocol/muc"/>
  2092. <feature var="muc_hidden"/>
  2093. <feature var="muc_temporary"/>
  2094. </query>
  2095. </iq>`
  2096. _converse.api.connection.get()._dataRecv(mock.createRequest(features_stanza));
  2097. const view = await u.waitUntil(() => _converse.chatboxviews.get(muc_jid));
  2098. await u.waitUntil(() => view.model.session.get('connection_status') === converse.ROOMSTATUS.CONNECTING);
  2099. const presence =
  2100. stx`<presence
  2101. from="${muc_jid}/romeo"
  2102. id="${u.getUniqueId()}"
  2103. to="romeo@montague.lit/pda"
  2104. type="error"
  2105. xmlns="jabber:client">
  2106. <x xmlns="http://jabber.org/protocol/muc"/>
  2107. <error by="lounge@montague.lit" type="auth">
  2108. <forbidden xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"/>
  2109. </error>
  2110. </presence>`;
  2111. _converse.api.connection.get()._dataRecv(mock.createRequest(presence));
  2112. const el = await u.waitUntil(() => view.querySelector('.chatroom-body converse-muc-disconnected .disconnect-msg:last-child'));
  2113. expect(el.textContent.trim()).toBe('You have been banned from this groupchat');
  2114. expect(view.model.session.get('connection_status')).toBe(converse.ROOMSTATUS.BANNED);
  2115. }));
  2116. it("will show an error message if the user is not allowed to have created the groupchat",
  2117. mock.initConverse([], {}, async function (_converse) {
  2118. const { api } = _converse;
  2119. const muc_jid = 'impermissable@muc.montague.lit'
  2120. api.rooms.open(muc_jid, { nick: 'romeo' });
  2121. await mock.waitForNewMUCDiscoInfo(_converse, muc_jid);
  2122. const presence =
  2123. stx`<presence xmlns="jabber:client"
  2124. from="${muc_jid}/romeo"
  2125. id="${u.getUniqueId()}"
  2126. to="romeo@montague.lit/pda"
  2127. type="error">
  2128. <x xmlns="http://jabber.org/protocol/muc"/>
  2129. <error by="lounge@montague.lit" type="cancel">
  2130. <not-allowed xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"/>
  2131. </error>
  2132. </presence>`;
  2133. _converse.api.connection.get()._dataRecv(mock.createRequest(presence));
  2134. const sent_IQs = _converse.api.connection.get().IQ_stanzas;
  2135. while (sent_IQs.length) sent_IQs.pop();
  2136. await mock.waitForMUCDiscoInfo(_converse, muc_jid);
  2137. const view = await u.waitUntil(() => _converse.chatboxviews.get(muc_jid));
  2138. const el = await u.waitUntil(() => view.querySelector('.chatroom-body converse-muc-disconnected .disconnect-msg:last-child'));
  2139. expect(el.textContent.trim()).toBe('You are not allowed to create new groupchats.');
  2140. }));
  2141. it("will show an error message if the groupchat doesn't yet exist",
  2142. mock.initConverse([], {}, async function (_converse) {
  2143. const { api } = _converse;
  2144. const muc_jid = 'nonexistent@muc.montague.lit'
  2145. api.rooms.open(muc_jid, { nick: 'romeo' });
  2146. const iq = await u.waitUntil(() => _converse.api.connection.get().IQ_stanzas.filter(
  2147. iq => iq.querySelector(
  2148. `iq[to="${muc_jid}"] query[xmlns="http://jabber.org/protocol/disco#info"]`
  2149. )).pop());
  2150. const features_stanza =
  2151. stx`<iq xmlns="jabber:client"
  2152. from="${muc_jid}"
  2153. id="${iq.getAttribute('id')}"
  2154. to="romeo@montague.lit/desktop"
  2155. type="result">
  2156. <query xmlns="http://jabber.org/protocol/disco#info">
  2157. <identity category="conference" name="A Dark Cave" type="text"/>
  2158. <feature var="http://jabber.org/protocol/muc"/>
  2159. </query>
  2160. </iq>`;
  2161. _converse.api.connection.get()._dataRecv(mock.createRequest(features_stanza));
  2162. const view = await u.waitUntil(() => _converse.chatboxviews.get(muc_jid));
  2163. await u.waitUntil(() => (view.model.session.get('connection_status') === converse.ROOMSTATUS.CONNECTING));
  2164. const presence =
  2165. stx`<presence xmlns="jabber:client"
  2166. from="${muc_jid}/romeo"
  2167. id="${u.getUniqueId()}"
  2168. to="romeo@montague.lit/pda"
  2169. type="error">
  2170. <x xmlns="http://jabber.org/protocol/muc"/>
  2171. <error by="lounge@montague.lit" type="cancel">
  2172. <item-not-found xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"/>
  2173. </error>
  2174. </presence>`;
  2175. _converse.api.connection.get()._dataRecv(mock.createRequest(presence));
  2176. const el = await u.waitUntil(() => view.querySelector('.chatroom-body converse-muc-disconnected .disconnect-msg:last-child'));
  2177. expect(el.textContent.trim()).toBe("This groupchat does not (yet) exist.");
  2178. }));
  2179. it("will show an error message if the groupchat has reached its maximum number of participants",
  2180. mock.initConverse([], {}, async function (_converse) {
  2181. const { api } = _converse;
  2182. const muc_jid = 'maxed-out@muc.montague.lit'
  2183. api.rooms.open(muc_jid, { nick: 'romeo' });
  2184. const iq = await u.waitUntil(() => _converse.api.connection.get().IQ_stanzas.filter(
  2185. iq => iq.querySelector(
  2186. `iq[to="${muc_jid}"] query[xmlns="http://jabber.org/protocol/disco#info"]`
  2187. )).pop());
  2188. const features_stanza =
  2189. stx`<iq from="${muc_jid}"
  2190. id="${iq.getAttribute('id')}"
  2191. to="romeo@montague.lit/desktop"
  2192. type="result"
  2193. xmlns="jabber:client">
  2194. <query xmlns="http://jabber.org/protocol/disco#info">
  2195. <identity category="conference" name="A Dark Cave" type="text"/>
  2196. <feature var="http://jabber.org/protocol/muc"/>
  2197. </query>
  2198. </iq>`;
  2199. _converse.api.connection.get()._dataRecv(mock.createRequest(features_stanza));
  2200. const view = await u.waitUntil(() => _converse.chatboxviews.get(muc_jid));
  2201. await u.waitUntil(() => (view.model.session.get('connection_status') === converse.ROOMSTATUS.CONNECTING));
  2202. const presence =
  2203. stx`<presence xmlns="jabber:client"
  2204. from="${muc_jid}/romeo"
  2205. id="${u.getUniqueId()}"
  2206. to="romeo@montague.lit/pda"
  2207. type="error">
  2208. <x xmlns="http://jabber.org/protocol/muc"/>
  2209. <error by="lounge@montague.lit" type="cancel">
  2210. <service-unavailable xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"/>
  2211. </error>
  2212. </presence>`;
  2213. _converse.api.connection.get()._dataRecv(mock.createRequest(presence));
  2214. const el = await u.waitUntil(() => view.querySelector('.chatroom-body converse-muc-disconnected .disconnect-msg:last-child'));
  2215. expect(el.textContent.trim()).toBe("This groupchat has reached its maximum number of participants.");
  2216. }));
  2217. });
  2218. describe("The affiliations delta", function () {
  2219. it("can be computed in various ways", mock.initConverse([], {}, async function (_converse) {
  2220. const { api } = _converse;
  2221. const muc_jid = 'coven@chat.shakespeare.lit';
  2222. api.rooms.open(muc_jid, { nick: 'romeo' });
  2223. let exclude_existing = false;
  2224. let remove_absentees = false;
  2225. let new_list = [];
  2226. let old_list = [];
  2227. const muc_utils = converse.env.muc_utils;
  2228. let delta = muc_utils.computeAffiliationsDelta(exclude_existing, remove_absentees, new_list, old_list);
  2229. expect(delta.length).toBe(0);
  2230. new_list = [{'jid': 'wiccarocks@shakespeare.lit', 'affiliation': 'member'}];
  2231. old_list = [{'jid': 'wiccarocks@shakespeare.lit', 'affiliation': 'member'}];
  2232. delta = muc_utils.computeAffiliationsDelta(exclude_existing, remove_absentees, new_list, old_list);
  2233. expect(delta.length).toBe(0);
  2234. // When remove_absentees is false, then affiliations in the old
  2235. // list which are not in the new one won't be removed.
  2236. old_list = [{'jid': 'oldhag666@shakespeare.lit', 'affiliation': 'owner'},
  2237. {'jid': 'wiccarocks@shakespeare.lit', 'affiliation': 'member'}];
  2238. delta = muc_utils.computeAffiliationsDelta(exclude_existing, remove_absentees, new_list, old_list);
  2239. expect(delta.length).toBe(0);
  2240. // With exclude_existing set to false, any changed affiliations
  2241. // will be included in the delta (i.e. existing affiliations are included in the comparison).
  2242. old_list = [{'jid': 'wiccarocks@shakespeare.lit', 'affiliation': 'owner'}];
  2243. delta = muc_utils.computeAffiliationsDelta(exclude_existing, remove_absentees, new_list, old_list);
  2244. expect(delta.length).toBe(1);
  2245. expect(delta[0].jid).toBe('wiccarocks@shakespeare.lit');
  2246. expect(delta[0].affiliation).toBe('member');
  2247. // To also remove affiliations from the old list which are not
  2248. // in the new list, we set remove_absentees to true
  2249. remove_absentees = true;
  2250. old_list = [{'jid': 'oldhag666@shakespeare.lit', 'affiliation': 'owner'},
  2251. {'jid': 'wiccarocks@shakespeare.lit', 'affiliation': 'member'}];
  2252. delta = muc_utils.computeAffiliationsDelta(exclude_existing, remove_absentees, new_list, old_list);
  2253. expect(delta.length).toBe(1);
  2254. expect(delta[0].jid).toBe('oldhag666@shakespeare.lit');
  2255. expect(delta[0].affiliation).toBe('none');
  2256. delta = muc_utils.computeAffiliationsDelta(exclude_existing, remove_absentees, [], old_list);
  2257. expect(delta.length).toBe(2);
  2258. expect(delta[0].jid).toBe('oldhag666@shakespeare.lit');
  2259. expect(delta[0].affiliation).toBe('none');
  2260. expect(delta[1].jid).toBe('wiccarocks@shakespeare.lit');
  2261. expect(delta[1].affiliation).toBe('none');
  2262. // To only add a user if they don't already have an
  2263. // affiliation, we set 'exclude_existing' to true
  2264. exclude_existing = true;
  2265. old_list = [{'jid': 'wiccarocks@shakespeare.lit', 'affiliation': 'owner'}];
  2266. delta = muc_utils.computeAffiliationsDelta(exclude_existing, remove_absentees, new_list, old_list);
  2267. expect(delta.length).toBe(0);
  2268. old_list = [{'jid': 'wiccarocks@shakespeare.lit', 'affiliation': 'admin'}];
  2269. delta = muc_utils.computeAffiliationsDelta(exclude_existing, remove_absentees, new_list, old_list);
  2270. expect(delta.length).toBe(0);
  2271. }));
  2272. });
  2273. });