muc.js 257 KB

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