chatroom.js 216 KB

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