chatroom.js 213 KB

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