muc.js 241 KB

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