2
0

chatroom.js 237 KB

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