muc.js 267 KB

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