chatroom.js 235 KB

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