muc.js 243 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409
  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("Groupchats", 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.initConverse(
  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. await _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. await _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. await _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.initConverse(
  49. null, ['rosterGroupsFetched'], {},
  50. async function (done, _converse) {
  51. await 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 = await _converse.api.rooms.get(jid);
  56. expect(room instanceof Object).toBeTruthy();
  57. let view = _converse.chatboxviews.get(jid);
  58. expect(view.is_chatroom).toBeTruthy();
  59. expect(u.isVisible(view.el)).toBeTruthy();
  60. await view.close();
  61. // Test with mixed case
  62. await test_utils.openAndEnterChatRoom(_converse, 'Leisure', 'localhost', 'dummy');
  63. jid = 'Leisure@localhost';
  64. room = await _converse.api.rooms.get(jid);
  65. expect(room instanceof Object).toBeTruthy();
  66. view = _converse.chatboxviews.get(jid.toLowerCase());
  67. expect(u.isVisible(view.el)).toBeTruthy();
  68. jid = 'leisure@localhost';
  69. room = await _converse.api.rooms.get(jid);
  70. expect(room instanceof Object).toBeTruthy();
  71. view = _converse.chatboxviews.get(jid.toLowerCase());
  72. expect(u.isVisible(view.el)).toBeTruthy();
  73. jid = 'leiSure@localhost';
  74. room = await _converse.api.rooms.get(jid);
  75. expect(room instanceof Object).toBeTruthy();
  76. view = _converse.chatboxviews.get(jid.toLowerCase());
  77. expect(u.isVisible(view.el)).toBeTruthy();
  78. await view.close();
  79. // Non-existing room
  80. jid = 'lounge2@localhost';
  81. room = await _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.initConverse(
  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. await 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. await 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. await 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. const node = u.toStanza(`
  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));
  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.initConverse(
  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.initConverse(
  357. null, ['rosterGroupsFetched'], {},
  358. async function (done, _converse) {
  359. await 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.initConverse(
  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. await test_utils.waitUntil(() => sizzle('div.chat-info', chat_content).length > 1);
  410. expect(chat_content.querySelectorAll('.chat-info').length).toBe(2);
  411. expect(sizzle('div.chat-info:first', chat_content).pop().textContent)
  412. .toBe("This groupchat is not anonymous");
  413. expect(sizzle('div.chat-info:last', chat_content).pop().textContent)
  414. .toBe("some1 has entered the groupchat");
  415. // Check that we don't show the notification twice
  416. presence = $pres({
  417. to: 'dummy@localhost/resource',
  418. from: 'coven@chat.shakespeare.lit/some1'
  419. }).c('x', {xmlns: Strophe.NS.MUC_USER})
  420. .c('item', {
  421. 'affiliation': 'owner',
  422. 'jid': 'dummy@localhost/_converse.js-29092160',
  423. 'role': 'moderator'
  424. }).up()
  425. .c('status', {code: '110'}).up()
  426. .c('status', {code: '100'});
  427. _converse.connection._dataRecv(test_utils.createRequest(presence));
  428. expect(chat_content.querySelectorAll('.chat-info').length).toBe(2);
  429. expect(sizzle('div.chat-info:first', chat_content).pop().textContent)
  430. .toBe("This groupchat is not anonymous");
  431. expect(sizzle('div.chat-info:last', chat_content).pop().textContent)
  432. .toBe("some1 has entered the groupchat");
  433. done();
  434. }));
  435. it("shows join/leave messages when users enter or exit a groupchat",
  436. mock.initConverse(
  437. null, ['rosterGroupsFetched', 'chatBoxesFetched'], {},
  438. async function (done, _converse) {
  439. await test_utils.openChatRoom(_converse, "coven", 'chat.shakespeare.lit', 'some1');
  440. const view = _converse.chatboxviews.get('coven@chat.shakespeare.lit');
  441. const chat_content = view.el.querySelector('.chat-content');
  442. /* We don't show join/leave messages for existing occupants. We
  443. * know about them because we receive their presences before we
  444. * receive our own.
  445. */
  446. let presence = $pres({
  447. to: 'dummy@localhost/_converse.js-29092160',
  448. from: 'coven@chat.shakespeare.lit/oldguy'
  449. }).c('x', {xmlns: Strophe.NS.MUC_USER})
  450. .c('item', {
  451. 'affiliation': 'none',
  452. 'jid': 'oldguy@localhost/_converse.js-290929789',
  453. 'role': 'participant'
  454. });
  455. _converse.connection._dataRecv(test_utils.createRequest(presence));
  456. expect(chat_content.querySelectorAll('div.chat-info').length).toBe(0);
  457. /* <presence to="dummy@localhost/_converse.js-29092160"
  458. * from="coven@chat.shakespeare.lit/some1">
  459. * <x xmlns="http://jabber.org/protocol/muc#user">
  460. * <item affiliation="owner" jid="dummy@localhost/_converse.js-29092160" role="moderator"/>
  461. * <status code="110"/>
  462. * </x>
  463. * </presence></body>
  464. */
  465. presence = $pres({
  466. to: 'dummy@localhost/_converse.js-29092160',
  467. from: 'coven@chat.shakespeare.lit/some1'
  468. }).c('x', {xmlns: Strophe.NS.MUC_USER})
  469. .c('item', {
  470. 'affiliation': 'owner',
  471. 'jid': 'dummy@localhost/_converse.js-29092160',
  472. 'role': 'moderator'
  473. }).up()
  474. .c('status', {code: '110'});
  475. _converse.connection._dataRecv(test_utils.createRequest(presence));
  476. expect(sizzle('div.chat-info:first', chat_content).pop().textContent)
  477. .toBe("some1 has entered the groupchat");
  478. presence = $pres({
  479. to: 'dummy@localhost/_converse.js-29092160',
  480. from: 'coven@chat.shakespeare.lit/newguy'
  481. })
  482. .c('x', {xmlns: Strophe.NS.MUC_USER})
  483. .c('item', {
  484. 'affiliation': 'none',
  485. 'jid': 'newguy@localhost/_converse.js-290929789',
  486. 'role': 'participant'
  487. });
  488. _converse.connection._dataRecv(test_utils.createRequest(presence));
  489. expect(chat_content.querySelectorAll('div.chat-info').length).toBe(2);
  490. expect(sizzle('div.chat-info:last', chat_content).pop().textContent)
  491. .toBe("newguy has entered the groupchat");
  492. const msg = $msg({
  493. 'from': 'coven@chat.shakespeare.lit/some1',
  494. 'id': (new Date()).getTime(),
  495. 'to': 'dummy@localhost',
  496. 'type': 'groupchat'
  497. }).c('body').t('hello world').tree();
  498. _converse.connection._dataRecv(test_utils.createRequest(msg));
  499. await new Promise((resolve, reject) => view.once('messageInserted', resolve));
  500. // Add another entrant, otherwise the above message will be
  501. // collapsed if "newguy" leaves immediately again
  502. presence = $pres({
  503. to: 'dummy@localhost/_converse.js-29092160',
  504. from: 'coven@chat.shakespeare.lit/newgirl'
  505. })
  506. .c('x', {xmlns: Strophe.NS.MUC_USER})
  507. .c('item', {
  508. 'affiliation': 'none',
  509. 'jid': 'newgirl@localhost/_converse.js-213098781',
  510. 'role': 'participant'
  511. });
  512. _converse.connection._dataRecv(test_utils.createRequest(presence));
  513. expect(chat_content.querySelectorAll('div.chat-info').length).toBe(3);
  514. expect(sizzle('div.chat-info:last', chat_content).pop().textContent)
  515. .toBe("newgirl has entered the groupchat");
  516. // Don't show duplicate join messages
  517. presence = $pres({
  518. to: 'dummy@localhost/_converse.js-290918392',
  519. from: 'coven@chat.shakespeare.lit/newguy'
  520. }).c('x', {xmlns: Strophe.NS.MUC_USER})
  521. .c('item', {
  522. 'affiliation': 'none',
  523. 'jid': 'newguy@localhost/_converse.js-290929789',
  524. 'role': 'participant'
  525. });
  526. _converse.connection._dataRecv(test_utils.createRequest(presence));
  527. expect(chat_content.querySelectorAll('div.chat-info').length).toBe(3);
  528. /* <presence
  529. * from='coven@chat.shakespeare.lit/thirdwitch'
  530. * to='crone1@shakespeare.lit/desktop'
  531. * type='unavailable'>
  532. * <status>Disconnected: Replaced by new connection</status>
  533. * <x xmlns='http://jabber.org/protocol/muc#user'>
  534. * <item affiliation='member'
  535. * jid='hag66@shakespeare.lit/pda'
  536. * role='none'/>
  537. * </x>
  538. * </presence>
  539. */
  540. presence = $pres({
  541. to: 'dummy@localhost/_converse.js-29092160',
  542. type: 'unavailable',
  543. from: 'coven@chat.shakespeare.lit/newguy'
  544. })
  545. .c('status', 'Disconnected: Replaced by new connection').up()
  546. .c('x', {xmlns: Strophe.NS.MUC_USER})
  547. .c('item', {
  548. 'affiliation': 'none',
  549. 'jid': 'newguy@localhost/_converse.js-290929789',
  550. 'role': 'none'
  551. });
  552. _converse.connection._dataRecv(test_utils.createRequest(presence));
  553. expect(chat_content.querySelectorAll('div.chat-info').length).toBe(4);
  554. expect(sizzle('div.chat-info:last', chat_content).pop().textContent).toBe(
  555. 'newguy has left the groupchat. '+
  556. '"Disconnected: Replaced by new connection"');
  557. // When the user immediately joins again, we collapse the
  558. // multiple join/leave messages.
  559. presence = $pres({
  560. to: 'dummy@localhost/_converse.js-29092160',
  561. from: 'coven@chat.shakespeare.lit/newguy'
  562. }).c('x', {xmlns: Strophe.NS.MUC_USER})
  563. .c('item', {
  564. 'affiliation': 'none',
  565. 'jid': 'newguy@localhost/_converse.js-290929789',
  566. 'role': 'participant'
  567. });
  568. _converse.connection._dataRecv(test_utils.createRequest(presence));
  569. expect(chat_content.querySelectorAll('div.chat-info').length).toBe(4);
  570. let msg_el = sizzle('div.chat-info:last', chat_content).pop();
  571. expect(msg_el.textContent).toBe("newguy has left and re-entered the groupchat");
  572. expect(msg_el.getAttribute('data-leavejoin')).toBe('newguy');
  573. presence = $pres({
  574. to: 'dummy@localhost/_converse.js-29092160',
  575. type: 'unavailable',
  576. from: 'coven@chat.shakespeare.lit/newguy'
  577. })
  578. .c('x', {xmlns: Strophe.NS.MUC_USER})
  579. .c('item', {
  580. 'affiliation': 'none',
  581. 'jid': 'newguy@localhost/_converse.js-290929789',
  582. 'role': 'none'
  583. });
  584. _converse.connection._dataRecv(test_utils.createRequest(presence));
  585. expect(chat_content.querySelectorAll('div.chat-info').length).toBe(4);
  586. msg_el = sizzle('div.chat-info', chat_content).pop();
  587. expect(msg_el.textContent).toBe('newguy has left the groupchat');
  588. expect(msg_el.getAttribute('data-leave')).toBe('newguy');
  589. presence = $pres({
  590. to: 'dummy@localhost/_converse.js-29092160',
  591. from: 'coven@chat.shakespeare.lit/nomorenicks'
  592. })
  593. .c('x', {xmlns: Strophe.NS.MUC_USER})
  594. .c('item', {
  595. 'affiliation': 'none',
  596. 'jid': 'nomorenicks@localhost/_converse.js-290929789',
  597. 'role': 'participant'
  598. });
  599. _converse.connection._dataRecv(test_utils.createRequest(presence));
  600. expect(chat_content.querySelectorAll('div.chat-info').length).toBe(5);
  601. expect(sizzle('div.chat-info:last', chat_content).pop().textContent)
  602. .toBe("nomorenicks has entered the groupchat");
  603. presence = $pres({
  604. to: 'dummy@localhost/_converse.js-290918392',
  605. type: 'unavailable',
  606. from: 'coven@chat.shakespeare.lit/nomorenicks'
  607. }).c('x', {xmlns: Strophe.NS.MUC_USER})
  608. .c('item', {
  609. 'affiliation': 'none',
  610. 'jid': 'nomorenicks@localhost/_converse.js-290929789',
  611. 'role': 'none'
  612. });
  613. _converse.connection._dataRecv(test_utils.createRequest(presence));
  614. expect(chat_content.querySelectorAll('div.chat-info').length).toBe(5);
  615. expect(sizzle('div.chat-info:last', chat_content).pop().textContent)
  616. .toBe("nomorenicks has entered and left the groupchat");
  617. presence = $pres({
  618. to: 'dummy@localhost/_converse.js-29092160',
  619. from: 'coven@chat.shakespeare.lit/nomorenicks'
  620. })
  621. .c('x', {xmlns: Strophe.NS.MUC_USER})
  622. .c('item', {
  623. 'affiliation': 'none',
  624. 'jid': 'nomorenicks@localhost/_converse.js-290929789',
  625. 'role': 'participant'
  626. });
  627. _converse.connection._dataRecv(test_utils.createRequest(presence));
  628. expect(chat_content.querySelectorAll('div.chat-info').length).toBe(5);
  629. expect(sizzle('div.chat-info:last', chat_content).pop().textContent)
  630. .toBe("nomorenicks has entered the groupchat");
  631. // Test a member joining and leaving
  632. presence = $pres({
  633. to: 'dummy@localhost/_converse.js-290918392',
  634. from: 'coven@chat.shakespeare.lit/insider'
  635. }).c('x', {xmlns: Strophe.NS.MUC_USER})
  636. .c('item', {
  637. 'affiliation': 'member',
  638. 'jid': 'insider@localhost/_converse.js-290929789',
  639. 'role': 'participant'
  640. });
  641. _converse.connection._dataRecv(test_utils.createRequest(presence));
  642. expect(chat_content.querySelectorAll('div.chat-info').length).toBe(6);
  643. /* <presence
  644. * from='coven@chat.shakespeare.lit/thirdwitch'
  645. * to='crone1@shakespeare.lit/desktop'
  646. * type='unavailable'>
  647. * <status>Disconnected: Replaced by new connection</status>
  648. * <x xmlns='http://jabber.org/protocol/muc#user'>
  649. * <item affiliation='member'
  650. * jid='hag66@shakespeare.lit/pda'
  651. * role='none'/>
  652. * </x>
  653. * </presence>
  654. */
  655. presence = $pres({
  656. to: 'dummy@localhost/_converse.js-29092160',
  657. type: 'unavailable',
  658. from: 'coven@chat.shakespeare.lit/insider'
  659. })
  660. .c('status', 'Disconnected: Replaced by new connection').up()
  661. .c('x', {xmlns: Strophe.NS.MUC_USER})
  662. .c('item', {
  663. 'affiliation': 'member',
  664. 'jid': 'insider@localhost/_converse.js-290929789',
  665. 'role': 'none'
  666. });
  667. _converse.connection._dataRecv(test_utils.createRequest(presence));
  668. expect(chat_content.querySelectorAll('div.chat-info').length).toBe(6);
  669. expect(sizzle('div.chat-info:last', chat_content).pop().textContent).toBe(
  670. 'insider has entered and left the groupchat. '+
  671. '"Disconnected: Replaced by new connection"');
  672. expect(view.model.occupants.length).toBe(5);
  673. expect(view.model.occupants.findWhere({'jid': 'insider@localhost'}).get('show')).toBe('offline');
  674. // New girl leaves
  675. presence = $pres({
  676. 'to': 'dummy@localhost/_converse.js-29092160',
  677. 'type': 'unavailable',
  678. 'from': 'coven@chat.shakespeare.lit/newgirl'
  679. })
  680. .c('x', {xmlns: Strophe.NS.MUC_USER})
  681. .c('item', {
  682. 'affiliation': 'none',
  683. 'jid': 'newgirl@localhost/_converse.js-213098781',
  684. 'role': 'none'
  685. });
  686. _converse.connection._dataRecv(test_utils.createRequest(presence));
  687. expect(chat_content.querySelectorAll('div.chat-info').length).toBe(6);
  688. expect(sizzle('div.chat-info:last', chat_content).pop().textContent).toBe("newgirl has entered and left the groupchat");
  689. expect(view.model.occupants.length).toBe(4);
  690. done();
  691. }));
  692. it("combines subsequent join/leave messages when users enter or exit a groupchat",
  693. mock.initConverse(
  694. null, ['rosterGroupsFetched', 'chatBoxesFetched'], {},
  695. async function (done, _converse) {
  696. await test_utils.openAndEnterChatRoom(_converse, 'coven', 'chat.shakespeare.lit', 'dummy')
  697. const view = _converse.chatboxviews.get('coven@chat.shakespeare.lit');
  698. const chat_content = view.el.querySelector('.chat-content');
  699. expect(sizzle('div.chat-info', chat_content).length).toBe(1);
  700. expect(sizzle('div.chat-info:last', chat_content).pop().textContent).toBe("dummy has entered the groupchat");
  701. let presence = u.toStanza(
  702. `<presence xmlns="jabber:client" to="dummy@localhost/resource" from="coven@chat.shakespeare.lit/fabio">
  703. <c xmlns="http://jabber.org/protocol/caps" node="http://conversations.im" ver="INI3xjRUioclBTP/aACfWi5m9UY=" hash="sha-1"/>
  704. <x xmlns="http://jabber.org/protocol/muc#user">
  705. <item affiliation="none" jid="fabio@montefuscolo.com.br/Conversations.ZvLu" role="participant"/>
  706. </x>
  707. </presence>`);
  708. _converse.connection._dataRecv(test_utils.createRequest(presence));
  709. expect(sizzle('div.chat-info', chat_content).length).toBe(2);
  710. expect(sizzle('div.chat-info:last', chat_content).pop().textContent).toBe("fabio has entered the groupchat");
  711. presence = u.toStanza(
  712. `<presence xmlns="jabber:client" to="dummy@localhost/resource" from="coven@chat.shakespeare.lit/Dele Olajide">
  713. <x xmlns="http://jabber.org/protocol/muc#user">
  714. <item affiliation="none" jid="deleo@traderlynk.4ng.net/converse.js-39320524" role="participant"/>
  715. </x>
  716. </presence>`);
  717. _converse.connection._dataRecv(test_utils.createRequest(presence));
  718. expect(sizzle('div.chat-info', chat_content).length).toBe(3);
  719. expect(sizzle('div.chat-info:last', chat_content).pop().textContent).toBe("Dele Olajide has entered the groupchat");
  720. presence = u.toStanza(
  721. `<presence xmlns="jabber:client" to="dummy@localhost/resource" from="coven@chat.shakespeare.lit/jcbrand">
  722. <x xmlns="http://jabber.org/protocol/muc#user">
  723. <item affiliation="owner" jid="jc@opkode.com/converse.js-30645022" role="moderator"/>
  724. <status code="110"/>
  725. </x>
  726. </presence>`);
  727. _converse.connection._dataRecv(test_utils.createRequest(presence));
  728. await test_utils.waitUntil(() => sizzle('div.chat-info', chat_content).length > 3);
  729. expect(sizzle('div.chat-info', chat_content).length).toBe(4);
  730. expect(sizzle('div.chat-info:last', chat_content).pop().textContent).toBe("jcbrand has entered the groupchat");
  731. presence = u.toStanza(
  732. `<presence xmlns="jabber:client" to="dummy@localhost/resource" type="unavailable" from="coven@chat.shakespeare.lit/Dele Olajide">
  733. <x xmlns="http://jabber.org/protocol/muc#user">
  734. <item affiliation="none" jid="deleo@traderlynk.4ng.net/converse.js-39320524" role="none"/>
  735. </x>
  736. </presence>`);
  737. _converse.connection._dataRecv(test_utils.createRequest(presence));
  738. expect(sizzle('div.chat-info', chat_content).length).toBe(4);
  739. expect(sizzle('div.chat-info:last', chat_content).pop().textContent).toBe("Dele Olajide has entered and left the groupchat");
  740. presence = u.toStanza(
  741. `<presence xmlns="jabber:client" to="dummy@localhost/resource" from="coven@chat.shakespeare.lit/Dele Olajide">
  742. <x xmlns="http://jabber.org/protocol/muc#user">
  743. <item affiliation="none" jid="deleo@traderlynk.4ng.net/converse.js-74567907" role="participant"/>
  744. </x>
  745. </presence>`);
  746. _converse.connection._dataRecv(test_utils.createRequest(presence));
  747. expect(sizzle('div.chat-info', chat_content).length).toBe(4);
  748. expect(sizzle('div.chat-info:last', chat_content).pop().textContent).toBe("Dele Olajide has entered the groupchat");
  749. presence = u.toStanza(
  750. `<presence xmlns="jabber:client" to="dummy@localhost/resource" from="coven@chat.shakespeare.lit/fuvuv" xml:lang="en">
  751. <c xmlns="http://jabber.org/protocol/caps" node="http://jabber.pix-art.de" ver="5tOurnuFnp2h50hKafeUyeN4Yl8=" hash="sha-1"/>
  752. <x xmlns="vcard-temp:x:update"/>
  753. <x xmlns="http://jabber.org/protocol/muc#user">
  754. <item affiliation="none" jid="fuvuv@blabber.im/Pix-Art Messenger.8zoB" role="participant"/>
  755. </x>
  756. </presence>`);
  757. _converse.connection._dataRecv(test_utils.createRequest(presence));
  758. expect(sizzle('div.chat-info', chat_content).length).toBe(5);
  759. expect(sizzle('div.chat-info:last', chat_content).pop().textContent).toBe("fuvuv has entered the groupchat");
  760. presence = u.toStanza(
  761. `<presence xmlns="jabber:client" to="dummy@localhost/resource" type="unavailable" from="coven@chat.shakespeare.lit/fuvuv">
  762. <x xmlns="http://jabber.org/protocol/muc#user">
  763. <item affiliation="none" jid="fuvuv@blabber.im/Pix-Art Messenger.8zoB" role="none"/>
  764. </x>
  765. </presence>`);
  766. _converse.connection._dataRecv(test_utils.createRequest(presence));
  767. expect(sizzle('div.chat-info', chat_content).length).toBe(5);
  768. expect(sizzle('div.chat-info:last', chat_content).pop().textContent).toBe("fuvuv has entered and left the groupchat");
  769. presence = u.toStanza(
  770. `<presence xmlns="jabber:client" to="dummy@localhost/resource" type="unavailable" from="coven@chat.shakespeare.lit/fabio">
  771. <status>Disconnected: Replaced by new connection</status>
  772. <x xmlns="http://jabber.org/protocol/muc#user">
  773. <item affiliation="none" jid="fabio@montefuscolo.com.br/Conversations.ZvLu" role="none"/>
  774. </x>
  775. </presence>`);
  776. _converse.connection._dataRecv(test_utils.createRequest(presence));
  777. expect(sizzle('div.chat-info', chat_content).length).toBe(5);
  778. expect(sizzle('div.chat-info:last', chat_content).pop().textContent).toBe(
  779. `fabio has entered and left the groupchat. "Disconnected: Replaced by new connection"`);
  780. presence = u.toStanza(
  781. `<presence xmlns="jabber:client" to="dummy@localhost/resource" from="coven@chat.shakespeare.lit/fabio">
  782. <c xmlns="http://jabber.org/protocol/caps" node="http://conversations.im" ver="INI3xjRUioclBTP/aACfWi5m9UY=" hash="sha-1"/>
  783. <x xmlns="http://jabber.org/protocol/muc#user">
  784. <item affiliation="none" jid="fabio@montefuscolo.com.br/Conversations.ZvLu" role="participant"/>
  785. </x>
  786. </presence>`);
  787. _converse.connection._dataRecv(test_utils.createRequest(presence));
  788. expect(sizzle('div.chat-info', chat_content).length).toBe(5);
  789. expect(sizzle('div.chat-info:last', chat_content).pop().textContent).toBe(
  790. `fabio has entered the groupchat`);
  791. // XXX: hack so that we can test leave/enter of occupants
  792. // who were already in the room when we joined.
  793. chat_content.innerHTML = '';
  794. presence = u.toStanza(
  795. `<presence xmlns="jabber:client" to="dummy@localhost/resource" type="unavailable" from="coven@chat.shakespeare.lit/fabio">
  796. <status>Disconnected: closed</status>
  797. <x xmlns="http://jabber.org/protocol/muc#user">
  798. <item affiliation="none" jid="fabio@montefuscolo.com.br/Conversations.ZvLu" role="none"/>
  799. </x>
  800. </presence>`);
  801. _converse.connection._dataRecv(test_utils.createRequest(presence));
  802. expect(sizzle('div.chat-info', chat_content).length).toBe(1);
  803. expect(sizzle('div.chat-info:last', chat_content).pop().textContent).toBe(
  804. `fabio has left the groupchat. "Disconnected: closed"`);
  805. presence = u.toStanza(
  806. `<presence xmlns="jabber:client" to="dummy@localhost/resource" type="unavailable" from="coven@chat.shakespeare.lit/Dele Olajide">
  807. <x xmlns="http://jabber.org/protocol/muc#user">
  808. <item affiliation="none" jid="deleo@traderlynk.4ng.net/converse.js-74567907" role="none"/>
  809. </x>
  810. </presence>`);
  811. _converse.connection._dataRecv(test_utils.createRequest(presence));
  812. expect(sizzle('div.chat-info', chat_content).length).toBe(2);
  813. expect(sizzle('div.chat-info:last', chat_content).pop().textContent).toBe(
  814. `Dele Olajide has left the groupchat`);
  815. presence = u.toStanza(
  816. `<presence xmlns="jabber:client" to="dummy@localhost/resource" from="coven@chat.shakespeare.lit/fabio">
  817. <c xmlns="http://jabber.org/protocol/caps" node="http://conversations.im" ver="INI3xjRUioclBTP/aACfWi5m9UY=" hash="sha-1"/>
  818. <x xmlns="http://jabber.org/protocol/muc#user">
  819. <item affiliation="none" jid="fabio@montefuscolo.com.br/Conversations.ZvLu" role="participant"/>
  820. </x>
  821. </presence>`);
  822. _converse.connection._dataRecv(test_utils.createRequest(presence));
  823. expect(sizzle('div.chat-info', chat_content).length).toBe(2);
  824. expect(sizzle('div.chat-info:last', chat_content).pop().textContent).toBe(
  825. `fabio has left and re-entered the groupchat`);
  826. done();
  827. }));
  828. it("role-change messages that follow a MUC leave are left out",
  829. mock.initConverse(
  830. null, ['rosterGroupsFetched', 'chatBoxesFetched'], {},
  831. async function (done, _converse) {
  832. // See https://github.com/conversejs/converse.js/issues/1259
  833. await test_utils.openAndEnterChatRoom(_converse, 'conversations', 'conference.siacs.eu', 'dummy');
  834. const presence = $pres({
  835. to: 'dummy@localhost/resource',
  836. from: 'conversations@conference.siacs.eu/Guus'
  837. }).c('x', {
  838. 'xmlns': Strophe.NS.MUC_USER
  839. }).c('item', {
  840. 'affiliation': 'none',
  841. 'jid': 'Guus@localhost/xxx',
  842. 'role': 'visitor'
  843. });
  844. _converse.connection._dataRecv(test_utils.createRequest(presence));
  845. const view = _converse.chatboxviews.get('conversations@conference.siacs.eu');
  846. const msg = $msg({
  847. 'from': 'conversations@conference.siacs.eu/dummy',
  848. 'id': (new Date()).getTime(),
  849. 'to': 'dummy@localhost',
  850. 'type': 'groupchat'
  851. }).c('body').t('Some message').tree();
  852. await view.model.onMessage(msg);
  853. await new Promise((resolve, reject) => view.once('messageInserted', resolve));
  854. let stanza = u.toStanza(
  855. `<presence xmlns="jabber:client" to="dummy@localhost/resource" type="unavailable" from="conversations@conference.siacs.eu/Guus">
  856. <x xmlns="http://jabber.org/protocol/muc#user">
  857. <item affiliation="none" role="none"/>
  858. </x>
  859. </presence>`);
  860. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  861. stanza = u.toStanza(
  862. `<presence xmlns="jabber:client" to="dummy@localhost/resource" from="conversations@conference.siacs.eu/Guus">
  863. <c xmlns="http://jabber.org/protocol/caps" node="http://conversations.im" ver="ISg6+9AoK1/cwhbNEDviSvjdPzI=" hash="sha-1"/>
  864. <x xmlns="vcard-temp:x:update">
  865. <photo>bf987c486c51fbc05a6a4a9f20dd19b5efba3758</photo>
  866. </x>
  867. <x xmlns="http://jabber.org/protocol/muc#user">
  868. <item affiliation="none" role="visitor"/>
  869. </x>
  870. </presence>`);
  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.initConverse(
  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 = u.toStanza(`
  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>`);
  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 = u.toStanza(`
  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>`);
  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.initConverse(
  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. await 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.initConverse(
  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. const 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.initConverse(
  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.initConverse(
  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.initConverse(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.initConverse(
  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.initConverse(
  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.initConverse(
  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.initConverse(
  1563. null, ['rosterGroupsFetched', 'chatBoxesFetched'], {},
  1564. async function (done, _converse) {
  1565. await 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. const features = [
  1570. 'http://jabber.org/protocol/muc',
  1571. 'jabber:iq:register',
  1572. 'muc_passwordprotected',
  1573. 'muc_hidden',
  1574. 'muc_temporary',
  1575. 'muc_membersonly',
  1576. 'muc_unmoderated',
  1577. 'muc_anonymous'
  1578. ]
  1579. await test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy', features);
  1580. spyOn(_converse, 'emit').and.callThrough();
  1581. spyOn(window, 'prompt').and.callFake(() => "Please join!");
  1582. const view = _converse.chatboxviews.get('lounge@localhost');
  1583. const chat_area = view.el.querySelector('.chat-area');
  1584. expect(view.model.get('affiliation')).toBe('owner');
  1585. expect(view.model.features.get('open')).toBe(false);
  1586. expect(view.el.querySelectorAll('input.invited-contact').length).toBe(1);
  1587. view.model.set('affiliation', 'member');
  1588. await test_utils.waitUntil(() => view.el.querySelectorAll('input.invited-contact').length === 0);
  1589. view.model.features.set('open', 'true');
  1590. spyOn(view.model, 'directInvite').and.callThrough();
  1591. await test_utils.waitUntil(() => view.el.querySelectorAll('input.invited-contact').length);
  1592. const input = view.el.querySelector('input.invited-contact');
  1593. expect(input.getAttribute('placeholder')).toBe('Invite');
  1594. input.value = "Felix";
  1595. let evt = new Event('input');
  1596. input.dispatchEvent(evt);
  1597. let sent_stanza;
  1598. spyOn(_converse.connection, 'send').and.callFake(function (stanza) {
  1599. sent_stanza = stanza;
  1600. });
  1601. const hint = input.nextSibling.firstElementChild;
  1602. expect(input.value).toBe('Felix');
  1603. expect(hint.textContent).toBe('Felix Amsel');
  1604. expect(input.nextSibling.childNodes.length).toBe(1);
  1605. evt = new Event('mousedown', {'bubbles': true});
  1606. evt.button = 0; // For some reason awesomplete wants this
  1607. hint.dispatchEvent(evt);
  1608. expect(window.prompt).toHaveBeenCalled();
  1609. expect(view.model.directInvite).toHaveBeenCalled();
  1610. expect(sent_stanza.toLocaleString()).toBe(
  1611. `<message from="dummy@localhost/resource" `+
  1612. `id="${sent_stanza.nodeTree.getAttribute("id")}" `+
  1613. `to="felix.amsel@localhost" `+
  1614. `xmlns="jabber:client">`+
  1615. `<x jid="lounge@localhost" reason="Please join!" xmlns="jabber:x:conference"/>`+
  1616. `</message>`
  1617. );
  1618. done();
  1619. }));
  1620. it("can be joined automatically, based upon a received invite",
  1621. mock.initConverse(
  1622. null, ['rosterGroupsFetched'], {},
  1623. async function (done, _converse) {
  1624. const result = await test_utils.createContacts(_converse, 'current', 1);
  1625. spyOn(window, 'confirm').and.callFake(() => true);
  1626. await test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy');
  1627. const view = _converse.chatboxviews.get('lounge@localhost');
  1628. await view.close(); // Hack, otherwise we have to mock stanzas.
  1629. const name = mock.cur_names[0];
  1630. const from_jid = name.replace(/ /g,'.').toLowerCase() + '@localhost';
  1631. const room_jid = 'lounge@localhost';
  1632. const reason = "Please join this groupchat";
  1633. expect(_converse.chatboxes.models.length).toBe(1);
  1634. expect(_converse.chatboxes.models[0].id).toBe("controlbox");
  1635. await test_utils.waitUntil(() => _converse.roster.get(from_jid).get('fullname'));
  1636. const stanza = u.toStanza(`
  1637. <message xmlns="jabber:client" to="${_converse.bare_jid}" from="${from_jid}" id="9bceb415-f34b-4fa4-80d5-c0d076a24231">
  1638. <x xmlns="jabber:x:conference" jid="${room_jid}" reason="${reason}"/>
  1639. </message>`);
  1640. await _converse.onDirectMUCInvitation(stanza);
  1641. expect(window.confirm).toHaveBeenCalledWith(
  1642. name + ' has invited you to join a groupchat: '+ room_jid +
  1643. ', and left the following reason: "'+reason+'"');
  1644. expect(_converse.chatboxes.models.length).toBe(2);
  1645. expect(_converse.chatboxes.models[0].id).toBe('controlbox');
  1646. expect(_converse.chatboxes.models[1].id).toBe(room_jid);
  1647. done();
  1648. }));
  1649. it("shows received groupchat messages",
  1650. mock.initConverse(
  1651. null, ['rosterGroupsFetched'], {},
  1652. async function (done, _converse) {
  1653. const text = 'This is a received message';
  1654. await test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy');
  1655. spyOn(_converse, 'emit');
  1656. const view = _converse.chatboxviews.get('lounge@localhost');
  1657. if (!view.el.querySelectorAll('.chat-area').length) {
  1658. view.renderChatArea();
  1659. }
  1660. var nick = mock.chatroom_names[0];
  1661. view.model.occupants.create({
  1662. 'nick': nick,
  1663. 'muc_jid': `${view.model.get('jid')}/${nick}`
  1664. });
  1665. const message = $msg({
  1666. from: 'lounge@localhost/'+nick,
  1667. id: '1',
  1668. to: 'dummy@localhost',
  1669. type: 'groupchat'
  1670. }).c('body').t(text);
  1671. await view.model.onMessage(message.nodeTree);
  1672. await new Promise((resolve, reject) => view.once('messageInserted', resolve));
  1673. const chat_content = view.el.querySelector('.chat-content');
  1674. expect(chat_content.querySelectorAll('.chat-msg').length).toBe(1);
  1675. expect(chat_content.querySelector('.chat-msg__text').textContent).toBe(text);
  1676. expect(_converse.emit).toHaveBeenCalledWith('message', jasmine.any(Object));
  1677. done();
  1678. }));
  1679. it("shows sent groupchat messages",
  1680. mock.initConverse(
  1681. null, ['rosterGroupsFetched'], {},
  1682. async function (done, _converse) {
  1683. await test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy');
  1684. spyOn(_converse, 'emit');
  1685. const view = _converse.chatboxviews.get('lounge@localhost');
  1686. if (!view.el.querySelectorAll('.chat-area').length) {
  1687. view.renderChatArea();
  1688. }
  1689. const text = 'This is a sent message';
  1690. const textarea = view.el.querySelector('.chat-textarea');
  1691. textarea.value = text;
  1692. view.keyPressed({
  1693. target: textarea,
  1694. preventDefault: _.noop,
  1695. keyCode: 13
  1696. });
  1697. await new Promise((resolve, reject) => view.once('messageInserted', resolve));
  1698. expect(_converse.emit).toHaveBeenCalledWith('messageSend', text);
  1699. const chat_content = view.el.querySelector('.chat-content');
  1700. expect(chat_content.querySelectorAll('.chat-msg').length).toBe(1);
  1701. // Let's check that if we receive the same message again, it's
  1702. // not shown.
  1703. const stanza = u.toStanza(`
  1704. <message xmlns="jabber:client"
  1705. from="lounge@localhost/dummy"
  1706. to="${_converse.connection.jid}"
  1707. type="groupchat">
  1708. <body>${text}</body>
  1709. <stanza-id xmlns="urn:xmpp:sid:0"
  1710. id="5f3dbc5e-e1d3-4077-a492-693f3769c7ad"
  1711. by="lounge@localhost"/>
  1712. <origin-id xmlns="urn:xmpp:sid:0" id="${view.model.messages.at(0).get('origin_id')}"/>
  1713. </message>`);
  1714. await view.model.onMessage(stanza);
  1715. expect(chat_content.querySelectorAll('.chat-msg').length).toBe(1);
  1716. expect(sizzle('.chat-msg__text:last').pop().textContent).toBe(text);
  1717. expect(view.model.messages.length).toBe(1);
  1718. // We don't emit an event if it's our own message
  1719. expect(_converse.emit.calls.count(), 1);
  1720. done();
  1721. }));
  1722. it("will cause the chat area to be scrolled down only if it was at the bottom already",
  1723. mock.initConverse(
  1724. null, ['rosterGroupsFetched'], {},
  1725. async function (done, _converse) {
  1726. var message = 'This message is received while the chat area is scrolled up';
  1727. await test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy');
  1728. var view = _converse.chatboxviews.get('lounge@localhost');
  1729. spyOn(view, 'scrollDown').and.callThrough();
  1730. // Create enough messages so that there's a scrollbar.
  1731. const promises = [];
  1732. for (var i=0; i<20; i++) {
  1733. view.model.onMessage(
  1734. $msg({
  1735. from: 'lounge@localhost/someone',
  1736. to: 'dummy@localhost.com',
  1737. type: 'groupchat',
  1738. id: (new Date()).getTime(),
  1739. }).c('body').t('Message: '+i).tree());
  1740. promises.push(new Promise((resolve, reject) => view.once('messageInserted', resolve)))
  1741. }
  1742. await Promise.all(promises);
  1743. // Give enough time for `markScrolled` to have been called
  1744. setTimeout(async () => {
  1745. view.content.scrollTop = 0;
  1746. await view.model.onMessage(
  1747. $msg({
  1748. from: 'lounge@localhost/someone',
  1749. to: 'dummy@localhost.com',
  1750. type: 'groupchat',
  1751. id: (new Date()).getTime(),
  1752. }).c('body').t(message).tree());
  1753. await new Promise((resolve, reject) => view.once('messageInserted', resolve));
  1754. // Now check that the message appears inside the chatbox in the DOM
  1755. const chat_content = view.el.querySelector('.chat-content');
  1756. const msg_txt = sizzle('.chat-msg:last .chat-msg__text', chat_content).pop().textContent;
  1757. expect(msg_txt).toEqual(message);
  1758. expect(view.content.scrollTop).toBe(0);
  1759. done();
  1760. }, 500);
  1761. }));
  1762. it("shows the room topic in the header",
  1763. mock.initConverse(
  1764. null, ['rosterGroupsFetched'], {},
  1765. async function (done, _converse) {
  1766. await test_utils.openAndEnterChatRoom(_converse, 'jdev', 'conference.jabber.org', 'jc');
  1767. const text = 'Jabber/XMPP Development | RFCs and Extensions: http://xmpp.org/ | Protocol and XSF discussions: xsf@muc.xmpp.org';
  1768. let stanza = u.toStanza(`
  1769. <message xmlns="jabber:client" to="jc@opkode.com/_converse.js-60429116" type="groupchat" from="jdev@conference.jabber.org/ralphm">
  1770. <subject>${text}</subject>
  1771. <delay xmlns="urn:xmpp:delay" stamp="2014-02-04T09:35:39Z" from="jdev@conference.jabber.org"/>
  1772. <x xmlns="jabber:x:delay" stamp="20140204T09:35:39" from="jdev@conference.jabber.org"/>
  1773. </message>`);
  1774. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  1775. const view = _converse.chatboxviews.get('jdev@conference.jabber.org');
  1776. await new Promise((resolve, reject) => view.model.once('change:subject', resolve));
  1777. let chat_content = view.el.querySelector('.chat-content');
  1778. expect(sizzle('.chat-event:last').pop().textContent).toBe('Topic set by ralphm');
  1779. expect(sizzle('.chat-topic:last').pop().textContent).toBe(text);
  1780. expect(view.el.querySelector('.chatroom-description').textContent).toBe(text);
  1781. stanza = u.toStanza(
  1782. `<message xmlns="jabber:client" to="jc@opkode.com/_converse.js-60429116" type="groupchat" from="jdev@conference.jabber.org/ralphm">
  1783. <subject>This is a message subject</subject>
  1784. <body>This is a message</body>
  1785. </message>`);
  1786. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  1787. await new Promise((resolve, reject) => view.once('messageInserted', resolve));
  1788. chat_content = view.el.querySelector('.chat-content');
  1789. expect(sizzle('.chat-topic').length).toBe(1);
  1790. expect(sizzle('.chat-msg__subject').length).toBe(1);
  1791. expect(sizzle('.chat-msg__subject').pop().textContent).toBe('This is a message subject');
  1792. expect(sizzle('.chat-msg__text').length).toBe(1);
  1793. expect(sizzle('.chat-msg__text').pop().textContent).toBe('This is a message');
  1794. expect(view.el.querySelector('.chatroom-description').textContent).toBe(text);
  1795. done();
  1796. }));
  1797. it("escapes the subject before rendering it, to avoid JS-injection attacks",
  1798. mock.initConverse(
  1799. null, ['rosterGroupsFetched'], {},
  1800. async function (done, _converse) {
  1801. await test_utils.openAndEnterChatRoom(_converse, 'jdev', 'conference.jabber.org', 'jc');
  1802. spyOn(window, 'alert');
  1803. const subject = '<img src="x" onerror="alert(\'XSS\');"/>';
  1804. const view = _converse.chatboxviews.get('jdev@conference.jabber.org');
  1805. view.model.set({'subject': {
  1806. 'text': subject,
  1807. 'author': 'ralphm'
  1808. }});
  1809. const chat_content = view.el.querySelector('.chat-content');
  1810. expect(sizzle('.chat-event:last').pop().textContent).toBe('Topic set by ralphm');
  1811. expect(sizzle('.chat-topic:last').pop().textContent).toBe(subject);
  1812. done();
  1813. }));
  1814. it("informs users if their nicknames has been changed.",
  1815. mock.initConverse(
  1816. null, ['rosterGroupsFetched'], {},
  1817. async function (done, _converse) {
  1818. /* The service then sends two presence stanzas to the full JID
  1819. * of each occupant (including the occupant who is changing his
  1820. * or her room nickname), one of type "unavailable" for the old
  1821. * nickname and one indicating availability for the new
  1822. * nickname.
  1823. *
  1824. * See: http://xmpp.org/extensions/xep-0045.html#changenick
  1825. *
  1826. * <presence
  1827. * from='coven@localhost/thirdwitch'
  1828. * id='DC352437-C019-40EC-B590-AF29E879AF98'
  1829. * to='hag66@shakespeare.lit/pda'
  1830. * type='unavailable'>
  1831. * <x xmlns='http://jabber.org/protocol/muc#user'>
  1832. * <item affiliation='member'
  1833. * jid='hag66@shakespeare.lit/pda'
  1834. * nick='oldhag'
  1835. * role='participant'/>
  1836. * <status code='303'/>
  1837. * <status code='110'/>
  1838. * </x>
  1839. * </presence>
  1840. *
  1841. * <presence
  1842. * from='coven@localhost/oldhag'
  1843. * id='5B4F27A4-25ED-43F7-A699-382C6B4AFC67'
  1844. * to='hag66@shakespeare.lit/pda'>
  1845. * <x xmlns='http://jabber.org/protocol/muc#user'>
  1846. * <item affiliation='member'
  1847. * jid='hag66@shakespeare.lit/pda'
  1848. * role='participant'/>
  1849. * <status code='110'/>
  1850. * </x>
  1851. * </presence>
  1852. */
  1853. const __ = _converse.__;
  1854. await test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'oldnick');
  1855. const view = _converse.chatboxviews.get('lounge@localhost');
  1856. const chat_content = view.el.querySelector('.chat-content');
  1857. let occupants = view.el.querySelector('.occupant-list');
  1858. expect(occupants.childNodes.length).toBe(1);
  1859. expect(occupants.firstElementChild.querySelector('.occupant-nick').textContent.trim()).toBe("oldnick");
  1860. expect(chat_content.querySelectorAll('div.chat-info').length).toBe(1);
  1861. expect(sizzle('div.chat-info:first', chat_content).pop().textContent)
  1862. .toBe("oldnick has entered the groupchat");
  1863. let presence = $pres().attrs({
  1864. from:'lounge@localhost/oldnick',
  1865. id:'DC352437-C019-40EC-B590-AF29E879AF98',
  1866. to:'dummy@localhost/pda',
  1867. type:'unavailable'
  1868. })
  1869. .c('x').attrs({xmlns:'http://jabber.org/protocol/muc#user'})
  1870. .c('item').attrs({
  1871. affiliation: 'owner',
  1872. jid: 'dummy@localhost/pda',
  1873. nick: 'newnick',
  1874. role: 'moderator'
  1875. }).up()
  1876. .c('status').attrs({code:'303'}).up()
  1877. .c('status').attrs({code:'110'}).nodeTree;
  1878. _converse.connection._dataRecv(test_utils.createRequest(presence));
  1879. expect(chat_content.querySelectorAll('div.chat-info').length).toBe(2);
  1880. expect(sizzle('div.chat-info:last').pop().textContent).toBe(
  1881. __(_converse.muc.new_nickname_messages["303"], "newnick")
  1882. );
  1883. occupants = view.el.querySelector('.occupant-list');
  1884. expect(occupants.childNodes.length).toBe(1);
  1885. presence = $pres().attrs({
  1886. from:'lounge@localhost/newnick',
  1887. id:'5B4F27A4-25ED-43F7-A699-382C6B4AFC67',
  1888. to:'dummy@localhost/pda'
  1889. })
  1890. .c('x').attrs({xmlns:'http://jabber.org/protocol/muc#user'})
  1891. .c('item').attrs({
  1892. affiliation: 'owner',
  1893. jid: 'dummy@localhost/pda',
  1894. role: 'moderator'
  1895. }).up()
  1896. .c('status').attrs({code:'110'}).nodeTree;
  1897. _converse.connection._dataRecv(test_utils.createRequest(presence));
  1898. // XXX: currently we still have an additional "has entered the groupchat"
  1899. // notification for the new nickname. Ideally we'd not have
  1900. // that, but that's probably not possible without some
  1901. // significant refactoring.
  1902. expect(chat_content.querySelectorAll('div.chat-info').length).toBe(3);
  1903. expect(sizzle('div.chat-info', chat_content)[1].textContent).toBe(
  1904. __(_converse.muc.new_nickname_messages["303"], "newnick")
  1905. );
  1906. occupants = view.el.querySelector('.occupant-list');
  1907. expect(occupants.childNodes.length).toBe(1);
  1908. expect(sizzle('.occupant-nick:first', occupants).pop().textContent).toBe("newnick");
  1909. done();
  1910. }));
  1911. it("queries for the groupchat information before attempting to join the user",
  1912. mock.initConverse(
  1913. null, ['rosterGroupsFetched', 'chatBoxesFetched'], {},
  1914. async function (done, _converse) {
  1915. const IQ_stanzas = _converse.connection.IQ_stanzas;
  1916. const room_jid = 'coven@chat.shakespeare.lit';
  1917. await _converse.api.rooms.open(room_jid, {'nick': 'some1'});
  1918. const node = await test_utils.waitUntil(() => _.filter(
  1919. IQ_stanzas,
  1920. iq => iq.nodeTree.querySelector(
  1921. `iq[to="${room_jid}"] query[xmlns="http://jabber.org/protocol/disco#info"]`
  1922. )).pop());
  1923. // Check that the groupchat queried for the feautures.
  1924. const stanza = node.nodeTree;
  1925. expect(node.toLocaleString()).toBe(
  1926. `<iq from="dummy@localhost/resource" id="${stanza.getAttribute("id")}" to="${room_jid}" type="get" xmlns="jabber:client">`+
  1927. `<query xmlns="http://jabber.org/protocol/disco#info"/>`+
  1928. `</iq>`);
  1929. /* <iq from='coven@chat.shakespeare.lit'
  1930. * id='ik3vs715'
  1931. * to='hag66@shakespeare.lit/pda'
  1932. * type='result'>
  1933. * <query xmlns='http://jabber.org/protocol/disco#info'>
  1934. * <identity
  1935. * category='conference'
  1936. * name='A Dark Cave'
  1937. * type='text'/>
  1938. * <feature var='http://jabber.org/protocol/muc'/>
  1939. * <feature var='muc_passwordprotected'/>
  1940. * <feature var='muc_hidden'/>
  1941. * <feature var='muc_temporary'/>
  1942. * <feature var='muc_open'/>
  1943. * <feature var='muc_unmoderated'/>
  1944. * <feature var='muc_nonanonymous'/>
  1945. * </query>
  1946. * </iq>
  1947. */
  1948. const features_stanza = $iq({
  1949. 'from': room_jid,
  1950. 'id': stanza.getAttribute('id'),
  1951. 'to': 'dummy@localhost/desktop',
  1952. 'type': 'result'
  1953. })
  1954. .c('query', { 'xmlns': 'http://jabber.org/protocol/disco#info'})
  1955. .c('identity', {
  1956. 'category': 'conference',
  1957. 'name': 'A Dark Cave',
  1958. 'type': 'text'
  1959. }).up()
  1960. .c('feature', {'var': 'http://jabber.org/protocol/muc'}).up()
  1961. .c('feature', {'var': 'muc_passwordprotected'}).up()
  1962. .c('feature', {'var': 'muc_hidden'}).up()
  1963. .c('feature', {'var': 'muc_temporary'}).up()
  1964. .c('feature', {'var': 'muc_open'}).up()
  1965. .c('feature', {'var': 'muc_unmoderated'}).up()
  1966. .c('feature', {'var': 'muc_nonanonymous'});
  1967. _converse.connection._dataRecv(test_utils.createRequest(features_stanza));
  1968. let view = _converse.chatboxviews.get('coven@chat.shakespeare.lit');
  1969. await test_utils.waitUntil(() => (view.model.get('connection_status') === converse.ROOMSTATUS.CONNECTING));
  1970. view = _converse.chatboxviews.get('coven@chat.shakespeare.lit');
  1971. expect(view.model.features.get('fetched')).toBeTruthy();
  1972. expect(view.model.features.get('passwordprotected')).toBe(true);
  1973. expect(view.model.features.get('hidden')).toBe(true);
  1974. expect(view.model.features.get('temporary')).toBe(true);
  1975. expect(view.model.features.get('open')).toBe(true);
  1976. expect(view.model.features.get('unmoderated')).toBe(true);
  1977. expect(view.model.features.get('nonanonymous')).toBe(true);
  1978. done();
  1979. }));
  1980. it("updates the shown features when the groupchat configuration has changed",
  1981. mock.initConverse(
  1982. null, ['rosterGroupsFetched'], {'view_mode': 'fullscreen'},
  1983. async function (done, _converse) {
  1984. let features = [
  1985. 'http://jabber.org/protocol/muc',
  1986. 'jabber:iq:register',
  1987. 'muc_passwordprotected',
  1988. 'muc_publicroom',
  1989. 'muc_temporary',
  1990. 'muc_open',
  1991. 'muc_unmoderated',
  1992. 'muc_nonanonymous'
  1993. ];
  1994. await test_utils.openAndEnterChatRoom(_converse, 'room', 'conference.example.org', 'dummy', features);
  1995. const jid = 'room@conference.example.org';
  1996. const chatroomview = _converse.chatboxviews.get(jid);
  1997. let features_list = chatroomview.el.querySelector('.features-list');
  1998. let features_shown = features_list.textContent.split('\n').map(s => s.trim()).filter(s => s);
  1999. expect(_.difference(["Password protected", "Open", "Temporary", "Not anonymous", "Not moderated"], features_shown).length).toBe(0);
  2000. expect(chatroomview.model.features.get('hidden')).toBe(false);
  2001. expect(chatroomview.model.features.get('mam_enabled')).toBe(false);
  2002. expect(chatroomview.model.features.get('membersonly')).toBe(false);
  2003. expect(chatroomview.model.features.get('moderated')).toBe(false);
  2004. expect(chatroomview.model.features.get('nonanonymous')).toBe(true);
  2005. expect(chatroomview.model.features.get('open')).toBe(true);
  2006. expect(chatroomview.model.features.get('passwordprotected')).toBe(true);
  2007. expect(chatroomview.model.features.get('persistent')).toBe(false);
  2008. expect(chatroomview.model.features.get('publicroom')).toBe(true);
  2009. expect(chatroomview.model.features.get('semianonymous')).toBe(false);
  2010. expect(chatroomview.model.features.get('temporary')).toBe(true);
  2011. expect(chatroomview.model.features.get('unmoderated')).toBe(true);
  2012. expect(chatroomview.model.features.get('unsecured')).toBe(false);
  2013. expect(chatroomview.el.querySelector('.chat-title').textContent.trim()).toBe('Room');
  2014. chatroomview.el.querySelector('.configure-chatroom-button').click();
  2015. const IQs = _converse.connection.IQ_stanzas;
  2016. let iq = await test_utils.waitUntil(() => _.filter(
  2017. IQs,
  2018. iq => iq.nodeTree.querySelector(
  2019. `iq[to="${jid}"] query[xmlns="${Strophe.NS.MUC_OWNER}"]`
  2020. )).pop());
  2021. const response_el = u.toStanza(
  2022. `<iq xmlns="jabber:client"
  2023. type="result"
  2024. to="dummy@localhost/pda"
  2025. from="room@conference.example.org" id="${iq.nodeTree.getAttribute('id')}">
  2026. <query xmlns="http://jabber.org/protocol/muc#owner">
  2027. <x xmlns="jabber:x:data" type="form">
  2028. <title>Configuration for room@conference.example.org</title>
  2029. <instructions>Complete and submit this form to configure the room.</instructions>
  2030. <field var="FORM_TYPE" type="hidden">
  2031. <value>http://jabber.org/protocol/muc#roomconfig</value>
  2032. </field>
  2033. <field type="fixed">
  2034. <value>Room information</value>
  2035. </field>
  2036. <field var="muc#roomconfig_roomname" type="text-single" label="Title">
  2037. <value>Room</value>
  2038. </field>
  2039. <field var="muc#roomconfig_roomdesc" type="text-single" label="Description">
  2040. <desc>A brief description of the room</desc>
  2041. <value>This room is used in tests</value>
  2042. </field>
  2043. <field var="muc#roomconfig_lang" type="text-single" label="Language tag for room (e.g. 'en', 'de', 'fr' etc.)">
  2044. <desc>Indicate the primary language spoken in this room</desc>
  2045. <value>en</value>
  2046. </field>
  2047. <field var="muc#roomconfig_persistentroom" type="boolean" label="Persistent (room should remain even when it is empty)">
  2048. <desc>Rooms are automatically deleted when they are empty, unless this option is enabled</desc>
  2049. <value>1</value>
  2050. </field>
  2051. <field var="muc#roomconfig_publicroom" type="boolean" label="Include room information in public lists">
  2052. <desc>Enable this to allow people to find the room</desc>
  2053. <value>1</value>
  2054. </field>
  2055. <field type="fixed"><value>Access to the room</value></field>
  2056. <field var="muc#roomconfig_roomsecret" type="text-private" label="Password"><value/></field>
  2057. <field var="muc#roomconfig_membersonly" type="boolean" label="Only allow members to join">
  2058. <desc>Enable this to only allow access for room owners, admins and members</desc>
  2059. </field>
  2060. <field var="{http://prosody.im/protocol/muc}roomconfig_allowmemberinvites" type="boolean" label="Allow members to invite new members"/>
  2061. <field type="fixed"><value>Permissions in the room</value>
  2062. </field>
  2063. <field var="muc#roomconfig_changesubject" type="boolean" label="Allow anyone to set the room's subject">
  2064. <desc>Choose whether anyone, or only moderators, may set the room's subject</desc>
  2065. </field>
  2066. <field var="muc#roomconfig_moderatedroom" type="boolean" label="Moderated (require permission to speak)">
  2067. <desc>In moderated rooms occupants must be given permission to speak by a room moderator</desc>
  2068. </field>
  2069. <field var="muc#roomconfig_whois" type="list-single" label="Addresses (JIDs) of room occupants may be viewed by:">
  2070. <option label="Moderators only"><value>moderators</value></option>
  2071. <option label="Anyone"><value>anyone</value></option>
  2072. <value>anyone</value>
  2073. </field>
  2074. <field type="fixed"><value>Other options</value></field>
  2075. <field var="muc#roomconfig_historylength" type="text-single" label="Maximum number of history messages returned by room">
  2076. <desc>Specify the maximum number of previous messages that should be sent to users when they join the room</desc>
  2077. <value>50</value>
  2078. </field>
  2079. <field var="muc#roomconfig_defaulthistorymessages" type="text-single" label="Default number of history messages returned by room">
  2080. <desc>Specify the number of previous messages sent to new users when they join the room</desc>
  2081. <value>20</value>
  2082. </field>
  2083. </x>
  2084. </query>
  2085. </iq>`);
  2086. _converse.connection._dataRecv(test_utils.createRequest(response_el));
  2087. const el = await test_utils.waitUntil(() => document.querySelector('.chatroom-form legend'));
  2088. expect(el.textContent).toBe("Configuration for room@conference.example.org");
  2089. sizzle('[name="muc#roomconfig_membersonly"]', chatroomview.el).pop().click();
  2090. sizzle('[name="muc#roomconfig_roomname"]', chatroomview.el).pop().value = "New room name"
  2091. chatroomview.el.querySelector('.btn-primary').click();
  2092. iq = await test_utils.waitUntil(() => _.filter(IQs, iq => u.matchesSelector(iq.nodeTree, `iq[to="${jid}"][type="set"]`)).pop());
  2093. const result = $iq({
  2094. "xmlns": "jabber:client",
  2095. "type": "result",
  2096. "to": "dummy@localhost/resource",
  2097. "from": "lounge@muc.localhost",
  2098. "id": iq.nodeTree.getAttribute('id')
  2099. });
  2100. IQs.length = 0; // Empty the array
  2101. _converse.connection._dataRecv(test_utils.createRequest(result));
  2102. iq = await test_utils.waitUntil(() => _.filter(
  2103. IQs,
  2104. iq => iq.nodeTree.querySelector(
  2105. `iq[to="${jid}"] query[xmlns="http://jabber.org/protocol/disco#info"]`
  2106. )).pop());
  2107. const features_stanza = $iq({
  2108. 'from': jid,
  2109. 'id': iq.nodeTree.getAttribute('id'),
  2110. 'to': 'dummy@localhost/desktop',
  2111. 'type': 'result'
  2112. }).c('query', { 'xmlns': 'http://jabber.org/protocol/disco#info'})
  2113. .c('identity', {
  2114. 'category': 'conference',
  2115. 'name': 'New room name',
  2116. 'type': 'text'
  2117. }).up();
  2118. features = [
  2119. 'http://jabber.org/protocol/muc',
  2120. 'jabber:iq:register',
  2121. 'muc_passwordprotected',
  2122. 'muc_hidden',
  2123. 'muc_temporary',
  2124. 'muc_membersonly',
  2125. 'muc_unmoderated',
  2126. 'muc_nonanonymous'
  2127. ];
  2128. features.forEach(f => features_stanza.c('feature', {'var': f}).up());
  2129. features_stanza.c('x', { 'xmlns':'jabber:x:data', 'type':'result'})
  2130. .c('field', {'var':'FORM_TYPE', 'type':'hidden'})
  2131. .c('value').t('http://jabber.org/protocol/muc#roominfo').up().up()
  2132. .c('field', {'type':'text-single', 'var':'muc#roominfo_description', 'label':'Description'})
  2133. .c('value').t('This is the description').up().up()
  2134. .c('field', {'type':'text-single', 'var':'muc#roominfo_occupants', 'label':'Number of occupants'})
  2135. .c('value').t(0);
  2136. _converse.connection._dataRecv(test_utils.createRequest(features_stanza));
  2137. await test_utils.waitUntil(() => new Promise(success => chatroomview.model.features.on('change', success)));
  2138. features_list = chatroomview.el.querySelector('.features-list');
  2139. features_shown = features_list.textContent.split('\n').map(s => s.trim()).filter(s => s);
  2140. expect(_.difference(["Password protected", "Hidden", "Members only", "Temporary", "Not anonymous", "Not moderated"], features_shown).length).toBe(0);
  2141. expect(chatroomview.model.features.get('hidden')).toBe(true);
  2142. expect(chatroomview.model.features.get('mam_enabled')).toBe(false);
  2143. expect(chatroomview.model.features.get('membersonly')).toBe(true);
  2144. expect(chatroomview.model.features.get('moderated')).toBe(false);
  2145. expect(chatroomview.model.features.get('nonanonymous')).toBe(true);
  2146. expect(chatroomview.model.features.get('open')).toBe(false);
  2147. expect(chatroomview.model.features.get('passwordprotected')).toBe(true);
  2148. expect(chatroomview.model.features.get('persistent')).toBe(false);
  2149. expect(chatroomview.model.features.get('publicroom')).toBe(false);
  2150. expect(chatroomview.model.features.get('semianonymous')).toBe(false);
  2151. expect(chatroomview.model.features.get('temporary')).toBe(true);
  2152. expect(chatroomview.model.features.get('unmoderated')).toBe(true);
  2153. expect(chatroomview.model.features.get('unsecured')).toBe(false);
  2154. expect(chatroomview.el.querySelector('.chat-title').textContent.trim()).toBe('New room name');
  2155. done();
  2156. }));
  2157. it("indicates when a room is no longer anonymous",
  2158. mock.initConverse(
  2159. null, ['rosterGroupsFetched'], {},
  2160. async function (done, _converse) {
  2161. let sent_IQ, IQ_id;
  2162. const sendIQ = _converse.connection.sendIQ;
  2163. await test_utils.openAndEnterChatRoom(_converse, 'coven', 'chat.shakespeare.lit', 'some1');
  2164. spyOn(_converse.connection, 'sendIQ').and.callFake(function (iq, callback, errback) {
  2165. sent_IQ = iq;
  2166. IQ_id = sendIQ.bind(this)(iq, callback, errback);
  2167. });
  2168. // We pretend this is a new room, so no disco info is returned.
  2169. const features_stanza = $iq({
  2170. from: 'coven@chat.shakespeare.lit',
  2171. 'id': IQ_id,
  2172. 'to': 'dummy@localhost/desktop',
  2173. 'type': 'error'
  2174. }).c('error', {'type': 'cancel'})
  2175. .c('item-not-found', {'xmlns': "urn:ietf:params:xml:ns:xmpp-stanzas"});
  2176. _converse.connection._dataRecv(test_utils.createRequest(features_stanza));
  2177. const view = _converse.chatboxviews.get('coven@chat.shakespeare.lit');
  2178. /* <message xmlns="jabber:client"
  2179. * type="groupchat"
  2180. * to="dummy@localhost/_converse.js-27854181"
  2181. * from="coven@chat.shakespeare.lit">
  2182. * <x xmlns="http://jabber.org/protocol/muc#user">
  2183. * <status code="104"/>
  2184. * <status code="172"/>
  2185. * </x>
  2186. * </message>
  2187. */
  2188. const message = $msg({
  2189. type:'groupchat',
  2190. to: 'dummy@localhost/_converse.js-27854181',
  2191. from: 'coven@chat.shakespeare.lit'
  2192. }).c('x', {xmlns: Strophe.NS.MUC_USER})
  2193. .c('status', {code: '104'}).up()
  2194. .c('status', {code: '172'});
  2195. _converse.connection._dataRecv(test_utils.createRequest(message));
  2196. const chat_body = view.el.querySelector('.chatroom-body');
  2197. expect(sizzle('.message:last', chat_body).pop().textContent)
  2198. .toBe('This groupchat is now no longer anonymous');
  2199. done();
  2200. }));
  2201. it("informs users if they have been kicked out of the groupchat",
  2202. mock.initConverse(
  2203. null, ['rosterGroupsFetched'], {},
  2204. async function (done, _converse) {
  2205. /* <presence
  2206. * from='harfleur@chat.shakespeare.lit/pistol'
  2207. * to='pistol@shakespeare.lit/harfleur'
  2208. * type='unavailable'>
  2209. * <x xmlns='http://jabber.org/protocol/muc#user'>
  2210. * <item affiliation='none' role='none'>
  2211. * <actor nick='Fluellen'/>
  2212. * <reason>Avaunt, you cullion!</reason>
  2213. * </item>
  2214. * <status code='110'/>
  2215. * <status code='307'/>
  2216. * </x>
  2217. * </presence>
  2218. */
  2219. await test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy');
  2220. var presence = $pres().attrs({
  2221. from:'lounge@localhost/dummy',
  2222. to:'dummy@localhost/pda',
  2223. type:'unavailable'
  2224. })
  2225. .c('x').attrs({xmlns:'http://jabber.org/protocol/muc#user'})
  2226. .c('item').attrs({
  2227. affiliation: 'none',
  2228. jid: 'dummy@localhost/pda',
  2229. role: 'none'
  2230. })
  2231. .c('actor').attrs({nick: 'Fluellen'}).up()
  2232. .c('reason').t('Avaunt, you cullion!').up()
  2233. .up()
  2234. .c('status').attrs({code:'110'}).up()
  2235. .c('status').attrs({code:'307'}).nodeTree;
  2236. _converse.connection._dataRecv(test_utils.createRequest(presence));
  2237. const view = _converse.chatboxviews.get('lounge@localhost');
  2238. expect(u.isVisible(view.el.querySelector('.chat-area'))).toBeFalsy();
  2239. expect(u.isVisible(view.el.querySelector('.occupants'))).toBeFalsy();
  2240. const chat_body = view.el.querySelector('.chatroom-body');
  2241. expect(chat_body.querySelectorAll('.disconnect-msg').length).toBe(3);
  2242. expect(chat_body.querySelector('.disconnect-msg:first-child').textContent).toBe(
  2243. 'You have been kicked from this groupchat');
  2244. expect(chat_body.querySelector('.disconnect-msg:nth-child(2)').textContent).toBe(
  2245. 'This action was done by Fluellen.');
  2246. expect(chat_body.querySelector('.disconnect-msg:nth-child(3)').textContent).toBe(
  2247. 'The reason given is: "Avaunt, you cullion!".');
  2248. done();
  2249. }));
  2250. it("can be saved to, and retrieved from, browserStorage",
  2251. mock.initConverse(
  2252. null, ['rosterGroupsFetched', 'chatBoxesFetched'], {},
  2253. async function (done, _converse) {
  2254. await test_utils.openChatRoom(_converse, 'lounge', 'localhost', 'dummy');
  2255. // We instantiate a new ChatBoxes collection, which by default
  2256. // will be empty.
  2257. test_utils.openControlBox();
  2258. const newchatboxes = new _converse.ChatBoxes();
  2259. expect(newchatboxes.length).toEqual(0);
  2260. // The chatboxes will then be fetched from browserStorage inside the
  2261. // onConnected method
  2262. newchatboxes.onConnected();
  2263. await new Promise((resolve, reject) => _converse.api.listen.once('chatBoxesFetched', resolve));
  2264. expect(newchatboxes.length).toEqual(2);
  2265. // Check that the chatrooms retrieved from browserStorage
  2266. // have the same attributes values as the original ones.
  2267. const attrs = ['id', 'box_id', 'visible'];
  2268. let new_attrs, old_attrs;
  2269. for (var i=0; i<attrs.length; i++) {
  2270. new_attrs = _.map(_.map(newchatboxes.models, 'attributes'), attrs[i]);
  2271. old_attrs = _.map(_.map(_converse.chatboxes.models, 'attributes'), attrs[i]);
  2272. // FIXME: should have have to sort here? Order must
  2273. // probably be the same...
  2274. // This should be fixed once the controlbox always opens
  2275. // only on the right.
  2276. expect(_.isEqual(new_attrs.sort(), old_attrs.sort())).toEqual(true);
  2277. }
  2278. _converse.rosterview.render();
  2279. done();
  2280. }));
  2281. it("can be minimized by clicking a DOM element with class 'toggle-chatbox-button'",
  2282. mock.initConverse(
  2283. null, ['rosterGroupsFetched', 'chatBoxesFetched'], {},
  2284. async function (done, _converse) {
  2285. await test_utils.openChatRoom(_converse, 'lounge', 'localhost', 'dummy');
  2286. const view = _converse.chatboxviews.get('lounge@localhost'),
  2287. trimmed_chatboxes = _converse.minimized_chats;
  2288. spyOn(view, 'minimize').and.callThrough();
  2289. spyOn(view, 'maximize').and.callThrough();
  2290. spyOn(_converse, 'emit');
  2291. view.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  2292. view.el.querySelector('.toggle-chatbox-button').click();
  2293. expect(view.minimize).toHaveBeenCalled();
  2294. expect(_converse.emit).toHaveBeenCalledWith('chatBoxMinimized', jasmine.any(Object));
  2295. expect(u.isVisible(view.el)).toBeFalsy();
  2296. expect(view.model.get('minimized')).toBeTruthy();
  2297. expect(view.minimize).toHaveBeenCalled();
  2298. await test_utils.waitUntil(() => trimmed_chatboxes.get(view.model.get('id')));
  2299. const trimmedview = trimmed_chatboxes.get(view.model.get('id'));
  2300. trimmedview.el.querySelector("a.restore-chat").click();
  2301. expect(view.maximize).toHaveBeenCalled();
  2302. expect(_converse.emit).toHaveBeenCalledWith('chatBoxMaximized', jasmine.any(Object));
  2303. expect(view.model.get('minimized')).toBeFalsy();
  2304. expect(_converse.emit.calls.count(), 3);
  2305. done();
  2306. }));
  2307. it("can be closed again by clicking a DOM element with class 'close-chatbox-button'",
  2308. mock.initConverse(
  2309. null, ['rosterGroupsFetched', 'chatBoxesFetched'], {},
  2310. async function (done, _converse) {
  2311. await test_utils.openChatRoom(_converse, 'lounge', 'localhost', 'dummy');
  2312. const view = _converse.chatboxviews.get('lounge@localhost');
  2313. spyOn(view, 'close').and.callThrough();
  2314. spyOn(_converse, 'emit');
  2315. spyOn(view.model, 'leave');
  2316. view.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  2317. view.el.querySelector('.close-chatbox-button').click();
  2318. expect(view.close).toHaveBeenCalled();
  2319. expect(view.model.leave).toHaveBeenCalled();
  2320. await test_utils.waitUntil(() => _converse.emit.calls.count());
  2321. expect(_converse.emit).toHaveBeenCalledWith('chatBoxClosed', jasmine.any(Object));
  2322. done();
  2323. }));
  2324. });
  2325. describe("Each chat groupchat can take special commands", function () {
  2326. it("takes /help to show the available commands",
  2327. mock.initConverse(
  2328. null, ['rosterGroupsFetched'], {},
  2329. async function (done, _converse) {
  2330. await test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy');
  2331. const view = _converse.chatboxviews.get('lounge@localhost');
  2332. var textarea = view.el.querySelector('.chat-textarea');
  2333. textarea.value = '/help This is the groupchat subject';
  2334. view.keyPressed({
  2335. target: textarea,
  2336. preventDefault: _.noop,
  2337. keyCode: 13
  2338. });
  2339. const info_messages = Array.prototype.slice.call(view.el.querySelectorAll('.chat-info'), 0);
  2340. expect(info_messages.length).toBe(19);
  2341. expect(info_messages.pop().textContent).toBe('/voice: Allow muted user to post messages');
  2342. expect(info_messages.pop().textContent).toBe('/topic: Set groupchat subject (alias for /subject)');
  2343. expect(info_messages.pop().textContent).toBe('/subject: Set groupchat subject');
  2344. expect(info_messages.pop().textContent).toBe('/revoke: Revoke user\'s membership');
  2345. expect(info_messages.pop().textContent).toBe('/register: Register a nickname for this groupchat');
  2346. expect(info_messages.pop().textContent).toBe('/owner: Grant ownership of this groupchat');
  2347. expect(info_messages.pop().textContent).toBe('/op: Grant moderator role to user');
  2348. expect(info_messages.pop().textContent).toBe('/nick: Change your nickname');
  2349. expect(info_messages.pop().textContent).toBe('/mute: Remove user\'s ability to post messages');
  2350. expect(info_messages.pop().textContent).toBe('/member: Grant membership to a user');
  2351. expect(info_messages.pop().textContent).toBe('/me: Write in 3rd person');
  2352. expect(info_messages.pop().textContent).toBe('/kick: Kick user from groupchat');
  2353. expect(info_messages.pop().textContent).toBe('/help: Show this menu');
  2354. expect(info_messages.pop().textContent).toBe('/destroy: Remove this groupchat');
  2355. expect(info_messages.pop().textContent).toBe('/deop: Change user role to participant');
  2356. expect(info_messages.pop().textContent).toBe('/clear: Remove messages');
  2357. expect(info_messages.pop().textContent).toBe('/ban: Ban user from groupchat');
  2358. expect(info_messages.pop().textContent).toBe('/admin: Change user\'s affiliation to admin');
  2359. done();
  2360. }));
  2361. it("takes /help to show the available commands and commands can be disabled by config",
  2362. mock.initConverse(
  2363. null, ['rosterGroupsFetched'], {muc_disable_moderator_commands: ['mute', 'voice']},
  2364. async function (done, _converse) {
  2365. await test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy');
  2366. const view = _converse.chatboxviews.get('lounge@localhost');
  2367. var textarea = view.el.querySelector('.chat-textarea');
  2368. textarea.value = '/help This is the groupchat subject';
  2369. view.keyPressed({
  2370. target: textarea,
  2371. preventDefault: _.noop,
  2372. keyCode: 13
  2373. });
  2374. const info_messages = Array.prototype.slice.call(view.el.querySelectorAll('.chat-info'), 0);
  2375. expect(info_messages.length).toBe(17);
  2376. expect(info_messages.pop().textContent).toBe('/topic: Set groupchat subject (alias for /subject)');
  2377. expect(info_messages.pop().textContent).toBe('/subject: Set groupchat subject');
  2378. expect(info_messages.pop().textContent).toBe('/revoke: Revoke user\'s membership');
  2379. expect(info_messages.pop().textContent).toBe('/register: Register a nickname for this groupchat');
  2380. expect(info_messages.pop().textContent).toBe('/owner: Grant ownership of this groupchat');
  2381. expect(info_messages.pop().textContent).toBe('/op: Grant moderator role to user');
  2382. expect(info_messages.pop().textContent).toBe('/nick: Change your nickname');
  2383. expect(info_messages.pop().textContent).toBe('/member: Grant membership to a user');
  2384. expect(info_messages.pop().textContent).toBe('/me: Write in 3rd person');
  2385. expect(info_messages.pop().textContent).toBe('/kick: Kick user from groupchat');
  2386. expect(info_messages.pop().textContent).toBe('/help: Show this menu');
  2387. expect(info_messages.pop().textContent).toBe('/destroy: Remove this groupchat');
  2388. expect(info_messages.pop().textContent).toBe('/deop: Change user role to participant');
  2389. expect(info_messages.pop().textContent).toBe('/clear: Remove messages');
  2390. expect(info_messages.pop().textContent).toBe('/ban: Ban user from groupchat');
  2391. expect(info_messages.pop().textContent).toBe('/admin: Change user\'s affiliation to admin');
  2392. done();
  2393. }));
  2394. it("takes /member to make an occupant a member",
  2395. mock.initConverse(
  2396. null, ['rosterGroupsFetched'], {},
  2397. async function (done, _converse) {
  2398. let iq_stanza;
  2399. await test_utils.openAndEnterChatRoom(_converse, 'lounge', 'muc.localhost', 'dummy');
  2400. const view = _converse.chatboxviews.get('lounge@muc.localhost');
  2401. /* We don't show join/leave messages for existing occupants. We
  2402. * know about them because we receive their presences before we
  2403. * receive our own.
  2404. */
  2405. const presence = $pres({
  2406. to: 'dummy@localhost/resource',
  2407. from: 'lounge@muc.localhost/marc'
  2408. }).c('x', {xmlns: Strophe.NS.MUC_USER})
  2409. .c('item', {
  2410. 'affiliation': 'none',
  2411. 'jid': 'marc@localhost/_converse.js-290929789',
  2412. 'role': 'participant'
  2413. });
  2414. _converse.connection._dataRecv(test_utils.createRequest(presence));
  2415. expect(view.model.occupants.length).toBe(2);
  2416. const textarea = view.el.querySelector('.chat-textarea');
  2417. let sent_stanza;
  2418. spyOn(_converse.connection, 'send').and.callFake((stanza) => {
  2419. sent_stanza = stanza;
  2420. });
  2421. // First check that an error message appears when a
  2422. // non-existent nick is used.
  2423. textarea.value = '/member chris Welcome to the club!';
  2424. view.keyPressed({
  2425. target: textarea,
  2426. preventDefault: _.noop,
  2427. keyCode: 13
  2428. });
  2429. expect(_converse.connection.send).not.toHaveBeenCalled();
  2430. expect(view.el.querySelectorAll('.chat-error').length).toBe(1);
  2431. expect(view.el.querySelector('.chat-error').textContent.trim())
  2432. .toBe(`Error: couldn't find a groupchat participant "chris"`)
  2433. // Now test with an existing nick
  2434. textarea.value = '/member marc Welcome to the club!';
  2435. view.keyPressed({
  2436. target: textarea,
  2437. preventDefault: _.noop,
  2438. keyCode: 13
  2439. });
  2440. expect(_converse.connection.send).toHaveBeenCalled();
  2441. expect(Strophe.serialize(sent_stanza)).toBe(
  2442. `<iq id="${sent_stanza.getAttribute('id')}" to="lounge@muc.localhost" type="set" xmlns="jabber:client">`+
  2443. `<query xmlns="http://jabber.org/protocol/muc#admin">`+
  2444. `<item affiliation="member" jid="marc@localhost">`+
  2445. `<reason>Welcome to the club!</reason>`+
  2446. `</item>`+
  2447. `</query>`+
  2448. `</iq>`);
  2449. let result = $iq({
  2450. "xmlns": "jabber:client",
  2451. "type": "result",
  2452. "to": "dummy@localhost/resource",
  2453. "from": "lounge@muc.localhost",
  2454. "id": sent_stanza.getAttribute('id')
  2455. });
  2456. _converse.connection.IQ_stanzas = [];
  2457. _converse.connection._dataRecv(test_utils.createRequest(result));
  2458. let node = await test_utils.waitUntil(() => _.filter(
  2459. _converse.connection.IQ_stanzas,
  2460. iq => iq.nodeTree.querySelector('iq[to="lounge@muc.localhost"][type="get"] item[affiliation="member"]')).pop()
  2461. );
  2462. iq_stanza = node.nodeTree;
  2463. expect(node.toLocaleString()).toBe(
  2464. `<iq id="${iq_stanza.getAttribute('id')}" to="lounge@muc.localhost" type="get" xmlns="jabber:client">`+
  2465. `<query xmlns="http://jabber.org/protocol/muc#admin">`+
  2466. `<item affiliation="member"/>`+
  2467. `</query>`+
  2468. `</iq>`)
  2469. expect(view.model.occupants.length).toBe(2);
  2470. result = $iq({
  2471. "xmlns": "jabber:client",
  2472. "type": "result",
  2473. "to": "dummy@localhost/resource",
  2474. "from": "lounge@muc.localhost",
  2475. "id": iq_stanza.getAttribute("id")
  2476. }).c("query", {"xmlns": "http://jabber.org/protocol/muc#admin"})
  2477. .c("item", {"jid": "marc", "affiliation": "member"});
  2478. _converse.connection._dataRecv(test_utils.createRequest(result));
  2479. expect(view.model.occupants.length).toBe(2);
  2480. node = await test_utils.waitUntil(() => _.filter(
  2481. _converse.connection.IQ_stanzas,
  2482. (iq) => iq.nodeTree.querySelector('iq[to="lounge@muc.localhost"][type="get"] item[affiliation="owner"]')).pop()
  2483. );
  2484. iq_stanza = node.nodeTree;
  2485. expect(node.toLocaleString()).toBe(
  2486. `<iq id="${iq_stanza.getAttribute('id')}" to="lounge@muc.localhost" type="get" xmlns="jabber:client">`+
  2487. `<query xmlns="http://jabber.org/protocol/muc#admin">`+
  2488. `<item affiliation="owner"/>`+
  2489. `</query>`+
  2490. `</iq>`)
  2491. expect(view.model.occupants.length).toBe(2);
  2492. result = $iq({
  2493. "xmlns": "jabber:client",
  2494. "type": "result",
  2495. "to": "dummy@localhost/resource",
  2496. "from": "lounge@muc.localhost",
  2497. "id": iq_stanza.getAttribute("id")
  2498. }).c("query", {"xmlns": "http://jabber.org/protocol/muc#admin"})
  2499. .c("item", {"jid": "dummy@localhost", "affiliation": "owner"});
  2500. _converse.connection._dataRecv(test_utils.createRequest(result));
  2501. expect(view.model.occupants.length).toBe(2);
  2502. node = await test_utils.waitUntil(() => _.filter(
  2503. _converse.connection.IQ_stanzas,
  2504. (iq) => iq.nodeTree.querySelector('iq[to="lounge@muc.localhost"][type="get"] item[affiliation="admin"]')).pop()
  2505. );
  2506. iq_stanza = node.nodeTree;
  2507. expect(node.toLocaleString()).toBe(
  2508. `<iq id="${iq_stanza.getAttribute('id')}" to="lounge@muc.localhost" type="get" xmlns="jabber:client">`+
  2509. `<query xmlns="http://jabber.org/protocol/muc#admin">`+
  2510. `<item affiliation="admin"/>`+
  2511. `</query>`+
  2512. `</iq>`)
  2513. expect(view.model.occupants.length).toBe(2);
  2514. result = $iq({
  2515. "xmlns": "jabber:client",
  2516. "type": "result",
  2517. "to": "dummy@localhost/resource",
  2518. "from": "lounge@muc.localhost",
  2519. "id": iq_stanza.getAttribute("id")
  2520. }).c("query", {"xmlns": "http://jabber.org/protocol/muc#admin"})
  2521. _converse.connection._dataRecv(test_utils.createRequest(result));
  2522. await test_utils.waitUntil(() => view.el.querySelectorAll('.badge').length > 1);
  2523. expect(view.model.occupants.length).toBe(2);
  2524. expect(view.el.querySelectorAll('.occupant').length).toBe(2);
  2525. done();
  2526. }));
  2527. it("takes /topic to set the groupchat topic",
  2528. mock.initConverse(
  2529. null, ['rosterGroupsFetched'], {},
  2530. async function (done, _converse) {
  2531. await test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy');
  2532. const view = _converse.chatboxviews.get('lounge@localhost');
  2533. spyOn(view, 'clearMessages');
  2534. let sent_stanza;
  2535. spyOn(_converse.connection, 'send').and.callFake(function (stanza) {
  2536. sent_stanza = stanza;
  2537. });
  2538. // Check the alias /topic
  2539. const textarea = view.el.querySelector('.chat-textarea');
  2540. textarea.value = '/topic This is the groupchat subject';
  2541. view.keyPressed({
  2542. target: textarea,
  2543. preventDefault: _.noop,
  2544. keyCode: 13
  2545. });
  2546. expect(_converse.connection.send).toHaveBeenCalled();
  2547. expect(sent_stanza.textContent).toBe('This is the groupchat subject');
  2548. // Check /subject
  2549. textarea.value = '/subject This is a new subject';
  2550. view.keyPressed({
  2551. target: textarea,
  2552. preventDefault: _.noop,
  2553. keyCode: 13
  2554. });
  2555. expect(sent_stanza.textContent).toBe('This is a new subject');
  2556. expect(Strophe.serialize(sent_stanza).toLocaleString()).toBe(
  2557. '<message from="dummy@localhost/resource" to="lounge@localhost" type="groupchat" xmlns="jabber:client">'+
  2558. '<subject xmlns="jabber:client">This is a new subject</subject>'+
  2559. '</message>');
  2560. // Check case insensitivity
  2561. textarea.value = '/Subject This is yet another subject';
  2562. view.keyPressed({
  2563. target: textarea,
  2564. preventDefault: _.noop,
  2565. keyCode: 13
  2566. });
  2567. expect(sent_stanza.textContent).toBe('This is yet another subject');
  2568. expect(Strophe.serialize(sent_stanza).toLocaleString()).toBe(
  2569. '<message from="dummy@localhost/resource" to="lounge@localhost" type="groupchat" xmlns="jabber:client">'+
  2570. '<subject xmlns="jabber:client">This is yet another subject</subject>'+
  2571. '</message>');
  2572. done();
  2573. }));
  2574. it("takes /clear to clear messages",
  2575. mock.initConverse(
  2576. null, ['rosterGroupsFetched'], {},
  2577. async function (done, _converse) {
  2578. await test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy');
  2579. const view = _converse.chatboxviews.get('lounge@localhost');
  2580. spyOn(view, 'clearMessages');
  2581. const textarea = view.el.querySelector('.chat-textarea')
  2582. textarea.value = '/clear';
  2583. view.keyPressed({
  2584. target: textarea,
  2585. preventDefault: _.noop,
  2586. keyCode: 13
  2587. });
  2588. expect(view.clearMessages).toHaveBeenCalled();
  2589. done();
  2590. }));
  2591. it("takes /owner to make a user an owner",
  2592. mock.initConverse(
  2593. null, ['rosterGroupsFetched'], {},
  2594. async function (done, _converse) {
  2595. let sent_IQ, IQ_id;
  2596. const sendIQ = _converse.connection.sendIQ;
  2597. spyOn(_converse.connection, 'sendIQ').and.callFake(function (iq, callback, errback) {
  2598. sent_IQ = iq;
  2599. IQ_id = sendIQ.bind(this)(iq, callback, errback);
  2600. });
  2601. await test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy');
  2602. const view = _converse.chatboxviews.get('lounge@localhost');
  2603. spyOn(view.model, 'setAffiliation').and.callThrough();
  2604. spyOn(view, 'showErrorMessage').and.callThrough();
  2605. spyOn(view, 'validateRoleChangeCommand').and.callThrough();
  2606. let presence = $pres({
  2607. 'from': 'lounge@localhost/annoyingGuy',
  2608. 'id':'27C55F89-1C6A-459A-9EB5-77690145D624',
  2609. 'to': 'dummy@localhost/desktop'
  2610. })
  2611. .c('x', { 'xmlns': 'http://jabber.org/protocol/muc#user'})
  2612. .c('item', {
  2613. 'jid': 'annoyingguy@localhost',
  2614. 'affiliation': 'member',
  2615. 'role': 'participant'
  2616. });
  2617. _converse.connection._dataRecv(test_utils.createRequest(presence));
  2618. var textarea = view.el.querySelector('.chat-textarea')
  2619. textarea.value = '/owner';
  2620. view.keyPressed({
  2621. target: textarea,
  2622. preventDefault: _.noop,
  2623. keyCode: 13
  2624. });
  2625. expect(view.validateRoleChangeCommand).toHaveBeenCalled();
  2626. expect(view.showErrorMessage).toHaveBeenCalledWith(
  2627. "Error: the \"owner\" command takes two arguments, the user's nickname and optionally a reason.");
  2628. expect(view.model.setAffiliation).not.toHaveBeenCalled();
  2629. // XXX: Calling onFormSubmitted directly, trying
  2630. // again via triggering Event doesn't work for some weird
  2631. // reason.
  2632. textarea.value = '/owner nobody You\'re responsible';
  2633. view.onFormSubmitted(new Event('submit'));
  2634. expect(view.showErrorMessage).toHaveBeenCalledWith(
  2635. 'Error: couldn\'t find a groupchat participant "nobody"');
  2636. expect(view.model.setAffiliation).not.toHaveBeenCalled();
  2637. // Call now with the correct of arguments.
  2638. // XXX: Calling onFormSubmitted directly, trying
  2639. // again via triggering Event doesn't work for some weird
  2640. // reason.
  2641. textarea.value = '/owner annoyingGuy You\'re responsible';
  2642. view.onFormSubmitted(new Event('submit'));
  2643. expect(view.validateRoleChangeCommand.calls.count()).toBe(3);
  2644. expect(view.model.setAffiliation).toHaveBeenCalled();
  2645. expect(view.showErrorMessage.calls.count()).toBe(2);
  2646. // Check that the member list now gets updated
  2647. expect(sent_IQ.toLocaleString()).toBe(
  2648. `<iq id="${IQ_id}" to="lounge@localhost" type="set" xmlns="jabber:client">`+
  2649. `<query xmlns="http://jabber.org/protocol/muc#admin">`+
  2650. `<item affiliation="owner" jid="annoyingGuy">`+
  2651. `<reason>You&apos;re responsible</reason>`+
  2652. `</item>`+
  2653. `</query>`+
  2654. `</iq>`);
  2655. presence = $pres({
  2656. 'from': 'lounge@localhost/annoyingGuy',
  2657. 'id':'27C55F89-1C6A-459A-9EB5-77690145D628',
  2658. 'to': 'dummy@localhost/desktop'
  2659. })
  2660. .c('x', { 'xmlns': 'http://jabber.org/protocol/muc#user'})
  2661. .c('item', {
  2662. 'jid': 'annoyingguy@localhost',
  2663. 'affiliation': 'owner',
  2664. 'role': 'participant'
  2665. });
  2666. _converse.connection._dataRecv(test_utils.createRequest(presence));
  2667. expect(view.el.querySelectorAll('.chat-info')[4].textContent).toBe("annoyingGuy is now an owner of this groupchat");
  2668. done();
  2669. }));
  2670. it("takes /ban to ban a user",
  2671. mock.initConverse(
  2672. null, ['rosterGroupsFetched'], {},
  2673. async function (done, _converse) {
  2674. let sent_IQ, IQ_id;
  2675. const sendIQ = _converse.connection.sendIQ;
  2676. spyOn(_converse.connection, 'sendIQ').and.callFake(function (iq, callback, errback) {
  2677. sent_IQ = iq;
  2678. IQ_id = sendIQ.bind(this)(iq, callback, errback);
  2679. });
  2680. await test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy');
  2681. const view = _converse.chatboxviews.get('lounge@localhost');
  2682. spyOn(view.model, 'setAffiliation').and.callThrough();
  2683. spyOn(view, 'showErrorMessage').and.callThrough();
  2684. spyOn(view, 'validateRoleChangeCommand').and.callThrough();
  2685. let presence = $pres({
  2686. 'from': 'lounge@localhost/annoyingGuy',
  2687. 'id':'27C55F89-1C6A-459A-9EB5-77690145D624',
  2688. 'to': 'dummy@localhost/desktop'
  2689. })
  2690. .c('x', { 'xmlns': 'http://jabber.org/protocol/muc#user'})
  2691. .c('item', {
  2692. 'jid': 'annoyingguy@localhost',
  2693. 'affiliation': 'member',
  2694. 'role': 'participant'
  2695. });
  2696. _converse.connection._dataRecv(test_utils.createRequest(presence));
  2697. const textarea = view.el.querySelector('.chat-textarea')
  2698. textarea.value = '/ban';
  2699. view.keyPressed({
  2700. target: textarea,
  2701. preventDefault: _.noop,
  2702. keyCode: 13
  2703. });
  2704. expect(view.validateRoleChangeCommand).toHaveBeenCalled();
  2705. expect(view.showErrorMessage).toHaveBeenCalledWith(
  2706. "Error: the \"ban\" command takes two arguments, the user's nickname and optionally a reason.");
  2707. expect(view.model.setAffiliation).not.toHaveBeenCalled();
  2708. // Call now with the correct amount of arguments.
  2709. // XXX: Calling onFormSubmitted directly, trying
  2710. // again via triggering Event doesn't work for some weird
  2711. // reason.
  2712. textarea.value = '/ban annoyingGuy You\'re annoying';
  2713. view.onFormSubmitted(new Event('submit'));
  2714. expect(view.validateRoleChangeCommand.calls.count()).toBe(2);
  2715. expect(view.showErrorMessage.calls.count()).toBe(1);
  2716. expect(view.model.setAffiliation).toHaveBeenCalled();
  2717. // Check that the member list now gets updated
  2718. expect(sent_IQ.toLocaleString()).toBe(
  2719. `<iq id="${IQ_id}" to="lounge@localhost" type="set" xmlns="jabber:client">`+
  2720. `<query xmlns="http://jabber.org/protocol/muc#admin">`+
  2721. `<item affiliation="outcast" jid="annoyingGuy">`+
  2722. `<reason>You&apos;re annoying</reason>`+
  2723. `</item>`+
  2724. `</query>`+
  2725. `</iq>`);
  2726. presence = $pres({
  2727. 'from': 'lounge@localhost/annoyingGuy',
  2728. 'id':'27C55F89-1C6A-459A-9EB5-77690145D628',
  2729. 'to': 'dummy@localhost/desktop'
  2730. })
  2731. .c('x', { 'xmlns': 'http://jabber.org/protocol/muc#user'})
  2732. .c('item', {
  2733. 'jid': 'annoyingguy@localhost',
  2734. 'affiliation': 'outcast',
  2735. 'role': 'participant'
  2736. });
  2737. _converse.connection._dataRecv(test_utils.createRequest(presence));
  2738. expect(
  2739. view.el.querySelectorAll('.chat-info')[3].textContent).toBe(
  2740. "annoyingGuy has been banned from this groupchat");
  2741. done();
  2742. }));
  2743. it("takes /kick to kick a user",
  2744. mock.initConverse(
  2745. null, ['rosterGroupsFetched'], {},
  2746. async function (done, _converse) {
  2747. let sent_IQ, IQ_id;
  2748. const sendIQ = _converse.connection.sendIQ;
  2749. spyOn(_converse.connection, 'sendIQ').and.callFake(function (iq, callback, errback) {
  2750. sent_IQ = iq;
  2751. IQ_id = sendIQ.bind(this)(iq, callback, errback);
  2752. });
  2753. await test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy');
  2754. const view = _converse.chatboxviews.get('lounge@localhost');
  2755. spyOn(view, 'modifyRole').and.callThrough();
  2756. spyOn(view, 'showErrorMessage').and.callThrough();
  2757. spyOn(view, 'validateRoleChangeCommand').and.callThrough();
  2758. let presence = $pres({
  2759. 'from': 'lounge@localhost/annoyingGuy',
  2760. 'id':'27C55F89-1C6A-459A-9EB5-77690145D624',
  2761. 'to': 'dummy@localhost/desktop'
  2762. })
  2763. .c('x', { 'xmlns': 'http://jabber.org/protocol/muc#user'})
  2764. .c('item', {
  2765. 'jid': 'annoyingguy@localhost',
  2766. 'affiliation': 'none',
  2767. 'role': 'participant'
  2768. });
  2769. _converse.connection._dataRecv(test_utils.createRequest(presence));
  2770. var textarea = view.el.querySelector('.chat-textarea')
  2771. textarea.value = '/kick';
  2772. view.keyPressed({
  2773. target: textarea,
  2774. preventDefault: _.noop,
  2775. keyCode: 13
  2776. });
  2777. expect(view.validateRoleChangeCommand).toHaveBeenCalled();
  2778. expect(view.showErrorMessage).toHaveBeenCalledWith(
  2779. "Error: the \"kick\" command takes two arguments, the user's nickname and optionally a reason.");
  2780. expect(view.modifyRole).not.toHaveBeenCalled();
  2781. // Call now with the correct amount of arguments.
  2782. // XXX: Calling onFormSubmitted directly, trying
  2783. // again via triggering Event doesn't work for some weird
  2784. // reason.
  2785. textarea.value = '/kick annoyingGuy You\'re annoying';
  2786. view.onFormSubmitted(new Event('submit'));
  2787. expect(view.validateRoleChangeCommand.calls.count()).toBe(2);
  2788. expect(view.showErrorMessage.calls.count()).toBe(1);
  2789. expect(view.modifyRole).toHaveBeenCalled();
  2790. expect(sent_IQ.toLocaleString()).toBe(
  2791. `<iq id="${IQ_id}" to="lounge@localhost" type="set" xmlns="jabber:client">`+
  2792. `<query xmlns="http://jabber.org/protocol/muc#admin">`+
  2793. `<item nick="annoyingGuy" role="none">`+
  2794. `<reason>You&apos;re annoying</reason>`+
  2795. `</item>`+
  2796. `</query>`+
  2797. `</iq>`);
  2798. /* <presence
  2799. * from='harfleur@chat.shakespeare.lit/pistol'
  2800. * to='gower@shakespeare.lit/cell'
  2801. * type='unavailable'>
  2802. * <x xmlns='http://jabber.org/protocol/muc#user'>
  2803. * <item affiliation='none' role='none'/>
  2804. * <status code='307'/>
  2805. * </x>
  2806. * </presence>
  2807. */
  2808. presence = $pres({
  2809. 'from': 'lounge@localhost/annoyingGuy',
  2810. 'to': 'dummy@localhost/desktop',
  2811. 'type': 'unavailable'
  2812. })
  2813. .c('x', { 'xmlns': 'http://jabber.org/protocol/muc#user'})
  2814. .c('item', {
  2815. 'affiliation': 'none',
  2816. 'role': 'none'
  2817. }).up()
  2818. .c('status', {'code': '307'});
  2819. _converse.connection._dataRecv(test_utils.createRequest(presence));
  2820. expect(view.el.querySelectorAll('.chat-info')[3].textContent).toBe("annoyingGuy has been kicked out");
  2821. expect(view.el.querySelectorAll('.chat-info').length).toBe(4);
  2822. done();
  2823. }));
  2824. it("takes /op and /deop to make a user a moderator or not",
  2825. mock.initConverse(
  2826. null, ['rosterGroupsFetched'], {},
  2827. async function (done, _converse) {
  2828. await test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy');
  2829. var sent_IQ, IQ_id;
  2830. var sendIQ = _converse.connection.sendIQ;
  2831. spyOn(_converse.connection, 'sendIQ').and.callFake(function (iq, callback, errback) {
  2832. sent_IQ = iq;
  2833. IQ_id = sendIQ.bind(this)(iq, callback, errback);
  2834. });
  2835. var view = _converse.chatboxviews.get('lounge@localhost');
  2836. spyOn(view, 'modifyRole').and.callThrough();
  2837. spyOn(view, 'showErrorMessage').and.callThrough();
  2838. spyOn(view, 'showChatEvent').and.callThrough();
  2839. spyOn(view, 'validateRoleChangeCommand').and.callThrough();
  2840. // New user enters the groupchat
  2841. /* <presence
  2842. * from='coven@chat.shakespeare.lit/thirdwitch'
  2843. * id='27C55F89-1C6A-459A-9EB5-77690145D624'
  2844. * to='crone1@shakespeare.lit/desktop'>
  2845. * <x xmlns='http://jabber.org/protocol/muc#user'>
  2846. * <item affiliation='member' role='moderator'/>
  2847. * </x>
  2848. * </presence>
  2849. */
  2850. var presence = $pres({
  2851. 'from': 'lounge@localhost/trustworthyguy',
  2852. 'id':'27C55F89-1C6A-459A-9EB5-77690145D624',
  2853. 'to': 'dummy@localhost/desktop'
  2854. })
  2855. .c('x', { 'xmlns': 'http://jabber.org/protocol/muc#user'})
  2856. .c('item', {
  2857. 'jid': 'trustworthyguy@localhost',
  2858. 'affiliation': 'member',
  2859. 'role': 'participant'
  2860. });
  2861. _converse.connection._dataRecv(test_utils.createRequest(presence));
  2862. var info_msgs = Array.prototype.slice.call(view.el.querySelectorAll('.chat-info'), 0);
  2863. expect(info_msgs.pop().textContent).toBe("trustworthyguy has entered the groupchat");
  2864. var textarea = view.el.querySelector('.chat-textarea')
  2865. textarea.value = '/op';
  2866. view.keyPressed({
  2867. target: textarea,
  2868. preventDefault: _.noop,
  2869. keyCode: 13
  2870. });
  2871. expect(view.validateRoleChangeCommand).toHaveBeenCalled();
  2872. expect(view.showErrorMessage).toHaveBeenCalledWith(
  2873. "Error: the \"op\" command takes two arguments, the user's nickname and optionally a reason.");
  2874. expect(view.modifyRole).not.toHaveBeenCalled();
  2875. // Call now with the correct amount of arguments.
  2876. // XXX: Calling onFormSubmitted directly, trying
  2877. // again via triggering Event doesn't work for some weird
  2878. // reason.
  2879. textarea.value = '/op trustworthyguy You\'re trustworthy';
  2880. view.onFormSubmitted(new Event('submit'));
  2881. expect(view.validateRoleChangeCommand.calls.count()).toBe(2);
  2882. expect(view.showErrorMessage.calls.count()).toBe(1);
  2883. expect(view.modifyRole).toHaveBeenCalled();
  2884. expect(sent_IQ.toLocaleString()).toBe(
  2885. `<iq id="${IQ_id}" to="lounge@localhost" type="set" xmlns="jabber:client">`+
  2886. `<query xmlns="http://jabber.org/protocol/muc#admin">`+
  2887. `<item nick="trustworthyguy" role="moderator">`+
  2888. `<reason>You&apos;re trustworthy</reason>`+
  2889. `</item>`+
  2890. `</query>`+
  2891. `</iq>`);
  2892. /* <presence
  2893. * from='coven@chat.shakespeare.lit/thirdwitch'
  2894. * to='crone1@shakespeare.lit/desktop'>
  2895. * <x xmlns='http://jabber.org/protocol/muc#user'>
  2896. * <item affiliation='member'
  2897. * jid='hag66@shakespeare.lit/pda'
  2898. * role='moderator'/>
  2899. * </x>
  2900. * </presence>
  2901. */
  2902. presence = $pres({
  2903. 'from': 'lounge@localhost/trustworthyguy',
  2904. 'to': 'dummy@localhost/desktop'
  2905. })
  2906. .c('x', { 'xmlns': 'http://jabber.org/protocol/muc#user'})
  2907. .c('item', {
  2908. 'jid': 'trustworthyguy@localhost',
  2909. 'affiliation': 'member',
  2910. 'role': 'moderator'
  2911. });
  2912. _converse.connection._dataRecv(test_utils.createRequest(presence));
  2913. info_msgs = Array.prototype.slice.call(view.el.querySelectorAll('.chat-info'), 0);
  2914. expect(info_msgs.pop().textContent).toBe("trustworthyguy is now a moderator");
  2915. // Call now with the correct amount of arguments.
  2916. // XXX: Calling onFormSubmitted directly, trying
  2917. // again via triggering Event doesn't work for some weird
  2918. // reason.
  2919. textarea.value = '/deop trustworthyguy Perhaps not';
  2920. view.onFormSubmitted(new Event('submit'));
  2921. expect(view.validateRoleChangeCommand.calls.count()).toBe(3);
  2922. expect(view.showChatEvent.calls.count()).toBe(1);
  2923. expect(view.modifyRole).toHaveBeenCalled();
  2924. expect(sent_IQ.toLocaleString()).toBe(
  2925. `<iq id="${IQ_id}" to="lounge@localhost" type="set" xmlns="jabber:client">`+
  2926. `<query xmlns="http://jabber.org/protocol/muc#admin">`+
  2927. `<item nick="trustworthyguy" role="participant">`+
  2928. `<reason>Perhaps not</reason>`+
  2929. `</item>`+
  2930. `</query>`+
  2931. `</iq>`);
  2932. /* <presence
  2933. * from='coven@chat.shakespeare.lit/thirdwitch'
  2934. * to='crone1@shakespeare.lit/desktop'>
  2935. * <x xmlns='http://jabber.org/protocol/muc#user'>
  2936. * <item affiliation='member'
  2937. * jid='hag66@shakespeare.lit/pda'
  2938. * role='participant'/>
  2939. * </x>
  2940. * </presence>
  2941. */
  2942. presence = $pres({
  2943. 'from': 'lounge@localhost/trustworthyguy',
  2944. 'to': 'dummy@localhost/desktop'
  2945. }).c('x', { 'xmlns': 'http://jabber.org/protocol/muc#user'})
  2946. .c('item', {
  2947. 'jid': 'trustworthyguy@localhost',
  2948. 'affiliation': 'member',
  2949. 'role': 'participant'
  2950. });
  2951. _converse.connection._dataRecv(test_utils.createRequest(presence));
  2952. info_msgs = Array.prototype.slice.call(view.el.querySelectorAll('.chat-info'), 0);
  2953. expect(info_msgs.pop().textContent).toBe("trustworthyguy is no longer a moderator");
  2954. done();
  2955. }));
  2956. it("takes /mute and /voice to mute and unmute a user",
  2957. mock.initConverse(
  2958. null, ['rosterGroupsFetched'], {},
  2959. async function (done, _converse) {
  2960. await test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy');
  2961. var sent_IQ, IQ_id;
  2962. var sendIQ = _converse.connection.sendIQ;
  2963. spyOn(_converse.connection, 'sendIQ').and.callFake(function (iq, callback, errback) {
  2964. sent_IQ = iq;
  2965. IQ_id = sendIQ.bind(this)(iq, callback, errback);
  2966. });
  2967. var view = _converse.chatboxviews.get('lounge@localhost');
  2968. spyOn(view, 'modifyRole').and.callThrough();
  2969. spyOn(view, 'showErrorMessage').and.callThrough();
  2970. spyOn(view, 'showChatEvent').and.callThrough();
  2971. spyOn(view, 'validateRoleChangeCommand').and.callThrough();
  2972. // New user enters the groupchat
  2973. /* <presence
  2974. * from='coven@chat.shakespeare.lit/thirdwitch'
  2975. * id='27C55F89-1C6A-459A-9EB5-77690145D624'
  2976. * to='crone1@shakespeare.lit/desktop'>
  2977. * <x xmlns='http://jabber.org/protocol/muc#user'>
  2978. * <item affiliation='member' role='participant'/>
  2979. * </x>
  2980. * </presence>
  2981. */
  2982. var presence = $pres({
  2983. 'from': 'lounge@localhost/annoyingGuy',
  2984. 'id':'27C55F89-1C6A-459A-9EB5-77690145D624',
  2985. 'to': 'dummy@localhost/desktop'
  2986. })
  2987. .c('x', { 'xmlns': 'http://jabber.org/protocol/muc#user'})
  2988. .c('item', {
  2989. 'jid': 'annoyingguy@localhost',
  2990. 'affiliation': 'member',
  2991. 'role': 'participant'
  2992. });
  2993. _converse.connection._dataRecv(test_utils.createRequest(presence));
  2994. var info_msgs = Array.prototype.slice.call(view.el.querySelectorAll('.chat-info'), 0);
  2995. expect(info_msgs.pop().textContent).toBe("annoyingGuy has entered the groupchat");
  2996. const textarea = view.el.querySelector('.chat-textarea')
  2997. textarea.value = '/mute';
  2998. view.keyPressed({
  2999. target: textarea,
  3000. preventDefault: _.noop,
  3001. keyCode: 13
  3002. });
  3003. expect(view.validateRoleChangeCommand).toHaveBeenCalled();
  3004. expect(view.showErrorMessage).toHaveBeenCalledWith(
  3005. "Error: the \"mute\" command takes two arguments, the user's nickname and optionally a reason.");
  3006. expect(view.modifyRole).not.toHaveBeenCalled();
  3007. // Call now with the correct amount of arguments.
  3008. // XXX: Calling onFormSubmitted directly, trying
  3009. // again via triggering Event doesn't work for some weird
  3010. // reason.
  3011. textarea.value = '/mute annoyingGuy You\'re annoying';
  3012. view.onFormSubmitted(new Event('submit'));
  3013. expect(view.validateRoleChangeCommand.calls.count()).toBe(2);
  3014. expect(view.showErrorMessage.calls.count()).toBe(1);
  3015. expect(view.modifyRole).toHaveBeenCalled();
  3016. expect(sent_IQ.toLocaleString()).toBe(
  3017. `<iq id="${IQ_id}" to="lounge@localhost" type="set" xmlns="jabber:client">`+
  3018. `<query xmlns="http://jabber.org/protocol/muc#admin">`+
  3019. `<item nick="annoyingGuy" role="visitor">`+
  3020. `<reason>You&apos;re annoying</reason>`+
  3021. `</item>`+
  3022. `</query>`+
  3023. `</iq>`);
  3024. /* <presence
  3025. * from='coven@chat.shakespeare.lit/thirdwitch'
  3026. * to='crone1@shakespeare.lit/desktop'>
  3027. * <x xmlns='http://jabber.org/protocol/muc#user'>
  3028. * <item affiliation='member'
  3029. * jid='hag66@shakespeare.lit/pda'
  3030. * role='visitor'/>
  3031. * </x>
  3032. * </presence>
  3033. */
  3034. presence = $pres({
  3035. 'from': 'lounge@localhost/annoyingGuy',
  3036. 'to': 'dummy@localhost/desktop'
  3037. })
  3038. .c('x', { 'xmlns': 'http://jabber.org/protocol/muc#user'})
  3039. .c('item', {
  3040. 'jid': 'annoyingguy@localhost',
  3041. 'affiliation': 'member',
  3042. 'role': 'visitor'
  3043. });
  3044. _converse.connection._dataRecv(test_utils.createRequest(presence));
  3045. info_msgs = Array.prototype.slice.call(view.el.querySelectorAll('.chat-info'), 0);
  3046. expect(info_msgs.pop().textContent).toBe("annoyingGuy has been muted");
  3047. // Call now with the correct of arguments.
  3048. // XXX: Calling onFormSubmitted directly, trying
  3049. // again via triggering Event doesn't work for some weird
  3050. // reason.
  3051. textarea.value = '/voice annoyingGuy Now you can talk again';
  3052. view.onFormSubmitted(new Event('submit'));
  3053. expect(view.validateRoleChangeCommand.calls.count()).toBe(3);
  3054. expect(view.showChatEvent.calls.count()).toBe(1);
  3055. expect(view.modifyRole).toHaveBeenCalled();
  3056. expect(sent_IQ.toLocaleString()).toBe(
  3057. `<iq id="${IQ_id}" to="lounge@localhost" type="set" xmlns="jabber:client">`+
  3058. `<query xmlns="http://jabber.org/protocol/muc#admin">`+
  3059. `<item nick="annoyingGuy" role="participant">`+
  3060. `<reason>Now you can talk again</reason>`+
  3061. `</item>`+
  3062. `</query>`+
  3063. `</iq>`);
  3064. /* <presence
  3065. * from='coven@chat.shakespeare.lit/thirdwitch'
  3066. * to='crone1@shakespeare.lit/desktop'>
  3067. * <x xmlns='http://jabber.org/protocol/muc#user'>
  3068. * <item affiliation='member'
  3069. * jid='hag66@shakespeare.lit/pda'
  3070. * role='visitor'/>
  3071. * </x>
  3072. * </presence>
  3073. */
  3074. presence = $pres({
  3075. 'from': 'lounge@localhost/annoyingGuy',
  3076. 'to': 'dummy@localhost/desktop'
  3077. })
  3078. .c('x', { 'xmlns': 'http://jabber.org/protocol/muc#user'})
  3079. .c('item', {
  3080. 'jid': 'annoyingguy@localhost',
  3081. 'affiliation': 'member',
  3082. 'role': 'participant'
  3083. });
  3084. _converse.connection._dataRecv(test_utils.createRequest(presence));
  3085. info_msgs = Array.prototype.slice.call(view.el.querySelectorAll('.chat-info'), 0);
  3086. expect(info_msgs.pop().textContent).toBe("annoyingGuy has been given a voice again");
  3087. done();
  3088. }));
  3089. it("takes /destroy to destroy a muc",
  3090. mock.initConverse(
  3091. null, ['rosterGroupsFetched'], {},
  3092. async function (done, _converse) {
  3093. await test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy');
  3094. let sent_IQ, IQ_id;
  3095. const sendIQ = _converse.connection.sendIQ;
  3096. spyOn(_converse.connection, 'sendIQ').and.callFake(function (iq, callback, errback) {
  3097. sent_IQ = iq;
  3098. IQ_id = sendIQ.bind(this)(iq, callback, errback);
  3099. });
  3100. const view = _converse.chatboxviews.get('lounge@localhost');
  3101. const textarea = view.el.querySelector('.chat-textarea');
  3102. textarea.value = '/destroy bored';
  3103. view.onFormSubmitted(new Event('submit'));
  3104. expect(sent_IQ.toLocaleString()).toBe(
  3105. `<iq id="${IQ_id}" to="lounge@localhost" type="set" xmlns="jabber:client">`+
  3106. `<query xmlns="http://jabber.org/protocol/muc#owner">`+
  3107. `<destroy>`+
  3108. `<reason>`+
  3109. `bored`+
  3110. `</reason>`+
  3111. `</destroy>`+
  3112. `</query>`+
  3113. `</iq>`);
  3114. /* <iq from="lounge@localhost"
  3115. * id="${IQ_id}"
  3116. * to="dummy@localhost/resource"
  3117. * type="result"
  3118. * xmlns="jabber:client"/>
  3119. */
  3120. const result_stanza = $iq({
  3121. 'type': 'result',
  3122. 'id': IQ_id,
  3123. 'from': view.model.get('jid'),
  3124. 'to': _converse.connection.jid
  3125. });
  3126. spyOn(view, 'close').and.callThrough();
  3127. spyOn(_converse, 'emit');
  3128. _converse.connection._dataRecv(test_utils.createRequest(result_stanza));
  3129. await test_utils.waitUntil(() => (view.model.get('connection_status') === converse.ROOMSTATUS.DISCONNECTED));
  3130. expect(view.close).toHaveBeenCalled();
  3131. expect(_converse.emit).toHaveBeenCalledWith('chatBoxClosed', jasmine.any(Object));
  3132. done();
  3133. }));
  3134. });
  3135. describe("When attempting to enter a groupchat", function () {
  3136. it("will use the nickname set in the global settings if the user doesn't have a VCard nickname",
  3137. mock.initConverse(
  3138. null, ['rosterGroupsFetched', 'chatBoxesFetched'], {'nickname': 'Benedict-Cucumberpatch'},
  3139. async function (done, _converse) {
  3140. await test_utils.openChatRoomViaModal(_converse, 'roomy@muc.localhost');
  3141. const view = _converse.chatboxviews.get('roomy@muc.localhost');
  3142. expect(view.model.get('nick')).toBe('Benedict-Cucumberpatch');
  3143. done();
  3144. }));
  3145. it("will show an error message if the groupchat requires a password",
  3146. mock.initConverse(
  3147. null, ['rosterGroupsFetched', 'chatBoxesFetched'], {},
  3148. async function (done, _converse) {
  3149. const groupchat_jid = 'protected';
  3150. await test_utils.openChatRoomViaModal(_converse, groupchat_jid, 'dummy');
  3151. const view = _converse.chatboxviews.get(groupchat_jid);
  3152. spyOn(view, 'renderPasswordForm').and.callThrough();
  3153. var presence = $pres().attrs({
  3154. 'from': `${groupchat_jid}/dummy`,
  3155. 'id': u.getUniqueId(),
  3156. 'to': 'dummy@localhost/pda',
  3157. 'type': 'error'
  3158. }).c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
  3159. .c('error').attrs({by:'lounge@localhost', type:'auth'})
  3160. .c('not-authorized').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'});
  3161. _converse.connection._dataRecv(test_utils.createRequest(presence));
  3162. const chat_body = view.el.querySelector('.chatroom-body');
  3163. expect(view.renderPasswordForm).toHaveBeenCalled();
  3164. expect(chat_body.querySelectorAll('form.chatroom-form').length).toBe(1);
  3165. expect(chat_body.querySelector('legend').textContent)
  3166. .toBe('This groupchat requires a password');
  3167. // Let's submit the form
  3168. spyOn(view, 'join');
  3169. const input_el = view.el.querySelector('[name="password"]');
  3170. input_el.value = 'secret';
  3171. view.el.querySelector('input[type=submit]').click();
  3172. expect(view.join).toHaveBeenCalledWith('dummy', 'secret');
  3173. done();
  3174. }));
  3175. it("will show an error message if the groupchat is members-only and the user not included",
  3176. mock.initConverse(
  3177. null, ['rosterGroupsFetched'], {},
  3178. async function (done, _converse) {
  3179. const groupchat_jid = 'members-only@muc.localhost'
  3180. await test_utils.openChatRoomViaModal(_converse, groupchat_jid, 'dummy');
  3181. const presence = $pres().attrs({
  3182. from: `${groupchat_jid}/dummy`,
  3183. id: u.getUniqueId(),
  3184. to: 'dummy@localhost/pda',
  3185. type: 'error'
  3186. }).c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
  3187. .c('error').attrs({by:'lounge@localhost', type:'auth'})
  3188. .c('registration-required').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
  3189. const view = _converse.chatboxviews.get(groupchat_jid);
  3190. spyOn(view, 'showErrorMessage').and.callThrough();
  3191. _converse.connection._dataRecv(test_utils.createRequest(presence));
  3192. expect(view.el.querySelector('.chatroom-body .disconnect-container .disconnect-msg:last-child').textContent)
  3193. .toBe('You are not on the member list of this groupchat.');
  3194. done();
  3195. }));
  3196. it("will show an error message if the user has been banned",
  3197. mock.initConverse(
  3198. null, ['rosterGroupsFetched'], {},
  3199. async function (done, _converse) {
  3200. const groupchat_jid = 'off-limits@muc.localhost'
  3201. await test_utils.openChatRoomViaModal(_converse, groupchat_jid, 'dummy');
  3202. const presence = $pres().attrs({
  3203. from: `${groupchat_jid}/dummy`,
  3204. id: u.getUniqueId(),
  3205. to: 'dummy@localhost/pda',
  3206. type: 'error'
  3207. }).c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
  3208. .c('error').attrs({by:'lounge@localhost', type:'auth'})
  3209. .c('forbidden').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. _converse.connection._dataRecv(test_utils.createRequest(presence));
  3213. expect(view.el.querySelector('.chatroom-body .disconnect-container .disconnect-msg:last-child').textContent)
  3214. .toBe('You have been banned from this groupchat.');
  3215. done();
  3216. }));
  3217. it("will render a nickname form if a nickname conflict happens and muc_nickname_from_jid=false",
  3218. mock.initConverse(
  3219. null, ['rosterGroupsFetched'], {},
  3220. async function (done, _converse) {
  3221. const groupchat_jid = 'conflicted@muc.localhost';
  3222. await test_utils.openChatRoomViaModal(_converse, groupchat_jid, 'dummy');
  3223. var presence = $pres().attrs({
  3224. from: `${groupchat_jid}/dummy`,
  3225. id: u.getUniqueId(),
  3226. to: 'dummy@localhost/pda',
  3227. type: 'error'
  3228. }).c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
  3229. .c('error').attrs({by:'lounge@localhost', type:'cancel'})
  3230. .c('conflict').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
  3231. const view = _converse.chatboxviews.get(groupchat_jid);
  3232. spyOn(view, 'showErrorMessage').and.callThrough();
  3233. _converse.connection._dataRecv(test_utils.createRequest(presence));
  3234. expect(sizzle('.chatroom-body form.chatroom-form label:first', view.el).pop().textContent)
  3235. .toBe('Please choose your nickname');
  3236. const input = sizzle('.chatroom-body form.chatroom-form input:first', view.el).pop();
  3237. input.value = 'nicky';
  3238. view.el.querySelector('input[type=submit]').click();
  3239. done();
  3240. }));
  3241. it("will automatically choose a new nickname if a nickname conflict happens and muc_nickname_from_jid=true",
  3242. mock.initConverse(
  3243. null, ['rosterGroupsFetched'], {},
  3244. async function (done, _converse) {
  3245. const groupchat_jid = 'conflicting@muc.localhost'
  3246. await test_utils.openChatRoomViaModal(_converse, groupchat_jid, 'dummy');
  3247. /* <presence
  3248. * from='coven@chat.shakespeare.lit/thirdwitch'
  3249. * id='n13mt3l'
  3250. * to='hag66@shakespeare.lit/pda'
  3251. * type='error'>
  3252. * <x xmlns='http://jabber.org/protocol/muc'/>
  3253. * <error by='coven@chat.shakespeare.lit' type='cancel'>
  3254. * <conflict xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/>
  3255. * </error>
  3256. * </presence>
  3257. */
  3258. _converse.muc_nickname_from_jid = true;
  3259. const attrs = {
  3260. 'from': `${groupchat_jid}/dummy`,
  3261. 'id': u.getUniqueId(),
  3262. 'to': 'dummy@localhost/pda',
  3263. 'type': 'error'
  3264. };
  3265. let presence = $pres().attrs(attrs)
  3266. .c('x').attrs({'xmlns':'http://jabber.org/protocol/muc'}).up()
  3267. .c('error').attrs({'by': groupchat_jid, 'type':'cancel'})
  3268. .c('conflict').attrs({'xmlns':'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
  3269. const view = _converse.chatboxviews.get(groupchat_jid);
  3270. spyOn(view, 'showErrorMessage').and.callThrough();
  3271. spyOn(view, 'join').and.callThrough();
  3272. // Simulate repeatedly that there's already someone in the groupchat
  3273. // with that nickname
  3274. _converse.connection._dataRecv(test_utils.createRequest(presence));
  3275. expect(view.join).toHaveBeenCalledWith('dummy-2');
  3276. attrs.from = `${groupchat_jid}/dummy-2`;
  3277. attrs.id = u.getUniqueId();
  3278. presence = $pres().attrs(attrs)
  3279. .c('x').attrs({'xmlns':'http://jabber.org/protocol/muc'}).up()
  3280. .c('error').attrs({'by': groupchat_jid, type:'cancel'})
  3281. .c('conflict').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
  3282. _converse.connection._dataRecv(test_utils.createRequest(presence));
  3283. expect(view.join).toHaveBeenCalledWith('dummy-3');
  3284. attrs.from = `${groupchat_jid}/dummy-3`;
  3285. attrs.id = new Date().getTime();
  3286. presence = $pres().attrs(attrs)
  3287. .c('x').attrs({'xmlns': 'http://jabber.org/protocol/muc'}).up()
  3288. .c('error').attrs({'by': groupchat_jid, 'type': 'cancel'})
  3289. .c('conflict').attrs({'xmlns':'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
  3290. _converse.connection._dataRecv(test_utils.createRequest(presence));
  3291. expect(view.join).toHaveBeenCalledWith('dummy-4');
  3292. done();
  3293. }));
  3294. it("will show an error message if the user is not allowed to have created the groupchat",
  3295. mock.initConverse(
  3296. null, ['rosterGroupsFetched'], {},
  3297. async function (done, _converse) {
  3298. const groupchat_jid = 'impermissable@muc.localhost'
  3299. await test_utils.openChatRoomViaModal(_converse, groupchat_jid, 'dummy')
  3300. var presence = $pres().attrs({
  3301. from: `${groupchat_jid}/dummy`,
  3302. id: u.getUniqueId(),
  3303. to:'dummy@localhost/pda',
  3304. type:'error'
  3305. }).c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
  3306. .c('error').attrs({by:'lounge@localhost', type:'cancel'})
  3307. .c('not-allowed').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
  3308. const view = _converse.chatboxviews.get(groupchat_jid);
  3309. spyOn(view, 'showErrorMessage').and.callThrough();
  3310. _converse.connection._dataRecv(test_utils.createRequest(presence));
  3311. expect(view.el.querySelector('.chatroom-body .disconnect-container .disconnect-msg:last-child').textContent)
  3312. .toBe('You are not allowed to create new groupchats.');
  3313. done();
  3314. }));
  3315. it("will show an error message if the user's nickname doesn't conform to groupchat policy",
  3316. mock.initConverse(
  3317. null, ['rosterGroupsFetched'], {},
  3318. async function (done, _converse) {
  3319. const groupchat_jid = 'conformist@muc.localhost'
  3320. await test_utils.openChatRoomViaModal(_converse, groupchat_jid, 'dummy');
  3321. const presence = $pres().attrs({
  3322. from: `${groupchat_jid}/dummy`,
  3323. id: u.getUniqueId(),
  3324. to:'dummy@localhost/pda',
  3325. type:'error'
  3326. }).c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
  3327. .c('error').attrs({by:'lounge@localhost', type:'cancel'})
  3328. .c('not-acceptable').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
  3329. const view = _converse.chatboxviews.get(groupchat_jid);
  3330. spyOn(view, 'showErrorMessage').and.callThrough();
  3331. _converse.connection._dataRecv(test_utils.createRequest(presence));
  3332. expect(view.el.querySelector('.chatroom-body .disconnect-container .disconnect-msg:last-child').textContent)
  3333. .toBe("Your nickname doesn't conform to this groupchat's policies.");
  3334. done();
  3335. }));
  3336. it("will show an error message if the groupchat doesn't yet exist",
  3337. mock.initConverse(
  3338. null, ['rosterGroupsFetched'], {},
  3339. async function (done, _converse) {
  3340. const groupchat_jid = 'nonexistent@muc.localhost'
  3341. await test_utils.openChatRoomViaModal(_converse, groupchat_jid, 'dummy');
  3342. const presence = $pres().attrs({
  3343. from: `${groupchat_jid}/dummy`,
  3344. id: u.getUniqueId(),
  3345. to: 'dummy@localhost/pda',
  3346. type:'error'
  3347. }).c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
  3348. .c('error').attrs({by:'lounge@localhost', type:'cancel'})
  3349. .c('item-not-found').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
  3350. const view = _converse.chatboxviews.get(groupchat_jid);
  3351. spyOn(view, 'showErrorMessage').and.callThrough();
  3352. _converse.connection._dataRecv(test_utils.createRequest(presence));
  3353. expect(view.el.querySelector('.chatroom-body .disconnect-container .disconnect-msg:last-child').textContent)
  3354. .toBe("This groupchat does not (yet) exist.");
  3355. done();
  3356. }));
  3357. it("will show an error message if the groupchat has reached its maximum number of participants",
  3358. mock.initConverse(
  3359. null, ['rosterGroupsFetched'], {},
  3360. async function (done, _converse) {
  3361. const groupchat_jid = 'maxed-out@muc.localhost'
  3362. await test_utils.openChatRoomViaModal(_converse, groupchat_jid, 'dummy')
  3363. const presence = $pres().attrs({
  3364. from: `${groupchat_jid}/dummy`,
  3365. id: u.getUniqueId(),
  3366. to:'dummy@localhost/pda',
  3367. type:'error'
  3368. }).c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
  3369. .c('error').attrs({by:'lounge@localhost', type:'cancel'})
  3370. .c('service-unavailable').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
  3371. const view = _converse.chatboxviews.get(groupchat_jid);
  3372. spyOn(view, 'showErrorMessage').and.callThrough();
  3373. _converse.connection._dataRecv(test_utils.createRequest(presence));
  3374. expect(view.el.querySelector('.chatroom-body .disconnect-container .disconnect-msg:last-child').textContent)
  3375. .toBe("This groupchat has reached its maximum number of participants.");
  3376. done();
  3377. }));
  3378. });
  3379. describe("Someone being invited to a groupchat", function () {
  3380. it("will first be added to the member list if the groupchat is members only",
  3381. mock.initConverse(
  3382. null, ['rosterGroupsFetched', 'chatBoxesFetched'], {},
  3383. async function (done, _converse) {
  3384. var sent_IQs = [], IQ_ids = [];
  3385. const sendIQ = _converse.connection.sendIQ;
  3386. const IQ_stanzas = _converse.connection.IQ_stanzas;
  3387. const room_jid = 'coven@chat.shakespeare.lit';
  3388. spyOn(_converse.connection, 'sendIQ').and.callFake(function (iq, callback, errback) {
  3389. sent_IQs.push(iq);
  3390. IQ_ids.push(sendIQ.bind(this)(iq, callback, errback));
  3391. });
  3392. await _converse.api.rooms.open(room_jid, {'nick': 'dummy'});
  3393. const node = await test_utils.waitUntil(() => _.filter(
  3394. IQ_stanzas,
  3395. iq => iq.nodeTree.querySelector(
  3396. `iq[to="${room_jid}"] query[xmlns="http://jabber.org/protocol/disco#info"]`
  3397. )).pop());
  3398. // Check that the groupchat queried for the feautures.
  3399. let stanza = node.nodeTree;
  3400. expect(node.toLocaleString()).toBe(
  3401. `<iq from="dummy@localhost/resource" id="${stanza.getAttribute("id")}" to="${room_jid}" type="get" xmlns="jabber:client">`+
  3402. `<query xmlns="http://jabber.org/protocol/disco#info"/>`+
  3403. `</iq>`);
  3404. const view = _converse.chatboxviews.get('coven@chat.shakespeare.lit');
  3405. // State that the chat is members-only via the features IQ
  3406. var features_stanza = $iq({
  3407. from: 'coven@chat.shakespeare.lit',
  3408. 'id': IQ_ids.pop(),
  3409. 'to': 'dummy@localhost/desktop',
  3410. 'type': 'result'
  3411. })
  3412. .c('query', { 'xmlns': 'http://jabber.org/protocol/disco#info'})
  3413. .c('identity', {
  3414. 'category': 'conference',
  3415. 'name': 'A Dark Cave',
  3416. 'type': 'text'
  3417. }).up()
  3418. .c('feature', {'var': 'http://jabber.org/protocol/muc'}).up()
  3419. .c('feature', {'var': 'muc_hidden'}).up()
  3420. .c('feature', {'var': 'muc_temporary'}).up()
  3421. .c('feature', {'var': 'muc_membersonly'}).up();
  3422. _converse.connection._dataRecv(test_utils.createRequest(features_stanza));
  3423. await test_utils.waitUntil(() => (view.model.get('connection_status') === converse.ROOMSTATUS.CONNECTING));
  3424. expect(view.model.features.get('membersonly')).toBeTruthy();
  3425. await test_utils.createContacts(_converse, 'current');
  3426. let sent_stanza, sent_id;
  3427. spyOn(_converse.connection, 'send').and.callFake(function (stanza) {
  3428. if (stanza.nodeTree && stanza.nodeTree.nodeName === 'message') {
  3429. sent_id = stanza.nodeTree.getAttribute('id');
  3430. sent_stanza = stanza;
  3431. }
  3432. });
  3433. var name = mock.cur_names[0];
  3434. const invitee_jid = name.replace(/ /g,'.').toLowerCase() + '@localhost';
  3435. var reason = "Please join this groupchat";
  3436. view.model.directInvite(invitee_jid, reason);
  3437. // Check in reverse order that we requested all three lists
  3438. // (member, owner and admin).
  3439. var admin_iq_id = IQ_ids.pop();
  3440. var owner_iq_id = IQ_ids.pop();
  3441. var member_iq_id = IQ_ids.pop();
  3442. expect(sent_IQs.pop().toLocaleString()).toBe(
  3443. `<iq id="${admin_iq_id}" to="coven@chat.shakespeare.lit" type="get" xmlns="jabber:client">`+
  3444. `<query xmlns="http://jabber.org/protocol/muc#admin">`+
  3445. `<item affiliation="admin"/>`+
  3446. `</query>`+
  3447. `</iq>`);
  3448. expect(sent_IQs.pop().toLocaleString()).toBe(
  3449. `<iq id="${owner_iq_id}" to="coven@chat.shakespeare.lit" type="get" xmlns="jabber:client">`+
  3450. `<query xmlns="http://jabber.org/protocol/muc#admin">`+
  3451. `<item affiliation="owner"/>`+
  3452. `</query>`+
  3453. `</iq>`);
  3454. expect(sent_IQs.pop().toLocaleString()).toBe(
  3455. `<iq id="${member_iq_id}" to="coven@chat.shakespeare.lit" type="get" xmlns="jabber:client">`+
  3456. `<query xmlns="http://jabber.org/protocol/muc#admin">`+
  3457. `<item affiliation="member"/>`+
  3458. `</query>`+
  3459. `</iq>`);
  3460. /* Now the service sends the member list to the user
  3461. *
  3462. * <iq from='coven@chat.shakespeare.lit'
  3463. * id='member3'
  3464. * to='crone1@shakespeare.lit/desktop'
  3465. * type='result'>
  3466. * <query xmlns='http://jabber.org/protocol/muc#admin'>
  3467. * <item affiliation='member'
  3468. * jid='hag66@shakespeare.lit'
  3469. * nick='thirdwitch'
  3470. * role='participant'/>
  3471. * </query>
  3472. * </iq>
  3473. */
  3474. var member_list_stanza = $iq({
  3475. 'from': 'coven@chat.shakespeare.lit',
  3476. 'id': member_iq_id,
  3477. 'to': 'dummy@localhost/resource',
  3478. 'type': 'result'
  3479. }).c('query', {'xmlns': Strophe.NS.MUC_ADMIN})
  3480. .c('item', {
  3481. 'affiliation': 'member',
  3482. 'jid': 'hag66@shakespeare.lit',
  3483. 'nick': 'thirdwitch',
  3484. 'role': 'participant'
  3485. });
  3486. _converse.connection._dataRecv(test_utils.createRequest(member_list_stanza));
  3487. var admin_list_stanza = $iq({
  3488. 'from': 'coven@chat.shakespeare.lit',
  3489. 'id': admin_iq_id,
  3490. 'to': 'dummy@localhost/resource',
  3491. 'type': 'result'
  3492. }).c('query', {'xmlns': Strophe.NS.MUC_ADMIN})
  3493. .c('item', {
  3494. 'affiliation': 'admin',
  3495. 'jid': 'wiccarocks@shakespeare.lit',
  3496. 'nick': 'secondwitch'
  3497. });
  3498. _converse.connection._dataRecv(test_utils.createRequest(admin_list_stanza));
  3499. var owner_list_stanza = $iq({
  3500. 'from': 'coven@chat.shakespeare.lit',
  3501. 'id': owner_iq_id,
  3502. 'to': 'dummy@localhost/resource',
  3503. 'type': 'result'
  3504. }).c('query', {'xmlns': Strophe.NS.MUC_ADMIN})
  3505. .c('item', {
  3506. 'affiliation': 'owner',
  3507. 'jid': 'crone1@shakespeare.lit',
  3508. });
  3509. _converse.connection._dataRecv(test_utils.createRequest(owner_list_stanza));
  3510. await test_utils.waitUntil(() => IQ_ids.length, 300);
  3511. stanza = await test_utils.waitUntil(() => _.get(_.filter(
  3512. IQ_stanzas,
  3513. iq => iq.nodeTree.querySelector(
  3514. `iq[to="${room_jid}"] query[xmlns="http://jabber.org/protocol/muc#admin"]`
  3515. )).pop(), 'nodeTree'));
  3516. expect(stanza.outerHTML,
  3517. `<iq id="${IQ_ids.pop()}" to="coven@chat.shakespeare.lit" type="set" xmlns="jabber:client">`+
  3518. `<query xmlns="http://jabber.org/protocol/muc#admin">`+
  3519. `<item affiliation="member" jid="${invitee_jid}">`+
  3520. `<reason>Please join this groupchat</reason>`+
  3521. `</item>`+
  3522. `</query>`+
  3523. `</iq>`);
  3524. // Finally check that the user gets invited.
  3525. expect(sent_stanza.toLocaleString()).toBe( // Strophe adds the xmlns attr (although not in spec)
  3526. `<message from="dummy@localhost/resource" id="${sent_id}" to="${invitee_jid}" xmlns="jabber:client">`+
  3527. `<x jid="coven@chat.shakespeare.lit" reason="Please join this groupchat" xmlns="jabber:x:conference"/>`+
  3528. `</message>`
  3529. );
  3530. done();
  3531. }));
  3532. });
  3533. describe("The affiliations delta", function () {
  3534. it("can be computed in various ways",
  3535. mock.initConverse(
  3536. null, ['rosterGroupsFetched'], {},
  3537. async function (done, _converse) {
  3538. await test_utils.openChatRoom(_converse, 'coven', 'chat.shakespeare.lit', 'dummy');
  3539. var roomview = _converse.chatboxviews.get('coven@chat.shakespeare.lit');
  3540. var exclude_existing = false;
  3541. var remove_absentees = false;
  3542. var new_list = [];
  3543. var old_list = [];
  3544. var delta = u.computeAffiliationsDelta(exclude_existing, remove_absentees, new_list, old_list);
  3545. expect(delta.length).toBe(0);
  3546. new_list = [{'jid': 'wiccarocks@shakespeare.lit', 'affiliation': 'member'}];
  3547. old_list = [{'jid': 'wiccarocks@shakespeare.lit', 'affiliation': 'member'}];
  3548. delta = u.computeAffiliationsDelta(exclude_existing, remove_absentees, new_list, old_list);
  3549. expect(delta.length).toBe(0);
  3550. // When remove_absentees is false, then affiliations in the old
  3551. // list which are not in the new one won't be removed.
  3552. old_list = [{'jid': 'oldhag666@shakespeare.lit', 'affiliation': 'owner'},
  3553. {'jid': 'wiccarocks@shakespeare.lit', 'affiliation': 'member'}];
  3554. delta = u.computeAffiliationsDelta(exclude_existing, remove_absentees, new_list, old_list);
  3555. expect(delta.length).toBe(0);
  3556. // With exclude_existing set to false, any changed affiliations
  3557. // will be included in the delta (i.e. existing affiliations
  3558. // are included in the comparison).
  3559. old_list = [{'jid': 'wiccarocks@shakespeare.lit', 'affiliation': 'owner'}];
  3560. delta = u.computeAffiliationsDelta(exclude_existing, remove_absentees, new_list, old_list);
  3561. expect(delta.length).toBe(1);
  3562. expect(delta[0].jid).toBe('wiccarocks@shakespeare.lit');
  3563. expect(delta[0].affiliation).toBe('member');
  3564. // To also remove affiliations from the old list which are not
  3565. // in the new list, we set remove_absentees to true
  3566. remove_absentees = true;
  3567. old_list = [{'jid': 'oldhag666@shakespeare.lit', 'affiliation': 'owner'},
  3568. {'jid': 'wiccarocks@shakespeare.lit', 'affiliation': 'member'}];
  3569. delta = u.computeAffiliationsDelta(exclude_existing, remove_absentees, new_list, old_list);
  3570. expect(delta.length).toBe(1);
  3571. expect(delta[0].jid).toBe('oldhag666@shakespeare.lit');
  3572. expect(delta[0].affiliation).toBe('none');
  3573. delta = u.computeAffiliationsDelta(exclude_existing, remove_absentees, [], old_list);
  3574. expect(delta.length).toBe(2);
  3575. expect(delta[0].jid).toBe('oldhag666@shakespeare.lit');
  3576. expect(delta[0].affiliation).toBe('none');
  3577. expect(delta[1].jid).toBe('wiccarocks@shakespeare.lit');
  3578. expect(delta[1].affiliation).toBe('none');
  3579. // To only add a user if they don't already have an
  3580. // affiliation, we set 'exclude_existing' to true
  3581. exclude_existing = true;
  3582. old_list = [{'jid': 'wiccarocks@shakespeare.lit', 'affiliation': 'owner'}];
  3583. delta = u.computeAffiliationsDelta(exclude_existing, remove_absentees, new_list, old_list);
  3584. expect(delta.length).toBe(0);
  3585. done();
  3586. }));
  3587. });
  3588. describe("The \"Groupchats\" section", function () {
  3589. it("contains a link to a modal through which a new chatroom can be created",
  3590. mock.initConverse(
  3591. null, ['rosterGroupsFetched', 'chatBoxesFetched'], {},
  3592. async function (done, _converse) {
  3593. test_utils.openControlBox();
  3594. _converse.emit('rosterContactsFetched');
  3595. const roomspanel = _converse.chatboxviews.get('controlbox').roomspanel;
  3596. roomspanel.el.querySelector('.show-add-muc-modal').click();
  3597. test_utils.closeControlBox(_converse);
  3598. const modal = roomspanel.add_room_modal;
  3599. await test_utils.waitUntil(() => u.isVisible(modal.el), 1000)
  3600. expect(modal.el.querySelector('.modal-title').textContent).toBe('Enter a new Groupchat');
  3601. spyOn(_converse.ChatRoom.prototype, 'getRoomFeatures').and.callFake(() => Promise.resolve());
  3602. roomspanel.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  3603. modal.el.querySelector('input[name="chatroom"]').value = 'lounce@muc.localhost';
  3604. modal.el.querySelector('form input[type="submit"]').click();
  3605. await test_utils.waitUntil(() => _converse.chatboxes.length > 1);
  3606. await test_utils.waitUntil(() => sizzle('.chatroom', _converse.el).filter(u.isVisible).length === 1);
  3607. expect(sizzle('.chatroom', _converse.el).filter(u.isVisible).length).toBe(1); // There should now be an open chatroom
  3608. done();
  3609. }));
  3610. it("contains a link to a modal which can list groupchats publically available on the server",
  3611. mock.initConverse(
  3612. null, ['rosterGroupsFetched', 'chatBoxesFetched'], {},
  3613. async function (done, _converse) {
  3614. var sendIQ = _converse.connection.sendIQ;
  3615. var sent_stanza, IQ_id;
  3616. spyOn(_converse.connection, 'sendIQ').and.callFake(function (iq, callback, errback) {
  3617. sent_stanza = iq;
  3618. IQ_id = sendIQ.bind(this)(iq, callback, errback);
  3619. });
  3620. test_utils.openControlBox();
  3621. var roomspanel = _converse.chatboxviews.get('controlbox').roomspanel;
  3622. roomspanel.el.querySelector('.show-list-muc-modal').click();
  3623. test_utils.closeControlBox(_converse);
  3624. const modal = roomspanel.list_rooms_modal;
  3625. await test_utils.waitUntil(() => u.isVisible(modal.el), 1000);
  3626. spyOn(_converse.ChatRoom.prototype, 'getRoomFeatures').and.callFake(() => Promise.resolve());
  3627. roomspanel.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  3628. // See: http://xmpp.org/extensions/xep-0045.html#disco-rooms
  3629. expect(modal.el.querySelectorAll('.available-chatrooms li').length).toBe(0);
  3630. const input = modal.el.querySelector('input[name="server"]').value = 'chat.shakespear.lit';
  3631. modal.el.querySelector('input[type="submit"]').click();
  3632. await test_utils.waitUntil(() => _converse.chatboxes.length);
  3633. expect(sent_stanza.toLocaleString()).toBe(
  3634. `<iq from="dummy@localhost/resource" id="${IQ_id}" to="chat.shakespear.lit" type="get" xmlns="jabber:client">`+
  3635. `<query xmlns="http://jabber.org/protocol/disco#items"/>`+
  3636. `</iq>`
  3637. );
  3638. const iq = $iq({
  3639. from:'muc.localhost',
  3640. to:'dummy@localhost/pda',
  3641. id: IQ_id,
  3642. type:'result'
  3643. }).c('query')
  3644. .c('item', { jid:'heath@chat.shakespeare.lit', name:'A Lonely Heath'}).up()
  3645. .c('item', { jid:'coven@chat.shakespeare.lit', name:'A Dark Cave'}).up()
  3646. .c('item', { jid:'forres@chat.shakespeare.lit', name:'The Palace'}).up()
  3647. .c('item', { jid:'inverness@chat.shakespeare.lit', name:'Macbeth&apos;s Castle'}).nodeTree;
  3648. _converse.connection._dataRecv(test_utils.createRequest(iq));
  3649. await test_utils.waitUntil(() => modal.el.querySelectorAll('.available-chatrooms li').length === 5);
  3650. const rooms = modal.el.querySelectorAll('.available-chatrooms li');
  3651. expect(rooms[0].textContent.trim()).toBe("Groupchats found:");
  3652. expect(rooms[1].textContent.trim()).toBe("A Lonely Heath");
  3653. expect(rooms[2].textContent.trim()).toBe("A Dark Cave");
  3654. expect(rooms[3].textContent.trim()).toBe("The Palace");
  3655. expect(rooms[4].textContent.trim()).toBe("Macbeth's Castle");
  3656. rooms[4].querySelector('.open-room').click();
  3657. await test_utils.waitUntil(() => _converse.chatboxes.length > 1);
  3658. expect(sizzle('.chatroom', _converse.el).filter(u.isVisible).length).toBe(1); // There should now be an open chatroom
  3659. var view = _converse.chatboxviews.get('inverness@chat.shakespeare.lit');
  3660. expect(view.el.querySelector('.chat-head-chatroom').textContent.trim()).toBe("Macbeth's Castle");
  3661. done();
  3662. }));
  3663. it("shows the number of unread mentions received",
  3664. mock.initConverse(
  3665. null, ['rosterGroupsFetched', 'chatBoxesFetched'], {'allow_bookmarks': false},
  3666. async function (done, _converse) {
  3667. // XXX: we set `allow_bookmarks` to false, so that the groupchats
  3668. // list gets rendered. Otherwise we would have to mock
  3669. // the bookmark stanza exchange.
  3670. test_utils.openControlBox();
  3671. const roomspanel = _converse.chatboxviews.get('controlbox').roomspanel;
  3672. expect(roomspanel.el.querySelectorAll('.available-room').length).toBe(0);
  3673. const room_jid = 'kitchen@conference.shakespeare.lit';
  3674. const message = 'fires: Your attention is required';
  3675. await test_utils.openAndEnterChatRoom(_converse, 'kitchen', 'conference.shakespeare.lit', 'fires');
  3676. expect(roomspanel.el.querySelectorAll('.available-room').length).toBe(1);
  3677. expect(roomspanel.el.querySelectorAll('.msgs-indicator').length).toBe(0);
  3678. const view = _converse.chatboxviews.get(room_jid);
  3679. view.model.set({'minimized': true});
  3680. const contact_jid = mock.cur_names[5].replace(/ /g,'.').toLowerCase() + '@localhost';
  3681. const nick = mock.chatroom_names[0];
  3682. await view.model.onMessage($msg({
  3683. from: room_jid+'/'+nick,
  3684. id: (new Date()).getTime(),
  3685. to: 'dummy@localhost',
  3686. type: 'groupchat'
  3687. }).c('body').t(message).tree());
  3688. await new Promise((resolve, reject) => view.once('messageInserted', resolve));
  3689. await test_utils.waitUntil(() => view.model.messages.length);
  3690. expect(roomspanel.el.querySelectorAll('.available-room').length).toBe(1);
  3691. expect(roomspanel.el.querySelectorAll('.msgs-indicator').length).toBe(1);
  3692. expect(roomspanel.el.querySelector('.msgs-indicator').textContent).toBe('1');
  3693. await view.model.onMessage($msg({
  3694. 'from': room_jid+'/'+nick,
  3695. 'id': (new Date()).getTime(),
  3696. 'to': 'dummy@localhost',
  3697. 'type': 'groupchat'
  3698. }).c('body').t(message).tree());
  3699. await test_utils.waitUntil(() => view.model.messages.length > 1);
  3700. expect(roomspanel.el.querySelectorAll('.available-room').length).toBe(1);
  3701. expect(roomspanel.el.querySelectorAll('.msgs-indicator').length).toBe(1);
  3702. expect(roomspanel.el.querySelector('.msgs-indicator').textContent).toBe('2');
  3703. view.model.set({'minimized': false});
  3704. expect(roomspanel.el.querySelectorAll('.available-room').length).toBe(1);
  3705. expect(roomspanel.el.querySelectorAll('.msgs-indicator').length).toBe(0);
  3706. done();
  3707. }));
  3708. describe("A Chat Status Notification", function () {
  3709. describe("A composing notification", function () {
  3710. it("will be shown if received",
  3711. mock.initConverse(
  3712. null, ['rosterGroupsFetched'], {},
  3713. async function (done, _converse) {
  3714. const room_jid = 'coven@chat.shakespeare.lit';
  3715. await test_utils.openAndEnterChatRoom(_converse, 'coven', 'chat.shakespeare.lit', 'some1');
  3716. const view = _converse.chatboxviews.get('coven@chat.shakespeare.lit');
  3717. const chat_content = view.el.querySelector('.chat-content');
  3718. expect(sizzle('div.chat-info:first', chat_content).pop().textContent)
  3719. .toBe("some1 has entered the groupchat");
  3720. let presence = $pres({
  3721. to: 'dummy@localhost/_converse.js-29092160',
  3722. from: 'coven@chat.shakespeare.lit/newguy'
  3723. })
  3724. .c('x', {xmlns: Strophe.NS.MUC_USER})
  3725. .c('item', {
  3726. 'affiliation': 'none',
  3727. 'jid': 'newguy@localhost/_converse.js-290929789',
  3728. 'role': 'participant'
  3729. });
  3730. _converse.connection._dataRecv(test_utils.createRequest(presence));
  3731. expect(chat_content.querySelectorAll('div.chat-info').length).toBe(2);
  3732. expect(sizzle('div.chat-info:last', chat_content).pop().textContent)
  3733. .toBe("newguy has entered the groupchat");
  3734. presence = $pres({
  3735. to: 'dummy@localhost/_converse.js-29092160',
  3736. from: 'coven@chat.shakespeare.lit/nomorenicks'
  3737. })
  3738. .c('x', {xmlns: Strophe.NS.MUC_USER})
  3739. .c('item', {
  3740. 'affiliation': 'none',
  3741. 'jid': 'nomorenicks@localhost/_converse.js-290929789',
  3742. 'role': 'participant'
  3743. });
  3744. _converse.connection._dataRecv(test_utils.createRequest(presence));
  3745. expect(chat_content.querySelectorAll('div.chat-info').length).toBe(3);
  3746. expect(sizzle('div.chat-info:last', chat_content).pop().textContent)
  3747. .toBe("nomorenicks has entered the groupchat");
  3748. // See XEP-0085 http://xmpp.org/extensions/xep-0085.html#definitions
  3749. // <composing> state
  3750. let msg = $msg({
  3751. from: room_jid+'/newguy',
  3752. id: (new Date()).getTime(),
  3753. to: 'dummy@localhost',
  3754. type: 'groupchat'
  3755. }).c('body').c('composing', {'xmlns': Strophe.NS.CHATSTATES}).tree();
  3756. await view.model.onMessage(msg);
  3757. await test_utils.waitUntil(() => view.el.querySelectorAll('.chat-state-notification').length);
  3758. // Check that the notification appears inside the chatbox in the DOM
  3759. let events = view.el.querySelectorAll('.chat-event');
  3760. expect(events.length).toBe(3);
  3761. expect(events[0].textContent).toEqual('some1 has entered the groupchat');
  3762. expect(events[1].textContent).toEqual('newguy has entered the groupchat');
  3763. expect(events[2].textContent).toEqual('nomorenicks has entered the groupchat');
  3764. let notifications = view.el.querySelectorAll('.chat-state-notification');
  3765. expect(notifications.length).toBe(1);
  3766. expect(notifications[0].textContent).toEqual('newguy is typing');
  3767. const timeout_functions = [];
  3768. spyOn(window, 'setTimeout').and.callFake(function (func, delay) {
  3769. timeout_functions.push(func);
  3770. });
  3771. // Check that it doesn't appear twice
  3772. msg = $msg({
  3773. from: room_jid+'/newguy',
  3774. id: (new Date()).getTime(),
  3775. to: 'dummy@localhost',
  3776. type: 'groupchat'
  3777. }).c('body').c('composing', {'xmlns': Strophe.NS.CHATSTATES}).tree();
  3778. await view.model.onMessage(msg);
  3779. events = view.el.querySelectorAll('.chat-event');
  3780. expect(events.length).toBe(3);
  3781. expect(events[0].textContent).toEqual('some1 has entered the groupchat');
  3782. expect(events[1].textContent).toEqual('newguy has entered the groupchat');
  3783. expect(events[2].textContent).toEqual('nomorenicks has entered the groupchat');
  3784. notifications = view.el.querySelectorAll('.chat-state-notification');
  3785. expect(notifications.length).toBe(1);
  3786. expect(notifications[0].textContent).toEqual('newguy is typing');
  3787. expect(timeout_functions.length).toBe(1);
  3788. // <composing> state for a different occupant
  3789. msg = $msg({
  3790. from: room_jid+'/nomorenicks',
  3791. id: (new Date()).getTime(),
  3792. to: 'dummy@localhost',
  3793. type: 'groupchat'
  3794. }).c('body').c('composing', {'xmlns': Strophe.NS.CHATSTATES}).tree();
  3795. await view.model.onMessage(msg);
  3796. events = view.el.querySelectorAll('.chat-event');
  3797. expect(events.length).toBe(3);
  3798. expect(events[0].textContent).toEqual('some1 has entered the groupchat');
  3799. expect(events[1].textContent).toEqual('newguy has entered the groupchat');
  3800. expect(events[2].textContent).toEqual('nomorenicks has entered the groupchat');
  3801. notifications = view.el.querySelectorAll('.chat-state-notification');
  3802. expect(notifications.length).toBe(2);
  3803. expect(notifications[0].textContent).toEqual('newguy is typing');
  3804. expect(notifications[1].textContent).toEqual('nomorenicks is typing');
  3805. expect(timeout_functions.length).toBe(2);
  3806. // Check that new messages appear under the chat state
  3807. // notifications
  3808. msg = $msg({
  3809. from: `${room_jid}/some1`,
  3810. id: (new Date()).getTime(),
  3811. to: 'dummy@localhost',
  3812. type: 'groupchat'
  3813. }).c('body').t('hello world').tree();
  3814. await view.model.onMessage(msg);
  3815. await new Promise((resolve, reject) => view.once('messageInserted', resolve));
  3816. const messages = view.el.querySelectorAll('.message');
  3817. expect(messages.length).toBe(7);
  3818. expect(view.el.querySelectorAll('.chat-msg').length).toBe(1);
  3819. expect(view.el.querySelector('.chat-msg .chat-msg__text').textContent).toBe('hello world');
  3820. // Test that the composing notifications get removed
  3821. // via timeout.
  3822. timeout_functions[0]();
  3823. events = view.el.querySelectorAll('.chat-event');
  3824. expect(events.length).toBe(3);
  3825. expect(events[0].textContent).toEqual('some1 has entered the groupchat');
  3826. expect(events[1].textContent).toEqual('newguy has entered the groupchat');
  3827. expect(events[2].textContent).toEqual('nomorenicks has entered the groupchat');
  3828. notifications = view.el.querySelectorAll('.chat-state-notification');
  3829. expect(notifications.length).toBe(1);
  3830. expect(notifications[0].textContent).toEqual('nomorenicks is typing');
  3831. timeout_functions[1]();
  3832. events = view.el.querySelectorAll('.chat-event');
  3833. expect(events.length).toBe(3);
  3834. expect(events[0].textContent).toEqual('some1 has entered the groupchat');
  3835. expect(events[1].textContent).toEqual('newguy has entered the groupchat');
  3836. expect(events[2].textContent).toEqual('nomorenicks has entered the groupchat');
  3837. notifications = view.el.querySelectorAll('.chat-state-notification');
  3838. expect(notifications.length).toBe(0);
  3839. done();
  3840. }));
  3841. });
  3842. describe("A paused notification", function () {
  3843. it("will be shown if received",
  3844. mock.initConverse(
  3845. null, ['rosterGroupsFetched', 'chatBoxViewsInitialized'], {},
  3846. async function (done, _converse) {
  3847. await test_utils.openChatRoom(_converse, "coven", 'chat.shakespeare.lit', 'some1');
  3848. const room_jid = 'coven@chat.shakespeare.lit';
  3849. const view = _converse.chatboxviews.get('coven@chat.shakespeare.lit');
  3850. const chat_content = view.el.querySelector('.chat-content');
  3851. /* <presence to="dummy@localhost/_converse.js-29092160"
  3852. * from="coven@chat.shakespeare.lit/some1">
  3853. * <x xmlns="http://jabber.org/protocol/muc#user">
  3854. * <item affiliation="owner" jid="dummy@localhost/_converse.js-29092160" role="moderator"/>
  3855. * <status code="110"/>
  3856. * </x>
  3857. * </presence></body>
  3858. */
  3859. let presence = $pres({
  3860. to: 'dummy@localhost/_converse.js-29092160',
  3861. from: 'coven@chat.shakespeare.lit/some1'
  3862. }).c('x', {xmlns: Strophe.NS.MUC_USER})
  3863. .c('item', {
  3864. 'affiliation': 'owner',
  3865. 'jid': 'dummy@localhost/_converse.js-29092160',
  3866. 'role': 'moderator'
  3867. }).up()
  3868. .c('status', {code: '110'});
  3869. _converse.connection._dataRecv(test_utils.createRequest(presence));
  3870. await test_utils.waitUntil(() => sizzle('div.chat-info:first', chat_content).length);
  3871. expect(sizzle('div.chat-info:first', chat_content).pop().textContent)
  3872. .toBe("some1 has entered the groupchat");
  3873. presence = $pres({
  3874. to: 'dummy@localhost/_converse.js-29092160',
  3875. from: 'coven@chat.shakespeare.lit/newguy'
  3876. })
  3877. .c('x', {xmlns: Strophe.NS.MUC_USER})
  3878. .c('item', {
  3879. 'affiliation': 'none',
  3880. 'jid': 'newguy@localhost/_converse.js-290929789',
  3881. 'role': 'participant'
  3882. });
  3883. _converse.connection._dataRecv(test_utils.createRequest(presence));
  3884. expect(chat_content.querySelectorAll('div.chat-info').length).toBe(2);
  3885. expect(sizzle('div.chat-info:last', chat_content).pop().textContent)
  3886. .toBe("newguy has entered the groupchat");
  3887. presence = $pres({
  3888. to: 'dummy@localhost/_converse.js-29092160',
  3889. from: 'coven@chat.shakespeare.lit/nomorenicks'
  3890. })
  3891. .c('x', {xmlns: Strophe.NS.MUC_USER})
  3892. .c('item', {
  3893. 'affiliation': 'none',
  3894. 'jid': 'nomorenicks@localhost/_converse.js-290929789',
  3895. 'role': 'participant'
  3896. });
  3897. _converse.connection._dataRecv(test_utils.createRequest(presence));
  3898. expect(chat_content.querySelectorAll('div.chat-info').length).toBe(3);
  3899. expect(sizzle('div.chat-info:last', chat_content).pop().textContent)
  3900. .toBe("nomorenicks has entered the groupchat");
  3901. // See XEP-0085 http://xmpp.org/extensions/xep-0085.html#definitions
  3902. // <composing> state
  3903. var msg = $msg({
  3904. from: room_jid+'/newguy',
  3905. id: (new Date()).getTime(),
  3906. to: 'dummy@localhost',
  3907. type: 'groupchat'
  3908. }).c('body').c('composing', {'xmlns': Strophe.NS.CHATSTATES}).tree();
  3909. await view.model.onMessage(msg);
  3910. // Check that the notification appears inside the chatbox in the DOM
  3911. var events = view.el.querySelectorAll('.chat-event');
  3912. expect(events.length).toBe(3);
  3913. expect(events[0].textContent).toEqual('some1 has entered the groupchat');
  3914. expect(events[1].textContent).toEqual('newguy has entered the groupchat');
  3915. expect(events[2].textContent).toEqual('nomorenicks has entered the groupchat');
  3916. var notifications = view.el.querySelectorAll('.chat-state-notification');
  3917. expect(notifications.length).toBe(1);
  3918. expect(notifications[0].textContent).toEqual('newguy is typing');
  3919. // Check that it doesn't appear twice
  3920. msg = $msg({
  3921. from: room_jid+'/newguy',
  3922. id: (new Date()).getTime(),
  3923. to: 'dummy@localhost',
  3924. type: 'groupchat'
  3925. }).c('body').c('composing', {'xmlns': Strophe.NS.CHATSTATES}).tree();
  3926. await view.model.onMessage(msg);
  3927. events = view.el.querySelectorAll('.chat-event');
  3928. expect(events.length).toBe(3);
  3929. expect(events[0].textContent).toEqual('some1 has entered the groupchat');
  3930. expect(events[1].textContent).toEqual('newguy has entered the groupchat');
  3931. expect(events[2].textContent).toEqual('nomorenicks has entered the groupchat');
  3932. notifications = view.el.querySelectorAll('.chat-state-notification');
  3933. expect(notifications.length).toBe(1);
  3934. expect(notifications[0].textContent).toEqual('newguy is typing');
  3935. // <composing> state for a different occupant
  3936. msg = $msg({
  3937. from: room_jid+'/nomorenicks',
  3938. id: (new Date()).getTime(),
  3939. to: 'dummy@localhost',
  3940. type: 'groupchat'
  3941. }).c('body').c('composing', {'xmlns': Strophe.NS.CHATSTATES}).tree();
  3942. await view.model.onMessage(msg);
  3943. events = view.el.querySelectorAll('.chat-event');
  3944. expect(events.length).toBe(3);
  3945. expect(events[0].textContent).toEqual('some1 has entered the groupchat');
  3946. expect(events[1].textContent).toEqual('newguy has entered the groupchat');
  3947. expect(events[2].textContent).toEqual('nomorenicks has entered the groupchat');
  3948. notifications = view.el.querySelectorAll('.chat-state-notification');
  3949. expect(notifications.length).toBe(2);
  3950. expect(notifications[0].textContent).toEqual('newguy is typing');
  3951. expect(notifications[1].textContent).toEqual('nomorenicks is typing');
  3952. // <paused> state from occupant who typed first
  3953. msg = $msg({
  3954. from: room_jid+'/newguy',
  3955. id: (new Date()).getTime(),
  3956. to: 'dummy@localhost',
  3957. type: 'groupchat'
  3958. }).c('body').c('paused', {'xmlns': Strophe.NS.CHATSTATES}).tree();
  3959. await view.model.onMessage(msg);
  3960. events = view.el.querySelectorAll('.chat-event');
  3961. expect(events.length).toBe(3);
  3962. expect(events[0].textContent).toEqual('some1 has entered the groupchat');
  3963. expect(events[1].textContent).toEqual('newguy has entered the groupchat');
  3964. expect(events[2].textContent).toEqual('nomorenicks has entered the groupchat');
  3965. notifications = view.el.querySelectorAll('.chat-state-notification');
  3966. expect(notifications.length).toBe(2);
  3967. expect(notifications[0].textContent).toEqual('nomorenicks is typing');
  3968. expect(notifications[1].textContent).toEqual('newguy has stopped typing');
  3969. done();
  3970. }));
  3971. });
  3972. });
  3973. });
  3974. });
  3975. }));