2
0

muc.js 253 KB

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