muc.js 239 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336
  1. (function (root, factory) {
  2. define(["jasmine", "mock", "test-utils" ], factory);
  3. } (this, function (jasmine, mock, test_utils) {
  4. const _ = converse.env._,
  5. $pres = converse.env.$pres,
  6. $iq = converse.env.$iq,
  7. $msg = converse.env.$msg,
  8. Strophe = converse.env.Strophe,
  9. Promise = converse.env.Promise,
  10. moment = converse.env.moment,
  11. sizzle = converse.env.sizzle,
  12. Backbone = converse.env.Backbone,
  13. u = converse.env.utils;
  14. describe("Chatrooms", function () {
  15. describe("The \"rooms\" API", function () {
  16. it("has a method 'close' which closes rooms by JID or all rooms when called with no arguments",
  17. mock.initConverseWithPromises(
  18. null, ['rosterGroupsFetched'], {},
  19. async function (done, _converse) {
  20. await test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy');
  21. await test_utils.openAndEnterChatRoom(_converse, 'leisure', 'localhost', 'dummy');
  22. await test_utils.openAndEnterChatRoom(_converse, 'news', 'localhost', 'dummy');
  23. expect(u.isVisible(_converse.chatboxviews.get('lounge@localhost').el)).toBeTruthy();
  24. expect(u.isVisible(_converse.chatboxviews.get('leisure@localhost').el)).toBeTruthy();
  25. expect(u.isVisible(_converse.chatboxviews.get('news@localhost').el)).toBeTruthy();
  26. // XXX: bit of a cheat here. We want `cleanup()` to be
  27. // called on the room. Either it's this or faking
  28. // `sendPresence`.
  29. _converse.connection.connected = false;
  30. _converse.api.roomviews.close('lounge@localhost');
  31. expect(_converse.chatboxviews.get('lounge@localhost')).toBeUndefined();
  32. expect(u.isVisible(_converse.chatboxviews.get('leisure@localhost').el)).toBeTruthy();
  33. expect(u.isVisible(_converse.chatboxviews.get('news@localhost').el)).toBeTruthy();
  34. _converse.api.roomviews.close(['leisure@localhost', 'news@localhost']);
  35. expect(_converse.chatboxviews.get('lounge@localhost')).toBeUndefined();
  36. expect(_converse.chatboxviews.get('leisure@localhost')).toBeUndefined();
  37. expect(_converse.chatboxviews.get('news@localhost')).toBeUndefined();
  38. await test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy');
  39. await test_utils.openAndEnterChatRoom(_converse, 'leisure', 'localhost', 'dummy');
  40. expect(u.isVisible(_converse.chatboxviews.get('lounge@localhost').el)).toBeTruthy();
  41. expect(u.isVisible(_converse.chatboxviews.get('leisure@localhost').el)).toBeTruthy();
  42. _converse.api.roomviews.close();
  43. expect(_converse.chatboxviews.get('lounge@localhost')).toBeUndefined();
  44. expect(_converse.chatboxviews.get('leisure@localhost')).toBeUndefined();
  45. done();
  46. }));
  47. it("has a method 'get' which returns a wrapped groupchat (if it exists)",
  48. mock.initConverseWithPromises(
  49. null, ['rosterGroupsFetched'], {},
  50. async function (done, _converse) {
  51. test_utils.createContacts(_converse, 'current');
  52. await test_utils.waitUntil(() => _converse.rosterview.el.querySelectorAll('.roster-group .group-toggle').length, 300);
  53. await test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy');
  54. let jid = 'lounge@localhost';
  55. let room = _converse.api.rooms.get(jid);
  56. expect(room instanceof Object).toBeTruthy();
  57. let chatroomview = _converse.chatboxviews.get(jid);
  58. expect(chatroomview.is_chatroom).toBeTruthy();
  59. expect(u.isVisible(chatroomview.el)).toBeTruthy();
  60. chatroomview.close();
  61. // Test with mixed case
  62. await test_utils.openAndEnterChatRoom(_converse, 'Leisure', 'localhost', 'dummy');
  63. jid = 'Leisure@localhost';
  64. room = _converse.api.rooms.get(jid);
  65. expect(room instanceof Object).toBeTruthy();
  66. chatroomview = _converse.chatboxviews.get(jid.toLowerCase());
  67. expect(u.isVisible(chatroomview.el)).toBeTruthy();
  68. jid = 'leisure@localhost';
  69. room = _converse.api.rooms.get(jid);
  70. expect(room instanceof Object).toBeTruthy();
  71. chatroomview = _converse.chatboxviews.get(jid.toLowerCase());
  72. expect(u.isVisible(chatroomview.el)).toBeTruthy();
  73. jid = 'leiSure@localhost';
  74. room = _converse.api.rooms.get(jid);
  75. expect(room instanceof Object).toBeTruthy();
  76. chatroomview = _converse.chatboxviews.get(jid.toLowerCase());
  77. expect(u.isVisible(chatroomview.el)).toBeTruthy();
  78. chatroomview.close();
  79. // Non-existing room
  80. jid = 'lounge2@localhost';
  81. room = _converse.api.rooms.get(jid);
  82. expect(typeof room === 'undefined').toBeTruthy();
  83. done();
  84. }));
  85. it("has a method 'open' which opens (optionally configures) and returns a wrapped chat box",
  86. mock.initConverseWithPromises(
  87. null, ['rosterGroupsFetched', 'chatBoxesFetched'], {},
  88. async function (done, _converse) {
  89. // Mock 'getRoomFeatures', otherwise the room won't be
  90. // displayed as it waits first for the features to be returned
  91. // (when it's a new room being created).
  92. spyOn(_converse.ChatRoom.prototype, 'getRoomFeatures').and.callFake(() => Promise.resolve());
  93. const sent_IQ_els = [];
  94. let jid = 'lounge@localhost';
  95. let chatroomview, sent_IQ, IQ_id;
  96. test_utils.openControlBox();
  97. test_utils.createContacts(_converse, 'current');
  98. await test_utils.waitUntil(() => _converse.rosterview.el.querySelectorAll('.roster-group .group-toggle').length);
  99. let room = await _converse.api.rooms.open(jid);
  100. // Test on groupchat that's not yet open
  101. expect(room instanceof Backbone.Model).toBeTruthy();
  102. chatroomview = _converse.chatboxviews.get(jid);
  103. expect(chatroomview.is_chatroom).toBeTruthy();
  104. expect(u.isVisible(chatroomview.el)).toBeTruthy();
  105. // Test again, now that the room exists.
  106. room = await _converse.api.rooms.open(jid);
  107. expect(room instanceof Backbone.Model).toBeTruthy();
  108. chatroomview = _converse.chatboxviews.get(jid);
  109. expect(chatroomview.is_chatroom).toBeTruthy();
  110. expect(u.isVisible(chatroomview.el)).toBeTruthy();
  111. chatroomview.close();
  112. // Test with mixed case in JID
  113. jid = 'Leisure@localhost';
  114. room = await _converse.api.rooms.open(jid);
  115. expect(room instanceof Backbone.Model).toBeTruthy();
  116. chatroomview = _converse.chatboxviews.get(jid.toLowerCase());
  117. expect(u.isVisible(chatroomview.el)).toBeTruthy();
  118. jid = 'leisure@localhost';
  119. room = await _converse.api.rooms.open(jid);
  120. expect(room instanceof Backbone.Model).toBeTruthy();
  121. chatroomview = _converse.chatboxviews.get(jid.toLowerCase());
  122. expect(u.isVisible(chatroomview.el)).toBeTruthy();
  123. jid = 'leiSure@localhost';
  124. room = await _converse.api.rooms.open(jid);
  125. expect(room instanceof Backbone.Model).toBeTruthy();
  126. chatroomview = _converse.chatboxviews.get(jid.toLowerCase());
  127. expect(u.isVisible(chatroomview.el)).toBeTruthy();
  128. chatroomview.close();
  129. _converse.muc_instant_rooms = false;
  130. var sendIQ = _converse.connection.sendIQ;
  131. spyOn(_converse.connection, 'sendIQ').and.callFake(function (iq, callback, errback) {
  132. sent_IQ = iq;
  133. sent_IQ_els.push(iq.nodeTree);
  134. IQ_id = sendIQ.bind(this)(iq, callback, errback);
  135. });
  136. // Test with configuration
  137. room = await _converse.api.rooms.open('room@conference.example.org', {
  138. 'nick': 'some1',
  139. 'auto_configure': true,
  140. 'roomconfig': {
  141. 'changesubject': false,
  142. 'membersonly': true,
  143. 'persistentroom': true,
  144. 'publicroom': true,
  145. 'roomdesc': 'Welcome to this groupchat',
  146. 'whois': 'anyone'
  147. }
  148. });
  149. chatroomview = _converse.chatboxviews.get('room@conference.example.org');
  150. // We pretend this is a new room, so no disco info is returned.
  151. var features_stanza = $iq({
  152. from: 'room@conference.example.org',
  153. 'id': IQ_id,
  154. 'to': 'dummy@localhost/desktop',
  155. 'type': 'error'
  156. }).c('error', {'type': 'cancel'})
  157. .c('item-not-found', {'xmlns': "urn:ietf:params:xml:ns:xmpp-stanzas"});
  158. _converse.connection._dataRecv(test_utils.createRequest(features_stanza));
  159. /* <presence xmlns="jabber:client" to="dummy@localhost/pda" from="room@conference.example.org/yo">
  160. * <x xmlns="http://jabber.org/protocol/muc#user">
  161. * <item affiliation="owner" jid="dummy@localhost/pda" role="moderator"/>
  162. * <status code="110"/>
  163. * <status code="201"/>
  164. * </x>
  165. * </presence>
  166. */
  167. var presence = $pres({
  168. from:'room@conference.example.org/some1',
  169. to:'dummy@localhost/pda'
  170. })
  171. .c('x', {xmlns:'http://jabber.org/protocol/muc#user'})
  172. .c('item', {
  173. affiliation: 'owner',
  174. jid: 'dummy@localhost/pda',
  175. role: 'moderator'
  176. }).up()
  177. .c('status', {code:'110'}).up()
  178. .c('status', {code:'201'});
  179. _converse.connection._dataRecv(test_utils.createRequest(presence));
  180. expect(_converse.connection.sendIQ).toHaveBeenCalled();
  181. expect(sent_IQ.toLocaleString()).toBe(
  182. `<iq id="${IQ_id}" to="room@conference.example.org" type="get" xmlns="jabber:client">`+
  183. `<query xmlns="http://jabber.org/protocol/muc#owner"/></iq>`
  184. );
  185. var node = Strophe.xmlHtmlNode(
  186. '<iq xmlns="jabber:client"'+
  187. ' type="result"'+
  188. ' to="dummy@localhost/pda"'+
  189. ' from="room@conference.example.org" id="'+IQ_id+'">'+
  190. ' <query xmlns="http://jabber.org/protocol/muc#owner">'+
  191. ' <x xmlns="jabber:x:data" type="form">'+
  192. ' <title>Configuration for room@conference.example.org</title>'+
  193. ' <instructions>Complete and submit this form to configure the room.</instructions>'+
  194. ' <field var="FORM_TYPE" type="hidden">'+
  195. ' <value>http://jabber.org/protocol/muc#roomconfig</value>'+
  196. ' </field>'+
  197. ' <field type="text-single" var="muc#roomconfig_roomname" label="Name">'+
  198. ' <value>Room</value>'+
  199. ' </field>'+
  200. ' <field type="text-single" var="muc#roomconfig_roomdesc" label="Description"><value/></field>'+
  201. ' <field type="boolean" var="muc#roomconfig_persistentroom" label="Make Room Persistent?"/>'+
  202. ' <field type="boolean" var="muc#roomconfig_publicroom" label="Make Room Publicly Searchable?"><value>1</value></field>'+
  203. ' <field type="boolean" var="muc#roomconfig_changesubject" label="Allow Occupants to Change Subject?"/>'+
  204. ' <field type="list-single" var="muc#roomconfig_whois" label="Who May Discover Real JIDs?"><option label="Moderators Only">'+
  205. ' <value>moderators</value></option><option label="Anyone"><value>anyone</value></option>'+
  206. ' </field>'+
  207. ' <field type="text-private" var="muc#roomconfig_roomsecret" label="Password"><value/></field>'+
  208. ' <field type="boolean" var="muc#roomconfig_moderatedroom" label="Make Room Moderated?"/>'+
  209. ' <field type="boolean" var="muc#roomconfig_membersonly" label="Make Room Members-Only?"/>'+
  210. ' <field type="text-single" var="muc#roomconfig_historylength" label="Maximum Number of History Messages Returned by Room">'+
  211. ' <value>20</value></field>'+
  212. ' </x>'+
  213. ' </query>'+
  214. ' </iq>');
  215. spyOn(chatroomview.model, 'sendConfiguration').and.callThrough();
  216. _converse.connection._dataRecv(test_utils.createRequest(node.firstElementChild));
  217. await test_utils.waitUntil(() => chatroomview.model.sendConfiguration.calls.count() === 1);
  218. var sent_stanza = sent_IQ_els.pop();
  219. while (sent_stanza.getAttribute('type') !== 'set') {
  220. sent_stanza = sent_IQ_els.pop();
  221. }
  222. expect(sizzle('field[var="muc#roomconfig_roomname"] value', sent_stanza).pop().textContent).toBe('Room');
  223. expect(sizzle('field[var="muc#roomconfig_roomdesc"] value', sent_stanza).pop().textContent).toBe('Welcome to this groupchat');
  224. expect(sizzle('field[var="muc#roomconfig_persistentroom"] value', sent_stanza).pop().textContent).toBe('1');
  225. expect(sizzle('field[var="muc#roomconfig_publicroom"] value ', sent_stanza).pop().textContent).toBe('1');
  226. expect(sizzle('field[var="muc#roomconfig_changesubject"] value', sent_stanza).pop().textContent).toBe('0');
  227. expect(sizzle('field[var="muc#roomconfig_whois"] value ', sent_stanza).pop().textContent).toBe('anyone');
  228. expect(sizzle('field[var="muc#roomconfig_membersonly"] value', sent_stanza).pop().textContent).toBe('1');
  229. expect(sizzle('field[var="muc#roomconfig_historylength"] value', sent_stanza).pop().textContent).toBe('20');
  230. done();
  231. }));
  232. });
  233. describe("An instant groupchat", function () {
  234. it("will be created when muc_instant_rooms is set to true",
  235. mock.initConverseWithPromises(
  236. null, ['rosterGroupsFetched', 'chatBoxesFetched'], {},
  237. async function (done, _converse) {
  238. const IQ_stanzas = _converse.connection.IQ_stanzas;
  239. const sendIQ = _converse.connection.sendIQ;
  240. const room_jid = 'lounge@localhost';
  241. let sent_IQ, IQ_id;
  242. spyOn(_converse.connection, 'sendIQ').and.callFake(function (iq, callback, errback) {
  243. if (iq.nodeTree.getAttribute('to') === 'lounge@localhost') {
  244. sent_IQ = iq;
  245. IQ_id = sendIQ.bind(this)(iq, callback, errback);
  246. } else {
  247. sendIQ.bind(this)(iq, callback, errback);
  248. }
  249. });
  250. await test_utils.openChatRoom(_converse, 'lounge', 'localhost', 'dummy');
  251. let stanza = await test_utils.waitUntil(() => _.get(_.filter(
  252. IQ_stanzas,
  253. iq => iq.nodeTree.querySelector(
  254. `iq[to="${room_jid}"] query[xmlns="http://jabber.org/protocol/disco#info"]`
  255. )).pop(), 'nodeTree'));
  256. // We pretend this is a new room, so no disco info is returned.
  257. /* <iq from="jordie.langen@chat.example.org/converse.js-11659299" to="myroom@conference.chat.example.org" type="get">
  258. * <query xmlns="http://jabber.org/protocol/disco#info"/>
  259. * </iq>
  260. * <iq xmlns="jabber:client" type="error" to="jordie.langen@chat.example.org/converse.js-11659299" from="myroom@conference.chat.example.org">
  261. * <error type="cancel">
  262. * <item-not-found xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"/>
  263. * </error>
  264. * </iq>
  265. */
  266. const features_stanza = $iq({
  267. 'from': 'lounge@localhost',
  268. 'id': stanza.getAttribute('id'),
  269. 'to': 'dummy@localhost/desktop',
  270. 'type': 'error'
  271. }).c('error', {'type': 'cancel'})
  272. .c('item-not-found', {'xmlns': "urn:ietf:params:xml:ns:xmpp-stanzas"});
  273. _converse.connection._dataRecv(test_utils.createRequest(features_stanza));
  274. const view = _converse.chatboxviews.get('lounge@localhost');
  275. spyOn(view, 'join').and.callThrough();
  276. spyOn(view, 'submitNickname').and.callThrough();
  277. /* <iq to="myroom@conference.chat.example.org"
  278. * from="jordie.langen@chat.example.org/converse.js-11659299"
  279. * type="get">
  280. * <query xmlns="http://jabber.org/protocol/disco#info"
  281. * node="x-roomuser-item"/>
  282. * </iq>
  283. */
  284. const node = await test_utils.waitUntil(() => _.filter(
  285. IQ_stanzas,
  286. s => sizzle(`iq[to="${room_jid}"] query[node="x-roomuser-item"]`, s.nodeTree).length
  287. ).pop()
  288. );
  289. stanza = node.nodeTree;
  290. expect(node.toLocaleString()).toBe(
  291. `<iq from="dummy@localhost/resource" id="${stanza.getAttribute("id")}" to="lounge@localhost" `+
  292. `type="get" xmlns="jabber:client">`+
  293. `<query node="x-roomuser-item" xmlns="http://jabber.org/protocol/disco#info"/></iq>`);
  294. /* <iq xmlns="jabber:client" type="error" to="jordie.langen@chat.example.org/converse.js-11659299" from="myroom@conference.chat.example.org">
  295. * <error type="cancel">
  296. * <item-not-found xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"/>
  297. * </error>
  298. * </iq>
  299. */
  300. var result_stanza = $iq({
  301. 'type': 'error',
  302. 'id': stanza.getAttribute('id'),
  303. 'from': view.model.get('jid'),
  304. 'to': _converse.connection.jid
  305. }).c('error', {'type': 'cancel'})
  306. .c('item-not-found', {'xmlns': "urn:ietf:params:xml:ns:xmpp-stanzas"});
  307. _converse.connection._dataRecv(test_utils.createRequest(result_stanza));
  308. const input = await test_utils.waitUntil(() => view.el.querySelector('input[name="nick"]'));
  309. input.value = 'nicky';
  310. view.el.querySelector('input[type=submit]').click();
  311. expect(view.submitNickname).toHaveBeenCalled();
  312. expect(view.join).toHaveBeenCalled();
  313. // The user has just entered the room (because join was called)
  314. // and receives their own presence from the server.
  315. // See example 24:
  316. // http://xmpp.org/extensions/xep-0045.html#enter-pres
  317. //
  318. /* <presence xmlns="jabber:client" to="jordie.langen@chat.example.org/converse.js-11659299" from="myroom@conference.chat.example.org/jc">
  319. * <x xmlns="http://jabber.org/protocol/muc#user">
  320. * <item jid="jordie.langen@chat.example.org/converse.js-11659299" affiliation="owner" role="moderator"/>
  321. * <status code="110"/>
  322. * <status code="201"/>
  323. * </x>
  324. * </presence>
  325. */
  326. var presence = $pres({
  327. to:'dummy@localhost/resource',
  328. from:'lounge@localhost/thirdwitch',
  329. id:'5025e055-036c-4bc5-a227-706e7e352053'
  330. }).c('x').attrs({xmlns:'http://jabber.org/protocol/muc#user'})
  331. .c('item').attrs({
  332. affiliation: 'owner',
  333. jid: 'dummy@localhost/resource',
  334. role: 'moderator'
  335. }).up()
  336. .c('status').attrs({code:'110'}).up()
  337. .c('status').attrs({code:'201'}).nodeTree;
  338. _converse.connection._dataRecv(test_utils.createRequest(presence));
  339. var info_text = view.el.querySelector('.chat-content .chat-info').textContent;
  340. expect(info_text).toBe('A new groupchat has been created');
  341. // An instant room is created by saving the default configuratoin.
  342. //
  343. /* <iq to="myroom@conference.chat.example.org" type="set" xmlns="jabber:client" id="5025e055-036c-4bc5-a227-706e7e352053:sendIQ">
  344. * <query xmlns="http://jabber.org/protocol/muc#owner"><x xmlns="jabber:x:data" type="submit"/></query>
  345. * </iq>
  346. */
  347. expect(sent_IQ.toLocaleString()).toBe(
  348. `<iq id="${IQ_id}" to="lounge@localhost" type="set" xmlns="jabber:client">`+
  349. `<query xmlns="http://jabber.org/protocol/muc#owner"><x type="submit" xmlns="jabber:x:data"/>`+
  350. `</query></iq>`);
  351. done();
  352. }));
  353. });
  354. describe("A Groupchat", function () {
  355. it("is opened when an xmpp: URI is clicked inside another groupchat",
  356. mock.initConverseWithPromises(
  357. null, ['rosterGroupsFetched'], {},
  358. async function (done, _converse) {
  359. test_utils.createContacts(_converse, 'current');
  360. await test_utils.waitUntil(() => test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy'));
  361. const view = _converse.chatboxviews.get('lounge@localhost');
  362. if (!view.el.querySelectorAll('.chat-area').length) {
  363. view.renderChatArea();
  364. }
  365. expect(_converse.chatboxes.length).toEqual(2);
  366. const message = 'Please go to xmpp:coven@chat.shakespeare.lit?join',
  367. nick = mock.chatroom_names[0],
  368. msg = $msg({
  369. 'from': 'lounge@localhost/'+nick,
  370. 'id': (new Date()).getTime(),
  371. 'to': 'dummy@localhost',
  372. 'type': 'groupchat'
  373. }).c('body').t(message).tree();
  374. await view.model.onMessage(msg);
  375. await new Promise((resolve, reject) => view.once('messageInserted', resolve));
  376. view.el.querySelector('.chat-msg__text a').click();
  377. await test_utils.waitUntil(() => _converse.chatboxes.length === 3)
  378. expect(_.includes(_converse.chatboxes.pluck('id'), 'coven@chat.shakespeare.lit')).toBe(true);
  379. done()
  380. }));
  381. it("shows a notification if its not anonymous",
  382. mock.initConverseWithPromises(
  383. null, ['rosterGroupsFetched', 'chatBoxesFetched'], {},
  384. async function (done, _converse) {
  385. await test_utils.openChatRoom(_converse, "coven", 'chat.shakespeare.lit', 'some1');
  386. const view = _converse.chatboxviews.get('coven@chat.shakespeare.lit');
  387. const chat_content = view.el.querySelector('.chat-content');
  388. /* <presence to="dummy@localhost/_converse.js-29092160"
  389. * from="coven@chat.shakespeare.lit/some1">
  390. * <x xmlns="http://jabber.org/protocol/muc#user">
  391. * <item affiliation="owner" jid="dummy@localhost/_converse.js-29092160" role="moderator"/>
  392. * <status code="110"/>
  393. * <status code="100"/>
  394. * </x>
  395. * </presence></body>
  396. */
  397. let presence = $pres({
  398. to: 'dummy@localhost/resource',
  399. from: 'coven@chat.shakespeare.lit/some1'
  400. }).c('x', {xmlns: Strophe.NS.MUC_USER})
  401. .c('item', {
  402. 'affiliation': 'owner',
  403. 'jid': 'dummy@localhost/_converse.js-29092160',
  404. 'role': 'moderator'
  405. }).up()
  406. .c('status', {code: '110'}).up()
  407. .c('status', {code: '100'});
  408. _converse.connection._dataRecv(test_utils.createRequest(presence));
  409. expect(chat_content.querySelectorAll('.chat-info').length).toBe(2);
  410. expect(sizzle('div.chat-info:first', chat_content).pop().textContent)
  411. .toBe("This groupchat is not anonymous");
  412. expect(sizzle('div.chat-info:last', chat_content).pop().textContent)
  413. .toBe("some1 has entered the groupchat");
  414. // Check that we don't show the notification twice
  415. presence = $pres({
  416. to: 'dummy@localhost/resource',
  417. from: 'coven@chat.shakespeare.lit/some1'
  418. }).c('x', {xmlns: Strophe.NS.MUC_USER})
  419. .c('item', {
  420. 'affiliation': 'owner',
  421. 'jid': 'dummy@localhost/_converse.js-29092160',
  422. 'role': 'moderator'
  423. }).up()
  424. .c('status', {code: '110'}).up()
  425. .c('status', {code: '100'});
  426. _converse.connection._dataRecv(test_utils.createRequest(presence));
  427. expect(chat_content.querySelectorAll('.chat-info').length).toBe(2);
  428. expect(sizzle('div.chat-info:first', chat_content).pop().textContent)
  429. .toBe("This groupchat is not anonymous");
  430. expect(sizzle('div.chat-info:last', chat_content).pop().textContent)
  431. .toBe("some1 has entered the groupchat");
  432. done();
  433. }));
  434. it("shows join/leave messages when users enter or exit a groupchat",
  435. mock.initConverseWithPromises(
  436. null, ['rosterGroupsFetched', 'chatBoxesFetched'], {},
  437. async function (done, _converse) {
  438. await test_utils.openChatRoom(_converse, "coven", 'chat.shakespeare.lit', 'some1');
  439. const view = _converse.chatboxviews.get('coven@chat.shakespeare.lit');
  440. const chat_content = view.el.querySelector('.chat-content');
  441. /* We don't show join/leave messages for existing occupants. We
  442. * know about them because we receive their presences before we
  443. * receive our own.
  444. */
  445. let presence = $pres({
  446. to: 'dummy@localhost/_converse.js-29092160',
  447. from: 'coven@chat.shakespeare.lit/oldguy'
  448. }).c('x', {xmlns: Strophe.NS.MUC_USER})
  449. .c('item', {
  450. 'affiliation': 'none',
  451. 'jid': 'oldguy@localhost/_converse.js-290929789',
  452. 'role': 'participant'
  453. });
  454. _converse.connection._dataRecv(test_utils.createRequest(presence));
  455. expect(chat_content.querySelectorAll('div.chat-info').length).toBe(0);
  456. /* <presence to="dummy@localhost/_converse.js-29092160"
  457. * from="coven@chat.shakespeare.lit/some1">
  458. * <x xmlns="http://jabber.org/protocol/muc#user">
  459. * <item affiliation="owner" jid="dummy@localhost/_converse.js-29092160" role="moderator"/>
  460. * <status code="110"/>
  461. * </x>
  462. * </presence></body>
  463. */
  464. presence = $pres({
  465. to: 'dummy@localhost/_converse.js-29092160',
  466. from: 'coven@chat.shakespeare.lit/some1'
  467. }).c('x', {xmlns: Strophe.NS.MUC_USER})
  468. .c('item', {
  469. 'affiliation': 'owner',
  470. 'jid': 'dummy@localhost/_converse.js-29092160',
  471. 'role': 'moderator'
  472. }).up()
  473. .c('status', {code: '110'});
  474. _converse.connection._dataRecv(test_utils.createRequest(presence));
  475. expect(sizzle('div.chat-info:first', chat_content).pop().textContent)
  476. .toBe("some1 has entered the groupchat");
  477. presence = $pres({
  478. to: 'dummy@localhost/_converse.js-29092160',
  479. from: 'coven@chat.shakespeare.lit/newguy'
  480. })
  481. .c('x', {xmlns: Strophe.NS.MUC_USER})
  482. .c('item', {
  483. 'affiliation': 'none',
  484. 'jid': 'newguy@localhost/_converse.js-290929789',
  485. 'role': 'participant'
  486. });
  487. _converse.connection._dataRecv(test_utils.createRequest(presence));
  488. expect(chat_content.querySelectorAll('div.chat-info').length).toBe(2);
  489. expect(sizzle('div.chat-info:last', chat_content).pop().textContent)
  490. .toBe("newguy has entered the groupchat");
  491. const msg = $msg({
  492. 'from': 'coven@chat.shakespeare.lit/some1',
  493. 'id': (new Date()).getTime(),
  494. 'to': 'dummy@localhost',
  495. 'type': 'groupchat'
  496. }).c('body').t('hello world').tree();
  497. _converse.connection._dataRecv(test_utils.createRequest(msg));
  498. await new Promise((resolve, reject) => view.once('messageInserted', resolve));
  499. // Add another entrant, otherwise the above message will be
  500. // collapsed if "newguy" leaves immediately again
  501. presence = $pres({
  502. to: 'dummy@localhost/_converse.js-29092160',
  503. from: 'coven@chat.shakespeare.lit/newgirl'
  504. })
  505. .c('x', {xmlns: Strophe.NS.MUC_USER})
  506. .c('item', {
  507. 'affiliation': 'none',
  508. 'jid': 'newgirl@localhost/_converse.js-213098781',
  509. 'role': 'participant'
  510. });
  511. _converse.connection._dataRecv(test_utils.createRequest(presence));
  512. expect(chat_content.querySelectorAll('div.chat-info').length).toBe(3);
  513. expect(sizzle('div.chat-info:last', chat_content).pop().textContent)
  514. .toBe("newgirl has entered the groupchat");
  515. // Don't show duplicate join messages
  516. presence = $pres({
  517. to: 'dummy@localhost/_converse.js-290918392',
  518. from: 'coven@chat.shakespeare.lit/newguy'
  519. }).c('x', {xmlns: Strophe.NS.MUC_USER})
  520. .c('item', {
  521. 'affiliation': 'none',
  522. 'jid': 'newguy@localhost/_converse.js-290929789',
  523. 'role': 'participant'
  524. });
  525. _converse.connection._dataRecv(test_utils.createRequest(presence));
  526. expect(chat_content.querySelectorAll('div.chat-info').length).toBe(3);
  527. /* <presence
  528. * from='coven@chat.shakespeare.lit/thirdwitch'
  529. * to='crone1@shakespeare.lit/desktop'
  530. * type='unavailable'>
  531. * <status>Disconnected: Replaced by new connection</status>
  532. * <x xmlns='http://jabber.org/protocol/muc#user'>
  533. * <item affiliation='member'
  534. * jid='hag66@shakespeare.lit/pda'
  535. * role='none'/>
  536. * </x>
  537. * </presence>
  538. */
  539. presence = $pres({
  540. to: 'dummy@localhost/_converse.js-29092160',
  541. type: 'unavailable',
  542. from: 'coven@chat.shakespeare.lit/newguy'
  543. })
  544. .c('status', 'Disconnected: Replaced by new connection').up()
  545. .c('x', {xmlns: Strophe.NS.MUC_USER})
  546. .c('item', {
  547. 'affiliation': 'none',
  548. 'jid': 'newguy@localhost/_converse.js-290929789',
  549. 'role': 'none'
  550. });
  551. _converse.connection._dataRecv(test_utils.createRequest(presence));
  552. expect(chat_content.querySelectorAll('div.chat-info').length).toBe(4);
  553. expect(sizzle('div.chat-info:last', chat_content).pop().textContent).toBe(
  554. 'newguy has left the groupchat. '+
  555. '"Disconnected: Replaced by new connection"');
  556. // When the user immediately joins again, we collapse the
  557. // multiple join/leave messages.
  558. presence = $pres({
  559. to: 'dummy@localhost/_converse.js-29092160',
  560. from: 'coven@chat.shakespeare.lit/newguy'
  561. }).c('x', {xmlns: Strophe.NS.MUC_USER})
  562. .c('item', {
  563. 'affiliation': 'none',
  564. 'jid': 'newguy@localhost/_converse.js-290929789',
  565. 'role': 'participant'
  566. });
  567. _converse.connection._dataRecv(test_utils.createRequest(presence));
  568. expect(chat_content.querySelectorAll('div.chat-info').length).toBe(4);
  569. let msg_el = sizzle('div.chat-info:last', chat_content).pop();
  570. expect(msg_el.textContent).toBe("newguy has left and re-entered the groupchat");
  571. expect(msg_el.getAttribute('data-leavejoin')).toBe('newguy');
  572. presence = $pres({
  573. to: 'dummy@localhost/_converse.js-29092160',
  574. type: 'unavailable',
  575. from: 'coven@chat.shakespeare.lit/newguy'
  576. })
  577. .c('x', {xmlns: Strophe.NS.MUC_USER})
  578. .c('item', {
  579. 'affiliation': 'none',
  580. 'jid': 'newguy@localhost/_converse.js-290929789',
  581. 'role': 'none'
  582. });
  583. _converse.connection._dataRecv(test_utils.createRequest(presence));
  584. expect(chat_content.querySelectorAll('div.chat-info').length).toBe(4);
  585. msg_el = sizzle('div.chat-info', chat_content).pop();
  586. expect(msg_el.textContent).toBe('newguy has left the groupchat');
  587. expect(msg_el.getAttribute('data-leave')).toBe('newguy');
  588. presence = $pres({
  589. to: 'dummy@localhost/_converse.js-29092160',
  590. from: 'coven@chat.shakespeare.lit/nomorenicks'
  591. })
  592. .c('x', {xmlns: Strophe.NS.MUC_USER})
  593. .c('item', {
  594. 'affiliation': 'none',
  595. 'jid': 'nomorenicks@localhost/_converse.js-290929789',
  596. 'role': 'participant'
  597. });
  598. _converse.connection._dataRecv(test_utils.createRequest(presence));
  599. expect(chat_content.querySelectorAll('div.chat-info').length).toBe(5);
  600. expect(sizzle('div.chat-info:last', chat_content).pop().textContent)
  601. .toBe("nomorenicks has entered the groupchat");
  602. presence = $pres({
  603. to: 'dummy@localhost/_converse.js-290918392',
  604. type: 'unavailable',
  605. from: 'coven@chat.shakespeare.lit/nomorenicks'
  606. }).c('x', {xmlns: Strophe.NS.MUC_USER})
  607. .c('item', {
  608. 'affiliation': 'none',
  609. 'jid': 'nomorenicks@localhost/_converse.js-290929789',
  610. 'role': 'none'
  611. });
  612. _converse.connection._dataRecv(test_utils.createRequest(presence));
  613. expect(chat_content.querySelectorAll('div.chat-info').length).toBe(5);
  614. expect(sizzle('div.chat-info:last', chat_content).pop().textContent)
  615. .toBe("nomorenicks has entered and left the groupchat");
  616. presence = $pres({
  617. to: 'dummy@localhost/_converse.js-29092160',
  618. from: 'coven@chat.shakespeare.lit/nomorenicks'
  619. })
  620. .c('x', {xmlns: Strophe.NS.MUC_USER})
  621. .c('item', {
  622. 'affiliation': 'none',
  623. 'jid': 'nomorenicks@localhost/_converse.js-290929789',
  624. 'role': 'participant'
  625. });
  626. _converse.connection._dataRecv(test_utils.createRequest(presence));
  627. expect(chat_content.querySelectorAll('div.chat-info').length).toBe(5);
  628. expect(sizzle('div.chat-info:last', chat_content).pop().textContent)
  629. .toBe("nomorenicks has entered the groupchat");
  630. // Test a member joining and leaving
  631. presence = $pres({
  632. to: 'dummy@localhost/_converse.js-290918392',
  633. from: 'coven@chat.shakespeare.lit/insider'
  634. }).c('x', {xmlns: Strophe.NS.MUC_USER})
  635. .c('item', {
  636. 'affiliation': 'member',
  637. 'jid': 'insider@localhost/_converse.js-290929789',
  638. 'role': 'participant'
  639. });
  640. _converse.connection._dataRecv(test_utils.createRequest(presence));
  641. expect(chat_content.querySelectorAll('div.chat-info').length).toBe(6);
  642. /* <presence
  643. * from='coven@chat.shakespeare.lit/thirdwitch'
  644. * to='crone1@shakespeare.lit/desktop'
  645. * type='unavailable'>
  646. * <status>Disconnected: Replaced by new connection</status>
  647. * <x xmlns='http://jabber.org/protocol/muc#user'>
  648. * <item affiliation='member'
  649. * jid='hag66@shakespeare.lit/pda'
  650. * role='none'/>
  651. * </x>
  652. * </presence>
  653. */
  654. presence = $pres({
  655. to: 'dummy@localhost/_converse.js-29092160',
  656. type: 'unavailable',
  657. from: 'coven@chat.shakespeare.lit/insider'
  658. })
  659. .c('status', 'Disconnected: Replaced by new connection').up()
  660. .c('x', {xmlns: Strophe.NS.MUC_USER})
  661. .c('item', {
  662. 'affiliation': 'member',
  663. 'jid': 'insider@localhost/_converse.js-290929789',
  664. 'role': 'none'
  665. });
  666. _converse.connection._dataRecv(test_utils.createRequest(presence));
  667. expect(chat_content.querySelectorAll('div.chat-info').length).toBe(6);
  668. expect(sizzle('div.chat-info:last', chat_content).pop().textContent).toBe(
  669. 'insider has entered and left the groupchat. '+
  670. '"Disconnected: Replaced by new connection"');
  671. expect(view.model.occupants.length).toBe(5);
  672. expect(view.model.occupants.findWhere({'jid': 'insider@localhost'}).get('show')).toBe('offline');
  673. // New girl leaves
  674. presence = $pres({
  675. 'to': 'dummy@localhost/_converse.js-29092160',
  676. 'type': 'unavailable',
  677. 'from': 'coven@chat.shakespeare.lit/newgirl'
  678. })
  679. .c('x', {xmlns: Strophe.NS.MUC_USER})
  680. .c('item', {
  681. 'affiliation': 'none',
  682. 'jid': 'newgirl@localhost/_converse.js-213098781',
  683. 'role': 'none'
  684. });
  685. _converse.connection._dataRecv(test_utils.createRequest(presence));
  686. expect(chat_content.querySelectorAll('div.chat-info').length).toBe(6);
  687. expect(sizzle('div.chat-info:last', chat_content).pop().textContent).toBe("newgirl has entered and left the groupchat");
  688. expect(view.model.occupants.length).toBe(4);
  689. done();
  690. }));
  691. it("combines subsequent join/leave messages when users enter or exit a groupchat",
  692. mock.initConverseWithPromises(
  693. null, ['rosterGroupsFetched', 'chatBoxesFetched'], {},
  694. async function (done, _converse) {
  695. await test_utils.openAndEnterChatRoom(_converse, 'coven', 'chat.shakespeare.lit', 'dummy')
  696. const view = _converse.chatboxviews.get('coven@chat.shakespeare.lit');
  697. const chat_content = view.el.querySelector('.chat-content');
  698. expect(sizzle('div.chat-info', chat_content).length).toBe(1);
  699. expect(sizzle('div.chat-info:last', chat_content).pop().textContent).toBe("dummy has entered the groupchat");
  700. let presence = Strophe.xmlHtmlNode(
  701. `<presence xmlns="jabber:client" to="dummy@localhost/resource" from="coven@chat.shakespeare.lit/fabio">
  702. <c xmlns="http://jabber.org/protocol/caps" node="http://conversations.im" ver="INI3xjRUioclBTP/aACfWi5m9UY=" hash="sha-1"/>
  703. <x xmlns="http://jabber.org/protocol/muc#user">
  704. <item affiliation="none" jid="fabio@montefuscolo.com.br/Conversations.ZvLu" role="participant"/>
  705. </x>
  706. </presence>`).firstElementChild;
  707. _converse.connection._dataRecv(test_utils.createRequest(presence));
  708. expect(sizzle('div.chat-info', chat_content).length).toBe(2);
  709. expect(sizzle('div.chat-info:last', chat_content).pop().textContent).toBe("fabio has entered the groupchat");
  710. presence = Strophe.xmlHtmlNode(
  711. `<presence xmlns="jabber:client" to="dummy@localhost/resource" from="coven@chat.shakespeare.lit/Dele Olajide">
  712. <x xmlns="http://jabber.org/protocol/muc#user">
  713. <item affiliation="none" jid="deleo@traderlynk.4ng.net/converse.js-39320524" role="participant"/>
  714. </x>
  715. </presence>`).firstElementChild;
  716. _converse.connection._dataRecv(test_utils.createRequest(presence));
  717. expect(sizzle('div.chat-info', chat_content).length).toBe(3);
  718. expect(sizzle('div.chat-info:last', chat_content).pop().textContent).toBe("Dele Olajide has entered the groupchat");
  719. presence = Strophe.xmlHtmlNode(
  720. `<presence xmlns="jabber:client" to="dummy@localhost/resource" from="coven@chat.shakespeare.lit/jcbrand">
  721. <x xmlns="http://jabber.org/protocol/muc#user">
  722. <item affiliation="owner" jid="jc@opkode.com/converse.js-30645022" role="moderator"/>
  723. <status code="110"/>
  724. </x>
  725. </presence>`).firstElementChild;
  726. _converse.connection._dataRecv(test_utils.createRequest(presence));
  727. expect(sizzle('div.chat-info', chat_content).length).toBe(4);
  728. expect(sizzle('div.chat-info:last', chat_content).pop().textContent).toBe("jcbrand has entered the groupchat");
  729. presence = Strophe.xmlHtmlNode(
  730. `<presence xmlns="jabber:client" to="dummy@localhost/resource" type="unavailable" from="coven@chat.shakespeare.lit/Dele Olajide">
  731. <x xmlns="http://jabber.org/protocol/muc#user">
  732. <item affiliation="none" jid="deleo@traderlynk.4ng.net/converse.js-39320524" role="none"/>
  733. </x>
  734. </presence>`).firstElementChild;
  735. _converse.connection._dataRecv(test_utils.createRequest(presence));
  736. expect(sizzle('div.chat-info', chat_content).length).toBe(4);
  737. expect(sizzle('div.chat-info:last', chat_content).pop().textContent).toBe("Dele Olajide has entered and left the groupchat");
  738. presence = Strophe.xmlHtmlNode(
  739. `<presence xmlns="jabber:client" to="dummy@localhost/resource" 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-74567907" role="participant"/>
  742. </x>
  743. </presence>`).firstElementChild;
  744. _converse.connection._dataRecv(test_utils.createRequest(presence));
  745. expect(sizzle('div.chat-info', chat_content).length).toBe(4);
  746. expect(sizzle('div.chat-info:last', chat_content).pop().textContent).toBe("Dele Olajide has entered the groupchat");
  747. presence = Strophe.xmlHtmlNode(
  748. `<presence xmlns="jabber:client" to="dummy@localhost/resource" from="coven@chat.shakespeare.lit/fuvuv" xml:lang="en">
  749. <c xmlns="http://jabber.org/protocol/caps" node="http://jabber.pix-art.de" ver="5tOurnuFnp2h50hKafeUyeN4Yl8=" hash="sha-1"/>
  750. <x xmlns="vcard-temp:x:update"/>
  751. <x xmlns="http://jabber.org/protocol/muc#user">
  752. <item affiliation="none" jid="fuvuv@blabber.im/Pix-Art Messenger.8zoB" role="participant"/>
  753. </x>
  754. </presence>`).firstElementChild;
  755. _converse.connection._dataRecv(test_utils.createRequest(presence));
  756. expect(sizzle('div.chat-info', chat_content).length).toBe(5);
  757. expect(sizzle('div.chat-info:last', chat_content).pop().textContent).toBe("fuvuv has entered the groupchat");
  758. presence = Strophe.xmlHtmlNode(
  759. `<presence xmlns="jabber:client" to="dummy@localhost/resource" type="unavailable" from="coven@chat.shakespeare.lit/fuvuv">
  760. <x xmlns="http://jabber.org/protocol/muc#user">
  761. <item affiliation="none" jid="fuvuv@blabber.im/Pix-Art Messenger.8zoB" role="none"/>
  762. </x>
  763. </presence>`).firstElementChild;
  764. _converse.connection._dataRecv(test_utils.createRequest(presence));
  765. expect(sizzle('div.chat-info', chat_content).length).toBe(5);
  766. expect(sizzle('div.chat-info:last', chat_content).pop().textContent).toBe("fuvuv has entered and left the groupchat");
  767. presence = Strophe.xmlHtmlNode(
  768. `<presence xmlns="jabber:client" to="dummy@localhost/resource" type="unavailable" from="coven@chat.shakespeare.lit/fabio">
  769. <status>Disconnected: Replaced by new connection</status>
  770. <x xmlns="http://jabber.org/protocol/muc#user">
  771. <item affiliation="none" jid="fabio@montefuscolo.com.br/Conversations.ZvLu" role="none"/>
  772. </x>
  773. </presence>`).firstElementChild;
  774. _converse.connection._dataRecv(test_utils.createRequest(presence));
  775. expect(sizzle('div.chat-info', chat_content).length).toBe(5);
  776. expect(sizzle('div.chat-info:last', chat_content).pop().textContent).toBe(
  777. `fabio has entered and left the groupchat. "Disconnected: Replaced by new connection"`);
  778. presence = Strophe.xmlHtmlNode(
  779. `<presence xmlns="jabber:client" to="dummy@localhost/resource" from="coven@chat.shakespeare.lit/fabio">
  780. <c xmlns="http://jabber.org/protocol/caps" node="http://conversations.im" ver="INI3xjRUioclBTP/aACfWi5m9UY=" hash="sha-1"/>
  781. <x xmlns="http://jabber.org/protocol/muc#user">
  782. <item affiliation="none" jid="fabio@montefuscolo.com.br/Conversations.ZvLu" role="participant"/>
  783. </x>
  784. </presence>`).firstElementChild;
  785. _converse.connection._dataRecv(test_utils.createRequest(presence));
  786. expect(sizzle('div.chat-info', chat_content).length).toBe(5);
  787. expect(sizzle('div.chat-info:last', chat_content).pop().textContent).toBe(
  788. `fabio has entered the groupchat`);
  789. // XXX: hack so that we can test leave/enter of occupants
  790. // who were already in the room when we joined.
  791. chat_content.innerHTML = '';
  792. presence = Strophe.xmlHtmlNode(
  793. `<presence xmlns="jabber:client" to="dummy@localhost/resource" type="unavailable" from="coven@chat.shakespeare.lit/fabio">
  794. <status>Disconnected: closed</status>
  795. <x xmlns="http://jabber.org/protocol/muc#user">
  796. <item affiliation="none" jid="fabio@montefuscolo.com.br/Conversations.ZvLu" role="none"/>
  797. </x>
  798. </presence>`).firstElementChild;
  799. _converse.connection._dataRecv(test_utils.createRequest(presence));
  800. expect(sizzle('div.chat-info', chat_content).length).toBe(1);
  801. expect(sizzle('div.chat-info:last', chat_content).pop().textContent).toBe(
  802. `fabio has left the groupchat. "Disconnected: closed"`);
  803. presence = Strophe.xmlHtmlNode(
  804. `<presence xmlns="jabber:client" to="dummy@localhost/resource" type="unavailable" from="coven@chat.shakespeare.lit/Dele Olajide">
  805. <x xmlns="http://jabber.org/protocol/muc#user">
  806. <item affiliation="none" jid="deleo@traderlynk.4ng.net/converse.js-74567907" role="none"/>
  807. </x>
  808. </presence>`).firstElementChild;
  809. _converse.connection._dataRecv(test_utils.createRequest(presence));
  810. expect(sizzle('div.chat-info', chat_content).length).toBe(2);
  811. expect(sizzle('div.chat-info:last', chat_content).pop().textContent).toBe(
  812. `Dele Olajide has left the groupchat`);
  813. presence = Strophe.xmlHtmlNode(
  814. `<presence xmlns="jabber:client" to="dummy@localhost/resource" from="coven@chat.shakespeare.lit/fabio">
  815. <c xmlns="http://jabber.org/protocol/caps" node="http://conversations.im" ver="INI3xjRUioclBTP/aACfWi5m9UY=" hash="sha-1"/>
  816. <x xmlns="http://jabber.org/protocol/muc#user">
  817. <item affiliation="none" jid="fabio@montefuscolo.com.br/Conversations.ZvLu" role="participant"/>
  818. </x>
  819. </presence>`).firstElementChild;
  820. _converse.connection._dataRecv(test_utils.createRequest(presence));
  821. expect(sizzle('div.chat-info', chat_content).length).toBe(2);
  822. expect(sizzle('div.chat-info:last', chat_content).pop().textContent).toBe(
  823. `fabio has left and re-entered the groupchat`);
  824. done();
  825. }));
  826. it("role-change messages that follow a MUC leave are left out",
  827. mock.initConverseWithPromises(
  828. null, ['rosterGroupsFetched', 'chatBoxesFetched'], {},
  829. async function (done, _converse) {
  830. // See https://github.com/conversejs/converse.js/issues/1259
  831. await test_utils.openAndEnterChatRoom(_converse, 'conversations', 'conference.siacs.eu', 'dummy');
  832. const presence = $pres({
  833. to: 'dummy@localhost/resource',
  834. from: 'conversations@conference.siacs.eu/Guus'
  835. }).c('x', {
  836. 'xmlns': Strophe.NS.MUC_USER
  837. }).c('item', {
  838. 'affiliation': 'none',
  839. 'jid': 'Guus@localhost/xxx',
  840. 'role': 'visitor'
  841. });
  842. _converse.connection._dataRecv(test_utils.createRequest(presence));
  843. const view = _converse.chatboxviews.get('conversations@conference.siacs.eu');
  844. const msg = $msg({
  845. 'from': 'conversations@conference.siacs.eu/dummy',
  846. 'id': (new Date()).getTime(),
  847. 'to': 'dummy@localhost',
  848. 'type': 'groupchat'
  849. }).c('body').t('Some message').tree();
  850. await view.model.onMessage(msg);
  851. await new Promise((resolve, reject) => view.once('messageInserted', resolve));
  852. let stanza = Strophe.xmlHtmlNode(
  853. `<presence xmlns="jabber:client" to="dummy@localhost/resource" type="unavailable" from="conversations@conference.siacs.eu/Guus">
  854. <x xmlns="http://jabber.org/protocol/muc#user">
  855. <item affiliation="none" role="none"/>
  856. </x>
  857. </presence>`
  858. ).firstElementChild;
  859. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  860. stanza = Strophe.xmlHtmlNode(
  861. `<presence xmlns="jabber:client" to="dummy@localhost/resource" from="conversations@conference.siacs.eu/Guus">
  862. <c xmlns="http://jabber.org/protocol/caps" node="http://conversations.im" ver="ISg6+9AoK1/cwhbNEDviSvjdPzI=" hash="sha-1"/>
  863. <x xmlns="vcard-temp:x:update">
  864. <photo>bf987c486c51fbc05a6a4a9f20dd19b5efba3758</photo>
  865. </x>
  866. <x xmlns="http://jabber.org/protocol/muc#user">
  867. <item affiliation="none" role="visitor"/>
  868. </x>
  869. </presence>`
  870. ).firstElementChild;
  871. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  872. const chat_content = view.el.querySelector('.chat-content');
  873. const messages = chat_content.querySelectorAll('div.chat-info');
  874. expect(messages.length).toBe(3);
  875. expect(messages[0].textContent).toBe('dummy has entered the groupchat');
  876. expect(messages[1].textContent).toBe('Guus has entered the groupchat');
  877. expect(messages[2].textContent).toBe('Guus has left and re-entered the groupchat');
  878. done();
  879. }));
  880. it("shows a new day indicator if a join/leave message is received on a new day",
  881. mock.initConverseWithPromises(
  882. null, ['rosterGroupsFetched'], {},
  883. async function (done, _converse) {
  884. await test_utils.openAndEnterChatRoom(_converse, 'coven', 'chat.shakespeare.lit', 'dummy');
  885. const view = _converse.chatboxviews.get('coven@chat.shakespeare.lit');
  886. const chat_content = view.el.querySelector('.chat-content');
  887. let indicator = chat_content.querySelector('.date-separator');
  888. expect(indicator).not.toBe(null);
  889. expect(indicator.getAttribute('class')).toEqual('message date-separator');
  890. expect(indicator.getAttribute('data-isodate')).toEqual(moment().startOf('day').format());
  891. expect(indicator.querySelector('time').textContent).toEqual(moment().startOf('day').format("dddd MMM Do YYYY"));
  892. expect(chat_content.querySelectorAll('div.chat-info').length).toBe(1);
  893. expect(chat_content.querySelector('div.chat-info').textContent).toBe("dummy has entered the groupchat");
  894. const baseTime = new Date();
  895. jasmine.clock().install();
  896. jasmine.clock().mockDate(baseTime);
  897. var ONE_DAY_LATER = 86400000;
  898. jasmine.clock().tick(ONE_DAY_LATER);
  899. /* <presence to="dummy@localhost/_converse.js-29092160"
  900. * from="coven@chat.shakespeare.lit/some1">
  901. * <x xmlns="http://jabber.org/protocol/muc#user">
  902. * <item affiliation="owner" jid="dummy@localhost/_converse.js-29092160" role="moderator"/>
  903. * <status code="110"/>
  904. * </x>
  905. * </presence></body>
  906. */
  907. var presence = $pres({
  908. to: 'dummy@localhost/_converse.js-29092160',
  909. from: 'coven@chat.shakespeare.lit/some1'
  910. }).c('x', {xmlns: Strophe.NS.MUC_USER})
  911. .c('item', {
  912. 'affiliation': 'owner',
  913. 'jid': 'some1@localhost/_converse.js-290929789',
  914. 'role': 'moderator'
  915. });
  916. _converse.connection._dataRecv(test_utils.createRequest(presence));
  917. indicator = chat_content.querySelector('.date-separator[data-isodate="'+moment().startOf('day').format()+'"]');
  918. expect(indicator).not.toBe(null);
  919. expect(indicator.getAttribute('class')).toEqual('message date-separator');
  920. expect(indicator.getAttribute('data-isodate')).toEqual(moment().startOf('day').format());
  921. expect(indicator.querySelector('time').getAttribute('class')).toEqual('separator-text');
  922. expect(indicator.querySelector('time').textContent).toEqual(moment().startOf('day').format("dddd MMM Do YYYY"));
  923. expect(chat_content.querySelectorAll('div.chat-info').length).toBe(2);
  924. expect(chat_content.querySelector('div.chat-info:last-child').textContent).toBe(
  925. "some1 has entered the groupchat"
  926. );
  927. jasmine.clock().tick(ONE_DAY_LATER);
  928. // Test a user leaving a groupchat
  929. presence = $pres({
  930. to: 'dummy@localhost/_converse.js-29092160',
  931. type: 'unavailable',
  932. from: 'coven@chat.shakespeare.lit/some1'
  933. })
  934. .c('status', 'Disconnected: Replaced by new connection').up()
  935. .c('x', {xmlns: Strophe.NS.MUC_USER})
  936. .c('item', {
  937. 'affiliation': 'owner',
  938. 'jid': 'some1@localhost/_converse.js-290929789',
  939. 'role': 'moderator'
  940. });
  941. _converse.connection._dataRecv(test_utils.createRequest(presence));
  942. indicator = chat_content.querySelector('.date-separator[data-isodate="'+moment().startOf('day').format()+'"]');
  943. expect(indicator).not.toBe(null);
  944. expect(indicator.getAttribute('class')).toEqual('message date-separator');
  945. expect(indicator.getAttribute('data-isodate')).toEqual(moment().startOf('day').format());
  946. expect(indicator.querySelector('time').textContent).toEqual(moment().startOf('day').format("dddd MMM Do YYYY"));
  947. expect(chat_content.querySelectorAll('div.chat-info').length).toBe(3);
  948. expect(sizzle('div.chat-info:last', chat_content).pop().textContent).toBe(
  949. 'some1 has left the groupchat. '+
  950. '"Disconnected: Replaced by new connection"');
  951. jasmine.clock().tick(ONE_DAY_LATER);
  952. let stanza = Strophe.xmlHtmlNode(
  953. '<message xmlns="jabber:client"' +
  954. ' to="dummy@localhost/_converse.js-290929789"' +
  955. ' type="groupchat"' +
  956. ' from="coven@chat.shakespeare.lit/some1">'+
  957. ' <body>hello world</body>'+
  958. ' <delay xmlns="urn:xmpp:delay" stamp="'+moment().format()+'" from="some1@localhost"/>'+
  959. '</message>').firstChild;
  960. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  961. await new Promise((resolve, reject) => view.once('messageInserted', resolve));
  962. presence = $pres({
  963. to: 'dummy@localhost/_converse.js-29092160',
  964. from: 'coven@chat.shakespeare.lit/newguy'
  965. }).c('x', {xmlns: Strophe.NS.MUC_USER})
  966. .c('item', {
  967. 'affiliation': 'none',
  968. 'jid': 'newguy@localhost/_converse.js-290929789',
  969. 'role': 'participant'
  970. });
  971. _converse.connection._dataRecv(test_utils.createRequest(presence));
  972. let time = chat_content.querySelectorAll('time.separator-text');
  973. expect(time.length).toEqual(4);
  974. indicator = sizzle('.date-separator:eq(3)', chat_content).pop();
  975. expect(indicator.getAttribute('class')).toEqual('message date-separator');
  976. expect(indicator.getAttribute('data-isodate')).toEqual(moment().startOf('day').format());
  977. expect(indicator.querySelector('time').textContent).toEqual(moment().startOf('day').format("dddd MMM Do YYYY"));
  978. expect(chat_content.querySelectorAll('div.chat-info').length).toBe(4);
  979. expect(sizzle('div.chat-info:last', chat_content).pop().textContent)
  980. .toBe("newguy has entered the groupchat");
  981. jasmine.clock().tick(ONE_DAY_LATER);
  982. stanza = Strophe.xmlHtmlNode(
  983. '<message xmlns="jabber:client"' +
  984. ' to="dummy@localhost/_converse.js-290929789"' +
  985. ' type="groupchat"' +
  986. ' from="coven@chat.shakespeare.lit/some1">'+
  987. ' <body>hello world</body>'+
  988. ' <delay xmlns="urn:xmpp:delay" stamp="'+moment().format()+'" from="some1@localhost"/>'+
  989. '</message>').firstChild;
  990. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  991. await new Promise((resolve, reject) => view.once('messageInserted', resolve));
  992. jasmine.clock().tick(ONE_DAY_LATER);
  993. // Test a user leaving a groupchat
  994. presence = $pres({
  995. to: 'dummy@localhost/_converse.js-29092160',
  996. type: 'unavailable',
  997. from: 'coven@chat.shakespeare.lit/newguy'
  998. })
  999. .c('status', 'Disconnected: Replaced by new connection').up()
  1000. .c('x', {xmlns: Strophe.NS.MUC_USER})
  1001. .c('item', {
  1002. 'affiliation': 'none',
  1003. 'jid': 'newguy@localhost/_converse.js-290929789',
  1004. 'role': 'none'
  1005. });
  1006. _converse.connection._dataRecv(test_utils.createRequest(presence));
  1007. time = chat_content.querySelectorAll('time.separator-text');
  1008. expect(time.length).toEqual(6);
  1009. indicator = sizzle('.date-separator:eq(5)', chat_content).pop();
  1010. expect(indicator.getAttribute('class')).toEqual('message date-separator');
  1011. expect(indicator.getAttribute('data-isodate')).toEqual(moment().startOf('day').format());
  1012. expect(indicator.querySelector('time').textContent).toEqual(moment().startOf('day').format("dddd MMM Do YYYY"));
  1013. expect(chat_content.querySelectorAll('div.chat-info').length).toBe(5);
  1014. expect(sizzle('div.chat-info:last', chat_content).pop().textContent).toBe(
  1015. 'newguy has left the groupchat. '+
  1016. '"Disconnected: Replaced by new connection"');
  1017. jasmine.clock().uninstall();
  1018. done();
  1019. }));
  1020. it("supports the /me command",
  1021. mock.initConverseWithPromises(
  1022. null, ['rosterGroupsFetched'], {},
  1023. async function (done, _converse) {
  1024. await test_utils.waitUntilDiscoConfirmed(_converse, 'localhost', [], ['vcard-temp']);
  1025. await test_utils.waitUntil(() => _converse.xmppstatus.vcard.get('fullname'));
  1026. test_utils.createContacts(_converse, 'current');
  1027. await test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy');
  1028. const view = _converse.chatboxviews.get('lounge@localhost');
  1029. if (!view.el.querySelectorAll('.chat-area').length) {
  1030. view.renderChatArea();
  1031. }
  1032. let message = '/me is tired';
  1033. const nick = mock.chatroom_names[0];
  1034. let msg = $msg({
  1035. 'from': 'lounge@localhost/'+nick,
  1036. 'id': (new Date()).getTime(),
  1037. 'to': 'dummy@localhost',
  1038. 'type': 'groupchat'
  1039. }).c('body').t(message).tree();
  1040. await view.model.onMessage(msg);
  1041. await new Promise((resolve, reject) => view.once('messageInserted', resolve));
  1042. expect(_.includes(view.el.querySelector('.chat-msg__author').textContent, '**Dyon van de Wege')).toBeTruthy();
  1043. expect(view.el.querySelector('.chat-msg__text').textContent).toBe('is tired');
  1044. message = '/me is as well';
  1045. msg = $msg({
  1046. from: 'lounge@localhost/Max Mustermann',
  1047. id: (new Date()).getTime(),
  1048. to: 'dummy@localhost',
  1049. type: 'groupchat'
  1050. }).c('body').t(message).tree();
  1051. await view.model.onMessage(msg);
  1052. await new Promise((resolve, reject) => view.once('messageInserted', resolve));
  1053. expect(_.includes(sizzle('.chat-msg__author:last', view.el).pop().textContent, '**Max Mustermann')).toBeTruthy();
  1054. expect(sizzle('.chat-msg__text:last', view.el).pop().textContent).toBe('is as well');
  1055. done();
  1056. }));
  1057. it("can be configured if you're its owner",
  1058. mock.initConverseWithPromises(
  1059. null, ['rosterGroupsFetched', 'chatBoxesFetched'], {},
  1060. async function (done, _converse) {
  1061. let sent_IQ, IQ_id;
  1062. const sendIQ = _converse.connection.sendIQ;
  1063. spyOn(_converse.connection, 'sendIQ').and.callFake(function (iq, callback, errback) {
  1064. sent_IQ = iq;
  1065. IQ_id = sendIQ.bind(this)(iq, callback, errback);
  1066. });
  1067. await _converse.api.rooms.open('coven@chat.shakespeare.lit', {'nick': 'some1'});
  1068. const view = _converse.chatboxviews.get('coven@chat.shakespeare.lit');
  1069. spyOn(view.model, 'saveAffiliationAndRole').and.callThrough();
  1070. // We pretend this is a new room, so no disco info is returned.
  1071. const features_stanza = $iq({
  1072. from: 'coven@chat.shakespeare.lit',
  1073. 'id': IQ_id,
  1074. 'to': 'dummy@localhost/desktop',
  1075. 'type': 'error'
  1076. }).c('error', {'type': 'cancel'})
  1077. .c('item-not-found', {'xmlns': "urn:ietf:params:xml:ns:xmpp-stanzas"});
  1078. _converse.connection._dataRecv(test_utils.createRequest(features_stanza));
  1079. /* <presence to="dummy@localhost/_converse.js-29092160"
  1080. * from="coven@chat.shakespeare.lit/some1">
  1081. * <x xmlns="http://jabber.org/protocol/muc#user">
  1082. * <item affiliation="owner" jid="dummy@localhost/_converse.js-29092160" role="moderator"/>
  1083. * <status code="110"/>
  1084. * </x>
  1085. * </presence></body>
  1086. */
  1087. var presence = $pres({
  1088. to: 'dummy@localhost/_converse.js-29092160',
  1089. from: 'coven@chat.shakespeare.lit/some1'
  1090. }).c('x', {xmlns: Strophe.NS.MUC_USER})
  1091. .c('item', {
  1092. 'affiliation': 'owner',
  1093. 'jid': 'dummy@localhost/_converse.js-29092160',
  1094. 'role': 'moderator'
  1095. }).up()
  1096. .c('status', {code: '110'});
  1097. _converse.connection._dataRecv(test_utils.createRequest(presence));
  1098. expect(view.model.saveAffiliationAndRole).toHaveBeenCalled();
  1099. expect(u.isVisible(view.el.querySelector('.toggle-chatbox-button'))).toBeTruthy();
  1100. await test_utils.waitUntil(() => !_.isNull(view.el.querySelector('.configure-chatroom-button')))
  1101. expect(u.isVisible(view.el.querySelector('.configure-chatroom-button'))).toBeTruthy();
  1102. view.el.querySelector('.configure-chatroom-button').click();
  1103. /* Check that an IQ is sent out, asking for the
  1104. * configuration form.
  1105. * See: // http://xmpp.org/extensions/xep-0045.html#example-163
  1106. *
  1107. * <iq from='crone1@shakespeare.lit/desktop'
  1108. * id='config1'
  1109. * to='coven@chat.shakespeare.lit'
  1110. * type='get'>
  1111. * <query xmlns='http://jabber.org/protocol/muc#owner'/>
  1112. * </iq>
  1113. */
  1114. expect(sent_IQ.toLocaleString()).toBe(
  1115. `<iq id="`+IQ_id+`" to="coven@chat.shakespeare.lit" type="get" xmlns="jabber:client">`+
  1116. `<query xmlns="http://jabber.org/protocol/muc#owner"/>`+
  1117. `</iq>`);
  1118. /* Server responds with the configuration form.
  1119. * See: // http://xmpp.org/extensions/xep-0045.html#example-165
  1120. */
  1121. var config_stanza = $iq({from: 'coven@chat.shakespeare.lit',
  1122. 'id': IQ_id,
  1123. 'to': 'dummy@localhost/desktop',
  1124. 'type': 'result'})
  1125. .c('query', { 'xmlns': 'http://jabber.org/protocol/muc#owner'})
  1126. .c('x', { 'xmlns': 'jabber:x:data', 'type': 'form'})
  1127. .c('title').t('Configuration for "coven" Room').up()
  1128. .c('instructions').t('Complete this form to modify the configuration of your room.').up()
  1129. .c('field', {'type': 'hidden', 'var': 'FORM_TYPE'})
  1130. .c('value').t('http://jabber.org/protocol/muc#roomconfig').up().up()
  1131. .c('field', {
  1132. 'label': 'Natural-Language Room Name',
  1133. 'type': 'text-single',
  1134. 'var': 'muc#roomconfig_roomname'})
  1135. .c('value').t('A Dark Cave').up().up()
  1136. .c('field', {
  1137. 'label': 'Short Description of Room',
  1138. 'type': 'text-single',
  1139. 'var': 'muc#roomconfig_roomdesc'})
  1140. .c('value').t('The place for all good witches!').up().up()
  1141. .c('field', {
  1142. 'label': 'Enable Public Logging?',
  1143. 'type': 'boolean',
  1144. 'var': 'muc#roomconfig_enablelogging'})
  1145. .c('value').t(0).up().up()
  1146. .c('field', {
  1147. 'label': 'Allow Occupants to Change Subject?',
  1148. 'type': 'boolean',
  1149. 'var': 'muc#roomconfig_changesubject'})
  1150. .c('value').t(0).up().up()
  1151. .c('field', {
  1152. 'label': 'Allow Occupants to Invite Others?',
  1153. 'type': 'boolean',
  1154. 'var': 'muc#roomconfig_allowinvites'})
  1155. .c('value').t(0).up().up()
  1156. .c('field', {
  1157. 'label': 'Who Can Send Private Messages?',
  1158. 'type': 'list-single',
  1159. 'var': 'muc#roomconfig_allowpm'})
  1160. .c('value').t('anyone').up()
  1161. .c('option', {'label': 'Anyone'})
  1162. .c('value').t('anyone').up().up()
  1163. .c('option', {'label': 'Anyone with Voice'})
  1164. .c('value').t('participants').up().up()
  1165. .c('option', {'label': 'Moderators Only'})
  1166. .c('value').t('moderators').up().up()
  1167. .c('option', {'label': 'Nobody'})
  1168. .c('value').t('none').up().up().up()
  1169. .c('field', {
  1170. 'label': 'Roles for which Presence is Broadcasted',
  1171. 'type': 'list-multi',
  1172. 'var': 'muc#roomconfig_presencebroadcast'})
  1173. .c('value').t('moderator').up()
  1174. .c('value').t('participant').up()
  1175. .c('value').t('visitor').up()
  1176. .c('option', {'label': 'Moderator'})
  1177. .c('value').t('moderator').up().up()
  1178. .c('option', {'label': 'Participant'})
  1179. .c('value').t('participant').up().up()
  1180. .c('option', {'label': 'Visitor'})
  1181. .c('value').t('visitor').up().up().up()
  1182. .c('field', {
  1183. 'label': 'Roles and Affiliations that May Retrieve Member List',
  1184. 'type': 'list-multi',
  1185. 'var': 'muc#roomconfig_getmemberlist'})
  1186. .c('value').t('moderator').up()
  1187. .c('value').t('participant').up()
  1188. .c('value').t('visitor').up()
  1189. .c('option', {'label': 'Moderator'})
  1190. .c('value').t('moderator').up().up()
  1191. .c('option', {'label': 'Participant'})
  1192. .c('value').t('participant').up().up()
  1193. .c('option', {'label': 'Visitor'})
  1194. .c('value').t('visitor').up().up().up()
  1195. .c('field', {
  1196. 'label': 'Make Room Publicly Searchable?',
  1197. 'type': 'boolean',
  1198. 'var': 'muc#roomconfig_publicroom'})
  1199. .c('value').t(0).up().up()
  1200. .c('field', {
  1201. 'label': 'Make Room Publicly Searchable?',
  1202. 'type': 'boolean',
  1203. 'var': 'muc#roomconfig_publicroom'})
  1204. .c('value').t(0).up().up()
  1205. .c('field', {
  1206. 'label': 'Make Room Persistent?',
  1207. 'type': 'boolean',
  1208. 'var': 'muc#roomconfig_persistentroom'})
  1209. .c('value').t(0).up().up()
  1210. .c('field', {
  1211. 'label': 'Make Room Moderated?',
  1212. 'type': 'boolean',
  1213. 'var': 'muc#roomconfig_moderatedroom'})
  1214. .c('value').t(0).up().up()
  1215. .c('field', {
  1216. 'label': 'Make Room Members Only?',
  1217. 'type': 'boolean',
  1218. 'var': 'muc#roomconfig_membersonly'})
  1219. .c('value').t(0).up().up()
  1220. .c('field', {
  1221. 'label': 'Password Required for Entry?',
  1222. 'type': 'boolean',
  1223. 'var': 'muc#roomconfig_passwordprotectedroom'})
  1224. .c('value').t(1).up().up()
  1225. .c('field', {'type': 'fixed'})
  1226. .c('value').t('If a password is required to enter this groupchat,'+
  1227. 'you must specify the password below.').up().up()
  1228. .c('field', {
  1229. 'label': 'Password',
  1230. 'type': 'text-private',
  1231. 'var': 'muc#roomconfig_roomsecret'})
  1232. .c('value').t('cauldronburn');
  1233. _converse.connection._dataRecv(test_utils.createRequest(config_stanza));
  1234. await test_utils.waitUntil(() => view.el.querySelectorAll('form.chatroom-form').length)
  1235. expect(view.el.querySelectorAll('form.chatroom-form').length).toBe(1);
  1236. expect(view.el.querySelectorAll('form.chatroom-form fieldset').length).toBe(2);
  1237. var membersonly = view.el.querySelectorAll('input[name="muc#roomconfig_membersonly"]');
  1238. expect(membersonly.length).toBe(1);
  1239. expect(membersonly[0].getAttribute('type')).toBe('checkbox');
  1240. membersonly[0].checked = true;
  1241. var moderated = view.el.querySelectorAll('input[name="muc#roomconfig_moderatedroom"]');
  1242. expect(moderated.length).toBe(1);
  1243. expect(moderated[0].getAttribute('type')).toBe('checkbox');
  1244. moderated[0].checked = true;
  1245. var password = view.el.querySelectorAll('input[name="muc#roomconfig_roomsecret"]');
  1246. expect(password.length).toBe(1);
  1247. expect(password[0].getAttribute('type')).toBe('password');
  1248. var allowpm = view.el.querySelectorAll('select[name="muc#roomconfig_allowpm"]');
  1249. expect(allowpm.length).toBe(1);
  1250. allowpm[0].value = 'moderators';
  1251. var presencebroadcast = view.el.querySelectorAll('select[name="muc#roomconfig_presencebroadcast"]');
  1252. expect(presencebroadcast.length).toBe(1);
  1253. presencebroadcast[0].value = ['moderator'];
  1254. view.el.querySelector('input[type="submit"]').click();
  1255. const sent_stanza = sent_IQ.nodeTree;
  1256. expect(sent_stanza.querySelector('field[var="muc#roomconfig_membersonly"] value').textContent).toBe('1');
  1257. expect(sent_stanza.querySelector('field[var="muc#roomconfig_moderatedroom"] value').textContent).toBe('1');
  1258. expect(sent_stanza.querySelector('field[var="muc#roomconfig_allowpm"] value').textContent).toBe('moderators');
  1259. expect(sent_stanza.querySelector('field[var="muc#roomconfig_presencebroadcast"] value').textContent).toBe('moderator');
  1260. done();
  1261. }));
  1262. it("shows all members even if they're not currently present in the groupchat",
  1263. mock.initConverseWithPromises(
  1264. null, ['rosterGroupsFetched'], {},
  1265. async function (done, _converse) {
  1266. await test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy');
  1267. var name;
  1268. var view = _converse.chatboxviews.get('lounge@localhost'),
  1269. occupants = view.el.querySelector('.occupant-list');
  1270. var presence, role, jid, model;
  1271. for (var i=0; i<mock.chatroom_names.length; i++) {
  1272. name = mock.chatroom_names[i];
  1273. role = mock.chatroom_roles[name].role;
  1274. // See example 21 http://xmpp.org/extensions/xep-0045.html#enter-pres
  1275. jid =
  1276. presence = $pres({
  1277. to:'dummy@localhost/pda',
  1278. from:'lounge@localhost/'+name
  1279. }).c('x').attrs({xmlns:'http://jabber.org/protocol/muc#user'})
  1280. .c('item').attrs({
  1281. affiliation: mock.chatroom_roles[name].affiliation,
  1282. jid: name.replace(/ /g,'.').toLowerCase() + '@localhost',
  1283. role: role
  1284. }).up()
  1285. .c('status').attrs({code:'110'}).nodeTree;
  1286. _converse.connection._dataRecv(test_utils.createRequest(presence));
  1287. expect(occupants.querySelectorAll('li').length).toBe(2+i);
  1288. model = view.occupantsview.model.where({'nick': name})[0];
  1289. var index = view.occupantsview.model.indexOf(model);
  1290. expect(occupants.querySelectorAll('li .occupant-nick')[index].textContent.trim()).toBe(mock.chatroom_names[i]);
  1291. }
  1292. // Test users leaving the groupchat
  1293. // http://xmpp.org/extensions/xep-0045.html#exit
  1294. for (i=mock.chatroom_names.length-1; i>-1; i--) {
  1295. name = mock.chatroom_names[i];
  1296. role = mock.chatroom_roles[name].role;
  1297. // See example 21 http://xmpp.org/extensions/xep-0045.html#enter-pres
  1298. presence = $pres({
  1299. to:'dummy@localhost/pda',
  1300. from:'lounge@localhost/'+name,
  1301. type: 'unavailable'
  1302. }).c('x').attrs({xmlns:'http://jabber.org/protocol/muc#user'})
  1303. .c('item').attrs({
  1304. affiliation: mock.chatroom_roles[name].affiliation,
  1305. jid: name.replace(/ /g,'.').toLowerCase() + '@localhost',
  1306. role: 'none'
  1307. }).nodeTree;
  1308. _converse.connection._dataRecv(test_utils.createRequest(presence));
  1309. expect(occupants.querySelectorAll('li').length).toBe(7);
  1310. }
  1311. done();
  1312. }));
  1313. it("shows users currently present in the groupchat",
  1314. mock.initConverseWithPromises(
  1315. null, ['rosterGroupsFetched'], {},
  1316. async function (done, _converse) {
  1317. await test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy');
  1318. var name;
  1319. var view = _converse.chatboxviews.get('lounge@localhost'),
  1320. occupants = view.el.querySelector('.occupant-list');
  1321. var presence, role, jid, model;
  1322. for (var i=0; i<mock.chatroom_names.length; i++) {
  1323. name = mock.chatroom_names[i];
  1324. role = mock.chatroom_roles[name].role;
  1325. // See example 21 http://xmpp.org/extensions/xep-0045.html#enter-pres
  1326. jid =
  1327. presence = $pres({
  1328. to:'dummy@localhost/pda',
  1329. from:'lounge@localhost/'+name
  1330. }).c('x').attrs({xmlns:'http://jabber.org/protocol/muc#user'})
  1331. .c('item').attrs({
  1332. affiliation: 'none',
  1333. jid: name.replace(/ /g,'.').toLowerCase() + '@localhost',
  1334. role: role
  1335. }).up()
  1336. .c('status').attrs({code:'110'}).nodeTree;
  1337. _converse.connection._dataRecv(test_utils.createRequest(presence));
  1338. expect(occupants.querySelectorAll('li').length).toBe(2+i);
  1339. model = view.occupantsview.model.where({'nick': name})[0];
  1340. var index = view.occupantsview.model.indexOf(model);
  1341. expect(occupants.querySelectorAll('li .occupant-nick')[index].textContent.trim()).toBe(mock.chatroom_names[i]);
  1342. }
  1343. // Test users leaving the groupchat
  1344. // http://xmpp.org/extensions/xep-0045.html#exit
  1345. for (i=mock.chatroom_names.length-1; i>-1; i--) {
  1346. name = mock.chatroom_names[i];
  1347. role = mock.chatroom_roles[name].role;
  1348. // See example 21 http://xmpp.org/extensions/xep-0045.html#enter-pres
  1349. presence = $pres({
  1350. to:'dummy@localhost/pda',
  1351. from:'lounge@localhost/'+name,
  1352. type: 'unavailable'
  1353. }).c('x').attrs({xmlns:'http://jabber.org/protocol/muc#user'})
  1354. .c('item').attrs({
  1355. affiliation: mock.chatroom_roles[name].affiliation,
  1356. jid: name.replace(/ /g,'.').toLowerCase() + '@localhost',
  1357. role: 'none'
  1358. }).nodeTree;
  1359. _converse.connection._dataRecv(test_utils.createRequest(presence));
  1360. expect(occupants.querySelectorAll('li').length).toBe(i+1);
  1361. }
  1362. done();
  1363. }));
  1364. it("escapes occupant nicknames when rendering them, to avoid JS-injection attacks",
  1365. mock.initConverseWithPromises(null, ['rosterGroupsFetched'], {},
  1366. async function (done, _converse) {
  1367. await test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy');
  1368. /* <presence xmlns="jabber:client" to="jc@chat.example.org/converse.js-17184538"
  1369. * from="oo@conference.chat.example.org/&lt;img src=&quot;x&quot; onerror=&quot;alert(123)&quot;/&gt;">
  1370. * <x xmlns="http://jabber.org/protocol/muc#user">
  1371. * <item jid="jc@chat.example.org/converse.js-17184538" affiliation="owner" role="moderator"/>
  1372. * <status code="110"/>
  1373. * </x>
  1374. * </presence>"
  1375. */
  1376. const presence = $pres({
  1377. to:'dummy@localhost/pda',
  1378. from:"lounge@localhost/&lt;img src=&quot;x&quot; onerror=&quot;alert(123)&quot;/&gt;"
  1379. }).c('x').attrs({xmlns:'http://jabber.org/protocol/muc#user'})
  1380. .c('item').attrs({
  1381. jid: 'someone@localhost',
  1382. role: 'moderator',
  1383. }).up()
  1384. .c('status').attrs({code:'110'}).nodeTree;
  1385. _converse.connection._dataRecv(test_utils.createRequest(presence));
  1386. const view = _converse.chatboxviews.get('lounge@localhost');
  1387. const occupants = view.el.querySelector('.occupant-list').querySelectorAll('li .occupant-nick');
  1388. expect(occupants.length).toBe(2);
  1389. expect(occupants[0].textContent.trim()).toBe("&lt;img src=&quot;x&quot; onerror=&quot;alert(123)&quot;/&gt;");
  1390. done();
  1391. }));
  1392. it("indicates moderators and visitors by means of a special css class and tooltip",
  1393. mock.initConverseWithPromises(
  1394. null, ['rosterGroupsFetched'], {'view_mode': 'fullscreen'},
  1395. async function (done, _converse) {
  1396. await test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy');
  1397. const view = _converse.chatboxviews.get('lounge@localhost');
  1398. let contact_jid = mock.cur_names[2].replace(/ /g,'.').toLowerCase() + '@localhost';
  1399. let occupants = view.el.querySelector('.occupant-list').querySelectorAll('li');
  1400. expect(occupants.length).toBe(1);
  1401. expect(occupants[0].querySelector('.occupant-nick').textContent.trim()).toBe("dummy");
  1402. expect(occupants[0].querySelectorAll('.badge').length).toBe(2);
  1403. expect(occupants[0].querySelectorAll('.badge')[0].textContent).toBe('Owner');
  1404. expect(sizzle('.badge:last', occupants[0]).pop().textContent).toBe('Moderator');
  1405. var presence = $pres({
  1406. to:'dummy@localhost/pda',
  1407. from:'lounge@localhost/moderatorman'
  1408. }).c('x').attrs({xmlns:'http://jabber.org/protocol/muc#user'})
  1409. .c('item').attrs({
  1410. affiliation: 'admin',
  1411. jid: contact_jid,
  1412. role: 'moderator',
  1413. }).up()
  1414. .c('status').attrs({code:'110'}).nodeTree;
  1415. _converse.connection._dataRecv(test_utils.createRequest(presence));
  1416. occupants = view.el.querySelectorAll('.occupant-list li');
  1417. expect(occupants.length).toBe(2);
  1418. expect(occupants[0].querySelector('.occupant-nick').textContent.trim()).toBe("dummy");
  1419. expect(occupants[1].querySelector('.occupant-nick').textContent.trim()).toBe("moderatorman");
  1420. expect(occupants[1].querySelectorAll('.badge').length).toBe(2);
  1421. expect(occupants[1].querySelectorAll('.badge')[0].textContent).toBe('Admin');
  1422. expect(occupants[1].querySelectorAll('.badge')[1].textContent).toBe('Moderator');
  1423. expect(occupants[1].getAttribute('title')).toBe(
  1424. contact_jid + ' This user is a moderator. Click to mention moderatorman in your message.'
  1425. );
  1426. contact_jid = mock.cur_names[3].replace(/ /g,'.').toLowerCase() + '@localhost';
  1427. presence = $pres({
  1428. to:'dummy@localhost/pda',
  1429. from:'lounge@localhost/visitorwoman'
  1430. }).c('x').attrs({xmlns:'http://jabber.org/protocol/muc#user'})
  1431. .c('item').attrs({
  1432. jid: contact_jid,
  1433. role: 'visitor',
  1434. }).up()
  1435. .c('status').attrs({code:'110'}).nodeTree;
  1436. _converse.connection._dataRecv(test_utils.createRequest(presence));
  1437. occupants = view.el.querySelector('.occupant-list').querySelectorAll('li');
  1438. expect(occupants.length).toBe(3);
  1439. expect(occupants[2].querySelector('.occupant-nick').textContent.trim()).toBe("visitorwoman");
  1440. expect(occupants[2].querySelectorAll('.badge').length).toBe(1);
  1441. expect(sizzle('.badge', occupants[2]).pop().textContent).toBe('Visitor');
  1442. expect(occupants[2].getAttribute('title')).toBe(
  1443. contact_jid + ' This user can NOT send messages in this groupchat. Click to mention visitorwoman in your message.'
  1444. );
  1445. done();
  1446. }));
  1447. it("properly handles notification that a room has been destroyed",
  1448. mock.initConverseWithPromises(
  1449. null, ['rosterGroupsFetched'], {},
  1450. async function (done, _converse) {
  1451. await test_utils.openChatRoomViaModal(_converse, 'problematic@muc.localhost', 'dummy')
  1452. const presence = $pres().attrs({
  1453. from:'problematic@muc.localhost',
  1454. id:'n13mt3l',
  1455. to:'dummy@localhost/pda',
  1456. type:'error'})
  1457. .c('error').attrs({'type':'cancel'})
  1458. .c('gone').attrs({'xmlns':'urn:ietf:params:xml:ns:xmpp-stanzas'})
  1459. .t('xmpp:other-room@chat.jabberfr.org?join').up()
  1460. .c('text').attrs({'xmlns':'urn:ietf:params:xml:ns:xmpp-stanzas'})
  1461. .t("We didn't like the name").nodeTree;
  1462. const view = _converse.chatboxviews.get('problematic@muc.localhost');
  1463. spyOn(view, 'showErrorMessage').and.callThrough();
  1464. _converse.connection._dataRecv(test_utils.createRequest(presence));
  1465. expect(view.el.querySelector('.chatroom-body .disconnect-msg').textContent)
  1466. .toBe('This groupchat no longer exists');
  1467. expect(view.el.querySelector('.chatroom-body .destroyed-reason').textContent)
  1468. .toBe(`"We didn't like the name"`);
  1469. expect(view.el.querySelector('.chatroom-body .moved-label').textContent.trim())
  1470. .toBe('The conversation has moved. Click below to enter.');
  1471. expect(view.el.querySelector('.chatroom-body .moved-link').textContent.trim())
  1472. .toBe(`other-room@chat.jabberfr.org`);
  1473. done();
  1474. }));
  1475. it("will use the user's reserved nickname, if it exists",
  1476. mock.initConverseWithPromises(
  1477. null, ['rosterGroupsFetched', 'chatBoxesFetched'], {},
  1478. async function (done, _converse) {
  1479. const IQ_stanzas = _converse.connection.IQ_stanzas;
  1480. const sendIQ = _converse.connection.sendIQ;
  1481. const room_jid = 'lounge@localhost';
  1482. await test_utils.openChatRoom(_converse, 'lounge', 'localhost', 'dummy');
  1483. let stanza = await test_utils.waitUntil(() => _.get(_.filter(
  1484. IQ_stanzas,
  1485. iq => iq.nodeTree.querySelector(
  1486. `iq[to="${room_jid}"] query[xmlns="http://jabber.org/protocol/disco#info"]`
  1487. )).pop(), 'nodeTree')
  1488. );
  1489. // We pretend this is a new room, so no disco info is returned.
  1490. const features_stanza = $iq({
  1491. from: 'lounge@localhost',
  1492. 'id': stanza.getAttribute('id'),
  1493. 'to': 'dummy@localhost/desktop',
  1494. 'type': 'error'
  1495. }).c('error', {'type': 'cancel'})
  1496. .c('item-not-found', {'xmlns': "urn:ietf:params:xml:ns:xmpp-stanzas"});
  1497. _converse.connection._dataRecv(test_utils.createRequest(features_stanza));
  1498. const view = _converse.chatboxviews.get('lounge@localhost');
  1499. spyOn(view, 'join').and.callThrough();
  1500. /* <iq from='hag66@shakespeare.lit/pda'
  1501. * id='getnick1'
  1502. * to='coven@chat.shakespeare.lit'
  1503. * type='get'>
  1504. * <query xmlns='http://jabber.org/protocol/disco#info'
  1505. * node='x-roomuser-item'/>
  1506. * </iq>
  1507. */
  1508. const node = await test_utils.waitUntil(() => _.filter(
  1509. IQ_stanzas,
  1510. s => sizzle(`iq[to="${room_jid}"] query[node="x-roomuser-item"]`, s.nodeTree).length
  1511. ).pop()
  1512. );
  1513. const iq = node.nodeTree;
  1514. expect(node.toLocaleString()).toBe(
  1515. `<iq from="dummy@localhost/resource" id="${iq.getAttribute('id')}" to="lounge@localhost" `+
  1516. `type="get" xmlns="jabber:client">`+
  1517. `<query node="x-roomuser-item" xmlns="http://jabber.org/protocol/disco#info"/></iq>`);
  1518. /* <iq from='coven@chat.shakespeare.lit'
  1519. * id='getnick1'
  1520. * to='hag66@shakespeare.lit/pda'
  1521. * type='result'>
  1522. * <query xmlns='http://jabber.org/protocol/disco#info'
  1523. * node='x-roomuser-item'>
  1524. * <identity
  1525. * category='conference'
  1526. * name='thirdwitch'
  1527. * type='text'/>
  1528. * </query>
  1529. * </iq>
  1530. */
  1531. stanza = $iq({
  1532. 'type': 'result',
  1533. 'id': node.nodeTree.getAttribute('id'),
  1534. 'from': view.model.get('jid'),
  1535. 'to': _converse.connection.jid
  1536. }).c('query', {'xmlns': 'http://jabber.org/protocol/disco#info', 'node': 'x-roomuser-item'})
  1537. .c('identity', {'category': 'conference', 'name': 'thirdwitch', 'type': 'text'});
  1538. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  1539. expect(view.join).toHaveBeenCalled();
  1540. // The user has just entered the groupchat (because join was called)
  1541. // and receives their own presence from the server.
  1542. // See example 24:
  1543. // http://xmpp.org/extensions/xep-0045.html#enter-pres
  1544. const presence = $pres({
  1545. to:'dummy@localhost/resource',
  1546. from:'lounge@localhost/thirdwitch',
  1547. id:'DC352437-C019-40EC-B590-AF29E879AF97'
  1548. }).c('x').attrs({xmlns:'http://jabber.org/protocol/muc#user'})
  1549. .c('item').attrs({
  1550. affiliation: 'member',
  1551. jid: 'dummy@localhost/resource',
  1552. role: 'participant'
  1553. }).up()
  1554. .c('status').attrs({code:'110'}).up()
  1555. .c('status').attrs({code:'210'}).nodeTree;
  1556. _converse.connection._dataRecv(test_utils.createRequest(presence));
  1557. const info_text = sizzle('.chat-content .chat-info:first', view.el).pop().textContent;
  1558. expect(info_text).toBe('Your nickname has been automatically set to thirdwitch');
  1559. done();
  1560. }));
  1561. it("allows the user to invite their roster contacts to enter the groupchat",
  1562. mock.initConverseWithPromises(
  1563. null, ['rosterGroupsFetched', 'chatBoxesFetched'], {},
  1564. async function (done, _converse) {
  1565. test_utils.createContacts(_converse, 'current'); // We need roster contacts, so that we have someone to invite
  1566. // Since we don't actually fetch roster contacts, we need to
  1567. // cheat here and emit the event.
  1568. _converse.emit('rosterContactsFetched');
  1569. await test_utils.openChatRoom(_converse, 'lounge', 'localhost', 'dummy');
  1570. spyOn(_converse, 'emit');
  1571. spyOn(window, 'prompt').and.callFake(function () {
  1572. return "Please join!";
  1573. });
  1574. const view = _converse.chatboxviews.get('lounge@localhost');
  1575. // XXX: cheating a lttle bit, normally this'll be set after
  1576. // receiving the features for the groupchat.
  1577. view.model.set('open', 'true');
  1578. spyOn(view.model, 'directInvite').and.callThrough();
  1579. const chat_area = view.el.querySelector('.chat-area');
  1580. chat_area.parentElement.removeChild(chat_area);
  1581. await test_utils.waitUntil(() => view.el.querySelectorAll('input.invited-contact').length);
  1582. const input = view.el.querySelector('input.invited-contact');
  1583. expect(input.getAttribute('placeholder')).toBe('Invite');
  1584. input.value = "Felix";
  1585. let evt = new Event('input');
  1586. input.dispatchEvent(evt);
  1587. let sent_stanza;
  1588. spyOn(_converse.connection, 'send').and.callFake(function (stanza) {
  1589. sent_stanza = stanza;
  1590. });
  1591. const hint = input.nextSibling.firstElementChild;
  1592. expect(input.value).toBe('Felix');
  1593. expect(hint.textContent).toBe('Felix Amsel');
  1594. expect(input.nextSibling.childNodes.length).toBe(1);
  1595. if (typeof(Event) === 'function') {
  1596. // Not working on PhantomJS
  1597. evt = new Event('mousedown', {'bubbles': true});
  1598. evt.button = 0; // For some reason awesomplete wants this
  1599. hint.dispatchEvent(evt);
  1600. expect(window.prompt).toHaveBeenCalled();
  1601. expect(view.model.directInvite).toHaveBeenCalled();
  1602. expect(sent_stanza.toLocaleString()).toBe(
  1603. `<message from="dummy@localhost/resource" `+
  1604. `id="${sent_stanza.nodeTree.getAttribute("id")}" `+
  1605. `to="felix.amsel@localhost" `+
  1606. `xmlns="jabber:client">`+
  1607. `<x jid="lounge@localhost" reason="Please join!" xmlns="jabber:x:conference"/>`+
  1608. `</message>`
  1609. );
  1610. }
  1611. done();
  1612. }));
  1613. it("can be joined automatically, based upon a received invite",
  1614. mock.initConverseWithPromises(
  1615. null, ['rosterGroupsFetched'], {},
  1616. async function (done, _converse) {
  1617. test_utils.createContacts(_converse, 'current'); // We need roster contacts, who can invite us
  1618. spyOn(window, 'confirm').and.callFake(function () {
  1619. return true;
  1620. });
  1621. await test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy');
  1622. const view = _converse.chatboxviews.get('lounge@localhost');
  1623. view.close(); // Hack, otherwise we have to mock stanzas.
  1624. const name = mock.cur_names[0];
  1625. const from_jid = name.replace(/ /g,'.').toLowerCase() + '@localhost';
  1626. const room_jid = 'lounge@localhost';
  1627. const reason = "Please join this groupchat";
  1628. expect(_converse.chatboxes.models.length).toBe(1);
  1629. expect(_converse.chatboxes.models[0].id).toBe("controlbox");
  1630. const stanza = Strophe.xmlHtmlNode(
  1631. '<message xmlns="jabber:client" to="'+_converse.bare_jid+'" from="'+from_jid+'" id="9bceb415-f34b-4fa4-80d5-c0d076a24231">'+
  1632. '<x xmlns="jabber:x:conference" jid="'+room_jid+'" reason="'+reason+'"/>'+
  1633. '</message>').firstChild;
  1634. _converse.onDirectMUCInvitation(stanza);
  1635. expect(window.confirm).toHaveBeenCalledWith(
  1636. name + ' has invited you to join a groupchat: '+ room_jid +
  1637. ', and left the following reason: "'+reason+'"');
  1638. expect(_converse.chatboxes.models.length).toBe(2);
  1639. expect(_converse.chatboxes.models[0].id).toBe('controlbox');
  1640. expect(_converse.chatboxes.models[1].id).toBe(room_jid);
  1641. done();
  1642. }));
  1643. it("shows received groupchat messages",
  1644. mock.initConverseWithPromises(
  1645. null, ['rosterGroupsFetched'], {},
  1646. async function (done, _converse) {
  1647. const text = 'This is a received message';
  1648. await test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy');
  1649. spyOn(_converse, 'emit');
  1650. const view = _converse.chatboxviews.get('lounge@localhost');
  1651. if (!view.el.querySelectorAll('.chat-area').length) {
  1652. view.renderChatArea();
  1653. }
  1654. var nick = mock.chatroom_names[0];
  1655. view.model.occupants.create({
  1656. 'nick': nick,
  1657. 'muc_jid': `${view.model.get('jid')}/${nick}`
  1658. });
  1659. const message = $msg({
  1660. from: 'lounge@localhost/'+nick,
  1661. id: '1',
  1662. to: 'dummy@localhost',
  1663. type: 'groupchat'
  1664. }).c('body').t(text);
  1665. await view.model.onMessage(message.nodeTree);
  1666. await new Promise((resolve, reject) => view.once('messageInserted', resolve));
  1667. const chat_content = view.el.querySelector('.chat-content');
  1668. expect(chat_content.querySelectorAll('.chat-msg').length).toBe(1);
  1669. expect(chat_content.querySelector('.chat-msg__text').textContent).toBe(text);
  1670. expect(_converse.emit).toHaveBeenCalledWith('message', jasmine.any(Object));
  1671. done();
  1672. }));
  1673. it("shows sent groupchat messages",
  1674. mock.initConverseWithPromises(
  1675. null, ['rosterGroupsFetched'], {},
  1676. async function (done, _converse) {
  1677. await test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy');
  1678. spyOn(_converse, 'emit');
  1679. const view = _converse.chatboxviews.get('lounge@localhost');
  1680. if (!view.el.querySelectorAll('.chat-area').length) {
  1681. view.renderChatArea();
  1682. }
  1683. const text = 'This is a sent message';
  1684. const textarea = view.el.querySelector('.chat-textarea');
  1685. textarea.value = text;
  1686. view.keyPressed({
  1687. target: textarea,
  1688. preventDefault: _.noop,
  1689. keyCode: 13
  1690. });
  1691. await new Promise((resolve, reject) => view.once('messageInserted', resolve));
  1692. expect(_converse.emit).toHaveBeenCalledWith('messageSend', text);
  1693. const chat_content = view.el.querySelector('.chat-content');
  1694. expect(chat_content.querySelectorAll('.chat-msg').length).toBe(1);
  1695. // Let's check that if we receive the same message again, it's
  1696. // not shown.
  1697. const message = $msg({
  1698. from: 'lounge@localhost/dummy',
  1699. to: 'dummy@localhost.com',
  1700. type: 'groupchat',
  1701. id: view.model.messages.at(0).get('msgid')
  1702. }).c('body').t(text);
  1703. await view.model.onMessage(message.nodeTree);
  1704. expect(chat_content.querySelectorAll('.chat-msg').length).toBe(1);
  1705. expect(sizzle('.chat-msg__text:last').pop().textContent).toBe(text);
  1706. expect(view.model.messages.length).toBe(1);
  1707. // We don't emit an event if it's our own message
  1708. expect(_converse.emit.calls.count(), 1);
  1709. done();
  1710. }));
  1711. it("will cause the chat area to be scrolled down only if it was at the bottom already",
  1712. mock.initConverseWithPromises(
  1713. null, ['rosterGroupsFetched'], {},
  1714. async function (done, _converse) {
  1715. var message = 'This message is received while the chat area is scrolled up';
  1716. await test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy');
  1717. var view = _converse.chatboxviews.get('lounge@localhost');
  1718. spyOn(view, 'scrollDown').and.callThrough();
  1719. // Create enough messages so that there's a scrollbar.
  1720. const promises = [];
  1721. for (var i=0; i<20; i++) {
  1722. view.model.onMessage(
  1723. $msg({
  1724. from: 'lounge@localhost/someone',
  1725. to: 'dummy@localhost.com',
  1726. type: 'groupchat',
  1727. id: (new Date()).getTime(),
  1728. }).c('body').t('Message: '+i).tree());
  1729. promises.push(new Promise((resolve, reject) => view.once('messageInserted', resolve)))
  1730. }
  1731. await Promise.all(promises);
  1732. // Give enough time for `markScrolled` to have been called
  1733. setTimeout(async () => {
  1734. view.content.scrollTop = 0;
  1735. await view.model.onMessage(
  1736. $msg({
  1737. from: 'lounge@localhost/someone',
  1738. to: 'dummy@localhost.com',
  1739. type: 'groupchat',
  1740. id: (new Date()).getTime(),
  1741. }).c('body').t(message).tree());
  1742. await new Promise((resolve, reject) => view.once('messageInserted', resolve));
  1743. // Now check that the message appears inside the chatbox in the DOM
  1744. const chat_content = view.el.querySelector('.chat-content');
  1745. const msg_txt = sizzle('.chat-msg:last .chat-msg__text', chat_content).pop().textContent;
  1746. expect(msg_txt).toEqual(message);
  1747. expect(view.content.scrollTop).toBe(0);
  1748. done();
  1749. }, 500);
  1750. }));
  1751. it("shows the room topic in the header",
  1752. mock.initConverseWithPromises(
  1753. null, ['rosterGroupsFetched'], {},
  1754. async function (done, _converse) {
  1755. await test_utils.openAndEnterChatRoom(_converse, 'jdev', 'conference.jabber.org', 'jc');
  1756. const text = 'Jabber/XMPP Development | RFCs and Extensions: http://xmpp.org/ | Protocol and XSF discussions: xsf@muc.xmpp.org';
  1757. const stanza = Strophe.xmlHtmlNode(
  1758. '<message xmlns="jabber:client" to="jc@opkode.com/_converse.js-60429116" type="groupchat" from="jdev@conference.jabber.org/ralphm">'+
  1759. ' <subject>'+text+'</subject>'+
  1760. ' <delay xmlns="urn:xmpp:delay" stamp="2014-02-04T09:35:39Z" from="jdev@conference.jabber.org"/>'+
  1761. ' <x xmlns="jabber:x:delay" stamp="20140204T09:35:39" from="jdev@conference.jabber.org"/>'+
  1762. '</message>').firstChild;
  1763. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  1764. const view = _converse.chatboxviews.get('jdev@conference.jabber.org');
  1765. await new Promise((resolve, reject) => view.once('messageInserted', resolve));
  1766. const chat_content = view.el.querySelector('.chat-content');
  1767. expect(sizzle('.chat-event:last').pop().textContent).toBe('Topic set by ralphm');
  1768. expect(sizzle('.chat-topic:last').pop().textContent).toBe(text);
  1769. expect(view.el.querySelector('.chatroom-description').textContent).toBe(text);
  1770. done();
  1771. }));
  1772. it("escapes the subject before rendering it, to avoid JS-injection attacks",
  1773. mock.initConverseWithPromises(
  1774. null, ['rosterGroupsFetched'], {},
  1775. async function (done, _converse) {
  1776. await test_utils.openAndEnterChatRoom(_converse, 'jdev', 'conference.jabber.org', 'jc');
  1777. spyOn(window, 'alert');
  1778. const subject = '<img src="x" onerror="alert(\'XSS\');"/>';
  1779. const view = _converse.chatboxviews.get('jdev@conference.jabber.org');
  1780. view.model.set({'subject': {
  1781. 'text': subject,
  1782. 'author': 'ralphm'
  1783. }});
  1784. const chat_content = view.el.querySelector('.chat-content');
  1785. expect(sizzle('.chat-event:last').pop().textContent).toBe('Topic set by ralphm');
  1786. expect(sizzle('.chat-topic:last').pop().textContent).toBe(subject);
  1787. done();
  1788. }));
  1789. it("informs users if their nicknames has been changed.",
  1790. mock.initConverseWithPromises(
  1791. null, ['rosterGroupsFetched'], {},
  1792. async function (done, _converse) {
  1793. /* The service then sends two presence stanzas to the full JID
  1794. * of each occupant (including the occupant who is changing his
  1795. * or her room nickname), one of type "unavailable" for the old
  1796. * nickname and one indicating availability for the new
  1797. * nickname.
  1798. *
  1799. * See: http://xmpp.org/extensions/xep-0045.html#changenick
  1800. *
  1801. * <presence
  1802. * from='coven@localhost/thirdwitch'
  1803. * id='DC352437-C019-40EC-B590-AF29E879AF98'
  1804. * to='hag66@shakespeare.lit/pda'
  1805. * type='unavailable'>
  1806. * <x xmlns='http://jabber.org/protocol/muc#user'>
  1807. * <item affiliation='member'
  1808. * jid='hag66@shakespeare.lit/pda'
  1809. * nick='oldhag'
  1810. * role='participant'/>
  1811. * <status code='303'/>
  1812. * <status code='110'/>
  1813. * </x>
  1814. * </presence>
  1815. *
  1816. * <presence
  1817. * from='coven@localhost/oldhag'
  1818. * id='5B4F27A4-25ED-43F7-A699-382C6B4AFC67'
  1819. * to='hag66@shakespeare.lit/pda'>
  1820. * <x xmlns='http://jabber.org/protocol/muc#user'>
  1821. * <item affiliation='member'
  1822. * jid='hag66@shakespeare.lit/pda'
  1823. * role='participant'/>
  1824. * <status code='110'/>
  1825. * </x>
  1826. * </presence>
  1827. */
  1828. const __ = _converse.__;
  1829. await test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'oldnick');
  1830. const view = _converse.chatboxviews.get('lounge@localhost');
  1831. const chat_content = view.el.querySelector('.chat-content');
  1832. let occupants = view.el.querySelector('.occupant-list');
  1833. expect(occupants.childNodes.length).toBe(1);
  1834. expect(occupants.firstElementChild.querySelector('.occupant-nick').textContent.trim()).toBe("oldnick");
  1835. expect(chat_content.querySelectorAll('div.chat-info').length).toBe(1);
  1836. expect(sizzle('div.chat-info:first', chat_content).pop().textContent)
  1837. .toBe("oldnick has entered the groupchat");
  1838. let presence = $pres().attrs({
  1839. from:'lounge@localhost/oldnick',
  1840. id:'DC352437-C019-40EC-B590-AF29E879AF98',
  1841. to:'dummy@localhost/pda',
  1842. type:'unavailable'
  1843. })
  1844. .c('x').attrs({xmlns:'http://jabber.org/protocol/muc#user'})
  1845. .c('item').attrs({
  1846. affiliation: 'owner',
  1847. jid: 'dummy@localhost/pda',
  1848. nick: 'newnick',
  1849. role: 'moderator'
  1850. }).up()
  1851. .c('status').attrs({code:'303'}).up()
  1852. .c('status').attrs({code:'110'}).nodeTree;
  1853. _converse.connection._dataRecv(test_utils.createRequest(presence));
  1854. expect(chat_content.querySelectorAll('div.chat-info').length).toBe(2);
  1855. expect(sizzle('div.chat-info:last').pop().textContent).toBe(
  1856. __(_converse.muc.new_nickname_messages["303"], "newnick")
  1857. );
  1858. occupants = view.el.querySelector('.occupant-list');
  1859. expect(occupants.childNodes.length).toBe(1);
  1860. presence = $pres().attrs({
  1861. from:'lounge@localhost/newnick',
  1862. id:'5B4F27A4-25ED-43F7-A699-382C6B4AFC67',
  1863. to:'dummy@localhost/pda'
  1864. })
  1865. .c('x').attrs({xmlns:'http://jabber.org/protocol/muc#user'})
  1866. .c('item').attrs({
  1867. affiliation: 'owner',
  1868. jid: 'dummy@localhost/pda',
  1869. role: 'moderator'
  1870. }).up()
  1871. .c('status').attrs({code:'110'}).nodeTree;
  1872. _converse.connection._dataRecv(test_utils.createRequest(presence));
  1873. // XXX: currently we still have an additional "has entered the groupchat"
  1874. // notification for the new nickname. Ideally we'd not have
  1875. // that, but that's probably not possible without some
  1876. // significant refactoring.
  1877. expect(chat_content.querySelectorAll('div.chat-info').length).toBe(3);
  1878. expect(sizzle('div.chat-info', chat_content)[1].textContent).toBe(
  1879. __(_converse.muc.new_nickname_messages["303"], "newnick")
  1880. );
  1881. occupants = view.el.querySelector('.occupant-list');
  1882. expect(occupants.childNodes.length).toBe(1);
  1883. expect(sizzle('.occupant-nick:first', occupants).pop().textContent).toBe("newnick");
  1884. done();
  1885. }));
  1886. it("queries for the groupchat information before attempting to join the user",
  1887. mock.initConverseWithPromises(
  1888. null, ['rosterGroupsFetched', 'chatBoxesFetched'], {},
  1889. async function (done, _converse) {
  1890. const IQ_stanzas = _converse.connection.IQ_stanzas;
  1891. const room_jid = 'coven@chat.shakespeare.lit';
  1892. await _converse.api.rooms.open(room_jid, {'nick': 'some1'});
  1893. const node = await test_utils.waitUntil(() => _.filter(
  1894. IQ_stanzas,
  1895. iq => iq.nodeTree.querySelector(
  1896. `iq[to="${room_jid}"] query[xmlns="http://jabber.org/protocol/disco#info"]`
  1897. )).pop());
  1898. // Check that the groupchat queried for the feautures.
  1899. const stanza = node.nodeTree;
  1900. expect(node.toLocaleString()).toBe(
  1901. `<iq from="dummy@localhost/resource" id="${stanza.getAttribute("id")}" to="${room_jid}" type="get" xmlns="jabber:client">`+
  1902. `<query xmlns="http://jabber.org/protocol/disco#info"/>`+
  1903. `</iq>`);
  1904. /* <iq from='coven@chat.shakespeare.lit'
  1905. * id='ik3vs715'
  1906. * to='hag66@shakespeare.lit/pda'
  1907. * type='result'>
  1908. * <query xmlns='http://jabber.org/protocol/disco#info'>
  1909. * <identity
  1910. * category='conference'
  1911. * name='A Dark Cave'
  1912. * type='text'/>
  1913. * <feature var='http://jabber.org/protocol/muc'/>
  1914. * <feature var='muc_passwordprotected'/>
  1915. * <feature var='muc_hidden'/>
  1916. * <feature var='muc_temporary'/>
  1917. * <feature var='muc_open'/>
  1918. * <feature var='muc_unmoderated'/>
  1919. * <feature var='muc_nonanonymous'/>
  1920. * </query>
  1921. * </iq>
  1922. */
  1923. const features_stanza = $iq({
  1924. 'from': room_jid,
  1925. 'id': stanza.getAttribute('id'),
  1926. 'to': 'dummy@localhost/desktop',
  1927. 'type': 'result'
  1928. })
  1929. .c('query', { 'xmlns': 'http://jabber.org/protocol/disco#info'})
  1930. .c('identity', {
  1931. 'category': 'conference',
  1932. 'name': 'A Dark Cave',
  1933. 'type': 'text'
  1934. }).up()
  1935. .c('feature', {'var': 'http://jabber.org/protocol/muc'}).up()
  1936. .c('feature', {'var': 'muc_passwordprotected'}).up()
  1937. .c('feature', {'var': 'muc_hidden'}).up()
  1938. .c('feature', {'var': 'muc_temporary'}).up()
  1939. .c('feature', {'var': 'muc_open'}).up()
  1940. .c('feature', {'var': 'muc_unmoderated'}).up()
  1941. .c('feature', {'var': 'muc_nonanonymous'});
  1942. _converse.connection._dataRecv(test_utils.createRequest(features_stanza));
  1943. let view = _converse.chatboxviews.get('coven@chat.shakespeare.lit');
  1944. await test_utils.waitUntil(() => (view.model.get('connection_status') === converse.ROOMSTATUS.CONNECTING));
  1945. view = _converse.chatboxviews.get('coven@chat.shakespeare.lit');
  1946. expect(view.model.features.get('fetched')).toBeTruthy();
  1947. expect(view.model.features.get('passwordprotected')).toBe(true);
  1948. expect(view.model.features.get('hidden')).toBe(true);
  1949. expect(view.model.features.get('temporary')).toBe(true);
  1950. expect(view.model.features.get('open')).toBe(true);
  1951. expect(view.model.features.get('unmoderated')).toBe(true);
  1952. expect(view.model.features.get('nonanonymous')).toBe(true);
  1953. done();
  1954. }));
  1955. it("updates the shown features when the groupchat configuration has changed",
  1956. mock.initConverseWithPromises(
  1957. null, ['rosterGroupsFetched'], {'view_mode': 'fullscreen'},
  1958. async function (done, _converse) {
  1959. let features = [
  1960. 'http://jabber.org/protocol/muc',
  1961. 'jabber:iq:register',
  1962. 'muc_passwordprotected',
  1963. 'muc_publicroom',
  1964. 'muc_temporary',
  1965. 'muc_open',
  1966. 'muc_unmoderated',
  1967. 'muc_nonanonymous'
  1968. ];
  1969. await test_utils.openAndEnterChatRoom(_converse, 'room', 'conference.example.org', 'dummy', features);
  1970. const jid = 'room@conference.example.org';
  1971. const chatroomview = _converse.chatboxviews.get(jid);
  1972. let features_list = chatroomview.el.querySelector('.features-list');
  1973. let features_shown = features_list.textContent.split('\n').map(s => s.trim()).filter(s => s);
  1974. expect(_.difference(["Password protected", "Open", "Temporary", "Not anonymous", "Not moderated"], features_shown).length).toBe(0);
  1975. expect(chatroomview.model.features.get('hidden')).toBe(false);
  1976. expect(chatroomview.model.features.get('mam_enabled')).toBe(false);
  1977. expect(chatroomview.model.features.get('membersonly')).toBe(false);
  1978. expect(chatroomview.model.features.get('moderated')).toBe(false);
  1979. expect(chatroomview.model.features.get('nonanonymous')).toBe(true);
  1980. expect(chatroomview.model.features.get('open')).toBe(true);
  1981. expect(chatroomview.model.features.get('passwordprotected')).toBe(true);
  1982. expect(chatroomview.model.features.get('persistent')).toBe(false);
  1983. expect(chatroomview.model.features.get('publicroom')).toBe(true);
  1984. expect(chatroomview.model.features.get('semianonymous')).toBe(false);
  1985. expect(chatroomview.model.features.get('temporary')).toBe(true);
  1986. expect(chatroomview.model.features.get('unmoderated')).toBe(true);
  1987. expect(chatroomview.model.features.get('unsecured')).toBe(false);
  1988. expect(chatroomview.el.querySelector('.chat-title').textContent.trim()).toBe('Room');
  1989. chatroomview.el.querySelector('.configure-chatroom-button').click();
  1990. const IQs = _converse.connection.IQ_stanzas;
  1991. let iq = await test_utils.waitUntil(() => _.filter(
  1992. IQs,
  1993. iq => iq.nodeTree.querySelector(
  1994. `iq[to="${jid}"] query[xmlns="${Strophe.NS.MUC_OWNER}"]`
  1995. )).pop());
  1996. const response = Strophe.xmlHtmlNode(
  1997. `<iq xmlns="jabber:client"
  1998. type="result"
  1999. to="dummy@localhost/pda"
  2000. from="room@conference.example.org" id="${iq.nodeTree.getAttribute('id')}">
  2001. <query xmlns="http://jabber.org/protocol/muc#owner">
  2002. <x xmlns="jabber:x:data" type="form">
  2003. <title>Configuration for room@conference.example.org</title>
  2004. <instructions>Complete and submit this form to configure the room.</instructions>
  2005. <field var="FORM_TYPE" type="hidden">
  2006. <value>http://jabber.org/protocol/muc#roomconfig</value>
  2007. </field>
  2008. <field type="fixed">
  2009. <value>Room information</value>
  2010. </field>
  2011. <field var="muc#roomconfig_roomname" type="text-single" label="Title">
  2012. <value>Room</value>
  2013. </field>
  2014. <field var="muc#roomconfig_roomdesc" type="text-single" label="Description">
  2015. <desc>A brief description of the room</desc>
  2016. <value>This room is used in tests</value>
  2017. </field>
  2018. <field var="muc#roomconfig_lang" type="text-single" label="Language tag for room (e.g. 'en', 'de', 'fr' etc.)">
  2019. <desc>Indicate the primary language spoken in this room</desc>
  2020. <value>en</value>
  2021. </field>
  2022. <field var="muc#roomconfig_persistentroom" type="boolean" label="Persistent (room should remain even when it is empty)">
  2023. <desc>Rooms are automatically deleted when they are empty, unless this option is enabled</desc>
  2024. <value>1</value>
  2025. </field>
  2026. <field var="muc#roomconfig_publicroom" type="boolean" label="Include room information in public lists">
  2027. <desc>Enable this to allow people to find the room</desc>
  2028. <value>1</value>
  2029. </field>
  2030. <field type="fixed"><value>Access to the room</value></field>
  2031. <field var="muc#roomconfig_roomsecret" type="text-private" label="Password"><value/></field>
  2032. <field var="muc#roomconfig_membersonly" type="boolean" label="Only allow members to join">
  2033. <desc>Enable this to only allow access for room owners, admins and members</desc>
  2034. </field>
  2035. <field var="{http://prosody.im/protocol/muc}roomconfig_allowmemberinvites" type="boolean" label="Allow members to invite new members"/>
  2036. <field type="fixed"><value>Permissions in the room</value>
  2037. </field>
  2038. <field var="muc#roomconfig_changesubject" type="boolean" label="Allow anyone to set the room's subject">
  2039. <desc>Choose whether anyone, or only moderators, may set the room's subject</desc>
  2040. </field>
  2041. <field var="muc#roomconfig_moderatedroom" type="boolean" label="Moderated (require permission to speak)">
  2042. <desc>In moderated rooms occupants must be given permission to speak by a room moderator</desc>
  2043. </field>
  2044. <field var="muc#roomconfig_whois" type="list-single" label="Addresses (JIDs) of room occupants may be viewed by:">
  2045. <option label="Moderators only"><value>moderators</value></option>
  2046. <option label="Anyone"><value>anyone</value></option>
  2047. <value>anyone</value>
  2048. </field>
  2049. <field type="fixed"><value>Other options</value></field>
  2050. <field var="muc#roomconfig_historylength" type="text-single" label="Maximum number of history messages returned by room">
  2051. <desc>Specify the maximum number of previous messages that should be sent to users when they join the room</desc>
  2052. <value>50</value>
  2053. </field>
  2054. <field var="muc#roomconfig_defaulthistorymessages" type="text-single" label="Default number of history messages returned by room">
  2055. <desc>Specify the number of previous messages sent to new users when they join the room</desc>
  2056. <value>20</value>
  2057. </field>
  2058. </x>
  2059. </query>
  2060. </iq>`);
  2061. const response_el = response.firstElementChild;
  2062. _converse.connection._dataRecv(test_utils.createRequest(response_el));
  2063. const el = await test_utils.waitUntil(() => document.querySelector('.chatroom-form legend'));
  2064. expect(el.textContent).toBe("Configuration for room@conference.example.org");
  2065. sizzle('[name="muc#roomconfig_membersonly"]', chatroomview.el).pop().click();
  2066. sizzle('[name="muc#roomconfig_roomname"]', chatroomview.el).pop().value = "New room name"
  2067. chatroomview.el.querySelector('.btn-primary').click();
  2068. iq = await test_utils.waitUntil(() => _.filter(IQs, iq => u.matchesSelector(iq.nodeTree, `iq[to="${jid}"][type="set"]`)).pop());
  2069. const result = $iq({
  2070. "xmlns": "jabber:client",
  2071. "type": "result",
  2072. "to": "dummy@localhost/resource",
  2073. "from": "lounge@muc.localhost",
  2074. "id": iq.nodeTree.getAttribute('id')
  2075. });
  2076. IQs.length = 0; // Empty the array
  2077. _converse.connection._dataRecv(test_utils.createRequest(result));
  2078. iq = await test_utils.waitUntil(() => _.filter(
  2079. IQs,
  2080. iq => iq.nodeTree.querySelector(
  2081. `iq[to="${jid}"] query[xmlns="http://jabber.org/protocol/disco#info"]`
  2082. )).pop());
  2083. const features_stanza = $iq({
  2084. 'from': jid,
  2085. 'id': iq.nodeTree.getAttribute('id'),
  2086. 'to': 'dummy@localhost/desktop',
  2087. 'type': 'result'
  2088. }).c('query', { 'xmlns': 'http://jabber.org/protocol/disco#info'})
  2089. .c('identity', {
  2090. 'category': 'conference',
  2091. 'name': 'New room name',
  2092. 'type': 'text'
  2093. }).up();
  2094. features = [
  2095. 'http://jabber.org/protocol/muc',
  2096. 'jabber:iq:register',
  2097. 'muc_passwordprotected',
  2098. 'muc_hidden',
  2099. 'muc_temporary',
  2100. 'muc_membersonly',
  2101. 'muc_unmoderated',
  2102. 'muc_nonanonymous'
  2103. ];
  2104. features.forEach(f => features_stanza.c('feature', {'var': f}).up());
  2105. features_stanza.c('x', { 'xmlns':'jabber:x:data', 'type':'result'})
  2106. .c('field', {'var':'FORM_TYPE', 'type':'hidden'})
  2107. .c('value').t('http://jabber.org/protocol/muc#roominfo').up().up()
  2108. .c('field', {'type':'text-single', 'var':'muc#roominfo_description', 'label':'Description'})
  2109. .c('value').t('This is the description').up().up()
  2110. .c('field', {'type':'text-single', 'var':'muc#roominfo_occupants', 'label':'Number of occupants'})
  2111. .c('value').t(0);
  2112. _converse.connection._dataRecv(test_utils.createRequest(features_stanza));
  2113. await test_utils.waitUntil(() => new Promise(success => chatroomview.model.features.on('change', success)));
  2114. features_list = chatroomview.el.querySelector('.features-list');
  2115. features_shown = features_list.textContent.split('\n').map(s => s.trim()).filter(s => s);
  2116. expect(_.difference(["Password protected", "Hidden", "Members only", "Temporary", "Not anonymous", "Not moderated"], features_shown).length).toBe(0);
  2117. expect(chatroomview.model.features.get('hidden')).toBe(true);
  2118. expect(chatroomview.model.features.get('mam_enabled')).toBe(false);
  2119. expect(chatroomview.model.features.get('membersonly')).toBe(true);
  2120. expect(chatroomview.model.features.get('moderated')).toBe(false);
  2121. expect(chatroomview.model.features.get('nonanonymous')).toBe(true);
  2122. expect(chatroomview.model.features.get('open')).toBe(false);
  2123. expect(chatroomview.model.features.get('passwordprotected')).toBe(true);
  2124. expect(chatroomview.model.features.get('persistent')).toBe(false);
  2125. expect(chatroomview.model.features.get('publicroom')).toBe(false);
  2126. expect(chatroomview.model.features.get('semianonymous')).toBe(false);
  2127. expect(chatroomview.model.features.get('temporary')).toBe(true);
  2128. expect(chatroomview.model.features.get('unmoderated')).toBe(true);
  2129. expect(chatroomview.model.features.get('unsecured')).toBe(false);
  2130. expect(chatroomview.el.querySelector('.chat-title').textContent.trim()).toBe('New room name');
  2131. done();
  2132. }));
  2133. it("indicates when a room is no longer anonymous",
  2134. mock.initConverseWithPromises(
  2135. null, ['rosterGroupsFetched'], {},
  2136. async function (done, _converse) {
  2137. let sent_IQ, IQ_id;
  2138. const sendIQ = _converse.connection.sendIQ;
  2139. await test_utils.openAndEnterChatRoom(_converse, 'coven', 'chat.shakespeare.lit', 'some1');
  2140. spyOn(_converse.connection, 'sendIQ').and.callFake(function (iq, callback, errback) {
  2141. sent_IQ = iq;
  2142. IQ_id = sendIQ.bind(this)(iq, callback, errback);
  2143. });
  2144. // We pretend this is a new room, so no disco info is returned.
  2145. const features_stanza = $iq({
  2146. from: 'coven@chat.shakespeare.lit',
  2147. 'id': IQ_id,
  2148. 'to': 'dummy@localhost/desktop',
  2149. 'type': 'error'
  2150. }).c('error', {'type': 'cancel'})
  2151. .c('item-not-found', {'xmlns': "urn:ietf:params:xml:ns:xmpp-stanzas"});
  2152. _converse.connection._dataRecv(test_utils.createRequest(features_stanza));
  2153. const view = _converse.chatboxviews.get('coven@chat.shakespeare.lit');
  2154. /* <message xmlns="jabber:client"
  2155. * type="groupchat"
  2156. * to="dummy@localhost/_converse.js-27854181"
  2157. * from="coven@chat.shakespeare.lit">
  2158. * <x xmlns="http://jabber.org/protocol/muc#user">
  2159. * <status code="104"/>
  2160. * <status code="172"/>
  2161. * </x>
  2162. * </message>
  2163. */
  2164. const message = $msg({
  2165. type:'groupchat',
  2166. to: 'dummy@localhost/_converse.js-27854181',
  2167. from: 'coven@chat.shakespeare.lit'
  2168. }).c('x', {xmlns: Strophe.NS.MUC_USER})
  2169. .c('status', {code: '104'}).up()
  2170. .c('status', {code: '172'});
  2171. _converse.connection._dataRecv(test_utils.createRequest(message));
  2172. const chat_body = view.el.querySelector('.chatroom-body');
  2173. expect(sizzle('.message:last', chat_body).pop().textContent)
  2174. .toBe('This groupchat is now no longer anonymous');
  2175. done();
  2176. }));
  2177. it("informs users if they have been kicked out of the groupchat",
  2178. mock.initConverseWithPromises(
  2179. null, ['rosterGroupsFetched'], {},
  2180. async function (done, _converse) {
  2181. /* <presence
  2182. * from='harfleur@chat.shakespeare.lit/pistol'
  2183. * to='pistol@shakespeare.lit/harfleur'
  2184. * type='unavailable'>
  2185. * <x xmlns='http://jabber.org/protocol/muc#user'>
  2186. * <item affiliation='none' role='none'>
  2187. * <actor nick='Fluellen'/>
  2188. * <reason>Avaunt, you cullion!</reason>
  2189. * </item>
  2190. * <status code='110'/>
  2191. * <status code='307'/>
  2192. * </x>
  2193. * </presence>
  2194. */
  2195. await test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy');
  2196. var presence = $pres().attrs({
  2197. from:'lounge@localhost/dummy',
  2198. to:'dummy@localhost/pda',
  2199. type:'unavailable'
  2200. })
  2201. .c('x').attrs({xmlns:'http://jabber.org/protocol/muc#user'})
  2202. .c('item').attrs({
  2203. affiliation: 'none',
  2204. jid: 'dummy@localhost/pda',
  2205. role: 'none'
  2206. })
  2207. .c('actor').attrs({nick: 'Fluellen'}).up()
  2208. .c('reason').t('Avaunt, you cullion!').up()
  2209. .up()
  2210. .c('status').attrs({code:'110'}).up()
  2211. .c('status').attrs({code:'307'}).nodeTree;
  2212. _converse.connection._dataRecv(test_utils.createRequest(presence));
  2213. const view = _converse.chatboxviews.get('lounge@localhost');
  2214. expect(u.isVisible(view.el.querySelector('.chat-area'))).toBeFalsy();
  2215. expect(u.isVisible(view.el.querySelector('.occupants'))).toBeFalsy();
  2216. const chat_body = view.el.querySelector('.chatroom-body');
  2217. expect(chat_body.querySelectorAll('.disconnect-msg').length).toBe(3);
  2218. expect(chat_body.querySelector('.disconnect-msg:first-child').textContent).toBe(
  2219. 'You have been kicked from this groupchat');
  2220. expect(chat_body.querySelector('.disconnect-msg:nth-child(2)').textContent).toBe(
  2221. 'This action was done by Fluellen.');
  2222. expect(chat_body.querySelector('.disconnect-msg:nth-child(3)').textContent).toBe(
  2223. 'The reason given is: "Avaunt, you cullion!".');
  2224. done();
  2225. }));
  2226. it("can be saved to, and retrieved from, browserStorage",
  2227. mock.initConverseWithPromises(
  2228. null, ['rosterGroupsFetched', 'chatBoxesFetched'], {},
  2229. async function (done, _converse) {
  2230. await test_utils.openChatRoom(_converse, 'lounge', 'localhost', 'dummy');
  2231. // We instantiate a new ChatBoxes collection, which by default
  2232. // will be empty.
  2233. test_utils.openControlBox();
  2234. const newchatboxes = new _converse.ChatBoxes();
  2235. expect(newchatboxes.length).toEqual(0);
  2236. // The chatboxes will then be fetched from browserStorage inside the
  2237. // onConnected method
  2238. newchatboxes.onConnected();
  2239. expect(newchatboxes.length).toEqual(2);
  2240. // Check that the chatrooms retrieved from browserStorage
  2241. // have the same attributes values as the original ones.
  2242. const attrs = ['id', 'box_id', 'visible'];
  2243. let new_attrs, old_attrs;
  2244. for (var i=0; i<attrs.length; i++) {
  2245. new_attrs = _.map(_.map(newchatboxes.models, 'attributes'), attrs[i]);
  2246. old_attrs = _.map(_.map(_converse.chatboxes.models, 'attributes'), attrs[i]);
  2247. // FIXME: should have have to sort here? Order must
  2248. // probably be the same...
  2249. // This should be fixed once the controlbox always opens
  2250. // only on the right.
  2251. expect(_.isEqual(new_attrs.sort(), old_attrs.sort())).toEqual(true);
  2252. }
  2253. _converse.rosterview.render();
  2254. done();
  2255. }));
  2256. it("can be minimized by clicking a DOM element with class 'toggle-chatbox-button'",
  2257. mock.initConverseWithPromises(
  2258. null, ['rosterGroupsFetched', 'chatBoxesFetched'], {},
  2259. async function (done, _converse) {
  2260. await test_utils.openChatRoom(_converse, 'lounge', 'localhost', 'dummy');
  2261. const view = _converse.chatboxviews.get('lounge@localhost'),
  2262. trimmed_chatboxes = _converse.minimized_chats;
  2263. spyOn(view, 'minimize').and.callThrough();
  2264. spyOn(view, 'maximize').and.callThrough();
  2265. spyOn(_converse, 'emit');
  2266. view.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  2267. view.el.querySelector('.toggle-chatbox-button').click();
  2268. expect(view.minimize).toHaveBeenCalled();
  2269. expect(_converse.emit).toHaveBeenCalledWith('chatBoxMinimized', jasmine.any(Object));
  2270. expect(u.isVisible(view.el)).toBeFalsy();
  2271. expect(view.model.get('minimized')).toBeTruthy();
  2272. expect(view.minimize).toHaveBeenCalled();
  2273. await test_utils.waitUntil(() => trimmed_chatboxes.get(view.model.get('id')));
  2274. const trimmedview = trimmed_chatboxes.get(view.model.get('id'));
  2275. trimmedview.el.querySelector("a.restore-chat").click();
  2276. expect(view.maximize).toHaveBeenCalled();
  2277. expect(_converse.emit).toHaveBeenCalledWith('chatBoxMaximized', jasmine.any(Object));
  2278. expect(view.model.get('minimized')).toBeFalsy();
  2279. expect(_converse.emit.calls.count(), 3);
  2280. done();
  2281. }));
  2282. it("can be closed again by clicking a DOM element with class 'close-chatbox-button'",
  2283. mock.initConverseWithPromises(
  2284. null, ['rosterGroupsFetched', 'chatBoxesFetched'], {},
  2285. async function (done, _converse) {
  2286. await test_utils.openChatRoom(_converse, 'lounge', 'localhost', 'dummy');
  2287. const view = _converse.chatboxviews.get('lounge@localhost');
  2288. spyOn(view, 'close').and.callThrough();
  2289. spyOn(_converse, 'emit');
  2290. spyOn(view.model, 'leave');
  2291. view.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  2292. view.el.querySelector('.close-chatbox-button').click();
  2293. expect(view.close).toHaveBeenCalled();
  2294. expect(view.model.leave).toHaveBeenCalled();
  2295. expect(_converse.emit).toHaveBeenCalledWith('chatBoxClosed', jasmine.any(Object));
  2296. done();
  2297. }));
  2298. });
  2299. describe("Each chat groupchat can take special commands", function () {
  2300. it("takes /help to show the available commands",
  2301. mock.initConverseWithPromises(
  2302. null, ['rosterGroupsFetched'], {},
  2303. async function (done, _converse) {
  2304. await test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy');
  2305. const view = _converse.chatboxviews.get('lounge@localhost');
  2306. var textarea = view.el.querySelector('.chat-textarea');
  2307. textarea.value = '/help This is the groupchat subject';
  2308. view.keyPressed({
  2309. target: textarea,
  2310. preventDefault: _.noop,
  2311. keyCode: 13
  2312. });
  2313. const info_messages = Array.prototype.slice.call(view.el.querySelectorAll('.chat-info'), 0);
  2314. expect(info_messages.length).toBe(19);
  2315. expect(info_messages.pop().textContent).toBe('/voice: Allow muted user to post messages');
  2316. expect(info_messages.pop().textContent).toBe('/topic: Set groupchat subject (alias for /subject)');
  2317. expect(info_messages.pop().textContent).toBe('/subject: Set groupchat subject');
  2318. expect(info_messages.pop().textContent).toBe('/revoke: Revoke user\'s membership');
  2319. expect(info_messages.pop().textContent).toBe('/register: Register a nickname for this groupchat');
  2320. expect(info_messages.pop().textContent).toBe('/owner: Grant ownership of this groupchat');
  2321. expect(info_messages.pop().textContent).toBe('/op: Grant moderator role to user');
  2322. expect(info_messages.pop().textContent).toBe('/nick: Change your nickname');
  2323. expect(info_messages.pop().textContent).toBe('/mute: Remove user\'s ability to post messages');
  2324. expect(info_messages.pop().textContent).toBe('/member: Grant membership to a user');
  2325. expect(info_messages.pop().textContent).toBe('/me: Write in 3rd person');
  2326. expect(info_messages.pop().textContent).toBe('/kick: Kick user from groupchat');
  2327. expect(info_messages.pop().textContent).toBe('/help: Show this menu');
  2328. expect(info_messages.pop().textContent).toBe('/destroy: Destroy room');
  2329. expect(info_messages.pop().textContent).toBe('/deop: Change user role to participant');
  2330. expect(info_messages.pop().textContent).toBe('/clear: Remove messages');
  2331. expect(info_messages.pop().textContent).toBe('/ban: Ban user from groupchat');
  2332. expect(info_messages.pop().textContent).toBe('/admin: Change user\'s affiliation to admin');
  2333. done();
  2334. }));
  2335. it("takes /member to make an occupant a member",
  2336. mock.initConverseWithPromises(
  2337. null, ['rosterGroupsFetched'], {},
  2338. async function (done, _converse) {
  2339. let iq_stanza;
  2340. await test_utils.openAndEnterChatRoom(_converse, 'lounge', 'muc.localhost', 'dummy');
  2341. const view = _converse.chatboxviews.get('lounge@muc.localhost');
  2342. /* We don't show join/leave messages for existing occupants. We
  2343. * know about them because we receive their presences before we
  2344. * receive our own.
  2345. */
  2346. const presence = $pres({
  2347. to: 'dummy@localhost/resource',
  2348. from: 'lounge@muc.localhost/marc'
  2349. }).c('x', {xmlns: Strophe.NS.MUC_USER})
  2350. .c('item', {
  2351. 'affiliation': 'none',
  2352. 'jid': 'marc@localhost/_converse.js-290929789',
  2353. 'role': 'participant'
  2354. });
  2355. _converse.connection._dataRecv(test_utils.createRequest(presence));
  2356. expect(view.model.occupants.length).toBe(2);
  2357. const textarea = view.el.querySelector('.chat-textarea');
  2358. let sent_stanza;
  2359. spyOn(_converse.connection, 'send').and.callFake((stanza) => {
  2360. sent_stanza = stanza;
  2361. });
  2362. // First check that an error message appears when a
  2363. // non-existent nick is used.
  2364. textarea.value = '/member chris Welcome to the club!';
  2365. view.keyPressed({
  2366. target: textarea,
  2367. preventDefault: _.noop,
  2368. keyCode: 13
  2369. });
  2370. expect(_converse.connection.send).not.toHaveBeenCalled();
  2371. expect(view.el.querySelectorAll('.chat-error').length).toBe(1);
  2372. expect(view.el.querySelector('.chat-error').textContent.trim())
  2373. .toBe(`Error: couldn't find a groupchat participant "chris"`)
  2374. // Now test with an existing nick
  2375. textarea.value = '/member marc Welcome to the club!';
  2376. view.keyPressed({
  2377. target: textarea,
  2378. preventDefault: _.noop,
  2379. keyCode: 13
  2380. });
  2381. expect(_converse.connection.send).toHaveBeenCalled();
  2382. expect(Strophe.serialize(sent_stanza)).toBe(
  2383. `<iq id="${sent_stanza.getAttribute('id')}" to="lounge@muc.localhost" type="set" xmlns="jabber:client">`+
  2384. `<query xmlns="http://jabber.org/protocol/muc#admin">`+
  2385. `<item affiliation="member" jid="marc@localhost">`+
  2386. `<reason>Welcome to the club!</reason>`+
  2387. `</item>`+
  2388. `</query>`+
  2389. `</iq>`);
  2390. let result = $iq({
  2391. "xmlns": "jabber:client",
  2392. "type": "result",
  2393. "to": "dummy@localhost/resource",
  2394. "from": "lounge@muc.localhost",
  2395. "id": sent_stanza.getAttribute('id')
  2396. });
  2397. _converse.connection.IQ_stanzas = [];
  2398. _converse.connection._dataRecv(test_utils.createRequest(result));
  2399. let node = await test_utils.waitUntil(() => _.filter(
  2400. _converse.connection.IQ_stanzas,
  2401. iq => iq.nodeTree.querySelector('iq[to="lounge@muc.localhost"][type="get"] item[affiliation="member"]')).pop()
  2402. );
  2403. iq_stanza = node.nodeTree;
  2404. expect(node.toLocaleString()).toBe(
  2405. `<iq id="${iq_stanza.getAttribute('id')}" to="lounge@muc.localhost" type="get" xmlns="jabber:client">`+
  2406. `<query xmlns="http://jabber.org/protocol/muc#admin">`+
  2407. `<item affiliation="member"/>`+
  2408. `</query>`+
  2409. `</iq>`)
  2410. expect(view.model.occupants.length).toBe(2);
  2411. result = $iq({
  2412. "xmlns": "jabber:client",
  2413. "type": "result",
  2414. "to": "dummy@localhost/resource",
  2415. "from": "lounge@muc.localhost",
  2416. "id": iq_stanza.getAttribute("id")
  2417. }).c("query", {"xmlns": "http://jabber.org/protocol/muc#admin"})
  2418. .c("item", {"jid": "marc", "affiliation": "member"});
  2419. _converse.connection._dataRecv(test_utils.createRequest(result));
  2420. expect(view.model.occupants.length).toBe(2);
  2421. node = await test_utils.waitUntil(() => _.filter(
  2422. _converse.connection.IQ_stanzas,
  2423. (iq) => iq.nodeTree.querySelector('iq[to="lounge@muc.localhost"][type="get"] item[affiliation="owner"]')).pop()
  2424. );
  2425. iq_stanza = node.nodeTree;
  2426. expect(node.toLocaleString()).toBe(
  2427. `<iq id="${iq_stanza.getAttribute('id')}" to="lounge@muc.localhost" type="get" xmlns="jabber:client">`+
  2428. `<query xmlns="http://jabber.org/protocol/muc#admin">`+
  2429. `<item affiliation="owner"/>`+
  2430. `</query>`+
  2431. `</iq>`)
  2432. expect(view.model.occupants.length).toBe(2);
  2433. result = $iq({
  2434. "xmlns": "jabber:client",
  2435. "type": "result",
  2436. "to": "dummy@localhost/resource",
  2437. "from": "lounge@muc.localhost",
  2438. "id": iq_stanza.getAttribute("id")
  2439. }).c("query", {"xmlns": "http://jabber.org/protocol/muc#admin"})
  2440. .c("item", {"jid": "dummy@localhost", "affiliation": "owner"});
  2441. _converse.connection._dataRecv(test_utils.createRequest(result));
  2442. expect(view.model.occupants.length).toBe(2);
  2443. node = await test_utils.waitUntil(() => _.filter(
  2444. _converse.connection.IQ_stanzas,
  2445. (iq) => iq.nodeTree.querySelector('iq[to="lounge@muc.localhost"][type="get"] item[affiliation="admin"]')).pop()
  2446. );
  2447. iq_stanza = node.nodeTree;
  2448. expect(node.toLocaleString()).toBe(
  2449. `<iq id="${iq_stanza.getAttribute('id')}" to="lounge@muc.localhost" type="get" xmlns="jabber:client">`+
  2450. `<query xmlns="http://jabber.org/protocol/muc#admin">`+
  2451. `<item affiliation="admin"/>`+
  2452. `</query>`+
  2453. `</iq>`)
  2454. expect(view.model.occupants.length).toBe(2);
  2455. result = $iq({
  2456. "xmlns": "jabber:client",
  2457. "type": "result",
  2458. "to": "dummy@localhost/resource",
  2459. "from": "lounge@muc.localhost",
  2460. "id": iq_stanza.getAttribute("id")
  2461. }).c("query", {"xmlns": "http://jabber.org/protocol/muc#admin"})
  2462. _converse.connection._dataRecv(test_utils.createRequest(result));
  2463. await test_utils.waitUntil(() => view.el.querySelectorAll('.badge').length > 1);
  2464. expect(view.model.occupants.length).toBe(2);
  2465. expect(view.el.querySelectorAll('.occupant').length).toBe(2);
  2466. done();
  2467. }));
  2468. it("takes /topic to set the groupchat topic",
  2469. mock.initConverseWithPromises(
  2470. null, ['rosterGroupsFetched'], {},
  2471. async function (done, _converse) {
  2472. await test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy');
  2473. const view = _converse.chatboxviews.get('lounge@localhost');
  2474. spyOn(view, 'clearMessages');
  2475. let sent_stanza;
  2476. spyOn(_converse.connection, 'send').and.callFake(function (stanza) {
  2477. sent_stanza = stanza;
  2478. });
  2479. // Check the alias /topic
  2480. const textarea = view.el.querySelector('.chat-textarea');
  2481. textarea.value = '/topic This is the groupchat subject';
  2482. view.keyPressed({
  2483. target: textarea,
  2484. preventDefault: _.noop,
  2485. keyCode: 13
  2486. });
  2487. expect(_converse.connection.send).toHaveBeenCalled();
  2488. expect(sent_stanza.textContent).toBe('This is the groupchat subject');
  2489. // Check /subject
  2490. textarea.value = '/subject This is a new subject';
  2491. view.keyPressed({
  2492. target: textarea,
  2493. preventDefault: _.noop,
  2494. keyCode: 13
  2495. });
  2496. expect(sent_stanza.textContent).toBe('This is a new subject');
  2497. expect(Strophe.serialize(sent_stanza).toLocaleString()).toBe(
  2498. '<message from="dummy@localhost/resource" to="lounge@localhost" type="groupchat" xmlns="jabber:client">'+
  2499. '<subject xmlns="jabber:client">This is a new subject</subject>'+
  2500. '</message>');
  2501. // Check case insensitivity
  2502. textarea.value = '/Subject This is yet another subject';
  2503. view.keyPressed({
  2504. target: textarea,
  2505. preventDefault: _.noop,
  2506. keyCode: 13
  2507. });
  2508. expect(sent_stanza.textContent).toBe('This is yet another subject');
  2509. expect(Strophe.serialize(sent_stanza).toLocaleString()).toBe(
  2510. '<message from="dummy@localhost/resource" to="lounge@localhost" type="groupchat" xmlns="jabber:client">'+
  2511. '<subject xmlns="jabber:client">This is yet another subject</subject>'+
  2512. '</message>');
  2513. done();
  2514. }));
  2515. it("takes /clear to clear messages",
  2516. mock.initConverseWithPromises(
  2517. null, ['rosterGroupsFetched'], {},
  2518. async function (done, _converse) {
  2519. await test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy');
  2520. const view = _converse.chatboxviews.get('lounge@localhost');
  2521. spyOn(view, 'clearMessages');
  2522. const textarea = view.el.querySelector('.chat-textarea')
  2523. textarea.value = '/clear';
  2524. view.keyPressed({
  2525. target: textarea,
  2526. preventDefault: _.noop,
  2527. keyCode: 13
  2528. });
  2529. expect(view.clearMessages).toHaveBeenCalled();
  2530. done();
  2531. }));
  2532. it("takes /owner to make a user an owner",
  2533. mock.initConverseWithPromises(
  2534. null, ['rosterGroupsFetched'], {},
  2535. async function (done, _converse) {
  2536. let sent_IQ, IQ_id;
  2537. const sendIQ = _converse.connection.sendIQ;
  2538. spyOn(_converse.connection, 'sendIQ').and.callFake(function (iq, callback, errback) {
  2539. sent_IQ = iq;
  2540. IQ_id = sendIQ.bind(this)(iq, callback, errback);
  2541. });
  2542. await test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy');
  2543. const view = _converse.chatboxviews.get('lounge@localhost');
  2544. spyOn(view.model, 'setAffiliation').and.callThrough();
  2545. spyOn(view, 'showErrorMessage').and.callThrough();
  2546. spyOn(view, 'validateRoleChangeCommand').and.callThrough();
  2547. let presence = $pres({
  2548. 'from': 'lounge@localhost/annoyingGuy',
  2549. 'id':'27C55F89-1C6A-459A-9EB5-77690145D624',
  2550. 'to': 'dummy@localhost/desktop'
  2551. })
  2552. .c('x', { 'xmlns': 'http://jabber.org/protocol/muc#user'})
  2553. .c('item', {
  2554. 'jid': 'annoyingguy@localhost',
  2555. 'affiliation': 'member',
  2556. 'role': 'participant'
  2557. });
  2558. _converse.connection._dataRecv(test_utils.createRequest(presence));
  2559. var textarea = view.el.querySelector('.chat-textarea')
  2560. textarea.value = '/owner';
  2561. view.keyPressed({
  2562. target: textarea,
  2563. preventDefault: _.noop,
  2564. keyCode: 13
  2565. });
  2566. expect(view.validateRoleChangeCommand).toHaveBeenCalled();
  2567. expect(view.showErrorMessage).toHaveBeenCalledWith(
  2568. "Error: the \"owner\" command takes two arguments, the user's nickname and optionally a reason.");
  2569. expect(view.model.setAffiliation).not.toHaveBeenCalled();
  2570. // XXX: Calling onFormSubmitted directly, trying
  2571. // again via triggering Event doesn't work for some weird
  2572. // reason.
  2573. textarea.value = '/owner nobody You\'re responsible';
  2574. view.onFormSubmitted(new Event('submit'));
  2575. expect(view.showErrorMessage).toHaveBeenCalledWith(
  2576. 'Error: couldn\'t find a groupchat participant "nobody"');
  2577. expect(view.model.setAffiliation).not.toHaveBeenCalled();
  2578. // Call now with the correct of arguments.
  2579. // XXX: Calling onFormSubmitted directly, trying
  2580. // again via triggering Event doesn't work for some weird
  2581. // reason.
  2582. textarea.value = '/owner annoyingGuy You\'re responsible';
  2583. view.onFormSubmitted(new Event('submit'));
  2584. expect(view.validateRoleChangeCommand.calls.count()).toBe(3);
  2585. expect(view.model.setAffiliation).toHaveBeenCalled();
  2586. expect(view.showErrorMessage.calls.count()).toBe(2);
  2587. // Check that the member list now gets updated
  2588. expect(sent_IQ.toLocaleString()).toBe(
  2589. `<iq id="${IQ_id}" to="lounge@localhost" type="set" xmlns="jabber:client">`+
  2590. `<query xmlns="http://jabber.org/protocol/muc#admin">`+
  2591. `<item affiliation="owner" jid="annoyingGuy">`+
  2592. `<reason>You&apos;re responsible</reason>`+
  2593. `</item>`+
  2594. `</query>`+
  2595. `</iq>`);
  2596. presence = $pres({
  2597. 'from': 'lounge@localhost/annoyingGuy',
  2598. 'id':'27C55F89-1C6A-459A-9EB5-77690145D628',
  2599. 'to': 'dummy@localhost/desktop'
  2600. })
  2601. .c('x', { 'xmlns': 'http://jabber.org/protocol/muc#user'})
  2602. .c('item', {
  2603. 'jid': 'annoyingguy@localhost',
  2604. 'affiliation': 'owner',
  2605. 'role': 'participant'
  2606. });
  2607. _converse.connection._dataRecv(test_utils.createRequest(presence));
  2608. expect(view.el.querySelectorAll('.chat-info')[4].textContent).toBe("annoyingGuy is now an owner of this groupchat");
  2609. done();
  2610. }));
  2611. it("takes /ban to ban a user",
  2612. mock.initConverseWithPromises(
  2613. null, ['rosterGroupsFetched'], {},
  2614. async function (done, _converse) {
  2615. let sent_IQ, IQ_id;
  2616. const sendIQ = _converse.connection.sendIQ;
  2617. spyOn(_converse.connection, 'sendIQ').and.callFake(function (iq, callback, errback) {
  2618. sent_IQ = iq;
  2619. IQ_id = sendIQ.bind(this)(iq, callback, errback);
  2620. });
  2621. await test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy');
  2622. const view = _converse.chatboxviews.get('lounge@localhost');
  2623. spyOn(view.model, 'setAffiliation').and.callThrough();
  2624. spyOn(view, 'showErrorMessage').and.callThrough();
  2625. spyOn(view, 'validateRoleChangeCommand').and.callThrough();
  2626. let presence = $pres({
  2627. 'from': 'lounge@localhost/annoyingGuy',
  2628. 'id':'27C55F89-1C6A-459A-9EB5-77690145D624',
  2629. 'to': 'dummy@localhost/desktop'
  2630. })
  2631. .c('x', { 'xmlns': 'http://jabber.org/protocol/muc#user'})
  2632. .c('item', {
  2633. 'jid': 'annoyingguy@localhost',
  2634. 'affiliation': 'member',
  2635. 'role': 'participant'
  2636. });
  2637. _converse.connection._dataRecv(test_utils.createRequest(presence));
  2638. const textarea = view.el.querySelector('.chat-textarea')
  2639. textarea.value = '/ban';
  2640. view.keyPressed({
  2641. target: textarea,
  2642. preventDefault: _.noop,
  2643. keyCode: 13
  2644. });
  2645. expect(view.validateRoleChangeCommand).toHaveBeenCalled();
  2646. expect(view.showErrorMessage).toHaveBeenCalledWith(
  2647. "Error: the \"ban\" command takes two arguments, the user's nickname and optionally a reason.");
  2648. expect(view.model.setAffiliation).not.toHaveBeenCalled();
  2649. // Call now with the correct amount of arguments.
  2650. // XXX: Calling onFormSubmitted directly, trying
  2651. // again via triggering Event doesn't work for some weird
  2652. // reason.
  2653. textarea.value = '/ban annoyingGuy You\'re annoying';
  2654. view.onFormSubmitted(new Event('submit'));
  2655. expect(view.validateRoleChangeCommand.calls.count()).toBe(2);
  2656. expect(view.showErrorMessage.calls.count()).toBe(1);
  2657. expect(view.model.setAffiliation).toHaveBeenCalled();
  2658. // Check that the member list now gets updated
  2659. expect(sent_IQ.toLocaleString()).toBe(
  2660. `<iq id="${IQ_id}" to="lounge@localhost" type="set" xmlns="jabber:client">`+
  2661. `<query xmlns="http://jabber.org/protocol/muc#admin">`+
  2662. `<item affiliation="outcast" jid="annoyingGuy">`+
  2663. `<reason>You&apos;re annoying</reason>`+
  2664. `</item>`+
  2665. `</query>`+
  2666. `</iq>`);
  2667. presence = $pres({
  2668. 'from': 'lounge@localhost/annoyingGuy',
  2669. 'id':'27C55F89-1C6A-459A-9EB5-77690145D628',
  2670. 'to': 'dummy@localhost/desktop'
  2671. })
  2672. .c('x', { 'xmlns': 'http://jabber.org/protocol/muc#user'})
  2673. .c('item', {
  2674. 'jid': 'annoyingguy@localhost',
  2675. 'affiliation': 'outcast',
  2676. 'role': 'participant'
  2677. });
  2678. _converse.connection._dataRecv(test_utils.createRequest(presence));
  2679. expect(
  2680. view.el.querySelectorAll('.chat-info')[3].textContent).toBe(
  2681. "annoyingGuy has been banned from this groupchat");
  2682. done();
  2683. }));
  2684. it("takes /kick to kick a user",
  2685. mock.initConverseWithPromises(
  2686. null, ['rosterGroupsFetched'], {},
  2687. async function (done, _converse) {
  2688. let sent_IQ, IQ_id;
  2689. const sendIQ = _converse.connection.sendIQ;
  2690. spyOn(_converse.connection, 'sendIQ').and.callFake(function (iq, callback, errback) {
  2691. sent_IQ = iq;
  2692. IQ_id = sendIQ.bind(this)(iq, callback, errback);
  2693. });
  2694. await test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy');
  2695. const view = _converse.chatboxviews.get('lounge@localhost');
  2696. spyOn(view, 'modifyRole').and.callThrough();
  2697. spyOn(view, 'showErrorMessage').and.callThrough();
  2698. spyOn(view, 'validateRoleChangeCommand').and.callThrough();
  2699. let presence = $pres({
  2700. 'from': 'lounge@localhost/annoyingGuy',
  2701. 'id':'27C55F89-1C6A-459A-9EB5-77690145D624',
  2702. 'to': 'dummy@localhost/desktop'
  2703. })
  2704. .c('x', { 'xmlns': 'http://jabber.org/protocol/muc#user'})
  2705. .c('item', {
  2706. 'jid': 'annoyingguy@localhost',
  2707. 'affiliation': 'none',
  2708. 'role': 'participant'
  2709. });
  2710. _converse.connection._dataRecv(test_utils.createRequest(presence));
  2711. var textarea = view.el.querySelector('.chat-textarea')
  2712. textarea.value = '/kick';
  2713. view.keyPressed({
  2714. target: textarea,
  2715. preventDefault: _.noop,
  2716. keyCode: 13
  2717. });
  2718. expect(view.validateRoleChangeCommand).toHaveBeenCalled();
  2719. expect(view.showErrorMessage).toHaveBeenCalledWith(
  2720. "Error: the \"kick\" command takes two arguments, the user's nickname and optionally a reason.");
  2721. expect(view.modifyRole).not.toHaveBeenCalled();
  2722. // Call now with the correct amount of arguments.
  2723. // XXX: Calling onFormSubmitted directly, trying
  2724. // again via triggering Event doesn't work for some weird
  2725. // reason.
  2726. textarea.value = '/kick annoyingGuy You\'re annoying';
  2727. view.onFormSubmitted(new Event('submit'));
  2728. expect(view.validateRoleChangeCommand.calls.count()).toBe(2);
  2729. expect(view.showErrorMessage.calls.count()).toBe(1);
  2730. expect(view.modifyRole).toHaveBeenCalled();
  2731. expect(sent_IQ.toLocaleString()).toBe(
  2732. `<iq id="${IQ_id}" to="lounge@localhost" type="set" xmlns="jabber:client">`+
  2733. `<query xmlns="http://jabber.org/protocol/muc#admin">`+
  2734. `<item nick="annoyingGuy" role="none">`+
  2735. `<reason>You&apos;re annoying</reason>`+
  2736. `</item>`+
  2737. `</query>`+
  2738. `</iq>`);
  2739. /* <presence
  2740. * from='harfleur@chat.shakespeare.lit/pistol'
  2741. * to='gower@shakespeare.lit/cell'
  2742. * type='unavailable'>
  2743. * <x xmlns='http://jabber.org/protocol/muc#user'>
  2744. * <item affiliation='none' role='none'/>
  2745. * <status code='307'/>
  2746. * </x>
  2747. * </presence>
  2748. */
  2749. presence = $pres({
  2750. 'from': 'lounge@localhost/annoyingGuy',
  2751. 'to': 'dummy@localhost/desktop',
  2752. 'type': 'unavailable'
  2753. })
  2754. .c('x', { 'xmlns': 'http://jabber.org/protocol/muc#user'})
  2755. .c('item', {
  2756. 'affiliation': 'none',
  2757. 'role': 'none'
  2758. }).up()
  2759. .c('status', {'code': '307'});
  2760. _converse.connection._dataRecv(test_utils.createRequest(presence));
  2761. expect(view.el.querySelectorAll('.chat-info')[3].textContent).toBe("annoyingGuy has been kicked out");
  2762. expect(view.el.querySelectorAll('.chat-info').length).toBe(4);
  2763. done();
  2764. }));
  2765. it("takes /op and /deop to make a user a moderator or not",
  2766. mock.initConverseWithPromises(
  2767. null, ['rosterGroupsFetched'], {},
  2768. async function (done, _converse) {
  2769. await test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy');
  2770. var sent_IQ, IQ_id;
  2771. var sendIQ = _converse.connection.sendIQ;
  2772. spyOn(_converse.connection, 'sendIQ').and.callFake(function (iq, callback, errback) {
  2773. sent_IQ = iq;
  2774. IQ_id = sendIQ.bind(this)(iq, callback, errback);
  2775. });
  2776. var view = _converse.chatboxviews.get('lounge@localhost');
  2777. spyOn(view, 'modifyRole').and.callThrough();
  2778. spyOn(view, 'showErrorMessage').and.callThrough();
  2779. spyOn(view, 'showChatEvent').and.callThrough();
  2780. spyOn(view, 'validateRoleChangeCommand').and.callThrough();
  2781. // New user enters the groupchat
  2782. /* <presence
  2783. * from='coven@chat.shakespeare.lit/thirdwitch'
  2784. * id='27C55F89-1C6A-459A-9EB5-77690145D624'
  2785. * to='crone1@shakespeare.lit/desktop'>
  2786. * <x xmlns='http://jabber.org/protocol/muc#user'>
  2787. * <item affiliation='member' role='moderator'/>
  2788. * </x>
  2789. * </presence>
  2790. */
  2791. var presence = $pres({
  2792. 'from': 'lounge@localhost/trustworthyguy',
  2793. 'id':'27C55F89-1C6A-459A-9EB5-77690145D624',
  2794. 'to': 'dummy@localhost/desktop'
  2795. })
  2796. .c('x', { 'xmlns': 'http://jabber.org/protocol/muc#user'})
  2797. .c('item', {
  2798. 'jid': 'trustworthyguy@localhost',
  2799. 'affiliation': 'member',
  2800. 'role': 'participant'
  2801. });
  2802. _converse.connection._dataRecv(test_utils.createRequest(presence));
  2803. var info_msgs = Array.prototype.slice.call(view.el.querySelectorAll('.chat-info'), 0);
  2804. expect(info_msgs.pop().textContent).toBe("trustworthyguy has entered the groupchat");
  2805. var textarea = view.el.querySelector('.chat-textarea')
  2806. textarea.value = '/op';
  2807. view.keyPressed({
  2808. target: textarea,
  2809. preventDefault: _.noop,
  2810. keyCode: 13
  2811. });
  2812. expect(view.validateRoleChangeCommand).toHaveBeenCalled();
  2813. expect(view.showErrorMessage).toHaveBeenCalledWith(
  2814. "Error: the \"op\" command takes two arguments, the user's nickname and optionally a reason.");
  2815. expect(view.modifyRole).not.toHaveBeenCalled();
  2816. // Call now with the correct amount of arguments.
  2817. // XXX: Calling onFormSubmitted directly, trying
  2818. // again via triggering Event doesn't work for some weird
  2819. // reason.
  2820. textarea.value = '/op trustworthyguy You\'re trustworthy';
  2821. view.onFormSubmitted(new Event('submit'));
  2822. expect(view.validateRoleChangeCommand.calls.count()).toBe(2);
  2823. expect(view.showErrorMessage.calls.count()).toBe(1);
  2824. expect(view.modifyRole).toHaveBeenCalled();
  2825. expect(sent_IQ.toLocaleString()).toBe(
  2826. `<iq id="${IQ_id}" to="lounge@localhost" type="set" xmlns="jabber:client">`+
  2827. `<query xmlns="http://jabber.org/protocol/muc#admin">`+
  2828. `<item nick="trustworthyguy" role="moderator">`+
  2829. `<reason>You&apos;re trustworthy</reason>`+
  2830. `</item>`+
  2831. `</query>`+
  2832. `</iq>`);
  2833. /* <presence
  2834. * from='coven@chat.shakespeare.lit/thirdwitch'
  2835. * to='crone1@shakespeare.lit/desktop'>
  2836. * <x xmlns='http://jabber.org/protocol/muc#user'>
  2837. * <item affiliation='member'
  2838. * jid='hag66@shakespeare.lit/pda'
  2839. * role='moderator'/>
  2840. * </x>
  2841. * </presence>
  2842. */
  2843. presence = $pres({
  2844. 'from': 'lounge@localhost/trustworthyguy',
  2845. 'to': 'dummy@localhost/desktop'
  2846. })
  2847. .c('x', { 'xmlns': 'http://jabber.org/protocol/muc#user'})
  2848. .c('item', {
  2849. 'jid': 'trustworthyguy@localhost',
  2850. 'affiliation': 'member',
  2851. 'role': 'moderator'
  2852. });
  2853. _converse.connection._dataRecv(test_utils.createRequest(presence));
  2854. info_msgs = Array.prototype.slice.call(view.el.querySelectorAll('.chat-info'), 0);
  2855. expect(info_msgs.pop().textContent).toBe("trustworthyguy is now a moderator");
  2856. // Call now with the correct amount of arguments.
  2857. // XXX: Calling onFormSubmitted directly, trying
  2858. // again via triggering Event doesn't work for some weird
  2859. // reason.
  2860. textarea.value = '/deop trustworthyguy Perhaps not';
  2861. view.onFormSubmitted(new Event('submit'));
  2862. expect(view.validateRoleChangeCommand.calls.count()).toBe(3);
  2863. expect(view.showChatEvent.calls.count()).toBe(1);
  2864. expect(view.modifyRole).toHaveBeenCalled();
  2865. expect(sent_IQ.toLocaleString()).toBe(
  2866. `<iq id="${IQ_id}" to="lounge@localhost" type="set" xmlns="jabber:client">`+
  2867. `<query xmlns="http://jabber.org/protocol/muc#admin">`+
  2868. `<item nick="trustworthyguy" role="participant">`+
  2869. `<reason>Perhaps not</reason>`+
  2870. `</item>`+
  2871. `</query>`+
  2872. `</iq>`);
  2873. /* <presence
  2874. * from='coven@chat.shakespeare.lit/thirdwitch'
  2875. * to='crone1@shakespeare.lit/desktop'>
  2876. * <x xmlns='http://jabber.org/protocol/muc#user'>
  2877. * <item affiliation='member'
  2878. * jid='hag66@shakespeare.lit/pda'
  2879. * role='participant'/>
  2880. * </x>
  2881. * </presence>
  2882. */
  2883. presence = $pres({
  2884. 'from': 'lounge@localhost/trustworthyguy',
  2885. 'to': 'dummy@localhost/desktop'
  2886. }).c('x', { 'xmlns': 'http://jabber.org/protocol/muc#user'})
  2887. .c('item', {
  2888. 'jid': 'trustworthyguy@localhost',
  2889. 'affiliation': 'member',
  2890. 'role': 'participant'
  2891. });
  2892. _converse.connection._dataRecv(test_utils.createRequest(presence));
  2893. info_msgs = Array.prototype.slice.call(view.el.querySelectorAll('.chat-info'), 0);
  2894. expect(info_msgs.pop().textContent).toBe("trustworthyguy is no longer a moderator");
  2895. done();
  2896. }));
  2897. it("takes /mute and /voice to mute and unmute a user",
  2898. mock.initConverseWithPromises(
  2899. null, ['rosterGroupsFetched'], {},
  2900. async function (done, _converse) {
  2901. await test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy');
  2902. var sent_IQ, IQ_id;
  2903. var sendIQ = _converse.connection.sendIQ;
  2904. spyOn(_converse.connection, 'sendIQ').and.callFake(function (iq, callback, errback) {
  2905. sent_IQ = iq;
  2906. IQ_id = sendIQ.bind(this)(iq, callback, errback);
  2907. });
  2908. var view = _converse.chatboxviews.get('lounge@localhost');
  2909. spyOn(view, 'modifyRole').and.callThrough();
  2910. spyOn(view, 'showErrorMessage').and.callThrough();
  2911. spyOn(view, 'showChatEvent').and.callThrough();
  2912. spyOn(view, 'validateRoleChangeCommand').and.callThrough();
  2913. // New user enters the groupchat
  2914. /* <presence
  2915. * from='coven@chat.shakespeare.lit/thirdwitch'
  2916. * id='27C55F89-1C6A-459A-9EB5-77690145D624'
  2917. * to='crone1@shakespeare.lit/desktop'>
  2918. * <x xmlns='http://jabber.org/protocol/muc#user'>
  2919. * <item affiliation='member' role='participant'/>
  2920. * </x>
  2921. * </presence>
  2922. */
  2923. var presence = $pres({
  2924. 'from': 'lounge@localhost/annoyingGuy',
  2925. 'id':'27C55F89-1C6A-459A-9EB5-77690145D624',
  2926. 'to': 'dummy@localhost/desktop'
  2927. })
  2928. .c('x', { 'xmlns': 'http://jabber.org/protocol/muc#user'})
  2929. .c('item', {
  2930. 'jid': 'annoyingguy@localhost',
  2931. 'affiliation': 'member',
  2932. 'role': 'participant'
  2933. });
  2934. _converse.connection._dataRecv(test_utils.createRequest(presence));
  2935. var info_msgs = Array.prototype.slice.call(view.el.querySelectorAll('.chat-info'), 0);
  2936. expect(info_msgs.pop().textContent).toBe("annoyingGuy has entered the groupchat");
  2937. const textarea = view.el.querySelector('.chat-textarea')
  2938. textarea.value = '/mute';
  2939. view.keyPressed({
  2940. target: textarea,
  2941. preventDefault: _.noop,
  2942. keyCode: 13
  2943. });
  2944. expect(view.validateRoleChangeCommand).toHaveBeenCalled();
  2945. expect(view.showErrorMessage).toHaveBeenCalledWith(
  2946. "Error: the \"mute\" command takes two arguments, the user's nickname and optionally a reason.");
  2947. expect(view.modifyRole).not.toHaveBeenCalled();
  2948. // Call now with the correct amount of arguments.
  2949. // XXX: Calling onFormSubmitted directly, trying
  2950. // again via triggering Event doesn't work for some weird
  2951. // reason.
  2952. textarea.value = '/mute annoyingGuy You\'re annoying';
  2953. view.onFormSubmitted(new Event('submit'));
  2954. expect(view.validateRoleChangeCommand.calls.count()).toBe(2);
  2955. expect(view.showErrorMessage.calls.count()).toBe(1);
  2956. expect(view.modifyRole).toHaveBeenCalled();
  2957. expect(sent_IQ.toLocaleString()).toBe(
  2958. `<iq id="${IQ_id}" to="lounge@localhost" type="set" xmlns="jabber:client">`+
  2959. `<query xmlns="http://jabber.org/protocol/muc#admin">`+
  2960. `<item nick="annoyingGuy" role="visitor">`+
  2961. `<reason>You&apos;re annoying</reason>`+
  2962. `</item>`+
  2963. `</query>`+
  2964. `</iq>`);
  2965. /* <presence
  2966. * from='coven@chat.shakespeare.lit/thirdwitch'
  2967. * to='crone1@shakespeare.lit/desktop'>
  2968. * <x xmlns='http://jabber.org/protocol/muc#user'>
  2969. * <item affiliation='member'
  2970. * jid='hag66@shakespeare.lit/pda'
  2971. * role='visitor'/>
  2972. * </x>
  2973. * </presence>
  2974. */
  2975. presence = $pres({
  2976. 'from': 'lounge@localhost/annoyingGuy',
  2977. 'to': 'dummy@localhost/desktop'
  2978. })
  2979. .c('x', { 'xmlns': 'http://jabber.org/protocol/muc#user'})
  2980. .c('item', {
  2981. 'jid': 'annoyingguy@localhost',
  2982. 'affiliation': 'member',
  2983. 'role': 'visitor'
  2984. });
  2985. _converse.connection._dataRecv(test_utils.createRequest(presence));
  2986. info_msgs = Array.prototype.slice.call(view.el.querySelectorAll('.chat-info'), 0);
  2987. expect(info_msgs.pop().textContent).toBe("annoyingGuy has been muted");
  2988. // Call now with the correct of arguments.
  2989. // XXX: Calling onFormSubmitted directly, trying
  2990. // again via triggering Event doesn't work for some weird
  2991. // reason.
  2992. textarea.value = '/voice annoyingGuy Now you can talk again';
  2993. view.onFormSubmitted(new Event('submit'));
  2994. expect(view.validateRoleChangeCommand.calls.count()).toBe(3);
  2995. expect(view.showChatEvent.calls.count()).toBe(1);
  2996. expect(view.modifyRole).toHaveBeenCalled();
  2997. expect(sent_IQ.toLocaleString()).toBe(
  2998. `<iq id="${IQ_id}" to="lounge@localhost" type="set" xmlns="jabber:client">`+
  2999. `<query xmlns="http://jabber.org/protocol/muc#admin">`+
  3000. `<item nick="annoyingGuy" role="participant">`+
  3001. `<reason>Now you can talk again</reason>`+
  3002. `</item>`+
  3003. `</query>`+
  3004. `</iq>`);
  3005. /* <presence
  3006. * from='coven@chat.shakespeare.lit/thirdwitch'
  3007. * to='crone1@shakespeare.lit/desktop'>
  3008. * <x xmlns='http://jabber.org/protocol/muc#user'>
  3009. * <item affiliation='member'
  3010. * jid='hag66@shakespeare.lit/pda'
  3011. * role='visitor'/>
  3012. * </x>
  3013. * </presence>
  3014. */
  3015. presence = $pres({
  3016. 'from': 'lounge@localhost/annoyingGuy',
  3017. 'to': 'dummy@localhost/desktop'
  3018. })
  3019. .c('x', { 'xmlns': 'http://jabber.org/protocol/muc#user'})
  3020. .c('item', {
  3021. 'jid': 'annoyingguy@localhost',
  3022. 'affiliation': 'member',
  3023. 'role': 'participant'
  3024. });
  3025. _converse.connection._dataRecv(test_utils.createRequest(presence));
  3026. info_msgs = Array.prototype.slice.call(view.el.querySelectorAll('.chat-info'), 0);
  3027. expect(info_msgs.pop().textContent).toBe("annoyingGuy has been given a voice again");
  3028. done();
  3029. }));
  3030. it("takes /destroy to destroy a muc",
  3031. mock.initConverseWithPromises(
  3032. null, ['rosterGroupsFetched'], {},
  3033. async function (done, _converse) {
  3034. await test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy');
  3035. let sent_IQ, IQ_id;
  3036. const sendIQ = _converse.connection.sendIQ;
  3037. spyOn(_converse.connection, 'sendIQ').and.callFake(function (iq, callback, errback) {
  3038. sent_IQ = iq;
  3039. IQ_id = sendIQ.bind(this)(iq, callback, errback);
  3040. });
  3041. const view = _converse.chatboxviews.get('lounge@localhost');
  3042. const textarea = view.el.querySelector('.chat-textarea');
  3043. textarea.value = '/destroy bored';
  3044. view.onFormSubmitted(new Event('submit'));
  3045. expect(sent_IQ.toLocaleString()).toBe(
  3046. `<iq id="${IQ_id}" to="lounge@localhost" type="set" xmlns="jabber:client">`+
  3047. `<query xmlns="http://jabber.org/protocol/muc#owner">`+
  3048. `<destroy>`+
  3049. `<reason>`+
  3050. `bored`+
  3051. `</reason>`+
  3052. `</destroy>`+
  3053. `</query>`+
  3054. `</iq>`);
  3055. /* <iq from="lounge@localhost"
  3056. * id="${IQ_id}"
  3057. * to="dummy@localhost/resource"
  3058. * type="result"
  3059. * xmlns="jabber:client"/>
  3060. */
  3061. const result_stanza = $iq({
  3062. 'type': 'result',
  3063. 'id': IQ_id,
  3064. 'from': view.model.get('jid'),
  3065. 'to': _converse.connection.jid
  3066. });
  3067. spyOn(view, 'close').and.callThrough();
  3068. spyOn(_converse, 'emit');
  3069. _converse.connection._dataRecv(test_utils.createRequest(result_stanza));
  3070. await test_utils.waitUntil(() => (view.model.get('connection_status') === converse.ROOMSTATUS.DISCONNECTED));
  3071. expect(view.close).toHaveBeenCalled();
  3072. expect(_converse.emit).toHaveBeenCalledWith('chatBoxClosed', jasmine.any(Object));
  3073. done();
  3074. }));
  3075. });
  3076. describe("When attempting to enter a groupchat", function () {
  3077. it("will use the nickname set in the global settings if the user doesn't have a VCard nickname",
  3078. mock.initConverseWithPromises(
  3079. null, ['rosterGroupsFetched', 'chatBoxesFetched'], {'nickname': 'Benedict-Cucumberpatch'},
  3080. async function (done, _converse) {
  3081. await test_utils.openChatRoomViaModal(_converse, 'roomy@muc.localhost');
  3082. const view = _converse.chatboxviews.get('roomy@muc.localhost');
  3083. expect(view.model.get('nick')).toBe('Benedict-Cucumberpatch');
  3084. done();
  3085. }));
  3086. it("will show an error message if the groupchat requires a password",
  3087. mock.initConverseWithPromises(
  3088. null, ['rosterGroupsFetched', 'chatBoxesFetched'], {},
  3089. async function (done, _converse) {
  3090. const groupchat_jid = 'protected';
  3091. await test_utils.openChatRoomViaModal(_converse, groupchat_jid, 'dummy');
  3092. const view = _converse.chatboxviews.get(groupchat_jid);
  3093. spyOn(view, 'renderPasswordForm').and.callThrough();
  3094. var presence = $pres().attrs({
  3095. 'from': `${groupchat_jid}/dummy`,
  3096. 'id': u.getUniqueId(),
  3097. 'to': 'dummy@localhost/pda',
  3098. 'type': 'error'
  3099. }).c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
  3100. .c('error').attrs({by:'lounge@localhost', type:'auth'})
  3101. .c('not-authorized').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'});
  3102. _converse.connection._dataRecv(test_utils.createRequest(presence));
  3103. const chat_body = view.el.querySelector('.chatroom-body');
  3104. expect(view.renderPasswordForm).toHaveBeenCalled();
  3105. expect(chat_body.querySelectorAll('form.chatroom-form').length).toBe(1);
  3106. expect(chat_body.querySelector('legend').textContent)
  3107. .toBe('This groupchat requires a password');
  3108. // Let's submit the form
  3109. spyOn(view, 'join');
  3110. const input_el = view.el.querySelector('[name="password"]');
  3111. input_el.value = 'secret';
  3112. view.el.querySelector('input[type=submit]').click();
  3113. expect(view.join).toHaveBeenCalledWith('dummy', 'secret');
  3114. done();
  3115. }));
  3116. it("will show an error message if the groupchat is members-only and the user not included",
  3117. mock.initConverseWithPromises(
  3118. null, ['rosterGroupsFetched'], {},
  3119. async function (done, _converse) {
  3120. const groupchat_jid = 'members-only@muc.localhost'
  3121. await test_utils.openChatRoomViaModal(_converse, groupchat_jid, 'dummy');
  3122. const presence = $pres().attrs({
  3123. from: `${groupchat_jid}/dummy`,
  3124. id: u.getUniqueId(),
  3125. to: 'dummy@localhost/pda',
  3126. type: 'error'
  3127. }).c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
  3128. .c('error').attrs({by:'lounge@localhost', type:'auth'})
  3129. .c('registration-required').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
  3130. const view = _converse.chatboxviews.get(groupchat_jid);
  3131. spyOn(view, 'showErrorMessage').and.callThrough();
  3132. _converse.connection._dataRecv(test_utils.createRequest(presence));
  3133. expect(view.el.querySelector('.chatroom-body .disconnect-container .disconnect-msg:last-child').textContent)
  3134. .toBe('You are not on the member list of this groupchat.');
  3135. done();
  3136. }));
  3137. it("will show an error message if the user has been banned",
  3138. mock.initConverseWithPromises(
  3139. null, ['rosterGroupsFetched'], {},
  3140. async function (done, _converse) {
  3141. const groupchat_jid = 'off-limits@muc.localhost'
  3142. await test_utils.openChatRoomViaModal(_converse, groupchat_jid, 'dummy');
  3143. const presence = $pres().attrs({
  3144. from: `${groupchat_jid}/dummy`,
  3145. id: u.getUniqueId(),
  3146. to: 'dummy@localhost/pda',
  3147. type: 'error'
  3148. }).c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
  3149. .c('error').attrs({by:'lounge@localhost', type:'auth'})
  3150. .c('forbidden').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
  3151. const view = _converse.chatboxviews.get(groupchat_jid);
  3152. spyOn(view, 'showErrorMessage').and.callThrough();
  3153. _converse.connection._dataRecv(test_utils.createRequest(presence));
  3154. expect(view.el.querySelector('.chatroom-body .disconnect-container .disconnect-msg:last-child').textContent)
  3155. .toBe('You have been banned from this groupchat.');
  3156. done();
  3157. }));
  3158. it("will render a nickname form if a nickname conflict happens and muc_nickname_from_jid=false",
  3159. mock.initConverseWithPromises(
  3160. null, ['rosterGroupsFetched'], {},
  3161. async function (done, _converse) {
  3162. const groupchat_jid = 'conflicted@muc.localhost';
  3163. await test_utils.openChatRoomViaModal(_converse, groupchat_jid, 'dummy');
  3164. var presence = $pres().attrs({
  3165. from: `${groupchat_jid}/dummy`,
  3166. id: u.getUniqueId(),
  3167. to: 'dummy@localhost/pda',
  3168. type: 'error'
  3169. }).c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
  3170. .c('error').attrs({by:'lounge@localhost', type:'cancel'})
  3171. .c('conflict').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
  3172. const view = _converse.chatboxviews.get(groupchat_jid);
  3173. spyOn(view, 'showErrorMessage').and.callThrough();
  3174. _converse.connection._dataRecv(test_utils.createRequest(presence));
  3175. expect(sizzle('.chatroom-body form.chatroom-form label:first', view.el).pop().textContent)
  3176. .toBe('Please choose your nickname');
  3177. const input = sizzle('.chatroom-body form.chatroom-form input:first', view.el).pop();
  3178. input.value = 'nicky';
  3179. view.el.querySelector('input[type=submit]').click();
  3180. done();
  3181. }));
  3182. it("will automatically choose a new nickname if a nickname conflict happens and muc_nickname_from_jid=true",
  3183. mock.initConverseWithPromises(
  3184. null, ['rosterGroupsFetched'], {},
  3185. async function (done, _converse) {
  3186. const groupchat_jid = 'conflicting@muc.localhost'
  3187. await test_utils.openChatRoomViaModal(_converse, groupchat_jid, 'dummy');
  3188. /* <presence
  3189. * from='coven@chat.shakespeare.lit/thirdwitch'
  3190. * id='n13mt3l'
  3191. * to='hag66@shakespeare.lit/pda'
  3192. * type='error'>
  3193. * <x xmlns='http://jabber.org/protocol/muc'/>
  3194. * <error by='coven@chat.shakespeare.lit' type='cancel'>
  3195. * <conflict xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/>
  3196. * </error>
  3197. * </presence>
  3198. */
  3199. _converse.muc_nickname_from_jid = true;
  3200. const attrs = {
  3201. 'from': `${groupchat_jid}/dummy`,
  3202. 'id': u.getUniqueId(),
  3203. 'to': 'dummy@localhost/pda',
  3204. 'type': 'error'
  3205. };
  3206. let presence = $pres().attrs(attrs)
  3207. .c('x').attrs({'xmlns':'http://jabber.org/protocol/muc'}).up()
  3208. .c('error').attrs({'by': groupchat_jid, 'type':'cancel'})
  3209. .c('conflict').attrs({'xmlns':'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
  3210. const view = _converse.chatboxviews.get(groupchat_jid);
  3211. spyOn(view, 'showErrorMessage').and.callThrough();
  3212. spyOn(view, 'join').and.callThrough();
  3213. // Simulate repeatedly that there's already someone in the groupchat
  3214. // with that nickname
  3215. _converse.connection._dataRecv(test_utils.createRequest(presence));
  3216. expect(view.join).toHaveBeenCalledWith('dummy-2');
  3217. attrs.from = `${groupchat_jid}/dummy-2`;
  3218. attrs.id = u.getUniqueId();
  3219. presence = $pres().attrs(attrs)
  3220. .c('x').attrs({'xmlns':'http://jabber.org/protocol/muc'}).up()
  3221. .c('error').attrs({'by': groupchat_jid, type:'cancel'})
  3222. .c('conflict').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
  3223. _converse.connection._dataRecv(test_utils.createRequest(presence));
  3224. expect(view.join).toHaveBeenCalledWith('dummy-3');
  3225. attrs.from = `${groupchat_jid}/dummy-3`;
  3226. attrs.id = new Date().getTime();
  3227. presence = $pres().attrs(attrs)
  3228. .c('x').attrs({'xmlns': 'http://jabber.org/protocol/muc'}).up()
  3229. .c('error').attrs({'by': groupchat_jid, 'type': 'cancel'})
  3230. .c('conflict').attrs({'xmlns':'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
  3231. _converse.connection._dataRecv(test_utils.createRequest(presence));
  3232. expect(view.join).toHaveBeenCalledWith('dummy-4');
  3233. done();
  3234. }));
  3235. it("will show an error message if the user is not allowed to have created the groupchat",
  3236. mock.initConverseWithPromises(
  3237. null, ['rosterGroupsFetched'], {},
  3238. async function (done, _converse) {
  3239. const groupchat_jid = 'impermissable@muc.localhost'
  3240. await test_utils.openChatRoomViaModal(_converse, groupchat_jid, 'dummy')
  3241. var presence = $pres().attrs({
  3242. from: `${groupchat_jid}/dummy`,
  3243. id: u.getUniqueId(),
  3244. to:'dummy@localhost/pda',
  3245. type:'error'
  3246. }).c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
  3247. .c('error').attrs({by:'lounge@localhost', type:'cancel'})
  3248. .c('not-allowed').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
  3249. const view = _converse.chatboxviews.get(groupchat_jid);
  3250. spyOn(view, 'showErrorMessage').and.callThrough();
  3251. _converse.connection._dataRecv(test_utils.createRequest(presence));
  3252. expect(view.el.querySelector('.chatroom-body .disconnect-container .disconnect-msg:last-child').textContent)
  3253. .toBe('You are not allowed to create new groupchats.');
  3254. done();
  3255. }));
  3256. it("will show an error message if the user's nickname doesn't conform to groupchat policy",
  3257. mock.initConverseWithPromises(
  3258. null, ['rosterGroupsFetched'], {},
  3259. async function (done, _converse) {
  3260. const groupchat_jid = 'conformist@muc.localhost'
  3261. await test_utils.openChatRoomViaModal(_converse, groupchat_jid, 'dummy');
  3262. const presence = $pres().attrs({
  3263. from: `${groupchat_jid}/dummy`,
  3264. id: u.getUniqueId(),
  3265. to:'dummy@localhost/pda',
  3266. type:'error'
  3267. }).c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
  3268. .c('error').attrs({by:'lounge@localhost', type:'cancel'})
  3269. .c('not-acceptable').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
  3270. const view = _converse.chatboxviews.get(groupchat_jid);
  3271. spyOn(view, 'showErrorMessage').and.callThrough();
  3272. _converse.connection._dataRecv(test_utils.createRequest(presence));
  3273. expect(view.el.querySelector('.chatroom-body .disconnect-container .disconnect-msg:last-child').textContent)
  3274. .toBe("Your nickname doesn't conform to this groupchat's policies.");
  3275. done();
  3276. }));
  3277. it("will show an error message if the groupchat doesn't yet exist",
  3278. mock.initConverseWithPromises(
  3279. null, ['rosterGroupsFetched'], {},
  3280. async function (done, _converse) {
  3281. const groupchat_jid = 'nonexistent@muc.localhost'
  3282. await test_utils.openChatRoomViaModal(_converse, groupchat_jid, 'dummy');
  3283. const presence = $pres().attrs({
  3284. from: `${groupchat_jid}/dummy`,
  3285. id: u.getUniqueId(),
  3286. to: 'dummy@localhost/pda',
  3287. type:'error'
  3288. }).c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
  3289. .c('error').attrs({by:'lounge@localhost', type:'cancel'})
  3290. .c('item-not-found').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
  3291. const view = _converse.chatboxviews.get(groupchat_jid);
  3292. spyOn(view, 'showErrorMessage').and.callThrough();
  3293. _converse.connection._dataRecv(test_utils.createRequest(presence));
  3294. expect(view.el.querySelector('.chatroom-body .disconnect-container .disconnect-msg:last-child').textContent)
  3295. .toBe("This groupchat does not (yet) exist.");
  3296. done();
  3297. }));
  3298. it("will show an error message if the groupchat has reached its maximum number of participants",
  3299. mock.initConverseWithPromises(
  3300. null, ['rosterGroupsFetched'], {},
  3301. async function (done, _converse) {
  3302. const groupchat_jid = 'maxed-out@muc.localhost'
  3303. await test_utils.openChatRoomViaModal(_converse, groupchat_jid, 'dummy')
  3304. const presence = $pres().attrs({
  3305. from: `${groupchat_jid}/dummy`,
  3306. id: u.getUniqueId(),
  3307. to:'dummy@localhost/pda',
  3308. type:'error'
  3309. }).c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
  3310. .c('error').attrs({by:'lounge@localhost', type:'cancel'})
  3311. .c('service-unavailable').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
  3312. const view = _converse.chatboxviews.get(groupchat_jid);
  3313. spyOn(view, 'showErrorMessage').and.callThrough();
  3314. _converse.connection._dataRecv(test_utils.createRequest(presence));
  3315. expect(view.el.querySelector('.chatroom-body .disconnect-container .disconnect-msg:last-child').textContent)
  3316. .toBe("This groupchat has reached its maximum number of participants.");
  3317. done();
  3318. }));
  3319. });
  3320. describe("Someone being invited to a groupchat", function () {
  3321. it("will first be added to the member list if the groupchat is members only",
  3322. mock.initConverseWithPromises(
  3323. null, ['rosterGroupsFetched', 'chatBoxesFetched'], {},
  3324. async function (done, _converse) {
  3325. var sent_IQs = [], IQ_ids = [];
  3326. const sendIQ = _converse.connection.sendIQ;
  3327. const IQ_stanzas = _converse.connection.IQ_stanzas;
  3328. const room_jid = 'coven@chat.shakespeare.lit';
  3329. spyOn(_converse.connection, 'sendIQ').and.callFake(function (iq, callback, errback) {
  3330. sent_IQs.push(iq);
  3331. IQ_ids.push(sendIQ.bind(this)(iq, callback, errback));
  3332. });
  3333. await _converse.api.rooms.open(room_jid, {'nick': 'dummy'});
  3334. const node = await test_utils.waitUntil(() => _.filter(
  3335. IQ_stanzas,
  3336. iq => iq.nodeTree.querySelector(
  3337. `iq[to="${room_jid}"] query[xmlns="http://jabber.org/protocol/disco#info"]`
  3338. )).pop());
  3339. // Check that the groupchat queried for the feautures.
  3340. let stanza = node.nodeTree;
  3341. expect(node.toLocaleString()).toBe(
  3342. `<iq from="dummy@localhost/resource" id="${stanza.getAttribute("id")}" to="${room_jid}" type="get" xmlns="jabber:client">`+
  3343. `<query xmlns="http://jabber.org/protocol/disco#info"/>`+
  3344. `</iq>`);
  3345. const view = _converse.chatboxviews.get('coven@chat.shakespeare.lit');
  3346. // State that the chat is members-only via the features IQ
  3347. var features_stanza = $iq({
  3348. from: 'coven@chat.shakespeare.lit',
  3349. 'id': IQ_ids.pop(),
  3350. 'to': 'dummy@localhost/desktop',
  3351. 'type': 'result'
  3352. })
  3353. .c('query', { 'xmlns': 'http://jabber.org/protocol/disco#info'})
  3354. .c('identity', {
  3355. 'category': 'conference',
  3356. 'name': 'A Dark Cave',
  3357. 'type': 'text'
  3358. }).up()
  3359. .c('feature', {'var': 'http://jabber.org/protocol/muc'}).up()
  3360. .c('feature', {'var': 'muc_hidden'}).up()
  3361. .c('feature', {'var': 'muc_temporary'}).up()
  3362. .c('feature', {'var': 'muc_membersonly'}).up();
  3363. _converse.connection._dataRecv(test_utils.createRequest(features_stanza));
  3364. await test_utils.waitUntil(() => (view.model.get('connection_status') === converse.ROOMSTATUS.CONNECTING));
  3365. expect(view.model.features.get('membersonly')).toBeTruthy();
  3366. test_utils.createContacts(_converse, 'current');
  3367. let sent_stanza, sent_id;
  3368. spyOn(_converse.connection, 'send').and.callFake(function (stanza) {
  3369. if (stanza.nodeTree && stanza.nodeTree.nodeName === 'message') {
  3370. sent_id = stanza.nodeTree.getAttribute('id');
  3371. sent_stanza = stanza;
  3372. }
  3373. });
  3374. var name = mock.cur_names[0];
  3375. const invitee_jid = name.replace(/ /g,'.').toLowerCase() + '@localhost';
  3376. var reason = "Please join this groupchat";
  3377. view.model.directInvite(invitee_jid, reason);
  3378. // Check in reverse order that we requested all three lists
  3379. // (member, owner and admin).
  3380. var admin_iq_id = IQ_ids.pop();
  3381. var owner_iq_id = IQ_ids.pop();
  3382. var member_iq_id = IQ_ids.pop();
  3383. expect(sent_IQs.pop().toLocaleString()).toBe(
  3384. `<iq id="${admin_iq_id}" to="coven@chat.shakespeare.lit" type="get" xmlns="jabber:client">`+
  3385. `<query xmlns="http://jabber.org/protocol/muc#admin">`+
  3386. `<item affiliation="admin"/>`+
  3387. `</query>`+
  3388. `</iq>`);
  3389. expect(sent_IQs.pop().toLocaleString()).toBe(
  3390. `<iq id="${owner_iq_id}" to="coven@chat.shakespeare.lit" type="get" xmlns="jabber:client">`+
  3391. `<query xmlns="http://jabber.org/protocol/muc#admin">`+
  3392. `<item affiliation="owner"/>`+
  3393. `</query>`+
  3394. `</iq>`);
  3395. expect(sent_IQs.pop().toLocaleString()).toBe(
  3396. `<iq id="${member_iq_id}" to="coven@chat.shakespeare.lit" type="get" xmlns="jabber:client">`+
  3397. `<query xmlns="http://jabber.org/protocol/muc#admin">`+
  3398. `<item affiliation="member"/>`+
  3399. `</query>`+
  3400. `</iq>`);
  3401. /* Now the service sends the member list to the user
  3402. *
  3403. * <iq from='coven@chat.shakespeare.lit'
  3404. * id='member3'
  3405. * to='crone1@shakespeare.lit/desktop'
  3406. * type='result'>
  3407. * <query xmlns='http://jabber.org/protocol/muc#admin'>
  3408. * <item affiliation='member'
  3409. * jid='hag66@shakespeare.lit'
  3410. * nick='thirdwitch'
  3411. * role='participant'/>
  3412. * </query>
  3413. * </iq>
  3414. */
  3415. var member_list_stanza = $iq({
  3416. 'from': 'coven@chat.shakespeare.lit',
  3417. 'id': member_iq_id,
  3418. 'to': 'dummy@localhost/resource',
  3419. 'type': 'result'
  3420. }).c('query', {'xmlns': Strophe.NS.MUC_ADMIN})
  3421. .c('item', {
  3422. 'affiliation': 'member',
  3423. 'jid': 'hag66@shakespeare.lit',
  3424. 'nick': 'thirdwitch',
  3425. 'role': 'participant'
  3426. });
  3427. _converse.connection._dataRecv(test_utils.createRequest(member_list_stanza));
  3428. var admin_list_stanza = $iq({
  3429. 'from': 'coven@chat.shakespeare.lit',
  3430. 'id': admin_iq_id,
  3431. 'to': 'dummy@localhost/resource',
  3432. 'type': 'result'
  3433. }).c('query', {'xmlns': Strophe.NS.MUC_ADMIN})
  3434. .c('item', {
  3435. 'affiliation': 'admin',
  3436. 'jid': 'wiccarocks@shakespeare.lit',
  3437. 'nick': 'secondwitch'
  3438. });
  3439. _converse.connection._dataRecv(test_utils.createRequest(admin_list_stanza));
  3440. var owner_list_stanza = $iq({
  3441. 'from': 'coven@chat.shakespeare.lit',
  3442. 'id': owner_iq_id,
  3443. 'to': 'dummy@localhost/resource',
  3444. 'type': 'result'
  3445. }).c('query', {'xmlns': Strophe.NS.MUC_ADMIN})
  3446. .c('item', {
  3447. 'affiliation': 'owner',
  3448. 'jid': 'crone1@shakespeare.lit',
  3449. });
  3450. _converse.connection._dataRecv(test_utils.createRequest(owner_list_stanza));
  3451. await test_utils.waitUntil(() => IQ_ids.length, 300);
  3452. stanza = await test_utils.waitUntil(() => _.get(_.filter(
  3453. IQ_stanzas,
  3454. iq => iq.nodeTree.querySelector(
  3455. `iq[to="${room_jid}"] query[xmlns="http://jabber.org/protocol/muc#admin"]`
  3456. )).pop(), 'nodeTree'));
  3457. expect(stanza.outerHTML,
  3458. `<iq id="${IQ_ids.pop()}" to="coven@chat.shakespeare.lit" type="set" xmlns="jabber:client">`+
  3459. `<query xmlns="http://jabber.org/protocol/muc#admin">`+
  3460. `<item affiliation="member" jid="${invitee_jid}">`+
  3461. `<reason>Please join this groupchat</reason>`+
  3462. `</item>`+
  3463. `</query>`+
  3464. `</iq>`);
  3465. // Finally check that the user gets invited.
  3466. expect(sent_stanza.toLocaleString()).toBe( // Strophe adds the xmlns attr (although not in spec)
  3467. `<message from="dummy@localhost/resource" id="${sent_id}" to="${invitee_jid}" xmlns="jabber:client">`+
  3468. `<x jid="coven@chat.shakespeare.lit" reason="Please join this groupchat" xmlns="jabber:x:conference"/>`+
  3469. `</message>`
  3470. );
  3471. done();
  3472. }));
  3473. });
  3474. describe("The affiliations delta", function () {
  3475. it("can be computed in various ways",
  3476. mock.initConverseWithPromises(
  3477. null, ['rosterGroupsFetched'], {},
  3478. async function (done, _converse) {
  3479. await test_utils.openChatRoom(_converse, 'coven', 'chat.shakespeare.lit', 'dummy');
  3480. var roomview = _converse.chatboxviews.get('coven@chat.shakespeare.lit');
  3481. var exclude_existing = false;
  3482. var remove_absentees = false;
  3483. var new_list = [];
  3484. var old_list = [];
  3485. var delta = u.computeAffiliationsDelta(exclude_existing, remove_absentees, new_list, old_list);
  3486. expect(delta.length).toBe(0);
  3487. new_list = [{'jid': 'wiccarocks@shakespeare.lit', 'affiliation': 'member'}];
  3488. old_list = [{'jid': 'wiccarocks@shakespeare.lit', 'affiliation': 'member'}];
  3489. delta = u.computeAffiliationsDelta(exclude_existing, remove_absentees, new_list, old_list);
  3490. expect(delta.length).toBe(0);
  3491. // When remove_absentees is false, then affiliations in the old
  3492. // list which are not in the new one won't be removed.
  3493. old_list = [{'jid': 'oldhag666@shakespeare.lit', 'affiliation': 'owner'},
  3494. {'jid': 'wiccarocks@shakespeare.lit', 'affiliation': 'member'}];
  3495. delta = u.computeAffiliationsDelta(exclude_existing, remove_absentees, new_list, old_list);
  3496. expect(delta.length).toBe(0);
  3497. // With exclude_existing set to false, any changed affiliations
  3498. // will be included in the delta (i.e. existing affiliations
  3499. // are included in the comparison).
  3500. old_list = [{'jid': 'wiccarocks@shakespeare.lit', 'affiliation': 'owner'}];
  3501. delta = u.computeAffiliationsDelta(exclude_existing, remove_absentees, new_list, old_list);
  3502. expect(delta.length).toBe(1);
  3503. expect(delta[0].jid).toBe('wiccarocks@shakespeare.lit');
  3504. expect(delta[0].affiliation).toBe('member');
  3505. // To also remove affiliations from the old list which are not
  3506. // in the new list, we set remove_absentees to true
  3507. remove_absentees = true;
  3508. old_list = [{'jid': 'oldhag666@shakespeare.lit', 'affiliation': 'owner'},
  3509. {'jid': 'wiccarocks@shakespeare.lit', 'affiliation': 'member'}];
  3510. delta = u.computeAffiliationsDelta(exclude_existing, remove_absentees, new_list, old_list);
  3511. expect(delta.length).toBe(1);
  3512. expect(delta[0].jid).toBe('oldhag666@shakespeare.lit');
  3513. expect(delta[0].affiliation).toBe('none');
  3514. delta = u.computeAffiliationsDelta(exclude_existing, remove_absentees, [], old_list);
  3515. expect(delta.length).toBe(2);
  3516. expect(delta[0].jid).toBe('oldhag666@shakespeare.lit');
  3517. expect(delta[0].affiliation).toBe('none');
  3518. expect(delta[1].jid).toBe('wiccarocks@shakespeare.lit');
  3519. expect(delta[1].affiliation).toBe('none');
  3520. // To only add a user if they don't already have an
  3521. // affiliation, we set 'exclude_existing' to true
  3522. exclude_existing = true;
  3523. old_list = [{'jid': 'wiccarocks@shakespeare.lit', 'affiliation': 'owner'}];
  3524. delta = u.computeAffiliationsDelta(exclude_existing, remove_absentees, new_list, old_list);
  3525. expect(delta.length).toBe(0);
  3526. done();
  3527. }));
  3528. });
  3529. describe("The \"Groupchats\" section", function () {
  3530. it("contains a link to a modal through which a new chatroom can be created",
  3531. mock.initConverseWithPromises(
  3532. null, ['rosterGroupsFetched', 'chatBoxesFetched'], {},
  3533. async function (done, _converse) {
  3534. test_utils.openControlBox();
  3535. _converse.emit('rosterContactsFetched');
  3536. var roomspanel = _converse.chatboxviews.get('controlbox').roomspanel;
  3537. roomspanel.el.querySelector('.show-add-muc-modal').click();
  3538. test_utils.closeControlBox(_converse);
  3539. const modal = roomspanel.add_room_modal;
  3540. await test_utils.waitUntil(() => u.isVisible(modal.el), 1000)
  3541. spyOn(_converse.ChatRoom.prototype, 'getRoomFeatures').and.callFake(() => Promise.resolve());
  3542. roomspanel.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  3543. modal.el.querySelector('input[name="chatroom"]').value = 'lounce@muc.localhost';
  3544. modal.el.querySelector('form input[type="submit"]').click();
  3545. await test_utils.waitUntil(() => _converse.chatboxes.length);
  3546. expect(sizzle('.chatroom', _converse.el).filter(u.isVisible).length).toBe(1); // There should now be an open chatroom
  3547. done();
  3548. }));
  3549. it("contains a link to a modal which can list groupchats publically available on the server",
  3550. mock.initConverseWithPromises(
  3551. null, ['rosterGroupsFetched', 'chatBoxesFetched'], {},
  3552. async function (done, _converse) {
  3553. var sendIQ = _converse.connection.sendIQ;
  3554. var sent_stanza, IQ_id;
  3555. spyOn(_converse.connection, 'sendIQ').and.callFake(function (iq, callback, errback) {
  3556. sent_stanza = iq;
  3557. IQ_id = sendIQ.bind(this)(iq, callback, errback);
  3558. });
  3559. test_utils.openControlBox();
  3560. var roomspanel = _converse.chatboxviews.get('controlbox').roomspanel;
  3561. roomspanel.el.querySelector('.show-list-muc-modal').click();
  3562. test_utils.closeControlBox(_converse);
  3563. const modal = roomspanel.list_rooms_modal;
  3564. await test_utils.waitUntil(() => u.isVisible(modal.el), 1000);
  3565. spyOn(_converse.ChatRoom.prototype, 'getRoomFeatures').and.callFake(() => Promise.resolve());
  3566. roomspanel.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  3567. // See: http://xmpp.org/extensions/xep-0045.html#disco-rooms
  3568. expect(modal.el.querySelectorAll('.available-chatrooms li').length).toBe(0);
  3569. const input = modal.el.querySelector('input[name="server"]').value = 'chat.shakespear.lit';
  3570. modal.el.querySelector('input[type="submit"]').click();
  3571. await test_utils.waitUntil(() => _converse.chatboxes.length);
  3572. expect(sent_stanza.toLocaleString()).toBe(
  3573. `<iq from="dummy@localhost/resource" id="${IQ_id}" to="chat.shakespear.lit" type="get" xmlns="jabber:client">`+
  3574. `<query xmlns="http://jabber.org/protocol/disco#items"/>`+
  3575. `</iq>`
  3576. );
  3577. const iq = $iq({
  3578. from:'muc.localhost',
  3579. to:'dummy@localhost/pda',
  3580. id: IQ_id,
  3581. type:'result'
  3582. }).c('query')
  3583. .c('item', { jid:'heath@chat.shakespeare.lit', name:'A Lonely Heath'}).up()
  3584. .c('item', { jid:'coven@chat.shakespeare.lit', name:'A Dark Cave'}).up()
  3585. .c('item', { jid:'forres@chat.shakespeare.lit', name:'The Palace'}).up()
  3586. .c('item', { jid:'inverness@chat.shakespeare.lit', name:'Macbeth&apos;s Castle'}).nodeTree;
  3587. _converse.connection._dataRecv(test_utils.createRequest(iq));
  3588. await test_utils.waitUntil(() => modal.el.querySelectorAll('.available-chatrooms li').length === 5);
  3589. const rooms = modal.el.querySelectorAll('.available-chatrooms li');
  3590. expect(rooms[0].textContent.trim()).toBe("Groupchats found:");
  3591. expect(rooms[1].textContent.trim()).toBe("A Lonely Heath");
  3592. expect(rooms[2].textContent.trim()).toBe("A Dark Cave");
  3593. expect(rooms[3].textContent.trim()).toBe("The Palace");
  3594. expect(rooms[4].textContent.trim()).toBe("Macbeth's Castle");
  3595. rooms[4].querySelector('.open-room').click();
  3596. await test_utils.waitUntil(() => _converse.chatboxes.length > 1);
  3597. expect(sizzle('.chatroom', _converse.el).filter(u.isVisible).length).toBe(1); // There should now be an open chatroom
  3598. var view = _converse.chatboxviews.get('inverness@chat.shakespeare.lit');
  3599. expect(view.el.querySelector('.chat-head-chatroom').textContent.trim()).toBe("Macbeth's Castle");
  3600. done();
  3601. }));
  3602. it("shows the number of unread mentions received",
  3603. mock.initConverseWithPromises(
  3604. null, ['rosterGroupsFetched'], {'allow_bookmarks': false},
  3605. async function (done, _converse) {
  3606. // XXX: we set `allow_bookmarks` to false, so that the groupchats
  3607. // list gets rendered. Otherwise we would have to mock
  3608. // the bookmark stanza exchange.
  3609. test_utils.openControlBox();
  3610. const roomspanel = _converse.chatboxviews.get('controlbox').roomspanel;
  3611. expect(roomspanel.el.querySelectorAll('.available-room').length).toBe(0);
  3612. const room_jid = 'kitchen@conference.shakespeare.lit';
  3613. const message = 'fires: Your attention is required';
  3614. await test_utils.openAndEnterChatRoom(_converse, 'kitchen', 'conference.shakespeare.lit', 'fires');
  3615. expect(roomspanel.el.querySelectorAll('.available-room').length).toBe(1);
  3616. expect(roomspanel.el.querySelectorAll('.msgs-indicator').length).toBe(0);
  3617. const view = _converse.chatboxviews.get(room_jid);
  3618. view.model.set({'minimized': true});
  3619. var contact_jid = mock.cur_names[5].replace(/ /g,'.').toLowerCase() + '@localhost';
  3620. const nick = mock.chatroom_names[0];
  3621. await view.model.onMessage($msg({
  3622. from: room_jid+'/'+nick,
  3623. id: (new Date()).getTime(),
  3624. to: 'dummy@localhost',
  3625. type: 'groupchat'
  3626. }).c('body').t(message).tree());
  3627. await test_utils.waitUntil(() => view.model.messages.length);
  3628. expect(roomspanel.el.querySelectorAll('.available-room').length).toBe(1);
  3629. expect(roomspanel.el.querySelectorAll('.msgs-indicator').length).toBe(1);
  3630. expect(roomspanel.el.querySelector('.msgs-indicator').textContent).toBe('1');
  3631. await view.model.onMessage($msg({
  3632. 'from': room_jid+'/'+nick,
  3633. 'id': (new Date()).getTime(),
  3634. 'to': 'dummy@localhost',
  3635. 'type': 'groupchat'
  3636. }).c('body').t(message).tree());
  3637. await test_utils.waitUntil(() => view.model.messages.length > 1);
  3638. expect(roomspanel.el.querySelectorAll('.available-room').length).toBe(1);
  3639. expect(roomspanel.el.querySelectorAll('.msgs-indicator').length).toBe(1);
  3640. expect(roomspanel.el.querySelector('.msgs-indicator').textContent).toBe('2');
  3641. view.model.set({'minimized': false});
  3642. expect(roomspanel.el.querySelectorAll('.available-room').length).toBe(1);
  3643. expect(roomspanel.el.querySelectorAll('.msgs-indicator').length).toBe(0);
  3644. done();
  3645. }));
  3646. describe("A Chat Status Notification", function () {
  3647. describe("A composing notification", function () {
  3648. it("will be shown if received",
  3649. mock.initConverseWithPromises(
  3650. null, ['rosterGroupsFetched'], {},
  3651. async function (done, _converse) {
  3652. const room_jid = 'coven@chat.shakespeare.lit';
  3653. await test_utils.openAndEnterChatRoom(_converse, 'coven', 'chat.shakespeare.lit', 'some1');
  3654. const view = _converse.chatboxviews.get('coven@chat.shakespeare.lit');
  3655. const chat_content = view.el.querySelector('.chat-content');
  3656. expect(sizzle('div.chat-info:first', chat_content).pop().textContent)
  3657. .toBe("some1 has entered the groupchat");
  3658. let presence = $pres({
  3659. to: 'dummy@localhost/_converse.js-29092160',
  3660. from: 'coven@chat.shakespeare.lit/newguy'
  3661. })
  3662. .c('x', {xmlns: Strophe.NS.MUC_USER})
  3663. .c('item', {
  3664. 'affiliation': 'none',
  3665. 'jid': 'newguy@localhost/_converse.js-290929789',
  3666. 'role': 'participant'
  3667. });
  3668. _converse.connection._dataRecv(test_utils.createRequest(presence));
  3669. expect(chat_content.querySelectorAll('div.chat-info').length).toBe(2);
  3670. expect(sizzle('div.chat-info:last', chat_content).pop().textContent)
  3671. .toBe("newguy has entered the groupchat");
  3672. presence = $pres({
  3673. to: 'dummy@localhost/_converse.js-29092160',
  3674. from: 'coven@chat.shakespeare.lit/nomorenicks'
  3675. })
  3676. .c('x', {xmlns: Strophe.NS.MUC_USER})
  3677. .c('item', {
  3678. 'affiliation': 'none',
  3679. 'jid': 'nomorenicks@localhost/_converse.js-290929789',
  3680. 'role': 'participant'
  3681. });
  3682. _converse.connection._dataRecv(test_utils.createRequest(presence));
  3683. expect(chat_content.querySelectorAll('div.chat-info').length).toBe(3);
  3684. expect(sizzle('div.chat-info:last', chat_content).pop().textContent)
  3685. .toBe("nomorenicks has entered the groupchat");
  3686. // See XEP-0085 http://xmpp.org/extensions/xep-0085.html#definitions
  3687. // <composing> state
  3688. let msg = $msg({
  3689. from: room_jid+'/newguy',
  3690. id: (new Date()).getTime(),
  3691. to: 'dummy@localhost',
  3692. type: 'groupchat'
  3693. }).c('body').c('composing', {'xmlns': Strophe.NS.CHATSTATES}).tree();
  3694. await view.model.onMessage(msg);
  3695. await test_utils.waitUntil(() => view.el.querySelectorAll('.chat-state-notification').length);
  3696. // Check that the notification appears inside the chatbox in the DOM
  3697. let events = view.el.querySelectorAll('.chat-event');
  3698. expect(events.length).toBe(3);
  3699. expect(events[0].textContent).toEqual('some1 has entered the groupchat');
  3700. expect(events[1].textContent).toEqual('newguy has entered the groupchat');
  3701. expect(events[2].textContent).toEqual('nomorenicks has entered the groupchat');
  3702. let notifications = view.el.querySelectorAll('.chat-state-notification');
  3703. expect(notifications.length).toBe(1);
  3704. expect(notifications[0].textContent).toEqual('newguy is typing');
  3705. const timeout_functions = [];
  3706. spyOn(window, 'setTimeout').and.callFake(function (func, delay) {
  3707. timeout_functions.push(func);
  3708. });
  3709. // Check that it doesn't appear twice
  3710. msg = $msg({
  3711. from: room_jid+'/newguy',
  3712. id: (new Date()).getTime(),
  3713. to: 'dummy@localhost',
  3714. type: 'groupchat'
  3715. }).c('body').c('composing', {'xmlns': Strophe.NS.CHATSTATES}).tree();
  3716. await view.model.onMessage(msg);
  3717. events = view.el.querySelectorAll('.chat-event');
  3718. expect(events.length).toBe(3);
  3719. expect(events[0].textContent).toEqual('some1 has entered the groupchat');
  3720. expect(events[1].textContent).toEqual('newguy has entered the groupchat');
  3721. expect(events[2].textContent).toEqual('nomorenicks has entered the groupchat');
  3722. notifications = view.el.querySelectorAll('.chat-state-notification');
  3723. expect(notifications.length).toBe(1);
  3724. expect(notifications[0].textContent).toEqual('newguy is typing');
  3725. expect(timeout_functions.length).toBe(1);
  3726. // <composing> state for a different occupant
  3727. msg = $msg({
  3728. from: room_jid+'/nomorenicks',
  3729. id: (new Date()).getTime(),
  3730. to: 'dummy@localhost',
  3731. type: 'groupchat'
  3732. }).c('body').c('composing', {'xmlns': Strophe.NS.CHATSTATES}).tree();
  3733. await view.model.onMessage(msg);
  3734. events = view.el.querySelectorAll('.chat-event');
  3735. expect(events.length).toBe(3);
  3736. expect(events[0].textContent).toEqual('some1 has entered the groupchat');
  3737. expect(events[1].textContent).toEqual('newguy has entered the groupchat');
  3738. expect(events[2].textContent).toEqual('nomorenicks has entered the groupchat');
  3739. notifications = view.el.querySelectorAll('.chat-state-notification');
  3740. expect(notifications.length).toBe(2);
  3741. expect(notifications[0].textContent).toEqual('newguy is typing');
  3742. expect(notifications[1].textContent).toEqual('nomorenicks is typing');
  3743. expect(timeout_functions.length).toBe(2);
  3744. // Check that new messages appear under the chat state
  3745. // notifications
  3746. msg = $msg({
  3747. from: 'lounge@localhost/some1',
  3748. id: (new Date()).getTime(),
  3749. to: 'dummy@localhost',
  3750. type: 'groupchat'
  3751. }).c('body').t('hello world').tree();
  3752. await view.model.onMessage(msg);
  3753. await new Promise((resolve, reject) => view.once('messageInserted', resolve));
  3754. const messages = view.el.querySelectorAll('.message');
  3755. expect(messages.length).toBe(7);
  3756. expect(view.el.querySelectorAll('.chat-msg').length).toBe(1);
  3757. expect(view.el.querySelector('.chat-msg .chat-msg__text').textContent).toBe('hello world');
  3758. // Test that the composing notifications get removed
  3759. // via timeout.
  3760. timeout_functions[0]();
  3761. events = view.el.querySelectorAll('.chat-event');
  3762. expect(events.length).toBe(3);
  3763. expect(events[0].textContent).toEqual('some1 has entered the groupchat');
  3764. expect(events[1].textContent).toEqual('newguy has entered the groupchat');
  3765. expect(events[2].textContent).toEqual('nomorenicks has entered the groupchat');
  3766. notifications = view.el.querySelectorAll('.chat-state-notification');
  3767. expect(notifications.length).toBe(1);
  3768. expect(notifications[0].textContent).toEqual('nomorenicks is typing');
  3769. timeout_functions[1]();
  3770. events = view.el.querySelectorAll('.chat-event');
  3771. expect(events.length).toBe(3);
  3772. expect(events[0].textContent).toEqual('some1 has entered the groupchat');
  3773. expect(events[1].textContent).toEqual('newguy has entered the groupchat');
  3774. expect(events[2].textContent).toEqual('nomorenicks has entered the groupchat');
  3775. notifications = view.el.querySelectorAll('.chat-state-notification');
  3776. expect(notifications.length).toBe(0);
  3777. done();
  3778. }));
  3779. });
  3780. describe("A paused notification", function () {
  3781. it("will be shown if received",
  3782. mock.initConverseWithPromises(
  3783. null, ['rosterGroupsFetched', 'chatBoxesFetched'], {},
  3784. async function (done, _converse) {
  3785. await test_utils.openChatRoom(_converse, "coven", 'chat.shakespeare.lit', 'some1');
  3786. const room_jid = 'coven@chat.shakespeare.lit';
  3787. const view = _converse.chatboxviews.get('coven@chat.shakespeare.lit');
  3788. const chat_content = view.el.querySelector('.chat-content');
  3789. /* <presence to="dummy@localhost/_converse.js-29092160"
  3790. * from="coven@chat.shakespeare.lit/some1">
  3791. * <x xmlns="http://jabber.org/protocol/muc#user">
  3792. * <item affiliation="owner" jid="dummy@localhost/_converse.js-29092160" role="moderator"/>
  3793. * <status code="110"/>
  3794. * </x>
  3795. * </presence></body>
  3796. */
  3797. let presence = $pres({
  3798. to: 'dummy@localhost/_converse.js-29092160',
  3799. from: 'coven@chat.shakespeare.lit/some1'
  3800. }).c('x', {xmlns: Strophe.NS.MUC_USER})
  3801. .c('item', {
  3802. 'affiliation': 'owner',
  3803. 'jid': 'dummy@localhost/_converse.js-29092160',
  3804. 'role': 'moderator'
  3805. }).up()
  3806. .c('status', {code: '110'});
  3807. _converse.connection._dataRecv(test_utils.createRequest(presence));
  3808. expect(sizzle('div.chat-info:first', chat_content).pop().textContent)
  3809. .toBe("some1 has entered the groupchat");
  3810. presence = $pres({
  3811. to: 'dummy@localhost/_converse.js-29092160',
  3812. from: 'coven@chat.shakespeare.lit/newguy'
  3813. })
  3814. .c('x', {xmlns: Strophe.NS.MUC_USER})
  3815. .c('item', {
  3816. 'affiliation': 'none',
  3817. 'jid': 'newguy@localhost/_converse.js-290929789',
  3818. 'role': 'participant'
  3819. });
  3820. _converse.connection._dataRecv(test_utils.createRequest(presence));
  3821. expect(chat_content.querySelectorAll('div.chat-info').length).toBe(2);
  3822. expect(sizzle('div.chat-info:last', chat_content).pop().textContent)
  3823. .toBe("newguy has entered the groupchat");
  3824. presence = $pres({
  3825. to: 'dummy@localhost/_converse.js-29092160',
  3826. from: 'coven@chat.shakespeare.lit/nomorenicks'
  3827. })
  3828. .c('x', {xmlns: Strophe.NS.MUC_USER})
  3829. .c('item', {
  3830. 'affiliation': 'none',
  3831. 'jid': 'nomorenicks@localhost/_converse.js-290929789',
  3832. 'role': 'participant'
  3833. });
  3834. _converse.connection._dataRecv(test_utils.createRequest(presence));
  3835. expect(chat_content.querySelectorAll('div.chat-info').length).toBe(3);
  3836. expect(sizzle('div.chat-info:last', chat_content).pop().textContent)
  3837. .toBe("nomorenicks has entered the groupchat");
  3838. // See XEP-0085 http://xmpp.org/extensions/xep-0085.html#definitions
  3839. // <composing> state
  3840. var msg = $msg({
  3841. from: room_jid+'/newguy',
  3842. id: (new Date()).getTime(),
  3843. to: 'dummy@localhost',
  3844. type: 'groupchat'
  3845. }).c('body').c('composing', {'xmlns': Strophe.NS.CHATSTATES}).tree();
  3846. await view.model.onMessage(msg);
  3847. // Check that the notification appears inside the chatbox in the DOM
  3848. var events = view.el.querySelectorAll('.chat-event');
  3849. expect(events.length).toBe(3);
  3850. expect(events[0].textContent).toEqual('some1 has entered the groupchat');
  3851. expect(events[1].textContent).toEqual('newguy has entered the groupchat');
  3852. expect(events[2].textContent).toEqual('nomorenicks has entered the groupchat');
  3853. var notifications = view.el.querySelectorAll('.chat-state-notification');
  3854. expect(notifications.length).toBe(1);
  3855. expect(notifications[0].textContent).toEqual('newguy is typing');
  3856. // Check that it doesn't appear twice
  3857. msg = $msg({
  3858. from: room_jid+'/newguy',
  3859. id: (new Date()).getTime(),
  3860. to: 'dummy@localhost',
  3861. type: 'groupchat'
  3862. }).c('body').c('composing', {'xmlns': Strophe.NS.CHATSTATES}).tree();
  3863. await view.model.onMessage(msg);
  3864. events = view.el.querySelectorAll('.chat-event');
  3865. expect(events.length).toBe(3);
  3866. expect(events[0].textContent).toEqual('some1 has entered the groupchat');
  3867. expect(events[1].textContent).toEqual('newguy has entered the groupchat');
  3868. expect(events[2].textContent).toEqual('nomorenicks has entered the groupchat');
  3869. notifications = view.el.querySelectorAll('.chat-state-notification');
  3870. expect(notifications.length).toBe(1);
  3871. expect(notifications[0].textContent).toEqual('newguy is typing');
  3872. // <composing> state for a different occupant
  3873. msg = $msg({
  3874. from: room_jid+'/nomorenicks',
  3875. id: (new Date()).getTime(),
  3876. to: 'dummy@localhost',
  3877. type: 'groupchat'
  3878. }).c('body').c('composing', {'xmlns': Strophe.NS.CHATSTATES}).tree();
  3879. await view.model.onMessage(msg);
  3880. events = view.el.querySelectorAll('.chat-event');
  3881. expect(events.length).toBe(3);
  3882. expect(events[0].textContent).toEqual('some1 has entered the groupchat');
  3883. expect(events[1].textContent).toEqual('newguy has entered the groupchat');
  3884. expect(events[2].textContent).toEqual('nomorenicks has entered the groupchat');
  3885. notifications = view.el.querySelectorAll('.chat-state-notification');
  3886. expect(notifications.length).toBe(2);
  3887. expect(notifications[0].textContent).toEqual('newguy is typing');
  3888. expect(notifications[1].textContent).toEqual('nomorenicks is typing');
  3889. // <paused> state from occupant who typed first
  3890. msg = $msg({
  3891. from: room_jid+'/newguy',
  3892. id: (new Date()).getTime(),
  3893. to: 'dummy@localhost',
  3894. type: 'groupchat'
  3895. }).c('body').c('paused', {'xmlns': Strophe.NS.CHATSTATES}).tree();
  3896. await view.model.onMessage(msg);
  3897. events = view.el.querySelectorAll('.chat-event');
  3898. expect(events.length).toBe(3);
  3899. expect(events[0].textContent).toEqual('some1 has entered the groupchat');
  3900. expect(events[1].textContent).toEqual('newguy has entered the groupchat');
  3901. expect(events[2].textContent).toEqual('nomorenicks has entered the groupchat');
  3902. notifications = view.el.querySelectorAll('.chat-state-notification');
  3903. expect(notifications.length).toBe(2);
  3904. expect(notifications[0].textContent).toEqual('nomorenicks is typing');
  3905. expect(notifications[1].textContent).toEqual('newguy has stopped typing');
  3906. done();
  3907. }));
  3908. });
  3909. });
  3910. });
  3911. });
  3912. }));