chatroom.js 218 KB

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