2
0

chatroom.js 234 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173
  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. async function (done, _converse) {
  1422. await test_utils.openChatRoomViaModal(_converse, 'problematic@muc.localhost', 'dummy')
  1423. const presence = $pres().attrs({
  1424. from:'problematic@muc.localhost',
  1425. id:'n13mt3l',
  1426. to:'dummy@localhost/pda',
  1427. type:'error'})
  1428. .c('error').attrs({'type':'cancel'})
  1429. .c('gone').attrs({'xmlns':'urn:ietf:params:xml:ns:xmpp-stanzas'})
  1430. .t('xmpp:other-room@chat.jabberfr.org?join').up()
  1431. .c('text').attrs({'xmlns':'urn:ietf:params:xml:ns:xmpp-stanzas'})
  1432. .t("We didn't like the name").nodeTree;
  1433. const view = _converse.chatboxviews.get('problematic@muc.localhost');
  1434. spyOn(view, 'showErrorMessage').and.callThrough();
  1435. _converse.connection._dataRecv(test_utils.createRequest(presence));
  1436. expect(view.el.querySelector('.chatroom-body .disconnect-msg').textContent)
  1437. .toBe('This room no longer exists');
  1438. expect(view.el.querySelector('.chatroom-body .destroyed-reason').textContent)
  1439. .toBe(`"We didn't like the name"`);
  1440. expect(view.el.querySelector('.chatroom-body .moved-label').textContent.trim())
  1441. .toBe('The conversation has moved. Click below to enter.');
  1442. expect(view.el.querySelector('.chatroom-body .moved-link').textContent.trim())
  1443. .toBe(`other-room@chat.jabberfr.org`);
  1444. done();
  1445. }));
  1446. it("will use the user's reserved nickname, if it exists",
  1447. mock.initConverseWithPromises(
  1448. null, ['rosterGroupsFetched', 'chatBoxesFetched'], {},
  1449. function (done, _converse) {
  1450. let sent_IQ, IQ_id, view;
  1451. const IQ_stanzas = _converse.connection.IQ_stanzas;
  1452. const sendIQ = _converse.connection.sendIQ;
  1453. const room_jid = 'lounge@localhost';
  1454. spyOn(_converse.connection, 'sendIQ').and.callFake(function (iq, callback, errback) {
  1455. if (iq.nodeTree.getAttribute('to') === 'lounge@localhost') {
  1456. sent_IQ = iq;
  1457. IQ_id = sendIQ.bind(this)(iq, callback, errback);
  1458. } else {
  1459. sendIQ.bind(this)(iq, callback, errback);
  1460. }
  1461. });
  1462. test_utils.openChatRoom(_converse, 'lounge', 'localhost', 'dummy')
  1463. .then(() => {
  1464. return test_utils.waitUntil(() => _.get(_.filter(
  1465. IQ_stanzas,
  1466. iq => iq.nodeTree.querySelector(
  1467. `iq[to="${room_jid}"] query[xmlns="http://jabber.org/protocol/disco#info"]`
  1468. )).pop(), 'nodeTree'));
  1469. }).then(stanza => {
  1470. // We pretend this is a new room, so no disco info is returned.
  1471. var features_stanza = $iq({
  1472. from: 'lounge@localhost',
  1473. 'id': stanza.getAttribute('id'),
  1474. 'to': 'dummy@localhost/desktop',
  1475. 'type': 'error'
  1476. }).c('error', {'type': 'cancel'})
  1477. .c('item-not-found', {'xmlns': "urn:ietf:params:xml:ns:xmpp-stanzas"});
  1478. _converse.connection._dataRecv(test_utils.createRequest(features_stanza));
  1479. view = _converse.chatboxviews.get('lounge@localhost');
  1480. spyOn(view, 'join').and.callThrough();
  1481. /* <iq from='hag66@shakespeare.lit/pda'
  1482. * id='getnick1'
  1483. * to='coven@chat.shakespeare.lit'
  1484. * type='get'>
  1485. * <query xmlns='http://jabber.org/protocol/disco#info'
  1486. * node='x-roomuser-item'/>
  1487. * </iq>
  1488. */
  1489. return test_utils.waitUntil(() => _.filter(
  1490. IQ_stanzas,
  1491. s => sizzle(`iq[to="${room_jid}"] query[node="x-roomuser-item"]`, s.nodeTree).length
  1492. ).pop()
  1493. );
  1494. }).then(node => {
  1495. const iq = node.nodeTree;
  1496. expect(node.toLocaleString()).toBe(
  1497. `<iq from="dummy@localhost/resource" id="${iq.getAttribute('id')}" to="lounge@localhost" `+
  1498. `type="get" xmlns="jabber:client">`+
  1499. `<query node="x-roomuser-item" xmlns="http://jabber.org/protocol/disco#info"/></iq>`);
  1500. /* <iq from='coven@chat.shakespeare.lit'
  1501. * id='getnick1'
  1502. * to='hag66@shakespeare.lit/pda'
  1503. * type='result'>
  1504. * <query xmlns='http://jabber.org/protocol/disco#info'
  1505. * node='x-roomuser-item'>
  1506. * <identity
  1507. * category='conference'
  1508. * name='thirdwitch'
  1509. * type='text'/>
  1510. * </query>
  1511. * </iq>
  1512. */
  1513. var stanza = $iq({
  1514. 'type': 'result',
  1515. 'id': IQ_id,
  1516. 'from': view.model.get('jid'),
  1517. 'to': _converse.connection.jid
  1518. }).c('query', {'xmlns': 'http://jabber.org/protocol/disco#info', 'node': 'x-roomuser-item'})
  1519. .c('identity', {'category': 'conference', 'name': 'thirdwitch', 'type': 'text'});
  1520. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  1521. expect(view.join).toHaveBeenCalled();
  1522. // The user has just entered the groupchat (because join was called)
  1523. // and receives their own presence from the server.
  1524. // See example 24:
  1525. // http://xmpp.org/extensions/xep-0045.html#enter-pres
  1526. var presence = $pres({
  1527. to:'dummy@localhost/resource',
  1528. from:'lounge@localhost/thirdwitch',
  1529. id:'DC352437-C019-40EC-B590-AF29E879AF97'
  1530. }).c('x').attrs({xmlns:'http://jabber.org/protocol/muc#user'})
  1531. .c('item').attrs({
  1532. affiliation: 'member',
  1533. jid: 'dummy@localhost/resource',
  1534. role: 'participant'
  1535. }).up()
  1536. .c('status').attrs({code:'110'}).up()
  1537. .c('status').attrs({code:'210'}).nodeTree;
  1538. _converse.connection._dataRecv(test_utils.createRequest(presence));
  1539. const info_text = sizzle('.chat-content .chat-info:first', view.el).pop().textContent;
  1540. expect(info_text).toBe('Your nickname has been automatically set to thirdwitch');
  1541. done();
  1542. });
  1543. }));
  1544. it("allows the user to invite their roster contacts to enter the groupchat",
  1545. mock.initConverseWithPromises(
  1546. null, ['rosterGroupsFetched', 'chatBoxesFetched'], {},
  1547. function (done, _converse) {
  1548. test_utils.createContacts(_converse, 'current'); // We need roster contacts, so that we have someone to invite
  1549. // Since we don't actually fetch roster contacts, we need to
  1550. // cheat here and emit the event.
  1551. _converse.emit('rosterContactsFetched');
  1552. let view;
  1553. test_utils.openChatRoom(_converse, 'lounge', 'localhost', 'dummy')
  1554. .then(() => {
  1555. spyOn(_converse, 'emit');
  1556. spyOn(window, 'prompt').and.callFake(function () {
  1557. return "Please join!";
  1558. });
  1559. view = _converse.chatboxviews.get('lounge@localhost');
  1560. // XXX: cheating a lttle bit, normally this'll be set after
  1561. // receiving the features for the groupchat.
  1562. view.model.set('open', 'true');
  1563. spyOn(view.model, 'directInvite').and.callThrough();
  1564. const chat_area = view.el.querySelector('.chat-area');
  1565. chat_area.parentElement.removeChild(chat_area);
  1566. return test_utils.waitUntil(() => view.el.querySelectorAll('input.invited-contact').length)
  1567. }).then(function () {
  1568. const input = view.el.querySelector('input.invited-contact');
  1569. expect(input.getAttribute('placeholder')).toBe('Invite');
  1570. input.value = "Felix";
  1571. let evt = new Event('input');
  1572. input.dispatchEvent(evt);
  1573. let sent_stanza;
  1574. spyOn(_converse.connection, 'send').and.callFake(function (stanza) {
  1575. sent_stanza = stanza;
  1576. });
  1577. const hint = input.nextSibling.firstElementChild;
  1578. expect(input.value).toBe('Felix');
  1579. expect(hint.textContent).toBe('Felix Amsel');
  1580. expect(input.nextSibling.childNodes.length).toBe(1);
  1581. if (typeof(Event) === 'function') {
  1582. // Not working on PhantomJS
  1583. evt = new Event('mousedown', {'bubbles': true});
  1584. evt.button = 0; // For some reason awesomplete wants this
  1585. hint.dispatchEvent(evt);
  1586. expect(window.prompt).toHaveBeenCalled();
  1587. expect(view.model.directInvite).toHaveBeenCalled();
  1588. expect(sent_stanza.toLocaleString()).toBe(
  1589. `<message from="dummy@localhost/resource" `+
  1590. `id="${sent_stanza.nodeTree.getAttribute("id")}" `+
  1591. `to="felix.amsel@localhost" `+
  1592. `xmlns="jabber:client">`+
  1593. `<x jid="lounge@localhost" reason="Please join!" xmlns="jabber:x:conference"/>`+
  1594. `</message>`
  1595. );
  1596. }
  1597. done();
  1598. }).catch(_.partial(_converse.log, _, Strophe.LogLevel.FATAL));
  1599. }));
  1600. it("can be joined automatically, based upon a received invite",
  1601. mock.initConverseWithPromises(
  1602. null, ['rosterGroupsFetched'], {},
  1603. function (done, _converse) {
  1604. test_utils.createContacts(_converse, 'current'); // We need roster contacts, who can invite us
  1605. spyOn(window, 'confirm').and.callFake(function () {
  1606. return true;
  1607. });
  1608. test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy').then(function () {
  1609. var view = _converse.chatboxviews.get('lounge@localhost');
  1610. view.close(); // Hack, otherwise we have to mock stanzas.
  1611. var name = mock.cur_names[0];
  1612. var from_jid = name.replace(/ /g,'.').toLowerCase() + '@localhost';
  1613. var room_jid = 'lounge@localhost';
  1614. var reason = "Please join this groupchat";
  1615. expect(_converse.chatboxes.models.length).toBe(1);
  1616. expect(_converse.chatboxes.models[0].id).toBe("controlbox");
  1617. var stanza = Strophe.xmlHtmlNode(
  1618. '<message xmlns="jabber:client" to="'+_converse.bare_jid+'" from="'+from_jid+'" id="9bceb415-f34b-4fa4-80d5-c0d076a24231">'+
  1619. '<x xmlns="jabber:x:conference" jid="'+room_jid+'" reason="'+reason+'"/>'+
  1620. '</message>').firstChild;
  1621. _converse.onDirectMUCInvitation(stanza);
  1622. expect(window.confirm).toHaveBeenCalledWith(
  1623. name + ' has invited you to join a groupchat: '+ room_jid +
  1624. ', and left the following reason: "'+reason+'"');
  1625. expect(_converse.chatboxes.models.length).toBe(2);
  1626. expect(_converse.chatboxes.models[0].id).toBe('controlbox');
  1627. expect(_converse.chatboxes.models[1].id).toBe(room_jid);
  1628. done();
  1629. });
  1630. }));
  1631. it("shows received groupchat messages",
  1632. mock.initConverseWithPromises(
  1633. null, ['rosterGroupsFetched'], {},
  1634. async function (done, _converse) {
  1635. const text = 'This is a received message';
  1636. await test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy');
  1637. spyOn(_converse, 'emit');
  1638. const view = _converse.chatboxviews.get('lounge@localhost');
  1639. if (!view.el.querySelectorAll('.chat-area').length) {
  1640. view.renderChatArea();
  1641. }
  1642. var nick = mock.chatroom_names[0];
  1643. view.model.occupants.create({
  1644. 'nick': nick,
  1645. 'muc_jid': `${view.model.get('jid')}/${nick}`
  1646. });
  1647. const message = $msg({
  1648. from: 'lounge@localhost/'+nick,
  1649. id: '1',
  1650. to: 'dummy@localhost',
  1651. type: 'groupchat'
  1652. }).c('body').t(text);
  1653. view.model.onMessage(message.nodeTree);
  1654. await new Promise((resolve, reject) => view.once('messageInserted', resolve));
  1655. const chat_content = view.el.querySelector('.chat-content');
  1656. expect(chat_content.querySelectorAll('.chat-msg').length).toBe(1);
  1657. expect(chat_content.querySelector('.chat-msg__text').textContent).toBe(text);
  1658. expect(_converse.emit).toHaveBeenCalledWith('message', jasmine.any(Object));
  1659. done();
  1660. }));
  1661. it("shows sent groupchat messages",
  1662. mock.initConverseWithPromises(
  1663. null, ['rosterGroupsFetched'], {},
  1664. async function (done, _converse) {
  1665. await test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy');
  1666. spyOn(_converse, 'emit');
  1667. const view = _converse.chatboxviews.get('lounge@localhost');
  1668. if (!view.el.querySelectorAll('.chat-area').length) {
  1669. view.renderChatArea();
  1670. }
  1671. const text = 'This is a sent message';
  1672. const textarea = view.el.querySelector('.chat-textarea');
  1673. textarea.value = text;
  1674. view.keyPressed({
  1675. target: textarea,
  1676. preventDefault: _.noop,
  1677. keyCode: 13
  1678. });
  1679. await new Promise((resolve, reject) => view.once('messageInserted', resolve));
  1680. expect(_converse.emit).toHaveBeenCalledWith('messageSend', text);
  1681. const chat_content = view.el.querySelector('.chat-content');
  1682. expect(chat_content.querySelectorAll('.chat-msg').length).toBe(1);
  1683. // Let's check that if we receive the same message again, it's
  1684. // not shown.
  1685. const message = $msg({
  1686. from: 'lounge@localhost/dummy',
  1687. to: 'dummy@localhost.com',
  1688. type: 'groupchat',
  1689. id: view.model.messages.at(0).get('msgid')
  1690. }).c('body').t(text);
  1691. view.model.onMessage(message.nodeTree);
  1692. expect(chat_content.querySelectorAll('.chat-msg').length).toBe(1);
  1693. expect(sizzle('.chat-msg__text:last').pop().textContent).toBe(text);
  1694. expect(view.model.messages.length).toBe(1);
  1695. // We don't emit an event if it's our own message
  1696. expect(_converse.emit.calls.count(), 1);
  1697. done();
  1698. }));
  1699. it("will cause the chat area to be scrolled down only if it was at the bottom already",
  1700. mock.initConverseWithPromises(
  1701. null, ['rosterGroupsFetched'], {},
  1702. async function (done, _converse) {
  1703. var message = 'This message is received while the chat area is scrolled up';
  1704. await test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy');
  1705. var view = _converse.chatboxviews.get('lounge@localhost');
  1706. spyOn(view, 'scrollDown').and.callThrough();
  1707. // Create enough messages so that there's a scrollbar.
  1708. const promises = [];
  1709. for (var i=0; i<20; i++) {
  1710. view.model.onMessage(
  1711. $msg({
  1712. from: 'lounge@localhost/someone',
  1713. to: 'dummy@localhost.com',
  1714. type: 'groupchat',
  1715. id: (new Date()).getTime(),
  1716. }).c('body').t('Message: '+i).tree());
  1717. promises.push(new Promise((resolve, reject) => view.once('messageInserted', resolve)))
  1718. }
  1719. await Promise.all(promises);
  1720. // Give enough time for `markScrolled` to have been called
  1721. setTimeout(async () => {
  1722. view.content.scrollTop = 0;
  1723. view.model.onMessage(
  1724. $msg({
  1725. from: 'lounge@localhost/someone',
  1726. to: 'dummy@localhost.com',
  1727. type: 'groupchat',
  1728. id: (new Date()).getTime(),
  1729. }).c('body').t(message).tree());
  1730. await new Promise((resolve, reject) => view.once('messageInserted', resolve));
  1731. // Now check that the message appears inside the chatbox in the DOM
  1732. const chat_content = view.el.querySelector('.chat-content');
  1733. const msg_txt = sizzle('.chat-msg:last .chat-msg__text', chat_content).pop().textContent;
  1734. expect(msg_txt).toEqual(message);
  1735. expect(view.content.scrollTop).toBe(0);
  1736. done();
  1737. }, 500);
  1738. }));
  1739. it("shows the room topic in the header",
  1740. mock.initConverseWithPromises(
  1741. null, ['rosterGroupsFetched'], {},
  1742. function (done, _converse) {
  1743. test_utils.openAndEnterChatRoom(_converse, 'jdev', 'conference.jabber.org', 'jc').then(function () {
  1744. const text = 'Jabber/XMPP Development | RFCs and Extensions: http://xmpp.org/ | Protocol and XSF discussions: xsf@muc.xmpp.org';
  1745. const stanza = Strophe.xmlHtmlNode(
  1746. '<message xmlns="jabber:client" to="jc@opkode.com/_converse.js-60429116" type="groupchat" from="jdev@conference.jabber.org/ralphm">'+
  1747. ' <subject>'+text+'</subject>'+
  1748. ' <delay xmlns="urn:xmpp:delay" stamp="2014-02-04T09:35:39Z" from="jdev@conference.jabber.org"/>'+
  1749. ' <x xmlns="jabber:x:delay" stamp="20140204T09:35:39" from="jdev@conference.jabber.org"/>'+
  1750. '</message>').firstChild;
  1751. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  1752. const view = _converse.chatboxviews.get('jdev@conference.jabber.org');
  1753. const chat_content = view.el.querySelector('.chat-content');
  1754. expect(sizzle('.chat-event:last').pop().textContent).toBe('Topic set by ralphm');
  1755. expect(sizzle('.chat-topic:last').pop().textContent).toBe(text);
  1756. expect(view.el.querySelector('.chatroom-description').textContent).toBe(text);
  1757. done();
  1758. });
  1759. }));
  1760. it("escapes the subject before rendering it, to avoid JS-injection attacks",
  1761. mock.initConverseWithPromises(
  1762. null, ['rosterGroupsFetched'], {},
  1763. function (done, _converse) {
  1764. test_utils.openAndEnterChatRoom(_converse, 'jdev', 'conference.jabber.org', 'jc').then(function () {
  1765. spyOn(window, 'alert');
  1766. const subject = '<img src="x" onerror="alert(\'XSS\');"/>';
  1767. const view = _converse.chatboxviews.get('jdev@conference.jabber.org');
  1768. view.model.set({'subject': {
  1769. 'text': subject,
  1770. 'author': 'ralphm'
  1771. }});
  1772. const chat_content = view.el.querySelector('.chat-content');
  1773. expect(sizzle('.chat-event:last').pop().textContent).toBe('Topic set by ralphm');
  1774. expect(sizzle('.chat-topic:last').pop().textContent).toBe(subject);
  1775. done();
  1776. });
  1777. }));
  1778. it("informs users if their nicknames has been changed.",
  1779. mock.initConverseWithPromises(
  1780. null, ['rosterGroupsFetched'], {},
  1781. function (done, _converse) {
  1782. /* The service then sends two presence stanzas to the full JID
  1783. * of each occupant (including the occupant who is changing his
  1784. * or her room nickname), one of type "unavailable" for the old
  1785. * nickname and one indicating availability for the new
  1786. * nickname.
  1787. *
  1788. * See: http://xmpp.org/extensions/xep-0045.html#changenick
  1789. *
  1790. * <presence
  1791. * from='coven@localhost/thirdwitch'
  1792. * id='DC352437-C019-40EC-B590-AF29E879AF98'
  1793. * to='hag66@shakespeare.lit/pda'
  1794. * type='unavailable'>
  1795. * <x xmlns='http://jabber.org/protocol/muc#user'>
  1796. * <item affiliation='member'
  1797. * jid='hag66@shakespeare.lit/pda'
  1798. * nick='oldhag'
  1799. * role='participant'/>
  1800. * <status code='303'/>
  1801. * <status code='110'/>
  1802. * </x>
  1803. * </presence>
  1804. *
  1805. * <presence
  1806. * from='coven@localhost/oldhag'
  1807. * id='5B4F27A4-25ED-43F7-A699-382C6B4AFC67'
  1808. * to='hag66@shakespeare.lit/pda'>
  1809. * <x xmlns='http://jabber.org/protocol/muc#user'>
  1810. * <item affiliation='member'
  1811. * jid='hag66@shakespeare.lit/pda'
  1812. * role='participant'/>
  1813. * <status code='110'/>
  1814. * </x>
  1815. * </presence>
  1816. */
  1817. const __ = _converse.__;
  1818. test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'oldnick').then(function () {
  1819. const view = _converse.chatboxviews.get('lounge@localhost');
  1820. const chat_content = view.el.querySelector('.chat-content');
  1821. let occupants = view.el.querySelector('.occupant-list');
  1822. expect(occupants.childNodes.length).toBe(1);
  1823. expect(occupants.firstElementChild.querySelector('.occupant-nick').textContent.trim()).toBe("oldnick");
  1824. expect(chat_content.querySelectorAll('div.chat-info').length).toBe(1);
  1825. expect(sizzle('div.chat-info:first', chat_content).pop().textContent)
  1826. .toBe("oldnick has entered the groupchat");
  1827. let presence = $pres().attrs({
  1828. from:'lounge@localhost/oldnick',
  1829. id:'DC352437-C019-40EC-B590-AF29E879AF98',
  1830. to:'dummy@localhost/pda',
  1831. type:'unavailable'
  1832. })
  1833. .c('x').attrs({xmlns:'http://jabber.org/protocol/muc#user'})
  1834. .c('item').attrs({
  1835. affiliation: 'owner',
  1836. jid: 'dummy@localhost/pda',
  1837. nick: 'newnick',
  1838. role: 'moderator'
  1839. }).up()
  1840. .c('status').attrs({code:'303'}).up()
  1841. .c('status').attrs({code:'110'}).nodeTree;
  1842. _converse.connection._dataRecv(test_utils.createRequest(presence));
  1843. expect(chat_content.querySelectorAll('div.chat-info').length).toBe(2);
  1844. expect(sizzle('div.chat-info:last').pop().textContent).toBe(
  1845. __(_converse.muc.new_nickname_messages["303"], "newnick")
  1846. );
  1847. occupants = view.el.querySelector('.occupant-list');
  1848. expect(occupants.childNodes.length).toBe(1);
  1849. presence = $pres().attrs({
  1850. from:'lounge@localhost/newnick',
  1851. id:'5B4F27A4-25ED-43F7-A699-382C6B4AFC67',
  1852. to:'dummy@localhost/pda'
  1853. })
  1854. .c('x').attrs({xmlns:'http://jabber.org/protocol/muc#user'})
  1855. .c('item').attrs({
  1856. affiliation: 'owner',
  1857. jid: 'dummy@localhost/pda',
  1858. role: 'moderator'
  1859. }).up()
  1860. .c('status').attrs({code:'110'}).nodeTree;
  1861. _converse.connection._dataRecv(test_utils.createRequest(presence));
  1862. // XXX: currently we still have an additional "has entered the groupchat"
  1863. // notification for the new nickname. Ideally we'd not have
  1864. // that, but that's probably not possible without some
  1865. // significant refactoring.
  1866. expect(chat_content.querySelectorAll('div.chat-info').length).toBe(3);
  1867. expect(sizzle('div.chat-info', chat_content)[1].textContent).toBe(
  1868. __(_converse.muc.new_nickname_messages["303"], "newnick")
  1869. );
  1870. occupants = view.el.querySelector('.occupant-list');
  1871. expect(occupants.childNodes.length).toBe(1);
  1872. expect(sizzle('.occupant-nick:first', occupants).pop().textContent).toBe("newnick");
  1873. done();
  1874. }).catch(_.partial(_converse.log, _, Strophe.LogLevel.FATAL));
  1875. }));
  1876. it("queries for the groupchat information before attempting to join the user",
  1877. mock.initConverseWithPromises(
  1878. null, ['rosterGroupsFetched', 'chatBoxesFetched'], {},
  1879. function (done, _converse) {
  1880. const IQ_stanzas = _converse.connection.IQ_stanzas;
  1881. const room_jid = 'coven@chat.shakespeare.lit';
  1882. _converse.api.rooms.open(room_jid, {'nick': 'some1'})
  1883. .then(() => {
  1884. return test_utils.waitUntil(() => _.filter(
  1885. IQ_stanzas,
  1886. iq => iq.nodeTree.querySelector(
  1887. `iq[to="${room_jid}"] query[xmlns="http://jabber.org/protocol/disco#info"]`
  1888. )).pop());
  1889. }).then(node => {
  1890. // Check that the groupchat queried for the feautures.
  1891. const stanza = node.nodeTree;
  1892. expect(node.toLocaleString()).toBe(
  1893. `<iq from="dummy@localhost/resource" id="${stanza.getAttribute("id")}" to="${room_jid}" type="get" xmlns="jabber:client">`+
  1894. `<query xmlns="http://jabber.org/protocol/disco#info"/>`+
  1895. `</iq>`);
  1896. /* <iq from='coven@chat.shakespeare.lit'
  1897. * id='ik3vs715'
  1898. * to='hag66@shakespeare.lit/pda'
  1899. * type='result'>
  1900. * <query xmlns='http://jabber.org/protocol/disco#info'>
  1901. * <identity
  1902. * category='conference'
  1903. * name='A Dark Cave'
  1904. * type='text'/>
  1905. * <feature var='http://jabber.org/protocol/muc'/>
  1906. * <feature var='muc_passwordprotected'/>
  1907. * <feature var='muc_hidden'/>
  1908. * <feature var='muc_temporary'/>
  1909. * <feature var='muc_open'/>
  1910. * <feature var='muc_unmoderated'/>
  1911. * <feature var='muc_nonanonymous'/>
  1912. * </query>
  1913. * </iq>
  1914. */
  1915. const features_stanza = $iq({
  1916. 'from': room_jid,
  1917. 'id': stanza.getAttribute('id'),
  1918. 'to': 'dummy@localhost/desktop',
  1919. 'type': 'result'
  1920. })
  1921. .c('query', { 'xmlns': 'http://jabber.org/protocol/disco#info'})
  1922. .c('identity', {
  1923. 'category': 'conference',
  1924. 'name': 'A Dark Cave',
  1925. 'type': 'text'
  1926. }).up()
  1927. .c('feature', {'var': 'http://jabber.org/protocol/muc'}).up()
  1928. .c('feature', {'var': 'muc_passwordprotected'}).up()
  1929. .c('feature', {'var': 'muc_hidden'}).up()
  1930. .c('feature', {'var': 'muc_temporary'}).up()
  1931. .c('feature', {'var': 'muc_open'}).up()
  1932. .c('feature', {'var': 'muc_unmoderated'}).up()
  1933. .c('feature', {'var': 'muc_nonanonymous'});
  1934. _converse.connection._dataRecv(test_utils.createRequest(features_stanza));
  1935. const view = _converse.chatboxviews.get('coven@chat.shakespeare.lit');
  1936. return test_utils.waitUntil(() => (view.model.get('connection_status') === converse.ROOMSTATUS.CONNECTING));
  1937. }).then(() => {
  1938. const view = _converse.chatboxviews.get('coven@chat.shakespeare.lit');
  1939. expect(view.model.get('features_fetched')).toBeTruthy();
  1940. expect(view.model.get('passwordprotected')).toBe(true);
  1941. expect(view.model.get('hidden')).toBe(true);
  1942. expect(view.model.get('temporary')).toBe(true);
  1943. expect(view.model.get('open')).toBe(true);
  1944. expect(view.model.get('unmoderated')).toBe(true);
  1945. expect(view.model.get('nonanonymous')).toBe(true);
  1946. done();
  1947. }).catch(_.partial(_converse.log, _, Strophe.LogLevel.FATAL));
  1948. }));
  1949. it("updates the shown features when the groupchat configuration has changed",
  1950. mock.initConverseWithPromises(
  1951. null, ['rosterGroupsFetched'], {},
  1952. function (done, _converse) {
  1953. var sent_IQ, IQ_id;
  1954. var sendIQ = _converse.connection.sendIQ;
  1955. test_utils.openAndEnterChatRoom(_converse, 'room', 'conference.example.org', 'dummy').then(function () {
  1956. var view = _converse.chatboxviews.get('room@conference.example.org');
  1957. view.model.set({
  1958. 'passwordprotected': false,
  1959. 'unsecured': true,
  1960. 'hidden': false,
  1961. 'public': true,
  1962. 'membersonly': false,
  1963. 'open': true,
  1964. 'persistent': false,
  1965. 'temporary': true,
  1966. 'nonanonymous': true,
  1967. 'semianonymous': false,
  1968. 'moderated': false,
  1969. 'unmoderated': true
  1970. });
  1971. expect(view.model.get('persistent')).toBe(false);
  1972. expect(view.model.get('temporary')).toBe(true);
  1973. view.model.set({'persistent': true});
  1974. expect(view.model.get('persistent')).toBe(true);
  1975. expect(view.model.get('temporary')).toBe(false);
  1976. expect(view.model.get('unsecured')).toBe(true);
  1977. expect(view.model.get('passwordprotected')).toBe(false);
  1978. view.model.set({'passwordprotected': true});
  1979. expect(view.model.get('unsecured')).toBe(false);
  1980. expect(view.model.get('passwordprotected')).toBe(true);
  1981. expect(view.model.get('unmoderated')).toBe(true);
  1982. expect(view.model.get('moderated')).toBe(false);
  1983. view.model.set({'moderated': true});
  1984. expect(view.model.get('unmoderated')).toBe(false);
  1985. expect(view.model.get('moderated')).toBe(true);
  1986. expect(view.model.get('nonanonymous')).toBe(true);
  1987. expect(view.model.get('semianonymous')).toBe(false);
  1988. view.model.set({'nonanonymous': false});
  1989. expect(view.model.get('nonanonymous')).toBe(false);
  1990. expect(view.model.get('semianonymous')).toBe(true);
  1991. expect(view.model.get('open')).toBe(true);
  1992. expect(view.model.get('membersonly')).toBe(false);
  1993. view.model.set({'membersonly': true});
  1994. expect(view.model.get('open')).toBe(false);
  1995. expect(view.model.get('membersonly')).toBe(true);
  1996. done();
  1997. }).catch(_.partial(console.error, _));
  1998. }));
  1999. it("indicates when a room is no longer anonymous",
  2000. mock.initConverseWithPromises(
  2001. null, ['rosterGroupsFetched'], {},
  2002. function (done, _converse) {
  2003. let sent_IQ, IQ_id;
  2004. const sendIQ = _converse.connection.sendIQ;
  2005. test_utils.openAndEnterChatRoom(_converse, 'coven', 'chat.shakespeare.lit', 'some1').then(function () {
  2006. spyOn(_converse.connection, 'sendIQ').and.callFake(function (iq, callback, errback) {
  2007. sent_IQ = iq;
  2008. IQ_id = sendIQ.bind(this)(iq, callback, errback);
  2009. });
  2010. // We pretend this is a new room, so no disco info is returned.
  2011. const features_stanza = $iq({
  2012. from: 'coven@chat.shakespeare.lit',
  2013. 'id': IQ_id,
  2014. 'to': 'dummy@localhost/desktop',
  2015. 'type': 'error'
  2016. }).c('error', {'type': 'cancel'})
  2017. .c('item-not-found', {'xmlns': "urn:ietf:params:xml:ns:xmpp-stanzas"});
  2018. _converse.connection._dataRecv(test_utils.createRequest(features_stanza));
  2019. const view = _converse.chatboxviews.get('coven@chat.shakespeare.lit');
  2020. /* <message xmlns="jabber:client"
  2021. * type="groupchat"
  2022. * to="dummy@localhost/_converse.js-27854181"
  2023. * from="coven@chat.shakespeare.lit">
  2024. * <x xmlns="http://jabber.org/protocol/muc#user">
  2025. * <status code="104"/>
  2026. * <status code="172"/>
  2027. * </x>
  2028. * </message>
  2029. */
  2030. const message = $msg({
  2031. type:'groupchat',
  2032. to: 'dummy@localhost/_converse.js-27854181',
  2033. from: 'coven@chat.shakespeare.lit'
  2034. }).c('x', {xmlns: Strophe.NS.MUC_USER})
  2035. .c('status', {code: '104'}).up()
  2036. .c('status', {code: '172'});
  2037. _converse.connection._dataRecv(test_utils.createRequest(message));
  2038. const chat_body = view.el.querySelector('.chatroom-body');
  2039. expect(sizzle('.message:last', chat_body).pop().textContent)
  2040. .toBe('This groupchat is now no longer anonymous');
  2041. done();
  2042. }).catch(_.partial(_converse.log, _, Strophe.LogLevel.FATAL));
  2043. }));
  2044. it("informs users if they have been kicked out of the groupchat",
  2045. mock.initConverseWithPromises(
  2046. null, ['rosterGroupsFetched'], {},
  2047. function (done, _converse) {
  2048. /* <presence
  2049. * from='harfleur@chat.shakespeare.lit/pistol'
  2050. * to='pistol@shakespeare.lit/harfleur'
  2051. * type='unavailable'>
  2052. * <x xmlns='http://jabber.org/protocol/muc#user'>
  2053. * <item affiliation='none' role='none'>
  2054. * <actor nick='Fluellen'/>
  2055. * <reason>Avaunt, you cullion!</reason>
  2056. * </item>
  2057. * <status code='110'/>
  2058. * <status code='307'/>
  2059. * </x>
  2060. * </presence>
  2061. */
  2062. test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy')
  2063. .then(() => {
  2064. var presence = $pres().attrs({
  2065. from:'lounge@localhost/dummy',
  2066. to:'dummy@localhost/pda',
  2067. type:'unavailable'
  2068. })
  2069. .c('x').attrs({xmlns:'http://jabber.org/protocol/muc#user'})
  2070. .c('item').attrs({
  2071. affiliation: 'none',
  2072. jid: 'dummy@localhost/pda',
  2073. role: 'none'
  2074. })
  2075. .c('actor').attrs({nick: 'Fluellen'}).up()
  2076. .c('reason').t('Avaunt, you cullion!').up()
  2077. .up()
  2078. .c('status').attrs({code:'110'}).up()
  2079. .c('status').attrs({code:'307'}).nodeTree;
  2080. _converse.connection._dataRecv(test_utils.createRequest(presence));
  2081. const view = _converse.chatboxviews.get('lounge@localhost');
  2082. expect(u.isVisible(view.el.querySelector('.chat-area'))).toBeFalsy();
  2083. expect(u.isVisible(view.el.querySelector('.occupants'))).toBeFalsy();
  2084. const chat_body = view.el.querySelector('.chatroom-body');
  2085. expect(chat_body.querySelectorAll('.disconnect-msg').length).toBe(3);
  2086. expect(chat_body.querySelector('.disconnect-msg:first-child').textContent).toBe(
  2087. 'You have been kicked from this groupchat');
  2088. expect(chat_body.querySelector('.disconnect-msg:nth-child(2)').textContent).toBe(
  2089. 'This action was done by Fluellen.');
  2090. expect(chat_body.querySelector('.disconnect-msg:nth-child(3)').textContent).toBe(
  2091. 'The reason given is: "Avaunt, you cullion!".');
  2092. done();
  2093. });
  2094. }));
  2095. it("can be saved to, and retrieved from, browserStorage",
  2096. mock.initConverseWithPromises(
  2097. null, ['rosterGroupsFetched', 'chatBoxesFetched'], {},
  2098. function (done, _converse) {
  2099. test_utils.openChatRoom(_converse, 'lounge', 'localhost', 'dummy')
  2100. .then(() => {
  2101. // We instantiate a new ChatBoxes collection, which by default
  2102. // will be empty.
  2103. test_utils.openControlBox();
  2104. var newchatboxes = new _converse.ChatBoxes();
  2105. expect(newchatboxes.length).toEqual(0);
  2106. // The chatboxes will then be fetched from browserStorage inside the
  2107. // onConnected method
  2108. newchatboxes.onConnected();
  2109. expect(newchatboxes.length).toEqual(2);
  2110. // Check that the chatrooms retrieved from browserStorage
  2111. // have the same attributes values as the original ones.
  2112. var attrs = ['id', 'box_id', 'visible'];
  2113. var new_attrs, old_attrs;
  2114. for (var i=0; i<attrs.length; i++) {
  2115. new_attrs = _.map(_.map(newchatboxes.models, 'attributes'), attrs[i]);
  2116. old_attrs = _.map(_.map(_converse.chatboxes.models, 'attributes'), attrs[i]);
  2117. // FIXME: should have have to sort here? Order must
  2118. // probably be the same...
  2119. // This should be fixed once the controlbox always opens
  2120. // only on the right.
  2121. expect(_.isEqual(new_attrs.sort(), old_attrs.sort())).toEqual(true);
  2122. }
  2123. _converse.rosterview.render();
  2124. done();
  2125. });
  2126. }));
  2127. it("can be minimized by clicking a DOM element with class 'toggle-chatbox-button'",
  2128. mock.initConverseWithPromises(
  2129. null, ['rosterGroupsFetched', 'chatBoxesFetched'], {},
  2130. function (done, _converse) {
  2131. test_utils.openChatRoom(_converse, 'lounge', 'localhost', 'dummy')
  2132. .then(() => {
  2133. const view = _converse.chatboxviews.get('lounge@localhost'),
  2134. trimmed_chatboxes = _converse.minimized_chats;
  2135. spyOn(view, 'minimize').and.callThrough();
  2136. spyOn(view, 'maximize').and.callThrough();
  2137. spyOn(_converse, 'emit');
  2138. view.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  2139. view.el.querySelector('.toggle-chatbox-button').click();
  2140. expect(view.minimize).toHaveBeenCalled();
  2141. expect(_converse.emit).toHaveBeenCalledWith('chatBoxMinimized', jasmine.any(Object));
  2142. expect(u.isVisible(view.el)).toBeFalsy();
  2143. expect(view.model.get('minimized')).toBeTruthy();
  2144. expect(view.minimize).toHaveBeenCalled();
  2145. var trimmedview = trimmed_chatboxes.get(view.model.get('id'));
  2146. trimmedview.el.querySelector("a.restore-chat").click();
  2147. expect(view.maximize).toHaveBeenCalled();
  2148. expect(_converse.emit).toHaveBeenCalledWith('chatBoxMaximized', jasmine.any(Object));
  2149. expect(view.model.get('minimized')).toBeFalsy();
  2150. expect(_converse.emit.calls.count(), 3);
  2151. done();
  2152. });
  2153. }));
  2154. it("can be closed again by clicking a DOM element with class 'close-chatbox-button'",
  2155. mock.initConverseWithPromises(
  2156. null, ['rosterGroupsFetched', 'chatBoxesFetched'], {},
  2157. function (done, _converse) {
  2158. test_utils.openChatRoom(_converse, 'lounge', 'localhost', 'dummy')
  2159. .then(() => {
  2160. const view = _converse.chatboxviews.get('lounge@localhost');
  2161. spyOn(view, 'close').and.callThrough();
  2162. spyOn(_converse, 'emit');
  2163. spyOn(view.model, 'leave');
  2164. view.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  2165. view.el.querySelector('.close-chatbox-button').click();
  2166. expect(view.close).toHaveBeenCalled();
  2167. expect(view.model.leave).toHaveBeenCalled();
  2168. expect(_converse.emit).toHaveBeenCalledWith('chatBoxClosed', jasmine.any(Object));
  2169. done();
  2170. });
  2171. }));
  2172. });
  2173. describe("Each chat groupchat can take special commands", function () {
  2174. it("takes /help to show the available commands",
  2175. mock.initConverseWithPromises(
  2176. null, ['rosterGroupsFetched'], {},
  2177. function (done, _converse) {
  2178. test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy').then(function () {
  2179. var view = _converse.chatboxviews.get('lounge@localhost');
  2180. spyOn(view, 'onMessageSubmitted').and.callThrough();
  2181. var textarea = view.el.querySelector('.chat-textarea');
  2182. textarea.value = '/help This is the groupchat subject';
  2183. view.keyPressed({
  2184. target: textarea,
  2185. preventDefault: _.noop,
  2186. keyCode: 13
  2187. });
  2188. expect(view.onMessageSubmitted).toHaveBeenCalled();
  2189. const info_messages = Array.prototype.slice.call(view.el.querySelectorAll('.chat-info'), 0);
  2190. expect(info_messages.length).toBe(18);
  2191. expect(info_messages.pop().textContent).toBe('/voice: Allow muted user to post messages');
  2192. expect(info_messages.pop().textContent).toBe('/topic: Set groupchat subject (alias for /subject)');
  2193. expect(info_messages.pop().textContent).toBe('/subject: Set groupchat subject');
  2194. expect(info_messages.pop().textContent).toBe('/revoke: Revoke user\'s membership');
  2195. expect(info_messages.pop().textContent).toBe('/register: Register a nickname for this room');
  2196. expect(info_messages.pop().textContent).toBe('/owner: Grant ownership of this groupchat');
  2197. expect(info_messages.pop().textContent).toBe('/op: Grant moderator role to user');
  2198. expect(info_messages.pop().textContent).toBe('/nick: Change your nickname');
  2199. expect(info_messages.pop().textContent).toBe('/mute: Remove user\'s ability to post messages');
  2200. expect(info_messages.pop().textContent).toBe('/member: Grant membership to a user');
  2201. expect(info_messages.pop().textContent).toBe('/me: Write in 3rd person');
  2202. expect(info_messages.pop().textContent).toBe('/kick: Kick user from groupchat');
  2203. expect(info_messages.pop().textContent).toBe('/help: Show this menu');
  2204. expect(info_messages.pop().textContent).toBe('/deop: Change user role to participant');
  2205. expect(info_messages.pop().textContent).toBe('/clear: Remove messages');
  2206. expect(info_messages.pop().textContent).toBe('/ban: Ban user from groupchat');
  2207. expect(info_messages.pop().textContent).toBe('/admin: Change user\'s affiliation to admin');
  2208. done();
  2209. }).catch(_.partial(console.error, _));
  2210. }));
  2211. it("takes /member to make an occupant a member",
  2212. mock.initConverseWithPromises(
  2213. null, ['rosterGroupsFetched'], {},
  2214. function (done, _converse) {
  2215. let iq_stanza, view;
  2216. test_utils.openAndEnterChatRoom(_converse, 'lounge', 'muc.localhost', 'dummy')
  2217. .then(() => {
  2218. view = _converse.chatboxviews.get('lounge@muc.localhost');
  2219. /* We don't show join/leave messages for existing occupants. We
  2220. * know about them because we receive their presences before we
  2221. * receive our own.
  2222. */
  2223. const presence = $pres({
  2224. to: 'dummy@localhost/resource',
  2225. from: 'lounge@muc.localhost/marc'
  2226. }).c('x', {xmlns: Strophe.NS.MUC_USER})
  2227. .c('item', {
  2228. 'affiliation': 'none',
  2229. 'jid': 'marc@localhost/_converse.js-290929789',
  2230. 'role': 'participant'
  2231. });
  2232. _converse.connection._dataRecv(test_utils.createRequest(presence));
  2233. expect(view.model.occupants.length).toBe(2);
  2234. const textarea = view.el.querySelector('.chat-textarea');
  2235. let sent_stanza;
  2236. spyOn(_converse.connection, 'send').and.callFake((stanza) => {
  2237. sent_stanza = stanza;
  2238. });
  2239. // First check that an error message appears when a
  2240. // non-existent nick is used.
  2241. textarea.value = '/member chris Welcome to the club!';
  2242. view.keyPressed({
  2243. target: textarea,
  2244. preventDefault: _.noop,
  2245. keyCode: 13
  2246. });
  2247. expect(_converse.connection.send).not.toHaveBeenCalled();
  2248. expect(view.el.querySelectorAll('.chat-error').length).toBe(1);
  2249. expect(view.el.querySelector('.chat-error').textContent.trim())
  2250. .toBe(`Error: couldn't find a groupchat participant "chris"`)
  2251. // Now test with an existing nick
  2252. textarea.value = '/member marc Welcome to the club!';
  2253. view.keyPressed({
  2254. target: textarea,
  2255. preventDefault: _.noop,
  2256. keyCode: 13
  2257. });
  2258. expect(_converse.connection.send).toHaveBeenCalled();
  2259. expect(Strophe.serialize(sent_stanza)).toBe(
  2260. `<iq id="${sent_stanza.getAttribute('id')}" to="lounge@muc.localhost" type="set" xmlns="jabber:client">`+
  2261. `<query xmlns="http://jabber.org/protocol/muc#admin">`+
  2262. `<item affiliation="member" jid="marc@localhost">`+
  2263. `<reason>Welcome to the club!</reason>`+
  2264. `</item>`+
  2265. `</query>`+
  2266. `</iq>`);
  2267. const result = $iq({
  2268. "xmlns": "jabber:client",
  2269. "type": "result",
  2270. "to": "dummy@localhost/resource",
  2271. "from": "lounge@muc.localhost",
  2272. "id": sent_stanza.getAttribute('id')
  2273. });
  2274. _converse.connection.IQ_stanzas = [];
  2275. _converse.connection._dataRecv(test_utils.createRequest(result));
  2276. return test_utils.waitUntil(() => _.filter(
  2277. _converse.connection.IQ_stanzas,
  2278. iq => iq.nodeTree.querySelector('iq[to="lounge@muc.localhost"][type="get"] item[affiliation="member"]')).pop()
  2279. );
  2280. }).then(node => {
  2281. iq_stanza = node.nodeTree;
  2282. expect(node.toLocaleString()).toBe(
  2283. `<iq id="${iq_stanza.getAttribute('id')}" to="lounge@muc.localhost" type="get" xmlns="jabber:client">`+
  2284. `<query xmlns="http://jabber.org/protocol/muc#admin">`+
  2285. `<item affiliation="member"/>`+
  2286. `</query>`+
  2287. `</iq>`)
  2288. expect(view.model.occupants.length).toBe(2);
  2289. const result = $iq({
  2290. "xmlns": "jabber:client",
  2291. "type": "result",
  2292. "to": "dummy@localhost/resource",
  2293. "from": "lounge@muc.localhost",
  2294. "id": iq_stanza.getAttribute("id")
  2295. }).c("query", {"xmlns": "http://jabber.org/protocol/muc#admin"})
  2296. .c("item", {"jid": "marc", "affiliation": "member"});
  2297. _converse.connection._dataRecv(test_utils.createRequest(result));
  2298. expect(view.model.occupants.length).toBe(2);
  2299. return test_utils.waitUntil(() => _.filter(
  2300. _converse.connection.IQ_stanzas,
  2301. (iq) => iq.nodeTree.querySelector('iq[to="lounge@muc.localhost"][type="get"] item[affiliation="owner"]')).pop()
  2302. );
  2303. }).then(node => {
  2304. iq_stanza = node.nodeTree;
  2305. expect(node.toLocaleString()).toBe(
  2306. `<iq id="${iq_stanza.getAttribute('id')}" to="lounge@muc.localhost" type="get" xmlns="jabber:client">`+
  2307. `<query xmlns="http://jabber.org/protocol/muc#admin">`+
  2308. `<item affiliation="owner"/>`+
  2309. `</query>`+
  2310. `</iq>`)
  2311. expect(view.model.occupants.length).toBe(2);
  2312. const result = $iq({
  2313. "xmlns": "jabber:client",
  2314. "type": "result",
  2315. "to": "dummy@localhost/resource",
  2316. "from": "lounge@muc.localhost",
  2317. "id": iq_stanza.getAttribute("id")
  2318. }).c("query", {"xmlns": "http://jabber.org/protocol/muc#admin"})
  2319. .c("item", {"jid": "dummy@localhost", "affiliation": "owner"});
  2320. _converse.connection._dataRecv(test_utils.createRequest(result));
  2321. expect(view.model.occupants.length).toBe(2);
  2322. return test_utils.waitUntil(() => _.filter(
  2323. _converse.connection.IQ_stanzas,
  2324. (iq) => iq.nodeTree.querySelector('iq[to="lounge@muc.localhost"][type="get"] item[affiliation="admin"]')).pop()
  2325. );
  2326. }).then(node => {
  2327. const iq_stanza = node.nodeTree;
  2328. expect(node.toLocaleString()).toBe(
  2329. `<iq id="${iq_stanza.getAttribute('id')}" to="lounge@muc.localhost" type="get" xmlns="jabber:client">`+
  2330. `<query xmlns="http://jabber.org/protocol/muc#admin">`+
  2331. `<item affiliation="admin"/>`+
  2332. `</query>`+
  2333. `</iq>`)
  2334. expect(view.model.occupants.length).toBe(2);
  2335. const result = $iq({
  2336. "xmlns": "jabber:client",
  2337. "type": "result",
  2338. "to": "dummy@localhost/resource",
  2339. "from": "lounge@muc.localhost",
  2340. "id": iq_stanza.getAttribute("id")
  2341. }).c("query", {"xmlns": "http://jabber.org/protocol/muc#admin"})
  2342. _converse.connection._dataRecv(test_utils.createRequest(result));
  2343. return test_utils.waitUntil(() => view.el.querySelectorAll('.badge').length > 1);
  2344. }).then(() => {
  2345. expect(view.model.occupants.length).toBe(2);
  2346. expect(view.el.querySelectorAll('.occupant').length).toBe(2);
  2347. done();
  2348. }).catch(_.partial(console.error, _));
  2349. }));
  2350. it("takes /topic to set the groupchat topic",
  2351. mock.initConverseWithPromises(
  2352. null, ['rosterGroupsFetched'], {},
  2353. function (done, _converse) {
  2354. test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy').then(function () {
  2355. var sent_stanza;
  2356. var view = _converse.chatboxviews.get('lounge@localhost');
  2357. spyOn(view, 'onMessageSubmitted').and.callThrough();
  2358. spyOn(view, 'clearMessages');
  2359. spyOn(_converse.connection, 'send').and.callFake(function (stanza) {
  2360. sent_stanza = stanza;
  2361. });
  2362. // Check the alias /topic
  2363. var textarea = view.el.querySelector('.chat-textarea');
  2364. textarea.value = '/topic This is the groupchat subject';
  2365. view.keyPressed({
  2366. target: textarea,
  2367. preventDefault: _.noop,
  2368. keyCode: 13
  2369. });
  2370. expect(view.onMessageSubmitted).toHaveBeenCalled();
  2371. expect(_converse.connection.send).toHaveBeenCalled();
  2372. expect(sent_stanza.textContent).toBe('This is the groupchat subject');
  2373. // Check /subject
  2374. textarea.value = '/subject This is a new subject';
  2375. view.keyPressed({
  2376. target: textarea,
  2377. preventDefault: _.noop,
  2378. keyCode: 13
  2379. });
  2380. expect(sent_stanza.textContent).toBe('This is a new subject');
  2381. expect(Strophe.serialize(sent_stanza).toLocaleString()).toBe(
  2382. '<message from="dummy@localhost/resource" to="lounge@localhost" type="groupchat" xmlns="jabber:client">'+
  2383. '<subject xmlns="jabber:client">This is a new subject</subject>'+
  2384. '</message>');
  2385. // Check case insensitivity
  2386. textarea.value = '/Subject This is yet another subject';
  2387. view.keyPressed({
  2388. target: textarea,
  2389. preventDefault: _.noop,
  2390. keyCode: 13
  2391. });
  2392. expect(sent_stanza.textContent).toBe('This is yet another subject');
  2393. expect(Strophe.serialize(sent_stanza).toLocaleString()).toBe(
  2394. '<message from="dummy@localhost/resource" to="lounge@localhost" type="groupchat" xmlns="jabber:client">'+
  2395. '<subject xmlns="jabber:client">This is yet another subject</subject>'+
  2396. '</message>');
  2397. done();
  2398. }).catch(_.partial(console.error, _));
  2399. }));
  2400. it("takes /clear to clear messages",
  2401. mock.initConverseWithPromises(
  2402. null, ['rosterGroupsFetched'], {},
  2403. function (done, _converse) {
  2404. test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy').then(function () {
  2405. var view = _converse.chatboxviews.get('lounge@localhost');
  2406. spyOn(view, 'onMessageSubmitted').and.callThrough();
  2407. spyOn(view, 'clearMessages');
  2408. var textarea = view.el.querySelector('.chat-textarea')
  2409. textarea.value = '/clear';
  2410. view.keyPressed({
  2411. target: textarea,
  2412. preventDefault: _.noop,
  2413. keyCode: 13
  2414. });
  2415. expect(view.onMessageSubmitted).toHaveBeenCalled();
  2416. expect(view.clearMessages).toHaveBeenCalled();
  2417. done();
  2418. }).catch(_.partial(console.error, _));
  2419. }));
  2420. it("takes /owner to make a user an owner",
  2421. mock.initConverseWithPromises(
  2422. null, ['rosterGroupsFetched'], {},
  2423. function (done, _converse) {
  2424. let sent_IQ, IQ_id;
  2425. const sendIQ = _converse.connection.sendIQ;
  2426. spyOn(_converse.connection, 'sendIQ').and.callFake(function (iq, callback, errback) {
  2427. sent_IQ = iq;
  2428. IQ_id = sendIQ.bind(this)(iq, callback, errback);
  2429. });
  2430. test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy').then(function () {
  2431. var view = _converse.chatboxviews.get('lounge@localhost');
  2432. spyOn(view, 'onMessageSubmitted').and.callThrough();
  2433. spyOn(view.model, 'setAffiliation').and.callThrough();
  2434. spyOn(view, 'showErrorMessage').and.callThrough();
  2435. spyOn(view, 'validateRoleChangeCommand').and.callThrough();
  2436. let presence = $pres({
  2437. 'from': 'lounge@localhost/annoyingGuy',
  2438. 'id':'27C55F89-1C6A-459A-9EB5-77690145D624',
  2439. 'to': 'dummy@localhost/desktop'
  2440. })
  2441. .c('x', { 'xmlns': 'http://jabber.org/protocol/muc#user'})
  2442. .c('item', {
  2443. 'jid': 'annoyingguy@localhost',
  2444. 'affiliation': 'member',
  2445. 'role': 'participant'
  2446. });
  2447. _converse.connection._dataRecv(test_utils.createRequest(presence));
  2448. var textarea = view.el.querySelector('.chat-textarea')
  2449. textarea.value = '/owner';
  2450. view.keyPressed({
  2451. target: textarea,
  2452. preventDefault: _.noop,
  2453. keyCode: 13
  2454. });
  2455. expect(view.onMessageSubmitted).toHaveBeenCalled();
  2456. expect(view.validateRoleChangeCommand).toHaveBeenCalled();
  2457. expect(view.showErrorMessage).toHaveBeenCalledWith(
  2458. "Error: the \"owner\" command takes two arguments, the user's nickname and optionally a reason.");
  2459. expect(view.model.setAffiliation).not.toHaveBeenCalled();
  2460. view.onMessageSubmitted('/owner nobody You\'re responsible');
  2461. expect(view.showErrorMessage).toHaveBeenCalledWith(
  2462. 'Error: couldn\'t find a groupchat participant "nobody"');
  2463. expect(view.model.setAffiliation).not.toHaveBeenCalled();
  2464. // Call now with the correct amount of arguments.
  2465. // XXX: Calling onMessageSubmitted directly, trying
  2466. // again via triggering Event doesn't work for some weird
  2467. // reason.
  2468. view.onMessageSubmitted('/owner annoyingGuy You\'re responsible');
  2469. expect(view.validateRoleChangeCommand.calls.count()).toBe(3);
  2470. expect(view.model.setAffiliation).toHaveBeenCalled();
  2471. expect(view.showErrorMessage.calls.count()).toBe(2);
  2472. // Check that the member list now gets updated
  2473. expect(sent_IQ.toLocaleString()).toBe(
  2474. `<iq id="${IQ_id}" to="lounge@localhost" type="set" xmlns="jabber:client">`+
  2475. `<query xmlns="http://jabber.org/protocol/muc#admin">`+
  2476. `<item affiliation="owner" jid="annoyingGuy">`+
  2477. `<reason>You&apos;re responsible</reason>`+
  2478. `</item>`+
  2479. `</query>`+
  2480. `</iq>`);
  2481. presence = $pres({
  2482. 'from': 'lounge@localhost/annoyingGuy',
  2483. 'id':'27C55F89-1C6A-459A-9EB5-77690145D628',
  2484. 'to': 'dummy@localhost/desktop'
  2485. })
  2486. .c('x', { 'xmlns': 'http://jabber.org/protocol/muc#user'})
  2487. .c('item', {
  2488. 'jid': 'annoyingguy@localhost',
  2489. 'affiliation': 'owner',
  2490. 'role': 'participant'
  2491. });
  2492. _converse.connection._dataRecv(test_utils.createRequest(presence));
  2493. expect(view.el.querySelectorAll('.chat-info')[4].textContent).toBe("annoyingGuy is now an owner of this groupchat");
  2494. done();
  2495. }).catch(_.partial(console.error, _));
  2496. }));
  2497. it("takes /ban to ban a user",
  2498. mock.initConverseWithPromises(
  2499. null, ['rosterGroupsFetched'], {},
  2500. function (done, _converse) {
  2501. let sent_IQ, IQ_id;
  2502. const sendIQ = _converse.connection.sendIQ;
  2503. spyOn(_converse.connection, 'sendIQ').and.callFake(function (iq, callback, errback) {
  2504. sent_IQ = iq;
  2505. IQ_id = sendIQ.bind(this)(iq, callback, errback);
  2506. });
  2507. test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy')
  2508. .then(() => {
  2509. const view = _converse.chatboxviews.get('lounge@localhost');
  2510. spyOn(view, 'onMessageSubmitted').and.callThrough();
  2511. spyOn(view.model, 'setAffiliation').and.callThrough();
  2512. spyOn(view, 'showErrorMessage').and.callThrough();
  2513. spyOn(view, 'validateRoleChangeCommand').and.callThrough();
  2514. let presence = $pres({
  2515. 'from': 'lounge@localhost/annoyingGuy',
  2516. 'id':'27C55F89-1C6A-459A-9EB5-77690145D624',
  2517. 'to': 'dummy@localhost/desktop'
  2518. })
  2519. .c('x', { 'xmlns': 'http://jabber.org/protocol/muc#user'})
  2520. .c('item', {
  2521. 'jid': 'annoyingguy@localhost',
  2522. 'affiliation': 'member',
  2523. 'role': 'participant'
  2524. });
  2525. _converse.connection._dataRecv(test_utils.createRequest(presence));
  2526. const textarea = view.el.querySelector('.chat-textarea')
  2527. textarea.value = '/ban';
  2528. view.keyPressed({
  2529. target: textarea,
  2530. preventDefault: _.noop,
  2531. keyCode: 13
  2532. });
  2533. expect(view.onMessageSubmitted).toHaveBeenCalled();
  2534. expect(view.validateRoleChangeCommand).toHaveBeenCalled();
  2535. expect(view.showErrorMessage).toHaveBeenCalledWith(
  2536. "Error: the \"ban\" command takes two arguments, the user's nickname and optionally a reason.");
  2537. expect(view.model.setAffiliation).not.toHaveBeenCalled();
  2538. // Call now with the correct amount of arguments.
  2539. // XXX: Calling onMessageSubmitted directly, trying
  2540. // again via triggering Event doesn't work for some weird
  2541. // reason.
  2542. view.onMessageSubmitted('/ban annoyingGuy You\'re annoying');
  2543. expect(view.validateRoleChangeCommand.calls.count()).toBe(2);
  2544. expect(view.showErrorMessage.calls.count()).toBe(1);
  2545. expect(view.model.setAffiliation).toHaveBeenCalled();
  2546. // Check that the member list now gets updated
  2547. expect(sent_IQ.toLocaleString()).toBe(
  2548. `<iq id="${IQ_id}" to="lounge@localhost" type="set" xmlns="jabber:client">`+
  2549. `<query xmlns="http://jabber.org/protocol/muc#admin">`+
  2550. `<item affiliation="outcast" jid="annoyingGuy">`+
  2551. `<reason>You&apos;re annoying</reason>`+
  2552. `</item>`+
  2553. `</query>`+
  2554. `</iq>`);
  2555. presence = $pres({
  2556. 'from': 'lounge@localhost/annoyingGuy',
  2557. 'id':'27C55F89-1C6A-459A-9EB5-77690145D628',
  2558. 'to': 'dummy@localhost/desktop'
  2559. })
  2560. .c('x', { 'xmlns': 'http://jabber.org/protocol/muc#user'})
  2561. .c('item', {
  2562. 'jid': 'annoyingguy@localhost',
  2563. 'affiliation': 'outcast',
  2564. 'role': 'participant'
  2565. });
  2566. _converse.connection._dataRecv(test_utils.createRequest(presence));
  2567. expect(
  2568. view.el.querySelectorAll('.chat-info')[3].textContent).toBe(
  2569. "annoyingGuy has been banned from this groupchat");
  2570. done();
  2571. }).catch(_.partial(console.error, _));
  2572. }));
  2573. it("takes /kick to kick a user",
  2574. mock.initConverseWithPromises(
  2575. null, ['rosterGroupsFetched'], {},
  2576. function (done, _converse) {
  2577. let sent_IQ, IQ_id;
  2578. const sendIQ = _converse.connection.sendIQ;
  2579. spyOn(_converse.connection, 'sendIQ').and.callFake(function (iq, callback, errback) {
  2580. sent_IQ = iq;
  2581. IQ_id = sendIQ.bind(this)(iq, callback, errback);
  2582. });
  2583. test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy').then(function () {
  2584. const view = _converse.chatboxviews.get('lounge@localhost');
  2585. spyOn(view, 'onMessageSubmitted').and.callThrough();
  2586. spyOn(view, 'modifyRole').and.callThrough();
  2587. spyOn(view, 'showErrorMessage').and.callThrough();
  2588. spyOn(view, 'validateRoleChangeCommand').and.callThrough();
  2589. let presence = $pres({
  2590. 'from': 'lounge@localhost/annoyingGuy',
  2591. 'id':'27C55F89-1C6A-459A-9EB5-77690145D624',
  2592. 'to': 'dummy@localhost/desktop'
  2593. })
  2594. .c('x', { 'xmlns': 'http://jabber.org/protocol/muc#user'})
  2595. .c('item', {
  2596. 'jid': 'annoyingguy@localhost',
  2597. 'affiliation': 'none',
  2598. 'role': 'participant'
  2599. });
  2600. _converse.connection._dataRecv(test_utils.createRequest(presence));
  2601. var textarea = view.el.querySelector('.chat-textarea')
  2602. textarea.value = '/kick';
  2603. view.keyPressed({
  2604. target: textarea,
  2605. preventDefault: _.noop,
  2606. keyCode: 13
  2607. });
  2608. expect(view.onMessageSubmitted).toHaveBeenCalled();
  2609. expect(view.validateRoleChangeCommand).toHaveBeenCalled();
  2610. expect(view.showErrorMessage).toHaveBeenCalledWith(
  2611. "Error: the \"kick\" command takes two arguments, the user's nickname and optionally a reason.");
  2612. expect(view.modifyRole).not.toHaveBeenCalled();
  2613. // Call now with the correct amount of arguments.
  2614. // XXX: Calling onMessageSubmitted directly, trying
  2615. // again via triggering Event doesn't work for some weird
  2616. // reason.
  2617. view.onMessageSubmitted('/kick annoyingGuy You\'re annoying');
  2618. expect(view.validateRoleChangeCommand.calls.count()).toBe(2);
  2619. expect(view.showErrorMessage.calls.count()).toBe(1);
  2620. expect(view.modifyRole).toHaveBeenCalled();
  2621. expect(sent_IQ.toLocaleString()).toBe(
  2622. `<iq id="${IQ_id}" to="lounge@localhost" type="set" xmlns="jabber:client">`+
  2623. `<query xmlns="http://jabber.org/protocol/muc#admin">`+
  2624. `<item nick="annoyingGuy" role="none">`+
  2625. `<reason>You&apos;re annoying</reason>`+
  2626. `</item>`+
  2627. `</query>`+
  2628. `</iq>`);
  2629. /* <presence
  2630. * from='harfleur@chat.shakespeare.lit/pistol'
  2631. * to='gower@shakespeare.lit/cell'
  2632. * type='unavailable'>
  2633. * <x xmlns='http://jabber.org/protocol/muc#user'>
  2634. * <item affiliation='none' role='none'/>
  2635. * <status code='307'/>
  2636. * </x>
  2637. * </presence>
  2638. */
  2639. presence = $pres({
  2640. 'from': 'lounge@localhost/annoyingGuy',
  2641. 'to': 'dummy@localhost/desktop',
  2642. 'type': 'unavailable'
  2643. })
  2644. .c('x', { 'xmlns': 'http://jabber.org/protocol/muc#user'})
  2645. .c('item', {
  2646. 'affiliation': 'none',
  2647. 'role': 'none'
  2648. }).up()
  2649. .c('status', {'code': '307'});
  2650. _converse.connection._dataRecv(test_utils.createRequest(presence));
  2651. expect(view.el.querySelectorAll('.chat-info')[3].textContent).toBe("annoyingGuy has been kicked out");
  2652. expect(view.el.querySelectorAll('.chat-info').length).toBe(4);
  2653. done();
  2654. }).catch(_.partial(console.error, _));
  2655. }));
  2656. it("takes /op and /deop to make a user a moderator or not",
  2657. mock.initConverseWithPromises(
  2658. null, ['rosterGroupsFetched'], {},
  2659. function (done, _converse) {
  2660. test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy').then(function () {
  2661. var sent_IQ, IQ_id;
  2662. var sendIQ = _converse.connection.sendIQ;
  2663. spyOn(_converse.connection, 'sendIQ').and.callFake(function (iq, callback, errback) {
  2664. sent_IQ = iq;
  2665. IQ_id = sendIQ.bind(this)(iq, callback, errback);
  2666. });
  2667. var view = _converse.chatboxviews.get('lounge@localhost');
  2668. spyOn(view, 'onMessageSubmitted').and.callThrough();
  2669. spyOn(view, 'modifyRole').and.callThrough();
  2670. spyOn(view, 'showErrorMessage').and.callThrough();
  2671. spyOn(view, 'showChatEvent').and.callThrough();
  2672. spyOn(view, 'validateRoleChangeCommand').and.callThrough();
  2673. // New user enters the groupchat
  2674. /* <presence
  2675. * from='coven@chat.shakespeare.lit/thirdwitch'
  2676. * id='27C55F89-1C6A-459A-9EB5-77690145D624'
  2677. * to='crone1@shakespeare.lit/desktop'>
  2678. * <x xmlns='http://jabber.org/protocol/muc#user'>
  2679. * <item affiliation='member' role='moderator'/>
  2680. * </x>
  2681. * </presence>
  2682. */
  2683. var presence = $pres({
  2684. 'from': 'lounge@localhost/trustworthyguy',
  2685. 'id':'27C55F89-1C6A-459A-9EB5-77690145D624',
  2686. 'to': 'dummy@localhost/desktop'
  2687. })
  2688. .c('x', { 'xmlns': 'http://jabber.org/protocol/muc#user'})
  2689. .c('item', {
  2690. 'jid': 'trustworthyguy@localhost',
  2691. 'affiliation': 'member',
  2692. 'role': 'participant'
  2693. });
  2694. _converse.connection._dataRecv(test_utils.createRequest(presence));
  2695. var info_msgs = Array.prototype.slice.call(view.el.querySelectorAll('.chat-info'), 0);
  2696. expect(info_msgs.pop().textContent).toBe("trustworthyguy has entered the groupchat");
  2697. var textarea = view.el.querySelector('.chat-textarea')
  2698. textarea.value = '/op';
  2699. view.keyPressed({
  2700. target: textarea,
  2701. preventDefault: _.noop,
  2702. keyCode: 13
  2703. });
  2704. expect(view.onMessageSubmitted).toHaveBeenCalled();
  2705. expect(view.validateRoleChangeCommand).toHaveBeenCalled();
  2706. expect(view.showErrorMessage).toHaveBeenCalledWith(
  2707. "Error: the \"op\" command takes two arguments, the user's nickname and optionally a reason.");
  2708. expect(view.modifyRole).not.toHaveBeenCalled();
  2709. // Call now with the correct amount of arguments.
  2710. // XXX: Calling onMessageSubmitted directly, trying
  2711. // again via triggering Event doesn't work for some weird
  2712. // reason.
  2713. view.onMessageSubmitted('/op trustworthyguy You\'re trustworthy');
  2714. expect(view.validateRoleChangeCommand.calls.count()).toBe(2);
  2715. expect(view.showErrorMessage.calls.count()).toBe(1);
  2716. expect(view.modifyRole).toHaveBeenCalled();
  2717. expect(sent_IQ.toLocaleString()).toBe(
  2718. `<iq id="${IQ_id}" to="lounge@localhost" type="set" xmlns="jabber:client">`+
  2719. `<query xmlns="http://jabber.org/protocol/muc#admin">`+
  2720. `<item nick="trustworthyguy" role="moderator">`+
  2721. `<reason>You&apos;re trustworthy</reason>`+
  2722. `</item>`+
  2723. `</query>`+
  2724. `</iq>`);
  2725. /* <presence
  2726. * from='coven@chat.shakespeare.lit/thirdwitch'
  2727. * to='crone1@shakespeare.lit/desktop'>
  2728. * <x xmlns='http://jabber.org/protocol/muc#user'>
  2729. * <item affiliation='member'
  2730. * jid='hag66@shakespeare.lit/pda'
  2731. * role='moderator'/>
  2732. * </x>
  2733. * </presence>
  2734. */
  2735. presence = $pres({
  2736. 'from': 'lounge@localhost/trustworthyguy',
  2737. 'to': 'dummy@localhost/desktop'
  2738. })
  2739. .c('x', { 'xmlns': 'http://jabber.org/protocol/muc#user'})
  2740. .c('item', {
  2741. 'jid': 'trustworthyguy@localhost',
  2742. 'affiliation': 'member',
  2743. 'role': 'moderator'
  2744. });
  2745. _converse.connection._dataRecv(test_utils.createRequest(presence));
  2746. info_msgs = Array.prototype.slice.call(view.el.querySelectorAll('.chat-info'), 0);
  2747. expect(info_msgs.pop().textContent).toBe("trustworthyguy is now a moderator");
  2748. view.onMessageSubmitted('/deop trustworthyguy Perhaps not');
  2749. expect(view.validateRoleChangeCommand.calls.count()).toBe(3);
  2750. expect(view.showChatEvent.calls.count()).toBe(1);
  2751. expect(view.modifyRole).toHaveBeenCalled();
  2752. expect(sent_IQ.toLocaleString()).toBe(
  2753. `<iq id="${IQ_id}" to="lounge@localhost" type="set" xmlns="jabber:client">`+
  2754. `<query xmlns="http://jabber.org/protocol/muc#admin">`+
  2755. `<item nick="trustworthyguy" role="participant">`+
  2756. `<reason>Perhaps not</reason>`+
  2757. `</item>`+
  2758. `</query>`+
  2759. `</iq>`);
  2760. /* <presence
  2761. * from='coven@chat.shakespeare.lit/thirdwitch'
  2762. * to='crone1@shakespeare.lit/desktop'>
  2763. * <x xmlns='http://jabber.org/protocol/muc#user'>
  2764. * <item affiliation='member'
  2765. * jid='hag66@shakespeare.lit/pda'
  2766. * role='participant'/>
  2767. * </x>
  2768. * </presence>
  2769. */
  2770. presence = $pres({
  2771. 'from': 'lounge@localhost/trustworthyguy',
  2772. 'to': 'dummy@localhost/desktop'
  2773. }).c('x', { 'xmlns': 'http://jabber.org/protocol/muc#user'})
  2774. .c('item', {
  2775. 'jid': 'trustworthyguy@localhost',
  2776. 'affiliation': 'member',
  2777. 'role': 'participant'
  2778. });
  2779. _converse.connection._dataRecv(test_utils.createRequest(presence));
  2780. info_msgs = Array.prototype.slice.call(view.el.querySelectorAll('.chat-info'), 0);
  2781. expect(info_msgs.pop().textContent).toBe("trustworthyguy is no longer a moderator");
  2782. done();
  2783. }).catch(_.partial(console.error, _));
  2784. }));
  2785. it("takes /mute and /voice to mute and unmute a user",
  2786. mock.initConverseWithPromises(
  2787. null, ['rosterGroupsFetched'], {},
  2788. function (done, _converse) {
  2789. test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy')
  2790. .then(() => {
  2791. var sent_IQ, IQ_id;
  2792. var sendIQ = _converse.connection.sendIQ;
  2793. spyOn(_converse.connection, 'sendIQ').and.callFake(function (iq, callback, errback) {
  2794. sent_IQ = iq;
  2795. IQ_id = sendIQ.bind(this)(iq, callback, errback);
  2796. });
  2797. var view = _converse.chatboxviews.get('lounge@localhost');
  2798. spyOn(view, 'onMessageSubmitted').and.callThrough();
  2799. spyOn(view, 'modifyRole').and.callThrough();
  2800. spyOn(view, 'showErrorMessage').and.callThrough();
  2801. spyOn(view, 'showChatEvent').and.callThrough();
  2802. spyOn(view, 'validateRoleChangeCommand').and.callThrough();
  2803. // New user enters the groupchat
  2804. /* <presence
  2805. * from='coven@chat.shakespeare.lit/thirdwitch'
  2806. * id='27C55F89-1C6A-459A-9EB5-77690145D624'
  2807. * to='crone1@shakespeare.lit/desktop'>
  2808. * <x xmlns='http://jabber.org/protocol/muc#user'>
  2809. * <item affiliation='member' role='participant'/>
  2810. * </x>
  2811. * </presence>
  2812. */
  2813. var presence = $pres({
  2814. 'from': 'lounge@localhost/annoyingGuy',
  2815. 'id':'27C55F89-1C6A-459A-9EB5-77690145D624',
  2816. 'to': 'dummy@localhost/desktop'
  2817. })
  2818. .c('x', { 'xmlns': 'http://jabber.org/protocol/muc#user'})
  2819. .c('item', {
  2820. 'jid': 'annoyingguy@localhost',
  2821. 'affiliation': 'member',
  2822. 'role': 'participant'
  2823. });
  2824. _converse.connection._dataRecv(test_utils.createRequest(presence));
  2825. var info_msgs = Array.prototype.slice.call(view.el.querySelectorAll('.chat-info'), 0);
  2826. expect(info_msgs.pop().textContent).toBe("annoyingGuy has entered the groupchat");
  2827. var textarea = view.el.querySelector('.chat-textarea')
  2828. textarea.value = '/mute';
  2829. view.keyPressed({
  2830. target: textarea,
  2831. preventDefault: _.noop,
  2832. keyCode: 13
  2833. });
  2834. expect(view.onMessageSubmitted).toHaveBeenCalled();
  2835. expect(view.validateRoleChangeCommand).toHaveBeenCalled();
  2836. expect(view.showErrorMessage).toHaveBeenCalledWith(
  2837. "Error: the \"mute\" command takes two arguments, the user's nickname and optionally a reason.");
  2838. expect(view.modifyRole).not.toHaveBeenCalled();
  2839. // Call now with the correct amount of arguments.
  2840. // XXX: Calling onMessageSubmitted directly, trying
  2841. // again via triggering Event doesn't work for some weird
  2842. // reason.
  2843. view.onMessageSubmitted('/mute annoyingGuy You\'re annoying');
  2844. expect(view.validateRoleChangeCommand.calls.count()).toBe(2);
  2845. expect(view.showErrorMessage.calls.count()).toBe(1);
  2846. expect(view.modifyRole).toHaveBeenCalled();
  2847. expect(sent_IQ.toLocaleString()).toBe(
  2848. `<iq id="${IQ_id}" to="lounge@localhost" type="set" xmlns="jabber:client">`+
  2849. `<query xmlns="http://jabber.org/protocol/muc#admin">`+
  2850. `<item nick="annoyingGuy" role="visitor">`+
  2851. `<reason>You&apos;re annoying</reason>`+
  2852. `</item>`+
  2853. `</query>`+
  2854. `</iq>`);
  2855. /* <presence
  2856. * from='coven@chat.shakespeare.lit/thirdwitch'
  2857. * to='crone1@shakespeare.lit/desktop'>
  2858. * <x xmlns='http://jabber.org/protocol/muc#user'>
  2859. * <item affiliation='member'
  2860. * jid='hag66@shakespeare.lit/pda'
  2861. * role='visitor'/>
  2862. * </x>
  2863. * </presence>
  2864. */
  2865. presence = $pres({
  2866. 'from': 'lounge@localhost/annoyingGuy',
  2867. 'to': 'dummy@localhost/desktop'
  2868. })
  2869. .c('x', { 'xmlns': 'http://jabber.org/protocol/muc#user'})
  2870. .c('item', {
  2871. 'jid': 'annoyingguy@localhost',
  2872. 'affiliation': 'member',
  2873. 'role': 'visitor'
  2874. });
  2875. _converse.connection._dataRecv(test_utils.createRequest(presence));
  2876. info_msgs = Array.prototype.slice.call(view.el.querySelectorAll('.chat-info'), 0);
  2877. expect(info_msgs.pop().textContent).toBe("annoyingGuy has been muted");
  2878. view.onMessageSubmitted('/voice annoyingGuy Now you can talk again');
  2879. expect(view.validateRoleChangeCommand.calls.count()).toBe(3);
  2880. expect(view.showChatEvent.calls.count()).toBe(1);
  2881. expect(view.modifyRole).toHaveBeenCalled();
  2882. expect(sent_IQ.toLocaleString()).toBe(
  2883. `<iq id="${IQ_id}" to="lounge@localhost" type="set" xmlns="jabber:client">`+
  2884. `<query xmlns="http://jabber.org/protocol/muc#admin">`+
  2885. `<item nick="annoyingGuy" role="participant">`+
  2886. `<reason>Now you can talk again</reason>`+
  2887. `</item>`+
  2888. `</query>`+
  2889. `</iq>`);
  2890. /* <presence
  2891. * from='coven@chat.shakespeare.lit/thirdwitch'
  2892. * to='crone1@shakespeare.lit/desktop'>
  2893. * <x xmlns='http://jabber.org/protocol/muc#user'>
  2894. * <item affiliation='member'
  2895. * jid='hag66@shakespeare.lit/pda'
  2896. * role='visitor'/>
  2897. * </x>
  2898. * </presence>
  2899. */
  2900. presence = $pres({
  2901. 'from': 'lounge@localhost/annoyingGuy',
  2902. 'to': 'dummy@localhost/desktop'
  2903. })
  2904. .c('x', { 'xmlns': 'http://jabber.org/protocol/muc#user'})
  2905. .c('item', {
  2906. 'jid': 'annoyingguy@localhost',
  2907. 'affiliation': 'member',
  2908. 'role': 'participant'
  2909. });
  2910. _converse.connection._dataRecv(test_utils.createRequest(presence));
  2911. info_msgs = Array.prototype.slice.call(view.el.querySelectorAll('.chat-info'), 0);
  2912. expect(info_msgs.pop().textContent).toBe("annoyingGuy has been given a voice again");
  2913. done();
  2914. }).catch(_.partial(console.error, _));
  2915. }));
  2916. });
  2917. describe("When attempting to enter a groupchat", function () {
  2918. it("will use the nickname set in the global settings if the user doesn't have a VCard nickname",
  2919. mock.initConverseWithPromises(
  2920. null, ['rosterGroupsFetched', 'chatBoxesFetched'], {'nickname': 'Benedict-Cucumberpatch'},
  2921. async function (done, _converse) {
  2922. await test_utils.openChatRoomViaModal(_converse, 'roomy@muc.localhost');
  2923. const view = _converse.chatboxviews.get('roomy@muc.localhost');
  2924. expect(view.model.get('nick')).toBe('Benedict-Cucumberpatch');
  2925. done();
  2926. }));
  2927. it("will show an error message if the groupchat requires a password",
  2928. mock.initConverseWithPromises(
  2929. null, ['rosterGroupsFetched', 'chatBoxesFetched'], {},
  2930. async function (done, _converse) {
  2931. const groupchat_jid = 'protected';
  2932. await test_utils.openChatRoomViaModal(_converse, groupchat_jid, 'dummy');
  2933. const view = _converse.chatboxviews.get(groupchat_jid);
  2934. spyOn(view, 'renderPasswordForm').and.callThrough();
  2935. var presence = $pres().attrs({
  2936. 'from': `${groupchat_jid}/dummy`,
  2937. 'id': u.getUniqueId(),
  2938. 'to': 'dummy@localhost/pda',
  2939. 'type': 'error'
  2940. }).c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
  2941. .c('error').attrs({by:'lounge@localhost', type:'auth'})
  2942. .c('not-authorized').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'});
  2943. _converse.connection._dataRecv(test_utils.createRequest(presence));
  2944. const chat_body = view.el.querySelector('.chatroom-body');
  2945. expect(view.renderPasswordForm).toHaveBeenCalled();
  2946. expect(chat_body.querySelectorAll('form.chatroom-form').length).toBe(1);
  2947. expect(chat_body.querySelector('legend').textContent)
  2948. .toBe('This groupchat requires a password');
  2949. // Let's submit the form
  2950. spyOn(view, 'join');
  2951. const input_el = view.el.querySelector('[name="password"]');
  2952. input_el.value = 'secret';
  2953. view.el.querySelector('input[type=submit]').click();
  2954. expect(view.join).toHaveBeenCalledWith('dummy', 'secret');
  2955. done();
  2956. }));
  2957. it("will show an error message if the groupchat is members-only and the user not included",
  2958. mock.initConverseWithPromises(
  2959. null, ['rosterGroupsFetched'], {},
  2960. async function (done, _converse) {
  2961. const groupchat_jid = 'members-only@muc.localhost'
  2962. await test_utils.openChatRoomViaModal(_converse, groupchat_jid, 'dummy');
  2963. const presence = $pres().attrs({
  2964. from: `${groupchat_jid}/dummy`,
  2965. id: u.getUniqueId(),
  2966. to: 'dummy@localhost/pda',
  2967. type: 'error'
  2968. }).c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
  2969. .c('error').attrs({by:'lounge@localhost', type:'auth'})
  2970. .c('registration-required').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
  2971. const view = _converse.chatboxviews.get(groupchat_jid);
  2972. spyOn(view, 'showErrorMessage').and.callThrough();
  2973. _converse.connection._dataRecv(test_utils.createRequest(presence));
  2974. expect(view.el.querySelector('.chatroom-body .disconnect-container .disconnect-msg:last-child').textContent)
  2975. .toBe('You are not on the member list of this groupchat.');
  2976. done();
  2977. }));
  2978. it("will show an error message if the user has been banned",
  2979. mock.initConverseWithPromises(
  2980. null, ['rosterGroupsFetched'], {},
  2981. async function (done, _converse) {
  2982. const groupchat_jid = 'off-limits@muc.localhost'
  2983. await test_utils.openChatRoomViaModal(_converse, groupchat_jid, 'dummy');
  2984. const presence = $pres().attrs({
  2985. from: `${groupchat_jid}/dummy`,
  2986. id: u.getUniqueId(),
  2987. to: 'dummy@localhost/pda',
  2988. type: 'error'
  2989. }).c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
  2990. .c('error').attrs({by:'lounge@localhost', type:'auth'})
  2991. .c('forbidden').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
  2992. const view = _converse.chatboxviews.get(groupchat_jid);
  2993. spyOn(view, 'showErrorMessage').and.callThrough();
  2994. _converse.connection._dataRecv(test_utils.createRequest(presence));
  2995. expect(view.el.querySelector('.chatroom-body .disconnect-container .disconnect-msg:last-child').textContent)
  2996. .toBe('You have been banned from this groupchat.');
  2997. done();
  2998. }));
  2999. it("will render a nickname form if a nickname conflict happens and muc_nickname_from_jid=false",
  3000. mock.initConverseWithPromises(
  3001. null, ['rosterGroupsFetched'], {},
  3002. async function (done, _converse) {
  3003. const groupchat_jid = 'conflicted@muc.localhost';
  3004. await test_utils.openChatRoomViaModal(_converse, groupchat_jid, 'dummy');
  3005. var presence = $pres().attrs({
  3006. from: `${groupchat_jid}/dummy`,
  3007. id: u.getUniqueId(),
  3008. to: 'dummy@localhost/pda',
  3009. type: 'error'
  3010. }).c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
  3011. .c('error').attrs({by:'lounge@localhost', type:'cancel'})
  3012. .c('conflict').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
  3013. const view = _converse.chatboxviews.get(groupchat_jid);
  3014. spyOn(view, 'showErrorMessage').and.callThrough();
  3015. _converse.connection._dataRecv(test_utils.createRequest(presence));
  3016. expect(sizzle('.chatroom-body form.chatroom-form label:first', view.el).pop().textContent)
  3017. .toBe('Please choose your nickname');
  3018. const input = sizzle('.chatroom-body form.chatroom-form input:first', view.el).pop();
  3019. input.value = 'nicky';
  3020. view.el.querySelector('input[type=submit]').click();
  3021. done();
  3022. }));
  3023. it("will automatically choose a new nickname if a nickname conflict happens and muc_nickname_from_jid=true",
  3024. mock.initConverseWithPromises(
  3025. null, ['rosterGroupsFetched'], {},
  3026. async function (done, _converse) {
  3027. const groupchat_jid = 'conflicting@muc.localhost'
  3028. await test_utils.openChatRoomViaModal(_converse, groupchat_jid, 'dummy');
  3029. /* <presence
  3030. * from='coven@chat.shakespeare.lit/thirdwitch'
  3031. * id='n13mt3l'
  3032. * to='hag66@shakespeare.lit/pda'
  3033. * type='error'>
  3034. * <x xmlns='http://jabber.org/protocol/muc'/>
  3035. * <error by='coven@chat.shakespeare.lit' type='cancel'>
  3036. * <conflict xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/>
  3037. * </error>
  3038. * </presence>
  3039. */
  3040. _converse.muc_nickname_from_jid = true;
  3041. const attrs = {
  3042. 'from': `${groupchat_jid}/dummy`,
  3043. 'id': u.getUniqueId(),
  3044. 'to': 'dummy@localhost/pda',
  3045. 'type': 'error'
  3046. };
  3047. let presence = $pres().attrs(attrs)
  3048. .c('x').attrs({'xmlns':'http://jabber.org/protocol/muc'}).up()
  3049. .c('error').attrs({'by': groupchat_jid, 'type':'cancel'})
  3050. .c('conflict').attrs({'xmlns':'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
  3051. const view = _converse.chatboxviews.get(groupchat_jid);
  3052. spyOn(view, 'showErrorMessage').and.callThrough();
  3053. spyOn(view, 'join').and.callThrough();
  3054. // Simulate repeatedly that there's already someone in the groupchat
  3055. // with that nickname
  3056. _converse.connection._dataRecv(test_utils.createRequest(presence));
  3057. expect(view.join).toHaveBeenCalledWith('dummy-2');
  3058. attrs.from = `${groupchat_jid}/dummy-2`;
  3059. attrs.id = u.getUniqueId();
  3060. presence = $pres().attrs(attrs)
  3061. .c('x').attrs({'xmlns':'http://jabber.org/protocol/muc'}).up()
  3062. .c('error').attrs({'by': groupchat_jid, type:'cancel'})
  3063. .c('conflict').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
  3064. _converse.connection._dataRecv(test_utils.createRequest(presence));
  3065. expect(view.join).toHaveBeenCalledWith('dummy-3');
  3066. attrs.from = `${groupchat_jid}/dummy-3`;
  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': groupchat_jid, '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-4');
  3074. done();
  3075. }));
  3076. it("will show an error message if the user is not allowed to have created the groupchat",
  3077. mock.initConverseWithPromises(
  3078. null, ['rosterGroupsFetched'], {},
  3079. async function (done, _converse) {
  3080. const groupchat_jid = 'impermissable@muc.localhost'
  3081. await test_utils.openChatRoomViaModal(_converse, groupchat_jid, 'dummy')
  3082. var presence = $pres().attrs({
  3083. from: `${groupchat_jid}/dummy`,
  3084. id: u.getUniqueId(),
  3085. to:'dummy@localhost/pda',
  3086. type:'error'
  3087. }).c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
  3088. .c('error').attrs({by:'lounge@localhost', type:'cancel'})
  3089. .c('not-allowed').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
  3090. const view = _converse.chatboxviews.get(groupchat_jid);
  3091. spyOn(view, 'showErrorMessage').and.callThrough();
  3092. _converse.connection._dataRecv(test_utils.createRequest(presence));
  3093. expect(view.el.querySelector('.chatroom-body .disconnect-container .disconnect-msg:last-child').textContent)
  3094. .toBe('You are not allowed to create new groupchats.');
  3095. done();
  3096. }));
  3097. it("will show an error message if the user's nickname doesn't conform to groupchat policy",
  3098. mock.initConverseWithPromises(
  3099. null, ['rosterGroupsFetched'], {},
  3100. async function (done, _converse) {
  3101. const groupchat_jid = 'conformist@muc.localhost'
  3102. await test_utils.openChatRoomViaModal(_converse, groupchat_jid, 'dummy');
  3103. const presence = $pres().attrs({
  3104. from: `${groupchat_jid}/dummy`,
  3105. id: u.getUniqueId(),
  3106. to:'dummy@localhost/pda',
  3107. type:'error'
  3108. }).c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
  3109. .c('error').attrs({by:'lounge@localhost', type:'cancel'})
  3110. .c('not-acceptable').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
  3111. const view = _converse.chatboxviews.get(groupchat_jid);
  3112. spyOn(view, 'showErrorMessage').and.callThrough();
  3113. _converse.connection._dataRecv(test_utils.createRequest(presence));
  3114. expect(view.el.querySelector('.chatroom-body .disconnect-container .disconnect-msg:last-child').textContent)
  3115. .toBe("Your nickname doesn't conform to this groupchat's policies.");
  3116. done();
  3117. }));
  3118. it("will show an error message if the groupchat doesn't yet exist",
  3119. mock.initConverseWithPromises(
  3120. null, ['rosterGroupsFetched'], {},
  3121. async function (done, _converse) {
  3122. const groupchat_jid = 'nonexistent@muc.localhost'
  3123. await test_utils.openChatRoomViaModal(_converse, groupchat_jid, 'dummy');
  3124. const presence = $pres().attrs({
  3125. from: `${groupchat_jid}/dummy`,
  3126. id: u.getUniqueId(),
  3127. to: 'dummy@localhost/pda',
  3128. type:'error'
  3129. }).c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
  3130. .c('error').attrs({by:'lounge@localhost', type:'cancel'})
  3131. .c('item-not-found').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
  3132. const view = _converse.chatboxviews.get(groupchat_jid);
  3133. spyOn(view, 'showErrorMessage').and.callThrough();
  3134. _converse.connection._dataRecv(test_utils.createRequest(presence));
  3135. expect(view.el.querySelector('.chatroom-body .disconnect-container .disconnect-msg:last-child').textContent)
  3136. .toBe("This groupchat does not (yet) exist.");
  3137. done();
  3138. }));
  3139. it("will show an error message if the groupchat has reached its maximum number of participants",
  3140. mock.initConverseWithPromises(
  3141. null, ['rosterGroupsFetched'], {},
  3142. async function (done, _converse) {
  3143. const groupchat_jid = 'maxed-out@muc.localhost'
  3144. await test_utils.openChatRoomViaModal(_converse, groupchat_jid, 'dummy')
  3145. const presence = $pres().attrs({
  3146. from: `${groupchat_jid}/dummy`,
  3147. id: u.getUniqueId(),
  3148. to:'dummy@localhost/pda',
  3149. type:'error'
  3150. }).c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
  3151. .c('error').attrs({by:'lounge@localhost', type:'cancel'})
  3152. .c('service-unavailable').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
  3153. const view = _converse.chatboxviews.get(groupchat_jid);
  3154. spyOn(view, 'showErrorMessage').and.callThrough();
  3155. _converse.connection._dataRecv(test_utils.createRequest(presence));
  3156. expect(view.el.querySelector('.chatroom-body .disconnect-container .disconnect-msg:last-child').textContent)
  3157. .toBe("This groupchat has reached its maximum number of participants.");
  3158. done();
  3159. }));
  3160. });
  3161. describe("Someone being invited to a groupchat", function () {
  3162. it("will first be added to the member list if the groupchat is members only",
  3163. mock.initConverseWithPromises(
  3164. null, ['rosterGroupsFetched', 'chatBoxesFetched'], {},
  3165. function (done, _converse) {
  3166. var sent_IQs = [], IQ_ids = [];
  3167. let invitee_jid, sent_stanza, sent_id, view;
  3168. const sendIQ = _converse.connection.sendIQ;
  3169. const IQ_stanzas = _converse.connection.IQ_stanzas;
  3170. const room_jid = 'coven@chat.shakespeare.lit';
  3171. spyOn(_converse.connection, 'sendIQ').and.callFake(function (iq, callback, errback) {
  3172. sent_IQs.push(iq);
  3173. IQ_ids.push(sendIQ.bind(this)(iq, callback, errback));
  3174. });
  3175. _converse.api.rooms.open(room_jid, {'nick': 'dummy'})
  3176. .then(() => {
  3177. return test_utils.waitUntil(() => _.filter(
  3178. IQ_stanzas,
  3179. iq => iq.nodeTree.querySelector(
  3180. `iq[to="${room_jid}"] query[xmlns="http://jabber.org/protocol/disco#info"]`
  3181. )).pop());
  3182. }).then(node => {
  3183. // Check that the groupchat queried for the feautures.
  3184. const stanza = node.nodeTree;
  3185. expect(node.toLocaleString()).toBe(
  3186. `<iq from="dummy@localhost/resource" id="${stanza.getAttribute("id")}" to="${room_jid}" type="get" xmlns="jabber:client">`+
  3187. `<query xmlns="http://jabber.org/protocol/disco#info"/>`+
  3188. `</iq>`);
  3189. view = _converse.chatboxviews.get('coven@chat.shakespeare.lit');
  3190. // State that the chat is members-only via the features IQ
  3191. var features_stanza = $iq({
  3192. from: 'coven@chat.shakespeare.lit',
  3193. 'id': IQ_ids.pop(),
  3194. 'to': 'dummy@localhost/desktop',
  3195. 'type': 'result'
  3196. })
  3197. .c('query', { 'xmlns': 'http://jabber.org/protocol/disco#info'})
  3198. .c('identity', {
  3199. 'category': 'conference',
  3200. 'name': 'A Dark Cave',
  3201. 'type': 'text'
  3202. }).up()
  3203. .c('feature', {'var': 'http://jabber.org/protocol/muc'}).up()
  3204. .c('feature', {'var': 'muc_hidden'}).up()
  3205. .c('feature', {'var': 'muc_temporary'}).up()
  3206. .c('feature', {'var': 'muc_membersonly'}).up();
  3207. _converse.connection._dataRecv(test_utils.createRequest(features_stanza));
  3208. return test_utils.waitUntil(() => (view.model.get('connection_status') === converse.ROOMSTATUS.CONNECTING));
  3209. }).then(() => {
  3210. expect(view.model.get('membersonly')).toBeTruthy();
  3211. test_utils.createContacts(_converse, 'current');
  3212. spyOn(_converse.connection, 'send').and.callFake(function (stanza) {
  3213. if (stanza.nodeTree && stanza.nodeTree.nodeName === 'message') {
  3214. sent_id = stanza.nodeTree.getAttribute('id');
  3215. sent_stanza = stanza;
  3216. }
  3217. });
  3218. var name = mock.cur_names[0];
  3219. invitee_jid = name.replace(/ /g,'.').toLowerCase() + '@localhost';
  3220. var reason = "Please join this groupchat";
  3221. view.model.directInvite(invitee_jid, reason);
  3222. // Check in reverse order that we requested all three lists
  3223. // (member, owner and admin).
  3224. var admin_iq_id = IQ_ids.pop();
  3225. var owner_iq_id = IQ_ids.pop();
  3226. var member_iq_id = IQ_ids.pop();
  3227. expect(sent_IQs.pop().toLocaleString()).toBe(
  3228. `<iq id="${admin_iq_id}" to="coven@chat.shakespeare.lit" type="get" xmlns="jabber:client">`+
  3229. `<query xmlns="http://jabber.org/protocol/muc#admin">`+
  3230. `<item affiliation="admin"/>`+
  3231. `</query>`+
  3232. `</iq>`);
  3233. expect(sent_IQs.pop().toLocaleString()).toBe(
  3234. `<iq id="${owner_iq_id}" to="coven@chat.shakespeare.lit" type="get" xmlns="jabber:client">`+
  3235. `<query xmlns="http://jabber.org/protocol/muc#admin">`+
  3236. `<item affiliation="owner"/>`+
  3237. `</query>`+
  3238. `</iq>`);
  3239. expect(sent_IQs.pop().toLocaleString()).toBe(
  3240. `<iq id="${member_iq_id}" to="coven@chat.shakespeare.lit" type="get" xmlns="jabber:client">`+
  3241. `<query xmlns="http://jabber.org/protocol/muc#admin">`+
  3242. `<item affiliation="member"/>`+
  3243. `</query>`+
  3244. `</iq>`);
  3245. /* Now the service sends the member list to the user
  3246. *
  3247. * <iq from='coven@chat.shakespeare.lit'
  3248. * id='member3'
  3249. * to='crone1@shakespeare.lit/desktop'
  3250. * type='result'>
  3251. * <query xmlns='http://jabber.org/protocol/muc#admin'>
  3252. * <item affiliation='member'
  3253. * jid='hag66@shakespeare.lit'
  3254. * nick='thirdwitch'
  3255. * role='participant'/>
  3256. * </query>
  3257. * </iq>
  3258. */
  3259. var member_list_stanza = $iq({
  3260. 'from': 'coven@chat.shakespeare.lit',
  3261. 'id': member_iq_id,
  3262. 'to': 'dummy@localhost/resource',
  3263. 'type': 'result'
  3264. }).c('query', {'xmlns': Strophe.NS.MUC_ADMIN})
  3265. .c('item', {
  3266. 'affiliation': 'member',
  3267. 'jid': 'hag66@shakespeare.lit',
  3268. 'nick': 'thirdwitch',
  3269. 'role': 'participant'
  3270. });
  3271. _converse.connection._dataRecv(test_utils.createRequest(member_list_stanza));
  3272. var admin_list_stanza = $iq({
  3273. 'from': 'coven@chat.shakespeare.lit',
  3274. 'id': admin_iq_id,
  3275. 'to': 'dummy@localhost/resource',
  3276. 'type': 'result'
  3277. }).c('query', {'xmlns': Strophe.NS.MUC_ADMIN})
  3278. .c('item', {
  3279. 'affiliation': 'admin',
  3280. 'jid': 'wiccarocks@shakespeare.lit',
  3281. 'nick': 'secondwitch'
  3282. });
  3283. _converse.connection._dataRecv(test_utils.createRequest(admin_list_stanza));
  3284. var owner_list_stanza = $iq({
  3285. 'from': 'coven@chat.shakespeare.lit',
  3286. 'id': owner_iq_id,
  3287. 'to': 'dummy@localhost/resource',
  3288. 'type': 'result'
  3289. }).c('query', {'xmlns': Strophe.NS.MUC_ADMIN})
  3290. .c('item', {
  3291. 'affiliation': 'owner',
  3292. 'jid': 'crone1@shakespeare.lit',
  3293. });
  3294. _converse.connection._dataRecv(test_utils.createRequest(owner_list_stanza));
  3295. return test_utils.waitUntil(() => IQ_ids.length, 300);
  3296. }).then(() => {
  3297. return test_utils.waitUntil(() => _.get(_.filter(
  3298. IQ_stanzas,
  3299. iq => iq.nodeTree.querySelector(
  3300. `iq[to="${room_jid}"] query[xmlns="http://jabber.org/protocol/muc#admin"]`
  3301. )).pop(), 'nodeTree'));
  3302. }).then(stanza => {
  3303. expect(stanza.outerHTML,
  3304. `<iq id="${IQ_ids.pop()}" to="coven@chat.shakespeare.lit" type="set" xmlns="jabber:client">`+
  3305. `<query xmlns="http://jabber.org/protocol/muc#admin">`+
  3306. `<item affiliation="member" jid="${invitee_jid}">`+
  3307. `<reason>Please join this groupchat</reason>`+
  3308. `</item>`+
  3309. `</query>`+
  3310. `</iq>`);
  3311. // Finally check that the user gets invited.
  3312. expect(sent_stanza.toLocaleString()).toBe( // Strophe adds the xmlns attr (although not in spec)
  3313. `<message from="dummy@localhost/resource" id="${sent_id}" to="${invitee_jid}" xmlns="jabber:client">`+
  3314. `<x jid="coven@chat.shakespeare.lit" reason="Please join this groupchat" xmlns="jabber:x:conference"/>`+
  3315. `</message>`
  3316. );
  3317. done();
  3318. }).catch(_.partial(_converse.log, _, Strophe.LogLevel.FATAL));
  3319. }));
  3320. });
  3321. describe("The affiliations delta", function () {
  3322. it("can be computed in various ways",
  3323. mock.initConverseWithPromises(
  3324. null, ['rosterGroupsFetched'], {},
  3325. function (done, _converse) {
  3326. test_utils.openChatRoom(_converse, 'coven', 'chat.shakespeare.lit', 'dummy');
  3327. var roomview = _converse.chatboxviews.get('coven@chat.shakespeare.lit');
  3328. var exclude_existing = false;
  3329. var remove_absentees = false;
  3330. var new_list = [];
  3331. var old_list = [];
  3332. var delta = u.computeAffiliationsDelta(exclude_existing, remove_absentees, new_list, old_list);
  3333. expect(delta.length).toBe(0);
  3334. new_list = [{'jid': 'wiccarocks@shakespeare.lit', 'affiliation': 'member'}];
  3335. old_list = [{'jid': 'wiccarocks@shakespeare.lit', 'affiliation': 'member'}];
  3336. delta = u.computeAffiliationsDelta(exclude_existing, remove_absentees, new_list, old_list);
  3337. expect(delta.length).toBe(0);
  3338. // When remove_absentees is false, then affiliations in the old
  3339. // list which are not in the new one won't be removed.
  3340. old_list = [{'jid': 'oldhag666@shakespeare.lit', 'affiliation': 'owner'},
  3341. {'jid': 'wiccarocks@shakespeare.lit', 'affiliation': 'member'}];
  3342. delta = u.computeAffiliationsDelta(exclude_existing, remove_absentees, new_list, old_list);
  3343. expect(delta.length).toBe(0);
  3344. // With exclude_existing set to false, any changed affiliations
  3345. // will be included in the delta (i.e. existing affiliations
  3346. // are included in the comparison).
  3347. old_list = [{'jid': 'wiccarocks@shakespeare.lit', 'affiliation': 'owner'}];
  3348. delta = u.computeAffiliationsDelta(exclude_existing, remove_absentees, new_list, old_list);
  3349. expect(delta.length).toBe(1);
  3350. expect(delta[0].jid).toBe('wiccarocks@shakespeare.lit');
  3351. expect(delta[0].affiliation).toBe('member');
  3352. // To also remove affiliations from the old list which are not
  3353. // in the new list, we set remove_absentees to true
  3354. remove_absentees = true;
  3355. old_list = [{'jid': 'oldhag666@shakespeare.lit', 'affiliation': 'owner'},
  3356. {'jid': 'wiccarocks@shakespeare.lit', 'affiliation': 'member'}];
  3357. delta = u.computeAffiliationsDelta(exclude_existing, remove_absentees, new_list, old_list);
  3358. expect(delta.length).toBe(1);
  3359. expect(delta[0].jid).toBe('oldhag666@shakespeare.lit');
  3360. expect(delta[0].affiliation).toBe('none');
  3361. delta = u.computeAffiliationsDelta(exclude_existing, remove_absentees, [], old_list);
  3362. expect(delta.length).toBe(2);
  3363. expect(delta[0].jid).toBe('oldhag666@shakespeare.lit');
  3364. expect(delta[0].affiliation).toBe('none');
  3365. expect(delta[1].jid).toBe('wiccarocks@shakespeare.lit');
  3366. expect(delta[1].affiliation).toBe('none');
  3367. // To only add a user if they don't already have an
  3368. // affiliation, we set 'exclude_existing' to true
  3369. exclude_existing = true;
  3370. old_list = [{'jid': 'wiccarocks@shakespeare.lit', 'affiliation': 'owner'}];
  3371. delta = u.computeAffiliationsDelta(exclude_existing, remove_absentees, new_list, old_list);
  3372. expect(delta.length).toBe(0);
  3373. done();
  3374. }));
  3375. });
  3376. describe("The \"Groupchats\" section", function () {
  3377. it("contains a link to a modal through which a new chatroom can be created",
  3378. mock.initConverseWithPromises(
  3379. null, ['rosterGroupsFetched', 'chatBoxesFetched'], {},
  3380. function (done, _converse) {
  3381. test_utils.openControlBox();
  3382. _converse.emit('rosterContactsFetched');
  3383. var roomspanel = _converse.chatboxviews.get('controlbox').roomspanel;
  3384. roomspanel.el.querySelector('.show-add-muc-modal').click();
  3385. test_utils.closeControlBox(_converse);
  3386. const modal = roomspanel.add_room_modal;
  3387. test_utils.waitUntil(() => u.isVisible(modal.el), 1000)
  3388. .then(() => {
  3389. spyOn(_converse.ChatRoom.prototype, 'getRoomFeatures').and.callFake(() => Promise.resolve());
  3390. roomspanel.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  3391. modal.el.querySelector('input[name="chatroom"]').value = 'lounce@muc.localhost';
  3392. modal.el.querySelector('form input[type="submit"]').click();
  3393. return test_utils.waitUntil(() => _converse.chatboxes.length);
  3394. }).then(() => {
  3395. expect(sizzle('.chatroom', _converse.el).filter(u.isVisible).length).toBe(1); // There should now be an open chatroom
  3396. done();
  3397. }).catch(_.partial(console.error, _));
  3398. }));
  3399. it("contains a link to a modal which can list groupchats publically available on the server",
  3400. mock.initConverseWithPromises(
  3401. null, ['rosterGroupsFetched', 'chatBoxesFetched'], {},
  3402. function (done, _converse) {
  3403. var sendIQ = _converse.connection.sendIQ;
  3404. var sent_stanza, IQ_id;
  3405. spyOn(_converse.connection, 'sendIQ').and.callFake(function (iq, callback, errback) {
  3406. sent_stanza = iq;
  3407. IQ_id = sendIQ.bind(this)(iq, callback, errback);
  3408. });
  3409. test_utils.openControlBox();
  3410. var roomspanel = _converse.chatboxviews.get('controlbox').roomspanel;
  3411. roomspanel.el.querySelector('.show-list-muc-modal').click();
  3412. test_utils.closeControlBox(_converse);
  3413. const modal = roomspanel.list_rooms_modal;
  3414. test_utils.waitUntil(() => u.isVisible(modal.el), 1000)
  3415. .then(() => {
  3416. spyOn(_converse.ChatRoom.prototype, 'getRoomFeatures').and.callFake(() => Promise.resolve());
  3417. roomspanel.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  3418. // See: http://xmpp.org/extensions/xep-0045.html#disco-rooms
  3419. expect(modal.el.querySelectorAll('.available-chatrooms li').length).toBe(0);
  3420. const input = modal.el.querySelector('input[name="server"]').value = 'chat.shakespear.lit';
  3421. modal.el.querySelector('input[type="submit"]').click();
  3422. return test_utils.waitUntil(() => _converse.chatboxes.length);
  3423. }).then(() => {
  3424. expect(sent_stanza.toLocaleString()).toBe(
  3425. `<iq from="dummy@localhost/resource" id="${IQ_id}" to="chat.shakespear.lit" type="get" xmlns="jabber:client">`+
  3426. `<query xmlns="http://jabber.org/protocol/disco#items"/>`+
  3427. `</iq>`
  3428. );
  3429. var iq = $iq({
  3430. from:'muc.localhost',
  3431. to:'dummy@localhost/pda',
  3432. id: IQ_id,
  3433. type:'result'
  3434. }).c('query')
  3435. .c('item', { jid:'heath@chat.shakespeare.lit', name:'A Lonely Heath'}).up()
  3436. .c('item', { jid:'coven@chat.shakespeare.lit', name:'A Dark Cave'}).up()
  3437. .c('item', { jid:'forres@chat.shakespeare.lit', name:'The Palace'}).up()
  3438. .c('item', { jid:'inverness@chat.shakespeare.lit', name:'Macbeth&apos;s Castle'}).nodeTree;
  3439. _converse.connection._dataRecv(test_utils.createRequest(iq));
  3440. expect(modal.el.querySelectorAll('.available-chatrooms li').length).toBe(5);
  3441. const rooms = modal.el.querySelectorAll('.available-chatrooms li');
  3442. expect(rooms[0].textContent.trim()).toBe("Groupchats found:");
  3443. expect(rooms[1].textContent.trim()).toBe("A Lonely Heath");
  3444. expect(rooms[2].textContent.trim()).toBe("A Dark Cave");
  3445. expect(rooms[3].textContent.trim()).toBe("The Palace");
  3446. expect(rooms[4].textContent.trim()).toBe("Macbeth's Castle");
  3447. rooms[4].querySelector('.open-room').click();
  3448. return test_utils.waitUntil(() => _converse.chatboxes.length > 1);
  3449. }).then(() => {
  3450. expect(sizzle('.chatroom', _converse.el).filter(u.isVisible).length).toBe(1); // There should now be an open chatroom
  3451. var view = _converse.chatboxviews.get('inverness@chat.shakespeare.lit');
  3452. expect(view.el.querySelector('.chat-head-chatroom').textContent.trim()).toBe("Macbeth's Castle");
  3453. done();
  3454. }).catch(_.partial(console.error, _));
  3455. }));
  3456. it("shows the number of unread mentions received",
  3457. mock.initConverseWithPromises(
  3458. null, ['rosterGroupsFetched'], {'allow_bookmarks': false},
  3459. function (done, _converse) {
  3460. // XXX: we set `allow_bookmarks` to false, so that the groupchats
  3461. // list gets rendered. Otherwise we would have to mock
  3462. // the bookmark stanza exchange.
  3463. test_utils.openControlBox();
  3464. var roomspanel = _converse.chatboxviews.get('controlbox').roomspanel;
  3465. expect(roomspanel.el.querySelectorAll('.available-room').length).toBe(0);
  3466. let view, nick;
  3467. const room_jid = 'kitchen@conference.shakespeare.lit';
  3468. const message = 'fires: Your attention is required';
  3469. test_utils.openAndEnterChatRoom(_converse, 'kitchen', 'conference.shakespeare.lit', 'fires')
  3470. .then(() => {
  3471. expect(roomspanel.el.querySelectorAll('.available-room').length).toBe(1);
  3472. expect(roomspanel.el.querySelectorAll('.msgs-indicator').length).toBe(0);
  3473. view = _converse.chatboxviews.get(room_jid);
  3474. view.model.set({'minimized': true});
  3475. var contact_jid = mock.cur_names[5].replace(/ /g,'.').toLowerCase() + '@localhost';
  3476. nick = mock.chatroom_names[0];
  3477. view.model.onMessage($msg({
  3478. from: room_jid+'/'+nick,
  3479. id: (new Date()).getTime(),
  3480. to: 'dummy@localhost',
  3481. type: 'groupchat'
  3482. }).c('body').t(message).tree());
  3483. return test_utils.waitUntil(() => view.model.messages.length);
  3484. }).then(() => {
  3485. expect(roomspanel.el.querySelectorAll('.available-room').length).toBe(1);
  3486. expect(roomspanel.el.querySelectorAll('.msgs-indicator').length).toBe(1);
  3487. expect(roomspanel.el.querySelector('.msgs-indicator').textContent).toBe('1');
  3488. view.model.onMessage($msg({
  3489. 'from': room_jid+'/'+nick,
  3490. 'id': (new Date()).getTime(),
  3491. 'to': 'dummy@localhost',
  3492. 'type': 'groupchat'
  3493. }).c('body').t(message).tree());
  3494. return test_utils.waitUntil(() => view.model.messages.length > 1);
  3495. }).then(() => {
  3496. expect(roomspanel.el.querySelectorAll('.available-room').length).toBe(1);
  3497. expect(roomspanel.el.querySelectorAll('.msgs-indicator').length).toBe(1);
  3498. expect(roomspanel.el.querySelector('.msgs-indicator').textContent).toBe('2');
  3499. view.model.set({'minimized': false});
  3500. expect(roomspanel.el.querySelectorAll('.available-room').length).toBe(1);
  3501. expect(roomspanel.el.querySelectorAll('.msgs-indicator').length).toBe(0);
  3502. done();
  3503. }).catch(_.partial(_converse.log, _, Strophe.LogLevel.FATAL))
  3504. }));
  3505. describe("A Chat Status Notification", function () {
  3506. describe("A composing notification", function () {
  3507. it("will be shown if received",
  3508. mock.initConverseWithPromises(
  3509. null, ['rosterGroupsFetched'], {},
  3510. async function (done, _converse) {
  3511. const room_jid = 'coven@chat.shakespeare.lit';
  3512. await test_utils.openAndEnterChatRoom(_converse, 'coven', 'chat.shakespeare.lit', 'some1');
  3513. const view = _converse.chatboxviews.get('coven@chat.shakespeare.lit');
  3514. const chat_content = view.el.querySelector('.chat-content');
  3515. expect(sizzle('div.chat-info:first', chat_content).pop().textContent)
  3516. .toBe("some1 has entered the groupchat");
  3517. let presence = $pres({
  3518. to: 'dummy@localhost/_converse.js-29092160',
  3519. from: 'coven@chat.shakespeare.lit/newguy'
  3520. })
  3521. .c('x', {xmlns: Strophe.NS.MUC_USER})
  3522. .c('item', {
  3523. 'affiliation': 'none',
  3524. 'jid': 'newguy@localhost/_converse.js-290929789',
  3525. 'role': 'participant'
  3526. });
  3527. _converse.connection._dataRecv(test_utils.createRequest(presence));
  3528. expect(chat_content.querySelectorAll('div.chat-info').length).toBe(2);
  3529. expect(sizzle('div.chat-info:last', chat_content).pop().textContent)
  3530. .toBe("newguy has entered the groupchat");
  3531. presence = $pres({
  3532. to: 'dummy@localhost/_converse.js-29092160',
  3533. from: 'coven@chat.shakespeare.lit/nomorenicks'
  3534. })
  3535. .c('x', {xmlns: Strophe.NS.MUC_USER})
  3536. .c('item', {
  3537. 'affiliation': 'none',
  3538. 'jid': 'nomorenicks@localhost/_converse.js-290929789',
  3539. 'role': 'participant'
  3540. });
  3541. _converse.connection._dataRecv(test_utils.createRequest(presence));
  3542. expect(chat_content.querySelectorAll('div.chat-info').length).toBe(3);
  3543. expect(sizzle('div.chat-info:last', chat_content).pop().textContent)
  3544. .toBe("nomorenicks has entered the groupchat");
  3545. // See XEP-0085 http://xmpp.org/extensions/xep-0085.html#definitions
  3546. // <composing> state
  3547. var msg = $msg({
  3548. from: room_jid+'/newguy',
  3549. id: (new Date()).getTime(),
  3550. to: 'dummy@localhost',
  3551. type: 'groupchat'
  3552. }).c('body').c('composing', {'xmlns': Strophe.NS.CHATSTATES}).tree();
  3553. view.model.onMessage(msg);
  3554. await test_utils.waitUntil(() => view.el.querySelectorAll('.chat-state-notification').length);
  3555. // Check that the notification appears inside the chatbox in the DOM
  3556. let events = view.el.querySelectorAll('.chat-event');
  3557. expect(events.length).toBe(3);
  3558. expect(events[0].textContent).toEqual('some1 has entered the groupchat');
  3559. expect(events[1].textContent).toEqual('newguy has entered the groupchat');
  3560. expect(events[2].textContent).toEqual('nomorenicks has entered the groupchat');
  3561. let notifications = view.el.querySelectorAll('.chat-state-notification');
  3562. expect(notifications.length).toBe(1);
  3563. expect(notifications[0].textContent).toEqual('newguy is typing');
  3564. const timeout_functions = [];
  3565. spyOn(window, 'setTimeout').and.callFake(function (func, delay) {
  3566. timeout_functions.push(func);
  3567. });
  3568. // Check that it doesn't appear twice
  3569. msg = $msg({
  3570. from: room_jid+'/newguy',
  3571. id: (new Date()).getTime(),
  3572. to: 'dummy@localhost',
  3573. type: 'groupchat'
  3574. }).c('body').c('composing', {'xmlns': Strophe.NS.CHATSTATES}).tree();
  3575. view.model.onMessage(msg);
  3576. await new Promise((resolve, reject) => view.once('messageInserted', resolve));
  3577. events = view.el.querySelectorAll('.chat-event');
  3578. expect(events.length).toBe(3);
  3579. expect(events[0].textContent).toEqual('some1 has entered the groupchat');
  3580. expect(events[1].textContent).toEqual('newguy has entered the groupchat');
  3581. expect(events[2].textContent).toEqual('nomorenicks has entered the groupchat');
  3582. notifications = view.el.querySelectorAll('.chat-state-notification');
  3583. expect(notifications.length).toBe(1);
  3584. expect(notifications[0].textContent).toEqual('newguy is typing');
  3585. expect(timeout_functions.length).toBe(1);
  3586. // <composing> state for a different occupant
  3587. msg = $msg({
  3588. from: room_jid+'/nomorenicks',
  3589. id: (new Date()).getTime(),
  3590. to: 'dummy@localhost',
  3591. type: 'groupchat'
  3592. }).c('body').c('composing', {'xmlns': Strophe.NS.CHATSTATES}).tree();
  3593. view.model.onMessage(msg);
  3594. await new Promise((resolve, reject) => view.once('messageInserted', resolve));
  3595. events = view.el.querySelectorAll('.chat-event');
  3596. expect(events.length).toBe(3);
  3597. expect(events[0].textContent).toEqual('some1 has entered the groupchat');
  3598. expect(events[1].textContent).toEqual('newguy has entered the groupchat');
  3599. expect(events[2].textContent).toEqual('nomorenicks has entered the groupchat');
  3600. notifications = view.el.querySelectorAll('.chat-state-notification');
  3601. expect(notifications.length).toBe(2);
  3602. expect(notifications[0].textContent).toEqual('newguy is typing');
  3603. expect(notifications[1].textContent).toEqual('nomorenicks is typing');
  3604. expect(timeout_functions.length).toBe(2);
  3605. // Check that new messages appear under the chat state
  3606. // notifications
  3607. msg = $msg({
  3608. from: 'lounge@localhost/some1',
  3609. id: (new Date()).getTime(),
  3610. to: 'dummy@localhost',
  3611. type: 'groupchat'
  3612. }).c('body').t('hello world').tree();
  3613. view.model.onMessage(msg);
  3614. await new Promise((resolve, reject) => view.once('messageInserted', resolve));
  3615. const messages = view.el.querySelectorAll('.message');
  3616. expect(messages.length).toBe(7);
  3617. expect(view.el.querySelectorAll('.chat-msg').length).toBe(1);
  3618. expect(view.el.querySelector('.chat-msg .chat-msg__text').textContent).toBe('hello world');
  3619. // Test that the composing notifications get removed
  3620. // via timeout.
  3621. timeout_functions[0]();
  3622. events = view.el.querySelectorAll('.chat-event');
  3623. expect(events.length).toBe(3);
  3624. expect(events[0].textContent).toEqual('some1 has entered the groupchat');
  3625. expect(events[1].textContent).toEqual('newguy has entered the groupchat');
  3626. expect(events[2].textContent).toEqual('nomorenicks has entered the groupchat');
  3627. notifications = view.el.querySelectorAll('.chat-state-notification');
  3628. expect(notifications.length).toBe(1);
  3629. expect(notifications[0].textContent).toEqual('nomorenicks is typing');
  3630. timeout_functions[1]();
  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(0);
  3638. done();
  3639. }));
  3640. });
  3641. describe("A paused notification", function () {
  3642. it("will be shown if received",
  3643. mock.initConverseWithPromises(
  3644. null, ['rosterGroupsFetched', 'chatBoxesFetched'], {},
  3645. async function (done, _converse) {
  3646. await test_utils.openChatRoom(_converse, "coven", 'chat.shakespeare.lit', 'some1');
  3647. const room_jid = 'coven@chat.shakespeare.lit';
  3648. const view = _converse.chatboxviews.get('coven@chat.shakespeare.lit');
  3649. const chat_content = view.el.querySelector('.chat-content');
  3650. /* <presence to="dummy@localhost/_converse.js-29092160"
  3651. * from="coven@chat.shakespeare.lit/some1">
  3652. * <x xmlns="http://jabber.org/protocol/muc#user">
  3653. * <item affiliation="owner" jid="dummy@localhost/_converse.js-29092160" role="moderator"/>
  3654. * <status code="110"/>
  3655. * </x>
  3656. * </presence></body>
  3657. */
  3658. let presence = $pres({
  3659. to: 'dummy@localhost/_converse.js-29092160',
  3660. from: 'coven@chat.shakespeare.lit/some1'
  3661. }).c('x', {xmlns: Strophe.NS.MUC_USER})
  3662. .c('item', {
  3663. 'affiliation': 'owner',
  3664. 'jid': 'dummy@localhost/_converse.js-29092160',
  3665. 'role': 'moderator'
  3666. }).up()
  3667. .c('status', {code: '110'});
  3668. _converse.connection._dataRecv(test_utils.createRequest(presence));
  3669. expect(sizzle('div.chat-info:first', chat_content).pop().textContent)
  3670. .toBe("some1 has entered the groupchat");
  3671. presence = $pres({
  3672. to: 'dummy@localhost/_converse.js-29092160',
  3673. from: 'coven@chat.shakespeare.lit/newguy'
  3674. })
  3675. .c('x', {xmlns: Strophe.NS.MUC_USER})
  3676. .c('item', {
  3677. 'affiliation': 'none',
  3678. 'jid': 'newguy@localhost/_converse.js-290929789',
  3679. 'role': 'participant'
  3680. });
  3681. _converse.connection._dataRecv(test_utils.createRequest(presence));
  3682. expect(chat_content.querySelectorAll('div.chat-info').length).toBe(2);
  3683. expect(sizzle('div.chat-info:last', chat_content).pop().textContent)
  3684. .toBe("newguy has entered the groupchat");
  3685. presence = $pres({
  3686. to: 'dummy@localhost/_converse.js-29092160',
  3687. from: 'coven@chat.shakespeare.lit/nomorenicks'
  3688. })
  3689. .c('x', {xmlns: Strophe.NS.MUC_USER})
  3690. .c('item', {
  3691. 'affiliation': 'none',
  3692. 'jid': 'nomorenicks@localhost/_converse.js-290929789',
  3693. 'role': 'participant'
  3694. });
  3695. _converse.connection._dataRecv(test_utils.createRequest(presence));
  3696. expect(chat_content.querySelectorAll('div.chat-info').length).toBe(3);
  3697. expect(sizzle('div.chat-info:last', chat_content).pop().textContent)
  3698. .toBe("nomorenicks has entered the groupchat");
  3699. // See XEP-0085 http://xmpp.org/extensions/xep-0085.html#definitions
  3700. // <composing> state
  3701. var msg = $msg({
  3702. from: room_jid+'/newguy',
  3703. id: (new Date()).getTime(),
  3704. to: 'dummy@localhost',
  3705. type: 'groupchat'
  3706. }).c('body').c('composing', {'xmlns': Strophe.NS.CHATSTATES}).tree();
  3707. view.model.onMessage(msg);
  3708. await new Promise((resolve, reject) => view.once('messageInserted', resolve));
  3709. // Check that the notification appears inside the chatbox in the DOM
  3710. var events = view.el.querySelectorAll('.chat-event');
  3711. expect(events.length).toBe(3);
  3712. expect(events[0].textContent).toEqual('some1 has entered the groupchat');
  3713. expect(events[1].textContent).toEqual('newguy has entered the groupchat');
  3714. expect(events[2].textContent).toEqual('nomorenicks has entered the groupchat');
  3715. var notifications = view.el.querySelectorAll('.chat-state-notification');
  3716. expect(notifications.length).toBe(1);
  3717. expect(notifications[0].textContent).toEqual('newguy is typing');
  3718. // Check that it doesn't appear twice
  3719. msg = $msg({
  3720. from: room_jid+'/newguy',
  3721. id: (new Date()).getTime(),
  3722. to: 'dummy@localhost',
  3723. type: 'groupchat'
  3724. }).c('body').c('composing', {'xmlns': Strophe.NS.CHATSTATES}).tree();
  3725. view.model.onMessage(msg);
  3726. await new Promise((resolve, reject) => view.once('messageInserted', resolve));
  3727. events = view.el.querySelectorAll('.chat-event');
  3728. expect(events.length).toBe(3);
  3729. expect(events[0].textContent).toEqual('some1 has entered the groupchat');
  3730. expect(events[1].textContent).toEqual('newguy has entered the groupchat');
  3731. expect(events[2].textContent).toEqual('nomorenicks has entered the groupchat');
  3732. notifications = view.el.querySelectorAll('.chat-state-notification');
  3733. expect(notifications.length).toBe(1);
  3734. expect(notifications[0].textContent).toEqual('newguy is typing');
  3735. // <composing> state for a different occupant
  3736. msg = $msg({
  3737. from: room_jid+'/nomorenicks',
  3738. id: (new Date()).getTime(),
  3739. to: 'dummy@localhost',
  3740. type: 'groupchat'
  3741. }).c('body').c('composing', {'xmlns': Strophe.NS.CHATSTATES}).tree();
  3742. view.model.onMessage(msg);
  3743. await new Promise((resolve, reject) => view.once('messageInserted', resolve));
  3744. events = view.el.querySelectorAll('.chat-event');
  3745. expect(events.length).toBe(3);
  3746. expect(events[0].textContent).toEqual('some1 has entered the groupchat');
  3747. expect(events[1].textContent).toEqual('newguy has entered the groupchat');
  3748. expect(events[2].textContent).toEqual('nomorenicks has entered the groupchat');
  3749. notifications = view.el.querySelectorAll('.chat-state-notification');
  3750. expect(notifications.length).toBe(2);
  3751. expect(notifications[0].textContent).toEqual('newguy is typing');
  3752. expect(notifications[1].textContent).toEqual('nomorenicks is typing');
  3753. // <paused> state from occupant who typed first
  3754. msg = $msg({
  3755. from: room_jid+'/newguy',
  3756. id: (new Date()).getTime(),
  3757. to: 'dummy@localhost',
  3758. type: 'groupchat'
  3759. }).c('body').c('paused', {'xmlns': Strophe.NS.CHATSTATES}).tree();
  3760. view.model.onMessage(msg);
  3761. await new Promise((resolve, reject) => view.once('messageInserted', resolve));
  3762. events = view.el.querySelectorAll('.chat-event');
  3763. expect(events.length).toBe(3);
  3764. expect(events[0].textContent).toEqual('some1 has entered the groupchat');
  3765. expect(events[1].textContent).toEqual('newguy has entered the groupchat');
  3766. expect(events[2].textContent).toEqual('nomorenicks has entered the groupchat');
  3767. notifications = view.el.querySelectorAll('.chat-state-notification');
  3768. expect(notifications.length).toBe(2);
  3769. expect(notifications[0].textContent).toEqual('nomorenicks is typing');
  3770. expect(notifications[1].textContent).toEqual('newguy has stopped typing');
  3771. done();
  3772. }));
  3773. });
  3774. });
  3775. });
  3776. });
  3777. }));