muc.js 287 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391
  1. /*global mock, converse, _ */
  2. const $pres = converse.env.$pres;
  3. const $iq = converse.env.$iq;
  4. const $msg = converse.env.$msg;
  5. const Model = converse.env.Model;
  6. const Strophe = converse.env.Strophe;
  7. const Promise = converse.env.Promise;
  8. const sizzle = converse.env.sizzle;
  9. const u = converse.env.utils;
  10. describe("Groupchats", function () {
  11. describe("The \"rooms\" API", function () {
  12. it("has a method 'close' which closes rooms by JID or all rooms when called with no arguments",
  13. mock.initConverse([], {}, async function (done, _converse) {
  14. await mock.openAndEnterChatRoom(_converse, 'lounge@montague.lit', 'romeo');
  15. _converse.connection.IQ_stanzas = [];
  16. await mock.openAndEnterChatRoom(_converse, 'leisure@montague.lit', 'romeo');
  17. _converse.connection.IQ_stanzas = [];
  18. await mock.openAndEnterChatRoom(_converse, 'news@montague.lit', 'romeo');
  19. expect(u.isVisible(_converse.chatboxviews.get('lounge@montague.lit'))).toBeTruthy();
  20. expect(u.isVisible(_converse.chatboxviews.get('leisure@montague.lit'))).toBeTruthy();
  21. expect(u.isVisible(_converse.chatboxviews.get('news@montague.lit'))).toBeTruthy();
  22. await _converse.api.roomviews.close('lounge@montague.lit');
  23. expect(_converse.chatboxviews.get('lounge@montague.lit')).toBeUndefined();
  24. expect(u.isVisible(_converse.chatboxviews.get('leisure@montague.lit'))).toBeTruthy();
  25. expect(u.isVisible(_converse.chatboxviews.get('news@montague.lit'))).toBeTruthy();
  26. await _converse.api.roomviews.close(['leisure@montague.lit', 'news@montague.lit']);
  27. expect(_converse.chatboxviews.get('lounge@montague.lit')).toBeUndefined();
  28. expect(_converse.chatboxviews.get('leisure@montague.lit')).toBeUndefined();
  29. expect(_converse.chatboxviews.get('news@montague.lit')).toBeUndefined();
  30. await mock.openAndEnterChatRoom(_converse, 'lounge@montague.lit', 'romeo');
  31. await mock.openAndEnterChatRoom(_converse, 'leisure@montague.lit', 'romeo');
  32. expect(u.isVisible(_converse.chatboxviews.get('lounge@montague.lit'))).toBeTruthy();
  33. expect(u.isVisible(_converse.chatboxviews.get('leisure@montague.lit'))).toBeTruthy();
  34. await _converse.api.roomviews.close();
  35. expect(_converse.chatboxviews.get('lounge@montague.lit')).toBeUndefined();
  36. expect(_converse.chatboxviews.get('leisure@montague.lit')).toBeUndefined();
  37. done();
  38. }));
  39. it("has a method 'get' which returns a wrapped groupchat (if it exists)",
  40. mock.initConverse([], {}, async function (done, _converse) {
  41. await mock.waitForRoster(_converse, 'current');
  42. const rosterview = document.querySelector('converse-roster');
  43. await u.waitUntil(() => rosterview.querySelectorAll('.roster-group .group-toggle').length, 300);
  44. let muc_jid = 'chillout@montague.lit';
  45. await mock.openAndEnterChatRoom(_converse, muc_jid, 'romeo');
  46. let room = await _converse.api.rooms.get(muc_jid);
  47. expect(room instanceof Object).toBeTruthy();
  48. let chatroomview = _converse.chatboxviews.get(muc_jid);
  49. expect(chatroomview.is_chatroom).toBeTruthy();
  50. expect(u.isVisible(chatroomview)).toBeTruthy();
  51. await chatroomview.close();
  52. // Test with mixed case
  53. muc_jid = 'Leisure@montague.lit';
  54. await mock.openAndEnterChatRoom(_converse, muc_jid, 'romeo');
  55. room = await _converse.api.rooms.get(muc_jid);
  56. expect(room instanceof Object).toBeTruthy();
  57. chatroomview = _converse.chatboxviews.get(muc_jid.toLowerCase());
  58. expect(u.isVisible(chatroomview)).toBeTruthy();
  59. muc_jid = 'leisure@montague.lit';
  60. room = await _converse.api.rooms.get(muc_jid);
  61. expect(room instanceof Object).toBeTruthy();
  62. chatroomview = _converse.chatboxviews.get(muc_jid.toLowerCase());
  63. expect(u.isVisible(chatroomview)).toBeTruthy();
  64. muc_jid = 'leiSure@montague.lit';
  65. room = await _converse.api.rooms.get(muc_jid);
  66. expect(room instanceof Object).toBeTruthy();
  67. chatroomview = _converse.chatboxviews.get(muc_jid.toLowerCase());
  68. expect(u.isVisible(chatroomview)).toBeTruthy();
  69. chatroomview.close();
  70. // Non-existing room
  71. muc_jid = 'chillout2@montague.lit';
  72. room = await _converse.api.rooms.get(muc_jid);
  73. expect(room).toBe(null);
  74. done();
  75. }));
  76. it("has a method 'open' which opens (optionally configures) and returns a wrapped chat box",
  77. mock.initConverse(['chatBoxesFetched'], {}, async function (done, _converse) {
  78. // Mock 'getDiscoInfo', otherwise the room won't be
  79. // displayed as it waits first for the features to be returned
  80. // (when it's a new room being created).
  81. spyOn(_converse.ChatRoom.prototype, 'getDiscoInfo').and.callFake(() => Promise.resolve());
  82. let jid = 'lounge@montague.lit';
  83. let chatroomview, IQ_id;
  84. await mock.openControlBox(_converse);
  85. await mock.waitForRoster(_converse, 'current');
  86. const rosterview = document.querySelector('converse-roster');
  87. await u.waitUntil(() => rosterview.querySelectorAll('.roster-group .group-toggle').length);
  88. let room = await _converse.api.rooms.open(jid);
  89. // Test on groupchat that's not yet open
  90. expect(room instanceof Model).toBeTruthy();
  91. chatroomview = _converse.chatboxviews.get(jid);
  92. expect(chatroomview.is_chatroom).toBeTruthy();
  93. await u.waitUntil(() => u.isVisible(chatroomview));
  94. // Test again, now that the room exists.
  95. room = await _converse.api.rooms.open(jid);
  96. expect(room instanceof Model).toBeTruthy();
  97. chatroomview = _converse.chatboxviews.get(jid);
  98. expect(chatroomview.is_chatroom).toBeTruthy();
  99. expect(u.isVisible(chatroomview)).toBeTruthy();
  100. await chatroomview.close();
  101. // Test with mixed case in JID
  102. jid = 'Leisure@montague.lit';
  103. room = await _converse.api.rooms.open(jid);
  104. expect(room instanceof Model).toBeTruthy();
  105. chatroomview = _converse.chatboxviews.get(jid.toLowerCase());
  106. await u.waitUntil(() => u.isVisible(chatroomview));
  107. jid = 'leisure@montague.lit';
  108. room = await _converse.api.rooms.open(jid);
  109. expect(room instanceof Model).toBeTruthy();
  110. chatroomview = _converse.chatboxviews.get(jid.toLowerCase());
  111. await u.waitUntil(() => u.isVisible(chatroomview));
  112. jid = 'leiSure@montague.lit';
  113. room = await _converse.api.rooms.open(jid);
  114. expect(room instanceof Model).toBeTruthy();
  115. chatroomview = _converse.chatboxviews.get(jid.toLowerCase());
  116. await u.waitUntil(() => u.isVisible(chatroomview));
  117. chatroomview.close();
  118. _converse.muc_instant_rooms = false;
  119. const sendIQ = _converse.connection.sendIQ;
  120. spyOn(_converse.connection, 'sendIQ').and.callFake(function (iq, callback, errback) {
  121. IQ_id = sendIQ.bind(this)(iq, callback, errback);
  122. });
  123. // Test with configuration
  124. room = await _converse.api.rooms.open('room@conference.example.org', {
  125. 'nick': 'some1',
  126. 'auto_configure': true,
  127. 'roomconfig': {
  128. 'getmemberlist': ['moderator', 'participant'],
  129. 'changesubject': false,
  130. 'membersonly': true,
  131. 'persistentroom': true,
  132. 'publicroom': true,
  133. 'roomdesc': 'Welcome to this groupchat',
  134. 'whois': 'anyone'
  135. }
  136. });
  137. expect(room instanceof Model).toBeTruthy();
  138. chatroomview = _converse.chatboxviews.get('room@conference.example.org');
  139. // We pretend this is a new room, so no disco info is returned.
  140. const features_stanza = $iq({
  141. from: 'room@conference.example.org',
  142. 'id': IQ_id,
  143. 'to': 'romeo@montague.lit/desktop',
  144. 'type': 'error'
  145. }).c('error', {'type': 'cancel'})
  146. .c('item-not-found', {'xmlns': "urn:ietf:params:xml:ns:xmpp-stanzas"});
  147. _converse.connection._dataRecv(mock.createRequest(features_stanza));
  148. /* <presence xmlns="jabber:client" to="romeo@montague.lit/pda" from="room@conference.example.org/yo">
  149. * <x xmlns="http://jabber.org/protocol/muc#user">
  150. * <item affiliation="owner" jid="romeo@montague.lit/pda" role="moderator"/>
  151. * <status code="110"/>
  152. * <status code="201"/>
  153. * </x>
  154. * </presence>
  155. */
  156. const presence = $pres({
  157. from:'room@conference.example.org/some1',
  158. to:'romeo@montague.lit/pda'
  159. })
  160. .c('x', {xmlns:'http://jabber.org/protocol/muc#user'})
  161. .c('item', {
  162. affiliation: 'owner',
  163. jid: 'romeo@montague.lit/pda',
  164. role: 'moderator'
  165. }).up()
  166. .c('status', {code:'110'}).up()
  167. .c('status', {code:'201'});
  168. _converse.connection._dataRecv(mock.createRequest(presence));
  169. expect(_converse.connection.sendIQ).toHaveBeenCalled();
  170. const IQ_stanzas = _converse.connection.IQ_stanzas;
  171. const iq = await u.waitUntil(() => IQ_stanzas.filter(s => s.querySelector(`query[xmlns="${Strophe.NS.MUC_OWNER}"]`)).pop());
  172. expect(Strophe.serialize(iq)).toBe(
  173. `<iq id="${iq.getAttribute('id')}" to="room@conference.example.org" type="get" xmlns="jabber:client">`+
  174. `<query xmlns="http://jabber.org/protocol/muc#owner"/></iq>`);
  175. const node = u.toStanza(`
  176. <iq xmlns="jabber:client"
  177. type="result"
  178. to="romeo@montague.lit/pda"
  179. from="room@conference.example.org" id="${iq.getAttribute('id')}">
  180. <query xmlns="http://jabber.org/protocol/muc#owner">
  181. <x xmlns="jabber:x:data" type="form">
  182. <title>Configuration for room@conference.example.org</title>
  183. <instructions>Complete and submit this form to configure the room.</instructions>
  184. <field var="FORM_TYPE" type="hidden">
  185. <value>http://jabber.org/protocol/muc#roomconfig</value>
  186. </field>
  187. <field type="text-single" var="muc#roomconfig_roomname" label="Name">
  188. <value>Room</value>
  189. </field>
  190. <field type="text-single" var="muc#roomconfig_roomdesc" label="Description"><value/></field>
  191. <field type="boolean" var="muc#roomconfig_persistentroom" label="Make Room Persistent?"/>
  192. <field type="boolean" var="muc#roomconfig_publicroom" label="Make Room Publicly Searchable?"><value>1</value></field>
  193. <field type="boolean" var="muc#roomconfig_changesubject" label="Allow Occupants to Change Subject?"/>
  194. <field type="list-single" var="muc#roomconfig_whois" label="Who May Discover Real JIDs?"><option label="Moderators Only">
  195. <value>moderators</value></option><option label="Anyone"><value>anyone</value></option>
  196. </field>
  197. <field label="Roles and Affiliations that May Retrieve Member List"
  198. type="list-multi"
  199. var="muc#roomconfig_getmemberlist">
  200. <value>moderator</value>
  201. <value>participant</value>
  202. <value>visitor</value>
  203. </field>
  204. <field type="text-private" var="muc#roomconfig_roomsecret" label="Password"><value/></field>
  205. <field type="boolean" var="muc#roomconfig_moderatedroom" label="Make Room Moderated?"/>
  206. <field type="boolean" var="muc#roomconfig_membersonly" label="Make Room Members-Only?"/>
  207. <field type="text-single" var="muc#roomconfig_historylength" label="Maximum Number of History Messages Returned by Room">
  208. <value>20</value></field>
  209. </x>
  210. </query>
  211. </iq>`);
  212. spyOn(chatroomview.model, 'sendConfiguration').and.callThrough();
  213. _converse.connection._dataRecv(mock.createRequest(node));
  214. await u.waitUntil(() => chatroomview.model.sendConfiguration.calls.count() === 1);
  215. const sent_stanza = IQ_stanzas.filter(s => s.getAttribute('type') === 'set').pop();
  216. expect(sizzle('field[var="muc#roomconfig_roomname"] value', sent_stanza).pop().textContent.trim()).toBe('Room');
  217. expect(sizzle('field[var="muc#roomconfig_roomdesc"] value', sent_stanza).pop().textContent.trim()).toBe('Welcome to this groupchat');
  218. expect(sizzle('field[var="muc#roomconfig_persistentroom"] value', sent_stanza).pop().textContent.trim()).toBe('1');
  219. expect(sizzle('field[var="muc#roomconfig_getmemberlist"] value', sent_stanza).map(e => e.textContent.trim()).join(' ')).toBe('moderator participant');
  220. expect(sizzle('field[var="muc#roomconfig_publicroom"] value ', sent_stanza).pop().textContent.trim()).toBe('1');
  221. expect(sizzle('field[var="muc#roomconfig_changesubject"] value', sent_stanza).pop().textContent.trim()).toBe('0');
  222. expect(sizzle('field[var="muc#roomconfig_whois"] value ', sent_stanza).pop().textContent.trim()).toBe('anyone');
  223. expect(sizzle('field[var="muc#roomconfig_membersonly"] value', sent_stanza).pop().textContent.trim()).toBe('1');
  224. expect(sizzle('field[var="muc#roomconfig_historylength"] value', sent_stanza).pop().textContent.trim()).toBe('20');
  225. done();
  226. }));
  227. });
  228. describe("An instant groupchat", function () {
  229. it("will be created when muc_instant_rooms is set to true",
  230. mock.initConverse(['chatBoxesFetched'], {}, async function (done, _converse) {
  231. let IQ_stanzas = _converse.connection.IQ_stanzas;
  232. const muc_jid = 'lounge@montague.lit';
  233. const nick = 'nicky';
  234. await mock.openChatRoom(_converse, 'lounge', 'montague.lit', 'romeo');
  235. const disco_selector = `iq[to="${muc_jid}"] query[xmlns="http://jabber.org/protocol/disco#info"]`;
  236. const stanza = await u.waitUntil(() => IQ_stanzas.filter(iq => iq.querySelector(disco_selector)).pop());
  237. // We pretend this is a new room, so no disco info is returned.
  238. const features_stanza = $iq({
  239. 'from': 'lounge@montague.lit',
  240. 'id': stanza.getAttribute('id'),
  241. 'to': 'romeo@montague.lit/desktop',
  242. 'type': 'error'
  243. }).c('error', {'type': 'cancel'})
  244. .c('item-not-found', {'xmlns': "urn:ietf:params:xml:ns:xmpp-stanzas"});
  245. _converse.connection._dataRecv(mock.createRequest(features_stanza));
  246. const view = _converse.chatboxviews.get('lounge@montague.lit');
  247. spyOn(view.model, 'join').and.callThrough();
  248. await mock.waitForReservedNick(_converse, muc_jid, '');
  249. const input = await u.waitUntil(() => view.querySelector('input[name="nick"]'), 1000);
  250. expect(view.model.session.get('connection_status')).toBe(converse.ROOMSTATUS.NICKNAME_REQUIRED);
  251. input.value = nick;
  252. view.querySelector('input[type=submit]').click();
  253. expect(view.model.join).toHaveBeenCalled();
  254. _converse.connection.IQ_stanzas = [];
  255. await mock.getRoomFeatures(_converse, muc_jid);
  256. await u.waitUntil(() => view.model.session.get('connection_status') === converse.ROOMSTATUS.CONNECTING);
  257. await mock.receiveOwnMUCPresence(_converse, muc_jid, nick);
  258. // The user has just entered the room (because join was called)
  259. // and receives their own presence from the server.
  260. // See example 24:
  261. // https://xmpp.org/extensions/xep-0045.html#enter-pres
  262. //
  263. /* <presence xmlns="jabber:client" to="jordie.langen@chat.example.org/converse.js-11659299" from="myroom@conference.chat.example.org/jc">
  264. * <x xmlns="http://jabber.org/protocol/muc#user">
  265. * <item jid="jordie.langen@chat.example.org/converse.js-11659299" affiliation="owner" role="moderator"/>
  266. * <status code="110"/>
  267. * <status code="201"/>
  268. * </x>
  269. * </presence>
  270. */
  271. const presence = $pres({
  272. to:'romeo@montague.lit/orchard',
  273. from:'lounge@montague.lit/nicky',
  274. id:'5025e055-036c-4bc5-a227-706e7e352053'
  275. }).c('x').attrs({xmlns:'http://jabber.org/protocol/muc#user'})
  276. .c('item').attrs({
  277. affiliation: 'owner',
  278. jid: 'romeo@montague.lit/orchard',
  279. role: 'moderator'
  280. }).up()
  281. .c('status').attrs({code:'110'}).up()
  282. .c('status').attrs({code:'201'}).nodeTree;
  283. _converse.connection._dataRecv(mock.createRequest(presence));
  284. await u.waitUntil(() => view.model.session.get('connection_status') === converse.ROOMSTATUS.ENTERED);
  285. await mock.returnMemberLists(_converse, muc_jid);
  286. const num_info_msgs = await u.waitUntil(() => view.querySelectorAll('.chat-content .chat-info').length);
  287. expect(num_info_msgs).toBe(1);
  288. const info_texts = Array.from(view.querySelectorAll('.chat-content .chat-info')).map(e => e.textContent.trim());
  289. expect(info_texts[0]).toBe('A new groupchat has been created');
  290. const csntext = await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent);
  291. expect(csntext.trim()).toEqual("nicky has entered the groupchat");
  292. // An instant room is created by saving the default configuratoin.
  293. //
  294. /* <iq to="myroom@conference.chat.example.org" type="set" xmlns="jabber:client" id="5025e055-036c-4bc5-a227-706e7e352053:sendIQ">
  295. * <query xmlns="http://jabber.org/protocol/muc#owner"><x xmlns="jabber:x:data" type="submit"/></query>
  296. * </iq>
  297. */
  298. const selector = `query[xmlns="${Strophe.NS.MUC_OWNER}"]`;
  299. IQ_stanzas = _converse.connection.IQ_stanzas;
  300. const iq = await u.waitUntil(() => IQ_stanzas.filter(s => s.querySelector(selector)).pop());
  301. expect(Strophe.serialize(iq)).toBe(
  302. `<iq id="${iq.getAttribute('id')}" to="lounge@montague.lit" type="set" xmlns="jabber:client">`+
  303. `<query xmlns="http://jabber.org/protocol/muc#owner"><x type="submit" xmlns="jabber:x:data"/>`+
  304. `</query></iq>`);
  305. done();
  306. }));
  307. });
  308. describe("A Groupchat", function () {
  309. it("Can be configured to show cached messages before being joined",
  310. mock.initConverse(['discoInitialized'],
  311. {
  312. 'muc_show_logs_before_join': true,
  313. 'archived_messages_page_size': 2,
  314. 'muc_nickname_from_jid': false,
  315. 'muc_clear_messages_on_leave': false,
  316. }, async function (done, _converse) {
  317. const { api } = _converse;
  318. const muc_jid = 'orchard@chat.shakespeare.lit';
  319. const nick = 'romeo';
  320. api.rooms.open(muc_jid);
  321. await mock.getRoomFeatures(_converse, muc_jid);
  322. await mock.waitForReservedNick(_converse, muc_jid);
  323. const view = _converse.chatboxviews.get(muc_jid);
  324. await view.model.messages.fetched;
  325. view.model.messages.create({
  326. 'type': 'groupchat',
  327. 'to': muc_jid,
  328. 'from': `${_converse.bare_jid}/orchard`,
  329. 'body': 'Hello world',
  330. 'message': 'Hello world',
  331. 'time': '2021-02-02T12:00:00Z'
  332. });
  333. expect(view.model.session.get('connection_status')).toBe(converse.ROOMSTATUS.NICKNAME_REQUIRED);
  334. await u.waitUntil(() => view.querySelectorAll('converse-chat-message').length === 1);
  335. const sel = 'converse-message-history converse-chat-message .chat-msg__text';
  336. await u.waitUntil(() => view.querySelector(sel)?.textContent.trim());
  337. expect(view.querySelector(sel).textContent.trim()).toBe('Hello world')
  338. const nick_input = await u.waitUntil(() => view.querySelector('[name="nick"]'));
  339. nick_input.value = nick;
  340. view.querySelector('.muc-nickname-form input[type="submit"]').click();
  341. _converse.connection.IQ_stanzas = [];
  342. await mock.getRoomFeatures(_converse, muc_jid);
  343. await u.waitUntil(() => view.model.session.get('connection_status') === converse.ROOMSTATUS.CONNECTING);
  344. await mock.receiveOwnMUCPresence(_converse, muc_jid, nick);
  345. return done();
  346. }));
  347. it("maintains its state across reloads",
  348. mock.initConverse([], {
  349. 'clear_messages_on_reconnection': true,
  350. 'enable_smacks': false
  351. }, async function (done, _converse) {
  352. const nick = 'romeo';
  353. const sent_IQs = _converse.connection.IQ_stanzas;
  354. const muc_jid = 'lounge@montague.lit'
  355. await mock.openAndEnterChatRoom(_converse, muc_jid, nick, [], []);
  356. const view = _converse.chatboxviews.get(muc_jid);
  357. let iq_get = await u.waitUntil(() => sent_IQs.filter(iq => iq.querySelector(`iq query[xmlns="${Strophe.NS.MAM}"]`)).pop());
  358. const first_msg_id = _converse.connection.getUniqueId();
  359. const last_msg_id = _converse.connection.getUniqueId();
  360. let message = u.toStanza(
  361. `<message xmlns="jabber:client"
  362. to="romeo@montague.lit/orchard"
  363. from="${muc_jid}">
  364. <result xmlns="urn:xmpp:mam:2" queryid="${iq_get.querySelector('query').getAttribute('queryid')}" id="${first_msg_id}">
  365. <forwarded xmlns="urn:xmpp:forward:0">
  366. <delay xmlns="urn:xmpp:delay" stamp="2018-01-09T06:15:23Z"/>
  367. <message from="${muc_jid}/some1" type="groupchat">
  368. <body>1st Message</body>
  369. </message>
  370. </forwarded>
  371. </result>
  372. </message>`);
  373. _converse.connection._dataRecv(mock.createRequest(message));
  374. message = u.toStanza(
  375. `<message xmlns="jabber:client"
  376. to="romeo@montague.lit/orchard"
  377. from="${muc_jid}">
  378. <result xmlns="urn:xmpp:mam:2" queryid="${iq_get.querySelector('query').getAttribute('queryid')}" id="${last_msg_id}">
  379. <forwarded xmlns="urn:xmpp:forward:0">
  380. <delay xmlns="urn:xmpp:delay" stamp="2018-01-09T06:16:23Z"/>
  381. <message from="${muc_jid}/some1" type="groupchat">
  382. <body>2nd Message</body>
  383. </message>
  384. </forwarded>
  385. </result>
  386. </message>`);
  387. _converse.connection._dataRecv(mock.createRequest(message));
  388. const result = u.toStanza(
  389. `<iq type='result' id='${iq_get.getAttribute('id')}'>
  390. <fin xmlns='urn:xmpp:mam:2'>
  391. <set xmlns='http://jabber.org/protocol/rsm'>
  392. <first index='0'>${first_msg_id}</first>
  393. <last>${last_msg_id}</last>
  394. <count>2</count>
  395. </set>
  396. </fin>
  397. </iq>`);
  398. _converse.connection._dataRecv(mock.createRequest(result));
  399. await u.waitUntil(() => view.querySelectorAll('.chat-msg__text').length === 2);
  400. while (sent_IQs.length) { sent_IQs.pop(); } // Clear so that we don't match the older query
  401. await _converse.api.connection.reconnect();
  402. await mock.getRoomFeatures(_converse, muc_jid, []);
  403. await u.waitUntil(() => (view.model.session.get('connection_status') === converse.ROOMSTATUS.CONNECTING));
  404. // The user has just entered the room (because join was called)
  405. // and receives their own presence from the server.
  406. // See example 24: https://xmpp.org/extensions/xep-0045.html#enter-pres
  407. await mock.receiveOwnMUCPresence(_converse, muc_jid, nick);
  408. message = u.toStanza(`
  409. <message xmlns="jabber:client" type="groupchat" id="918172de-d5c5-4da4-b388-446ef4a05bec" to="${_converse.jid}" xml:lang="en" from="${muc_jid}/juliet">
  410. <body>Wherefore art though?</body>
  411. <active xmlns="http://jabber.org/protocol/chatstates"/>
  412. <origin-id xmlns="urn:xmpp:sid:0" id="918172de-d5c5-4da4-b388-446ef4a05bec"/>
  413. <stanza-id xmlns="urn:xmpp:sid:0" id="88cc9c93-a8f4-4dd5-b02a-d19855eb6303" by="${muc_jid}"/>
  414. <delay xmlns="urn:xmpp:delay" stamp="2020-07-14T17:46:47Z" from="juliet@shakespeare.lit"/>
  415. </message>`);
  416. _converse.connection._dataRecv(mock.createRequest(message));
  417. message = u.toStanza(`
  418. <message xmlns="jabber:client" type="groupchat" id="awQo6a-mi-Wa6NYh" to="${_converse.jid}" from="${muc_jid}/ews000" xml:lang="en">
  419. <composing xmlns="http://jabber.org/protocol/chatstates"/>
  420. <no-store xmlns="urn:xmpp:hints"/>
  421. <no-permanent-store xmlns="urn:xmpp:hints"/>
  422. <delay xmlns="urn:xmpp:delay" stamp="2020-07-14T17:46:54Z" from="juliet@shakespeare.lit"/>
  423. </message>`);
  424. _converse.connection._dataRecv(mock.createRequest(message));
  425. const affs = _converse.muc_fetch_members;
  426. const all_affiliations = Array.isArray(affs) ? affs : (affs ? ['member', 'admin', 'owner'] : []);
  427. await mock.returnMemberLists(_converse, muc_jid, [], all_affiliations);
  428. iq_get = await u.waitUntil(() => sent_IQs.filter(iq => iq.querySelector(`iq query[xmlns="${Strophe.NS.MAM}"]`)).pop());
  429. expect(Strophe.serialize(iq_get)).toBe(
  430. `<iq id="${iq_get.getAttribute('id')}" to="${muc_jid}" type="set" xmlns="jabber:client">`+
  431. `<query queryid="${iq_get.querySelector('query').getAttribute('queryid')}" xmlns="${Strophe.NS.MAM}">`+
  432. `<x type="submit" xmlns="jabber:x:data">`+
  433. `<field type="hidden" var="FORM_TYPE"><value>urn:xmpp:mam:2</value></field>`+
  434. `</x>`+
  435. `<set xmlns="http://jabber.org/protocol/rsm"><before></before><max>50</max></set>`+
  436. `</query>`+
  437. `</iq>`);
  438. done();
  439. }));
  440. describe("upon being entered", function () {
  441. it("will fetch the member list if muc_fetch_members is true",
  442. mock.initConverse([], {'muc_fetch_members': true}, async function (done, _converse) {
  443. let sent_IQs = _converse.connection.IQ_stanzas;
  444. const muc_jid = 'lounge@montague.lit';
  445. await mock.openAndEnterChatRoom(_converse, muc_jid, 'romeo');
  446. let view = _converse.chatboxviews.get(muc_jid);
  447. expect(sent_IQs.filter(iq => iq.querySelector('query item[affiliation]')).length).toBe(3);
  448. // Check in reverse order that we requested all three lists
  449. const owner_iq = sent_IQs.pop();
  450. expect(Strophe.serialize(owner_iq)).toBe(
  451. `<iq id="${owner_iq.getAttribute('id')}" to="${muc_jid}" type="get" xmlns="jabber:client">`+
  452. `<query xmlns="http://jabber.org/protocol/muc#admin"><item affiliation="owner"/></query>`+
  453. `</iq>`);
  454. const admin_iq = sent_IQs.pop();
  455. expect(Strophe.serialize(admin_iq)).toBe(
  456. `<iq id="${admin_iq.getAttribute('id')}" to="${muc_jid}" type="get" xmlns="jabber:client">`+
  457. `<query xmlns="http://jabber.org/protocol/muc#admin"><item affiliation="admin"/></query>`+
  458. `</iq>`);
  459. const member_iq = sent_IQs.pop();
  460. expect(Strophe.serialize(member_iq)).toBe(
  461. `<iq id="${member_iq.getAttribute('id')}" to="${muc_jid}" type="get" xmlns="jabber:client">`+
  462. `<query xmlns="http://jabber.org/protocol/muc#admin"><item affiliation="member"/></query>`+
  463. `</iq>`);
  464. view.close();
  465. _converse.connection.IQ_stanzas = [];
  466. sent_IQs = _converse.connection.IQ_stanzas;
  467. _converse.muc_fetch_members = false;
  468. await mock.openAndEnterChatRoom(_converse, 'orchard@montague.lit', 'romeo');
  469. view = _converse.chatboxviews.get('orchard@montague.lit');
  470. expect(sent_IQs.filter(iq => iq.querySelector('query item[affiliation]')).length).toBe(0);
  471. await view.close();
  472. _converse.connection.IQ_stanzas = [];
  473. sent_IQs = _converse.connection.IQ_stanzas;
  474. _converse.muc_fetch_members = ['admin'];
  475. await mock.openAndEnterChatRoom(_converse, 'courtyard@montague.lit', 'romeo');
  476. view = _converse.chatboxviews.get('courtyard@montague.lit');
  477. expect(sent_IQs.filter(iq => iq.querySelector('query item[affiliation]')).length).toBe(1);
  478. expect(sent_IQs.filter(iq => iq.querySelector('query item[affiliation="admin"]')).length).toBe(1);
  479. view.close();
  480. _converse.connection.IQ_stanzas = [];
  481. sent_IQs = _converse.connection.IQ_stanzas;
  482. _converse.muc_fetch_members = ['owner'];
  483. await mock.openAndEnterChatRoom(_converse, 'garden@montague.lit', 'romeo');
  484. view = _converse.chatboxviews.get('garden@montague.lit');
  485. expect(sent_IQs.filter(iq => iq.querySelector('query item[affiliation]')).length).toBe(1);
  486. expect(sent_IQs.filter(iq => iq.querySelector('query item[affiliation="owner"]')).length).toBe(1);
  487. view.close();
  488. done();
  489. }));
  490. describe("when fetching the member lists", function () {
  491. it("gracefully handles being forbidden from fetching the lists for certain affiliations",
  492. mock.initConverse([], {'muc_fetch_members': true}, async function (done, _converse) {
  493. const sent_IQs = _converse.connection.IQ_stanzas;
  494. const muc_jid = 'lounge@montague.lit';
  495. const features = [
  496. 'http://jabber.org/protocol/muc',
  497. 'jabber:iq:register',
  498. 'muc_hidden',
  499. 'muc_membersonly',
  500. 'muc_passwordprotected',
  501. Strophe.NS.MAM,
  502. Strophe.NS.SID
  503. ];
  504. const nick = 'romeo';
  505. await _converse.api.rooms.open(muc_jid);
  506. await mock.getRoomFeatures(_converse, muc_jid, features);
  507. await mock.waitForReservedNick(_converse, muc_jid, nick);
  508. mock.receiveOwnMUCPresence(_converse, muc_jid, nick);
  509. const view = _converse.chatboxviews.get(muc_jid);
  510. await u.waitUntil(() => (view.model.session.get('connection_status') === converse.ROOMSTATUS.ENTERED));
  511. // Check in reverse order that we requested all three lists
  512. const owner_iq = sent_IQs.pop();
  513. expect(Strophe.serialize(owner_iq)).toBe(
  514. `<iq id="${owner_iq.getAttribute('id')}" to="${muc_jid}" type="get" xmlns="jabber:client">`+
  515. `<query xmlns="http://jabber.org/protocol/muc#admin"><item affiliation="owner"/></query>`+
  516. `</iq>`);
  517. const admin_iq = sent_IQs.pop();
  518. expect(Strophe.serialize(admin_iq)).toBe(
  519. `<iq id="${admin_iq.getAttribute('id')}" to="${muc_jid}" type="get" xmlns="jabber:client">`+
  520. `<query xmlns="http://jabber.org/protocol/muc#admin"><item affiliation="admin"/></query>`+
  521. `</iq>`);
  522. const member_iq = sent_IQs.pop();
  523. expect(Strophe.serialize(member_iq)).toBe(
  524. `<iq id="${member_iq.getAttribute('id')}" to="${muc_jid}" type="get" xmlns="jabber:client">`+
  525. `<query xmlns="http://jabber.org/protocol/muc#admin"><item affiliation="member"/></query>`+
  526. `</iq>`);
  527. // It might be that the user is not allowed to fetch certain lists.
  528. let err_stanza = u.toStanza(
  529. `<iq xmlns="jabber:client" type="error" to="${_converse.jid}" from="${muc_jid}" id="${admin_iq.getAttribute('id')}">
  530. <error type="auth"><forbidden xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"/></error>
  531. </iq>`);
  532. _converse.connection._dataRecv(mock.createRequest(err_stanza));
  533. err_stanza = u.toStanza(
  534. `<iq xmlns="jabber:client" type="error" to="${_converse.jid}" from="${muc_jid}" id="${owner_iq.getAttribute('id')}">
  535. <error type="auth"><forbidden xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"/></error>
  536. </iq>`);
  537. _converse.connection._dataRecv(mock.createRequest(err_stanza));
  538. // Now the service sends the member lists to the user
  539. const member_list_stanza = $iq({
  540. 'from': muc_jid,
  541. 'id': member_iq.getAttribute('id'),
  542. 'to': 'romeo@montague.lit/orchard',
  543. 'type': 'result'
  544. }).c('query', {'xmlns': Strophe.NS.MUC_ADMIN})
  545. .c('item', {
  546. 'affiliation': 'member',
  547. 'jid': 'hag66@shakespeare.lit',
  548. 'nick': 'thirdwitch',
  549. 'role': 'participant'
  550. });
  551. _converse.connection._dataRecv(mock.createRequest(member_list_stanza));
  552. await u.waitUntil(() => view.model.occupants.length > 1);
  553. expect(view.model.occupants.length).toBe(2);
  554. // The existing owner occupant should not have their
  555. // affiliation removed due to the owner list
  556. // not being returned (forbidden err).
  557. expect(view.model.occupants.findWhere({'jid': _converse.bare_jid}).get('affiliation')).toBe('owner');
  558. expect(view.model.occupants.findWhere({'jid': 'hag66@shakespeare.lit'}).get('affiliation')).toBe('member');
  559. done();
  560. }));
  561. });
  562. });
  563. describe("topic", function () {
  564. it("is shown the header", mock.initConverse([], {}, async function (done, _converse) {
  565. await mock.openAndEnterChatRoom(_converse, 'jdev@conference.jabber.org', 'jc');
  566. const text = 'Jabber/XMPP Development | RFCs and Extensions: https://xmpp.org/ | Protocol and XSF discussions: xsf@muc.xmpp.org';
  567. let stanza = u.toStanza(`
  568. <message xmlns="jabber:client" to="${_converse.jid}" type="groupchat" from="jdev@conference.jabber.org/ralphm">
  569. <subject>${text}</subject>
  570. <delay xmlns="urn:xmpp:delay" stamp="2014-02-04T09:35:39Z" from="jdev@conference.jabber.org"/>
  571. <x xmlns="jabber:x:delay" stamp="20140204T09:35:39" from="jdev@conference.jabber.org"/>
  572. </message>`);
  573. _converse.connection._dataRecv(mock.createRequest(stanza));
  574. const view = _converse.chatboxviews.get('jdev@conference.jabber.org');
  575. await new Promise(resolve => view.model.once('change:subject', resolve));
  576. const head_desc = await u.waitUntil(() => view.querySelector('.chat-head__desc'), 1000);
  577. expect(head_desc?.textContent.trim()).toBe(text);
  578. stanza = u.toStanza(
  579. `<message xmlns="jabber:client" to="${_converse.jid}" type="groupchat" from="jdev@conference.jabber.org/ralphm">
  580. <subject>This is a message subject</subject>
  581. <body>This is a message</body>
  582. </message>`);
  583. _converse.connection._dataRecv(mock.createRequest(stanza));
  584. await u.waitUntil(() => view.querySelectorAll('.chat-msg__text').length);
  585. expect(sizzle('.chat-msg__subject', view).length).toBe(1);
  586. expect(sizzle('.chat-msg__subject', view).pop().textContent.trim()).toBe('This is a message subject');
  587. expect(sizzle('.chat-msg__text').length).toBe(1);
  588. expect(sizzle('.chat-msg__text').pop().textContent.trim()).toBe('This is a message');
  589. expect(view.querySelector('.chat-head__desc').textContent.trim()).toBe(text);
  590. done();
  591. }));
  592. it("can be toggled by the user", mock.initConverse([], {}, async function (done, _converse) {
  593. await mock.openAndEnterChatRoom(_converse, 'jdev@conference.jabber.org', 'jc');
  594. const text = 'Jabber/XMPP Development | RFCs and Extensions: https://xmpp.org/ | Protocol and XSF discussions: xsf@muc.xmpp.org';
  595. let stanza = u.toStanza(`
  596. <message xmlns="jabber:client" to="${_converse.jid}" type="groupchat" from="jdev@conference.jabber.org/ralphm">
  597. <subject>${text}</subject>
  598. <delay xmlns="urn:xmpp:delay" stamp="2014-02-04T09:35:39Z" from="jdev@conference.jabber.org"/>
  599. <x xmlns="jabber:x:delay" stamp="20140204T09:35:39" from="jdev@conference.jabber.org"/>
  600. </message>`);
  601. _converse.connection._dataRecv(mock.createRequest(stanza));
  602. const view = _converse.chatboxviews.get('jdev@conference.jabber.org');
  603. await new Promise(resolve => view.model.once('change:subject', resolve));
  604. const head_desc = await u.waitUntil(() => view.querySelector('.chat-head__desc'));
  605. expect(head_desc?.textContent.trim()).toBe(text);
  606. stanza = u.toStanza(
  607. `<message xmlns="jabber:client" to="${_converse.jid}" type="groupchat" from="jdev@conference.jabber.org/ralphm">
  608. <subject>This is a message subject</subject>
  609. <body>This is a message</body>
  610. </message>`);
  611. _converse.connection._dataRecv(mock.createRequest(stanza));
  612. await u.waitUntil(() => view.querySelectorAll('.chat-msg__text').length);
  613. expect(sizzle('.chat-msg__subject', view).length).toBe(1);
  614. expect(sizzle('.chat-msg__subject', view).pop().textContent.trim()).toBe('This is a message subject');
  615. expect(sizzle('.chat-msg__text').length).toBe(1);
  616. expect(sizzle('.chat-msg__text').pop().textContent.trim()).toBe('This is a message');
  617. const topic_el = view.querySelector('.chat-head__desc');
  618. expect(topic_el.textContent.trim()).toBe(text);
  619. expect(u.isVisible(topic_el)).toBe(true);
  620. const toggle = view.querySelector('.hide-topic');
  621. expect(toggle.textContent).toBe('Hide topic');
  622. toggle.click();
  623. await u.waitUntil(() => view.querySelector('.hide-topic').textContent === 'Show topic');
  624. done();
  625. }));
  626. it("will always be shown when it's new", mock.initConverse([], {}, async function (done, _converse) {
  627. await mock.openAndEnterChatRoom(_converse, 'jdev@conference.jabber.org', 'jc');
  628. const text = 'Jabber/XMPP Development | RFCs and Extensions: https://xmpp.org/ | Protocol and XSF discussions: xsf@muc.xmpp.org';
  629. let stanza = u.toStanza(`
  630. <message xmlns="jabber:client" to="${_converse.jid}" type="groupchat" from="jdev@conference.jabber.org/ralphm">
  631. <subject>${text}</subject>
  632. </message>`);
  633. _converse.connection._dataRecv(mock.createRequest(stanza));
  634. const view = _converse.chatboxviews.get('jdev@conference.jabber.org');
  635. await new Promise(resolve => view.model.once('change:subject', resolve));
  636. const head_desc = await u.waitUntil(() => view.querySelector('.chat-head__desc'));
  637. expect(head_desc?.textContent.trim()).toBe(text);
  638. let topic_el = view.querySelector('.chat-head__desc');
  639. expect(topic_el.textContent.trim()).toBe(text);
  640. expect(u.isVisible(topic_el)).toBe(true);
  641. const toggle = view.querySelector('.hide-topic');
  642. expect(toggle.textContent).toBe('Hide topic');
  643. toggle.click();
  644. await u.waitUntil(() => !u.isVisible(topic_el));
  645. stanza = u.toStanza(`
  646. <message xmlns="jabber:client" to="${_converse.jid}" type="groupchat" from="jdev@conference.jabber.org/ralphm">
  647. <subject>Another topic</subject>
  648. </message>`);
  649. _converse.connection._dataRecv(mock.createRequest(stanza));
  650. await u.waitUntil(() => u.isVisible(view.querySelector('.chat-head__desc')));
  651. topic_el = view.querySelector('.chat-head__desc');
  652. expect(topic_el.textContent.trim()).toBe('Another topic');
  653. done();
  654. }));
  655. it("causes an info message to be shown when received in real-time", mock.initConverse([], {}, async function (done, _converse) {
  656. spyOn(_converse.ChatRoom.prototype, 'handleSubjectChange').and.callThrough();
  657. await mock.openAndEnterChatRoom(_converse, 'jdev@conference.jabber.org', 'romeo');
  658. const view = _converse.chatboxviews.get('jdev@conference.jabber.org');
  659. _converse.connection._dataRecv(mock.createRequest(u.toStanza(`
  660. <message xmlns="jabber:client" to="${_converse.jid}" type="groupchat" from="jdev@conference.jabber.org/ralphm">
  661. <subject>This is an older topic</subject>
  662. <delay xmlns="urn:xmpp:delay" stamp="2014-02-04T09:35:39Z" from="jdev@conference.jabber.org"/>
  663. <x xmlns="jabber:x:delay" stamp="20140204T09:35:39" from="jdev@conference.jabber.org"/>
  664. </message>`)));
  665. await u.waitUntil(() => view.model.handleSubjectChange.calls.count());
  666. expect(sizzle('.chat-info__message', view).length).toBe(0);
  667. const desc = await u.waitUntil(() => view.querySelector('.chat-head__desc'));
  668. expect(desc.textContent.trim()).toBe('This is an older topic');
  669. _converse.connection._dataRecv(mock.createRequest(u.toStanza(`
  670. <message xmlns="jabber:client" to="${_converse.jid}" type="groupchat" from="jdev@conference.jabber.org/ralphm">
  671. <subject>This is a new topic</subject>
  672. </message>`)));
  673. await u.waitUntil(() => view.model.handleSubjectChange.calls.count() === 2);
  674. await u.waitUntil(() => sizzle('.chat-info__message', view).pop()?.textContent.trim() === 'Topic set by ralphm');
  675. await u.waitUntil(() => desc.textContent.trim() === 'This is a new topic');
  676. // Doesn't show multiple subsequent topic change notifications
  677. _converse.connection._dataRecv(mock.createRequest(u.toStanza(`
  678. <message xmlns="jabber:client" to="${_converse.jid}" type="groupchat" from="jdev@conference.jabber.org/ralphm">
  679. <subject>Yet another topic</subject>
  680. </message>`)));
  681. await u.waitUntil(() => view.model.handleSubjectChange.calls.count() === 3);
  682. await u.waitUntil(() => desc.textContent.trim() === 'Yet another topic');
  683. expect(sizzle('.chat-info__message', view).length).toBe(1);
  684. // Sow multiple subsequent topic change notification from someone else
  685. _converse.connection._dataRecv(mock.createRequest(u.toStanza(`
  686. <message xmlns="jabber:client" to="${_converse.jid}" type="groupchat" from="jdev@conference.jabber.org/some1">
  687. <subject>Some1's topic</subject>
  688. </message>`)));
  689. await u.waitUntil(() => view.model.handleSubjectChange.calls.count() === 4);
  690. await u.waitUntil(() => desc.textContent.trim() === "Some1's topic");
  691. expect(sizzle('.chat-info__message', view).length).toBe(2);
  692. const el = sizzle('.chat-info__message', view).pop();
  693. expect(el.textContent.trim()).toBe('Topic set by some1');
  694. // Removes current topic
  695. const stanza = u.toStanza(
  696. `<message xmlns="jabber:client" to="${_converse.jid}" type="groupchat" from="jdev@conference.jabber.org/some1">
  697. <subject/>
  698. </message>`);
  699. _converse.connection._dataRecv(mock.createRequest(stanza));
  700. await u.waitUntil(() => view.model.handleSubjectChange.calls.count() === 5);
  701. await u.waitUntil(() => view.querySelector('.chat-head__desc').textContent.replace(/<!---->/g, '') === '');
  702. await u.waitUntil(() => view.querySelector('converse-chat-message:last-child .chat-info').textContent.trim() === "Topic cleared by some1");
  703. done();
  704. }));
  705. });
  706. it("restores cached messages when it reconnects and clear_messages_on_reconnection and muc_clear_messages_on_leave are false",
  707. mock.initConverse([], {
  708. 'clear_messages_on_reconnection': false,
  709. 'muc_clear_messages_on_leave': false
  710. },
  711. async function (done, _converse) {
  712. const muc_jid = 'lounge@montague.lit';
  713. await mock.openAndEnterChatRoom(_converse, muc_jid , 'romeo');
  714. const view = _converse.chatboxviews.get(muc_jid);
  715. const message = 'Hello world',
  716. nick = mock.chatroom_names[0],
  717. msg = $msg({
  718. 'from': 'lounge@montague.lit/'+nick,
  719. 'id': u.getUniqueId(),
  720. 'to': 'romeo@montague.lit',
  721. 'type': 'groupchat'
  722. }).c('body').t(message).tree();
  723. await view.model.handleMessageStanza(msg);
  724. await view.model.close();
  725. _converse.connection.IQ_stanzas = [];
  726. await mock.openAndEnterChatRoom(_converse, muc_jid , 'romeo');
  727. await u.waitUntil(() => view.querySelector('converse-chat-message'));
  728. expect(view.model.messages.length).toBe(1);
  729. expect(view.querySelectorAll('converse-chat-message').length).toBe(1);
  730. done()
  731. }));
  732. it("clears cached messages when it reconnects and clear_messages_on_reconnection is true",
  733. mock.initConverse([], {'clear_messages_on_reconnection': true}, async function (done, _converse) {
  734. const muc_jid = 'lounge@montague.lit';
  735. await mock.openAndEnterChatRoom(_converse, muc_jid , 'romeo');
  736. const view = _converse.chatboxviews.get(muc_jid);
  737. const message = 'Hello world',
  738. nick = mock.chatroom_names[0],
  739. msg = $msg({
  740. 'from': 'lounge@montague.lit/'+nick,
  741. 'id': u.getUniqueId(),
  742. 'to': 'romeo@montague.lit',
  743. 'type': 'groupchat'
  744. }).c('body').t(message).tree();
  745. await view.model.handleMessageStanza(msg);
  746. await view.model.close();
  747. _converse.connection.IQ_stanzas = [];
  748. await mock.openAndEnterChatRoom(_converse, muc_jid , 'romeo');
  749. expect(view.model.messages.length).toBe(0);
  750. expect(view.querySelector('converse-chat-history')).toBe(null);
  751. done()
  752. }));
  753. it("is opened when an xmpp: URI is clicked inside another groupchat",
  754. mock.initConverse([], {}, async function (done, _converse) {
  755. await mock.waitForRoster(_converse, 'current');
  756. await mock.openAndEnterChatRoom(_converse, 'lounge@montague.lit', 'romeo');
  757. const view = _converse.chatboxviews.get('lounge@montague.lit');
  758. if (!view.querySelectorAll('.chat-area').length) {
  759. view.renderChatArea();
  760. }
  761. expect(_converse.chatboxes.length).toEqual(2);
  762. const message = 'Please go to xmpp:coven@chat.shakespeare.lit?join',
  763. nick = mock.chatroom_names[0],
  764. msg = $msg({
  765. 'from': 'lounge@montague.lit/'+nick,
  766. 'id': u.getUniqueId(),
  767. 'to': 'romeo@montague.lit',
  768. 'type': 'groupchat'
  769. }).c('body').t(message).tree();
  770. await view.model.handleMessageStanza(msg);
  771. await u.waitUntil(() => view.querySelector('.chat-msg__text a'));
  772. view.querySelector('.chat-msg__text a').click();
  773. await u.waitUntil(() => _converse.chatboxes.length === 3)
  774. expect(_converse.chatboxes.pluck('id').includes('coven@chat.shakespeare.lit')).toBe(true);
  775. done()
  776. }));
  777. it("shows a notification if it's not anonymous",
  778. mock.initConverse(['chatBoxesFetched'], {}, async function (done, _converse) {
  779. const muc_jid = 'coven@chat.shakespeare.lit';
  780. const nick = 'romeo';
  781. await _converse.api.rooms.open(muc_jid);
  782. await mock.getRoomFeatures(_converse, muc_jid);
  783. await mock.waitForReservedNick(_converse, muc_jid, nick);
  784. const view = _converse.chatboxviews.get(muc_jid);
  785. /* <presence to="romeo@montague.lit/_converse.js-29092160"
  786. * from="coven@chat.shakespeare.lit/some1">
  787. * <x xmlns="http://jabber.org/protocol/muc#user">
  788. * <item affiliation="owner" jid="romeo@montague.lit/_converse.js-29092160" role="moderator"/>
  789. * <status code="110"/>
  790. * <status code="100"/>
  791. * </x>
  792. * </presence></body>
  793. */
  794. const presence = $pres({
  795. to: 'romeo@montague.lit/orchard',
  796. from: 'coven@chat.shakespeare.lit/some1'
  797. }).c('x', {xmlns: Strophe.NS.MUC_USER})
  798. .c('item', {
  799. 'affiliation': 'owner',
  800. 'jid': 'romeo@montague.lit/_converse.js-29092160',
  801. 'role': 'moderator'
  802. }).up()
  803. .c('status', {code: '110'}).up()
  804. .c('status', {code: '100'});
  805. _converse.connection._dataRecv(mock.createRequest(presence));
  806. await u.waitUntil(() => (view.model.session.get('connection_status') === converse.ROOMSTATUS.ENTERED));
  807. await mock.returnMemberLists(_converse, muc_jid, [], ['member', 'admin', 'owner']);
  808. const num_info_msgs = await u.waitUntil(() => view.querySelectorAll('.chat-content .chat-info').length);
  809. expect(num_info_msgs).toBe(1);
  810. expect(sizzle('div.chat-info', view.content).pop().textContent.trim()).toBe("This groupchat is not anonymous");
  811. const csntext = await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent);
  812. expect(csntext.trim()).toEqual("some1 has entered the groupchat");
  813. done();
  814. }));
  815. it("shows join/leave messages when users enter or exit a groupchat",
  816. mock.initConverse(['chatBoxesFetched'], {'muc_fetch_members': false}, async function (done, _converse) {
  817. const muc_jid = 'coven@chat.shakespeare.lit';
  818. const nick = 'some1';
  819. const room_creation_promise = await _converse.api.rooms.open(muc_jid, {nick});
  820. await mock.getRoomFeatures(_converse, muc_jid);
  821. const sent_stanzas = _converse.connection.sent_stanzas;
  822. await u.waitUntil(() => sent_stanzas.filter(iq => sizzle('presence history', iq).length).pop());
  823. const view = _converse.chatboxviews.get('coven@chat.shakespeare.lit');
  824. await _converse.api.waitUntil('chatRoomViewInitialized');
  825. /* We don't show join/leave messages for existing occupants. We
  826. * know about them because we receive their presences before we
  827. * receive our own.
  828. */
  829. let presence = $pres({
  830. to: 'romeo@montague.lit/_converse.js-29092160',
  831. from: 'coven@chat.shakespeare.lit/oldguy'
  832. }).c('x', {xmlns: Strophe.NS.MUC_USER})
  833. .c('item', {
  834. 'affiliation': 'none',
  835. 'jid': 'oldguy@montague.lit/_converse.js-290929789',
  836. 'role': 'participant'
  837. });
  838. _converse.connection._dataRecv(mock.createRequest(presence));
  839. /* <presence to="romeo@montague.lit/_converse.js-29092160"
  840. * from="coven@chat.shakespeare.lit/some1">
  841. * <x xmlns="http://jabber.org/protocol/muc#user">
  842. * <item affiliation="owner" jid="romeo@montague.lit/_converse.js-29092160" role="moderator"/>
  843. * <status code="110"/>
  844. * </x>
  845. * </presence></body>
  846. */
  847. presence = $pres({
  848. to: 'romeo@montague.lit/_converse.js-29092160',
  849. from: 'coven@chat.shakespeare.lit/some1'
  850. }).c('x', {xmlns: Strophe.NS.MUC_USER})
  851. .c('item', {
  852. 'affiliation': 'owner',
  853. 'jid': 'romeo@montague.lit/_converse.js-29092160',
  854. 'role': 'moderator'
  855. }).up()
  856. .c('status', {code: '110'});
  857. _converse.connection._dataRecv(mock.createRequest(presence));
  858. const csntext = await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent);
  859. expect(csntext.trim()).toEqual("some1 has entered the groupchat");
  860. await room_creation_promise;
  861. await u.waitUntil(() => (view.model.session.get('connection_status') === converse.ROOMSTATUS.ENTERED));
  862. await view.model.messages.fetched;
  863. presence = $pres({
  864. to: 'romeo@montague.lit/_converse.js-29092160',
  865. from: 'coven@chat.shakespeare.lit/newguy'
  866. })
  867. .c('x', {xmlns: Strophe.NS.MUC_USER})
  868. .c('item', {
  869. 'affiliation': 'none',
  870. 'jid': 'newguy@montague.lit/_converse.js-290929789',
  871. 'role': 'participant'
  872. });
  873. _converse.connection._dataRecv(mock.createRequest(presence));
  874. await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent.trim() ===
  875. "some1 and newguy have entered the groupchat");
  876. const msg = $msg({
  877. 'from': 'coven@chat.shakespeare.lit/some1',
  878. 'id': u.getUniqueId(),
  879. 'to': 'romeo@montague.lit',
  880. 'type': 'groupchat'
  881. }).c('body').t('hello world').tree();
  882. _converse.connection._dataRecv(mock.createRequest(msg));
  883. await u.waitUntil(() => view.querySelectorAll('.chat-msg__text').length);
  884. // Add another entrant, otherwise the above message will be
  885. // collapsed if "newguy" leaves immediately again
  886. presence = $pres({
  887. to: 'romeo@montague.lit/_converse.js-29092160',
  888. from: 'coven@chat.shakespeare.lit/newgirl'
  889. })
  890. .c('x', {xmlns: Strophe.NS.MUC_USER})
  891. .c('item', {
  892. 'affiliation': 'none',
  893. 'jid': 'newgirl@montague.lit/_converse.js-213098781',
  894. 'role': 'participant'
  895. });
  896. _converse.connection._dataRecv(mock.createRequest(presence));
  897. await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent.trim() ===
  898. "some1, newguy and newgirl have entered the groupchat");
  899. // Don't show duplicate join messages
  900. presence = $pres({
  901. to: 'romeo@montague.lit/_converse.js-290918392',
  902. from: 'coven@chat.shakespeare.lit/newguy'
  903. }).c('x', {xmlns: Strophe.NS.MUC_USER})
  904. .c('item', {
  905. 'affiliation': 'none',
  906. 'jid': 'newguy@montague.lit/_converse.js-290929789',
  907. 'role': 'participant'
  908. });
  909. _converse.connection._dataRecv(mock.createRequest(presence));
  910. /* <presence
  911. * from='coven@chat.shakespeare.lit/thirdwitch'
  912. * to='crone1@shakespeare.lit/desktop'
  913. * type='unavailable'>
  914. * <status>Disconnected: Replaced by new connection</status>
  915. * <x xmlns='http://jabber.org/protocol/muc#user'>
  916. * <item affiliation='member'
  917. * jid='hag66@shakespeare.lit/pda'
  918. * role='none'/>
  919. * </x>
  920. * </presence>
  921. */
  922. presence = $pres({
  923. to: 'romeo@montague.lit/_converse.js-29092160',
  924. type: 'unavailable',
  925. from: 'coven@chat.shakespeare.lit/newguy'
  926. })
  927. .c('status', 'Disconnected: Replaced by new connection').up()
  928. .c('x', {xmlns: Strophe.NS.MUC_USER})
  929. .c('item', {
  930. 'affiliation': 'none',
  931. 'jid': 'newguy@montague.lit/_converse.js-290929789',
  932. 'role': 'none'
  933. });
  934. _converse.connection._dataRecv(mock.createRequest(presence));
  935. await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent.trim() ===
  936. "some1 and newgirl have entered the groupchat\nnewguy has left the groupchat");
  937. // When the user immediately joins again, we collapse the
  938. // multiple join/leave messages.
  939. presence = $pres({
  940. to: 'romeo@montague.lit/_converse.js-29092160',
  941. from: 'coven@chat.shakespeare.lit/newguy'
  942. }).c('x', {xmlns: Strophe.NS.MUC_USER})
  943. .c('item', {
  944. 'affiliation': 'none',
  945. 'jid': 'newguy@montague.lit/_converse.js-290929789',
  946. 'role': 'participant'
  947. });
  948. _converse.connection._dataRecv(mock.createRequest(presence));
  949. await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent.trim() ===
  950. "some1, newgirl and newguy have entered the groupchat");
  951. presence = $pres({
  952. to: 'romeo@montague.lit/_converse.js-29092160',
  953. type: 'unavailable',
  954. from: 'coven@chat.shakespeare.lit/newguy'
  955. })
  956. .c('x', {xmlns: Strophe.NS.MUC_USER})
  957. .c('item', {
  958. 'affiliation': 'none',
  959. 'jid': 'newguy@montague.lit/_converse.js-290929789',
  960. 'role': 'none'
  961. });
  962. _converse.connection._dataRecv(mock.createRequest(presence));
  963. await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent.trim() ===
  964. "some1 and newgirl have entered the groupchat\nnewguy has left the groupchat");
  965. presence = $pres({
  966. to: 'romeo@montague.lit/_converse.js-29092160',
  967. from: 'coven@chat.shakespeare.lit/nomorenicks'
  968. })
  969. .c('x', {xmlns: Strophe.NS.MUC_USER})
  970. .c('item', {
  971. 'affiliation': 'none',
  972. 'jid': 'nomorenicks@montague.lit/_converse.js-290929789',
  973. 'role': 'participant'
  974. });
  975. _converse.connection._dataRecv(mock.createRequest(presence));
  976. await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent.trim() ===
  977. "some1, newgirl and nomorenicks have entered the groupchat\nnewguy has left the groupchat");
  978. presence = $pres({
  979. to: 'romeo@montague.lit/_converse.js-290918392',
  980. type: 'unavailable',
  981. from: 'coven@chat.shakespeare.lit/nomorenicks'
  982. }).c('x', {xmlns: Strophe.NS.MUC_USER})
  983. .c('item', {
  984. 'affiliation': 'none',
  985. 'jid': 'nomorenicks@montague.lit/_converse.js-290929789',
  986. 'role': 'none'
  987. });
  988. _converse.connection._dataRecv(mock.createRequest(presence));
  989. await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent.trim() ===
  990. "some1 and newgirl have entered the groupchat\nnewguy and nomorenicks have left the groupchat");
  991. presence = $pres({
  992. to: 'romeo@montague.lit/_converse.js-29092160',
  993. from: 'coven@chat.shakespeare.lit/nomorenicks'
  994. })
  995. .c('x', {xmlns: Strophe.NS.MUC_USER})
  996. .c('item', {
  997. 'affiliation': 'none',
  998. 'jid': 'nomorenicks@montague.lit/_converse.js-290929789',
  999. 'role': 'participant'
  1000. });
  1001. _converse.connection._dataRecv(mock.createRequest(presence));
  1002. await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent.trim() ===
  1003. "some1, newgirl and nomorenicks have entered the groupchat\nnewguy has left the groupchat");
  1004. // Test a member joining and leaving
  1005. presence = $pres({
  1006. to: 'romeo@montague.lit/_converse.js-290918392',
  1007. from: 'coven@chat.shakespeare.lit/insider'
  1008. }).c('x', {xmlns: Strophe.NS.MUC_USER})
  1009. .c('item', {
  1010. 'affiliation': 'member',
  1011. 'jid': 'insider@montague.lit/_converse.js-290929789',
  1012. 'role': 'participant'
  1013. });
  1014. _converse.connection._dataRecv(mock.createRequest(presence));
  1015. /* <presence
  1016. * from='coven@chat.shakespeare.lit/thirdwitch'
  1017. * to='crone1@shakespeare.lit/desktop'
  1018. * type='unavailable'>
  1019. * <status>Disconnected: Replaced by new connection</status>
  1020. * <x xmlns='http://jabber.org/protocol/muc#user'>
  1021. * <item affiliation='member'
  1022. * jid='hag66@shakespeare.lit/pda'
  1023. * role='none'/>
  1024. * </x>
  1025. * </presence>
  1026. */
  1027. presence = $pres({
  1028. to: 'romeo@montague.lit/_converse.js-29092160',
  1029. type: 'unavailable',
  1030. from: 'coven@chat.shakespeare.lit/insider'
  1031. })
  1032. .c('status', 'Disconnected: Replaced by new connection').up()
  1033. .c('x', {xmlns: Strophe.NS.MUC_USER})
  1034. .c('item', {
  1035. 'affiliation': 'member',
  1036. 'jid': 'insider@montague.lit/_converse.js-290929789',
  1037. 'role': 'none'
  1038. });
  1039. _converse.connection._dataRecv(mock.createRequest(presence));
  1040. await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent.trim() ===
  1041. "some1, newgirl and nomorenicks have entered the groupchat\nnewguy and insider have left the groupchat");
  1042. expect(view.model.occupants.length).toBe(5);
  1043. expect(view.model.occupants.findWhere({'jid': 'insider@montague.lit'}).get('show')).toBe('offline');
  1044. // New girl leaves
  1045. presence = $pres({
  1046. 'to': 'romeo@montague.lit/_converse.js-29092160',
  1047. 'type': 'unavailable',
  1048. 'from': 'coven@chat.shakespeare.lit/newgirl'
  1049. })
  1050. .c('x', {xmlns: Strophe.NS.MUC_USER})
  1051. .c('item', {
  1052. 'affiliation': 'none',
  1053. 'jid': 'newgirl@montague.lit/_converse.js-213098781',
  1054. 'role': 'none'
  1055. });
  1056. _converse.connection._dataRecv(mock.createRequest(presence));
  1057. await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent.trim() ===
  1058. "some1 and nomorenicks have entered the groupchat\nnewguy, insider and newgirl have left the groupchat");
  1059. expect(view.model.occupants.length).toBe(4);
  1060. done();
  1061. }));
  1062. it("combines subsequent join/leave messages when users enter or exit a groupchat",
  1063. mock.initConverse(['chatBoxesFetched'], {}, async function (done, _converse) {
  1064. await mock.openAndEnterChatRoom(_converse, 'coven@chat.shakespeare.lit', 'romeo')
  1065. const view = _converse.chatboxviews.get('coven@chat.shakespeare.lit');
  1066. await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent.trim() === "romeo has entered the groupchat");
  1067. let presence = u.toStanza(
  1068. `<presence xmlns="jabber:client" to="romeo@montague.lit/orchard" from="coven@chat.shakespeare.lit/fabio">
  1069. <c xmlns="http://jabber.org/protocol/caps" node="http://conversations.im" ver="INI3xjRUioclBTP/aACfWi5m9UY=" hash="sha-1"/>
  1070. <x xmlns="http://jabber.org/protocol/muc#user">
  1071. <item affiliation="none" jid="fabio@montefuscolo.com.br/Conversations.ZvLu" role="participant"/>
  1072. </x>
  1073. </presence>`);
  1074. _converse.connection._dataRecv(mock.createRequest(presence));
  1075. await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent.trim() === "romeo and fabio have entered the groupchat");
  1076. presence = u.toStanza(
  1077. `<presence xmlns="jabber:client" to="romeo@montague.lit/orchard" from="coven@chat.shakespeare.lit/Dele Olajide">
  1078. <x xmlns="http://jabber.org/protocol/muc#user">
  1079. <item affiliation="none" jid="deleo@traderlynk.4ng.net/converse.js-39320524" role="participant"/>
  1080. </x>
  1081. </presence>`);
  1082. _converse.connection._dataRecv(mock.createRequest(presence));
  1083. await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent.trim() === "romeo, fabio and Dele Olajide have entered the groupchat");
  1084. presence = u.toStanza(
  1085. `<presence xmlns="jabber:client" to="romeo@montague.lit/orchard" from="coven@chat.shakespeare.lit/jcbrand">
  1086. <x xmlns="http://jabber.org/protocol/muc#user">
  1087. <item affiliation="owner" jid="jc@opkode.com/converse.js-30645022" role="moderator"/>
  1088. <status code="110"/>
  1089. </x>
  1090. </presence>`);
  1091. _converse.connection._dataRecv(mock.createRequest(presence));
  1092. await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent.trim() === "romeo, fabio and others have entered the groupchat");
  1093. presence = u.toStanza(
  1094. `<presence xmlns="jabber:client" to="romeo@montague.lit/orchard" type="unavailable" from="coven@chat.shakespeare.lit/Dele Olajide">
  1095. <x xmlns="http://jabber.org/protocol/muc#user">
  1096. <item affiliation="none" jid="deleo@traderlynk.4ng.net/converse.js-39320524" role="none"/>
  1097. </x>
  1098. </presence>`);
  1099. _converse.connection._dataRecv(mock.createRequest(presence));
  1100. await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent.trim() ===
  1101. "romeo, fabio and jcbrand have entered the groupchat\nDele Olajide has left the groupchat");
  1102. presence = u.toStanza(
  1103. `<presence xmlns="jabber:client" to="romeo@montague.lit/orchard" from="coven@chat.shakespeare.lit/Dele Olajide">
  1104. <x xmlns="http://jabber.org/protocol/muc#user">
  1105. <item affiliation="none" jid="deleo@traderlynk.4ng.net/converse.js-74567907" role="participant"/>
  1106. </x>
  1107. </presence>`);
  1108. _converse.connection._dataRecv(mock.createRequest(presence));
  1109. await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent.trim() ===
  1110. "romeo, fabio and others have entered the groupchat");
  1111. presence = u.toStanza(
  1112. `<presence xmlns="jabber:client" to="romeo@montague.lit/orchard" from="coven@chat.shakespeare.lit/fuvuv" xml:lang="en">
  1113. <c xmlns="http://jabber.org/protocol/caps" node="http://jabber.pix-art.de" ver="5tOurnuFnp2h50hKafeUyeN4Yl8=" hash="sha-1"/>
  1114. <x xmlns="vcard-temp:x:update"/>
  1115. <x xmlns="http://jabber.org/protocol/muc#user">
  1116. <item affiliation="none" jid="fuvuv@blabber.im/Pix-Art Messenger.8zoB" role="participant"/>
  1117. </x>
  1118. </presence>`);
  1119. _converse.connection._dataRecv(mock.createRequest(presence));
  1120. await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent.trim() ===
  1121. "romeo, fabio and others have entered the groupchat");
  1122. presence = u.toStanza(
  1123. `<presence xmlns="jabber:client" to="romeo@montague.lit/orchard" type="unavailable" from="coven@chat.shakespeare.lit/fuvuv">
  1124. <x xmlns="http://jabber.org/protocol/muc#user">
  1125. <item affiliation="none" jid="fuvuv@blabber.im/Pix-Art Messenger.8zoB" role="none"/>
  1126. </x>
  1127. </presence>`);
  1128. _converse.connection._dataRecv(mock.createRequest(presence));
  1129. await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent.trim() ===
  1130. "romeo, fabio and others have entered the groupchat\nfuvuv has left the groupchat");
  1131. presence = u.toStanza(
  1132. `<presence xmlns="jabber:client" to="romeo@montague.lit/orchard" type="unavailable" from="coven@chat.shakespeare.lit/fabio">
  1133. <status>Disconnected: Replaced by new connection</status>
  1134. <x xmlns="http://jabber.org/protocol/muc#user">
  1135. <item affiliation="none" jid="fabio@montefuscolo.com.br/Conversations.ZvLu" role="none"/>
  1136. </x>
  1137. </presence>`);
  1138. _converse.connection._dataRecv(mock.createRequest(presence));
  1139. await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent.trim() ===
  1140. "romeo, jcbrand and Dele Olajide have entered the groupchat\nfuvuv and fabio have left the groupchat");
  1141. presence = u.toStanza(
  1142. `<presence xmlns="jabber:client" to="romeo@montague.lit/orchard" from="coven@chat.shakespeare.lit/fabio">
  1143. <c xmlns="http://jabber.org/protocol/caps" node="http://conversations.im" ver="INI3xjRUioclBTP/aACfWi5m9UY=" hash="sha-1"/>
  1144. <status>Ready for a new day</status>
  1145. <x xmlns="http://jabber.org/protocol/muc#user">
  1146. <item affiliation="none" jid="fabio@montefuscolo.com.br/Conversations.ZvLu" role="participant"/>
  1147. </x>
  1148. </presence>`);
  1149. _converse.connection._dataRecv(mock.createRequest(presence));
  1150. await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent.trim() ===
  1151. "romeo, jcbrand and others have entered the groupchat\nfuvuv has left the groupchat");
  1152. presence = u.toStanza(
  1153. `<presence xmlns="jabber:client" to="romeo@montague.lit/orchard" type="unavailable" from="coven@chat.shakespeare.lit/fabio">
  1154. <status>Disconnected: closed</status>
  1155. <x xmlns="http://jabber.org/protocol/muc#user">
  1156. <item affiliation="none" jid="fabio@montefuscolo.com.br/Conversations.ZvLu" role="none"/>
  1157. </x>
  1158. </presence>`);
  1159. _converse.connection._dataRecv(mock.createRequest(presence));
  1160. await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent.trim() ===
  1161. "romeo, jcbrand and Dele Olajide have entered the groupchat\nfuvuv and fabio have left the groupchat");
  1162. presence = u.toStanza(
  1163. `<presence xmlns="jabber:client" to="romeo@montague.lit/orchard" type="unavailable" from="coven@chat.shakespeare.lit/Dele Olajide">
  1164. <x xmlns="http://jabber.org/protocol/muc#user">
  1165. <item affiliation="none" jid="deleo@traderlynk.4ng.net/converse.js-74567907" role="none"/>
  1166. </x>
  1167. </presence>`);
  1168. _converse.connection._dataRecv(mock.createRequest(presence));
  1169. await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent.trim() ===
  1170. "romeo and jcbrand have entered the groupchat\nfuvuv, fabio and Dele Olajide have left the groupchat");
  1171. presence = u.toStanza(
  1172. `<presence xmlns="jabber:client" to="romeo@montague.lit/orchard" from="coven@chat.shakespeare.lit/fabio">
  1173. <c xmlns="http://jabber.org/protocol/caps" node="http://conversations.im" ver="INI3xjRUioclBTP/aACfWi5m9UY=" hash="sha-1"/>
  1174. <x xmlns="http://jabber.org/protocol/muc#user">
  1175. <item affiliation="none" jid="fabio@montefuscolo.com.br/Conversations.ZvLu" role="participant"/>
  1176. </x>
  1177. </presence>`);
  1178. _converse.connection._dataRecv(mock.createRequest(presence));
  1179. await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent.trim() ===
  1180. "romeo, jcbrand and fabio have entered the groupchat\nfuvuv and Dele Olajide have left the groupchat");
  1181. expect(1).toBe(1);
  1182. done();
  1183. }));
  1184. it("doesn't show the disconnection messages when join_leave_events is not in muc_show_info_messages setting",
  1185. mock.initConverse(['chatBoxesFetched'], {'muc_show_info_messages': []}, async function (done, _converse) {
  1186. spyOn(_converse.ChatRoom.prototype, 'onOccupantAdded').and.callThrough();
  1187. spyOn(_converse.ChatRoom.prototype, 'onOccupantRemoved').and.callThrough();
  1188. await mock.openAndEnterChatRoom(_converse, 'coven@chat.shakespeare.lit', 'some1');
  1189. const view = _converse.chatboxviews.get('coven@chat.shakespeare.lit');
  1190. let presence = $pres({
  1191. to: 'romeo@montague.lit/orchard',
  1192. from: 'coven@chat.shakespeare.lit/newguy'
  1193. }).c('x', {xmlns: Strophe.NS.MUC_USER})
  1194. .c('item', {
  1195. 'affiliation': 'none',
  1196. 'jid': 'newguy@montague.lit/_converse.js-290929789',
  1197. 'role': 'participant'
  1198. });
  1199. _converse.connection._dataRecv(mock.createRequest(presence));
  1200. await u.waitUntil(() => view.model.onOccupantAdded.calls.count() === 2);
  1201. expect(view.model.notifications.get('entered')).toBeFalsy();
  1202. expect(view.querySelector('.chat-content__notifications').textContent.trim()).toBe('');
  1203. await mock.sendMessage(view, 'hello world');
  1204. presence = u.toStanza(
  1205. `<presence xmlns="jabber:client" to="romeo@montague.lit/orchard" type="unavailable" from="coven@chat.shakespeare.lit/newguy">
  1206. <status>Gotta go!</status>
  1207. <x xmlns="http://jabber.org/protocol/muc#user">
  1208. <item affiliation="none" jid="newguy@montague.lit/_converse.js-290929789" role="none"/>
  1209. </x>
  1210. </presence>`);
  1211. _converse.connection._dataRecv(mock.createRequest(presence));
  1212. await u.waitUntil(() => view.model.onOccupantRemoved.calls.count());
  1213. expect(view.model.onOccupantRemoved.calls.count()).toBe(1);
  1214. expect(view.model.notifications.get('entered')).toBeFalsy();
  1215. await mock.sendMessage(view, 'hello world');
  1216. expect(view.querySelector('.chat-content__notifications').textContent.trim()).toBe('');
  1217. done();
  1218. }));
  1219. it("role-change messages that follow a MUC leave are left out",
  1220. mock.initConverse(['chatBoxesFetched'], {}, async function (done, _converse) {
  1221. // See https://github.com/conversejs/converse.js/issues/1259
  1222. await mock.openAndEnterChatRoom(_converse, 'conversations@conference.siacs.eu', 'romeo');
  1223. const presence = $pres({
  1224. to: 'romeo@montague.lit/orchard',
  1225. from: 'conversations@conference.siacs.eu/Guus'
  1226. }).c('x', {
  1227. 'xmlns': Strophe.NS.MUC_USER
  1228. }).c('item', {
  1229. 'affiliation': 'none',
  1230. 'jid': 'Guus@montague.lit/xxx',
  1231. 'role': 'visitor'
  1232. });
  1233. _converse.connection._dataRecv(mock.createRequest(presence));
  1234. const view = _converse.chatboxviews.get('conversations@conference.siacs.eu');
  1235. const msg = $msg({
  1236. 'from': 'conversations@conference.siacs.eu/romeo',
  1237. 'id': u.getUniqueId(),
  1238. 'to': 'romeo@montague.lit',
  1239. 'type': 'groupchat'
  1240. }).c('body').t('Some message').tree();
  1241. await view.model.handleMessageStanza(msg);
  1242. await u.waitUntil(() => sizzle('.chat-msg:last .chat-msg__text', view.content).pop());
  1243. let stanza = u.toStanza(
  1244. `<presence xmlns="jabber:client" to="romeo@montague.lit/orchard" type="unavailable" from="conversations@conference.siacs.eu/Guus">
  1245. <x xmlns="http://jabber.org/protocol/muc#user">
  1246. <item affiliation="none" role="none"/>
  1247. </x>
  1248. </presence>`);
  1249. _converse.connection._dataRecv(mock.createRequest(stanza));
  1250. stanza = u.toStanza(
  1251. `<presence xmlns="jabber:client" to="romeo@montague.lit/orchard" from="conversations@conference.siacs.eu/Guus">
  1252. <c xmlns="http://jabber.org/protocol/caps" node="http://conversations.im" ver="ISg6+9AoK1/cwhbNEDviSvjdPzI=" hash="sha-1"/>
  1253. <x xmlns="vcard-temp:x:update">
  1254. <photo>bf987c486c51fbc05a6a4a9f20dd19b5efba3758</photo>
  1255. </x>
  1256. <x xmlns="http://jabber.org/protocol/muc#user">
  1257. <item affiliation="none" role="visitor"/>
  1258. </x>
  1259. </presence>`);
  1260. _converse.connection._dataRecv(mock.createRequest(stanza));
  1261. await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent.trim()
  1262. === "romeo and Guus have entered the groupchat");
  1263. expect(1).toBe(1);
  1264. done();
  1265. }));
  1266. it("can be configured if you're its owner", mock.initConverse(['chatBoxesFetched'], {}, async function (done, _converse) {
  1267. let sent_IQ, IQ_id;
  1268. const sendIQ = _converse.connection.sendIQ;
  1269. spyOn(_converse.connection, 'sendIQ').and.callFake(function (iq, callback, errback) {
  1270. sent_IQ = iq;
  1271. IQ_id = sendIQ.bind(this)(iq, callback, errback);
  1272. });
  1273. await _converse.api.rooms.open('coven@chat.shakespeare.lit', {'nick': 'some1'});
  1274. const view = _converse.chatboxviews.get('coven@chat.shakespeare.lit');
  1275. await u.waitUntil(() => u.isVisible(view));
  1276. // We pretend this is a new room, so no disco info is returned.
  1277. const features_stanza = $iq({
  1278. from: 'coven@chat.shakespeare.lit',
  1279. 'id': IQ_id,
  1280. 'to': 'romeo@montague.lit/desktop',
  1281. 'type': 'error'
  1282. }).c('error', {'type': 'cancel'})
  1283. .c('item-not-found', {'xmlns': "urn:ietf:params:xml:ns:xmpp-stanzas"});
  1284. _converse.connection._dataRecv(mock.createRequest(features_stanza));
  1285. /* <presence to="romeo@montague.lit/_converse.js-29092160"
  1286. * from="coven@chat.shakespeare.lit/some1">
  1287. * <x xmlns="http://jabber.org/protocol/muc#user">
  1288. * <item affiliation="owner" jid="romeo@montague.lit/_converse.js-29092160" role="moderator"/>
  1289. * <status code="110"/>
  1290. * </x>
  1291. * </presence></body>
  1292. */
  1293. const presence = $pres({
  1294. to: 'romeo@montague.lit/_converse.js-29092160',
  1295. from: 'coven@chat.shakespeare.lit/some1'
  1296. }).c('x', {xmlns: Strophe.NS.MUC_USER})
  1297. .c('item', {
  1298. 'affiliation': 'owner',
  1299. 'jid': 'romeo@montague.lit/_converse.js-29092160',
  1300. 'role': 'moderator'
  1301. }).up()
  1302. .c('status', {code: '110'});
  1303. _converse.connection._dataRecv(mock.createRequest(presence));
  1304. await u.waitUntil(() => view.querySelector('.configure-chatroom-button') !== null);
  1305. view.querySelector('.configure-chatroom-button').click();
  1306. /* Check that an IQ is sent out, asking for the
  1307. * configuration form.
  1308. * See: // https://xmpp.org/extensions/xep-0045.html#example-163
  1309. *
  1310. * <iq from='crone1@shakespeare.lit/desktop'
  1311. * id='config1'
  1312. * to='coven@chat.shakespeare.lit'
  1313. * type='get'>
  1314. * <query xmlns='http://jabber.org/protocol/muc#owner'/>
  1315. * </iq>
  1316. */
  1317. expect(Strophe.serialize(sent_IQ)).toBe(
  1318. `<iq id="`+IQ_id+`" to="coven@chat.shakespeare.lit" type="get" xmlns="jabber:client">`+
  1319. `<query xmlns="http://jabber.org/protocol/muc#owner"/>`+
  1320. `</iq>`);
  1321. /* Server responds with the configuration form.
  1322. * See: // https://xmpp.org/extensions/xep-0045.html#example-165
  1323. */
  1324. const config_stanza = $iq({from: 'coven@chat.shakespeare.lit',
  1325. 'id': IQ_id,
  1326. 'to': 'romeo@montague.lit/desktop',
  1327. 'type': 'result'})
  1328. .c('query', { 'xmlns': 'http://jabber.org/protocol/muc#owner'})
  1329. .c('x', { 'xmlns': 'jabber:x:data', 'type': 'form'})
  1330. .c('title').t('Configuration for "coven" Room').up()
  1331. .c('instructions').t('Complete this form to modify the configuration of your room.').up()
  1332. .c('field', {'type': 'hidden', 'var': 'FORM_TYPE'})
  1333. .c('value').t('http://jabber.org/protocol/muc#roomconfig').up().up()
  1334. .c('field', {
  1335. 'label': 'Natural-Language Room Name',
  1336. 'type': 'text-single',
  1337. 'var': 'muc#roomconfig_roomname'})
  1338. .c('value').t('A Dark Cave').up().up()
  1339. .c('field', {
  1340. 'label': 'Short Description of Room',
  1341. 'type': 'text-single',
  1342. 'var': 'muc#roomconfig_roomdesc'})
  1343. .c('value').t('The place for all good witches!').up().up()
  1344. .c('field', {
  1345. 'label': 'Enable Public Logging?',
  1346. 'type': 'boolean',
  1347. 'var': 'muc#roomconfig_enablelogging'})
  1348. .c('value').t(0).up().up()
  1349. .c('field', {
  1350. 'label': 'Allow Occupants to Change Subject?',
  1351. 'type': 'boolean',
  1352. 'var': 'muc#roomconfig_changesubject'})
  1353. .c('value').t(0).up().up()
  1354. .c('field', {
  1355. 'label': 'Allow Occupants to Invite Others?',
  1356. 'type': 'boolean',
  1357. 'var': 'muc#roomconfig_allowinvites'})
  1358. .c('value').t(0).up().up()
  1359. .c('field', {
  1360. 'label': 'Who Can Send Private Messages?',
  1361. 'type': 'list-single',
  1362. 'var': 'muc#roomconfig_allowpm'})
  1363. .c('value').t('anyone').up()
  1364. .c('option', {'label': 'Anyone'})
  1365. .c('value').t('anyone').up().up()
  1366. .c('option', {'label': 'Anyone with Voice'})
  1367. .c('value').t('participants').up().up()
  1368. .c('option', {'label': 'Moderators Only'})
  1369. .c('value').t('moderators').up().up()
  1370. .c('option', {'label': 'Nobody'})
  1371. .c('value').t('none').up().up().up()
  1372. .c('field', {
  1373. 'label': 'Roles for which Presence is Broadcasted',
  1374. 'type': 'list-multi',
  1375. 'var': 'muc#roomconfig_presencebroadcast'})
  1376. .c('value').t('moderator').up()
  1377. .c('value').t('participant').up()
  1378. .c('value').t('visitor').up()
  1379. .c('option', {'label': 'Moderator'})
  1380. .c('value').t('moderator').up().up()
  1381. .c('option', {'label': 'Participant'})
  1382. .c('value').t('participant').up().up()
  1383. .c('option', {'label': 'Visitor'})
  1384. .c('value').t('visitor').up().up().up()
  1385. .c('field', {
  1386. 'label': 'Roles and Affiliations that May Retrieve Member List',
  1387. 'type': 'list-multi',
  1388. 'var': 'muc#roomconfig_getmemberlist'})
  1389. .c('value').t('moderator').up()
  1390. .c('value').t('participant').up()
  1391. .c('value').t('visitor').up()
  1392. .c('option', {'label': 'Moderator'})
  1393. .c('value').t('moderator').up().up()
  1394. .c('option', {'label': 'Participant'})
  1395. .c('value').t('participant').up().up()
  1396. .c('option', {'label': 'Visitor'})
  1397. .c('value').t('visitor').up().up().up()
  1398. .c('field', {
  1399. 'label': 'Make Room Publicly Searchable?',
  1400. 'type': 'boolean',
  1401. 'var': 'muc#roomconfig_publicroom'})
  1402. .c('value').t(0).up().up()
  1403. .c('field', {
  1404. 'label': 'Make Room Publicly Searchable?',
  1405. 'type': 'boolean',
  1406. 'var': 'muc#roomconfig_publicroom'})
  1407. .c('value').t(0).up().up()
  1408. .c('field', {
  1409. 'label': 'Make Room Persistent?',
  1410. 'type': 'boolean',
  1411. 'var': 'muc#roomconfig_persistentroom'})
  1412. .c('value').t(0).up().up()
  1413. .c('field', {
  1414. 'label': 'Make Room Moderated?',
  1415. 'type': 'boolean',
  1416. 'var': 'muc#roomconfig_moderatedroom'})
  1417. .c('value').t(0).up().up()
  1418. .c('field', {
  1419. 'label': 'Make Room Members Only?',
  1420. 'type': 'boolean',
  1421. 'var': 'muc#roomconfig_membersonly'})
  1422. .c('value').t(0).up().up()
  1423. .c('field', {
  1424. 'label': 'Password Required for Entry?',
  1425. 'type': 'boolean',
  1426. 'var': 'muc#roomconfig_passwordprotectedroom'})
  1427. .c('value').t(1).up().up()
  1428. .c('field', {'type': 'fixed'})
  1429. .c('value').t(
  1430. 'If a password is required to enter this groupchat, you must specify the password below.'
  1431. ).up().up()
  1432. .c('field', {
  1433. 'label': 'Password',
  1434. 'type': 'text-private',
  1435. 'var': 'muc#roomconfig_roomsecret'})
  1436. .c('value').t('cauldronburn');
  1437. _converse.connection._dataRecv(mock.createRequest(config_stanza));
  1438. const membersonly = await u.waitUntil(() => view.querySelector('input[name="muc#roomconfig_membersonly"]'));
  1439. expect(membersonly.getAttribute('type')).toBe('checkbox');
  1440. membersonly.checked = true;
  1441. const moderated = view.querySelectorAll('input[name="muc#roomconfig_moderatedroom"]');
  1442. expect(moderated.length).toBe(1);
  1443. expect(moderated[0].getAttribute('type')).toBe('checkbox');
  1444. moderated[0].checked = true;
  1445. const password = view.querySelectorAll('input[name="muc#roomconfig_roomsecret"]');
  1446. expect(password.length).toBe(1);
  1447. expect(password[0].getAttribute('type')).toBe('password');
  1448. const allowpm = view.querySelectorAll('select[name="muc#roomconfig_allowpm"]');
  1449. expect(allowpm.length).toBe(1);
  1450. allowpm[0].value = 'moderators';
  1451. const presencebroadcast = view.querySelectorAll('select[name="muc#roomconfig_presencebroadcast"]');
  1452. expect(presencebroadcast.length).toBe(1);
  1453. presencebroadcast[0].value = ['moderator'];
  1454. view.querySelector('.chatroom-form input[type="submit"]').click();
  1455. expect(sent_IQ.querySelector('field[var="muc#roomconfig_membersonly"] value').textContent.trim()).toBe('1');
  1456. expect(sent_IQ.querySelector('field[var="muc#roomconfig_moderatedroom"] value').textContent.trim()).toBe('1');
  1457. expect(sent_IQ.querySelector('field[var="muc#roomconfig_allowpm"] value').textContent.trim()).toBe('moderators');
  1458. expect(sent_IQ.querySelector('field[var="muc#roomconfig_presencebroadcast"] value').textContent.trim()).toBe('moderator');
  1459. done();
  1460. }));
  1461. it("shows all members even if they're not currently present in the groupchat",
  1462. mock.initConverse([], {}, async function (done, _converse) {
  1463. const muc_jid = 'lounge@montague.lit'
  1464. const members = [{
  1465. 'nick': 'juliet',
  1466. 'jid': 'juliet@capulet.lit',
  1467. 'affiliation': 'member'
  1468. }];
  1469. await mock.openAndEnterChatRoom(_converse, muc_jid, 'romeo', [], members);
  1470. const view = _converse.chatboxviews.get(muc_jid);
  1471. await u.waitUntil(() => view.model.occupants.length === 2);
  1472. const occupants = view.querySelector('.occupant-list');
  1473. for (let i=0; i<mock.chatroom_names.length; i++) {
  1474. const name = mock.chatroom_names[i];
  1475. const role = mock.chatroom_roles[name].role;
  1476. // See example 21 https://xmpp.org/extensions/xep-0045.html#enter-pres
  1477. const presence = $pres({
  1478. to:'romeo@montague.lit/pda',
  1479. from:'lounge@montague.lit/'+name
  1480. }).c('x').attrs({xmlns:'http://jabber.org/protocol/muc#user'})
  1481. .c('item').attrs({
  1482. affiliation: mock.chatroom_roles[name].affiliation,
  1483. jid: name.replace(/ /g,'.').toLowerCase() + '@montague.lit',
  1484. role: role
  1485. });
  1486. _converse.connection._dataRecv(mock.createRequest(presence));
  1487. }
  1488. await u.waitUntil(() => occupants.querySelectorAll('li').length > 2, 500);
  1489. expect(occupants.querySelectorAll('li').length).toBe(2+mock.chatroom_names.length);
  1490. expect(view.model.occupants.length).toBe(2+mock.chatroom_names.length);
  1491. mock.chatroom_names.forEach(name => {
  1492. const model = view.model.occupants.findWhere({'nick': name});
  1493. const index = view.model.occupants.indexOf(model);
  1494. expect(occupants.querySelectorAll('li .occupant-nick')[index].textContent.trim()).toBe(name);
  1495. });
  1496. // Test users leaving the groupchat
  1497. // https://xmpp.org/extensions/xep-0045.html#exit
  1498. for (let i=mock.chatroom_names.length-1; i>-1; i--) {
  1499. const name = mock.chatroom_names[i];
  1500. // See example 21 https://xmpp.org/extensions/xep-0045.html#enter-pres
  1501. const presence = $pres({
  1502. to:'romeo@montague.lit/pda',
  1503. from:'lounge@montague.lit/'+name,
  1504. type: 'unavailable'
  1505. }).c('x').attrs({xmlns:'http://jabber.org/protocol/muc#user'})
  1506. .c('item').attrs({
  1507. affiliation: mock.chatroom_roles[name].affiliation,
  1508. jid: name.replace(/ /g,'.').toLowerCase() + '@montague.lit',
  1509. role: 'none'
  1510. }).nodeTree;
  1511. _converse.connection._dataRecv(mock.createRequest(presence));
  1512. expect(occupants.querySelectorAll('li').length).toBe(8);
  1513. }
  1514. const presence = $pres({
  1515. to: 'romeo@montague.lit/pda',
  1516. from: 'lounge@montague.lit/nonmember'
  1517. }).c('x').attrs({xmlns:'http://jabber.org/protocol/muc#user'})
  1518. .c('item').attrs({
  1519. affiliation: null,
  1520. jid: 'servant@montague.lit',
  1521. role: 'visitor'
  1522. });
  1523. _converse.connection._dataRecv(mock.createRequest(presence));
  1524. await u.waitUntil(() => occupants.querySelectorAll('li').length > 8, 500);
  1525. expect(occupants.querySelectorAll('li').length).toBe(9);
  1526. expect(view.model.occupants.length).toBe(9);
  1527. expect(view.model.occupants.filter(o => o.isMember()).length).toBe(8);
  1528. view.model.rejoin();
  1529. // Test that members aren't removed when we reconnect
  1530. expect(view.model.occupants.length).toBe(8);
  1531. await u.waitUntil(() => occupants.querySelectorAll('li').length === 8);
  1532. done();
  1533. }));
  1534. it("shows users currently present in the groupchat",
  1535. mock.initConverse([], {}, async function (done, _converse) {
  1536. await mock.openAndEnterChatRoom(_converse, 'lounge@montague.lit', 'romeo');
  1537. var view = _converse.chatboxviews.get('lounge@montague.lit');
  1538. const occupants = view.querySelector('.occupant-list');
  1539. for (var i=0; i<mock.chatroom_names.length; i++) {
  1540. const name = mock.chatroom_names[i];
  1541. // See example 21 https://xmpp.org/extensions/xep-0045.html#enter-pres
  1542. const presence = $pres({
  1543. to:'romeo@montague.lit/pda',
  1544. from:'lounge@montague.lit/'+name
  1545. }).c('x').attrs({xmlns:'http://jabber.org/protocol/muc#user'})
  1546. .c('item').attrs({
  1547. affiliation: 'none',
  1548. jid: name.replace(/ /g,'.').toLowerCase() + '@montague.lit',
  1549. role: 'participant'
  1550. }).up()
  1551. .c('status');
  1552. _converse.connection._dataRecv(mock.createRequest(presence));
  1553. }
  1554. await u.waitUntil(() => occupants.querySelectorAll('li').length > 1, 500);
  1555. expect(occupants.querySelectorAll('li').length).toBe(1+mock.chatroom_names.length);
  1556. mock.chatroom_names.forEach(name => {
  1557. const model = view.model.occupants.findWhere({'nick': name});
  1558. const index = view.model.occupants.indexOf(model);
  1559. expect(occupants.querySelectorAll('li .occupant-nick')[index].textContent.trim()).toBe(name);
  1560. });
  1561. // Test users leaving the groupchat
  1562. // https://xmpp.org/extensions/xep-0045.html#exit
  1563. for (i=mock.chatroom_names.length-1; i>-1; i--) {
  1564. const name = mock.chatroom_names[i];
  1565. // See example 21 https://xmpp.org/extensions/xep-0045.html#enter-pres
  1566. const presence = $pres({
  1567. to:'romeo@montague.lit/pda',
  1568. from:'lounge@montague.lit/'+name,
  1569. type: 'unavailable'
  1570. }).c('x').attrs({xmlns:'http://jabber.org/protocol/muc#user'})
  1571. .c('item').attrs({
  1572. affiliation: "none",
  1573. jid: name.replace(/ /g,'.').toLowerCase() + '@montague.lit',
  1574. role: 'none'
  1575. });
  1576. _converse.connection._dataRecv(mock.createRequest(presence));
  1577. }
  1578. await u.waitUntil(() => occupants.querySelectorAll('li').length === 1);
  1579. done();
  1580. }));
  1581. it("indicates moderators and visitors by means of a special css class and tooltip",
  1582. mock.initConverse([], {'view_mode': 'fullscreen'}, async function (done, _converse) {
  1583. await mock.openAndEnterChatRoom(_converse, 'lounge@montague.lit', 'romeo');
  1584. const view = _converse.chatboxviews.get('lounge@montague.lit');
  1585. let contact_jid = mock.cur_names[2].replace(/ /g,'.').toLowerCase() + '@montague.lit';
  1586. await u.waitUntil(() => view.querySelectorAll('.occupant-list li').length, 500);
  1587. let occupants = view.querySelectorAll('.occupant-list li');
  1588. expect(occupants.length).toBe(1);
  1589. expect(occupants[0].querySelector('.occupant-nick').textContent.trim()).toBe("romeo");
  1590. expect(occupants[0].querySelectorAll('.badge').length).toBe(2);
  1591. expect(occupants[0].querySelectorAll('.badge')[0].textContent.trim()).toBe('Owner');
  1592. expect(sizzle('.badge:last', occupants[0]).pop().textContent.trim()).toBe('Moderator');
  1593. var presence = $pres({
  1594. to:'romeo@montague.lit/pda',
  1595. from:'lounge@montague.lit/moderatorman'
  1596. }).c('x').attrs({xmlns:'http://jabber.org/protocol/muc#user'})
  1597. .c('item').attrs({
  1598. affiliation: 'admin',
  1599. jid: contact_jid,
  1600. role: 'moderator',
  1601. }).up()
  1602. .c('status').attrs({code:'110'}).nodeTree;
  1603. _converse.connection._dataRecv(mock.createRequest(presence));
  1604. await u.waitUntil(() => view.querySelectorAll('.occupant-list li').length > 1, 500);
  1605. occupants = view.querySelectorAll('.occupant-list li');
  1606. expect(occupants.length).toBe(2);
  1607. expect(occupants[0].querySelector('.occupant-nick').textContent.trim()).toBe("moderatorman");
  1608. expect(occupants[1].querySelector('.occupant-nick').textContent.trim()).toBe("romeo");
  1609. expect(occupants[0].querySelectorAll('.badge').length).toBe(2);
  1610. expect(occupants[0].querySelectorAll('.badge')[0].textContent.trim()).toBe('Admin');
  1611. expect(occupants[0].querySelectorAll('.badge')[1].textContent.trim()).toBe('Moderator');
  1612. expect(occupants[0].getAttribute('title')).toBe(
  1613. contact_jid + ' This user is a moderator. Click to mention moderatorman in your message.'
  1614. );
  1615. contact_jid = mock.cur_names[3].replace(/ /g,'.').toLowerCase() + '@montague.lit';
  1616. presence = $pres({
  1617. to:'romeo@montague.lit/pda',
  1618. from:'lounge@montague.lit/visitorwoman'
  1619. }).c('x').attrs({xmlns:'http://jabber.org/protocol/muc#user'})
  1620. .c('item').attrs({
  1621. jid: contact_jid,
  1622. role: 'visitor',
  1623. }).up()
  1624. .c('status').attrs({code:'110'}).nodeTree;
  1625. _converse.connection._dataRecv(mock.createRequest(presence));
  1626. await u.waitUntil(() => view.querySelectorAll('.occupant-list li').length > 2, 500);
  1627. occupants = view.querySelector('.occupant-list').querySelectorAll('li');
  1628. expect(occupants.length).toBe(3);
  1629. expect(occupants[2].querySelector('.occupant-nick').textContent.trim()).toBe("visitorwoman");
  1630. expect(occupants[2].querySelectorAll('.badge').length).toBe(1);
  1631. expect(sizzle('.badge', occupants[2]).pop().textContent.trim()).toBe('Visitor');
  1632. expect(occupants[2].getAttribute('title')).toBe(
  1633. contact_jid + ' This user can NOT send messages in this groupchat. Click to mention visitorwoman in your message.'
  1634. );
  1635. done();
  1636. }));
  1637. it("properly handles notification that a room has been destroyed",
  1638. mock.initConverse([], {}, async function (done, _converse) {
  1639. await mock.openChatRoomViaModal(_converse, 'problematic@muc.montague.lit', 'romeo')
  1640. const presence = $pres().attrs({
  1641. from:'problematic@muc.montague.lit',
  1642. id:'n13mt3l',
  1643. to:'romeo@montague.lit/pda',
  1644. type:'error'})
  1645. .c('error').attrs({'type':'cancel'})
  1646. .c('gone').attrs({'xmlns':'urn:ietf:params:xml:ns:xmpp-stanzas'})
  1647. .t('xmpp:other-room@chat.jabberfr.org?join').up()
  1648. .c('text').attrs({'xmlns':'urn:ietf:params:xml:ns:xmpp-stanzas'})
  1649. .t("We didn't like the name").nodeTree;
  1650. const view = _converse.chatboxviews.get('problematic@muc.montague.lit');
  1651. _converse.connection._dataRecv(mock.createRequest(presence));
  1652. expect(view.querySelector('.chatroom-body .disconnect-msg').textContent.trim())
  1653. .toBe('This groupchat no longer exists');
  1654. expect(view.querySelector('.chatroom-body .destroyed-reason').textContent.trim())
  1655. .toBe(`"We didn't like the name"`);
  1656. expect(view.querySelector('.chatroom-body .moved-label').textContent.trim())
  1657. .toBe('The conversation has moved. Click below to enter.');
  1658. expect(view.querySelector('.chatroom-body .moved-link').textContent.trim())
  1659. .toBe(`other-room@chat.jabberfr.org`);
  1660. done();
  1661. }));
  1662. it("will use the user's reserved nickname, if it exists",
  1663. mock.initConverse(['chatBoxesFetched'], {}, async function (done, _converse) {
  1664. const IQ_stanzas = _converse.connection.IQ_stanzas;
  1665. const muc_jid = 'lounge@montague.lit';
  1666. await mock.openChatRoom(_converse, 'lounge', 'montague.lit', 'romeo');
  1667. let stanza = await u.waitUntil(() => IQ_stanzas.filter(
  1668. iq => iq.querySelector(
  1669. `iq[to="${muc_jid}"] query[xmlns="http://jabber.org/protocol/disco#info"]`
  1670. )).pop()
  1671. );
  1672. // We pretend this is a new room, so no disco info is returned.
  1673. const features_stanza = $iq({
  1674. from: 'lounge@montague.lit',
  1675. 'id': stanza.getAttribute('id'),
  1676. 'to': 'romeo@montague.lit/desktop',
  1677. 'type': 'error'
  1678. }).c('error', {'type': 'cancel'})
  1679. .c('item-not-found', {'xmlns': "urn:ietf:params:xml:ns:xmpp-stanzas"});
  1680. _converse.connection._dataRecv(mock.createRequest(features_stanza));
  1681. /* <iq from='hag66@shakespeare.lit/pda'
  1682. * id='getnick1'
  1683. * to='coven@chat.shakespeare.lit'
  1684. * type='get'>
  1685. * <query xmlns='http://jabber.org/protocol/disco#info'
  1686. * node='x-roomuser-item'/>
  1687. * </iq>
  1688. */
  1689. const iq = await u.waitUntil(() => _.filter(
  1690. IQ_stanzas,
  1691. s => sizzle(`iq[to="${muc_jid}"] query[node="x-roomuser-item"]`, s).length
  1692. ).pop()
  1693. );
  1694. expect(Strophe.serialize(iq)).toBe(
  1695. `<iq from="romeo@montague.lit/orchard" id="${iq.getAttribute('id')}" to="lounge@montague.lit" `+
  1696. `type="get" xmlns="jabber:client">`+
  1697. `<query node="x-roomuser-item" xmlns="http://jabber.org/protocol/disco#info"/></iq>`);
  1698. /* <iq from='coven@chat.shakespeare.lit'
  1699. * id='getnick1'
  1700. * to='hag66@shakespeare.lit/pda'
  1701. * type='result'>
  1702. * <query xmlns='http://jabber.org/protocol/disco#info'
  1703. * node='x-roomuser-item'>
  1704. * <identity
  1705. * category='conference'
  1706. * name='thirdwitch'
  1707. * type='text'/>
  1708. * </query>
  1709. * </iq>
  1710. */
  1711. const view = _converse.chatboxviews.get('lounge@montague.lit');
  1712. stanza = $iq({
  1713. 'type': 'result',
  1714. 'id': iq.getAttribute('id'),
  1715. 'from': view.model.get('jid'),
  1716. 'to': _converse.connection.jid
  1717. }).c('query', {'xmlns': 'http://jabber.org/protocol/disco#info', 'node': 'x-roomuser-item'})
  1718. .c('identity', {'category': 'conference', 'name': 'thirdwitch', 'type': 'text'});
  1719. _converse.connection._dataRecv(mock.createRequest(stanza));
  1720. // The user has just entered the groupchat (because join was called)
  1721. // and receives their own presence from the server.
  1722. // See example 24:
  1723. // https://xmpp.org/extensions/xep-0045.html#enter-pres
  1724. const presence = $pres({
  1725. to:'romeo@montague.lit/orchard',
  1726. from:'lounge@montague.lit/thirdwitch',
  1727. id:'DC352437-C019-40EC-B590-AF29E879AF97'
  1728. }).c('x').attrs({xmlns:'http://jabber.org/protocol/muc#user'})
  1729. .c('item').attrs({
  1730. affiliation: 'member',
  1731. jid: 'romeo@montague.lit/orchard',
  1732. role: 'participant'
  1733. }).up()
  1734. .c('status').attrs({code:'110'}).up()
  1735. .c('status').attrs({code:'210'}).nodeTree;
  1736. _converse.connection._dataRecv(mock.createRequest(presence));
  1737. await u.waitUntil(() => (view.model.session.get('connection_status') === converse.ROOMSTATUS.ENTERED));
  1738. await mock.returnMemberLists(_converse, muc_jid, [], ['member', 'admin', 'owner']);
  1739. await u.waitUntil(() => view.querySelectorAll('.chat-content .chat-info').length);
  1740. const info_text = sizzle('.chat-content .chat-info:first', view).pop().textContent.trim();
  1741. expect(info_text).toBe('Your nickname has been automatically set to thirdwitch');
  1742. done();
  1743. }));
  1744. it("allows the user to invite their roster contacts to enter the groupchat",
  1745. mock.initConverse(['chatBoxesFetched'], {'view_mode': 'fullscreen'}, async function (done, _converse) {
  1746. // We need roster contacts, so that we have someone to invite
  1747. await mock.waitForRoster(_converse, 'current');
  1748. const features = [
  1749. 'http://jabber.org/protocol/muc',
  1750. 'jabber:iq:register',
  1751. 'muc_passwordprotected',
  1752. 'muc_hidden',
  1753. 'muc_temporary',
  1754. 'muc_membersonly',
  1755. 'muc_unmoderated',
  1756. 'muc_anonymous'
  1757. ]
  1758. await mock.openAndEnterChatRoom(_converse, 'lounge@montague.lit', 'romeo', features);
  1759. const view = _converse.chatboxviews.get('lounge@montague.lit');
  1760. expect(view.model.getOwnAffiliation()).toBe('owner');
  1761. expect(view.model.features.get('open')).toBe(false);
  1762. await u.waitUntil(() => view.querySelector('.open-invite-modal'));
  1763. // Members can't invite if the room isn't open
  1764. view.model.getOwnOccupant().set('affiliation', 'member');
  1765. await u.waitUntil(() => view.querySelector('.open-invite-modal') === null);
  1766. view.model.features.set('open', 'true');
  1767. await u.waitUntil(() => view.querySelector('.open-invite-modal'));
  1768. view.querySelector('.open-invite-modal').click();
  1769. const modal = _converse.api.modal.get('muc-invite-modal');
  1770. await u.waitUntil(() => u.isVisible(modal.el), 1000)
  1771. expect(modal.el.querySelectorAll('#invitee_jids').length).toBe(1);
  1772. expect(modal.el.querySelectorAll('textarea').length).toBe(1);
  1773. spyOn(view.model, 'directInvite').and.callThrough();
  1774. const input = modal.el.querySelector('#invitee_jids');
  1775. input.value = "Balt";
  1776. modal.el.querySelector('button[type="submit"]').click();
  1777. await u.waitUntil(() => modal.el.querySelector('.error'));
  1778. const error = modal.el.querySelector('.error');
  1779. expect(error.textContent).toBe('Please enter a valid XMPP address');
  1780. let evt = new Event('input');
  1781. input.dispatchEvent(evt);
  1782. let sent_stanza;
  1783. spyOn(_converse.connection, 'send').and.callFake(stanza => (sent_stanza = stanza));
  1784. const hint = await u.waitUntil(() => modal.el.querySelector('.suggestion-box__results li'));
  1785. expect(input.value).toBe('Balt');
  1786. expect(hint.textContent.trim()).toBe('Balthasar');
  1787. evt = new Event('mousedown', {'bubbles': true});
  1788. evt.button = 0;
  1789. hint.dispatchEvent(evt);
  1790. const textarea = modal.el.querySelector('textarea');
  1791. textarea.value = "Please join!";
  1792. modal.el.querySelector('button[type="submit"]').click();
  1793. expect(view.model.directInvite).toHaveBeenCalled();
  1794. expect(sent_stanza.toLocaleString()).toBe(
  1795. `<message from="romeo@montague.lit/orchard" `+
  1796. `id="${sent_stanza.nodeTree.getAttribute("id")}" `+
  1797. `to="balthasar@montague.lit" `+
  1798. `xmlns="jabber:client">`+
  1799. `<x jid="lounge@montague.lit" reason="Please join!" xmlns="jabber:x:conference"/>`+
  1800. `</message>`
  1801. );
  1802. done();
  1803. }));
  1804. it("can be joined automatically, based upon a received invite",
  1805. mock.initConverse([], {}, async function (done, _converse) {
  1806. await mock.waitForRoster(_converse, 'current'); // We need roster contacts, who can invite us
  1807. const name = mock.cur_names[0];
  1808. const from_jid = name.replace(/ /g,'.').toLowerCase() + '@montague.lit';
  1809. await u.waitUntil(() => _converse.roster.get(from_jid).vcard.get('fullname'));
  1810. spyOn(window, 'confirm').and.callFake(() => true);
  1811. await mock.openAndEnterChatRoom(_converse, 'lounge@montague.lit', 'romeo');
  1812. const view = _converse.chatboxviews.get('lounge@montague.lit');
  1813. await view.close(); // Hack, otherwise we have to mock stanzas.
  1814. const muc_jid = 'lounge@montague.lit';
  1815. const reason = "Please join this groupchat";
  1816. expect(_converse.chatboxes.models.length).toBe(1);
  1817. expect(_converse.chatboxes.models[0].id).toBe("controlbox");
  1818. const stanza = u.toStanza(`
  1819. <message xmlns="jabber:client" to="${_converse.bare_jid}" from="${from_jid}" id="9bceb415-f34b-4fa4-80d5-c0d076a24231">
  1820. <x xmlns="jabber:x:conference" jid="${muc_jid}" reason="${reason}"/>
  1821. </message>`);
  1822. await _converse.onDirectMUCInvitation(stanza);
  1823. expect(window.confirm).toHaveBeenCalledWith(
  1824. name + ' has invited you to join a groupchat: '+ muc_jid +
  1825. ', and left the following reason: "'+reason+'"');
  1826. expect(_converse.chatboxes.models.length).toBe(2);
  1827. expect(_converse.chatboxes.models[0].id).toBe('controlbox');
  1828. expect(_converse.chatboxes.models[1].id).toBe(muc_jid);
  1829. done();
  1830. }));
  1831. it("shows received groupchat messages",
  1832. mock.initConverse([], {}, async function (done, _converse) {
  1833. const text = 'This is a received message';
  1834. await mock.openAndEnterChatRoom(_converse, 'lounge@montague.lit', 'romeo');
  1835. spyOn(_converse.api, "trigger").and.callThrough();
  1836. const view = _converse.chatboxviews.get('lounge@montague.lit');
  1837. const nick = mock.chatroom_names[0];
  1838. view.model.occupants.create({
  1839. 'nick': nick,
  1840. 'muc_jid': `${view.model.get('jid')}/${nick}`
  1841. });
  1842. const message = $msg({
  1843. from: 'lounge@montague.lit/'+nick,
  1844. id: '1',
  1845. to: 'romeo@montague.lit',
  1846. type: 'groupchat'
  1847. }).c('body').t(text);
  1848. await view.model.handleMessageStanza(message.nodeTree);
  1849. await u.waitUntil(() => view.querySelectorAll('.chat-msg').length);
  1850. expect(view.content.querySelectorAll('.chat-msg').length).toBe(1);
  1851. expect(view.content.querySelector('.chat-msg__text').textContent.trim()).toBe(text);
  1852. expect(_converse.api.trigger).toHaveBeenCalledWith('message', jasmine.any(Object));
  1853. done();
  1854. }));
  1855. it("shows sent groupchat messages", mock.initConverse([], {}, async function (done, _converse) {
  1856. await mock.openAndEnterChatRoom(_converse, 'lounge@montague.lit', 'romeo');
  1857. spyOn(_converse.api, "trigger").and.callThrough();
  1858. const view = _converse.chatboxviews.get('lounge@montague.lit');
  1859. const text = 'This is a sent message';
  1860. const textarea = await u.waitUntil(() => view.querySelector('.chat-textarea'));
  1861. textarea.value = text;
  1862. const bottom_panel = view.querySelector('converse-muc-bottom-panel');
  1863. bottom_panel.onKeyDown({
  1864. target: textarea,
  1865. preventDefault: function preventDefault () {},
  1866. keyCode: 13
  1867. });
  1868. await u.waitUntil(() => view.querySelectorAll('.chat-msg__text').length);
  1869. expect(_converse.api.trigger).toHaveBeenCalledWith('messageSend', jasmine.any(_converse.Message));
  1870. expect(view.content.querySelectorAll('.chat-msg').length).toBe(1);
  1871. // Let's check that if we receive the same message again, it's
  1872. // not shown.
  1873. const stanza = u.toStanza(`
  1874. <message xmlns="jabber:client"
  1875. from="lounge@montague.lit/romeo"
  1876. to="${_converse.connection.jid}"
  1877. type="groupchat">
  1878. <body>${text}</body>
  1879. <stanza-id xmlns="urn:xmpp:sid:0"
  1880. id="5f3dbc5e-e1d3-4077-a492-693f3769c7ad"
  1881. by="lounge@montague.lit"/>
  1882. <origin-id xmlns="urn:xmpp:sid:0" id="${view.model.messages.at(0).get('origin_id')}"/>
  1883. </message>`);
  1884. await view.model.handleMessageStanza(stanza);
  1885. expect(view.content.querySelectorAll('.chat-msg').length).toBe(1);
  1886. expect(sizzle('.chat-msg__text:last').pop().textContent.trim()).toBe(text);
  1887. expect(view.model.messages.length).toBe(1);
  1888. // We don't emit an event if it's our own message
  1889. expect(_converse.api.trigger.calls.count(), 1);
  1890. done();
  1891. }));
  1892. it("will cause the chat area to be scrolled down only if it was at the bottom already",
  1893. mock.initConverse([], {}, async function (done, _converse) {
  1894. const message = 'This message is received while the chat area is scrolled up';
  1895. await mock.openAndEnterChatRoom(_converse, 'lounge@montague.lit', 'romeo');
  1896. const view = _converse.chatboxviews.get('lounge@montague.lit');
  1897. spyOn(view, 'scrollDown').and.callThrough();
  1898. // Create enough messages so that there's a scrollbar.
  1899. const promises = [];
  1900. for (let i=0; i<20; i++) {
  1901. promises.push(
  1902. view.model.handleMessageStanza(
  1903. $msg({
  1904. from: 'lounge@montague.lit/someone',
  1905. to: 'romeo@montague.lit.com',
  1906. type: 'groupchat',
  1907. id: u.getUniqueId(),
  1908. }).c('body').t('Message: '+i).tree())
  1909. );
  1910. }
  1911. await Promise.all(promises);
  1912. // Give enough time for `markScrolled` to have been called
  1913. setTimeout(async () => {
  1914. view.content.scrollTop = 0;
  1915. await view.model.handleMessageStanza(
  1916. $msg({
  1917. from: 'lounge@montague.lit/someone',
  1918. to: 'romeo@montague.lit.com',
  1919. type: 'groupchat',
  1920. id: u.getUniqueId(),
  1921. }).c('body').t(message).tree());
  1922. await u.waitUntil(() => view.querySelectorAll('.chat-msg__text').length === 21);
  1923. // Now check that the message appears inside the chatbox in the DOM
  1924. const msg_txt = sizzle('.chat-msg:last .chat-msg__text', view.content).pop().textContent;
  1925. expect(msg_txt).toEqual(message);
  1926. expect(view.content.scrollTop).toBe(0);
  1927. done();
  1928. }, 500);
  1929. }));
  1930. it("reconnects when no-acceptable error is returned when sending a message",
  1931. mock.initConverse([], {}, async function (done, _converse) {
  1932. const muc_jid = 'coven@chat.shakespeare.lit';
  1933. await mock.openAndEnterChatRoom(_converse, muc_jid, 'romeo');
  1934. const view = _converse.chatboxviews.get(muc_jid);
  1935. expect(view.model.session.get('connection_status')).toBe(converse.ROOMSTATUS.ENTERED);
  1936. await mock.sendMessage(view, 'hello world');
  1937. const stanza = u.toStanza(`
  1938. <message xmlns='jabber:client'
  1939. from='${muc_jid}'
  1940. type='error'
  1941. to='${_converse.bare_jid}'>
  1942. <error type='cancel'>
  1943. <not-acceptable xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/>
  1944. </error>
  1945. </message>`);
  1946. _converse.connection._dataRecv(mock.createRequest(stanza));
  1947. let sent_stanzas = _converse.connection.sent_stanzas;
  1948. const iq = await u.waitUntil(() => sent_stanzas.filter(s => sizzle(`[xmlns="${Strophe.NS.PING}"]`, s).length).pop());
  1949. expect(Strophe.serialize(iq)).toBe(
  1950. `<iq id="${iq.getAttribute('id')}" to="coven@chat.shakespeare.lit/romeo" type="get" xmlns="jabber:client">`+
  1951. `<ping xmlns="urn:xmpp:ping"/>`+
  1952. `</iq>`);
  1953. const result = u.toStanza(`
  1954. <iq from='${muc_jid}'
  1955. id='${iq.getAttribute('id')}'
  1956. to='${_converse.bare_jid}'
  1957. type='error'>
  1958. <error type='cancel'>
  1959. <not-acceptable xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/>
  1960. </error>
  1961. </iq>`);
  1962. sent_stanzas = _converse.connection.sent_stanzas;
  1963. const index = sent_stanzas.length -1;
  1964. _converse.connection.IQ_stanzas = [];
  1965. _converse.connection._dataRecv(mock.createRequest(result));
  1966. await mock.getRoomFeatures(_converse, muc_jid);
  1967. const pres = await u.waitUntil(
  1968. () => sent_stanzas.slice(index).filter(s => s.nodeName === 'presence').pop());
  1969. expect(Strophe.serialize(pres)).toBe(
  1970. `<presence from="${_converse.jid}" to="coven@chat.shakespeare.lit/romeo" xmlns="jabber:client">`+
  1971. `<x xmlns="http://jabber.org/protocol/muc"><history maxstanzas="0"/></x>`+
  1972. `</presence>`);
  1973. done();
  1974. }));
  1975. it("informs users if the room configuration has changed",
  1976. mock.initConverse([], {}, async function (done, _converse) {
  1977. const muc_jid = 'coven@chat.shakespeare.lit';
  1978. await mock.openAndEnterChatRoom(_converse, 'coven@chat.shakespeare.lit', 'romeo');
  1979. const view = _converse.chatboxviews.get(muc_jid);
  1980. expect(view.model.session.get('connection_status')).toBe(converse.ROOMSTATUS.ENTERED);
  1981. const stanza = u.toStanza(`
  1982. <message from='${muc_jid}'
  1983. id='80349046-F26A-44F3-A7A6-54825064DD9E'
  1984. to='${_converse.jid}'
  1985. type='groupchat'>
  1986. <x xmlns='http://jabber.org/protocol/muc#user'>
  1987. <status code='170'/>
  1988. </x>
  1989. </message>`);
  1990. _converse.connection._dataRecv(mock.createRequest(stanza));
  1991. await u.waitUntil(() => view.querySelectorAll('.chat-content .chat-info').length);
  1992. const info_messages = view.querySelectorAll('.chat-content .chat-info');
  1993. expect(info_messages[0].textContent.trim()).toBe('Groupchat logging is now enabled');
  1994. done();
  1995. }));
  1996. it("informs users if their nicknames have been changed.",
  1997. mock.initConverse([], {}, async function (done, _converse) {
  1998. /* The service then sends two presence stanzas to the full JID
  1999. * of each occupant (including the occupant who is changing his
  2000. * or her room nickname), one of type "unavailable" for the old
  2001. * nickname and one indicating availability for the new
  2002. * nickname.
  2003. *
  2004. * See: https://xmpp.org/extensions/xep-0045.html#changenick
  2005. *
  2006. * <presence
  2007. * from='coven@montague.lit/thirdwitch'
  2008. * id='DC352437-C019-40EC-B590-AF29E879AF98'
  2009. * to='hag66@shakespeare.lit/pda'
  2010. * type='unavailable'>
  2011. * <x xmlns='http://jabber.org/protocol/muc#user'>
  2012. * <item affiliation='member'
  2013. * jid='hag66@shakespeare.lit/pda'
  2014. * nick='oldhag'
  2015. * role='participant'/>
  2016. * <status code='303'/>
  2017. * <status code='110'/>
  2018. * </x>
  2019. * </presence>
  2020. *
  2021. * <presence
  2022. * from='coven@montague.lit/oldhag'
  2023. * id='5B4F27A4-25ED-43F7-A699-382C6B4AFC67'
  2024. * to='hag66@shakespeare.lit/pda'>
  2025. * <x xmlns='http://jabber.org/protocol/muc#user'>
  2026. * <item affiliation='member'
  2027. * jid='hag66@shakespeare.lit/pda'
  2028. * role='participant'/>
  2029. * <status code='110'/>
  2030. * </x>
  2031. * </presence>
  2032. */
  2033. const __ = _converse.__;
  2034. await mock.openAndEnterChatRoom(_converse, 'lounge@montague.lit', 'oldnick');
  2035. const view = _converse.chatboxviews.get('lounge@montague.lit');
  2036. expect(view.model.session.get('connection_status')).toBe(converse.ROOMSTATUS.ENTERED);
  2037. await u.waitUntil(() => view.querySelectorAll('li .occupant-nick').length, 500);
  2038. let occupants = view.querySelector('.occupant-list');
  2039. expect(occupants.childElementCount).toBe(1);
  2040. expect(occupants.firstElementChild.querySelector('.occupant-nick').textContent.trim()).toBe("oldnick");
  2041. const csntext = await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent);
  2042. expect(csntext.trim()).toEqual("oldnick has entered the groupchat");
  2043. let presence = $pres().attrs({
  2044. from:'lounge@montague.lit/oldnick',
  2045. id:'DC352437-C019-40EC-B590-AF29E879AF98',
  2046. to:'romeo@montague.lit/pda',
  2047. type:'unavailable'
  2048. })
  2049. .c('x').attrs({xmlns:'http://jabber.org/protocol/muc#user'})
  2050. .c('item').attrs({
  2051. affiliation: 'owner',
  2052. jid: 'romeo@montague.lit/pda',
  2053. nick: 'newnick',
  2054. role: 'moderator'
  2055. }).up()
  2056. .c('status').attrs({code:'303'}).up()
  2057. .c('status').attrs({code:'110'}).nodeTree;
  2058. _converse.connection._dataRecv(mock.createRequest(presence));
  2059. await u.waitUntil(() => view.querySelectorAll('.chat-info').length);
  2060. expect(sizzle('div.chat-info:last').pop().textContent.trim()).toBe(
  2061. __(_converse.muc.new_nickname_messages["303"], "newnick")
  2062. );
  2063. expect(view.model.session.get('connection_status')).toBe(converse.ROOMSTATUS.ENTERED);
  2064. occupants = view.querySelector('.occupant-list');
  2065. expect(occupants.childElementCount).toBe(1);
  2066. presence = $pres().attrs({
  2067. from:'lounge@montague.lit/newnick',
  2068. id:'5B4F27A4-25ED-43F7-A699-382C6B4AFC67',
  2069. to:'romeo@montague.lit/pda'
  2070. })
  2071. .c('x').attrs({xmlns:'http://jabber.org/protocol/muc#user'})
  2072. .c('item').attrs({
  2073. affiliation: 'owner',
  2074. jid: 'romeo@montague.lit/pda',
  2075. role: 'moderator'
  2076. }).up()
  2077. .c('status').attrs({code:'110'}).nodeTree;
  2078. _converse.connection._dataRecv(mock.createRequest(presence));
  2079. expect(view.model.session.get('connection_status')).toBe(converse.ROOMSTATUS.ENTERED);
  2080. expect(view.content.querySelectorAll('div.chat-info').length).toBe(1);
  2081. expect(sizzle('div.chat-info', view.content)[0].textContent.trim()).toBe(
  2082. __(_converse.muc.new_nickname_messages["303"], "newnick")
  2083. );
  2084. occupants = view.querySelector('.occupant-list');
  2085. expect(occupants.childElementCount).toBe(1);
  2086. expect(sizzle('.occupant-nick:first', occupants).pop().textContent.trim()).toBe("newnick");
  2087. done();
  2088. }));
  2089. it("queries for the groupchat information before attempting to join the user",
  2090. mock.initConverse(['chatBoxesFetched'], {}, async function (done, _converse) {
  2091. const nick = "some1";
  2092. const IQ_stanzas = _converse.connection.IQ_stanzas;
  2093. const muc_jid = 'coven@chat.shakespeare.lit';
  2094. await _converse.api.rooms.open(muc_jid, { nick });
  2095. const stanza = await u.waitUntil(() => _.filter(
  2096. IQ_stanzas,
  2097. iq => iq.querySelector(
  2098. `iq[to="${muc_jid}"] query[xmlns="http://jabber.org/protocol/disco#info"]`
  2099. )).pop());
  2100. // Check that the groupchat queried for the feautures.
  2101. expect(Strophe.serialize(stanza)).toBe(
  2102. `<iq from="romeo@montague.lit/orchard" id="${stanza.getAttribute("id")}" to="${muc_jid}" type="get" xmlns="jabber:client">`+
  2103. `<query xmlns="http://jabber.org/protocol/disco#info"/>`+
  2104. `</iq>`);
  2105. /* <iq from='coven@chat.shakespeare.lit'
  2106. * id='ik3vs715'
  2107. * to='hag66@shakespeare.lit/pda'
  2108. * type='result'>
  2109. * <query xmlns='http://jabber.org/protocol/disco#info'>
  2110. * <identity
  2111. * category='conference'
  2112. * name='A Dark Cave'
  2113. * type='text'/>
  2114. * <feature var='http://jabber.org/protocol/muc'/>
  2115. * <feature var='muc_passwordprotected'/>
  2116. * <feature var='muc_hidden'/>
  2117. * <feature var='muc_temporary'/>
  2118. * <feature var='muc_open'/>
  2119. * <feature var='muc_unmoderated'/>
  2120. * <feature var='muc_nonanonymous'/>
  2121. * </query>
  2122. * </iq>
  2123. */
  2124. const features_stanza = $iq({
  2125. 'from': muc_jid,
  2126. 'id': stanza.getAttribute('id'),
  2127. 'to': 'romeo@montague.lit/desktop',
  2128. 'type': 'result'
  2129. })
  2130. .c('query', { 'xmlns': 'http://jabber.org/protocol/disco#info'})
  2131. .c('identity', {
  2132. 'category': 'conference',
  2133. 'name': 'A Dark Cave',
  2134. 'type': 'text'
  2135. }).up()
  2136. .c('feature', {'var': 'http://jabber.org/protocol/muc'}).up()
  2137. .c('feature', {'var': 'muc_passwordprotected'}).up()
  2138. .c('feature', {'var': 'muc_hidden'}).up()
  2139. .c('feature', {'var': 'muc_temporary'}).up()
  2140. .c('feature', {'var': 'muc_open'}).up()
  2141. .c('feature', {'var': 'muc_unmoderated'}).up()
  2142. .c('feature', {'var': 'muc_nonanonymous'});
  2143. _converse.connection._dataRecv(mock.createRequest(features_stanza));
  2144. let view = _converse.chatboxviews.get('coven@chat.shakespeare.lit');
  2145. const sent_stanzas = _converse.connection.sent_stanzas;
  2146. await u.waitUntil(() => sent_stanzas.filter(s => s.matches(`presence[to="${muc_jid}/${nick}"]`)).pop());
  2147. view = _converse.chatboxviews.get('coven@chat.shakespeare.lit');
  2148. expect(view.model.features.get('fetched')).toBeTruthy();
  2149. expect(view.model.features.get('passwordprotected')).toBe(true);
  2150. expect(view.model.features.get('hidden')).toBe(true);
  2151. expect(view.model.features.get('temporary')).toBe(true);
  2152. expect(view.model.features.get('open')).toBe(true);
  2153. expect(view.model.features.get('unmoderated')).toBe(true);
  2154. expect(view.model.features.get('nonanonymous')).toBe(true);
  2155. done();
  2156. }));
  2157. it("updates the shown features when the groupchat configuration has changed",
  2158. mock.initConverse([], {'view_mode': 'fullscreen'}, async function (done, _converse) {
  2159. let features = [
  2160. 'http://jabber.org/protocol/muc',
  2161. 'jabber:iq:register',
  2162. 'muc_passwordprotected',
  2163. 'muc_publicroom',
  2164. 'muc_temporary',
  2165. 'muc_open',
  2166. 'muc_unmoderated',
  2167. 'muc_nonanonymous'
  2168. ];
  2169. await mock.openAndEnterChatRoom(_converse, 'room@conference.example.org', 'romeo', features);
  2170. const jid = 'room@conference.example.org';
  2171. const view = _converse.chatboxviews.get(jid);
  2172. const info_el = view.querySelector(".show-muc-details-modal");
  2173. info_el.click();
  2174. const modal = _converse.api.modal.get('muc-details-modal');
  2175. await u.waitUntil(() => u.isVisible(modal.el), 1000);
  2176. let features_list = modal.el.querySelector('.features-list');
  2177. let features_shown = features_list.textContent.split('\n').map(s => s.trim()).filter(s => s);
  2178. expect(features_shown.join(' ')).toBe(
  2179. 'Password protected - This groupchat requires a password before entry '+
  2180. 'Open - Anyone can join this groupchat '+
  2181. 'Temporary - This groupchat will disappear once the last person leaves '+
  2182. 'Not anonymous - All other groupchat participants can see your XMPP address '+
  2183. 'Not moderated - Participants entering this groupchat can write right away');
  2184. expect(view.model.features.get('hidden')).toBe(false);
  2185. expect(view.model.features.get('mam_enabled')).toBe(false);
  2186. expect(view.model.features.get('membersonly')).toBe(false);
  2187. expect(view.model.features.get('moderated')).toBe(false);
  2188. expect(view.model.features.get('nonanonymous')).toBe(true);
  2189. expect(view.model.features.get('open')).toBe(true);
  2190. expect(view.model.features.get('passwordprotected')).toBe(true);
  2191. expect(view.model.features.get('persistent')).toBe(false);
  2192. expect(view.model.features.get('publicroom')).toBe(true);
  2193. expect(view.model.features.get('semianonymous')).toBe(false);
  2194. expect(view.model.features.get('temporary')).toBe(true);
  2195. expect(view.model.features.get('unmoderated')).toBe(true);
  2196. expect(view.model.features.get('unsecured')).toBe(false);
  2197. await u.waitUntil(() => view.querySelector('.chatbox-title__text').textContent.trim() === 'Room');
  2198. view.querySelector('.configure-chatroom-button').click();
  2199. const IQs = _converse.connection.IQ_stanzas;
  2200. let iq = await u.waitUntil(() => _.filter(
  2201. IQs,
  2202. iq => iq.querySelector(
  2203. `iq[to="${jid}"] query[xmlns="${Strophe.NS.MUC_OWNER}"]`
  2204. )).pop());
  2205. const response_el = u.toStanza(
  2206. `<iq xmlns="jabber:client"
  2207. type="result"
  2208. to="romeo@montague.lit/pda"
  2209. from="room@conference.example.org" id="${iq.getAttribute('id')}">
  2210. <query xmlns="http://jabber.org/protocol/muc#owner">
  2211. <x xmlns="jabber:x:data" type="form">
  2212. <title>Configuration for room@conference.example.org</title>
  2213. <instructions>Complete and submit this form to configure the room.</instructions>
  2214. <field var="FORM_TYPE" type="hidden">
  2215. <value>http://jabber.org/protocol/muc#roomconfig</value>
  2216. </field>
  2217. <field type="fixed">
  2218. <value>Room information</value>
  2219. </field>
  2220. <field var="muc#roomconfig_roomname" type="text-single" label="Title">
  2221. <value>Room</value>
  2222. </field>
  2223. <field var="muc#roomconfig_roomdesc" type="text-single" label="Description">
  2224. <desc>A brief description of the room</desc>
  2225. <value>This room is used in tests</value>
  2226. </field>
  2227. <field var="muc#roomconfig_lang" type="text-single" label="Language tag for room (e.g. 'en', 'de', 'fr' etc.)">
  2228. <desc>Indicate the primary language spoken in this room</desc>
  2229. <value>en</value>
  2230. </field>
  2231. <field var="muc#roomconfig_persistentroom" type="boolean" label="Persistent (room should remain even when it is empty)">
  2232. <desc>Rooms are automatically deleted when they are empty, unless this option is enabled</desc>
  2233. <value>1</value>
  2234. </field>
  2235. <field var="muc#roomconfig_publicroom" type="boolean" label="Include room information in public lists">
  2236. <desc>Enable this to allow people to find the room</desc>
  2237. <value>1</value>
  2238. </field>
  2239. <field type="fixed"><value>Access to the room</value></field>
  2240. <field var="muc#roomconfig_roomsecret" type="text-private" label="Password"><value/></field>
  2241. <field var="muc#roomconfig_membersonly" type="boolean" label="Only allow members to join">
  2242. <desc>Enable this to only allow access for room owners, admins and members</desc>
  2243. </field>
  2244. <field var="{http://prosody.im/protocol/muc}roomconfig_allowmemberinvites" type="boolean" label="Allow members to invite new members"/>
  2245. <field type="fixed"><value>Permissions in the room</value>
  2246. </field>
  2247. <field var="muc#roomconfig_changesubject" type="boolean" label="Allow anyone to set the room's subject">
  2248. <desc>Choose whether anyone, or only moderators, may set the room's subject</desc>
  2249. </field>
  2250. <field var="muc#roomconfig_moderatedroom" type="boolean" label="Moderated (require permission to speak)">
  2251. <desc>In moderated rooms occupants must be given permission to speak by a room moderator</desc>
  2252. </field>
  2253. <field var="muc#roomconfig_whois" type="list-single" label="Addresses (JIDs) of room occupants may be viewed by:">
  2254. <option label="Moderators only"><value>moderators</value></option>
  2255. <option label="Anyone"><value>anyone</value></option>
  2256. <value>anyone</value>
  2257. </field>
  2258. <field type="fixed"><value>Other options</value></field>
  2259. <field var="muc#roomconfig_historylength" type="text-single" label="Maximum number of history messages returned by room">
  2260. <desc>Specify the maximum number of previous messages that should be sent to users when they join the room</desc>
  2261. <value>50</value>
  2262. </field>
  2263. <field var="muc#roomconfig_defaulthistorymessages" type="text-single" label="Default number of history messages returned by room">
  2264. <desc>Specify the number of previous messages sent to new users when they join the room</desc>
  2265. <value>20</value>
  2266. </field>
  2267. </x>
  2268. </query>
  2269. </iq>`);
  2270. _converse.connection._dataRecv(mock.createRequest(response_el));
  2271. const el = await u.waitUntil(() => document.querySelector('.chatroom-form legend'));
  2272. expect(el.textContent.trim()).toBe("Configuration for room@conference.example.org");
  2273. sizzle('[name="muc#roomconfig_membersonly"]', view).pop().click();
  2274. sizzle('[name="muc#roomconfig_roomname"]', view).pop().value = "New room name"
  2275. view.querySelector('.chatroom-form input[type="submit"]').click();
  2276. iq = await u.waitUntil(() => _.filter(IQs, iq => u.matchesSelector(iq, `iq[to="${jid}"][type="set"]`)).pop());
  2277. const result = $iq({
  2278. "xmlns": "jabber:client",
  2279. "type": "result",
  2280. "to": "romeo@montague.lit/orchard",
  2281. "from": "lounge@muc.montague.lit",
  2282. "id": iq.getAttribute('id')
  2283. });
  2284. IQs.length = 0; // Empty the array
  2285. _converse.connection._dataRecv(mock.createRequest(result));
  2286. iq = await u.waitUntil(() => _.filter(
  2287. IQs,
  2288. iq => iq.querySelector(
  2289. `iq[to="${jid}"] query[xmlns="http://jabber.org/protocol/disco#info"]`
  2290. )).pop());
  2291. const features_stanza = $iq({
  2292. 'from': jid,
  2293. 'id': iq.getAttribute('id'),
  2294. 'to': 'romeo@montague.lit/desktop',
  2295. 'type': 'result'
  2296. }).c('query', { 'xmlns': 'http://jabber.org/protocol/disco#info'})
  2297. .c('identity', {
  2298. 'category': 'conference',
  2299. 'name': 'New room name',
  2300. 'type': 'text'
  2301. }).up();
  2302. features = [
  2303. 'http://jabber.org/protocol/muc',
  2304. 'jabber:iq:register',
  2305. 'muc_passwordprotected',
  2306. 'muc_hidden',
  2307. 'muc_temporary',
  2308. 'muc_membersonly',
  2309. 'muc_unmoderated',
  2310. 'muc_nonanonymous'
  2311. ];
  2312. features.forEach(f => features_stanza.c('feature', {'var': f}).up());
  2313. features_stanza.c('x', { 'xmlns':'jabber:x:data', 'type':'result'})
  2314. .c('field', {'var':'FORM_TYPE', 'type':'hidden'})
  2315. .c('value').t('http://jabber.org/protocol/muc#roominfo').up().up()
  2316. .c('field', {'type':'text-single', 'var':'muc#roominfo_description', 'label':'Description'})
  2317. .c('value').t('This is the description').up().up()
  2318. .c('field', {'type':'text-single', 'var':'muc#roominfo_occupants', 'label':'Number of occupants'})
  2319. .c('value').t(0);
  2320. _converse.connection._dataRecv(mock.createRequest(features_stanza));
  2321. await u.waitUntil(() => new Promise(success => view.model.features.on('change', success)));
  2322. features_list = modal.el.querySelector('.features-list');
  2323. features_shown = features_list.textContent.split('\n').map(s => s.trim()).filter(s => s);
  2324. expect(features_shown.join(' ')).toBe(
  2325. 'Password protected - This groupchat requires a password before entry '+
  2326. 'Hidden - This groupchat is not publicly searchable '+
  2327. 'Members only - This groupchat is restricted to members only '+
  2328. 'Temporary - This groupchat will disappear once the last person leaves '+
  2329. 'Not anonymous - All other groupchat participants can see your XMPP address '+
  2330. 'Not moderated - Participants entering this groupchat can write right away');
  2331. expect(view.model.features.get('hidden')).toBe(true);
  2332. expect(view.model.features.get('mam_enabled')).toBe(false);
  2333. expect(view.model.features.get('membersonly')).toBe(true);
  2334. expect(view.model.features.get('moderated')).toBe(false);
  2335. expect(view.model.features.get('nonanonymous')).toBe(true);
  2336. expect(view.model.features.get('open')).toBe(false);
  2337. expect(view.model.features.get('passwordprotected')).toBe(true);
  2338. expect(view.model.features.get('persistent')).toBe(false);
  2339. expect(view.model.features.get('publicroom')).toBe(false);
  2340. expect(view.model.features.get('semianonymous')).toBe(false);
  2341. expect(view.model.features.get('temporary')).toBe(true);
  2342. expect(view.model.features.get('unmoderated')).toBe(true);
  2343. expect(view.model.features.get('unsecured')).toBe(false);
  2344. await u.waitUntil(() => view.querySelector('.chatbox-title__text')?.textContent.trim() === 'New room name');
  2345. done();
  2346. }));
  2347. it("indicates when a room is no longer anonymous",
  2348. mock.initConverse([], {}, async function (done, _converse) {
  2349. let IQ_id;
  2350. const sendIQ = _converse.connection.sendIQ;
  2351. await mock.openAndEnterChatRoom(_converse, 'coven@chat.shakespeare.lit', 'some1');
  2352. spyOn(_converse.connection, 'sendIQ').and.callFake(function (iq, callback, errback) {
  2353. IQ_id = sendIQ.bind(this)(iq, callback, errback);
  2354. });
  2355. // We pretend this is a new room, so no disco info is returned.
  2356. const features_stanza = $iq({
  2357. from: 'coven@chat.shakespeare.lit',
  2358. 'id': IQ_id,
  2359. 'to': 'romeo@montague.lit/desktop',
  2360. 'type': 'error'
  2361. }).c('error', {'type': 'cancel'})
  2362. .c('item-not-found', {'xmlns': "urn:ietf:params:xml:ns:xmpp-stanzas"});
  2363. _converse.connection._dataRecv(mock.createRequest(features_stanza));
  2364. const view = _converse.chatboxviews.get('coven@chat.shakespeare.lit');
  2365. /* <message xmlns="jabber:client"
  2366. * type="groupchat"
  2367. * to="romeo@montague.lit/_converse.js-27854181"
  2368. * from="coven@chat.shakespeare.lit">
  2369. * <x xmlns="http://jabber.org/protocol/muc#user">
  2370. * <status code="104"/>
  2371. * <status code="172"/>
  2372. * </x>
  2373. * </message>
  2374. */
  2375. const message = $msg({
  2376. type:'groupchat',
  2377. to: 'romeo@montague.lit/_converse.js-27854181',
  2378. from: 'coven@chat.shakespeare.lit'
  2379. }).c('x', {xmlns: Strophe.NS.MUC_USER})
  2380. .c('status', {code: '104'}).up()
  2381. .c('status', {code: '172'});
  2382. _converse.connection._dataRecv(mock.createRequest(message));
  2383. await u.waitUntil(() => view.querySelectorAll('.chat-content .chat-info').length);
  2384. const chat_body = view.querySelector('.chatroom-body');
  2385. expect(sizzle('.message:last', chat_body).pop().textContent.trim())
  2386. .toBe('This groupchat is now no longer anonymous');
  2387. done();
  2388. }));
  2389. it("informs users if they have been kicked out of the groupchat",
  2390. mock.initConverse([], {}, async function (done, _converse) {
  2391. /* <presence
  2392. * from='harfleur@chat.shakespeare.lit/pistol'
  2393. * to='pistol@shakespeare.lit/harfleur'
  2394. * type='unavailable'>
  2395. * <x xmlns='http://jabber.org/protocol/muc#user'>
  2396. * <item affiliation='none' role='none'>
  2397. * <actor nick='Fluellen'/>
  2398. * <reason>Avaunt, you cullion!</reason>
  2399. * </item>
  2400. * <status code='110'/>
  2401. * <status code='307'/>
  2402. * </x>
  2403. * </presence>
  2404. */
  2405. await mock.openAndEnterChatRoom(_converse, 'lounge@montague.lit', 'romeo');
  2406. const view = _converse.chatboxviews.get('lounge@montague.lit');
  2407. expect(view.model.session.get('connection_status')).toBe(converse.ROOMSTATUS.ENTERED);
  2408. const presence = $pres().attrs({
  2409. from:'lounge@montague.lit/romeo',
  2410. to:'romeo@montague.lit/pda',
  2411. type:'unavailable'
  2412. })
  2413. .c('x').attrs({xmlns:'http://jabber.org/protocol/muc#user'})
  2414. .c('item').attrs({
  2415. affiliation: 'none',
  2416. jid: 'romeo@montague.lit/pda',
  2417. role: 'none'
  2418. })
  2419. .c('actor').attrs({nick: 'Fluellen'}).up()
  2420. .c('reason').t('Avaunt, you cullion!').up()
  2421. .up()
  2422. .c('status').attrs({code:'110'}).up()
  2423. .c('status').attrs({code:'307'}).nodeTree;
  2424. _converse.connection._dataRecv(mock.createRequest(presence));
  2425. await u.waitUntil(() => !u.isVisible(view.querySelector('.chat-area')));
  2426. expect(u.isVisible(view.querySelector('.occupants'))).toBeFalsy();
  2427. const chat_body = view.querySelector('.chatroom-body');
  2428. expect(chat_body.querySelectorAll('.disconnect-msg').length).toBe(3);
  2429. expect(chat_body.querySelector('.disconnect-msg:first-child').textContent.trim()).toBe(
  2430. 'You have been kicked from this groupchat');
  2431. expect(chat_body.querySelector('.disconnect-msg:nth-child(2)').textContent.trim()).toBe(
  2432. 'This action was done by Fluellen.');
  2433. expect(chat_body.querySelector('.disconnect-msg:nth-child(3)').textContent.trim()).toBe(
  2434. 'The reason given is: "Avaunt, you cullion!".');
  2435. expect(view.model.session.get('connection_status')).toBe(converse.ROOMSTATUS.DISCONNECTED);
  2436. done();
  2437. }));
  2438. it("informs users if they have exited the groupchat due to a technical reason",
  2439. mock.initConverse([], {}, async function (done, _converse) {
  2440. /* <presence
  2441. * from='harfleur@chat.shakespeare.lit/pistol'
  2442. * to='pistol@shakespeare.lit/harfleur'
  2443. * type='unavailable'>
  2444. * <x xmlns='http://jabber.org/protocol/muc#user'>
  2445. * <item affiliation='none' role='none'>
  2446. * <actor nick='Fluellen'/>
  2447. * <reason>Avaunt, you cullion!</reason>
  2448. * </item>
  2449. * <status code='110'/>
  2450. * <status code='307'/>
  2451. * </x>
  2452. * </presence>
  2453. */
  2454. await mock.openAndEnterChatRoom(_converse, 'lounge@montague.lit', 'romeo');
  2455. var presence = $pres().attrs({
  2456. from:'lounge@montague.lit/romeo',
  2457. to:'romeo@montague.lit/pda',
  2458. type:'unavailable'
  2459. })
  2460. .c('x').attrs({xmlns:'http://jabber.org/protocol/muc#user'})
  2461. .c('item').attrs({
  2462. affiliation: 'none',
  2463. jid: 'romeo@montague.lit/pda',
  2464. role: 'none'
  2465. })
  2466. .c('reason').t('Flux capacitor overload!').up()
  2467. .up()
  2468. .c('status').attrs({code:'110'}).up()
  2469. .c('status').attrs({code:'333'}).up()
  2470. .c('status').attrs({code:'307'}).nodeTree;
  2471. _converse.connection._dataRecv(mock.createRequest(presence));
  2472. const view = _converse.chatboxviews.get('lounge@montague.lit');
  2473. await u.waitUntil(() => !u.isVisible(view.querySelector('.chat-area')));
  2474. expect(u.isVisible(view.querySelector('.occupants'))).toBeFalsy();
  2475. const chat_body = view.querySelector('.chatroom-body');
  2476. expect(chat_body.querySelectorAll('.disconnect-msg').length).toBe(2);
  2477. expect(chat_body.querySelector('.disconnect-msg:first-child').textContent.trim()).toBe(
  2478. 'You have exited this groupchat due to a technical problem');
  2479. expect(chat_body.querySelector('.disconnect-msg:nth-child(2)').textContent.trim()).toBe(
  2480. 'The reason given is: "Flux capacitor overload!".');
  2481. done();
  2482. }));
  2483. it("can be saved to, and retrieved from, browserStorage",
  2484. mock.initConverse(['chatBoxesFetched'], {}, async function (done, _converse) {
  2485. await mock.openChatRoom(_converse, 'lounge', 'montague.lit', 'romeo');
  2486. // We instantiate a new ChatBoxes collection, which by default
  2487. // will be empty.
  2488. await mock.openControlBox(_converse);
  2489. const newchatboxes = new _converse.ChatBoxes();
  2490. expect(newchatboxes.length).toEqual(0);
  2491. // The chatboxes will then be fetched from browserStorage inside the
  2492. // onConnected method
  2493. newchatboxes.onConnected();
  2494. await new Promise(resolve => _converse.api.listen.once('chatBoxesFetched', resolve));
  2495. expect(newchatboxes.length).toEqual(2);
  2496. // Check that the chatrooms retrieved from browserStorage
  2497. // have the same attributes values as the original ones.
  2498. const attrs = ['id', 'box_id', 'visible'];
  2499. let new_attrs, old_attrs;
  2500. for (var i=0; i<attrs.length; i++) {
  2501. new_attrs = _.map(_.map(newchatboxes.models, 'attributes'), attrs[i]);
  2502. old_attrs = _.map(_.map(_converse.chatboxes.models, 'attributes'), attrs[i]);
  2503. // FIXME: should have have to sort here? Order must
  2504. // probably be the same...
  2505. // This should be fixed once the controlbox always opens
  2506. // only on the right.
  2507. expect(_.isEqual(new_attrs.sort(), old_attrs.sort())).toEqual(true);
  2508. }
  2509. done();
  2510. }));
  2511. it("can be closed again by clicking a DOM element with class 'close-chatbox-button'",
  2512. mock.initConverse(['chatBoxesFetched'], {}, async function (done, _converse) {
  2513. await mock.openChatRoom(_converse, 'lounge', 'montague.lit', 'romeo');
  2514. const view = _converse.chatboxviews.get('lounge@montague.lit');
  2515. spyOn(view, 'close').and.callThrough();
  2516. spyOn(_converse.api, "trigger").and.callThrough();
  2517. spyOn(view.model, 'leave');
  2518. view.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  2519. spyOn(_converse.api, 'confirm').and.callFake(() => Promise.resolve(true));
  2520. const button = await u.waitUntil(() => view.querySelector('.close-chatbox-button'));
  2521. button.click();
  2522. await u.waitUntil(() => view.close.calls.count());
  2523. expect(view.model.leave).toHaveBeenCalled();
  2524. await u.waitUntil(() => _converse.api.trigger.calls.count());
  2525. expect(_converse.api.trigger).toHaveBeenCalledWith('chatBoxClosed', jasmine.any(Object));
  2526. done();
  2527. }));
  2528. it("informs users of role and affiliation changes",
  2529. mock.initConverse([], {}, async function (done, _converse) {
  2530. const muc_jid = 'lounge@montague.lit';
  2531. await mock.openAndEnterChatRoom(_converse, muc_jid, 'romeo');
  2532. const view = _converse.api.chatviews.get(muc_jid);
  2533. let presence = $pres({
  2534. 'from': 'lounge@montague.lit/annoyingGuy',
  2535. 'id':'27C55F89-1C6A-459A-9EB5-77690145D624',
  2536. 'to': 'romeo@montague.lit/desktop'
  2537. })
  2538. .c('x', { 'xmlns': 'http://jabber.org/protocol/muc#user'})
  2539. .c('item', {
  2540. 'jid': 'annoyingguy@montague.lit',
  2541. 'affiliation': 'member',
  2542. 'role': 'participant'
  2543. });
  2544. _converse.connection._dataRecv(mock.createRequest(presence));
  2545. await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent.trim() ===
  2546. "romeo and annoyingGuy have entered the groupchat");
  2547. presence = $pres({
  2548. 'from': 'lounge@montague.lit/annoyingGuy',
  2549. 'to': 'romeo@montague.lit/desktop'
  2550. })
  2551. .c('x', { 'xmlns': 'http://jabber.org/protocol/muc#user'})
  2552. .c('item', {
  2553. 'jid': 'annoyingguy@montague.lit',
  2554. 'affiliation': 'member',
  2555. 'role': 'visitor'
  2556. });
  2557. _converse.connection._dataRecv(mock.createRequest(presence));
  2558. await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent.trim() ===
  2559. "romeo has entered the groupchat\nannoyingGuy has been muted");
  2560. presence = $pres({
  2561. 'from': 'lounge@montague.lit/annoyingGuy',
  2562. 'to': 'romeo@montague.lit/desktop'
  2563. })
  2564. .c('x', { 'xmlns': 'http://jabber.org/protocol/muc#user'})
  2565. .c('item', {
  2566. 'jid': 'annoyingguy@montague.lit',
  2567. 'affiliation': 'member',
  2568. 'role': 'participant'
  2569. });
  2570. _converse.connection._dataRecv(mock.createRequest(presence));
  2571. await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent.trim() ===
  2572. "romeo has entered the groupchat\nannoyingGuy has been given a voice");
  2573. // Check that we don't see an info message concerning the role,
  2574. // if the affiliation has changed.
  2575. presence = $pres({
  2576. 'from': 'lounge@montague.lit/annoyingGuy',
  2577. 'to': 'romeo@montague.lit/desktop'
  2578. })
  2579. .c('x', { 'xmlns': 'http://jabber.org/protocol/muc#user'})
  2580. .c('item', {
  2581. 'jid': 'annoyingguy@montague.lit',
  2582. 'affiliation': 'none',
  2583. 'role': 'visitor'
  2584. });
  2585. _converse.connection._dataRecv(mock.createRequest(presence));
  2586. await u.waitUntil(() =>
  2587. Array.from(view.querySelectorAll('.chat-info__message')).pop()?.textContent.trim() ===
  2588. "annoyingGuy is no longer a member of this groupchat"
  2589. );
  2590. expect(1).toBe(1);
  2591. done();
  2592. }));
  2593. it("notifies users of role and affiliation changes for members not currently in the groupchat",
  2594. mock.initConverse([], {}, async function (done, _converse) {
  2595. const muc_jid = 'lounge@montague.lit';
  2596. await mock.openAndEnterChatRoom(_converse, muc_jid, 'romeo');
  2597. const view = _converse.api.chatviews.get(muc_jid);
  2598. let message = $msg({
  2599. from: 'lounge@montague.lit',
  2600. id: '2CF9013B-E8A8-42A1-9633-85AD7CA12F40',
  2601. to: 'romeo@montague.lit'
  2602. })
  2603. .c('x', { 'xmlns': 'http://jabber.org/protocol/muc#user'})
  2604. .c('item', {
  2605. 'jid': 'absentguy@montague.lit',
  2606. 'affiliation': 'member',
  2607. 'role': 'none'
  2608. });
  2609. _converse.connection._dataRecv(mock.createRequest(message));
  2610. await u.waitUntil(() => view.model.occupants.length > 1);
  2611. expect(view.model.occupants.length).toBe(2);
  2612. expect(view.model.occupants.findWhere({'jid': 'absentguy@montague.lit'}).get('affiliation')).toBe('member');
  2613. message = $msg({
  2614. from: 'lounge@montague.lit',
  2615. id: '2CF9013B-E8A8-42A1-9633-85AD7CA12F41',
  2616. to: 'romeo@montague.lit'
  2617. })
  2618. .c('x', { 'xmlns': 'http://jabber.org/protocol/muc#user'})
  2619. .c('item', {
  2620. 'jid': 'absentguy@montague.lit',
  2621. 'affiliation': 'none',
  2622. 'role': 'none'
  2623. });
  2624. _converse.connection._dataRecv(mock.createRequest(message));
  2625. expect(view.model.occupants.length).toBe(2);
  2626. expect(view.model.occupants.findWhere({'jid': 'absentguy@montague.lit'}).get('affiliation')).toBe('none');
  2627. done();
  2628. }));
  2629. });
  2630. describe("Each chat groupchat can take special commands", function () {
  2631. it("takes /help to show the available commands",
  2632. mock.initConverse([], {}, async function (done, _converse) {
  2633. spyOn(window, 'confirm').and.callFake(() => true);
  2634. await mock.openAndEnterChatRoom(_converse, 'lounge@montague.lit', 'romeo');
  2635. const view = _converse.chatboxviews.get('lounge@montague.lit');
  2636. const textarea = await u.waitUntil(() => view.querySelector('.chat-textarea'));
  2637. const enter = { 'target': textarea, 'preventDefault': function preventDefault () {}, 'keyCode': 13 };
  2638. textarea.value = '/help';
  2639. const bottom_panel = view.querySelector('converse-muc-bottom-panel');
  2640. bottom_panel.onKeyDown(enter);
  2641. await u.waitUntil(() => sizzle('converse-chat-help .chat-info', view).length);
  2642. const chat_help_el = view.querySelector('converse-chat-help');
  2643. let info_messages = sizzle('.chat-info', chat_help_el);
  2644. expect(info_messages.length).toBe(19);
  2645. expect(info_messages.pop().textContent.trim()).toBe('/voice: Allow muted user to post messages');
  2646. expect(info_messages.pop().textContent.trim()).toBe('/topic: Set groupchat subject (alias for /subject)');
  2647. expect(info_messages.pop().textContent.trim()).toBe('/subject: Set groupchat subject');
  2648. expect(info_messages.pop().textContent.trim()).toBe('/revoke: Revoke the user\'s current affiliation');
  2649. expect(info_messages.pop().textContent.trim()).toBe('/register: Register your nickname');
  2650. expect(info_messages.pop().textContent.trim()).toBe('/owner: Grant ownership of this groupchat');
  2651. expect(info_messages.pop().textContent.trim()).toBe('/op: Grant moderator role to user');
  2652. expect(info_messages.pop().textContent.trim()).toBe('/nick: Change your nickname');
  2653. expect(info_messages.pop().textContent.trim()).toBe('/mute: Remove user\'s ability to post messages');
  2654. expect(info_messages.pop().textContent.trim()).toBe('/modtools: Opens up the moderator tools GUI');
  2655. expect(info_messages.pop().textContent.trim()).toBe('/member: Grant membership to a user');
  2656. expect(info_messages.pop().textContent.trim()).toBe('/me: Write in 3rd person');
  2657. expect(info_messages.pop().textContent.trim()).toBe('/kick: Kick user from groupchat');
  2658. expect(info_messages.pop().textContent.trim()).toBe('/help: Show this menu');
  2659. expect(info_messages.pop().textContent.trim()).toBe('/destroy: Remove this groupchat');
  2660. expect(info_messages.pop().textContent.trim()).toBe('/deop: Change user role to participant');
  2661. expect(info_messages.pop().textContent.trim()).toBe('/clear: Clear the chat area');
  2662. expect(info_messages.pop().textContent.trim()).toBe('/ban: Ban user by changing their affiliation to outcast');
  2663. expect(info_messages.pop().textContent.trim()).toBe('/admin: Change user\'s affiliation to admin');
  2664. const occupant = view.model.occupants.findWhere({'jid': _converse.bare_jid});
  2665. occupant.set('affiliation', 'admin');
  2666. view.querySelector('.close-chat-help').click();
  2667. await u.waitUntil(() => chat_help_el.hidden);
  2668. textarea.value = '/help';
  2669. bottom_panel.onKeyDown(enter);
  2670. await u.waitUntil(() => !chat_help_el.hidden);
  2671. info_messages = sizzle('.chat-info', chat_help_el);
  2672. expect(info_messages.length).toBe(18);
  2673. let commands = info_messages.map(m => m.textContent.replace(/:.*$/, ''));
  2674. expect(commands).toEqual([
  2675. "/admin", "/ban", "/clear", "/deop", "/destroy",
  2676. "/help", "/kick", "/me", "/member", "/modtools", "/mute", "/nick",
  2677. "/op", "/register", "/revoke", "/subject", "/topic", "/voice"
  2678. ]);
  2679. occupant.set('affiliation', 'member');
  2680. view.querySelector('.close-chat-help').click();
  2681. await u.waitUntil(() => chat_help_el.hidden);
  2682. textarea.value = '/help';
  2683. bottom_panel.onKeyDown(enter);
  2684. await u.waitUntil(() => !chat_help_el.hidden);
  2685. info_messages = sizzle('.chat-info', chat_help_el);
  2686. expect(info_messages.length).toBe(9);
  2687. commands = info_messages.map(m => m.textContent.replace(/:.*$/, ''));
  2688. expect(commands).toEqual(["/clear", "/help", "/kick", "/me", "/modtools", "/mute", "/nick", "/register", "/voice"]);
  2689. view.querySelector('.close-chat-help').click();
  2690. await u.waitUntil(() => chat_help_el.hidden);
  2691. expect(view.model.get('show_help_messages')).toBe(false);
  2692. occupant.set('role', 'participant');
  2693. // Role changes causes rerender, so we need to get the new textarea
  2694. textarea.value = '/help';
  2695. bottom_panel.onKeyDown(enter);
  2696. await u.waitUntil(() => view.model.get('show_help_messages'));
  2697. await u.waitUntil(() => !chat_help_el.hidden);
  2698. info_messages = sizzle('.chat-info', chat_help_el);
  2699. expect(info_messages.length).toBe(5);
  2700. commands = info_messages.map(m => m.textContent.replace(/:.*$/, ''));
  2701. expect(commands).toEqual(["/clear", "/help", "/me", "/nick", "/register"]);
  2702. // Test that /topic is available if all users may change the subject
  2703. // Note: we're making a shortcut here, this value should never be set manually
  2704. view.model.config.set('changesubject', true);
  2705. view.querySelector('.close-chat-help').click();
  2706. await u.waitUntil(() => chat_help_el.hidden);
  2707. textarea.value = '/help';
  2708. bottom_panel.onKeyDown(enter);
  2709. await u.waitUntil(() => !chat_help_el.hidden, 1000);
  2710. info_messages = sizzle('.chat-info', chat_help_el);
  2711. expect(info_messages.length).toBe(7);
  2712. commands = info_messages.map(m => m.textContent.replace(/:.*$/, ''));
  2713. expect(commands).toEqual(["/clear", "/help", "/me", "/nick", "/register", "/subject", "/topic"]);
  2714. done();
  2715. }));
  2716. it("takes /help to show the available commands and commands can be disabled by config",
  2717. mock.initConverse([], {muc_disable_slash_commands: ['mute', 'voice']}, async function (done, _converse) {
  2718. await mock.openAndEnterChatRoom(_converse, 'lounge@montague.lit', 'romeo');
  2719. const view = _converse.chatboxviews.get('lounge@montague.lit');
  2720. const textarea = await u.waitUntil(() => view.querySelector('.chat-textarea'));
  2721. const enter = { 'target': textarea, 'preventDefault': function () {}, 'keyCode': 13 };
  2722. spyOn(window, 'confirm').and.callFake(() => true);
  2723. textarea.value = '/clear';
  2724. const bottom_panel = view.querySelector('converse-muc-bottom-panel');
  2725. bottom_panel.onKeyDown(enter);
  2726. textarea.value = '/help';
  2727. bottom_panel.onKeyDown(enter);
  2728. await u.waitUntil(() => sizzle('.chat-info:not(.chat-event)', view).length);
  2729. const info_messages = sizzle('.chat-info:not(.chat-event)', view);
  2730. expect(info_messages.length).toBe(17);
  2731. expect(info_messages.pop().textContent.trim()).toBe('/topic: Set groupchat subject (alias for /subject)');
  2732. expect(info_messages.pop().textContent.trim()).toBe('/subject: Set groupchat subject');
  2733. expect(info_messages.pop().textContent.trim()).toBe('/revoke: Revoke the user\'s current affiliation');
  2734. expect(info_messages.pop().textContent.trim()).toBe('/register: Register your nickname');
  2735. expect(info_messages.pop().textContent.trim()).toBe('/owner: Grant ownership of this groupchat');
  2736. expect(info_messages.pop().textContent.trim()).toBe('/op: Grant moderator role to user');
  2737. expect(info_messages.pop().textContent.trim()).toBe('/nick: Change your nickname');
  2738. expect(info_messages.pop().textContent.trim()).toBe('/modtools: Opens up the moderator tools GUI');
  2739. expect(info_messages.pop().textContent.trim()).toBe('/member: Grant membership to a user');
  2740. expect(info_messages.pop().textContent.trim()).toBe('/me: Write in 3rd person');
  2741. expect(info_messages.pop().textContent.trim()).toBe('/kick: Kick user from groupchat');
  2742. expect(info_messages.pop().textContent.trim()).toBe('/help: Show this menu');
  2743. expect(info_messages.pop().textContent.trim()).toBe('/destroy: Remove this groupchat');
  2744. expect(info_messages.pop().textContent.trim()).toBe('/deop: Change user role to participant');
  2745. expect(info_messages.pop().textContent.trim()).toBe('/clear: Clear the chat area');
  2746. expect(info_messages.pop().textContent.trim()).toBe('/ban: Ban user by changing their affiliation to outcast');
  2747. expect(info_messages.pop().textContent.trim()).toBe('/admin: Change user\'s affiliation to admin');
  2748. done();
  2749. }));
  2750. it("takes /member to make an occupant a member",
  2751. mock.initConverse([], {}, async function (done, _converse) {
  2752. let iq_stanza;
  2753. await mock.openAndEnterChatRoom(_converse, 'lounge@muc.montague.lit', 'romeo');
  2754. const view = _converse.chatboxviews.get('lounge@muc.montague.lit');
  2755. /* We don't show join/leave messages for existing occupants. We
  2756. * know about them because we receive their presences before we
  2757. * receive our own.
  2758. */
  2759. const presence = $pres({
  2760. to: 'romeo@montague.lit/orchard',
  2761. from: 'lounge@muc.montague.lit/marc'
  2762. }).c('x', {xmlns: Strophe.NS.MUC_USER})
  2763. .c('item', {
  2764. 'affiliation': 'none',
  2765. 'jid': 'marc@montague.lit/_converse.js-290929789',
  2766. 'role': 'participant'
  2767. });
  2768. _converse.connection._dataRecv(mock.createRequest(presence));
  2769. expect(view.model.occupants.length).toBe(2);
  2770. const textarea = await u.waitUntil(() => view.querySelector('.chat-textarea'));
  2771. let sent_stanza;
  2772. spyOn(_converse.connection, 'send').and.callFake((stanza) => {
  2773. sent_stanza = stanza;
  2774. });
  2775. // First check that an error message appears when a
  2776. // non-existent nick is used.
  2777. textarea.value = '/member chris Welcome to the club!';
  2778. const bottom_panel = view.querySelector('converse-muc-bottom-panel');
  2779. bottom_panel.onKeyDown({
  2780. target: textarea,
  2781. preventDefault: function preventDefault () {},
  2782. keyCode: 13
  2783. });
  2784. expect(_converse.connection.send).not.toHaveBeenCalled();
  2785. await u.waitUntil(() => view.querySelectorAll('.chat-error').length);
  2786. expect(view.querySelector('.chat-error').textContent.trim())
  2787. .toBe('Error: couldn\'t find a groupchat participant based on your arguments');
  2788. // Now test with an existing nick
  2789. textarea.value = '/member marc Welcome to the club!';
  2790. bottom_panel.onKeyDown({
  2791. target: textarea,
  2792. preventDefault: function preventDefault () {},
  2793. keyCode: 13
  2794. });
  2795. expect(_converse.connection.send).toHaveBeenCalled();
  2796. expect(Strophe.serialize(sent_stanza)).toBe(
  2797. `<iq id="${sent_stanza.getAttribute('id')}" to="lounge@muc.montague.lit" type="set" xmlns="jabber:client">`+
  2798. `<query xmlns="http://jabber.org/protocol/muc#admin">`+
  2799. `<item affiliation="member" jid="marc@montague.lit">`+
  2800. `<reason>Welcome to the club!</reason>`+
  2801. `</item>`+
  2802. `</query>`+
  2803. `</iq>`);
  2804. let result = $iq({
  2805. "xmlns": "jabber:client",
  2806. "type": "result",
  2807. "to": "romeo@montague.lit/orchard",
  2808. "from": "lounge@muc.montague.lit",
  2809. "id": sent_stanza.getAttribute('id')
  2810. });
  2811. _converse.connection.IQ_stanzas = [];
  2812. _converse.connection._dataRecv(mock.createRequest(result));
  2813. iq_stanza = await u.waitUntil(() => _.filter(
  2814. _converse.connection.IQ_stanzas,
  2815. iq => iq.querySelector('iq[to="lounge@muc.montague.lit"][type="get"] item[affiliation="member"]')).pop()
  2816. );
  2817. expect(Strophe.serialize(iq_stanza)).toBe(
  2818. `<iq id="${iq_stanza.getAttribute('id')}" to="lounge@muc.montague.lit" type="get" xmlns="jabber:client">`+
  2819. `<query xmlns="http://jabber.org/protocol/muc#admin">`+
  2820. `<item affiliation="member"/>`+
  2821. `</query>`+
  2822. `</iq>`)
  2823. expect(view.model.occupants.length).toBe(2);
  2824. result = $iq({
  2825. "xmlns": "jabber:client",
  2826. "type": "result",
  2827. "to": "romeo@montague.lit/orchard",
  2828. "from": "lounge@muc.montague.lit",
  2829. "id": iq_stanza.getAttribute("id")
  2830. }).c("query", {"xmlns": "http://jabber.org/protocol/muc#admin"})
  2831. .c("item", {"jid": "marc", "affiliation": "member"});
  2832. _converse.connection._dataRecv(mock.createRequest(result));
  2833. expect(view.model.occupants.length).toBe(2);
  2834. iq_stanza = await u.waitUntil(() => _.filter(
  2835. _converse.connection.IQ_stanzas,
  2836. iq => iq.querySelector('iq[to="lounge@muc.montague.lit"][type="get"] item[affiliation="owner"]')).pop()
  2837. );
  2838. expect(Strophe.serialize(iq_stanza)).toBe(
  2839. `<iq id="${iq_stanza.getAttribute('id')}" to="lounge@muc.montague.lit" type="get" xmlns="jabber:client">`+
  2840. `<query xmlns="http://jabber.org/protocol/muc#admin">`+
  2841. `<item affiliation="owner"/>`+
  2842. `</query>`+
  2843. `</iq>`)
  2844. expect(view.model.occupants.length).toBe(2);
  2845. result = $iq({
  2846. "xmlns": "jabber:client",
  2847. "type": "result",
  2848. "to": "romeo@montague.lit/orchard",
  2849. "from": "lounge@muc.montague.lit",
  2850. "id": iq_stanza.getAttribute("id")
  2851. }).c("query", {"xmlns": "http://jabber.org/protocol/muc#admin"})
  2852. .c("item", {"jid": "romeo@montague.lit", "affiliation": "owner"});
  2853. _converse.connection._dataRecv(mock.createRequest(result));
  2854. expect(view.model.occupants.length).toBe(2);
  2855. iq_stanza = await u.waitUntil(() => _.filter(
  2856. _converse.connection.IQ_stanzas,
  2857. iq => iq.querySelector('iq[to="lounge@muc.montague.lit"][type="get"] item[affiliation="admin"]')).pop()
  2858. );
  2859. expect(Strophe.serialize(iq_stanza)).toBe(
  2860. `<iq id="${iq_stanza.getAttribute('id')}" to="lounge@muc.montague.lit" type="get" xmlns="jabber:client">`+
  2861. `<query xmlns="http://jabber.org/protocol/muc#admin">`+
  2862. `<item affiliation="admin"/>`+
  2863. `</query>`+
  2864. `</iq>`)
  2865. expect(view.model.occupants.length).toBe(2);
  2866. result = $iq({
  2867. "xmlns": "jabber:client",
  2868. "type": "result",
  2869. "to": "romeo@montague.lit/orchard",
  2870. "from": "lounge@muc.montague.lit",
  2871. "id": iq_stanza.getAttribute("id")
  2872. }).c("query", {"xmlns": "http://jabber.org/protocol/muc#admin"})
  2873. _converse.connection._dataRecv(mock.createRequest(result));
  2874. await u.waitUntil(() => view.querySelectorAll('.occupant').length, 500);
  2875. await u.waitUntil(() => view.querySelectorAll('.badge').length > 1);
  2876. expect(view.model.occupants.length).toBe(2);
  2877. expect(view.querySelectorAll('.occupant').length).toBe(2);
  2878. done();
  2879. }));
  2880. it("takes /topic to set the groupchat topic", mock.initConverse([], {}, async function (done, _converse) {
  2881. await mock.openAndEnterChatRoom(_converse, 'lounge@montague.lit', 'romeo');
  2882. const view = _converse.chatboxviews.get('lounge@montague.lit');
  2883. let sent_stanza;
  2884. spyOn(_converse.connection, 'send').and.callFake(function (stanza) {
  2885. sent_stanza = stanza;
  2886. });
  2887. // Check the alias /topic
  2888. const textarea = await u.waitUntil(() => view.querySelector('.chat-textarea'));
  2889. textarea.value = '/topic This is the groupchat subject';
  2890. const bottom_panel = view.querySelector('converse-muc-bottom-panel');
  2891. bottom_panel.onKeyDown({
  2892. target: textarea,
  2893. preventDefault: function preventDefault () {},
  2894. keyCode: 13
  2895. });
  2896. expect(_converse.connection.send).toHaveBeenCalled();
  2897. expect(sent_stanza.textContent.trim()).toBe('This is the groupchat subject');
  2898. // Check /subject
  2899. textarea.value = '/subject This is a new subject';
  2900. bottom_panel.onKeyDown({
  2901. target: textarea,
  2902. preventDefault: function preventDefault () {},
  2903. keyCode: 13
  2904. });
  2905. expect(sent_stanza.textContent.trim()).toBe('This is a new subject');
  2906. expect(Strophe.serialize(sent_stanza).toLocaleString()).toBe(
  2907. '<message from="romeo@montague.lit/orchard" to="lounge@montague.lit" type="groupchat" xmlns="jabber:client">'+
  2908. '<subject xmlns="jabber:client">This is a new subject</subject>'+
  2909. '</message>');
  2910. // Check case insensitivity
  2911. textarea.value = '/Subject This is yet another subject';
  2912. bottom_panel.onKeyDown({
  2913. target: textarea,
  2914. preventDefault: function preventDefault () {},
  2915. keyCode: 13
  2916. });
  2917. expect(sent_stanza.textContent.trim()).toBe('This is yet another subject');
  2918. expect(Strophe.serialize(sent_stanza).toLocaleString()).toBe(
  2919. '<message from="romeo@montague.lit/orchard" to="lounge@montague.lit" type="groupchat" xmlns="jabber:client">'+
  2920. '<subject xmlns="jabber:client">This is yet another subject</subject>'+
  2921. '</message>');
  2922. // Check unsetting the topic
  2923. textarea.value = '/topic';
  2924. bottom_panel.onKeyDown({
  2925. target: textarea,
  2926. preventDefault: function preventDefault () {},
  2927. keyCode: 13
  2928. });
  2929. expect(Strophe.serialize(sent_stanza).toLocaleString()).toBe(
  2930. '<message from="romeo@montague.lit/orchard" to="lounge@montague.lit" type="groupchat" xmlns="jabber:client">'+
  2931. '<subject xmlns="jabber:client"></subject>'+
  2932. '</message>');
  2933. done();
  2934. }));
  2935. it("takes /clear to clear messages", mock.initConverse([], {}, async function (done, _converse) {
  2936. await mock.openAndEnterChatRoom(_converse, 'lounge@montague.lit', 'romeo');
  2937. const view = _converse.chatboxviews.get('lounge@montague.lit');
  2938. const textarea = await u.waitUntil(() => view.querySelector('.chat-textarea'));
  2939. textarea.value = '/clear';
  2940. const bottom_panel = view.querySelector('converse-muc-bottom-panel');
  2941. spyOn(bottom_panel, 'clearMessages');
  2942. bottom_panel.onKeyDown({
  2943. target: textarea,
  2944. preventDefault: function preventDefault () {},
  2945. keyCode: 13
  2946. });
  2947. expect(bottom_panel.clearMessages).toHaveBeenCalled();
  2948. done();
  2949. }));
  2950. it("takes /owner to make a user an owner", mock.initConverse([], {}, async function (done, _converse) {
  2951. let sent_IQ, IQ_id;
  2952. const sendIQ = _converse.connection.sendIQ;
  2953. spyOn(_converse.connection, 'sendIQ').and.callFake(function (iq, callback, errback) {
  2954. sent_IQ = iq;
  2955. IQ_id = sendIQ.bind(this)(iq, callback, errback);
  2956. });
  2957. await mock.openAndEnterChatRoom(_converse, 'lounge@montague.lit', 'romeo');
  2958. const view = _converse.chatboxviews.get('lounge@montague.lit');
  2959. spyOn(view.model, 'setAffiliation').and.callThrough();
  2960. spyOn(view.model, 'validateRoleOrAffiliationChangeArgs').and.callThrough();
  2961. let presence = $pres({
  2962. 'from': 'lounge@montague.lit/annoyingGuy',
  2963. 'id':'27C55F89-1C6A-459A-9EB5-77690145D624',
  2964. 'to': 'romeo@montague.lit/desktop'
  2965. })
  2966. .c('x', { 'xmlns': 'http://jabber.org/protocol/muc#user'})
  2967. .c('item', {
  2968. 'jid': 'annoyingguy@montague.lit',
  2969. 'affiliation': 'member',
  2970. 'role': 'participant'
  2971. });
  2972. _converse.connection._dataRecv(mock.createRequest(presence));
  2973. const textarea = await u.waitUntil(() => view.querySelector('.chat-textarea'));
  2974. textarea.value = '/owner';
  2975. const bottom_panel = view.querySelector('converse-muc-bottom-panel');
  2976. bottom_panel.onKeyDown({
  2977. target: textarea,
  2978. preventDefault: function preventDefault () {},
  2979. keyCode: 13
  2980. });
  2981. expect(view.model.validateRoleOrAffiliationChangeArgs).toHaveBeenCalled();
  2982. const err_msg = await u.waitUntil(() => view.querySelector('.chat-error'));
  2983. expect(err_msg.textContent.trim()).toBe(
  2984. "Error: the \"owner\" command takes two arguments, the user's nickname and optionally a reason.");
  2985. expect(view.model.setAffiliation).not.toHaveBeenCalled();
  2986. // XXX: Calling onFormSubmitted directly, trying
  2987. // again via triggering Event doesn't work for some weird
  2988. // reason.
  2989. textarea.value = '/owner nobody You\'re responsible';
  2990. bottom_panel.onFormSubmitted(new Event('submit'));
  2991. await u.waitUntil(() => view.querySelectorAll('.chat-error').length === 2);
  2992. expect(Array.from(view.querySelectorAll('.chat-error')).pop().textContent.trim()).toBe(
  2993. "Error: couldn't find a groupchat participant based on your arguments");
  2994. expect(view.model.setAffiliation).not.toHaveBeenCalled();
  2995. // Call now with the correct of arguments.
  2996. // XXX: Calling onFormSubmitted directly, trying
  2997. // again via triggering Event doesn't work for some weird
  2998. // reason.
  2999. textarea.value = '/owner annoyingGuy You\'re responsible';
  3000. bottom_panel.onFormSubmitted(new Event('submit'));
  3001. expect(view.model.validateRoleOrAffiliationChangeArgs.calls.count()).toBe(3);
  3002. expect(view.model.setAffiliation).toHaveBeenCalled();
  3003. // Check that the member list now gets updated
  3004. expect(Strophe.serialize(sent_IQ)).toBe(
  3005. `<iq id="${IQ_id}" to="lounge@montague.lit" type="set" xmlns="jabber:client">`+
  3006. `<query xmlns="http://jabber.org/protocol/muc#admin">`+
  3007. `<item affiliation="owner" jid="annoyingguy@montague.lit">`+
  3008. `<reason>You&apos;re responsible</reason>`+
  3009. `</item>`+
  3010. `</query>`+
  3011. `</iq>`);
  3012. presence = $pres({
  3013. 'from': 'lounge@montague.lit/annoyingGuy',
  3014. 'id':'27C55F89-1C6A-459A-9EB5-77690145D628',
  3015. 'to': 'romeo@montague.lit/desktop'
  3016. })
  3017. .c('x', { 'xmlns': 'http://jabber.org/protocol/muc#user'})
  3018. .c('item', {
  3019. 'jid': 'annoyingguy@montague.lit',
  3020. 'affiliation': 'owner',
  3021. 'role': 'participant'
  3022. });
  3023. _converse.connection._dataRecv(mock.createRequest(presence));
  3024. await u.waitUntil(() =>
  3025. Array.from(view.querySelectorAll('.chat-info__message')).pop()?.textContent.trim() ===
  3026. "annoyingGuy is now an owner of this groupchat"
  3027. );
  3028. done();
  3029. }));
  3030. it("takes /ban to ban a user", mock.initConverse([], {}, async function (done, _converse) {
  3031. let sent_IQ, IQ_id;
  3032. const sendIQ = _converse.connection.sendIQ;
  3033. spyOn(_converse.connection, 'sendIQ').and.callFake(function (iq, callback, errback) {
  3034. sent_IQ = iq;
  3035. IQ_id = sendIQ.bind(this)(iq, callback, errback);
  3036. });
  3037. await mock.openAndEnterChatRoom(_converse, 'lounge@montague.lit', 'romeo');
  3038. const view = _converse.chatboxviews.get('lounge@montague.lit');
  3039. spyOn(view.model, 'setAffiliation').and.callThrough();
  3040. spyOn(view.model, 'validateRoleOrAffiliationChangeArgs').and.callThrough();
  3041. let presence = $pres({
  3042. 'from': 'lounge@montague.lit/annoyingGuy',
  3043. 'id':'27C55F89-1C6A-459A-9EB5-77690145D624',
  3044. 'to': 'romeo@montague.lit/desktop'
  3045. })
  3046. .c('x', { 'xmlns': 'http://jabber.org/protocol/muc#user'})
  3047. .c('item', {
  3048. 'jid': 'annoyingguy@montague.lit',
  3049. 'affiliation': 'member',
  3050. 'role': 'participant'
  3051. });
  3052. _converse.connection._dataRecv(mock.createRequest(presence));
  3053. const textarea = await u.waitUntil(() => view.querySelector('.chat-textarea'));
  3054. textarea.value = '/ban';
  3055. const bottom_panel = view.querySelector('converse-muc-bottom-panel');
  3056. bottom_panel.onKeyDown({
  3057. target: textarea,
  3058. preventDefault: function preventDefault () {},
  3059. keyCode: 13
  3060. });
  3061. expect(view.model.validateRoleOrAffiliationChangeArgs).toHaveBeenCalled();
  3062. await u.waitUntil(() => view.querySelector('.message:last-child')?.textContent?.trim() ===
  3063. "Error: the \"ban\" command takes two arguments, the user's nickname and optionally a reason.");
  3064. expect(view.model.setAffiliation).not.toHaveBeenCalled();
  3065. // Call now with the correct amount of arguments.
  3066. // XXX: Calling onFormSubmitted directly, trying
  3067. // again via triggering Event doesn't work for some weird
  3068. // reason.
  3069. textarea.value = '/ban annoyingGuy You\'re annoying';
  3070. bottom_panel.onFormSubmitted(new Event('submit'));
  3071. expect(view.model.validateRoleOrAffiliationChangeArgs.calls.count()).toBe(2);
  3072. expect(view.model.setAffiliation).toHaveBeenCalled();
  3073. // Check that the member list now gets updated
  3074. expect(Strophe.serialize(sent_IQ)).toBe(
  3075. `<iq id="${IQ_id}" to="lounge@montague.lit" type="set" xmlns="jabber:client">`+
  3076. `<query xmlns="http://jabber.org/protocol/muc#admin">`+
  3077. `<item affiliation="outcast" jid="annoyingguy@montague.lit">`+
  3078. `<reason>You&apos;re annoying</reason>`+
  3079. `</item>`+
  3080. `</query>`+
  3081. `</iq>`);
  3082. presence = $pres({
  3083. 'from': 'lounge@montague.lit/annoyingGuy',
  3084. 'id':'27C55F89-1C6A-459A-9EB5-77690145D628',
  3085. 'to': 'romeo@montague.lit/desktop'
  3086. }).c('x', { 'xmlns': 'http://jabber.org/protocol/muc#user'})
  3087. .c('item', {
  3088. 'jid': 'annoyingguy@montague.lit',
  3089. 'affiliation': 'outcast',
  3090. 'role': 'participant'
  3091. }).c('actor', {'nick': 'romeo'}).up()
  3092. .c('reason').t("You're annoying").up().up()
  3093. .c('status', {'code': '301'});
  3094. _converse.connection._dataRecv(mock.createRequest(presence));
  3095. await u.waitUntil(() => view.querySelectorAll('.chat-info').length === 2);
  3096. expect(view.querySelectorAll('.chat-info__message')[1].textContent.trim()).toBe("annoyingGuy has been banned by romeo");
  3097. expect(view.querySelector('.chat-info:last-child q').textContent.trim()).toBe("You're annoying");
  3098. presence = $pres({
  3099. 'from': 'lounge@montague.lit/joe2',
  3100. 'id':'27C55F89-1C6A-459A-9EB5-77690145D624',
  3101. 'to': 'romeo@montague.lit/desktop'
  3102. })
  3103. .c('x', { 'xmlns': 'http://jabber.org/protocol/muc#user'})
  3104. .c('item', {
  3105. 'jid': 'joe2@montague.lit',
  3106. 'affiliation': 'member',
  3107. 'role': 'participant'
  3108. });
  3109. _converse.connection._dataRecv(mock.createRequest(presence));
  3110. textarea.value = '/ban joe22';
  3111. bottom_panel.onFormSubmitted(new Event('submit'));
  3112. await u.waitUntil(() => view.querySelector('converse-chat-message:last-child')?.textContent?.trim() ===
  3113. "Error: couldn't find a groupchat participant based on your arguments");
  3114. done();
  3115. }));
  3116. it("takes a /kick command to kick a user", mock.initConverse([], {}, async function (done, _converse) {
  3117. let sent_IQ, IQ_id;
  3118. const sendIQ = _converse.connection.sendIQ;
  3119. spyOn(_converse.connection, 'sendIQ').and.callFake(function (iq, callback, errback) {
  3120. sent_IQ = iq;
  3121. IQ_id = sendIQ.bind(this)(iq, callback, errback);
  3122. });
  3123. const muc_jid = 'lounge@montague.lit';
  3124. await mock.openAndEnterChatRoom(_converse, muc_jid, 'romeo');
  3125. const view = _converse.api.chatviews.get(muc_jid);
  3126. spyOn(view.model, 'setRole').and.callThrough();
  3127. spyOn(view.model, 'validateRoleOrAffiliationChangeArgs').and.callThrough();
  3128. let presence = $pres({
  3129. 'from': 'lounge@montague.lit/annoying guy',
  3130. 'id':'27C55F89-1C6A-459A-9EB5-77690145D624',
  3131. 'to': 'romeo@montague.lit/desktop'
  3132. })
  3133. .c('x', { 'xmlns': 'http://jabber.org/protocol/muc#user'})
  3134. .c('item', {
  3135. 'jid': 'annoyingguy@montague.lit',
  3136. 'affiliation': 'none',
  3137. 'role': 'participant'
  3138. });
  3139. _converse.connection._dataRecv(mock.createRequest(presence));
  3140. const textarea = await u.waitUntil(() => view.querySelector('.chat-textarea'));
  3141. textarea.value = '/kick';
  3142. const bottom_panel = view.querySelector('converse-muc-bottom-panel');
  3143. bottom_panel.onKeyDown({
  3144. target: textarea,
  3145. preventDefault: function preventDefault () {},
  3146. keyCode: 13
  3147. });
  3148. expect(view.model.validateRoleOrAffiliationChangeArgs).toHaveBeenCalled();
  3149. await u.waitUntil(() => view.querySelector('.message:last-child')?.textContent?.trim() ===
  3150. "Error: the \"kick\" command takes two arguments, the user's nickname and optionally a reason.");
  3151. expect(view.model.setRole).not.toHaveBeenCalled();
  3152. // Call now with the correct amount of arguments.
  3153. // XXX: Calling onFormSubmitted directly, trying
  3154. // again via triggering Event doesn't work for some weird
  3155. // reason.
  3156. textarea.value = '/kick @annoying guy You\'re annoying';
  3157. bottom_panel.onFormSubmitted(new Event('submit'));
  3158. expect(view.model.validateRoleOrAffiliationChangeArgs.calls.count()).toBe(2);
  3159. expect(view.model.setRole).toHaveBeenCalled();
  3160. expect(Strophe.serialize(sent_IQ)).toBe(
  3161. `<iq id="${IQ_id}" to="lounge@montague.lit" type="set" xmlns="jabber:client">`+
  3162. `<query xmlns="http://jabber.org/protocol/muc#admin">`+
  3163. `<item nick="annoying guy" role="none">`+
  3164. `<reason>You&apos;re annoying</reason>`+
  3165. `</item>`+
  3166. `</query>`+
  3167. `</iq>`);
  3168. /* <presence
  3169. * from='harfleur@chat.shakespeare.lit/pistol'
  3170. * to='gower@shakespeare.lit/cell'
  3171. * type='unavailable'>
  3172. * <x xmlns='http://jabber.org/protocol/muc#user'>
  3173. * <item affiliation='none' role='none'/>
  3174. * <status code='307'/>
  3175. * </x>
  3176. * </presence>
  3177. */
  3178. presence = $pres({
  3179. 'from': 'lounge@montague.lit/annoying guy',
  3180. 'to': 'romeo@montague.lit/desktop',
  3181. 'type': 'unavailable'
  3182. })
  3183. .c('x', { 'xmlns': 'http://jabber.org/protocol/muc#user'})
  3184. .c('item', {
  3185. 'affiliation': 'none',
  3186. 'role': 'none'
  3187. }).c('actor', {'nick': 'romeo'}).up()
  3188. .c('reason').t("You're annoying").up().up()
  3189. .c('status', {'code': '307'});
  3190. _converse.connection._dataRecv(mock.createRequest(presence));
  3191. await u.waitUntil(() => view.querySelectorAll('.chat-info').length === 2);
  3192. expect(view.querySelectorAll('.chat-info__message')[1].textContent.trim()).toBe("annoying guy has been kicked out by romeo");
  3193. expect(view.querySelector('.chat-info:last-child q').textContent.trim()).toBe("You're annoying");
  3194. done();
  3195. }));
  3196. it("takes /op and /deop to make a user a moderator or not",
  3197. mock.initConverse([], {}, async function (done, _converse) {
  3198. const muc_jid = 'lounge@montague.lit';
  3199. await mock.openAndEnterChatRoom(_converse, muc_jid, 'romeo');
  3200. const view = _converse.api.chatviews.get(muc_jid);
  3201. let sent_IQ, IQ_id;
  3202. const sendIQ = _converse.connection.sendIQ;
  3203. spyOn(_converse.connection, 'sendIQ').and.callFake(function (iq, callback, errback) {
  3204. sent_IQ = iq;
  3205. IQ_id = sendIQ.bind(this)(iq, callback, errback);
  3206. });
  3207. spyOn(view.model, 'setRole').and.callThrough();
  3208. spyOn(view.model, 'validateRoleOrAffiliationChangeArgs').and.callThrough();
  3209. // New user enters the groupchat
  3210. /* <presence
  3211. * from='coven@chat.shakespeare.lit/thirdwitch'
  3212. * id='27C55F89-1C6A-459A-9EB5-77690145D624'
  3213. * to='crone1@shakespeare.lit/desktop'>
  3214. * <x xmlns='http://jabber.org/protocol/muc#user'>
  3215. * <item affiliation='member' role='moderator'/>
  3216. * </x>
  3217. * </presence>
  3218. */
  3219. let presence = $pres({
  3220. 'from': 'lounge@montague.lit/trustworthyguy',
  3221. 'id':'27C55F89-1C6A-459A-9EB5-77690145D624',
  3222. 'to': 'romeo@montague.lit/desktop'
  3223. })
  3224. .c('x', { 'xmlns': 'http://jabber.org/protocol/muc#user'})
  3225. .c('item', {
  3226. 'jid': 'trustworthyguy@montague.lit',
  3227. 'affiliation': 'member',
  3228. 'role': 'participant'
  3229. });
  3230. _converse.connection._dataRecv(mock.createRequest(presence));
  3231. await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent.trim() ===
  3232. "romeo and trustworthyguy have entered the groupchat");
  3233. const textarea = await u.waitUntil(() => view.querySelector('.chat-textarea'));
  3234. textarea.value = '/op';
  3235. const bottom_panel = view.querySelector('converse-muc-bottom-panel');
  3236. bottom_panel.onKeyDown({
  3237. target: textarea,
  3238. preventDefault: function preventDefault () {},
  3239. keyCode: 13
  3240. });
  3241. expect(view.model.validateRoleOrAffiliationChangeArgs).toHaveBeenCalled();
  3242. await u.waitUntil(() => view.querySelector('.message:last-child')?.textContent?.trim() ===
  3243. "Error: the \"op\" command takes two arguments, the user's nickname and optionally a reason.");
  3244. expect(view.model.setRole).not.toHaveBeenCalled();
  3245. // Call now with the correct amount of arguments.
  3246. // XXX: Calling onFormSubmitted directly, trying
  3247. // again via triggering Event doesn't work for some weird
  3248. // reason.
  3249. textarea.value = '/op trustworthyguy You\'re trustworthy';
  3250. bottom_panel.onFormSubmitted(new Event('submit'));
  3251. expect(view.model.validateRoleOrAffiliationChangeArgs.calls.count()).toBe(2);
  3252. expect(view.model.setRole).toHaveBeenCalled();
  3253. expect(Strophe.serialize(sent_IQ)).toBe(
  3254. `<iq id="${IQ_id}" to="lounge@montague.lit" type="set" xmlns="jabber:client">`+
  3255. `<query xmlns="http://jabber.org/protocol/muc#admin">`+
  3256. `<item nick="trustworthyguy" role="moderator">`+
  3257. `<reason>You&apos;re trustworthy</reason>`+
  3258. `</item>`+
  3259. `</query>`+
  3260. `</iq>`);
  3261. /* <presence
  3262. * from='coven@chat.shakespeare.lit/thirdwitch'
  3263. * to='crone1@shakespeare.lit/desktop'>
  3264. * <x xmlns='http://jabber.org/protocol/muc#user'>
  3265. * <item affiliation='member'
  3266. * jid='hag66@shakespeare.lit/pda'
  3267. * role='moderator'/>
  3268. * </x>
  3269. * </presence>
  3270. */
  3271. presence = $pres({
  3272. 'from': 'lounge@montague.lit/trustworthyguy',
  3273. 'to': 'romeo@montague.lit/desktop'
  3274. })
  3275. .c('x', { 'xmlns': 'http://jabber.org/protocol/muc#user'})
  3276. .c('item', {
  3277. 'jid': 'trustworthyguy@montague.lit',
  3278. 'affiliation': 'member',
  3279. 'role': 'moderator'
  3280. });
  3281. _converse.connection._dataRecv(mock.createRequest(presence));
  3282. // Check now that things get restored when the user is given a voice
  3283. await u.waitUntil(
  3284. () => view.querySelector('.chat-content__notifications').textContent.split('\n', 2).pop()?.trim() ===
  3285. "trustworthyguy is now a moderator");
  3286. // Call now with the correct amount of arguments.
  3287. // XXX: Calling onFormSubmitted directly, trying
  3288. // again via triggering Event doesn't work for some weird
  3289. // reason.
  3290. textarea.value = '/deop trustworthyguy Perhaps not';
  3291. bottom_panel.onFormSubmitted(new Event('submit'));
  3292. expect(view.model.validateRoleOrAffiliationChangeArgs.calls.count()).toBe(3);
  3293. expect(view.model.setRole).toHaveBeenCalled();
  3294. expect(Strophe.serialize(sent_IQ)).toBe(
  3295. `<iq id="${IQ_id}" to="lounge@montague.lit" type="set" xmlns="jabber:client">`+
  3296. `<query xmlns="http://jabber.org/protocol/muc#admin">`+
  3297. `<item nick="trustworthyguy" role="participant">`+
  3298. `<reason>Perhaps not</reason>`+
  3299. `</item>`+
  3300. `</query>`+
  3301. `</iq>`);
  3302. /* <presence
  3303. * from='coven@chat.shakespeare.lit/thirdwitch'
  3304. * to='crone1@shakespeare.lit/desktop'>
  3305. * <x xmlns='http://jabber.org/protocol/muc#user'>
  3306. * <item affiliation='member'
  3307. * jid='hag66@shakespeare.lit/pda'
  3308. * role='participant'/>
  3309. * </x>
  3310. * </presence>
  3311. */
  3312. presence = $pres({
  3313. 'from': 'lounge@montague.lit/trustworthyguy',
  3314. 'to': 'romeo@montague.lit/desktop'
  3315. }).c('x', { 'xmlns': 'http://jabber.org/protocol/muc#user'})
  3316. .c('item', {
  3317. 'jid': 'trustworthyguy@montague.lit',
  3318. 'affiliation': 'member',
  3319. 'role': 'participant'
  3320. });
  3321. _converse.connection._dataRecv(mock.createRequest(presence));
  3322. await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent.includes("trustworthyguy is no longer a moderator"));
  3323. done();
  3324. }));
  3325. it("takes /mute and /voice to mute and unmute a user",
  3326. mock.initConverse([], {}, async function (done, _converse) {
  3327. const muc_jid = 'lounge@montague.lit';
  3328. await mock.openAndEnterChatRoom(_converse, muc_jid, 'romeo');
  3329. const view = _converse.api.chatviews.get(muc_jid);
  3330. var sent_IQ, IQ_id;
  3331. var sendIQ = _converse.connection.sendIQ;
  3332. spyOn(_converse.connection, 'sendIQ').and.callFake(function (iq, callback, errback) {
  3333. sent_IQ = iq;
  3334. IQ_id = sendIQ.bind(this)(iq, callback, errback);
  3335. });
  3336. spyOn(view.model, 'setRole').and.callThrough();
  3337. spyOn(view.model, 'validateRoleOrAffiliationChangeArgs').and.callThrough();
  3338. // New user enters the groupchat
  3339. /* <presence
  3340. * from='coven@chat.shakespeare.lit/thirdwitch'
  3341. * id='27C55F89-1C6A-459A-9EB5-77690145D624'
  3342. * to='crone1@shakespeare.lit/desktop'>
  3343. * <x xmlns='http://jabber.org/protocol/muc#user'>
  3344. * <item affiliation='member' role='participant'/>
  3345. * </x>
  3346. * </presence>
  3347. */
  3348. let presence = $pres({
  3349. 'from': 'lounge@montague.lit/annoyingGuy',
  3350. 'id':'27C55F89-1C6A-459A-9EB5-77690145D624',
  3351. 'to': 'romeo@montague.lit/desktop'
  3352. })
  3353. .c('x', { 'xmlns': 'http://jabber.org/protocol/muc#user'})
  3354. .c('item', {
  3355. 'jid': 'annoyingguy@montague.lit',
  3356. 'affiliation': 'member',
  3357. 'role': 'participant'
  3358. });
  3359. _converse.connection._dataRecv(mock.createRequest(presence));
  3360. await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent.trim() ===
  3361. "romeo and annoyingGuy have entered the groupchat");
  3362. const textarea = await u.waitUntil(() => view.querySelector('.chat-textarea'));
  3363. textarea.value = '/mute';
  3364. const bottom_panel = view.querySelector('converse-muc-bottom-panel');
  3365. bottom_panel.onKeyDown({
  3366. target: textarea,
  3367. preventDefault: function preventDefault () {},
  3368. keyCode: 13
  3369. });
  3370. expect(view.model.validateRoleOrAffiliationChangeArgs).toHaveBeenCalled();
  3371. await u.waitUntil(() => view.querySelector('.message:last-child')?.textContent?.trim() ===
  3372. "Error: the \"mute\" command takes two arguments, the user's nickname and optionally a reason.");
  3373. expect(view.model.setRole).not.toHaveBeenCalled();
  3374. // Call now with the correct amount of arguments.
  3375. // XXX: Calling onFormSubmitted directly, trying
  3376. // again via triggering Event doesn't work for some weird
  3377. // reason.
  3378. textarea.value = '/mute annoyingGuy You\'re annoying';
  3379. bottom_panel.onFormSubmitted(new Event('submit'));
  3380. expect(view.model.validateRoleOrAffiliationChangeArgs.calls.count()).toBe(2);
  3381. expect(view.model.setRole).toHaveBeenCalled();
  3382. expect(Strophe.serialize(sent_IQ)).toBe(
  3383. `<iq id="${IQ_id}" to="lounge@montague.lit" type="set" xmlns="jabber:client">`+
  3384. `<query xmlns="http://jabber.org/protocol/muc#admin">`+
  3385. `<item nick="annoyingGuy" role="visitor">`+
  3386. `<reason>You&apos;re annoying</reason>`+
  3387. `</item>`+
  3388. `</query>`+
  3389. `</iq>`);
  3390. /* <presence
  3391. * from='coven@chat.shakespeare.lit/thirdwitch'
  3392. * to='crone1@shakespeare.lit/desktop'>
  3393. * <x xmlns='http://jabber.org/protocol/muc#user'>
  3394. * <item affiliation='member'
  3395. * jid='hag66@shakespeare.lit/pda'
  3396. * role='visitor'/>
  3397. * </x>
  3398. * </presence>
  3399. */
  3400. presence = $pres({
  3401. 'from': 'lounge@montague.lit/annoyingGuy',
  3402. 'to': 'romeo@montague.lit/desktop'
  3403. })
  3404. .c('x', { 'xmlns': 'http://jabber.org/protocol/muc#user'})
  3405. .c('item', {
  3406. 'jid': 'annoyingguy@montague.lit',
  3407. 'affiliation': 'member',
  3408. 'role': 'visitor'
  3409. });
  3410. _converse.connection._dataRecv(mock.createRequest(presence));
  3411. await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent.includes("annoyingGuy has been muted"));
  3412. // Call now with the correct of arguments.
  3413. // XXX: Calling onFormSubmitted directly, trying
  3414. // again via triggering Event doesn't work for some weird
  3415. // reason.
  3416. textarea.value = '/voice annoyingGuy Now you can talk again';
  3417. bottom_panel.onFormSubmitted(new Event('submit'));
  3418. expect(view.model.validateRoleOrAffiliationChangeArgs.calls.count()).toBe(3);
  3419. expect(view.model.setRole).toHaveBeenCalled();
  3420. expect(Strophe.serialize(sent_IQ)).toBe(
  3421. `<iq id="${IQ_id}" to="lounge@montague.lit" type="set" xmlns="jabber:client">`+
  3422. `<query xmlns="http://jabber.org/protocol/muc#admin">`+
  3423. `<item nick="annoyingGuy" role="participant">`+
  3424. `<reason>Now you can talk again</reason>`+
  3425. `</item>`+
  3426. `</query>`+
  3427. `</iq>`);
  3428. /* <presence
  3429. * from='coven@chat.shakespeare.lit/thirdwitch'
  3430. * to='crone1@shakespeare.lit/desktop'>
  3431. * <x xmlns='http://jabber.org/protocol/muc#user'>
  3432. * <item affiliation='member'
  3433. * jid='hag66@shakespeare.lit/pda'
  3434. * role='visitor'/>
  3435. * </x>
  3436. * </presence>
  3437. */
  3438. presence = $pres({
  3439. 'from': 'lounge@montague.lit/annoyingGuy',
  3440. 'to': 'romeo@montague.lit/desktop'
  3441. })
  3442. .c('x', { 'xmlns': 'http://jabber.org/protocol/muc#user'})
  3443. .c('item', {
  3444. 'jid': 'annoyingguy@montague.lit',
  3445. 'affiliation': 'member',
  3446. 'role': 'participant'
  3447. });
  3448. _converse.connection._dataRecv(mock.createRequest(presence));
  3449. await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent.includes("annoyingGuy has been given a voice"));
  3450. done();
  3451. }));
  3452. it("takes /destroy to destroy a muc",
  3453. mock.initConverse([], {}, async function (done, _converse) {
  3454. const muc_jid = 'lounge@montague.lit';
  3455. const new_muc_jid = 'foyer@montague.lit';
  3456. await mock.openAndEnterChatRoom(_converse, muc_jid, 'romeo');
  3457. let view = _converse.api.chatviews.get(muc_jid);
  3458. spyOn(_converse.api, 'confirm').and.callThrough();
  3459. let textarea = await u.waitUntil(() => view.querySelector('.chat-textarea'));
  3460. textarea.value = '/destroy';
  3461. let bottom_panel = view.querySelector('converse-muc-bottom-panel');
  3462. bottom_panel.onFormSubmitted(new Event('submit'));
  3463. let modal = await u.waitUntil(() => document.querySelector('.modal-dialog'));
  3464. await u.waitUntil(() => u.isVisible(modal));
  3465. let challenge_el = modal.querySelector('[name="challenge"]');
  3466. challenge_el.value = muc_jid+'e';
  3467. const reason_el = modal.querySelector('[name="reason"]');
  3468. reason_el.value = 'Moved to a new location';
  3469. const newjid_el = modal.querySelector('[name="newjid"]');
  3470. newjid_el.value = new_muc_jid;
  3471. let submit = modal.querySelector('[type="submit"]');
  3472. submit.click();
  3473. expect(u.isVisible(modal)).toBeTruthy();
  3474. expect(u.hasClass('error', challenge_el)).toBeTruthy();
  3475. challenge_el.value = muc_jid;
  3476. submit.click();
  3477. let sent_IQs = _converse.connection.IQ_stanzas;
  3478. let sent_IQ = await u.waitUntil(() => sent_IQs.filter(iq => iq.querySelector('destroy')).pop());
  3479. expect(Strophe.serialize(sent_IQ)).toBe(
  3480. `<iq id="${sent_IQ.getAttribute('id')}" to="${muc_jid}" type="set" xmlns="jabber:client">`+
  3481. `<query xmlns="http://jabber.org/protocol/muc#owner">`+
  3482. `<destroy jid="${new_muc_jid}">`+
  3483. `<reason>`+
  3484. `Moved to a new location`+
  3485. `</reason>`+
  3486. `</destroy>`+
  3487. `</query>`+
  3488. `</iq>`);
  3489. let result_stanza = $iq({
  3490. 'type': 'result',
  3491. 'id': sent_IQ.getAttribute('id'),
  3492. 'from': view.model.get('jid'),
  3493. 'to': _converse.connection.jid
  3494. });
  3495. expect(_converse.chatboxes.length).toBe(2);
  3496. spyOn(_converse.api, "trigger").and.callThrough();
  3497. _converse.connection._dataRecv(mock.createRequest(result_stanza));
  3498. await u.waitUntil(() => (view.model.session.get('connection_status') === converse.ROOMSTATUS.DISCONNECTED));
  3499. await u.waitUntil(() => _converse.chatboxes.length === 1);
  3500. expect(_converse.api.trigger).toHaveBeenCalledWith('chatBoxClosed', jasmine.any(Object));
  3501. // Try again without reason or new JID
  3502. _converse.connection.IQ_stanzas = [];
  3503. sent_IQs = _converse.connection.IQ_stanzas;
  3504. await mock.openAndEnterChatRoom(_converse, new_muc_jid, 'romeo');
  3505. view = _converse.api.chatviews.get(new_muc_jid);
  3506. textarea = await u.waitUntil(() => view.querySelector('.chat-textarea'));
  3507. textarea.value = '/destroy';
  3508. bottom_panel = view.querySelector('converse-muc-bottom-panel');
  3509. bottom_panel.onFormSubmitted(new Event('submit'));
  3510. modal = await u.waitUntil(() => document.querySelector('.modal-dialog'));
  3511. await u.waitUntil(() => u.isVisible(modal));
  3512. challenge_el = modal.querySelector('[name="challenge"]');
  3513. challenge_el.value = new_muc_jid;
  3514. submit = modal.querySelector('[type="submit"]');
  3515. submit.click();
  3516. sent_IQ = await u.waitUntil(() => sent_IQs.filter(iq => iq.querySelector('destroy')).pop());
  3517. expect(Strophe.serialize(sent_IQ)).toBe(
  3518. `<iq id="${sent_IQ.getAttribute('id')}" to="${new_muc_jid}" type="set" xmlns="jabber:client">`+
  3519. `<query xmlns="http://jabber.org/protocol/muc#owner">`+
  3520. `<destroy/>`+
  3521. `</query>`+
  3522. `</iq>`);
  3523. result_stanza = $iq({
  3524. 'type': 'result',
  3525. 'id': sent_IQ.getAttribute('id'),
  3526. 'from': view.model.get('jid'),
  3527. 'to': _converse.connection.jid
  3528. });
  3529. expect(_converse.chatboxes.length).toBe(2);
  3530. _converse.connection._dataRecv(mock.createRequest(result_stanza));
  3531. await u.waitUntil(() => (view.model.session.get('connection_status') === converse.ROOMSTATUS.DISCONNECTED));
  3532. await u.waitUntil(() => _converse.chatboxes.length === 1);
  3533. done();
  3534. }));
  3535. });
  3536. describe("When attempting to enter a groupchat", function () {
  3537. it("will use the nickname set in the global settings if the user doesn't have a VCard nickname",
  3538. mock.initConverse(['chatBoxesFetched'], {'nickname': 'Benedict-Cucumberpatch'},
  3539. async function (done, _converse) {
  3540. await mock.openChatRoomViaModal(_converse, 'roomy@muc.montague.lit');
  3541. const view = _converse.chatboxviews.get('roomy@muc.montague.lit');
  3542. expect(view.model.get('nick')).toBe('Benedict-Cucumberpatch');
  3543. done();
  3544. }));
  3545. it("will show an error message if the groupchat requires a password",
  3546. mock.initConverse(['chatBoxesFetched'], {}, async function (done, _converse) {
  3547. const muc_jid = 'protected';
  3548. await mock.openChatRoomViaModal(_converse, muc_jid, 'romeo');
  3549. const view = _converse.chatboxviews.get(muc_jid);
  3550. spyOn(view, 'renderPasswordForm').and.callThrough();
  3551. const presence = $pres().attrs({
  3552. 'from': `${muc_jid}/romeo`,
  3553. 'id': u.getUniqueId(),
  3554. 'to': 'romeo@montague.lit/pda',
  3555. 'type': 'error'
  3556. }).c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
  3557. .c('error').attrs({by:'lounge@montague.lit', type:'auth'})
  3558. .c('not-authorized').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'});
  3559. _converse.connection._dataRecv(mock.createRequest(presence));
  3560. const chat_body = view.querySelector('.chatroom-body');
  3561. expect(view.renderPasswordForm).toHaveBeenCalled();
  3562. expect(chat_body.querySelectorAll('form.chatroom-form').length).toBe(1);
  3563. expect(chat_body.querySelector('.chatroom-form label').textContent.trim())
  3564. .toBe('This groupchat requires a password');
  3565. // Let's submit the form
  3566. spyOn(view.model, 'join');
  3567. const input_el = view.querySelector('[name="password"]');
  3568. input_el.value = 'secret';
  3569. view.querySelector('input[type=submit]').click();
  3570. expect(view.model.join).toHaveBeenCalledWith('romeo', 'secret');
  3571. done();
  3572. }));
  3573. it("will show an error message if the groupchat is members-only and the user not included",
  3574. mock.initConverse([], {}, async function (done, _converse) {
  3575. const muc_jid = 'members-only@muc.montague.lit'
  3576. await mock.openChatRoomViaModal(_converse, muc_jid, 'romeo');
  3577. const view = _converse.chatboxviews.get(muc_jid);
  3578. const iq = await u.waitUntil(() => _.filter(
  3579. _converse.connection.IQ_stanzas,
  3580. iq => iq.querySelector(
  3581. `iq[to="${muc_jid}"] query[xmlns="http://jabber.org/protocol/disco#info"]`
  3582. )).pop());
  3583. // State that the chat is members-only via the features IQ
  3584. const features_stanza = $iq({
  3585. 'from': muc_jid,
  3586. 'id': iq.getAttribute('id'),
  3587. 'to': 'romeo@montague.lit/desktop',
  3588. 'type': 'result'
  3589. })
  3590. .c('query', { 'xmlns': 'http://jabber.org/protocol/disco#info'})
  3591. .c('identity', {
  3592. 'category': 'conference',
  3593. 'name': 'A Dark Cave',
  3594. 'type': 'text'
  3595. }).up()
  3596. .c('feature', {'var': 'http://jabber.org/protocol/muc'}).up()
  3597. .c('feature', {'var': 'muc_hidden'}).up()
  3598. .c('feature', {'var': 'muc_temporary'}).up()
  3599. .c('feature', {'var': 'muc_membersonly'}).up();
  3600. _converse.connection._dataRecv(mock.createRequest(features_stanza));
  3601. await u.waitUntil(() => view.model.session.get('connection_status') === converse.ROOMSTATUS.CONNECTING);
  3602. const presence = $pres().attrs({
  3603. from: `${muc_jid}/romeo`,
  3604. id: u.getUniqueId(),
  3605. to: 'romeo@montague.lit/pda',
  3606. type: 'error'
  3607. }).c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
  3608. .c('error').attrs({by:'lounge@montague.lit', type:'auth'})
  3609. .c('registration-required').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
  3610. _converse.connection._dataRecv(mock.createRequest(presence));
  3611. expect(view.querySelector('.chatroom-body .disconnect-container .disconnect-msg:last-child').textContent.trim())
  3612. .toBe('You are not on the member list of this groupchat.');
  3613. done();
  3614. }));
  3615. it("will show an error message if the user has been banned",
  3616. mock.initConverse([], {}, async function (done, _converse) {
  3617. const muc_jid = 'off-limits@muc.montague.lit'
  3618. await mock.openChatRoomViaModal(_converse, muc_jid, 'romeo');
  3619. const iq = await u.waitUntil(() => _.filter(
  3620. _converse.connection.IQ_stanzas,
  3621. iq => iq.querySelector(
  3622. `iq[to="${muc_jid}"] query[xmlns="http://jabber.org/protocol/disco#info"]`
  3623. )).pop());
  3624. const features_stanza = $iq({
  3625. 'from': muc_jid,
  3626. 'id': iq.getAttribute('id'),
  3627. 'to': 'romeo@montague.lit/desktop',
  3628. 'type': 'result'
  3629. })
  3630. .c('query', { 'xmlns': 'http://jabber.org/protocol/disco#info'})
  3631. .c('identity', {'category': 'conference', 'name': 'A Dark Cave', 'type': 'text'}).up()
  3632. .c('feature', {'var': 'http://jabber.org/protocol/muc'}).up()
  3633. .c('feature', {'var': 'muc_hidden'}).up()
  3634. .c('feature', {'var': 'muc_temporary'}).up()
  3635. _converse.connection._dataRecv(mock.createRequest(features_stanza));
  3636. const view = _converse.chatboxviews.get(muc_jid);
  3637. await u.waitUntil(() => view.model.session.get('connection_status') === converse.ROOMSTATUS.CONNECTING);
  3638. const presence = $pres().attrs({
  3639. from: `${muc_jid}/romeo`,
  3640. id: u.getUniqueId(),
  3641. to: 'romeo@montague.lit/pda',
  3642. type: 'error'
  3643. }).c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
  3644. .c('error').attrs({by:'lounge@montague.lit', type:'auth'})
  3645. .c('forbidden').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
  3646. _converse.connection._dataRecv(mock.createRequest(presence));
  3647. expect(view.querySelector('.chatroom-body .disconnect-container .disconnect-msg:last-child').textContent.trim())
  3648. .toBe('You have been banned from this groupchat.');
  3649. done();
  3650. }));
  3651. it("will render a nickname form if a nickname conflict happens and muc_nickname_from_jid=false",
  3652. mock.initConverse([], {}, async function (done, _converse) {
  3653. const muc_jid = 'conflicted@muc.montague.lit';
  3654. await mock.openChatRoomViaModal(_converse, muc_jid, 'romeo');
  3655. const iq = await u.waitUntil(() => _.filter(
  3656. _converse.connection.IQ_stanzas,
  3657. iq => iq.querySelector(
  3658. `iq[to="${muc_jid}"] query[xmlns="http://jabber.org/protocol/disco#info"]`
  3659. )).pop());
  3660. const features_stanza = $iq({
  3661. 'from': muc_jid,
  3662. 'id': iq.getAttribute('id'),
  3663. 'to': 'romeo@montague.lit/desktop',
  3664. 'type': 'result'
  3665. })
  3666. .c('query', { 'xmlns': 'http://jabber.org/protocol/disco#info'})
  3667. .c('identity', {'category': 'conference', 'name': 'A Dark Cave', 'type': 'text'}).up()
  3668. .c('feature', {'var': 'http://jabber.org/protocol/muc'}).up()
  3669. .c('feature', {'var': 'muc_hidden'}).up()
  3670. .c('feature', {'var': 'muc_temporary'}).up()
  3671. _converse.connection._dataRecv(mock.createRequest(features_stanza));
  3672. const view = _converse.chatboxviews.get(muc_jid);
  3673. await u.waitUntil(() => view.model.session.get('connection_status') === converse.ROOMSTATUS.CONNECTING);
  3674. const presence = $pres().attrs({
  3675. from: `${muc_jid}/romeo`,
  3676. id: u.getUniqueId(),
  3677. to: 'romeo@montague.lit/pda',
  3678. type: 'error'
  3679. }).c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
  3680. .c('error').attrs({by: muc_jid, type:'cancel'})
  3681. .c('conflict').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
  3682. _converse.connection._dataRecv(mock.createRequest(presence));
  3683. expect(view.querySelector('.muc-nickname-form .validation-message').textContent.trim())
  3684. .toBe('The nickname you chose is reserved or currently in use, please choose a different one.');
  3685. done();
  3686. }));
  3687. it("will automatically choose a new nickname if a nickname conflict happens and muc_nickname_from_jid=true",
  3688. mock.initConverse(['chatBoxesFetched'], {}, async function (done, _converse) {
  3689. const muc_jid = 'conflicting@muc.montague.lit'
  3690. await mock.openChatRoomViaModal(_converse, muc_jid, 'romeo');
  3691. /* <presence
  3692. * from='coven@chat.shakespeare.lit/thirdwitch'
  3693. * id='n13mt3l'
  3694. * to='hag66@shakespeare.lit/pda'
  3695. * type='error'>
  3696. * <x xmlns='http://jabber.org/protocol/muc'/>
  3697. * <error by='coven@chat.shakespeare.lit' type='cancel'>
  3698. * <conflict xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/>
  3699. * </error>
  3700. * </presence>
  3701. */
  3702. _converse.muc_nickname_from_jid = true;
  3703. const attrs = {
  3704. 'from': `${muc_jid}/romeo`,
  3705. 'id': u.getUniqueId(),
  3706. 'to': 'romeo@montague.lit/pda',
  3707. 'type': 'error'
  3708. };
  3709. let presence = $pres().attrs(attrs)
  3710. .c('x').attrs({'xmlns':'http://jabber.org/protocol/muc'}).up()
  3711. .c('error').attrs({'by': muc_jid, 'type':'cancel'})
  3712. .c('conflict').attrs({'xmlns':'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
  3713. const view = _converse.chatboxviews.get(muc_jid);
  3714. spyOn(view.model, 'join').and.callThrough();
  3715. // Simulate repeatedly that there's already someone in the groupchat
  3716. // with that nickname
  3717. _converse.connection._dataRecv(mock.createRequest(presence));
  3718. expect(view.model.join).toHaveBeenCalledWith('romeo-2');
  3719. attrs.from = `${muc_jid}/romeo-2`;
  3720. attrs.id = u.getUniqueId();
  3721. presence = $pres().attrs(attrs)
  3722. .c('x').attrs({'xmlns':'http://jabber.org/protocol/muc'}).up()
  3723. .c('error').attrs({'by': muc_jid, type:'cancel'})
  3724. .c('conflict').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
  3725. _converse.connection._dataRecv(mock.createRequest(presence));
  3726. expect(view.model.join).toHaveBeenCalledWith('romeo-3');
  3727. attrs.from = `${muc_jid}/romeo-3`;
  3728. attrs.id = new Date().getTime();
  3729. presence = $pres().attrs(attrs)
  3730. .c('x').attrs({'xmlns': 'http://jabber.org/protocol/muc'}).up()
  3731. .c('error').attrs({'by': muc_jid, 'type': 'cancel'})
  3732. .c('conflict').attrs({'xmlns':'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
  3733. _converse.connection._dataRecv(mock.createRequest(presence));
  3734. expect(view.model.join).toHaveBeenCalledWith('romeo-4');
  3735. done();
  3736. }));
  3737. it("will show an error message if the user is not allowed to have created the groupchat",
  3738. mock.initConverse([], {}, async function (done, _converse) {
  3739. const muc_jid = 'impermissable@muc.montague.lit'
  3740. await mock.openChatRoomViaModal(_converse, muc_jid, 'romeo')
  3741. // We pretend this is a new room, so no disco info is returned.
  3742. const iq = await u.waitUntil(() => _.filter(
  3743. _converse.connection.IQ_stanzas,
  3744. iq => iq.querySelector(
  3745. `iq[to="${muc_jid}"] query[xmlns="http://jabber.org/protocol/disco#info"]`
  3746. )).pop());
  3747. const features_stanza = $iq({
  3748. 'from': 'room@conference.example.org',
  3749. 'id': iq.getAttribute('id'),
  3750. 'to': 'romeo@montague.lit/desktop',
  3751. 'type': 'error'
  3752. }).c('error', {'type': 'cancel'})
  3753. .c('item-not-found', {'xmlns': "urn:ietf:params:xml:ns:xmpp-stanzas"});
  3754. _converse.connection._dataRecv(mock.createRequest(features_stanza));
  3755. const view = _converse.chatboxviews.get(muc_jid);
  3756. await u.waitUntil(() => (view.model.session.get('connection_status') === converse.ROOMSTATUS.CONNECTING));
  3757. const presence = $pres().attrs({
  3758. from: `${muc_jid}/romeo`,
  3759. id: u.getUniqueId(),
  3760. to:'romeo@montague.lit/pda',
  3761. type:'error'
  3762. }).c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
  3763. .c('error').attrs({by:'lounge@montague.lit', type:'cancel'})
  3764. .c('not-allowed').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
  3765. _converse.connection._dataRecv(mock.createRequest(presence));
  3766. expect(view.querySelector('.chatroom-body .disconnect-container .disconnect-msg:last-child').textContent.trim())
  3767. .toBe('You are not allowed to create new groupchats.');
  3768. done();
  3769. }));
  3770. it("will show an error message if the user's nickname doesn't conform to groupchat policy",
  3771. mock.initConverse([], {}, async function (done, _converse) {
  3772. const muc_jid = 'conformist@muc.montague.lit'
  3773. await mock.openChatRoomViaModal(_converse, muc_jid, 'romeo');
  3774. const iq = await u.waitUntil(() => _.filter(
  3775. _converse.connection.IQ_stanzas,
  3776. iq => iq.querySelector(
  3777. `iq[to="${muc_jid}"] query[xmlns="http://jabber.org/protocol/disco#info"]`
  3778. )).pop());
  3779. const features_stanza = $iq({
  3780. 'from': muc_jid,
  3781. 'id': iq.getAttribute('id'),
  3782. 'to': 'romeo@montague.lit/desktop',
  3783. 'type': 'result'
  3784. }).c('query', { 'xmlns': 'http://jabber.org/protocol/disco#info'})
  3785. .c('identity', {'category': 'conference', 'name': 'A Dark Cave', 'type': 'text'}).up()
  3786. .c('feature', {'var': 'http://jabber.org/protocol/muc'}).up()
  3787. _converse.connection._dataRecv(mock.createRequest(features_stanza));
  3788. const view = _converse.chatboxviews.get(muc_jid);
  3789. await u.waitUntil(() => (view.model.session.get('connection_status') === converse.ROOMSTATUS.CONNECTING));
  3790. const presence = $pres().attrs({
  3791. from: `${muc_jid}/romeo`,
  3792. id: u.getUniqueId(),
  3793. to:'romeo@montague.lit/pda',
  3794. type:'error'
  3795. }).c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
  3796. .c('error').attrs({by:'lounge@montague.lit', type:'cancel'})
  3797. .c('not-acceptable').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
  3798. _converse.connection._dataRecv(mock.createRequest(presence));
  3799. expect(view.querySelector('.chatroom-body .disconnect-container .disconnect-msg:last-child').textContent.trim())
  3800. .toBe("Your nickname doesn't conform to this groupchat's policies.");
  3801. done();
  3802. }));
  3803. it("will show an error message if the groupchat doesn't yet exist",
  3804. mock.initConverse([], {}, async function (done, _converse) {
  3805. const muc_jid = 'nonexistent@muc.montague.lit'
  3806. await mock.openChatRoomViaModal(_converse, muc_jid, 'romeo');
  3807. const iq = await u.waitUntil(() => _.filter(
  3808. _converse.connection.IQ_stanzas,
  3809. iq => iq.querySelector(
  3810. `iq[to="${muc_jid}"] query[xmlns="http://jabber.org/protocol/disco#info"]`
  3811. )).pop());
  3812. const features_stanza = $iq({
  3813. 'from': muc_jid,
  3814. 'id': iq.getAttribute('id'),
  3815. 'to': 'romeo@montague.lit/desktop',
  3816. 'type': 'result'
  3817. }).c('query', { 'xmlns': 'http://jabber.org/protocol/disco#info'})
  3818. .c('identity', {'category': 'conference', 'name': 'A Dark Cave', 'type': 'text'}).up()
  3819. .c('feature', {'var': 'http://jabber.org/protocol/muc'}).up()
  3820. _converse.connection._dataRecv(mock.createRequest(features_stanza));
  3821. const view = _converse.chatboxviews.get(muc_jid);
  3822. await u.waitUntil(() => (view.model.session.get('connection_status') === converse.ROOMSTATUS.CONNECTING));
  3823. const presence = $pres().attrs({
  3824. from: `${muc_jid}/romeo`,
  3825. id: u.getUniqueId(),
  3826. to: 'romeo@montague.lit/pda',
  3827. type:'error'
  3828. }).c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
  3829. .c('error').attrs({by:'lounge@montague.lit', type:'cancel'})
  3830. .c('item-not-found').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
  3831. _converse.connection._dataRecv(mock.createRequest(presence));
  3832. expect(view.querySelector('.chatroom-body .disconnect-container .disconnect-msg:last-child').textContent.trim())
  3833. .toBe("This groupchat does not (yet) exist.");
  3834. done();
  3835. }));
  3836. it("will show an error message if the groupchat has reached its maximum number of participants",
  3837. mock.initConverse([], {}, async function (done, _converse) {
  3838. const muc_jid = 'maxed-out@muc.montague.lit'
  3839. await mock.openChatRoomViaModal(_converse, muc_jid, 'romeo')
  3840. const iq = await u.waitUntil(() => _.filter(
  3841. _converse.connection.IQ_stanzas,
  3842. iq => iq.querySelector(
  3843. `iq[to="${muc_jid}"] query[xmlns="http://jabber.org/protocol/disco#info"]`
  3844. )).pop());
  3845. const features_stanza = $iq({
  3846. 'from': muc_jid,
  3847. 'id': iq.getAttribute('id'),
  3848. 'to': 'romeo@montague.lit/desktop',
  3849. 'type': 'result'
  3850. }).c('query', { 'xmlns': 'http://jabber.org/protocol/disco#info'})
  3851. .c('identity', {'category': 'conference', 'name': 'A Dark Cave', 'type': 'text'}).up()
  3852. .c('feature', {'var': 'http://jabber.org/protocol/muc'}).up()
  3853. _converse.connection._dataRecv(mock.createRequest(features_stanza));
  3854. const view = _converse.chatboxviews.get(muc_jid);
  3855. await u.waitUntil(() => (view.model.session.get('connection_status') === converse.ROOMSTATUS.CONNECTING));
  3856. const presence = $pres().attrs({
  3857. from: `${muc_jid}/romeo`,
  3858. id: u.getUniqueId(),
  3859. to:'romeo@montague.lit/pda',
  3860. type:'error'
  3861. }).c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
  3862. .c('error').attrs({by:'lounge@montague.lit', type:'cancel'})
  3863. .c('service-unavailable').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
  3864. _converse.connection._dataRecv(mock.createRequest(presence));
  3865. expect(view.querySelector('.chatroom-body .disconnect-container .disconnect-msg:last-child').textContent.trim())
  3866. .toBe("This groupchat has reached its maximum number of participants.");
  3867. done();
  3868. }));
  3869. });
  3870. describe("Someone being invited to a groupchat", function () {
  3871. it("will first be added to the member list if the groupchat is members only",
  3872. mock.initConverse(['chatBoxesFetched'], {}, async function (done, _converse) {
  3873. await mock.waitForRoster(_converse, 'current', 0);
  3874. spyOn(_converse.ChatRoomOccupants.prototype, 'fetchMembers').and.callThrough();
  3875. const sent_IQs = _converse.connection.IQ_stanzas;
  3876. const muc_jid = 'coven@chat.shakespeare.lit';
  3877. const nick = 'romeo';
  3878. const room_creation_promise = _converse.api.rooms.open(muc_jid, {nick});
  3879. // Check that the groupchat queried for the features.
  3880. let stanza = await u.waitUntil(() => sent_IQs.filter(iq => iq.querySelector(`iq[to="${muc_jid}"] query[xmlns="http://jabber.org/protocol/disco#info"]`)).pop());
  3881. expect(Strophe.serialize(stanza)).toBe(
  3882. `<iq from="romeo@montague.lit/orchard" id="${stanza.getAttribute("id")}" to="${muc_jid}" type="get" xmlns="jabber:client">`+
  3883. `<query xmlns="http://jabber.org/protocol/disco#info"/>`+
  3884. `</iq>`);
  3885. // State that the chat is members-only via the features IQ
  3886. const view = _converse.chatboxviews.get(muc_jid);
  3887. const features_stanza = $iq({
  3888. from: 'coven@chat.shakespeare.lit',
  3889. 'id': stanza.getAttribute('id'),
  3890. 'to': 'romeo@montague.lit/desktop',
  3891. 'type': 'result'
  3892. })
  3893. .c('query', { 'xmlns': 'http://jabber.org/protocol/disco#info'})
  3894. .c('identity', {
  3895. 'category': 'conference',
  3896. 'name': 'A Dark Cave',
  3897. 'type': 'text'
  3898. }).up()
  3899. .c('feature', {'var': 'http://jabber.org/protocol/muc'}).up()
  3900. .c('feature', {'var': 'muc_hidden'}).up()
  3901. .c('feature', {'var': 'muc_temporary'}).up()
  3902. .c('feature', {'var': 'muc_membersonly'}).up();
  3903. _converse.connection._dataRecv(mock.createRequest(features_stanza));
  3904. const sent_stanzas = _converse.connection.sent_stanzas;
  3905. await u.waitUntil(() => sent_stanzas.filter(s => s.matches(`presence[to="${muc_jid}/${nick}"]`)).pop());
  3906. expect(view.model.features.get('membersonly')).toBeTruthy();
  3907. await room_creation_promise;
  3908. await mock.createContacts(_converse, 'current');
  3909. let sent_stanza, sent_id;
  3910. spyOn(_converse.connection, 'send').and.callFake(function (stanza) {
  3911. if (stanza.nodeTree && stanza.nodeTree.nodeName === 'message') {
  3912. sent_id = stanza.nodeTree.getAttribute('id');
  3913. sent_stanza = stanza;
  3914. }
  3915. });
  3916. const invitee_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@montague.lit';
  3917. const reason = "Please join this groupchat";
  3918. view.model.directInvite(invitee_jid, reason);
  3919. // Check in reverse order that we requested all three lists
  3920. const owner_iq = sent_IQs.pop();
  3921. expect(Strophe.serialize(owner_iq)).toBe(
  3922. `<iq id="${owner_iq.getAttribute('id')}" to="coven@chat.shakespeare.lit" type="get" xmlns="jabber:client">`+
  3923. `<query xmlns="http://jabber.org/protocol/muc#admin"><item affiliation="owner"/></query>`+
  3924. `</iq>`);
  3925. const admin_iq = sent_IQs.pop();
  3926. expect(Strophe.serialize(admin_iq)).toBe(
  3927. `<iq id="${admin_iq.getAttribute('id')}" to="coven@chat.shakespeare.lit" type="get" xmlns="jabber:client">`+
  3928. `<query xmlns="http://jabber.org/protocol/muc#admin"><item affiliation="admin"/></query>`+
  3929. `</iq>`);
  3930. const member_iq = sent_IQs.pop();
  3931. expect(Strophe.serialize(member_iq)).toBe(
  3932. `<iq id="${member_iq.getAttribute('id')}" to="coven@chat.shakespeare.lit" type="get" xmlns="jabber:client">`+
  3933. `<query xmlns="http://jabber.org/protocol/muc#admin"><item affiliation="member"/></query>`+
  3934. `</iq>`);
  3935. // Now the service sends the member lists to the user
  3936. const member_list_stanza = $iq({
  3937. 'from': 'coven@chat.shakespeare.lit',
  3938. 'id': member_iq.getAttribute('id'),
  3939. 'to': 'romeo@montague.lit/orchard',
  3940. 'type': 'result'
  3941. }).c('query', {'xmlns': Strophe.NS.MUC_ADMIN})
  3942. .c('item', {
  3943. 'affiliation': 'member',
  3944. 'jid': 'hag66@shakespeare.lit',
  3945. 'nick': 'thirdwitch',
  3946. 'role': 'participant'
  3947. });
  3948. _converse.connection._dataRecv(mock.createRequest(member_list_stanza));
  3949. const admin_list_stanza = $iq({
  3950. 'from': 'coven@chat.shakespeare.lit',
  3951. 'id': admin_iq.getAttribute('id'),
  3952. 'to': 'romeo@montague.lit/orchard',
  3953. 'type': 'result'
  3954. }).c('query', {'xmlns': Strophe.NS.MUC_ADMIN})
  3955. .c('item', {
  3956. 'affiliation': 'admin',
  3957. 'jid': 'wiccarocks@shakespeare.lit',
  3958. 'nick': 'secondwitch'
  3959. });
  3960. _converse.connection._dataRecv(mock.createRequest(admin_list_stanza));
  3961. const owner_list_stanza = $iq({
  3962. 'from': 'coven@chat.shakespeare.lit',
  3963. 'id': owner_iq.getAttribute('id'),
  3964. 'to': 'romeo@montague.lit/orchard',
  3965. 'type': 'result'
  3966. }).c('query', {'xmlns': Strophe.NS.MUC_ADMIN})
  3967. .c('item', {
  3968. 'affiliation': 'owner',
  3969. 'jid': 'crone1@shakespeare.lit',
  3970. });
  3971. _converse.connection._dataRecv(mock.createRequest(owner_list_stanza));
  3972. // Converse puts the user on the member list
  3973. stanza = await u.waitUntil(() => sent_IQs.filter(iq => iq.querySelector(`iq[to="${muc_jid}"] query[xmlns="http://jabber.org/protocol/muc#admin"]`)).pop());
  3974. expect(stanza.outerHTML,
  3975. `<iq id="${stanza.getAttribute('id')}" to="coven@chat.shakespeare.lit" type="set" xmlns="jabber:client">`+
  3976. `<query xmlns="http://jabber.org/protocol/muc#admin">`+
  3977. `<item affiliation="member" jid="${invitee_jid}">`+
  3978. `<reason>Please join this groupchat</reason>`+
  3979. `</item>`+
  3980. `</query>`+
  3981. `</iq>`);
  3982. const result = $iq({
  3983. 'from': 'coven@chat.shakespeare.lit',
  3984. 'id': stanza.getAttribute('id'),
  3985. 'to': 'romeo@montague.lit/orchard',
  3986. 'type': 'result'
  3987. });
  3988. _converse.connection._dataRecv(mock.createRequest(result));
  3989. await u.waitUntil(() => view.model.occupants.fetchMembers.calls.count());
  3990. // Finally check that the user gets invited.
  3991. expect(sent_stanza.toLocaleString()).toBe( // Strophe adds the xmlns attr (although not in spec)
  3992. `<message from="romeo@montague.lit/orchard" id="${sent_id}" to="${invitee_jid}" xmlns="jabber:client">`+
  3993. `<x jid="coven@chat.shakespeare.lit" reason="Please join this groupchat" xmlns="jabber:x:conference"/>`+
  3994. `</message>`
  3995. );
  3996. done();
  3997. }));
  3998. });
  3999. describe("The affiliations delta", function () {
  4000. it("can be computed in various ways", mock.initConverse([], {}, async function (done, _converse) {
  4001. await mock.openChatRoom(_converse, 'coven', 'chat.shakespeare.lit', 'romeo');
  4002. var exclude_existing = false;
  4003. var remove_absentees = false;
  4004. var new_list = [];
  4005. var old_list = [];
  4006. const muc_utils = converse.env.muc_utils;
  4007. var delta = muc_utils.computeAffiliationsDelta(exclude_existing, remove_absentees, new_list, old_list);
  4008. expect(delta.length).toBe(0);
  4009. new_list = [{'jid': 'wiccarocks@shakespeare.lit', 'affiliation': 'member'}];
  4010. old_list = [{'jid': 'wiccarocks@shakespeare.lit', 'affiliation': 'member'}];
  4011. delta = muc_utils.computeAffiliationsDelta(exclude_existing, remove_absentees, new_list, old_list);
  4012. expect(delta.length).toBe(0);
  4013. // When remove_absentees is false, then affiliations in the old
  4014. // list which are not in the new one won't be removed.
  4015. old_list = [{'jid': 'oldhag666@shakespeare.lit', 'affiliation': 'owner'},
  4016. {'jid': 'wiccarocks@shakespeare.lit', 'affiliation': 'member'}];
  4017. delta = muc_utils.computeAffiliationsDelta(exclude_existing, remove_absentees, new_list, old_list);
  4018. expect(delta.length).toBe(0);
  4019. // With exclude_existing set to false, any changed affiliations
  4020. // will be included in the delta (i.e. existing affiliations are included in the comparison).
  4021. old_list = [{'jid': 'wiccarocks@shakespeare.lit', 'affiliation': 'owner'}];
  4022. delta = muc_utils.computeAffiliationsDelta(exclude_existing, remove_absentees, new_list, old_list);
  4023. expect(delta.length).toBe(1);
  4024. expect(delta[0].jid).toBe('wiccarocks@shakespeare.lit');
  4025. expect(delta[0].affiliation).toBe('member');
  4026. // To also remove affiliations from the old list which are not
  4027. // in the new list, we set remove_absentees to true
  4028. remove_absentees = true;
  4029. old_list = [{'jid': 'oldhag666@shakespeare.lit', 'affiliation': 'owner'},
  4030. {'jid': 'wiccarocks@shakespeare.lit', 'affiliation': 'member'}];
  4031. delta = muc_utils.computeAffiliationsDelta(exclude_existing, remove_absentees, new_list, old_list);
  4032. expect(delta.length).toBe(1);
  4033. expect(delta[0].jid).toBe('oldhag666@shakespeare.lit');
  4034. expect(delta[0].affiliation).toBe('none');
  4035. delta = muc_utils.computeAffiliationsDelta(exclude_existing, remove_absentees, [], old_list);
  4036. expect(delta.length).toBe(2);
  4037. expect(delta[0].jid).toBe('oldhag666@shakespeare.lit');
  4038. expect(delta[0].affiliation).toBe('none');
  4039. expect(delta[1].jid).toBe('wiccarocks@shakespeare.lit');
  4040. expect(delta[1].affiliation).toBe('none');
  4041. // To only add a user if they don't already have an
  4042. // affiliation, we set 'exclude_existing' to true
  4043. exclude_existing = true;
  4044. old_list = [{'jid': 'wiccarocks@shakespeare.lit', 'affiliation': 'owner'}];
  4045. delta = muc_utils.computeAffiliationsDelta(exclude_existing, remove_absentees, new_list, old_list);
  4046. expect(delta.length).toBe(0);
  4047. old_list = [{'jid': 'wiccarocks@shakespeare.lit', 'affiliation': 'admin'}];
  4048. delta = muc_utils.computeAffiliationsDelta(exclude_existing, remove_absentees, new_list, old_list);
  4049. expect(delta.length).toBe(0);
  4050. done();
  4051. }));
  4052. });
  4053. describe("The \"Groupchats\" Add modal", function () {
  4054. it("can be opened from a link in the \"Groupchats\" section of the controlbox",
  4055. mock.initConverse(['chatBoxesFetched'], {}, async function (done, _converse) {
  4056. await mock.openControlBox(_converse);
  4057. await mock.waitForRoster(_converse, 'current', 0);
  4058. const roomspanel = _converse.chatboxviews.get('controlbox').querySelector('converse-rooms-list');
  4059. roomspanel.querySelector('.show-add-muc-modal').click();
  4060. mock.closeControlBox(_converse);
  4061. const modal = _converse.api.modal.get('add-chatroom-modal');
  4062. await u.waitUntil(() => u.isVisible(modal.el), 1000)
  4063. let label_name = modal.el.querySelector('label[for="chatroom"]');
  4064. expect(label_name.textContent.trim()).toBe('Groupchat address:');
  4065. let name_input = modal.el.querySelector('input[name="chatroom"]');
  4066. expect(name_input.placeholder).toBe('name@conference.example.org');
  4067. const label_nick = modal.el.querySelector('label[for="nickname"]');
  4068. expect(label_nick.textContent.trim()).toBe('Nickname:');
  4069. const nick_input = modal.el.querySelector('input[name="nickname"]');
  4070. expect(nick_input.value).toBe('');
  4071. nick_input.value = 'romeo';
  4072. expect(modal.el.querySelector('.modal-title').textContent.trim()).toBe('Enter a new Groupchat');
  4073. spyOn(_converse.ChatRoom.prototype, 'getDiscoInfo').and.callFake(() => Promise.resolve());
  4074. roomspanel.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  4075. modal.el.querySelector('input[name="chatroom"]').value = 'lounce@muc.montague.lit';
  4076. modal.el.querySelector('form input[type="submit"]').click();
  4077. await u.waitUntil(() => _converse.chatboxes.length);
  4078. await u.waitUntil(() => sizzle('.chatroom', _converse.el).filter(u.isVisible).length === 1);
  4079. roomspanel.model.set('muc_domain', 'muc.example.org');
  4080. roomspanel.querySelector('.show-add-muc-modal').click();
  4081. label_name = modal.el.querySelector('label[for="chatroom"]');
  4082. expect(label_name.textContent.trim()).toBe('Groupchat address:');
  4083. name_input = modal.el.querySelector('input[name="chatroom"]');
  4084. expect(name_input.placeholder).toBe('name@muc.example.org');
  4085. done();
  4086. }));
  4087. it("doesn't show the nickname field if locked_muc_nickname is true",
  4088. mock.initConverse(['chatBoxesFetched'], {'locked_muc_nickname': true, 'muc_nickname_from_jid': true}, async function (done, _converse) {
  4089. await mock.openControlBox(_converse);
  4090. await mock.waitForRoster(_converse, 'current', 0);
  4091. const roomspanel = _converse.chatboxviews.get('controlbox').querySelector('converse-rooms-list');
  4092. roomspanel.querySelector('.show-add-muc-modal').click();
  4093. mock.closeControlBox(_converse);
  4094. const modal = _converse.api.modal.get('add-chatroom-modal');
  4095. await u.waitUntil(() => u.isVisible(modal.el), 1000)
  4096. const name_input = modal.el.querySelector('input[name="chatroom"]');
  4097. name_input.value = 'lounge@montague.lit';
  4098. expect(modal.el.querySelector('label[for="nickname"]')).toBe(null);
  4099. expect(modal.el.querySelector('input[name="nickname"]')).toBe(null);
  4100. modal.el.querySelector('form input[type="submit"]').click();
  4101. await u.waitUntil(() => _converse.chatboxes.length > 1);
  4102. const chatroom = _converse.chatboxes.get('lounge@montague.lit');
  4103. expect(chatroom.get('nick')).toBe('romeo');
  4104. done();
  4105. }));
  4106. it("uses the JID node if muc_nickname_from_jid is set to true",
  4107. mock.initConverse(['chatBoxesFetched'], {'muc_nickname_from_jid': true}, async function (done, _converse) {
  4108. await mock.openControlBox(_converse);
  4109. await mock.waitForRoster(_converse, 'current', 0);
  4110. const roomspanel = _converse.chatboxviews.get('controlbox').querySelector('converse-rooms-list');
  4111. roomspanel.querySelector('.show-add-muc-modal').click();
  4112. mock.closeControlBox(_converse);
  4113. const modal = _converse.api.modal.get('add-chatroom-modal');
  4114. await u.waitUntil(() => u.isVisible(modal.el), 1000)
  4115. const label_nick = modal.el.querySelector('label[for="nickname"]');
  4116. expect(label_nick.textContent.trim()).toBe('Nickname:');
  4117. const nick_input = modal.el.querySelector('input[name="nickname"]');
  4118. expect(nick_input.value).toBe('romeo');
  4119. done();
  4120. }));
  4121. it("uses the nickname passed in to converse.initialize",
  4122. mock.initConverse(['chatBoxesFetched'], {'nickname': 'st.nick'}, async function (done, _converse) {
  4123. await mock.openControlBox(_converse);
  4124. await mock.waitForRoster(_converse, 'current', 0);
  4125. const roomspanel = _converse.chatboxviews.get('controlbox').querySelector('converse-rooms-list');
  4126. roomspanel.querySelector('.show-add-muc-modal').click();
  4127. mock.closeControlBox(_converse);
  4128. const modal = _converse.api.modal.get('add-chatroom-modal');
  4129. await u.waitUntil(() => u.isVisible(modal.el), 1000)
  4130. const label_nick = modal.el.querySelector('label[for="nickname"]');
  4131. expect(label_nick.textContent.trim()).toBe('Nickname:');
  4132. const nick_input = modal.el.querySelector('input[name="nickname"]');
  4133. expect(nick_input.value).toBe('st.nick');
  4134. done();
  4135. }));
  4136. it("doesn't require the domain when muc_domain is set",
  4137. mock.initConverse(['chatBoxesFetched'], {'muc_domain': 'muc.example.org'}, async function (done, _converse) {
  4138. await mock.openControlBox(_converse);
  4139. const roomspanel = _converse.chatboxviews.get('controlbox').querySelector('converse-rooms-list');
  4140. roomspanel.querySelector('.show-add-muc-modal').click();
  4141. const modal = _converse.api.modal.get('add-chatroom-modal');
  4142. await u.waitUntil(() => u.isVisible(modal.el), 1000)
  4143. expect(modal.el.querySelector('.modal-title').textContent.trim()).toBe('Enter a new Groupchat');
  4144. spyOn(_converse.ChatRoom.prototype, 'getDiscoInfo').and.callFake(() => Promise.resolve());
  4145. roomspanel.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  4146. const label_name = modal.el.querySelector('label[for="chatroom"]');
  4147. expect(label_name.textContent.trim()).toBe('Groupchat name:');
  4148. let name_input = modal.el.querySelector('input[name="chatroom"]');
  4149. expect(name_input.placeholder).toBe('name@muc.example.org');
  4150. name_input.value = 'lounge';
  4151. let nick_input = modal.el.querySelector('input[name="nickname"]');
  4152. nick_input.value = 'max';
  4153. modal.el.querySelector('form input[type="submit"]').click();
  4154. await u.waitUntil(() => _converse.chatboxes.length);
  4155. await u.waitUntil(() => sizzle('.chatroom', _converse.el).filter(u.isVisible).length === 1);
  4156. expect(_.includes(_converse.chatboxes.models.map(m => m.get('id')), 'lounge@muc.example.org')).toBe(true);
  4157. // However, you can still open MUCs with different domains
  4158. roomspanel.querySelector('.show-add-muc-modal').click();
  4159. await u.waitUntil(() => u.isVisible(modal.el), 1000);
  4160. name_input = modal.el.querySelector('input[name="chatroom"]');
  4161. name_input.value = 'lounge@conference.example.org';
  4162. nick_input = modal.el.querySelector('input[name="nickname"]');
  4163. nick_input.value = 'max';
  4164. modal.el.querySelector('form input[type="submit"]').click();
  4165. await u.waitUntil(() => _converse.chatboxes.models.filter(c => c.get('type') === 'chatroom').length === 2);
  4166. await u.waitUntil(() => sizzle('.chatroom', _converse.el).filter(u.isVisible).length === 2);
  4167. expect(_.includes(_converse.chatboxes.models.map(m => m.get('id')), 'lounge@conference.example.org')).toBe(true);
  4168. done();
  4169. }));
  4170. it("only uses the muc_domain is locked_muc_domain is true",
  4171. mock.initConverse(['chatBoxesFetched'], {'muc_domain': 'muc.example.org', 'locked_muc_domain': true},
  4172. async function (done, _converse) {
  4173. await mock.openControlBox(_converse);
  4174. const roomspanel = _converse.chatboxviews.get('controlbox').querySelector('converse-rooms-list');
  4175. roomspanel.querySelector('.show-add-muc-modal').click();
  4176. const modal = _converse.api.modal.get('add-chatroom-modal');
  4177. await u.waitUntil(() => u.isVisible(modal.el), 1000)
  4178. expect(modal.el.querySelector('.modal-title').textContent.trim()).toBe('Enter a new Groupchat');
  4179. spyOn(_converse.ChatRoom.prototype, 'getDiscoInfo').and.callFake(() => Promise.resolve());
  4180. roomspanel.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  4181. const label_name = modal.el.querySelector('label[for="chatroom"]');
  4182. expect(label_name.textContent.trim()).toBe('Groupchat name:');
  4183. let name_input = modal.el.querySelector('input[name="chatroom"]');
  4184. expect(name_input.placeholder).toBe('');
  4185. name_input.value = 'lounge';
  4186. let nick_input = modal.el.querySelector('input[name="nickname"]');
  4187. nick_input.value = 'max';
  4188. modal.el.querySelector('form input[type="submit"]').click();
  4189. await u.waitUntil(() => _converse.chatboxes.length);
  4190. await u.waitUntil(() => sizzle('.chatroom', _converse.el).filter(u.isVisible).length === 1);
  4191. expect(_.includes(_converse.chatboxes.models.map(m => m.get('id')), 'lounge@muc.example.org')).toBe(true);
  4192. // However, you can still open MUCs with different domains
  4193. roomspanel.querySelector('.show-add-muc-modal').click();
  4194. await u.waitUntil(() => u.isVisible(modal.el), 1000);
  4195. name_input = modal.el.querySelector('input[name="chatroom"]');
  4196. name_input.value = 'lounge@conference';
  4197. nick_input = modal.el.querySelector('input[name="nickname"]');
  4198. nick_input.value = 'max';
  4199. modal.el.querySelector('form input[type="submit"]').click();
  4200. await u.waitUntil(() => _converse.chatboxes.models.filter(c => c.get('type') === 'chatroom').length === 2);
  4201. await u.waitUntil(() => sizzle('.chatroom', _converse.el).filter(u.isVisible).length === 2);
  4202. expect(_.includes(_converse.chatboxes.models.map(m => m.get('id')), 'lounge\\40conference@muc.example.org')).toBe(true);
  4203. done();
  4204. }));
  4205. });
  4206. describe("The \"Groupchats\" List modal", function () {
  4207. it("can be opened from a link in the \"Groupchats\" section of the controlbox",
  4208. mock.initConverse(['chatBoxesFetched'], {}, async function (done, _converse) {
  4209. await mock.openControlBox(_converse);
  4210. const roomspanel = _converse.chatboxviews.get('controlbox').querySelector('converse-rooms-list');
  4211. roomspanel.querySelector('.show-list-muc-modal').click();
  4212. mock.closeControlBox(_converse);
  4213. const modal = _converse.api.modal.get('muc-list-modal');
  4214. await u.waitUntil(() => u.isVisible(modal.el), 1000);
  4215. spyOn(_converse.ChatRoom.prototype, 'getDiscoInfo').and.callFake(() => Promise.resolve());
  4216. roomspanel.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  4217. // See: https://xmpp.org/extensions/xep-0045.html#disco-rooms
  4218. expect(modal.el.querySelectorAll('.available-chatrooms li').length).toBe(0);
  4219. const server_input = modal.el.querySelector('input[name="server"]');
  4220. expect(server_input.placeholder).toBe('conference.example.org');
  4221. server_input.value = 'chat.shakespeare.lit';
  4222. modal.el.querySelector('input[type="submit"]').click();
  4223. await u.waitUntil(() => _converse.chatboxes.length);
  4224. const IQ_stanzas = _converse.connection.IQ_stanzas;
  4225. const sent_stanza = await u.waitUntil(
  4226. () => IQ_stanzas.filter(s => sizzle(`query[xmlns="${Strophe.NS.DISCO_ITEMS}"]`, s).length).pop()
  4227. );
  4228. const id = sent_stanza.getAttribute('id');
  4229. expect(Strophe.serialize(sent_stanza)).toBe(
  4230. `<iq from="romeo@montague.lit/orchard" id="${id}" `+
  4231. `to="chat.shakespeare.lit" `+
  4232. `type="get" `+
  4233. `xmlns="jabber:client">`+
  4234. `<query xmlns="http://jabber.org/protocol/disco#items"/>`+
  4235. `</iq>`
  4236. );
  4237. const iq = $iq({
  4238. 'from':'muc.montague.lit',
  4239. 'to':'romeo@montague.lit/pda',
  4240. 'id': id,
  4241. 'type':'result'
  4242. }).c('query')
  4243. .c('item', { jid:'heath@chat.shakespeare.lit', name:'A Lonely Heath'}).up()
  4244. .c('item', { jid:'coven@chat.shakespeare.lit', name:'A Dark Cave'}).up()
  4245. .c('item', { jid:'forres@chat.shakespeare.lit', name:'The Palace'}).up()
  4246. .c('item', { jid:'inverness@chat.shakespeare.lit', name:'Macbeth&apos;s Castle'}).up()
  4247. .c('item', { jid:'orchard@chat.shakespeare.lit', name:'Capulet\'s Orchard'}).up()
  4248. .c('item', { jid:'friar@chat.shakespeare.lit', name:'Friar Laurence\'s cell'}).up()
  4249. .c('item', { jid:'hall@chat.shakespeare.lit', name:'Hall in Capulet\'s house'}).up()
  4250. .c('item', { jid:'chamber@chat.shakespeare.lit', name:'Juliet\'s chamber'}).up()
  4251. .c('item', { jid:'public@chat.shakespeare.lit', name:'A public place'}).up()
  4252. .c('item', { jid:'street@chat.shakespeare.lit', name:'A street'}).nodeTree;
  4253. _converse.connection._dataRecv(mock.createRequest(iq));
  4254. await u.waitUntil(() => modal.el.querySelectorAll('.available-chatrooms li').length === 11);
  4255. const rooms = modal.el.querySelectorAll('.available-chatrooms li');
  4256. expect(rooms[0].textContent.trim()).toBe("Groupchats found");
  4257. expect(rooms[1].textContent.trim()).toBe("A Lonely Heath");
  4258. expect(rooms[2].textContent.trim()).toBe("A Dark Cave");
  4259. expect(rooms[3].textContent.trim()).toBe("The Palace");
  4260. expect(rooms[4].textContent.trim()).toBe("Macbeth's Castle");
  4261. expect(rooms[5].textContent.trim()).toBe('Capulet\'s Orchard');
  4262. expect(rooms[6].textContent.trim()).toBe('Friar Laurence\'s cell');
  4263. expect(rooms[7].textContent.trim()).toBe('Hall in Capulet\'s house');
  4264. expect(rooms[8].textContent.trim()).toBe('Juliet\'s chamber');
  4265. expect(rooms[9].textContent.trim()).toBe('A public place');
  4266. expect(rooms[10].textContent.trim()).toBe('A street');
  4267. rooms[4].querySelector('.open-room').click();
  4268. await u.waitUntil(() => _converse.chatboxes.length > 1);
  4269. expect(sizzle('.chatroom', _converse.el).filter(u.isVisible).length).toBe(1); // There should now be an open chatroom
  4270. var view = _converse.chatboxviews.get('inverness@chat.shakespeare.lit');
  4271. expect(view.querySelector('.chatbox-title__text').textContent.trim()).toBe("Macbeth's Castle");
  4272. done();
  4273. }));
  4274. it("is pre-filled with the muc_domain",
  4275. mock.initConverse(
  4276. ['chatBoxesFetched'],
  4277. {'muc_domain': 'muc.example.org'},
  4278. async function (done, _converse) {
  4279. await mock.openControlBox(_converse);
  4280. const roomspanel = _converse.chatboxviews.get('controlbox').querySelector('converse-rooms-list');
  4281. roomspanel.querySelector('.show-list-muc-modal').click();
  4282. mock.closeControlBox(_converse);
  4283. const modal = _converse.api.modal.get('muc-list-modal');
  4284. await u.waitUntil(() => u.isVisible(modal.el), 1000);
  4285. const server_input = modal.el.querySelector('input[name="server"]');
  4286. expect(server_input.value).toBe('muc.example.org');
  4287. done();
  4288. }));
  4289. it("doesn't let you set the MUC domain if it's locked",
  4290. mock.initConverse(
  4291. ['chatBoxesFetched'],
  4292. {'muc_domain': 'chat.shakespeare.lit', 'locked_muc_domain': true},
  4293. async function (done, _converse) {
  4294. await mock.openControlBox(_converse);
  4295. const roomspanel = _converse.chatboxviews.get('controlbox').querySelector('converse-rooms-list');
  4296. roomspanel.querySelector('.show-list-muc-modal').click();
  4297. mock.closeControlBox(_converse);
  4298. const modal = _converse.api.modal.get('muc-list-modal');
  4299. await u.waitUntil(() => u.isVisible(modal.el), 1000);
  4300. spyOn(_converse.ChatRoom.prototype, 'getDiscoInfo').and.callFake(() => Promise.resolve());
  4301. roomspanel.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  4302. expect(modal.el.querySelector('input[name="server"]')).toBe(null);
  4303. expect(modal.el.querySelector('input[type="submit"]')).toBe(null);
  4304. await u.waitUntil(() => _converse.chatboxes.length);
  4305. const sent_stanza = await u.waitUntil(() =>
  4306. _converse.connection.sent_stanzas.filter(
  4307. s => sizzle(`query[xmlns="http://jabber.org/protocol/disco#items"]`, s).length).pop()
  4308. );
  4309. expect(Strophe.serialize(sent_stanza)).toBe(
  4310. `<iq from="romeo@montague.lit/orchard" id="${sent_stanza.getAttribute('id')}" `+
  4311. `to="chat.shakespeare.lit" type="get" xmlns="jabber:client">`+
  4312. `<query xmlns="http://jabber.org/protocol/disco#items"/>`+
  4313. `</iq>`
  4314. );
  4315. const iq = $iq({
  4316. from:'muc.montague.lit',
  4317. to:'romeo@montague.lit/pda',
  4318. id: sent_stanza.getAttribute('id'),
  4319. type:'result'
  4320. }).c('query')
  4321. .c('item', { jid:'heath@chat.shakespeare.lit', name:'A Lonely Heath'}).up()
  4322. .c('item', { jid:'coven@chat.shakespeare.lit', name:'A Dark Cave'}).up()
  4323. .c('item', { jid:'forres@chat.shakespeare.lit', name:'The Palace'}).up()
  4324. _converse.connection._dataRecv(mock.createRequest(iq));
  4325. await u.waitUntil(() => modal.el.querySelectorAll('.available-chatrooms li').length === 4);
  4326. const rooms = modal.el.querySelectorAll('.available-chatrooms li');
  4327. expect(rooms[0].textContent.trim()).toBe("Groupchats found");
  4328. expect(rooms[1].textContent.trim()).toBe("A Lonely Heath");
  4329. expect(rooms[2].textContent.trim()).toBe("A Dark Cave");
  4330. expect(rooms[3].textContent.trim()).toBe("The Palace");
  4331. done();
  4332. }));
  4333. });
  4334. describe("The \"Groupchats\" section", function () {
  4335. it("shows the number of unread mentions received",
  4336. mock.initConverse(
  4337. [], {'allow_bookmarks': false},
  4338. async function (done, _converse) {
  4339. await mock.openControlBox(_converse);
  4340. const roomspanel = _converse.chatboxviews.get('controlbox').querySelector('converse-rooms-list');
  4341. expect(roomspanel.querySelectorAll('.available-room').length).toBe(0);
  4342. const muc_jid = 'kitchen@conference.shakespeare.lit';
  4343. const message = 'fires: Your attention is required';
  4344. await mock.openAndEnterChatRoom(_converse, muc_jid, 'fires');
  4345. const view = _converse.api.chatviews.get(muc_jid);
  4346. await u.waitUntil(() => roomspanel.querySelectorAll('.available-room').length);
  4347. expect(roomspanel.querySelectorAll('.available-room').length).toBe(1);
  4348. expect(roomspanel.querySelectorAll('.msgs-indicator').length).toBe(0);
  4349. view.model.set({'minimized': true});
  4350. const nick = mock.chatroom_names[0];
  4351. await view.model.handleMessageStanza($msg({
  4352. from: muc_jid+'/'+nick,
  4353. id: u.getUniqueId(),
  4354. to: 'romeo@montague.lit',
  4355. type: 'groupchat'
  4356. }).c('body').t(message).tree());
  4357. await u.waitUntil(() => view.model.messages.length);
  4358. expect(roomspanel.querySelectorAll('.available-room').length).toBe(1);
  4359. expect(roomspanel.querySelectorAll('.msgs-indicator').length).toBe(1);
  4360. expect(roomspanel.querySelector('.msgs-indicator').textContent.trim()).toBe('1');
  4361. await view.model.handleMessageStanza($msg({
  4362. 'from': muc_jid+'/'+nick,
  4363. 'id': u.getUniqueId(),
  4364. 'to': 'romeo@montague.lit',
  4365. 'type': 'groupchat'
  4366. }).c('body').t(message).tree());
  4367. await u.waitUntil(() => view.model.messages.length > 1);
  4368. expect(roomspanel.querySelectorAll('.available-room').length).toBe(1);
  4369. expect(roomspanel.querySelectorAll('.msgs-indicator').length).toBe(1);
  4370. expect(roomspanel.querySelector('.msgs-indicator').textContent.trim()).toBe('2');
  4371. view.model.set({'minimized': false});
  4372. expect(roomspanel.querySelectorAll('.available-room').length).toBe(1);
  4373. expect(roomspanel.querySelectorAll('.msgs-indicator').length).toBe(0);
  4374. done();
  4375. }));
  4376. });
  4377. describe("A XEP-0085 Chat Status Notification", function () {
  4378. it("is is not sent out to a MUC if the user is a visitor in a moderated room",
  4379. mock.initConverse(
  4380. ['chatBoxesFetched'], {},
  4381. async function (done, _converse) {
  4382. spyOn(_converse.ChatRoom.prototype, 'sendChatState').and.callThrough();
  4383. const muc_jid = 'lounge@montague.lit';
  4384. const features = [
  4385. 'http://jabber.org/protocol/muc',
  4386. 'jabber:iq:register',
  4387. 'muc_passwordprotected',
  4388. 'muc_hidden',
  4389. 'muc_temporary',
  4390. 'muc_membersonly',
  4391. 'muc_moderated',
  4392. 'muc_anonymous'
  4393. ]
  4394. await mock.openAndEnterChatRoom(_converse, muc_jid, 'romeo', features);
  4395. const view = _converse.api.chatviews.get(muc_jid);
  4396. view.model.setChatState(_converse.ACTIVE);
  4397. expect(view.model.sendChatState).toHaveBeenCalled();
  4398. const last_stanza = _converse.connection.sent_stanzas.pop();
  4399. expect(Strophe.serialize(last_stanza)).toBe(
  4400. `<message to="lounge@montague.lit" type="groupchat" xmlns="jabber:client">`+
  4401. `<active xmlns="http://jabber.org/protocol/chatstates"/>`+
  4402. `<no-store xmlns="urn:xmpp:hints"/>`+
  4403. `<no-permanent-store xmlns="urn:xmpp:hints"/>`+
  4404. `</message>`);
  4405. // Romeo loses his voice
  4406. const presence = $pres({
  4407. to: 'romeo@montague.lit/orchard',
  4408. from: `${muc_jid}/romeo`
  4409. }).c('x', {xmlns: Strophe.NS.MUC_USER})
  4410. .c('item', {'affiliation': 'none', 'role': 'visitor'}).up()
  4411. .c('status', {code: '110'});
  4412. _converse.connection._dataRecv(mock.createRequest(presence));
  4413. const occupant = view.model.occupants.findWhere({'jid': _converse.bare_jid});
  4414. await u.waitUntil(() => occupant.get('role') === 'visitor');
  4415. spyOn(_converse.connection, 'send');
  4416. view.model.setChatState(_converse.INACTIVE);
  4417. expect(view.model.sendChatState.calls.count()).toBe(2);
  4418. expect(_converse.connection.send).not.toHaveBeenCalled();
  4419. done();
  4420. }));
  4421. describe("A composing notification", function () {
  4422. it("will be shown if received", mock.initConverse([], {}, async function (done, _converse) {
  4423. const muc_jid = 'coven@chat.shakespeare.lit';
  4424. const members = [
  4425. {'affiliation': 'member', 'nick': 'majortom', 'jid': 'majortom@example.org'},
  4426. {'affiliation': 'admin', 'nick': 'groundcontrol', 'jid': 'groundcontrol@example.org'}
  4427. ];
  4428. await mock.openAndEnterChatRoom(_converse, muc_jid, 'some1', [], members);
  4429. const view = _converse.api.chatviews.get(muc_jid);
  4430. let csntext = await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent);
  4431. expect(csntext.trim()).toEqual("some1 has entered the groupchat");
  4432. let presence = $pres({
  4433. to: 'romeo@montague.lit/_converse.js-29092160',
  4434. from: 'coven@chat.shakespeare.lit/newguy'
  4435. })
  4436. .c('x', {xmlns: Strophe.NS.MUC_USER})
  4437. .c('item', {
  4438. 'affiliation': 'none',
  4439. 'jid': 'newguy@montague.lit/_converse.js-290929789',
  4440. 'role': 'participant'
  4441. });
  4442. _converse.connection._dataRecv(mock.createRequest(presence));
  4443. await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent.trim() ===
  4444. "some1 and newguy have entered the groupchat");
  4445. presence = $pres({
  4446. to: 'romeo@montague.lit/_converse.js-29092160',
  4447. from: 'coven@chat.shakespeare.lit/nomorenicks'
  4448. })
  4449. .c('x', {xmlns: Strophe.NS.MUC_USER})
  4450. .c('item', {
  4451. 'affiliation': 'none',
  4452. 'jid': 'nomorenicks@montague.lit/_converse.js-290929789',
  4453. 'role': 'participant'
  4454. });
  4455. _converse.connection._dataRecv(mock.createRequest(presence));
  4456. await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent.trim() ===
  4457. "some1, newguy and nomorenicks have entered the groupchat", 1000);
  4458. // Manually clear so that we can more easily test
  4459. view.model.notifications.set('entered', []);
  4460. await u.waitUntil(() => !view.querySelector('.chat-content__notifications').textContent, 1000);
  4461. // See XEP-0085 https://xmpp.org/extensions/xep-0085.html#definitions
  4462. const remove_notifications_timeouts = [];
  4463. const setTimeout = window.setTimeout;
  4464. spyOn(window, 'setTimeout').and.callFake((f, w) => {
  4465. if (f.toString() === "() => this.removeNotification(actor, state)") {
  4466. remove_notifications_timeouts.push(f)
  4467. }
  4468. setTimeout(f, w);
  4469. });
  4470. // <composing> state
  4471. let msg = $msg({
  4472. from: muc_jid+'/newguy',
  4473. id: u.getUniqueId(),
  4474. to: 'romeo@montague.lit',
  4475. type: 'groupchat'
  4476. }).c('body').c('composing', {'xmlns': Strophe.NS.CHATSTATES}).tree();
  4477. _converse.connection._dataRecv(mock.createRequest(msg));
  4478. csntext = await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent, 1000);
  4479. expect(csntext.trim()).toEqual('newguy is typing');
  4480. expect(remove_notifications_timeouts.length).toBe(1);
  4481. expect(view.querySelector('.chat-content__notifications').textContent.trim()).toEqual('newguy is typing');
  4482. // <composing> state for a different occupant
  4483. msg = $msg({
  4484. from: muc_jid+'/nomorenicks',
  4485. id: u.getUniqueId(),
  4486. to: 'romeo@montague.lit',
  4487. type: 'groupchat'
  4488. }).c('body').c('composing', {'xmlns': Strophe.NS.CHATSTATES}).tree();
  4489. await view.model.handleMessageStanza(msg);
  4490. await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent.trim() === 'newguy and nomorenicks are typing', 1000);
  4491. // <composing> state for a different occupant
  4492. msg = $msg({
  4493. from: muc_jid+'/majortom',
  4494. id: u.getUniqueId(),
  4495. to: 'romeo@montague.lit',
  4496. type: 'groupchat'
  4497. }).c('body').c('composing', {'xmlns': Strophe.NS.CHATSTATES}).tree();
  4498. await view.model.handleMessageStanza(msg);
  4499. await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent.trim() === 'newguy, nomorenicks and majortom are typing', 1000);
  4500. // <composing> state for a different occupant
  4501. msg = $msg({
  4502. from: muc_jid+'/groundcontrol',
  4503. id: u.getUniqueId(),
  4504. to: 'romeo@montague.lit',
  4505. type: 'groupchat'
  4506. }).c('body').c('composing', {'xmlns': Strophe.NS.CHATSTATES}).tree();
  4507. await view.model.handleMessageStanza(msg);
  4508. await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent.trim() === 'newguy, nomorenicks and others are typing', 1000);
  4509. msg = $msg({
  4510. from: `${muc_jid}/some1`,
  4511. id: u.getUniqueId(),
  4512. to: 'romeo@montague.lit',
  4513. type: 'groupchat'
  4514. }).c('body').t('hello world').tree();
  4515. await view.model.handleMessageStanza(msg);
  4516. await u.waitUntil(() => view.querySelectorAll('.chat-msg').length === 1);
  4517. expect(view.querySelector('.chat-msg .chat-msg__text').textContent.trim()).toBe('hello world');
  4518. // Test that the composing notifications get removed via timeout.
  4519. if (remove_notifications_timeouts.length) {
  4520. remove_notifications_timeouts[0]();
  4521. }
  4522. await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent.trim() === 'nomorenicks, majortom and groundcontrol are typing', 1000);
  4523. done();
  4524. }));
  4525. });
  4526. describe("A paused notification", function () {
  4527. it("will be shown if received", mock.initConverse(['chatBoxesFetched'], {}, async function (done, _converse) {
  4528. const muc_jid = 'coven@chat.shakespeare.lit';
  4529. await mock.openAndEnterChatRoom(_converse, muc_jid, 'some1');
  4530. const view = _converse.chatboxviews.get('coven@chat.shakespeare.lit');
  4531. /* <presence to="romeo@montague.lit/_converse.js-29092160"
  4532. * from="coven@chat.shakespeare.lit/some1">
  4533. * <x xmlns="http://jabber.org/protocol/muc#user">
  4534. * <item affiliation="owner" jid="romeo@montague.lit/_converse.js-29092160" role="moderator"/>
  4535. * <status code="110"/>
  4536. * </x>
  4537. * </presence></body>
  4538. */
  4539. let presence = $pres({
  4540. to: 'romeo@montague.lit/_converse.js-29092160',
  4541. from: 'coven@chat.shakespeare.lit/some1'
  4542. }).c('x', {xmlns: Strophe.NS.MUC_USER})
  4543. .c('item', {
  4544. 'affiliation': 'owner',
  4545. 'jid': 'romeo@montague.lit/_converse.js-29092160',
  4546. 'role': 'moderator'
  4547. }).up()
  4548. .c('status', {code: '110'});
  4549. _converse.connection._dataRecv(mock.createRequest(presence));
  4550. const csntext = await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent);
  4551. expect(csntext.trim()).toEqual("some1 has entered the groupchat");
  4552. presence = $pres({
  4553. to: 'romeo@montague.lit/_converse.js-29092160',
  4554. from: 'coven@chat.shakespeare.lit/newguy'
  4555. })
  4556. .c('x', {xmlns: Strophe.NS.MUC_USER})
  4557. .c('item', {
  4558. 'affiliation': 'none',
  4559. 'jid': 'newguy@montague.lit/_converse.js-290929789',
  4560. 'role': 'participant'
  4561. });
  4562. _converse.connection._dataRecv(mock.createRequest(presence));
  4563. await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent.trim() ===
  4564. "some1 and newguy have entered the groupchat");
  4565. presence = $pres({
  4566. to: 'romeo@montague.lit/_converse.js-29092160',
  4567. from: 'coven@chat.shakespeare.lit/nomorenicks'
  4568. })
  4569. .c('x', {xmlns: Strophe.NS.MUC_USER})
  4570. .c('item', {
  4571. 'affiliation': 'none',
  4572. 'jid': 'nomorenicks@montague.lit/_converse.js-290929789',
  4573. 'role': 'participant'
  4574. });
  4575. _converse.connection._dataRecv(mock.createRequest(presence));
  4576. await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent.trim() ===
  4577. "some1, newguy and nomorenicks have entered the groupchat");
  4578. // Manually clear so that we can more easily test
  4579. view.model.notifications.set('entered', []);
  4580. await u.waitUntil(() => !view.querySelector('.chat-content__notifications').textContent);
  4581. // See XEP-0085 https://xmpp.org/extensions/xep-0085.html#definitions
  4582. // <composing> state
  4583. var msg = $msg({
  4584. from: muc_jid+'/newguy',
  4585. id: u.getUniqueId(),
  4586. to: 'romeo@montague.lit',
  4587. type: 'groupchat'
  4588. }).c('body').c('composing', {'xmlns': Strophe.NS.CHATSTATES}).tree();
  4589. await view.model.handleMessageStanza(msg);
  4590. await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent);
  4591. expect(view.querySelector('.chat-content__notifications').textContent.trim()).toBe('newguy is typing');
  4592. // <composing> state for a different occupant
  4593. msg = $msg({
  4594. from: muc_jid+'/nomorenicks',
  4595. id: u.getUniqueId(),
  4596. to: 'romeo@montague.lit',
  4597. type: 'groupchat'
  4598. }).c('body').c('composing', {'xmlns': Strophe.NS.CHATSTATES}).tree();
  4599. await view.model.handleMessageStanza(msg);
  4600. await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent.trim() == 'newguy and nomorenicks are typing');
  4601. // <paused> state from occupant who typed first
  4602. msg = $msg({
  4603. from: muc_jid+'/newguy',
  4604. id: u.getUniqueId(),
  4605. to: 'romeo@montague.lit',
  4606. type: 'groupchat'
  4607. }).c('body').c('paused', {'xmlns': Strophe.NS.CHATSTATES}).tree();
  4608. await view.model.handleMessageStanza(msg);
  4609. await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent.trim() == 'nomorenicks is typing\nnewguy has stopped typing');
  4610. done();
  4611. }));
  4612. });
  4613. });
  4614. describe("A muted user", function () {
  4615. it("will receive a user-friendly error message when trying to send a message",
  4616. mock.initConverse(['chatBoxesFetched'], {}, async function (done, _converse) {
  4617. const muc_jid = 'trollbox@montague.lit';
  4618. await mock.openAndEnterChatRoom(_converse, muc_jid, 'troll');
  4619. const view = _converse.api.chatviews.get(muc_jid);
  4620. const textarea = await u.waitUntil(() => view.querySelector('textarea.chat-textarea'));
  4621. textarea.value = 'Hello world';
  4622. const bottom_panel = view.querySelector('converse-muc-bottom-panel');
  4623. bottom_panel.onFormSubmitted(new Event('submit'));
  4624. await new Promise(resolve => view.model.messages.once('rendered', resolve));
  4625. let stanza = u.toStanza(`
  4626. <message id="${view.model.messages.at(0).get('msgid')}"
  4627. xmlns="jabber:client"
  4628. type="error"
  4629. to="troll@montague.lit/resource"
  4630. from="trollbox@montague.lit">
  4631. <error type="auth"><forbidden xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"/></error>
  4632. </message>`);
  4633. _converse.connection._dataRecv(mock.createRequest(stanza));
  4634. await u.waitUntil(() => view.querySelector('.chat-msg__error')?.textContent.trim(), 1000);
  4635. expect(view.querySelector('.chat-msg__error').textContent.trim()).toBe(
  4636. "Your message was not delivered because you weren't allowed to send it.");
  4637. textarea.value = 'Hello again';
  4638. bottom_panel.onFormSubmitted(new Event('submit'));
  4639. await u.waitUntil(() => view.querySelectorAll('.chat-msg__text').length === 2);
  4640. stanza = u.toStanza(`
  4641. <message id="${view.model.messages.at(1).get('msgid')}"
  4642. xmlns="jabber:client"
  4643. type="error"
  4644. to="troll@montague.lit/resource"
  4645. from="trollbox@montague.lit">
  4646. <error type="auth">
  4647. <forbidden xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"/>
  4648. <text xmlns="urn:ietf:params:xml:ns:xmpp-stanzas">Thou shalt not!</text>
  4649. </error>
  4650. </message>`);
  4651. _converse.connection._dataRecv(mock.createRequest(stanza));
  4652. await u.waitUntil(() => view.querySelectorAll('.chat-msg__error').length === 2);
  4653. const sel = 'converse-message-history converse-chat-message:last-child .chat-msg__error';
  4654. await u.waitUntil(() => view.querySelector(sel)?.textContent.trim());
  4655. expect(view.querySelector(sel).textContent.trim()).toBe('Thou shalt not!')
  4656. done();
  4657. }));
  4658. it("will see an explanatory message instead of a textarea",
  4659. mock.initConverse(['chatBoxesFetched'], {}, async function (done, _converse) {
  4660. const features = [
  4661. 'http://jabber.org/protocol/muc',
  4662. 'jabber:iq:register',
  4663. Strophe.NS.SID,
  4664. 'muc_moderated',
  4665. ]
  4666. const muc_jid = 'trollbox@montague.lit';
  4667. await mock.openAndEnterChatRoom(_converse, muc_jid, 'troll', features);
  4668. const view = _converse.api.chatviews.get(muc_jid);
  4669. await u.waitUntil(() => view.querySelector('.chat-textarea'));
  4670. let stanza = u.toStanza(`
  4671. <presence
  4672. from='trollbox@montague.lit/troll'
  4673. to='romeo@montague.lit/orchard'>
  4674. <x xmlns='http://jabber.org/protocol/muc#user'>
  4675. <item affiliation='none'
  4676. nick='troll'
  4677. role='visitor'/>
  4678. <status code='110'/>
  4679. </x>
  4680. </presence>`);
  4681. _converse.connection._dataRecv(mock.createRequest(stanza));
  4682. await u.waitUntil(() => view.querySelector('.chat-textarea') === null);
  4683. let bottom_panel = view.querySelector('.muc-bottom-panel');
  4684. expect(bottom_panel.textContent.trim()).toBe("You're not allowed to send messages in this room");
  4685. // This only applies to moderated rooms, so let's check that
  4686. // the textarea becomes visible when the room's
  4687. // configuration changes to be non-moderated
  4688. view.model.features.set('moderated', false);
  4689. await u.waitUntil(() => view.querySelector('.muc-bottom-panel') === null);
  4690. const textarea = await u.waitUntil(() => view.querySelector('textarea.chat-textarea'));
  4691. expect(textarea === null).toBe(false);
  4692. view.model.features.set('moderated', true);
  4693. await u.waitUntil(() => view.querySelector('.chat-textarea') === null);
  4694. bottom_panel = view.querySelector('.muc-bottom-panel');
  4695. expect(bottom_panel.textContent.trim()).toBe("You're not allowed to send messages in this room");
  4696. // Check now that things get restored when the user is given a voice
  4697. await u.waitUntil(() =>
  4698. Array.from(view.querySelectorAll('.chat-info__message')).pop()?.textContent.trim() ===
  4699. "troll is no longer an owner of this groupchat"
  4700. );
  4701. stanza = u.toStanza(`
  4702. <presence
  4703. from='trollbox@montague.lit/troll'
  4704. to='romeo@montague.lit/orchard'>
  4705. <x xmlns='http://jabber.org/protocol/muc#user'>
  4706. <item affiliation='none'
  4707. nick='troll'
  4708. role='participant'/>
  4709. <status code='110'/>
  4710. </x>
  4711. </presence>`);
  4712. _converse.connection._dataRecv(mock.createRequest(stanza));
  4713. await u.waitUntil(() => view.querySelector('.muc-bottom-panel') === null);
  4714. expect(textarea === null).toBe(false);
  4715. // Check now that things get restored when the user is given a voice
  4716. await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent.trim() === "troll has been given a voice");
  4717. done();
  4718. }));
  4719. });
  4720. describe("when muc_send_probes is true", function () {
  4721. it("sends presence probes when muc_send_probes is true",
  4722. mock.initConverse([], {'muc_send_probes': true}, async function (done, _converse) {
  4723. const muc_jid = 'lounge@montague.lit';
  4724. await mock.openAndEnterChatRoom(_converse, muc_jid, 'romeo');
  4725. let stanza = u.toStanza(`
  4726. <message xmlns="jabber:client" to="${_converse.jid}" type="groupchat" from="${muc_jid}/ralphm">
  4727. <body>This message will trigger a presence probe</body>
  4728. </message>`);
  4729. _converse.connection._dataRecv(mock.createRequest(stanza));
  4730. const view = _converse.chatboxviews.get(muc_jid);
  4731. await u.waitUntil(() => view.model.messages.length);
  4732. let occupant = view.model.messages.at(0)?.occupant;
  4733. expect(occupant).toBeDefined();
  4734. expect(occupant.get('nick')).toBe('ralphm');
  4735. expect(occupant.get('affiliation')).toBeUndefined();
  4736. expect(occupant.get('role')).toBeUndefined();
  4737. const sent_stanzas = _converse.connection.sent_stanzas;
  4738. let probe = await u.waitUntil(() => sent_stanzas.filter(s => s.matches('presence[type="probe"]')).pop());
  4739. expect(Strophe.serialize(probe)).toBe(
  4740. `<presence to="${muc_jid}/ralphm" type="probe" xmlns="jabber:client">`+
  4741. `<priority>0</priority>`+
  4742. `<c hash="sha-1" node="https://conversejs.org" ver="PxXfr6uz8ClMWIga0OB/MhKNH/M=" xmlns="http://jabber.org/protocol/caps"/>`+
  4743. `</presence>`);
  4744. let presence = u.toStanza(
  4745. `<presence xmlns="jabber:client" to="${converse.jid}" from="${muc_jid}/ralphm">
  4746. <x xmlns="http://jabber.org/protocol/muc#user">
  4747. <item affiliation="member" jid="ralph@example.org/Conversations.ZvLu" role="participant"/>
  4748. </x>
  4749. </presence>`);
  4750. _converse.connection._dataRecv(mock.createRequest(presence));
  4751. expect(occupant.get('affiliation')).toBe('member');
  4752. expect(occupant.get('role')).toBe('participant');
  4753. // Check that unavailable but affiliated occupants don't get destroyed
  4754. stanza = u.toStanza(`
  4755. <message xmlns="jabber:client" to="${_converse.jid}" type="groupchat" from="${muc_jid}/gonePhising">
  4756. <body>This message from an unavailable user will trigger a presence probe</body>
  4757. </message>`);
  4758. _converse.connection._dataRecv(mock.createRequest(stanza));
  4759. await u.waitUntil(() => view.model.messages.length === 2);
  4760. occupant = view.model.messages.at(1)?.occupant;
  4761. expect(occupant).toBeDefined();
  4762. expect(occupant.get('nick')).toBe('gonePhising');
  4763. expect(occupant.get('affiliation')).toBeUndefined();
  4764. expect(occupant.get('role')).toBeUndefined();
  4765. probe = await u.waitUntil(() => sent_stanzas.filter(s => s.matches(`presence[to="${muc_jid}/gonePhising"]`)).pop());
  4766. expect(Strophe.serialize(probe)).toBe(
  4767. `<presence to="${muc_jid}/gonePhising" type="probe" xmlns="jabber:client">`+
  4768. `<priority>0</priority>`+
  4769. `<c hash="sha-1" node="https://conversejs.org" ver="PxXfr6uz8ClMWIga0OB/MhKNH/M=" xmlns="http://jabber.org/protocol/caps"/>`+
  4770. `</presence>`);
  4771. presence = u.toStanza(
  4772. `<presence xmlns="jabber:client" type="unavailable" to="${converse.jid}" from="${muc_jid}/gonePhising">
  4773. <x xmlns="http://jabber.org/protocol/muc#user">
  4774. <item affiliation="member" jid="gonePhishing@example.org/d34dBEEF" role="participant"/>
  4775. </x>
  4776. </presence>`);
  4777. _converse.connection._dataRecv(mock.createRequest(presence));
  4778. expect(view.model.occupants.length).toBe(3);
  4779. expect(occupant.get('affiliation')).toBe('member');
  4780. expect(occupant.get('role')).toBe('participant');
  4781. done();
  4782. }));
  4783. });
  4784. });