chatroom.js 237 KB

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