chatroom.js 237 KB

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