muc.js 285 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387
  1. /*global mock, converse, _ */
  2. const $pres = converse.env.$pres;
  3. const $iq = converse.env.$iq;
  4. const $msg = converse.env.$msg;
  5. const Model = converse.env.Model;
  6. const Strophe = converse.env.Strophe;
  7. const Promise = converse.env.Promise;
  8. const sizzle = converse.env.sizzle;
  9. const u = converse.env.utils;
  10. describe("Groupchats", function () {
  11. describe("The \"rooms\" API", function () {
  12. it("has a method 'close' which closes rooms by JID or all rooms when called with no arguments",
  13. mock.initConverse([], {}, async function (done, _converse) {
  14. await mock.openAndEnterChatRoom(_converse, 'lounge@montague.lit', 'romeo');
  15. _converse.connection.IQ_stanzas = [];
  16. await mock.openAndEnterChatRoom(_converse, 'leisure@montague.lit', 'romeo');
  17. _converse.connection.IQ_stanzas = [];
  18. await mock.openAndEnterChatRoom(_converse, 'news@montague.lit', 'romeo');
  19. expect(u.isVisible(_converse.chatboxviews.get('lounge@montague.lit'))).toBeTruthy();
  20. expect(u.isVisible(_converse.chatboxviews.get('leisure@montague.lit'))).toBeTruthy();
  21. expect(u.isVisible(_converse.chatboxviews.get('news@montague.lit'))).toBeTruthy();
  22. await _converse.api.roomviews.close('lounge@montague.lit');
  23. expect(_converse.chatboxviews.get('lounge@montague.lit')).toBeUndefined();
  24. expect(u.isVisible(_converse.chatboxviews.get('leisure@montague.lit'))).toBeTruthy();
  25. expect(u.isVisible(_converse.chatboxviews.get('news@montague.lit'))).toBeTruthy();
  26. await _converse.api.roomviews.close(['leisure@montague.lit', 'news@montague.lit']);
  27. expect(_converse.chatboxviews.get('lounge@montague.lit')).toBeUndefined();
  28. expect(_converse.chatboxviews.get('leisure@montague.lit')).toBeUndefined();
  29. expect(_converse.chatboxviews.get('news@montague.lit')).toBeUndefined();
  30. await mock.openAndEnterChatRoom(_converse, 'lounge@montague.lit', 'romeo');
  31. await mock.openAndEnterChatRoom(_converse, 'leisure@montague.lit', 'romeo');
  32. expect(u.isVisible(_converse.chatboxviews.get('lounge@montague.lit'))).toBeTruthy();
  33. expect(u.isVisible(_converse.chatboxviews.get('leisure@montague.lit'))).toBeTruthy();
  34. await _converse.api.roomviews.close();
  35. expect(_converse.chatboxviews.get('lounge@montague.lit')).toBeUndefined();
  36. expect(_converse.chatboxviews.get('leisure@montague.lit')).toBeUndefined();
  37. done();
  38. }));
  39. it("has a method 'get' which returns a wrapped groupchat (if it exists)",
  40. mock.initConverse([], {}, async function (done, _converse) {
  41. await mock.waitForRoster(_converse, 'current');
  42. const rosterview = document.querySelector('converse-roster');
  43. await u.waitUntil(() => rosterview.querySelectorAll('.roster-group .group-toggle').length, 300);
  44. let muc_jid = 'chillout@montague.lit';
  45. await mock.openAndEnterChatRoom(_converse, muc_jid, 'romeo');
  46. let room = await _converse.api.rooms.get(muc_jid);
  47. expect(room instanceof Object).toBeTruthy();
  48. let chatroomview = _converse.chatboxviews.get(muc_jid);
  49. expect(chatroomview.is_chatroom).toBeTruthy();
  50. expect(u.isVisible(chatroomview)).toBeTruthy();
  51. await chatroomview.close();
  52. // Test with mixed case
  53. muc_jid = 'Leisure@montague.lit';
  54. await mock.openAndEnterChatRoom(_converse, muc_jid, 'romeo');
  55. room = await _converse.api.rooms.get(muc_jid);
  56. expect(room instanceof Object).toBeTruthy();
  57. chatroomview = _converse.chatboxviews.get(muc_jid.toLowerCase());
  58. expect(u.isVisible(chatroomview)).toBeTruthy();
  59. muc_jid = 'leisure@montague.lit';
  60. room = await _converse.api.rooms.get(muc_jid);
  61. expect(room instanceof Object).toBeTruthy();
  62. chatroomview = _converse.chatboxviews.get(muc_jid.toLowerCase());
  63. expect(u.isVisible(chatroomview)).toBeTruthy();
  64. muc_jid = 'leiSure@montague.lit';
  65. room = await _converse.api.rooms.get(muc_jid);
  66. expect(room instanceof Object).toBeTruthy();
  67. chatroomview = _converse.chatboxviews.get(muc_jid.toLowerCase());
  68. expect(u.isVisible(chatroomview)).toBeTruthy();
  69. chatroomview.close();
  70. // Non-existing room
  71. muc_jid = 'chillout2@montague.lit';
  72. room = await _converse.api.rooms.get(muc_jid);
  73. expect(room).toBe(null);
  74. done();
  75. }));
  76. it("has a method 'open' which opens (optionally configures) and returns a wrapped chat box",
  77. mock.initConverse(['chatBoxesFetched'], {}, async function (done, _converse) {
  78. // Mock 'getDiscoInfo', otherwise the room won't be
  79. // displayed as it waits first for the features to be returned
  80. // (when it's a new room being created).
  81. spyOn(_converse.ChatRoom.prototype, 'getDiscoInfo').and.callFake(() => Promise.resolve());
  82. let jid = 'lounge@montague.lit';
  83. let chatroomview, IQ_id;
  84. await mock.openControlBox(_converse);
  85. await mock.waitForRoster(_converse, 'current');
  86. const rosterview = document.querySelector('converse-roster');
  87. await u.waitUntil(() => rosterview.querySelectorAll('.roster-group .group-toggle').length);
  88. let room = await _converse.api.rooms.open(jid);
  89. // Test on groupchat that's not yet open
  90. expect(room instanceof Model).toBeTruthy();
  91. chatroomview = _converse.chatboxviews.get(jid);
  92. expect(chatroomview.is_chatroom).toBeTruthy();
  93. await u.waitUntil(() => u.isVisible(chatroomview));
  94. // Test again, now that the room exists.
  95. room = await _converse.api.rooms.open(jid);
  96. expect(room instanceof Model).toBeTruthy();
  97. chatroomview = _converse.chatboxviews.get(jid);
  98. expect(chatroomview.is_chatroom).toBeTruthy();
  99. expect(u.isVisible(chatroomview)).toBeTruthy();
  100. await chatroomview.close();
  101. // Test with mixed case in JID
  102. jid = 'Leisure@montague.lit';
  103. room = await _converse.api.rooms.open(jid);
  104. expect(room instanceof Model).toBeTruthy();
  105. chatroomview = _converse.chatboxviews.get(jid.toLowerCase());
  106. await u.waitUntil(() => u.isVisible(chatroomview));
  107. jid = 'leisure@montague.lit';
  108. room = await _converse.api.rooms.open(jid);
  109. expect(room instanceof Model).toBeTruthy();
  110. chatroomview = _converse.chatboxviews.get(jid.toLowerCase());
  111. await u.waitUntil(() => u.isVisible(chatroomview));
  112. jid = 'leiSure@montague.lit';
  113. room = await _converse.api.rooms.open(jid);
  114. expect(room instanceof Model).toBeTruthy();
  115. chatroomview = _converse.chatboxviews.get(jid.toLowerCase());
  116. await u.waitUntil(() => u.isVisible(chatroomview));
  117. chatroomview.close();
  118. _converse.muc_instant_rooms = false;
  119. const sendIQ = _converse.connection.sendIQ;
  120. spyOn(_converse.connection, 'sendIQ').and.callFake(function (iq, callback, errback) {
  121. IQ_id = sendIQ.bind(this)(iq, callback, errback);
  122. });
  123. // Test with configuration
  124. room = await _converse.api.rooms.open('room@conference.example.org', {
  125. 'nick': 'some1',
  126. 'auto_configure': true,
  127. 'roomconfig': {
  128. 'getmemberlist': ['moderator', 'participant'],
  129. 'changesubject': false,
  130. 'membersonly': true,
  131. 'persistentroom': true,
  132. 'publicroom': true,
  133. 'roomdesc': 'Welcome to this groupchat',
  134. 'whois': 'anyone'
  135. }
  136. });
  137. expect(room instanceof Model).toBeTruthy();
  138. chatroomview = _converse.chatboxviews.get('room@conference.example.org');
  139. // We pretend this is a new room, so no disco info is returned.
  140. const features_stanza = $iq({
  141. from: 'room@conference.example.org',
  142. 'id': IQ_id,
  143. 'to': 'romeo@montague.lit/desktop',
  144. 'type': 'error'
  145. }).c('error', {'type': 'cancel'})
  146. .c('item-not-found', {'xmlns': "urn:ietf:params:xml:ns:xmpp-stanzas"});
  147. _converse.connection._dataRecv(mock.createRequest(features_stanza));
  148. /* <presence xmlns="jabber:client" to="romeo@montague.lit/pda" from="room@conference.example.org/yo">
  149. * <x xmlns="http://jabber.org/protocol/muc#user">
  150. * <item affiliation="owner" jid="romeo@montague.lit/pda" role="moderator"/>
  151. * <status code="110"/>
  152. * <status code="201"/>
  153. * </x>
  154. * </presence>
  155. */
  156. const presence = $pres({
  157. from:'room@conference.example.org/some1',
  158. to:'romeo@montague.lit/pda'
  159. })
  160. .c('x', {xmlns:'http://jabber.org/protocol/muc#user'})
  161. .c('item', {
  162. affiliation: 'owner',
  163. jid: 'romeo@montague.lit/pda',
  164. role: 'moderator'
  165. }).up()
  166. .c('status', {code:'110'}).up()
  167. .c('status', {code:'201'});
  168. _converse.connection._dataRecv(mock.createRequest(presence));
  169. expect(_converse.connection.sendIQ).toHaveBeenCalled();
  170. const IQ_stanzas = _converse.connection.IQ_stanzas;
  171. const iq = await u.waitUntil(() => IQ_stanzas.filter(s => s.querySelector(`query[xmlns="${Strophe.NS.MUC_OWNER}"]`)).pop());
  172. expect(Strophe.serialize(iq)).toBe(
  173. `<iq id="${iq.getAttribute('id')}" to="room@conference.example.org" type="get" xmlns="jabber:client">`+
  174. `<query xmlns="http://jabber.org/protocol/muc#owner"/></iq>`);
  175. const node = u.toStanza(`
  176. <iq xmlns="jabber:client"
  177. type="result"
  178. to="romeo@montague.lit/pda"
  179. from="room@conference.example.org" id="${iq.getAttribute('id')}">
  180. <query xmlns="http://jabber.org/protocol/muc#owner">
  181. <x xmlns="jabber:x:data" type="form">
  182. <title>Configuration for room@conference.example.org</title>
  183. <instructions>Complete and submit this form to configure the room.</instructions>
  184. <field var="FORM_TYPE" type="hidden">
  185. <value>http://jabber.org/protocol/muc#roomconfig</value>
  186. </field>
  187. <field type="text-single" var="muc#roomconfig_roomname" label="Name">
  188. <value>Room</value>
  189. </field>
  190. <field type="text-single" var="muc#roomconfig_roomdesc" label="Description"><value/></field>
  191. <field type="boolean" var="muc#roomconfig_persistentroom" label="Make Room Persistent?"/>
  192. <field type="boolean" var="muc#roomconfig_publicroom" label="Make Room Publicly Searchable?"><value>1</value></field>
  193. <field type="boolean" var="muc#roomconfig_changesubject" label="Allow Occupants to Change Subject?"/>
  194. <field type="list-single" var="muc#roomconfig_whois" label="Who May Discover Real JIDs?"><option label="Moderators Only">
  195. <value>moderators</value></option><option label="Anyone"><value>anyone</value></option>
  196. </field>
  197. <field label="Roles and Affiliations that May Retrieve Member List"
  198. type="list-multi"
  199. var="muc#roomconfig_getmemberlist">
  200. <value>moderator</value>
  201. <value>participant</value>
  202. <value>visitor</value>
  203. </field>
  204. <field type="text-private" var="muc#roomconfig_roomsecret" label="Password"><value/></field>
  205. <field type="boolean" var="muc#roomconfig_moderatedroom" label="Make Room Moderated?"/>
  206. <field type="boolean" var="muc#roomconfig_membersonly" label="Make Room Members-Only?"/>
  207. <field type="text-single" var="muc#roomconfig_historylength" label="Maximum Number of History Messages Returned by Room">
  208. <value>20</value></field>
  209. </x>
  210. </query>
  211. </iq>`);
  212. spyOn(chatroomview.model, 'sendConfiguration').and.callThrough();
  213. _converse.connection._dataRecv(mock.createRequest(node));
  214. await u.waitUntil(() => chatroomview.model.sendConfiguration.calls.count() === 1);
  215. const sent_stanza = IQ_stanzas.filter(s => s.getAttribute('type') === 'set').pop();
  216. expect(sizzle('field[var="muc#roomconfig_roomname"] value', sent_stanza).pop().textContent.trim()).toBe('Room');
  217. expect(sizzle('field[var="muc#roomconfig_roomdesc"] value', sent_stanza).pop().textContent.trim()).toBe('Welcome to this groupchat');
  218. expect(sizzle('field[var="muc#roomconfig_persistentroom"] value', sent_stanza).pop().textContent.trim()).toBe('1');
  219. expect(sizzle('field[var="muc#roomconfig_getmemberlist"] value', sent_stanza).map(e => e.textContent.trim()).join(' ')).toBe('moderator participant');
  220. expect(sizzle('field[var="muc#roomconfig_publicroom"] value ', sent_stanza).pop().textContent.trim()).toBe('1');
  221. expect(sizzle('field[var="muc#roomconfig_changesubject"] value', sent_stanza).pop().textContent.trim()).toBe('0');
  222. expect(sizzle('field[var="muc#roomconfig_whois"] value ', sent_stanza).pop().textContent.trim()).toBe('anyone');
  223. expect(sizzle('field[var="muc#roomconfig_membersonly"] value', sent_stanza).pop().textContent.trim()).toBe('1');
  224. expect(sizzle('field[var="muc#roomconfig_historylength"] value', sent_stanza).pop().textContent.trim()).toBe('20');
  225. done();
  226. }));
  227. });
  228. describe("An instant groupchat", function () {
  229. it("will be created when muc_instant_rooms is set to true",
  230. mock.initConverse(['chatBoxesFetched'], {}, async function (done, _converse) {
  231. let IQ_stanzas = _converse.connection.IQ_stanzas;
  232. const muc_jid = 'lounge@montague.lit';
  233. const nick = 'nicky';
  234. await mock.openChatRoom(_converse, 'lounge', 'montague.lit', 'romeo');
  235. const disco_selector = `iq[to="${muc_jid}"] query[xmlns="http://jabber.org/protocol/disco#info"]`;
  236. const stanza = await u.waitUntil(() => IQ_stanzas.filter(iq => iq.querySelector(disco_selector)).pop());
  237. // We pretend this is a new room, so no disco info is returned.
  238. const features_stanza = $iq({
  239. 'from': 'lounge@montague.lit',
  240. 'id': stanza.getAttribute('id'),
  241. 'to': 'romeo@montague.lit/desktop',
  242. 'type': 'error'
  243. }).c('error', {'type': 'cancel'})
  244. .c('item-not-found', {'xmlns': "urn:ietf:params:xml:ns:xmpp-stanzas"});
  245. _converse.connection._dataRecv(mock.createRequest(features_stanza));
  246. const view = _converse.chatboxviews.get('lounge@montague.lit');
  247. spyOn(view.model, 'join').and.callThrough();
  248. await mock.waitForReservedNick(_converse, muc_jid, '');
  249. const input = await u.waitUntil(() => view.querySelector('input[name="nick"]'), 1000);
  250. expect(view.model.session.get('connection_status')).toBe(converse.ROOMSTATUS.NICKNAME_REQUIRED);
  251. input.value = nick;
  252. view.querySelector('input[type=submit]').click();
  253. expect(view.model.join).toHaveBeenCalled();
  254. _converse.connection.IQ_stanzas = [];
  255. await mock.getRoomFeatures(_converse, muc_jid);
  256. await u.waitUntil(() => view.model.session.get('connection_status') === converse.ROOMSTATUS.CONNECTING);
  257. await mock.receiveOwnMUCPresence(_converse, muc_jid, nick);
  258. // The user has just entered the room (because join was called)
  259. // and receives their own presence from the server.
  260. // See example 24:
  261. // https://xmpp.org/extensions/xep-0045.html#enter-pres
  262. //
  263. /* <presence xmlns="jabber:client" to="jordie.langen@chat.example.org/converse.js-11659299" from="myroom@conference.chat.example.org/jc">
  264. * <x xmlns="http://jabber.org/protocol/muc#user">
  265. * <item jid="jordie.langen@chat.example.org/converse.js-11659299" affiliation="owner" role="moderator"/>
  266. * <status code="110"/>
  267. * <status code="201"/>
  268. * </x>
  269. * </presence>
  270. */
  271. const presence = $pres({
  272. to:'romeo@montague.lit/orchard',
  273. from:'lounge@montague.lit/nicky',
  274. id:'5025e055-036c-4bc5-a227-706e7e352053'
  275. }).c('x').attrs({xmlns:'http://jabber.org/protocol/muc#user'})
  276. .c('item').attrs({
  277. affiliation: 'owner',
  278. jid: 'romeo@montague.lit/orchard',
  279. role: 'moderator'
  280. }).up()
  281. .c('status').attrs({code:'110'}).up()
  282. .c('status').attrs({code:'201'}).nodeTree;
  283. _converse.connection._dataRecv(mock.createRequest(presence));
  284. await u.waitUntil(() => view.model.session.get('connection_status') === converse.ROOMSTATUS.ENTERED);
  285. await mock.returnMemberLists(_converse, muc_jid);
  286. const num_info_msgs = await u.waitUntil(() => view.querySelectorAll('.chat-content .chat-info').length);
  287. expect(num_info_msgs).toBe(1);
  288. const info_texts = Array.from(view.querySelectorAll('.chat-content .chat-info')).map(e => e.textContent.trim());
  289. expect(info_texts[0]).toBe('A new groupchat has been created');
  290. const csntext = await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent);
  291. expect(csntext.trim()).toEqual("nicky has entered the groupchat");
  292. // An instant room is created by saving the default configuratoin.
  293. //
  294. /* <iq to="myroom@conference.chat.example.org" type="set" xmlns="jabber:client" id="5025e055-036c-4bc5-a227-706e7e352053:sendIQ">
  295. * <query xmlns="http://jabber.org/protocol/muc#owner"><x xmlns="jabber:x:data" type="submit"/></query>
  296. * </iq>
  297. */
  298. const selector = `query[xmlns="${Strophe.NS.MUC_OWNER}"]`;
  299. IQ_stanzas = _converse.connection.IQ_stanzas;
  300. const iq = await u.waitUntil(() => IQ_stanzas.filter(s => s.querySelector(selector)).pop());
  301. expect(Strophe.serialize(iq)).toBe(
  302. `<iq id="${iq.getAttribute('id')}" to="lounge@montague.lit" type="set" xmlns="jabber:client">`+
  303. `<query xmlns="http://jabber.org/protocol/muc#owner"><x type="submit" xmlns="jabber:x:data"/>`+
  304. `</query></iq>`);
  305. done();
  306. }));
  307. });
  308. describe("A Groupchat", function () {
  309. it("Can be configured to show cached messages before being joined",
  310. mock.initConverse(['discoInitialized'],
  311. {
  312. 'muc_show_logs_before_join': true,
  313. 'archived_messages_page_size': 2,
  314. 'muc_nickname_from_jid': false,
  315. 'muc_clear_messages_on_leave': false,
  316. }, async function (done, _converse) {
  317. const { api } = _converse;
  318. const muc_jid = 'orchard@chat.shakespeare.lit';
  319. const nick = 'romeo';
  320. api.rooms.open(muc_jid);
  321. await mock.getRoomFeatures(_converse, muc_jid);
  322. await mock.waitForReservedNick(_converse, muc_jid);
  323. const view = _converse.chatboxviews.get(muc_jid);
  324. await view.model.messages.fetched;
  325. view.model.messages.create({
  326. 'type': 'groupchat',
  327. 'to': muc_jid,
  328. 'from': `${_converse.bare_jid}/orchard`,
  329. 'body': 'Hello world',
  330. 'message': 'Hello world',
  331. 'time': '2021-02-02T12:00:00Z'
  332. });
  333. expect(view.model.session.get('connection_status')).toBe(converse.ROOMSTATUS.NICKNAME_REQUIRED);
  334. await u.waitUntil(() => view.querySelectorAll('converse-chat-message').length === 1);
  335. const sel = 'converse-message-history converse-chat-message .chat-msg__text';
  336. await u.waitUntil(() => view.querySelector(sel)?.textContent.trim());
  337. expect(view.querySelector(sel).textContent.trim()).toBe('Hello world')
  338. view.querySelector('[name="nick"]').value = nick;
  339. view.querySelector('.muc-nickname-form input[type="submit"]').click();
  340. _converse.connection.IQ_stanzas = [];
  341. await mock.getRoomFeatures(_converse, muc_jid);
  342. await u.waitUntil(() => view.model.session.get('connection_status') === converse.ROOMSTATUS.CONNECTING);
  343. await mock.receiveOwnMUCPresence(_converse, muc_jid, nick);
  344. return done();
  345. }));
  346. it("maintains its state across reloads",
  347. mock.initConverse([], {
  348. 'clear_messages_on_reconnection': true,
  349. 'enable_smacks': false
  350. }, async function (done, _converse) {
  351. const nick = 'romeo';
  352. const sent_IQs = _converse.connection.IQ_stanzas;
  353. const muc_jid = 'lounge@montague.lit'
  354. await mock.openAndEnterChatRoom(_converse, muc_jid, nick, [], []);
  355. const view = _converse.chatboxviews.get(muc_jid);
  356. let iq_get = await u.waitUntil(() => sent_IQs.filter(iq => iq.querySelector(`iq query[xmlns="${Strophe.NS.MAM}"]`)).pop());
  357. const first_msg_id = _converse.connection.getUniqueId();
  358. const last_msg_id = _converse.connection.getUniqueId();
  359. let message = u.toStanza(
  360. `<message xmlns="jabber:client"
  361. to="romeo@montague.lit/orchard"
  362. from="${muc_jid}">
  363. <result xmlns="urn:xmpp:mam:2" queryid="${iq_get.querySelector('query').getAttribute('queryid')}" id="${first_msg_id}">
  364. <forwarded xmlns="urn:xmpp:forward:0">
  365. <delay xmlns="urn:xmpp:delay" stamp="2018-01-09T06:15:23Z"/>
  366. <message from="${muc_jid}/some1" type="groupchat">
  367. <body>1st Message</body>
  368. </message>
  369. </forwarded>
  370. </result>
  371. </message>`);
  372. _converse.connection._dataRecv(mock.createRequest(message));
  373. message = u.toStanza(
  374. `<message xmlns="jabber:client"
  375. to="romeo@montague.lit/orchard"
  376. from="${muc_jid}">
  377. <result xmlns="urn:xmpp:mam:2" queryid="${iq_get.querySelector('query').getAttribute('queryid')}" id="${last_msg_id}">
  378. <forwarded xmlns="urn:xmpp:forward:0">
  379. <delay xmlns="urn:xmpp:delay" stamp="2018-01-09T06:16:23Z"/>
  380. <message from="${muc_jid}/some1" type="groupchat">
  381. <body>2nd Message</body>
  382. </message>
  383. </forwarded>
  384. </result>
  385. </message>`);
  386. _converse.connection._dataRecv(mock.createRequest(message));
  387. const result = u.toStanza(
  388. `<iq type='result' id='${iq_get.getAttribute('id')}'>
  389. <fin xmlns='urn:xmpp:mam:2'>
  390. <set xmlns='http://jabber.org/protocol/rsm'>
  391. <first index='0'>${first_msg_id}</first>
  392. <last>${last_msg_id}</last>
  393. <count>2</count>
  394. </set>
  395. </fin>
  396. </iq>`);
  397. _converse.connection._dataRecv(mock.createRequest(result));
  398. await u.waitUntil(() => view.querySelectorAll('.chat-msg__text').length === 2);
  399. while (sent_IQs.length) { sent_IQs.pop(); } // Clear so that we don't match the older query
  400. await _converse.api.connection.reconnect();
  401. await mock.getRoomFeatures(_converse, muc_jid, []);
  402. await u.waitUntil(() => (view.model.session.get('connection_status') === converse.ROOMSTATUS.CONNECTING));
  403. // The user has just entered the room (because join was called)
  404. // and receives their own presence from the server.
  405. // See example 24: https://xmpp.org/extensions/xep-0045.html#enter-pres
  406. await mock.receiveOwnMUCPresence(_converse, muc_jid, nick);
  407. message = u.toStanza(`
  408. <message xmlns="jabber:client" type="groupchat" id="918172de-d5c5-4da4-b388-446ef4a05bec" to="${_converse.jid}" xml:lang="en" from="${muc_jid}/juliet">
  409. <body>Wherefore art though?</body>
  410. <active xmlns="http://jabber.org/protocol/chatstates"/>
  411. <origin-id xmlns="urn:xmpp:sid:0" id="918172de-d5c5-4da4-b388-446ef4a05bec"/>
  412. <stanza-id xmlns="urn:xmpp:sid:0" id="88cc9c93-a8f4-4dd5-b02a-d19855eb6303" by="${muc_jid}"/>
  413. <delay xmlns="urn:xmpp:delay" stamp="2020-07-14T17:46:47Z" from="juliet@shakespeare.lit"/>
  414. </message>`);
  415. _converse.connection._dataRecv(mock.createRequest(message));
  416. message = u.toStanza(`
  417. <message xmlns="jabber:client" type="groupchat" id="awQo6a-mi-Wa6NYh" to="${_converse.jid}" from="${muc_jid}/ews000" xml:lang="en">
  418. <composing xmlns="http://jabber.org/protocol/chatstates"/>
  419. <no-store xmlns="urn:xmpp:hints"/>
  420. <no-permanent-store xmlns="urn:xmpp:hints"/>
  421. <delay xmlns="urn:xmpp:delay" stamp="2020-07-14T17:46:54Z" from="juliet@shakespeare.lit"/>
  422. </message>`);
  423. _converse.connection._dataRecv(mock.createRequest(message));
  424. const affs = _converse.muc_fetch_members;
  425. const all_affiliations = Array.isArray(affs) ? affs : (affs ? ['member', 'admin', 'owner'] : []);
  426. await mock.returnMemberLists(_converse, muc_jid, [], all_affiliations);
  427. iq_get = await u.waitUntil(() => sent_IQs.filter(iq => iq.querySelector(`iq query[xmlns="${Strophe.NS.MAM}"]`)).pop());
  428. expect(Strophe.serialize(iq_get)).toBe(
  429. `<iq id="${iq_get.getAttribute('id')}" to="${muc_jid}" type="set" xmlns="jabber:client">`+
  430. `<query queryid="${iq_get.querySelector('query').getAttribute('queryid')}" xmlns="${Strophe.NS.MAM}">`+
  431. `<x type="submit" xmlns="jabber:x:data">`+
  432. `<field type="hidden" var="FORM_TYPE"><value>urn:xmpp:mam:2</value></field>`+
  433. `</x>`+
  434. `<set xmlns="http://jabber.org/protocol/rsm"><before></before><max>50</max></set>`+
  435. `</query>`+
  436. `</iq>`);
  437. done();
  438. }));
  439. describe("upon being entered", function () {
  440. it("will fetch the member list if muc_fetch_members is true",
  441. mock.initConverse([], {'muc_fetch_members': true}, async function (done, _converse) {
  442. let sent_IQs = _converse.connection.IQ_stanzas;
  443. const muc_jid = 'lounge@montague.lit';
  444. await mock.openAndEnterChatRoom(_converse, muc_jid, 'romeo');
  445. let view = _converse.chatboxviews.get(muc_jid);
  446. expect(sent_IQs.filter(iq => iq.querySelector('query item[affiliation]')).length).toBe(3);
  447. // Check in reverse order that we requested all three lists
  448. const owner_iq = sent_IQs.pop();
  449. expect(Strophe.serialize(owner_iq)).toBe(
  450. `<iq id="${owner_iq.getAttribute('id')}" to="${muc_jid}" type="get" xmlns="jabber:client">`+
  451. `<query xmlns="http://jabber.org/protocol/muc#admin"><item affiliation="owner"/></query>`+
  452. `</iq>`);
  453. const admin_iq = sent_IQs.pop();
  454. expect(Strophe.serialize(admin_iq)).toBe(
  455. `<iq id="${admin_iq.getAttribute('id')}" to="${muc_jid}" type="get" xmlns="jabber:client">`+
  456. `<query xmlns="http://jabber.org/protocol/muc#admin"><item affiliation="admin"/></query>`+
  457. `</iq>`);
  458. const member_iq = sent_IQs.pop();
  459. expect(Strophe.serialize(member_iq)).toBe(
  460. `<iq id="${member_iq.getAttribute('id')}" to="${muc_jid}" type="get" xmlns="jabber:client">`+
  461. `<query xmlns="http://jabber.org/protocol/muc#admin"><item affiliation="member"/></query>`+
  462. `</iq>`);
  463. view.close();
  464. _converse.connection.IQ_stanzas = [];
  465. sent_IQs = _converse.connection.IQ_stanzas;
  466. _converse.muc_fetch_members = false;
  467. await mock.openAndEnterChatRoom(_converse, 'orchard@montague.lit', 'romeo');
  468. view = _converse.chatboxviews.get('orchard@montague.lit');
  469. expect(sent_IQs.filter(iq => iq.querySelector('query item[affiliation]')).length).toBe(0);
  470. await view.close();
  471. _converse.connection.IQ_stanzas = [];
  472. sent_IQs = _converse.connection.IQ_stanzas;
  473. _converse.muc_fetch_members = ['admin'];
  474. await mock.openAndEnterChatRoom(_converse, 'courtyard@montague.lit', 'romeo');
  475. view = _converse.chatboxviews.get('courtyard@montague.lit');
  476. expect(sent_IQs.filter(iq => iq.querySelector('query item[affiliation]')).length).toBe(1);
  477. expect(sent_IQs.filter(iq => iq.querySelector('query item[affiliation="admin"]')).length).toBe(1);
  478. view.close();
  479. _converse.connection.IQ_stanzas = [];
  480. sent_IQs = _converse.connection.IQ_stanzas;
  481. _converse.muc_fetch_members = ['owner'];
  482. await mock.openAndEnterChatRoom(_converse, 'garden@montague.lit', 'romeo');
  483. view = _converse.chatboxviews.get('garden@montague.lit');
  484. expect(sent_IQs.filter(iq => iq.querySelector('query item[affiliation]')).length).toBe(1);
  485. expect(sent_IQs.filter(iq => iq.querySelector('query item[affiliation="owner"]')).length).toBe(1);
  486. view.close();
  487. done();
  488. }));
  489. describe("when fetching the member lists", function () {
  490. it("gracefully handles being forbidden from fetching the lists for certain affiliations",
  491. mock.initConverse([], {'muc_fetch_members': true}, async function (done, _converse) {
  492. const sent_IQs = _converse.connection.IQ_stanzas;
  493. const muc_jid = 'lounge@montague.lit';
  494. const features = [
  495. 'http://jabber.org/protocol/muc',
  496. 'jabber:iq:register',
  497. 'muc_hidden',
  498. 'muc_membersonly',
  499. 'muc_passwordprotected',
  500. Strophe.NS.MAM,
  501. Strophe.NS.SID
  502. ];
  503. const nick = 'romeo';
  504. await _converse.api.rooms.open(muc_jid);
  505. await mock.getRoomFeatures(_converse, muc_jid, features);
  506. await mock.waitForReservedNick(_converse, muc_jid, nick);
  507. mock.receiveOwnMUCPresence(_converse, muc_jid, nick);
  508. const view = _converse.chatboxviews.get(muc_jid);
  509. await u.waitUntil(() => (view.model.session.get('connection_status') === converse.ROOMSTATUS.ENTERED));
  510. // Check in reverse order that we requested all three lists
  511. const owner_iq = sent_IQs.pop();
  512. expect(Strophe.serialize(owner_iq)).toBe(
  513. `<iq id="${owner_iq.getAttribute('id')}" to="${muc_jid}" type="get" xmlns="jabber:client">`+
  514. `<query xmlns="http://jabber.org/protocol/muc#admin"><item affiliation="owner"/></query>`+
  515. `</iq>`);
  516. const admin_iq = sent_IQs.pop();
  517. expect(Strophe.serialize(admin_iq)).toBe(
  518. `<iq id="${admin_iq.getAttribute('id')}" to="${muc_jid}" type="get" xmlns="jabber:client">`+
  519. `<query xmlns="http://jabber.org/protocol/muc#admin"><item affiliation="admin"/></query>`+
  520. `</iq>`);
  521. const member_iq = sent_IQs.pop();
  522. expect(Strophe.serialize(member_iq)).toBe(
  523. `<iq id="${member_iq.getAttribute('id')}" to="${muc_jid}" type="get" xmlns="jabber:client">`+
  524. `<query xmlns="http://jabber.org/protocol/muc#admin"><item affiliation="member"/></query>`+
  525. `</iq>`);
  526. // It might be that the user is not allowed to fetch certain lists.
  527. let err_stanza = u.toStanza(
  528. `<iq xmlns="jabber:client" type="error" to="${_converse.jid}" from="${muc_jid}" id="${admin_iq.getAttribute('id')}">
  529. <error type="auth"><forbidden xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"/></error>
  530. </iq>`);
  531. _converse.connection._dataRecv(mock.createRequest(err_stanza));
  532. err_stanza = u.toStanza(
  533. `<iq xmlns="jabber:client" type="error" to="${_converse.jid}" from="${muc_jid}" id="${owner_iq.getAttribute('id')}">
  534. <error type="auth"><forbidden xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"/></error>
  535. </iq>`);
  536. _converse.connection._dataRecv(mock.createRequest(err_stanza));
  537. // Now the service sends the member lists to the user
  538. const member_list_stanza = $iq({
  539. 'from': muc_jid,
  540. 'id': member_iq.getAttribute('id'),
  541. 'to': 'romeo@montague.lit/orchard',
  542. 'type': 'result'
  543. }).c('query', {'xmlns': Strophe.NS.MUC_ADMIN})
  544. .c('item', {
  545. 'affiliation': 'member',
  546. 'jid': 'hag66@shakespeare.lit',
  547. 'nick': 'thirdwitch',
  548. 'role': 'participant'
  549. });
  550. _converse.connection._dataRecv(mock.createRequest(member_list_stanza));
  551. await u.waitUntil(() => view.model.occupants.length > 1);
  552. expect(view.model.occupants.length).toBe(2);
  553. // The existing owner occupant should not have their
  554. // affiliation removed due to the owner list
  555. // not being returned (forbidden err).
  556. expect(view.model.occupants.findWhere({'jid': _converse.bare_jid}).get('affiliation')).toBe('owner');
  557. expect(view.model.occupants.findWhere({'jid': 'hag66@shakespeare.lit'}).get('affiliation')).toBe('member');
  558. done();
  559. }));
  560. });
  561. });
  562. describe("topic", function () {
  563. it("is shown the header", mock.initConverse([], {}, async function (done, _converse) {
  564. await mock.openAndEnterChatRoom(_converse, 'jdev@conference.jabber.org', 'jc');
  565. const text = 'Jabber/XMPP Development | RFCs and Extensions: https://xmpp.org/ | Protocol and XSF discussions: xsf@muc.xmpp.org';
  566. let stanza = u.toStanza(`
  567. <message xmlns="jabber:client" to="${_converse.jid}" type="groupchat" from="jdev@conference.jabber.org/ralphm">
  568. <subject>${text}</subject>
  569. <delay xmlns="urn:xmpp:delay" stamp="2014-02-04T09:35:39Z" from="jdev@conference.jabber.org"/>
  570. <x xmlns="jabber:x:delay" stamp="20140204T09:35:39" from="jdev@conference.jabber.org"/>
  571. </message>`);
  572. _converse.connection._dataRecv(mock.createRequest(stanza));
  573. const view = _converse.chatboxviews.get('jdev@conference.jabber.org');
  574. await new Promise(resolve => view.model.once('change:subject', resolve));
  575. const head_desc = await u.waitUntil(() => view.querySelector('.chat-head__desc'), 1000);
  576. expect(head_desc?.textContent.trim()).toBe(text);
  577. stanza = u.toStanza(
  578. `<message xmlns="jabber:client" to="${_converse.jid}" type="groupchat" from="jdev@conference.jabber.org/ralphm">
  579. <subject>This is a message subject</subject>
  580. <body>This is a message</body>
  581. </message>`);
  582. _converse.connection._dataRecv(mock.createRequest(stanza));
  583. await new Promise(resolve => view.model.messages.once('rendered', resolve));
  584. expect(sizzle('.chat-msg__subject', view).length).toBe(1);
  585. expect(sizzle('.chat-msg__subject', view).pop().textContent.trim()).toBe('This is a message subject');
  586. expect(sizzle('.chat-msg__text').length).toBe(1);
  587. expect(sizzle('.chat-msg__text').pop().textContent.trim()).toBe('This is a message');
  588. expect(view.querySelector('.chat-head__desc').textContent.trim()).toBe(text);
  589. done();
  590. }));
  591. it("can be toggled by the user", mock.initConverse([], {}, async function (done, _converse) {
  592. await mock.openAndEnterChatRoom(_converse, 'jdev@conference.jabber.org', 'jc');
  593. const text = 'Jabber/XMPP Development | RFCs and Extensions: https://xmpp.org/ | Protocol and XSF discussions: xsf@muc.xmpp.org';
  594. let stanza = u.toStanza(`
  595. <message xmlns="jabber:client" to="${_converse.jid}" type="groupchat" from="jdev@conference.jabber.org/ralphm">
  596. <subject>${text}</subject>
  597. <delay xmlns="urn:xmpp:delay" stamp="2014-02-04T09:35:39Z" from="jdev@conference.jabber.org"/>
  598. <x xmlns="jabber:x:delay" stamp="20140204T09:35:39" from="jdev@conference.jabber.org"/>
  599. </message>`);
  600. _converse.connection._dataRecv(mock.createRequest(stanza));
  601. const view = _converse.chatboxviews.get('jdev@conference.jabber.org');
  602. await new Promise(resolve => view.model.once('change:subject', resolve));
  603. const head_desc = await u.waitUntil(() => view.querySelector('.chat-head__desc'));
  604. expect(head_desc?.textContent.trim()).toBe(text);
  605. stanza = u.toStanza(
  606. `<message xmlns="jabber:client" to="${_converse.jid}" type="groupchat" from="jdev@conference.jabber.org/ralphm">
  607. <subject>This is a message subject</subject>
  608. <body>This is a message</body>
  609. </message>`);
  610. _converse.connection._dataRecv(mock.createRequest(stanza));
  611. await new Promise(resolve => view.model.messages.once('rendered', resolve));
  612. expect(sizzle('.chat-msg__subject', view).length).toBe(1);
  613. expect(sizzle('.chat-msg__subject', view).pop().textContent.trim()).toBe('This is a message subject');
  614. expect(sizzle('.chat-msg__text').length).toBe(1);
  615. expect(sizzle('.chat-msg__text').pop().textContent.trim()).toBe('This is a message');
  616. const topic_el = view.querySelector('.chat-head__desc');
  617. expect(topic_el.textContent.trim()).toBe(text);
  618. expect(u.isVisible(topic_el)).toBe(true);
  619. const toggle = view.querySelector('.hide-topic');
  620. expect(toggle.textContent).toBe('Hide topic');
  621. toggle.click();
  622. await u.waitUntil(() => view.querySelector('.hide-topic').textContent === 'Show topic');
  623. done();
  624. }));
  625. it("will always be shown when it's new", mock.initConverse([], {}, async function (done, _converse) {
  626. await mock.openAndEnterChatRoom(_converse, 'jdev@conference.jabber.org', 'jc');
  627. const text = 'Jabber/XMPP Development | RFCs and Extensions: https://xmpp.org/ | Protocol and XSF discussions: xsf@muc.xmpp.org';
  628. let stanza = u.toStanza(`
  629. <message xmlns="jabber:client" to="${_converse.jid}" type="groupchat" from="jdev@conference.jabber.org/ralphm">
  630. <subject>${text}</subject>
  631. </message>`);
  632. _converse.connection._dataRecv(mock.createRequest(stanza));
  633. const view = _converse.chatboxviews.get('jdev@conference.jabber.org');
  634. await new Promise(resolve => view.model.once('change:subject', resolve));
  635. const head_desc = await u.waitUntil(() => view.querySelector('.chat-head__desc'));
  636. expect(head_desc?.textContent.trim()).toBe(text);
  637. let topic_el = view.querySelector('.chat-head__desc');
  638. expect(topic_el.textContent.trim()).toBe(text);
  639. expect(u.isVisible(topic_el)).toBe(true);
  640. const toggle = view.querySelector('.hide-topic');
  641. expect(toggle.textContent).toBe('Hide topic');
  642. toggle.click();
  643. await u.waitUntil(() => !u.isVisible(topic_el));
  644. stanza = u.toStanza(`
  645. <message xmlns="jabber:client" to="${_converse.jid}" type="groupchat" from="jdev@conference.jabber.org/ralphm">
  646. <subject>Another topic</subject>
  647. </message>`);
  648. _converse.connection._dataRecv(mock.createRequest(stanza));
  649. await u.waitUntil(() => u.isVisible(view.querySelector('.chat-head__desc')));
  650. topic_el = view.querySelector('.chat-head__desc');
  651. expect(topic_el.textContent.trim()).toBe('Another topic');
  652. done();
  653. }));
  654. it("causes an info message to be shown when received in real-time", mock.initConverse([], {}, async function (done, _converse) {
  655. spyOn(_converse.ChatRoom.prototype, 'handleSubjectChange').and.callThrough();
  656. await mock.openAndEnterChatRoom(_converse, 'jdev@conference.jabber.org', 'romeo');
  657. const view = _converse.chatboxviews.get('jdev@conference.jabber.org');
  658. _converse.connection._dataRecv(mock.createRequest(u.toStanza(`
  659. <message xmlns="jabber:client" to="${_converse.jid}" type="groupchat" from="jdev@conference.jabber.org/ralphm">
  660. <subject>This is an older topic</subject>
  661. <delay xmlns="urn:xmpp:delay" stamp="2014-02-04T09:35:39Z" from="jdev@conference.jabber.org"/>
  662. <x xmlns="jabber:x:delay" stamp="20140204T09:35:39" from="jdev@conference.jabber.org"/>
  663. </message>`)));
  664. await u.waitUntil(() => view.model.handleSubjectChange.calls.count());
  665. expect(sizzle('.chat-info__message', view).length).toBe(0);
  666. const desc = await u.waitUntil(() => view.querySelector('.chat-head__desc'));
  667. expect(desc.textContent.trim()).toBe('This is an older topic');
  668. _converse.connection._dataRecv(mock.createRequest(u.toStanza(`
  669. <message xmlns="jabber:client" to="${_converse.jid}" type="groupchat" from="jdev@conference.jabber.org/ralphm">
  670. <subject>This is a new topic</subject>
  671. </message>`)));
  672. await u.waitUntil(() => view.model.handleSubjectChange.calls.count() === 2);
  673. await u.waitUntil(() => sizzle('.chat-info__message', view).pop()?.textContent.trim() === 'Topic set by ralphm');
  674. await u.waitUntil(() => desc.textContent.trim() === 'This is a new topic');
  675. // Doesn't show multiple subsequent topic change notifications
  676. _converse.connection._dataRecv(mock.createRequest(u.toStanza(`
  677. <message xmlns="jabber:client" to="${_converse.jid}" type="groupchat" from="jdev@conference.jabber.org/ralphm">
  678. <subject>Yet another topic</subject>
  679. </message>`)));
  680. await u.waitUntil(() => view.model.handleSubjectChange.calls.count() === 3);
  681. await u.waitUntil(() => desc.textContent.trim() === 'Yet another topic');
  682. expect(sizzle('.chat-info__message', view).length).toBe(1);
  683. // Sow multiple subsequent topic change notification from someone else
  684. _converse.connection._dataRecv(mock.createRequest(u.toStanza(`
  685. <message xmlns="jabber:client" to="${_converse.jid}" type="groupchat" from="jdev@conference.jabber.org/some1">
  686. <subject>Some1's topic</subject>
  687. </message>`)));
  688. await u.waitUntil(() => view.model.handleSubjectChange.calls.count() === 4);
  689. await u.waitUntil(() => desc.textContent.trim() === "Some1's topic");
  690. expect(sizzle('.chat-info__message', view).length).toBe(2);
  691. const el = sizzle('.chat-info__message', view).pop();
  692. expect(el.textContent.trim()).toBe('Topic set by some1');
  693. // Removes current topic
  694. const stanza = u.toStanza(
  695. `<message xmlns="jabber:client" to="${_converse.jid}" type="groupchat" from="jdev@conference.jabber.org/some1">
  696. <subject/>
  697. </message>`);
  698. _converse.connection._dataRecv(mock.createRequest(stanza));
  699. await u.waitUntil(() => view.model.handleSubjectChange.calls.count() === 5);
  700. await u.waitUntil(() => view.querySelector('.chat-head__desc').textContent.replace(/<!---->/g, '') === '');
  701. await u.waitUntil(() => view.querySelector('converse-chat-message:last-child .chat-info').textContent.trim() === "Topic cleared by some1");
  702. done();
  703. }));
  704. });
  705. it("restores cached messages when it reconnects and clear_messages_on_reconnection and muc_clear_messages_on_leave are false",
  706. mock.initConverse([], {
  707. 'clear_messages_on_reconnection': false,
  708. 'muc_clear_messages_on_leave': false
  709. },
  710. async function (done, _converse) {
  711. const muc_jid = 'lounge@montague.lit';
  712. await mock.openAndEnterChatRoom(_converse, muc_jid , 'romeo');
  713. const view = _converse.chatboxviews.get(muc_jid);
  714. const message = 'Hello world',
  715. nick = mock.chatroom_names[0],
  716. msg = $msg({
  717. 'from': 'lounge@montague.lit/'+nick,
  718. 'id': u.getUniqueId(),
  719. 'to': 'romeo@montague.lit',
  720. 'type': 'groupchat'
  721. }).c('body').t(message).tree();
  722. await view.model.handleMessageStanza(msg);
  723. await view.model.close();
  724. _converse.connection.IQ_stanzas = [];
  725. await mock.openAndEnterChatRoom(_converse, muc_jid , 'romeo');
  726. await u.waitUntil(() => view.querySelector('converse-chat-message'));
  727. expect(view.model.messages.length).toBe(1);
  728. expect(view.querySelectorAll('converse-chat-message').length).toBe(1);
  729. done()
  730. }));
  731. it("clears cached messages when it reconnects and clear_messages_on_reconnection is true",
  732. mock.initConverse([], {'clear_messages_on_reconnection': true}, async function (done, _converse) {
  733. const muc_jid = 'lounge@montague.lit';
  734. await mock.openAndEnterChatRoom(_converse, muc_jid , 'romeo');
  735. const view = _converse.chatboxviews.get(muc_jid);
  736. const message = 'Hello world',
  737. nick = mock.chatroom_names[0],
  738. msg = $msg({
  739. 'from': 'lounge@montague.lit/'+nick,
  740. 'id': u.getUniqueId(),
  741. 'to': 'romeo@montague.lit',
  742. 'type': 'groupchat'
  743. }).c('body').t(message).tree();
  744. await view.model.handleMessageStanza(msg);
  745. await view.model.close();
  746. _converse.connection.IQ_stanzas = [];
  747. await mock.openAndEnterChatRoom(_converse, muc_jid , 'romeo');
  748. expect(view.model.messages.length).toBe(0);
  749. expect(view.querySelector('converse-chat-history')).toBe(null);
  750. done()
  751. }));
  752. it("is opened when an xmpp: URI is clicked inside another groupchat",
  753. mock.initConverse([], {}, async function (done, _converse) {
  754. await mock.waitForRoster(_converse, 'current');
  755. await mock.openAndEnterChatRoom(_converse, 'lounge@montague.lit', 'romeo');
  756. const view = _converse.chatboxviews.get('lounge@montague.lit');
  757. if (!view.querySelectorAll('.chat-area').length) {
  758. view.renderChatArea();
  759. }
  760. expect(_converse.chatboxes.length).toEqual(2);
  761. const message = 'Please go to xmpp:coven@chat.shakespeare.lit?join',
  762. nick = mock.chatroom_names[0],
  763. msg = $msg({
  764. 'from': 'lounge@montague.lit/'+nick,
  765. 'id': u.getUniqueId(),
  766. 'to': 'romeo@montague.lit',
  767. 'type': 'groupchat'
  768. }).c('body').t(message).tree();
  769. await view.model.handleMessageStanza(msg);
  770. await u.waitUntil(() => view.querySelector('.chat-msg__text a'));
  771. view.querySelector('.chat-msg__text a').click();
  772. await u.waitUntil(() => _converse.chatboxes.length === 3)
  773. expect(_converse.chatboxes.pluck('id').includes('coven@chat.shakespeare.lit')).toBe(true);
  774. done()
  775. }));
  776. it("shows a notification if it's not anonymous",
  777. mock.initConverse(['chatBoxesFetched'], {}, async function (done, _converse) {
  778. const muc_jid = 'coven@chat.shakespeare.lit';
  779. const nick = 'romeo';
  780. await _converse.api.rooms.open(muc_jid);
  781. await mock.getRoomFeatures(_converse, muc_jid);
  782. await mock.waitForReservedNick(_converse, muc_jid, nick);
  783. const view = _converse.chatboxviews.get(muc_jid);
  784. /* <presence to="romeo@montague.lit/_converse.js-29092160"
  785. * from="coven@chat.shakespeare.lit/some1">
  786. * <x xmlns="http://jabber.org/protocol/muc#user">
  787. * <item affiliation="owner" jid="romeo@montague.lit/_converse.js-29092160" role="moderator"/>
  788. * <status code="110"/>
  789. * <status code="100"/>
  790. * </x>
  791. * </presence></body>
  792. */
  793. const presence = $pres({
  794. to: 'romeo@montague.lit/orchard',
  795. from: 'coven@chat.shakespeare.lit/some1'
  796. }).c('x', {xmlns: Strophe.NS.MUC_USER})
  797. .c('item', {
  798. 'affiliation': 'owner',
  799. 'jid': 'romeo@montague.lit/_converse.js-29092160',
  800. 'role': 'moderator'
  801. }).up()
  802. .c('status', {code: '110'}).up()
  803. .c('status', {code: '100'});
  804. _converse.connection._dataRecv(mock.createRequest(presence));
  805. await u.waitUntil(() => (view.model.session.get('connection_status') === converse.ROOMSTATUS.ENTERED));
  806. await mock.returnMemberLists(_converse, muc_jid, [], ['member', 'admin', 'owner']);
  807. const num_info_msgs = await u.waitUntil(() => view.querySelectorAll('.chat-content .chat-info').length);
  808. expect(num_info_msgs).toBe(1);
  809. expect(sizzle('div.chat-info', view.content).pop().textContent.trim()).toBe("This groupchat is not anonymous");
  810. const csntext = await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent);
  811. expect(csntext.trim()).toEqual("some1 has entered the groupchat");
  812. done();
  813. }));
  814. it("shows join/leave messages when users enter or exit a groupchat",
  815. mock.initConverse(['chatBoxesFetched'], {'muc_fetch_members': false}, async function (done, _converse) {
  816. const muc_jid = 'coven@chat.shakespeare.lit';
  817. const nick = 'some1';
  818. const room_creation_promise = await _converse.api.rooms.open(muc_jid, {nick});
  819. await mock.getRoomFeatures(_converse, muc_jid);
  820. const sent_stanzas = _converse.connection.sent_stanzas;
  821. await u.waitUntil(() => sent_stanzas.filter(iq => sizzle('presence history', iq).length).pop());
  822. const view = _converse.chatboxviews.get('coven@chat.shakespeare.lit');
  823. await _converse.api.waitUntil('chatRoomViewInitialized');
  824. /* We don't show join/leave messages for existing occupants. We
  825. * know about them because we receive their presences before we
  826. * receive our own.
  827. */
  828. let presence = $pres({
  829. to: 'romeo@montague.lit/_converse.js-29092160',
  830. from: 'coven@chat.shakespeare.lit/oldguy'
  831. }).c('x', {xmlns: Strophe.NS.MUC_USER})
  832. .c('item', {
  833. 'affiliation': 'none',
  834. 'jid': 'oldguy@montague.lit/_converse.js-290929789',
  835. 'role': 'participant'
  836. });
  837. _converse.connection._dataRecv(mock.createRequest(presence));
  838. /* <presence to="romeo@montague.lit/_converse.js-29092160"
  839. * from="coven@chat.shakespeare.lit/some1">
  840. * <x xmlns="http://jabber.org/protocol/muc#user">
  841. * <item affiliation="owner" jid="romeo@montague.lit/_converse.js-29092160" role="moderator"/>
  842. * <status code="110"/>
  843. * </x>
  844. * </presence></body>
  845. */
  846. presence = $pres({
  847. to: 'romeo@montague.lit/_converse.js-29092160',
  848. from: 'coven@chat.shakespeare.lit/some1'
  849. }).c('x', {xmlns: Strophe.NS.MUC_USER})
  850. .c('item', {
  851. 'affiliation': 'owner',
  852. 'jid': 'romeo@montague.lit/_converse.js-29092160',
  853. 'role': 'moderator'
  854. }).up()
  855. .c('status', {code: '110'});
  856. _converse.connection._dataRecv(mock.createRequest(presence));
  857. const csntext = await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent);
  858. expect(csntext.trim()).toEqual("some1 has entered the groupchat");
  859. await room_creation_promise;
  860. await u.waitUntil(() => (view.model.session.get('connection_status') === converse.ROOMSTATUS.ENTERED));
  861. await view.model.messages.fetched;
  862. presence = $pres({
  863. to: 'romeo@montague.lit/_converse.js-29092160',
  864. from: 'coven@chat.shakespeare.lit/newguy'
  865. })
  866. .c('x', {xmlns: Strophe.NS.MUC_USER})
  867. .c('item', {
  868. 'affiliation': 'none',
  869. 'jid': 'newguy@montague.lit/_converse.js-290929789',
  870. 'role': 'participant'
  871. });
  872. _converse.connection._dataRecv(mock.createRequest(presence));
  873. await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent.trim() ===
  874. "some1 and newguy have entered the groupchat");
  875. const msg = $msg({
  876. 'from': 'coven@chat.shakespeare.lit/some1',
  877. 'id': u.getUniqueId(),
  878. 'to': 'romeo@montague.lit',
  879. 'type': 'groupchat'
  880. }).c('body').t('hello world').tree();
  881. _converse.connection._dataRecv(mock.createRequest(msg));
  882. await new Promise(resolve => view.model.messages.once('rendered', resolve));
  883. // Add another entrant, otherwise the above message will be
  884. // collapsed if "newguy" leaves immediately again
  885. presence = $pres({
  886. to: 'romeo@montague.lit/_converse.js-29092160',
  887. from: 'coven@chat.shakespeare.lit/newgirl'
  888. })
  889. .c('x', {xmlns: Strophe.NS.MUC_USER})
  890. .c('item', {
  891. 'affiliation': 'none',
  892. 'jid': 'newgirl@montague.lit/_converse.js-213098781',
  893. 'role': 'participant'
  894. });
  895. _converse.connection._dataRecv(mock.createRequest(presence));
  896. await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent.trim() ===
  897. "some1, newguy and newgirl have entered the groupchat");
  898. // Don't show duplicate join messages
  899. presence = $pres({
  900. to: 'romeo@montague.lit/_converse.js-290918392',
  901. from: 'coven@chat.shakespeare.lit/newguy'
  902. }).c('x', {xmlns: Strophe.NS.MUC_USER})
  903. .c('item', {
  904. 'affiliation': 'none',
  905. 'jid': 'newguy@montague.lit/_converse.js-290929789',
  906. 'role': 'participant'
  907. });
  908. _converse.connection._dataRecv(mock.createRequest(presence));
  909. /* <presence
  910. * from='coven@chat.shakespeare.lit/thirdwitch'
  911. * to='crone1@shakespeare.lit/desktop'
  912. * type='unavailable'>
  913. * <status>Disconnected: Replaced by new connection</status>
  914. * <x xmlns='http://jabber.org/protocol/muc#user'>
  915. * <item affiliation='member'
  916. * jid='hag66@shakespeare.lit/pda'
  917. * role='none'/>
  918. * </x>
  919. * </presence>
  920. */
  921. presence = $pres({
  922. to: 'romeo@montague.lit/_converse.js-29092160',
  923. type: 'unavailable',
  924. from: 'coven@chat.shakespeare.lit/newguy'
  925. })
  926. .c('status', 'Disconnected: Replaced by new connection').up()
  927. .c('x', {xmlns: Strophe.NS.MUC_USER})
  928. .c('item', {
  929. 'affiliation': 'none',
  930. 'jid': 'newguy@montague.lit/_converse.js-290929789',
  931. 'role': 'none'
  932. });
  933. _converse.connection._dataRecv(mock.createRequest(presence));
  934. await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent.trim() ===
  935. "some1 and newgirl have entered the groupchat\nnewguy has left the groupchat");
  936. // When the user immediately joins again, we collapse the
  937. // multiple join/leave messages.
  938. presence = $pres({
  939. to: 'romeo@montague.lit/_converse.js-29092160',
  940. from: 'coven@chat.shakespeare.lit/newguy'
  941. }).c('x', {xmlns: Strophe.NS.MUC_USER})
  942. .c('item', {
  943. 'affiliation': 'none',
  944. 'jid': 'newguy@montague.lit/_converse.js-290929789',
  945. 'role': 'participant'
  946. });
  947. _converse.connection._dataRecv(mock.createRequest(presence));
  948. await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent.trim() ===
  949. "some1, newgirl and newguy have entered the groupchat");
  950. presence = $pres({
  951. to: 'romeo@montague.lit/_converse.js-29092160',
  952. type: 'unavailable',
  953. from: 'coven@chat.shakespeare.lit/newguy'
  954. })
  955. .c('x', {xmlns: Strophe.NS.MUC_USER})
  956. .c('item', {
  957. 'affiliation': 'none',
  958. 'jid': 'newguy@montague.lit/_converse.js-290929789',
  959. 'role': 'none'
  960. });
  961. _converse.connection._dataRecv(mock.createRequest(presence));
  962. await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent.trim() ===
  963. "some1 and newgirl have entered the groupchat\nnewguy has left the groupchat");
  964. presence = $pres({
  965. to: 'romeo@montague.lit/_converse.js-29092160',
  966. from: 'coven@chat.shakespeare.lit/nomorenicks'
  967. })
  968. .c('x', {xmlns: Strophe.NS.MUC_USER})
  969. .c('item', {
  970. 'affiliation': 'none',
  971. 'jid': 'nomorenicks@montague.lit/_converse.js-290929789',
  972. 'role': 'participant'
  973. });
  974. _converse.connection._dataRecv(mock.createRequest(presence));
  975. await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent.trim() ===
  976. "some1, newgirl and nomorenicks have entered the groupchat\nnewguy has left the groupchat");
  977. presence = $pres({
  978. to: 'romeo@montague.lit/_converse.js-290918392',
  979. type: 'unavailable',
  980. from: 'coven@chat.shakespeare.lit/nomorenicks'
  981. }).c('x', {xmlns: Strophe.NS.MUC_USER})
  982. .c('item', {
  983. 'affiliation': 'none',
  984. 'jid': 'nomorenicks@montague.lit/_converse.js-290929789',
  985. 'role': 'none'
  986. });
  987. _converse.connection._dataRecv(mock.createRequest(presence));
  988. await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent.trim() ===
  989. "some1 and newgirl have entered the groupchat\nnewguy and nomorenicks have left the groupchat");
  990. presence = $pres({
  991. to: 'romeo@montague.lit/_converse.js-29092160',
  992. from: 'coven@chat.shakespeare.lit/nomorenicks'
  993. })
  994. .c('x', {xmlns: Strophe.NS.MUC_USER})
  995. .c('item', {
  996. 'affiliation': 'none',
  997. 'jid': 'nomorenicks@montague.lit/_converse.js-290929789',
  998. 'role': 'participant'
  999. });
  1000. _converse.connection._dataRecv(mock.createRequest(presence));
  1001. await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent.trim() ===
  1002. "some1, newgirl and nomorenicks have entered the groupchat\nnewguy has left the groupchat");
  1003. // Test a member joining and leaving
  1004. presence = $pres({
  1005. to: 'romeo@montague.lit/_converse.js-290918392',
  1006. from: 'coven@chat.shakespeare.lit/insider'
  1007. }).c('x', {xmlns: Strophe.NS.MUC_USER})
  1008. .c('item', {
  1009. 'affiliation': 'member',
  1010. 'jid': 'insider@montague.lit/_converse.js-290929789',
  1011. 'role': 'participant'
  1012. });
  1013. _converse.connection._dataRecv(mock.createRequest(presence));
  1014. /* <presence
  1015. * from='coven@chat.shakespeare.lit/thirdwitch'
  1016. * to='crone1@shakespeare.lit/desktop'
  1017. * type='unavailable'>
  1018. * <status>Disconnected: Replaced by new connection</status>
  1019. * <x xmlns='http://jabber.org/protocol/muc#user'>
  1020. * <item affiliation='member'
  1021. * jid='hag66@shakespeare.lit/pda'
  1022. * role='none'/>
  1023. * </x>
  1024. * </presence>
  1025. */
  1026. presence = $pres({
  1027. to: 'romeo@montague.lit/_converse.js-29092160',
  1028. type: 'unavailable',
  1029. from: 'coven@chat.shakespeare.lit/insider'
  1030. })
  1031. .c('status', 'Disconnected: Replaced by new connection').up()
  1032. .c('x', {xmlns: Strophe.NS.MUC_USER})
  1033. .c('item', {
  1034. 'affiliation': 'member',
  1035. 'jid': 'insider@montague.lit/_converse.js-290929789',
  1036. 'role': 'none'
  1037. });
  1038. _converse.connection._dataRecv(mock.createRequest(presence));
  1039. await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent.trim() ===
  1040. "some1, newgirl and nomorenicks have entered the groupchat\nnewguy and insider have left the groupchat");
  1041. expect(view.model.occupants.length).toBe(5);
  1042. expect(view.model.occupants.findWhere({'jid': 'insider@montague.lit'}).get('show')).toBe('offline');
  1043. // New girl leaves
  1044. presence = $pres({
  1045. 'to': 'romeo@montague.lit/_converse.js-29092160',
  1046. 'type': 'unavailable',
  1047. 'from': 'coven@chat.shakespeare.lit/newgirl'
  1048. })
  1049. .c('x', {xmlns: Strophe.NS.MUC_USER})
  1050. .c('item', {
  1051. 'affiliation': 'none',
  1052. 'jid': 'newgirl@montague.lit/_converse.js-213098781',
  1053. 'role': 'none'
  1054. });
  1055. _converse.connection._dataRecv(mock.createRequest(presence));
  1056. await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent.trim() ===
  1057. "some1 and nomorenicks have entered the groupchat\nnewguy, insider and newgirl have left the groupchat");
  1058. expect(view.model.occupants.length).toBe(4);
  1059. done();
  1060. }));
  1061. it("combines subsequent join/leave messages when users enter or exit a groupchat",
  1062. mock.initConverse(['chatBoxesFetched'], {}, async function (done, _converse) {
  1063. await mock.openAndEnterChatRoom(_converse, 'coven@chat.shakespeare.lit', 'romeo')
  1064. const view = _converse.chatboxviews.get('coven@chat.shakespeare.lit');
  1065. await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent.trim() === "romeo has entered the groupchat");
  1066. let presence = u.toStanza(
  1067. `<presence xmlns="jabber:client" to="romeo@montague.lit/orchard" from="coven@chat.shakespeare.lit/fabio">
  1068. <c xmlns="http://jabber.org/protocol/caps" node="http://conversations.im" ver="INI3xjRUioclBTP/aACfWi5m9UY=" hash="sha-1"/>
  1069. <x xmlns="http://jabber.org/protocol/muc#user">
  1070. <item affiliation="none" jid="fabio@montefuscolo.com.br/Conversations.ZvLu" role="participant"/>
  1071. </x>
  1072. </presence>`);
  1073. _converse.connection._dataRecv(mock.createRequest(presence));
  1074. await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent.trim() === "romeo and fabio have entered the groupchat");
  1075. presence = u.toStanza(
  1076. `<presence xmlns="jabber:client" to="romeo@montague.lit/orchard" from="coven@chat.shakespeare.lit/Dele Olajide">
  1077. <x xmlns="http://jabber.org/protocol/muc#user">
  1078. <item affiliation="none" jid="deleo@traderlynk.4ng.net/converse.js-39320524" role="participant"/>
  1079. </x>
  1080. </presence>`);
  1081. _converse.connection._dataRecv(mock.createRequest(presence));
  1082. await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent.trim() === "romeo, fabio and Dele Olajide have entered the groupchat");
  1083. presence = u.toStanza(
  1084. `<presence xmlns="jabber:client" to="romeo@montague.lit/orchard" from="coven@chat.shakespeare.lit/jcbrand">
  1085. <x xmlns="http://jabber.org/protocol/muc#user">
  1086. <item affiliation="owner" jid="jc@opkode.com/converse.js-30645022" role="moderator"/>
  1087. <status code="110"/>
  1088. </x>
  1089. </presence>`);
  1090. _converse.connection._dataRecv(mock.createRequest(presence));
  1091. await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent.trim() === "romeo, fabio and others have entered the groupchat");
  1092. presence = u.toStanza(
  1093. `<presence xmlns="jabber:client" to="romeo@montague.lit/orchard" type="unavailable" from="coven@chat.shakespeare.lit/Dele Olajide">
  1094. <x xmlns="http://jabber.org/protocol/muc#user">
  1095. <item affiliation="none" jid="deleo@traderlynk.4ng.net/converse.js-39320524" role="none"/>
  1096. </x>
  1097. </presence>`);
  1098. _converse.connection._dataRecv(mock.createRequest(presence));
  1099. await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent.trim() ===
  1100. "romeo, fabio and jcbrand have entered the groupchat\nDele Olajide has left the groupchat");
  1101. presence = u.toStanza(
  1102. `<presence xmlns="jabber:client" to="romeo@montague.lit/orchard" from="coven@chat.shakespeare.lit/Dele Olajide">
  1103. <x xmlns="http://jabber.org/protocol/muc#user">
  1104. <item affiliation="none" jid="deleo@traderlynk.4ng.net/converse.js-74567907" role="participant"/>
  1105. </x>
  1106. </presence>`);
  1107. _converse.connection._dataRecv(mock.createRequest(presence));
  1108. await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent.trim() ===
  1109. "romeo, fabio and others have entered the groupchat");
  1110. presence = u.toStanza(
  1111. `<presence xmlns="jabber:client" to="romeo@montague.lit/orchard" from="coven@chat.shakespeare.lit/fuvuv" xml:lang="en">
  1112. <c xmlns="http://jabber.org/protocol/caps" node="http://jabber.pix-art.de" ver="5tOurnuFnp2h50hKafeUyeN4Yl8=" hash="sha-1"/>
  1113. <x xmlns="vcard-temp:x:update"/>
  1114. <x xmlns="http://jabber.org/protocol/muc#user">
  1115. <item affiliation="none" jid="fuvuv@blabber.im/Pix-Art Messenger.8zoB" role="participant"/>
  1116. </x>
  1117. </presence>`);
  1118. _converse.connection._dataRecv(mock.createRequest(presence));
  1119. await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent.trim() ===
  1120. "romeo, fabio and others have entered the groupchat");
  1121. presence = u.toStanza(
  1122. `<presence xmlns="jabber:client" to="romeo@montague.lit/orchard" type="unavailable" from="coven@chat.shakespeare.lit/fuvuv">
  1123. <x xmlns="http://jabber.org/protocol/muc#user">
  1124. <item affiliation="none" jid="fuvuv@blabber.im/Pix-Art Messenger.8zoB" role="none"/>
  1125. </x>
  1126. </presence>`);
  1127. _converse.connection._dataRecv(mock.createRequest(presence));
  1128. await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent.trim() ===
  1129. "romeo, fabio and others have entered the groupchat\nfuvuv has left the groupchat");
  1130. presence = u.toStanza(
  1131. `<presence xmlns="jabber:client" to="romeo@montague.lit/orchard" type="unavailable" from="coven@chat.shakespeare.lit/fabio">
  1132. <status>Disconnected: Replaced by new connection</status>
  1133. <x xmlns="http://jabber.org/protocol/muc#user">
  1134. <item affiliation="none" jid="fabio@montefuscolo.com.br/Conversations.ZvLu" role="none"/>
  1135. </x>
  1136. </presence>`);
  1137. _converse.connection._dataRecv(mock.createRequest(presence));
  1138. await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent.trim() ===
  1139. "romeo, jcbrand and Dele Olajide have entered the groupchat\nfuvuv and fabio have left the groupchat");
  1140. presence = u.toStanza(
  1141. `<presence xmlns="jabber:client" to="romeo@montague.lit/orchard" from="coven@chat.shakespeare.lit/fabio">
  1142. <c xmlns="http://jabber.org/protocol/caps" node="http://conversations.im" ver="INI3xjRUioclBTP/aACfWi5m9UY=" hash="sha-1"/>
  1143. <status>Ready for a new day</status>
  1144. <x xmlns="http://jabber.org/protocol/muc#user">
  1145. <item affiliation="none" jid="fabio@montefuscolo.com.br/Conversations.ZvLu" role="participant"/>
  1146. </x>
  1147. </presence>`);
  1148. _converse.connection._dataRecv(mock.createRequest(presence));
  1149. await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent.trim() ===
  1150. "romeo, jcbrand and others have entered the groupchat\nfuvuv has left the groupchat");
  1151. presence = u.toStanza(
  1152. `<presence xmlns="jabber:client" to="romeo@montague.lit/orchard" type="unavailable" from="coven@chat.shakespeare.lit/fabio">
  1153. <status>Disconnected: closed</status>
  1154. <x xmlns="http://jabber.org/protocol/muc#user">
  1155. <item affiliation="none" jid="fabio@montefuscolo.com.br/Conversations.ZvLu" role="none"/>
  1156. </x>
  1157. </presence>`);
  1158. _converse.connection._dataRecv(mock.createRequest(presence));
  1159. await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent.trim() ===
  1160. "romeo, jcbrand and Dele Olajide have entered the groupchat\nfuvuv and fabio have left the groupchat");
  1161. presence = u.toStanza(
  1162. `<presence xmlns="jabber:client" to="romeo@montague.lit/orchard" type="unavailable" from="coven@chat.shakespeare.lit/Dele Olajide">
  1163. <x xmlns="http://jabber.org/protocol/muc#user">
  1164. <item affiliation="none" jid="deleo@traderlynk.4ng.net/converse.js-74567907" role="none"/>
  1165. </x>
  1166. </presence>`);
  1167. _converse.connection._dataRecv(mock.createRequest(presence));
  1168. await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent.trim() ===
  1169. "romeo and jcbrand have entered the groupchat\nfuvuv, fabio and Dele Olajide have left the groupchat");
  1170. presence = u.toStanza(
  1171. `<presence xmlns="jabber:client" to="romeo@montague.lit/orchard" from="coven@chat.shakespeare.lit/fabio">
  1172. <c xmlns="http://jabber.org/protocol/caps" node="http://conversations.im" ver="INI3xjRUioclBTP/aACfWi5m9UY=" hash="sha-1"/>
  1173. <x xmlns="http://jabber.org/protocol/muc#user">
  1174. <item affiliation="none" jid="fabio@montefuscolo.com.br/Conversations.ZvLu" role="participant"/>
  1175. </x>
  1176. </presence>`);
  1177. _converse.connection._dataRecv(mock.createRequest(presence));
  1178. await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent.trim() ===
  1179. "romeo, jcbrand and fabio have entered the groupchat\nfuvuv and Dele Olajide have left the groupchat");
  1180. expect(1).toBe(1);
  1181. done();
  1182. }));
  1183. it("doesn't show the disconnection messages when join_leave_events is not in muc_show_info_messages setting",
  1184. mock.initConverse(['chatBoxesFetched'], {'muc_show_info_messages': []}, async function (done, _converse) {
  1185. spyOn(_converse.ChatRoom.prototype, 'onOccupantAdded').and.callThrough();
  1186. spyOn(_converse.ChatRoom.prototype, 'onOccupantRemoved').and.callThrough();
  1187. await mock.openAndEnterChatRoom(_converse, 'coven@chat.shakespeare.lit', 'some1');
  1188. const view = _converse.chatboxviews.get('coven@chat.shakespeare.lit');
  1189. let presence = $pres({
  1190. to: 'romeo@montague.lit/orchard',
  1191. from: 'coven@chat.shakespeare.lit/newguy'
  1192. }).c('x', {xmlns: Strophe.NS.MUC_USER})
  1193. .c('item', {
  1194. 'affiliation': 'none',
  1195. 'jid': 'newguy@montague.lit/_converse.js-290929789',
  1196. 'role': 'participant'
  1197. });
  1198. _converse.connection._dataRecv(mock.createRequest(presence));
  1199. await u.waitUntil(() => view.model.onOccupantAdded.calls.count() === 2);
  1200. expect(view.model.notifications.get('entered')).toBeFalsy();
  1201. expect(view.querySelector('.chat-content__notifications').textContent.trim()).toBe('');
  1202. await mock.sendMessage(view, 'hello world');
  1203. presence = u.toStanza(
  1204. `<presence xmlns="jabber:client" to="romeo@montague.lit/orchard" type="unavailable" from="coven@chat.shakespeare.lit/newguy">
  1205. <status>Gotta go!</status>
  1206. <x xmlns="http://jabber.org/protocol/muc#user">
  1207. <item affiliation="none" jid="newguy@montague.lit/_converse.js-290929789" role="none"/>
  1208. </x>
  1209. </presence>`);
  1210. _converse.connection._dataRecv(mock.createRequest(presence));
  1211. await u.waitUntil(() => view.model.onOccupantRemoved.calls.count());
  1212. expect(view.model.onOccupantRemoved.calls.count()).toBe(1);
  1213. expect(view.model.notifications.get('entered')).toBeFalsy();
  1214. await mock.sendMessage(view, 'hello world');
  1215. expect(view.querySelector('.chat-content__notifications').textContent.trim()).toBe('');
  1216. done();
  1217. }));
  1218. it("role-change messages that follow a MUC leave are left out",
  1219. mock.initConverse(['chatBoxesFetched'], {}, async function (done, _converse) {
  1220. // See https://github.com/conversejs/converse.js/issues/1259
  1221. await mock.openAndEnterChatRoom(_converse, 'conversations@conference.siacs.eu', 'romeo');
  1222. const presence = $pres({
  1223. to: 'romeo@montague.lit/orchard',
  1224. from: 'conversations@conference.siacs.eu/Guus'
  1225. }).c('x', {
  1226. 'xmlns': Strophe.NS.MUC_USER
  1227. }).c('item', {
  1228. 'affiliation': 'none',
  1229. 'jid': 'Guus@montague.lit/xxx',
  1230. 'role': 'visitor'
  1231. });
  1232. _converse.connection._dataRecv(mock.createRequest(presence));
  1233. const view = _converse.chatboxviews.get('conversations@conference.siacs.eu');
  1234. const msg = $msg({
  1235. 'from': 'conversations@conference.siacs.eu/romeo',
  1236. 'id': u.getUniqueId(),
  1237. 'to': 'romeo@montague.lit',
  1238. 'type': 'groupchat'
  1239. }).c('body').t('Some message').tree();
  1240. await view.model.handleMessageStanza(msg);
  1241. await u.waitUntil(() => sizzle('.chat-msg:last .chat-msg__text', view.content).pop());
  1242. let stanza = u.toStanza(
  1243. `<presence xmlns="jabber:client" to="romeo@montague.lit/orchard" type="unavailable" from="conversations@conference.siacs.eu/Guus">
  1244. <x xmlns="http://jabber.org/protocol/muc#user">
  1245. <item affiliation="none" role="none"/>
  1246. </x>
  1247. </presence>`);
  1248. _converse.connection._dataRecv(mock.createRequest(stanza));
  1249. stanza = u.toStanza(
  1250. `<presence xmlns="jabber:client" to="romeo@montague.lit/orchard" from="conversations@conference.siacs.eu/Guus">
  1251. <c xmlns="http://jabber.org/protocol/caps" node="http://conversations.im" ver="ISg6+9AoK1/cwhbNEDviSvjdPzI=" hash="sha-1"/>
  1252. <x xmlns="vcard-temp:x:update">
  1253. <photo>bf987c486c51fbc05a6a4a9f20dd19b5efba3758</photo>
  1254. </x>
  1255. <x xmlns="http://jabber.org/protocol/muc#user">
  1256. <item affiliation="none" role="visitor"/>
  1257. </x>
  1258. </presence>`);
  1259. _converse.connection._dataRecv(mock.createRequest(stanza));
  1260. await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent.trim()
  1261. === "romeo and Guus have entered the groupchat");
  1262. expect(1).toBe(1);
  1263. done();
  1264. }));
  1265. it("can be configured if you're its owner", mock.initConverse(['chatBoxesFetched'], {}, async function (done, _converse) {
  1266. let sent_IQ, IQ_id;
  1267. const sendIQ = _converse.connection.sendIQ;
  1268. spyOn(_converse.connection, 'sendIQ').and.callFake(function (iq, callback, errback) {
  1269. sent_IQ = iq;
  1270. IQ_id = sendIQ.bind(this)(iq, callback, errback);
  1271. });
  1272. await _converse.api.rooms.open('coven@chat.shakespeare.lit', {'nick': 'some1'});
  1273. const view = _converse.chatboxviews.get('coven@chat.shakespeare.lit');
  1274. await u.waitUntil(() => u.isVisible(view));
  1275. // We pretend this is a new room, so no disco info is returned.
  1276. const features_stanza = $iq({
  1277. from: 'coven@chat.shakespeare.lit',
  1278. 'id': IQ_id,
  1279. 'to': 'romeo@montague.lit/desktop',
  1280. 'type': 'error'
  1281. }).c('error', {'type': 'cancel'})
  1282. .c('item-not-found', {'xmlns': "urn:ietf:params:xml:ns:xmpp-stanzas"});
  1283. _converse.connection._dataRecv(mock.createRequest(features_stanza));
  1284. /* <presence to="romeo@montague.lit/_converse.js-29092160"
  1285. * from="coven@chat.shakespeare.lit/some1">
  1286. * <x xmlns="http://jabber.org/protocol/muc#user">
  1287. * <item affiliation="owner" jid="romeo@montague.lit/_converse.js-29092160" role="moderator"/>
  1288. * <status code="110"/>
  1289. * </x>
  1290. * </presence></body>
  1291. */
  1292. const presence = $pres({
  1293. to: 'romeo@montague.lit/_converse.js-29092160',
  1294. from: 'coven@chat.shakespeare.lit/some1'
  1295. }).c('x', {xmlns: Strophe.NS.MUC_USER})
  1296. .c('item', {
  1297. 'affiliation': 'owner',
  1298. 'jid': 'romeo@montague.lit/_converse.js-29092160',
  1299. 'role': 'moderator'
  1300. }).up()
  1301. .c('status', {code: '110'});
  1302. _converse.connection._dataRecv(mock.createRequest(presence));
  1303. await u.waitUntil(() => view.querySelector('.configure-chatroom-button') !== null);
  1304. view.querySelector('.configure-chatroom-button').click();
  1305. /* Check that an IQ is sent out, asking for the
  1306. * configuration form.
  1307. * See: // https://xmpp.org/extensions/xep-0045.html#example-163
  1308. *
  1309. * <iq from='crone1@shakespeare.lit/desktop'
  1310. * id='config1'
  1311. * to='coven@chat.shakespeare.lit'
  1312. * type='get'>
  1313. * <query xmlns='http://jabber.org/protocol/muc#owner'/>
  1314. * </iq>
  1315. */
  1316. expect(Strophe.serialize(sent_IQ)).toBe(
  1317. `<iq id="`+IQ_id+`" to="coven@chat.shakespeare.lit" type="get" xmlns="jabber:client">`+
  1318. `<query xmlns="http://jabber.org/protocol/muc#owner"/>`+
  1319. `</iq>`);
  1320. /* Server responds with the configuration form.
  1321. * See: // https://xmpp.org/extensions/xep-0045.html#example-165
  1322. */
  1323. const config_stanza = $iq({from: 'coven@chat.shakespeare.lit',
  1324. 'id': IQ_id,
  1325. 'to': 'romeo@montague.lit/desktop',
  1326. 'type': 'result'})
  1327. .c('query', { 'xmlns': 'http://jabber.org/protocol/muc#owner'})
  1328. .c('x', { 'xmlns': 'jabber:x:data', 'type': 'form'})
  1329. .c('title').t('Configuration for "coven" Room').up()
  1330. .c('instructions').t('Complete this form to modify the configuration of your room.').up()
  1331. .c('field', {'type': 'hidden', 'var': 'FORM_TYPE'})
  1332. .c('value').t('http://jabber.org/protocol/muc#roomconfig').up().up()
  1333. .c('field', {
  1334. 'label': 'Natural-Language Room Name',
  1335. 'type': 'text-single',
  1336. 'var': 'muc#roomconfig_roomname'})
  1337. .c('value').t('A Dark Cave').up().up()
  1338. .c('field', {
  1339. 'label': 'Short Description of Room',
  1340. 'type': 'text-single',
  1341. 'var': 'muc#roomconfig_roomdesc'})
  1342. .c('value').t('The place for all good witches!').up().up()
  1343. .c('field', {
  1344. 'label': 'Enable Public Logging?',
  1345. 'type': 'boolean',
  1346. 'var': 'muc#roomconfig_enablelogging'})
  1347. .c('value').t(0).up().up()
  1348. .c('field', {
  1349. 'label': 'Allow Occupants to Change Subject?',
  1350. 'type': 'boolean',
  1351. 'var': 'muc#roomconfig_changesubject'})
  1352. .c('value').t(0).up().up()
  1353. .c('field', {
  1354. 'label': 'Allow Occupants to Invite Others?',
  1355. 'type': 'boolean',
  1356. 'var': 'muc#roomconfig_allowinvites'})
  1357. .c('value').t(0).up().up()
  1358. .c('field', {
  1359. 'label': 'Who Can Send Private Messages?',
  1360. 'type': 'list-single',
  1361. 'var': 'muc#roomconfig_allowpm'})
  1362. .c('value').t('anyone').up()
  1363. .c('option', {'label': 'Anyone'})
  1364. .c('value').t('anyone').up().up()
  1365. .c('option', {'label': 'Anyone with Voice'})
  1366. .c('value').t('participants').up().up()
  1367. .c('option', {'label': 'Moderators Only'})
  1368. .c('value').t('moderators').up().up()
  1369. .c('option', {'label': 'Nobody'})
  1370. .c('value').t('none').up().up().up()
  1371. .c('field', {
  1372. 'label': 'Roles for which Presence is Broadcasted',
  1373. 'type': 'list-multi',
  1374. 'var': 'muc#roomconfig_presencebroadcast'})
  1375. .c('value').t('moderator').up()
  1376. .c('value').t('participant').up()
  1377. .c('value').t('visitor').up()
  1378. .c('option', {'label': 'Moderator'})
  1379. .c('value').t('moderator').up().up()
  1380. .c('option', {'label': 'Participant'})
  1381. .c('value').t('participant').up().up()
  1382. .c('option', {'label': 'Visitor'})
  1383. .c('value').t('visitor').up().up().up()
  1384. .c('field', {
  1385. 'label': 'Roles and Affiliations that May Retrieve Member List',
  1386. 'type': 'list-multi',
  1387. 'var': 'muc#roomconfig_getmemberlist'})
  1388. .c('value').t('moderator').up()
  1389. .c('value').t('participant').up()
  1390. .c('value').t('visitor').up()
  1391. .c('option', {'label': 'Moderator'})
  1392. .c('value').t('moderator').up().up()
  1393. .c('option', {'label': 'Participant'})
  1394. .c('value').t('participant').up().up()
  1395. .c('option', {'label': 'Visitor'})
  1396. .c('value').t('visitor').up().up().up()
  1397. .c('field', {
  1398. 'label': 'Make Room Publicly Searchable?',
  1399. 'type': 'boolean',
  1400. 'var': 'muc#roomconfig_publicroom'})
  1401. .c('value').t(0).up().up()
  1402. .c('field', {
  1403. 'label': 'Make Room Publicly Searchable?',
  1404. 'type': 'boolean',
  1405. 'var': 'muc#roomconfig_publicroom'})
  1406. .c('value').t(0).up().up()
  1407. .c('field', {
  1408. 'label': 'Make Room Persistent?',
  1409. 'type': 'boolean',
  1410. 'var': 'muc#roomconfig_persistentroom'})
  1411. .c('value').t(0).up().up()
  1412. .c('field', {
  1413. 'label': 'Make Room Moderated?',
  1414. 'type': 'boolean',
  1415. 'var': 'muc#roomconfig_moderatedroom'})
  1416. .c('value').t(0).up().up()
  1417. .c('field', {
  1418. 'label': 'Make Room Members Only?',
  1419. 'type': 'boolean',
  1420. 'var': 'muc#roomconfig_membersonly'})
  1421. .c('value').t(0).up().up()
  1422. .c('field', {
  1423. 'label': 'Password Required for Entry?',
  1424. 'type': 'boolean',
  1425. 'var': 'muc#roomconfig_passwordprotectedroom'})
  1426. .c('value').t(1).up().up()
  1427. .c('field', {'type': 'fixed'})
  1428. .c('value').t(
  1429. 'If a password is required to enter this groupchat, you must specify the password below.'
  1430. ).up().up()
  1431. .c('field', {
  1432. 'label': 'Password',
  1433. 'type': 'text-private',
  1434. 'var': 'muc#roomconfig_roomsecret'})
  1435. .c('value').t('cauldronburn');
  1436. _converse.connection._dataRecv(mock.createRequest(config_stanza));
  1437. const membersonly = await u.waitUntil(() => view.querySelector('input[name="muc#roomconfig_membersonly"]'));
  1438. expect(membersonly.getAttribute('type')).toBe('checkbox');
  1439. membersonly.checked = true;
  1440. const moderated = view.querySelectorAll('input[name="muc#roomconfig_moderatedroom"]');
  1441. expect(moderated.length).toBe(1);
  1442. expect(moderated[0].getAttribute('type')).toBe('checkbox');
  1443. moderated[0].checked = true;
  1444. const password = view.querySelectorAll('input[name="muc#roomconfig_roomsecret"]');
  1445. expect(password.length).toBe(1);
  1446. expect(password[0].getAttribute('type')).toBe('password');
  1447. const allowpm = view.querySelectorAll('select[name="muc#roomconfig_allowpm"]');
  1448. expect(allowpm.length).toBe(1);
  1449. allowpm[0].value = 'moderators';
  1450. const presencebroadcast = view.querySelectorAll('select[name="muc#roomconfig_presencebroadcast"]');
  1451. expect(presencebroadcast.length).toBe(1);
  1452. presencebroadcast[0].value = ['moderator'];
  1453. view.querySelector('.chatroom-form input[type="submit"]').click();
  1454. expect(sent_IQ.querySelector('field[var="muc#roomconfig_membersonly"] value').textContent.trim()).toBe('1');
  1455. expect(sent_IQ.querySelector('field[var="muc#roomconfig_moderatedroom"] value').textContent.trim()).toBe('1');
  1456. expect(sent_IQ.querySelector('field[var="muc#roomconfig_allowpm"] value').textContent.trim()).toBe('moderators');
  1457. expect(sent_IQ.querySelector('field[var="muc#roomconfig_presencebroadcast"] value').textContent.trim()).toBe('moderator');
  1458. done();
  1459. }));
  1460. it("shows all members even if they're not currently present in the groupchat",
  1461. mock.initConverse([], {}, async function (done, _converse) {
  1462. const muc_jid = 'lounge@montague.lit'
  1463. const members = [{
  1464. 'nick': 'juliet',
  1465. 'jid': 'juliet@capulet.lit',
  1466. 'affiliation': 'member'
  1467. }];
  1468. await mock.openAndEnterChatRoom(_converse, muc_jid, 'romeo', [], members);
  1469. const view = _converse.chatboxviews.get(muc_jid);
  1470. await u.waitUntil(() => view.model.occupants.length === 2);
  1471. const occupants = view.querySelector('.occupant-list');
  1472. for (let i=0; i<mock.chatroom_names.length; i++) {
  1473. const name = mock.chatroom_names[i];
  1474. const role = mock.chatroom_roles[name].role;
  1475. // See example 21 https://xmpp.org/extensions/xep-0045.html#enter-pres
  1476. const presence = $pres({
  1477. to:'romeo@montague.lit/pda',
  1478. from:'lounge@montague.lit/'+name
  1479. }).c('x').attrs({xmlns:'http://jabber.org/protocol/muc#user'})
  1480. .c('item').attrs({
  1481. affiliation: mock.chatroom_roles[name].affiliation,
  1482. jid: name.replace(/ /g,'.').toLowerCase() + '@montague.lit',
  1483. role: role
  1484. });
  1485. _converse.connection._dataRecv(mock.createRequest(presence));
  1486. }
  1487. await u.waitUntil(() => occupants.querySelectorAll('li').length > 2, 500);
  1488. expect(occupants.querySelectorAll('li').length).toBe(2+mock.chatroom_names.length);
  1489. expect(view.model.occupants.length).toBe(2+mock.chatroom_names.length);
  1490. mock.chatroom_names.forEach(name => {
  1491. const model = view.model.occupants.findWhere({'nick': name});
  1492. const index = view.model.occupants.indexOf(model);
  1493. expect(occupants.querySelectorAll('li .occupant-nick')[index].textContent.trim()).toBe(name);
  1494. });
  1495. // Test users leaving the groupchat
  1496. // https://xmpp.org/extensions/xep-0045.html#exit
  1497. for (let i=mock.chatroom_names.length-1; i>-1; i--) {
  1498. const name = mock.chatroom_names[i];
  1499. // See example 21 https://xmpp.org/extensions/xep-0045.html#enter-pres
  1500. const presence = $pres({
  1501. to:'romeo@montague.lit/pda',
  1502. from:'lounge@montague.lit/'+name,
  1503. type: 'unavailable'
  1504. }).c('x').attrs({xmlns:'http://jabber.org/protocol/muc#user'})
  1505. .c('item').attrs({
  1506. affiliation: mock.chatroom_roles[name].affiliation,
  1507. jid: name.replace(/ /g,'.').toLowerCase() + '@montague.lit',
  1508. role: 'none'
  1509. }).nodeTree;
  1510. _converse.connection._dataRecv(mock.createRequest(presence));
  1511. expect(occupants.querySelectorAll('li').length).toBe(8);
  1512. }
  1513. const presence = $pres({
  1514. to: 'romeo@montague.lit/pda',
  1515. from: 'lounge@montague.lit/nonmember'
  1516. }).c('x').attrs({xmlns:'http://jabber.org/protocol/muc#user'})
  1517. .c('item').attrs({
  1518. affiliation: null,
  1519. jid: 'servant@montague.lit',
  1520. role: 'visitor'
  1521. });
  1522. _converse.connection._dataRecv(mock.createRequest(presence));
  1523. await u.waitUntil(() => occupants.querySelectorAll('li').length > 8, 500);
  1524. expect(occupants.querySelectorAll('li').length).toBe(9);
  1525. expect(view.model.occupants.length).toBe(9);
  1526. expect(view.model.occupants.filter(o => o.isMember()).length).toBe(8);
  1527. view.model.rejoin();
  1528. // Test that members aren't removed when we reconnect
  1529. expect(view.model.occupants.length).toBe(8);
  1530. await u.waitUntil(() => occupants.querySelectorAll('li').length === 8);
  1531. done();
  1532. }));
  1533. it("shows users currently present in the groupchat",
  1534. mock.initConverse([], {}, async function (done, _converse) {
  1535. await mock.openAndEnterChatRoom(_converse, 'lounge@montague.lit', 'romeo');
  1536. var view = _converse.chatboxviews.get('lounge@montague.lit');
  1537. const occupants = view.querySelector('.occupant-list');
  1538. for (var i=0; i<mock.chatroom_names.length; i++) {
  1539. const name = mock.chatroom_names[i];
  1540. // See example 21 https://xmpp.org/extensions/xep-0045.html#enter-pres
  1541. const presence = $pres({
  1542. to:'romeo@montague.lit/pda',
  1543. from:'lounge@montague.lit/'+name
  1544. }).c('x').attrs({xmlns:'http://jabber.org/protocol/muc#user'})
  1545. .c('item').attrs({
  1546. affiliation: 'none',
  1547. jid: name.replace(/ /g,'.').toLowerCase() + '@montague.lit',
  1548. role: 'participant'
  1549. }).up()
  1550. .c('status');
  1551. _converse.connection._dataRecv(mock.createRequest(presence));
  1552. }
  1553. await u.waitUntil(() => occupants.querySelectorAll('li').length > 1, 500);
  1554. expect(occupants.querySelectorAll('li').length).toBe(1+mock.chatroom_names.length);
  1555. mock.chatroom_names.forEach(name => {
  1556. const model = view.model.occupants.findWhere({'nick': name});
  1557. const index = view.model.occupants.indexOf(model);
  1558. expect(occupants.querySelectorAll('li .occupant-nick')[index].textContent.trim()).toBe(name);
  1559. });
  1560. // Test users leaving the groupchat
  1561. // https://xmpp.org/extensions/xep-0045.html#exit
  1562. for (i=mock.chatroom_names.length-1; i>-1; i--) {
  1563. const name = mock.chatroom_names[i];
  1564. // See example 21 https://xmpp.org/extensions/xep-0045.html#enter-pres
  1565. const presence = $pres({
  1566. to:'romeo@montague.lit/pda',
  1567. from:'lounge@montague.lit/'+name,
  1568. type: 'unavailable'
  1569. }).c('x').attrs({xmlns:'http://jabber.org/protocol/muc#user'})
  1570. .c('item').attrs({
  1571. affiliation: "none",
  1572. jid: name.replace(/ /g,'.').toLowerCase() + '@montague.lit',
  1573. role: 'none'
  1574. });
  1575. _converse.connection._dataRecv(mock.createRequest(presence));
  1576. }
  1577. await u.waitUntil(() => occupants.querySelectorAll('li').length === 1);
  1578. done();
  1579. }));
  1580. it("indicates moderators and visitors by means of a special css class and tooltip",
  1581. mock.initConverse([], {'view_mode': 'fullscreen'}, async function (done, _converse) {
  1582. await mock.openAndEnterChatRoom(_converse, 'lounge@montague.lit', 'romeo');
  1583. const view = _converse.chatboxviews.get('lounge@montague.lit');
  1584. let contact_jid = mock.cur_names[2].replace(/ /g,'.').toLowerCase() + '@montague.lit';
  1585. await u.waitUntil(() => view.querySelectorAll('.occupant-list li').length, 500);
  1586. let occupants = view.querySelectorAll('.occupant-list li');
  1587. expect(occupants.length).toBe(1);
  1588. expect(occupants[0].querySelector('.occupant-nick').textContent.trim()).toBe("romeo");
  1589. expect(occupants[0].querySelectorAll('.badge').length).toBe(2);
  1590. expect(occupants[0].querySelectorAll('.badge')[0].textContent.trim()).toBe('Owner');
  1591. expect(sizzle('.badge:last', occupants[0]).pop().textContent.trim()).toBe('Moderator');
  1592. var presence = $pres({
  1593. to:'romeo@montague.lit/pda',
  1594. from:'lounge@montague.lit/moderatorman'
  1595. }).c('x').attrs({xmlns:'http://jabber.org/protocol/muc#user'})
  1596. .c('item').attrs({
  1597. affiliation: 'admin',
  1598. jid: contact_jid,
  1599. role: 'moderator',
  1600. }).up()
  1601. .c('status').attrs({code:'110'}).nodeTree;
  1602. _converse.connection._dataRecv(mock.createRequest(presence));
  1603. await u.waitUntil(() => view.querySelectorAll('.occupant-list li').length > 1, 500);
  1604. occupants = view.querySelectorAll('.occupant-list li');
  1605. expect(occupants.length).toBe(2);
  1606. expect(occupants[0].querySelector('.occupant-nick').textContent.trim()).toBe("moderatorman");
  1607. expect(occupants[1].querySelector('.occupant-nick').textContent.trim()).toBe("romeo");
  1608. expect(occupants[0].querySelectorAll('.badge').length).toBe(2);
  1609. expect(occupants[0].querySelectorAll('.badge')[0].textContent.trim()).toBe('Admin');
  1610. expect(occupants[0].querySelectorAll('.badge')[1].textContent.trim()).toBe('Moderator');
  1611. expect(occupants[0].getAttribute('title')).toBe(
  1612. contact_jid + ' This user is a moderator. Click to mention moderatorman in your message.'
  1613. );
  1614. contact_jid = mock.cur_names[3].replace(/ /g,'.').toLowerCase() + '@montague.lit';
  1615. presence = $pres({
  1616. to:'romeo@montague.lit/pda',
  1617. from:'lounge@montague.lit/visitorwoman'
  1618. }).c('x').attrs({xmlns:'http://jabber.org/protocol/muc#user'})
  1619. .c('item').attrs({
  1620. jid: contact_jid,
  1621. role: 'visitor',
  1622. }).up()
  1623. .c('status').attrs({code:'110'}).nodeTree;
  1624. _converse.connection._dataRecv(mock.createRequest(presence));
  1625. await u.waitUntil(() => view.querySelectorAll('.occupant-list li').length > 2, 500);
  1626. occupants = view.querySelector('.occupant-list').querySelectorAll('li');
  1627. expect(occupants.length).toBe(3);
  1628. expect(occupants[2].querySelector('.occupant-nick').textContent.trim()).toBe("visitorwoman");
  1629. expect(occupants[2].querySelectorAll('.badge').length).toBe(1);
  1630. expect(sizzle('.badge', occupants[2]).pop().textContent.trim()).toBe('Visitor');
  1631. expect(occupants[2].getAttribute('title')).toBe(
  1632. contact_jid + ' This user can NOT send messages in this groupchat. Click to mention visitorwoman in your message.'
  1633. );
  1634. done();
  1635. }));
  1636. it("properly handles notification that a room has been destroyed",
  1637. mock.initConverse([], {}, async function (done, _converse) {
  1638. await mock.openChatRoomViaModal(_converse, 'problematic@muc.montague.lit', 'romeo')
  1639. const presence = $pres().attrs({
  1640. from:'problematic@muc.montague.lit',
  1641. id:'n13mt3l',
  1642. to:'romeo@montague.lit/pda',
  1643. type:'error'})
  1644. .c('error').attrs({'type':'cancel'})
  1645. .c('gone').attrs({'xmlns':'urn:ietf:params:xml:ns:xmpp-stanzas'})
  1646. .t('xmpp:other-room@chat.jabberfr.org?join').up()
  1647. .c('text').attrs({'xmlns':'urn:ietf:params:xml:ns:xmpp-stanzas'})
  1648. .t("We didn't like the name").nodeTree;
  1649. const view = _converse.chatboxviews.get('problematic@muc.montague.lit');
  1650. _converse.connection._dataRecv(mock.createRequest(presence));
  1651. expect(view.querySelector('.chatroom-body .disconnect-msg').textContent.trim())
  1652. .toBe('This groupchat no longer exists');
  1653. expect(view.querySelector('.chatroom-body .destroyed-reason').textContent.trim())
  1654. .toBe(`"We didn't like the name"`);
  1655. expect(view.querySelector('.chatroom-body .moved-label').textContent.trim())
  1656. .toBe('The conversation has moved. Click below to enter.');
  1657. expect(view.querySelector('.chatroom-body .moved-link').textContent.trim())
  1658. .toBe(`other-room@chat.jabberfr.org`);
  1659. done();
  1660. }));
  1661. it("will use the user's reserved nickname, if it exists",
  1662. mock.initConverse(['chatBoxesFetched'], {}, async function (done, _converse) {
  1663. const IQ_stanzas = _converse.connection.IQ_stanzas;
  1664. const muc_jid = 'lounge@montague.lit';
  1665. await mock.openChatRoom(_converse, 'lounge', 'montague.lit', 'romeo');
  1666. let stanza = await u.waitUntil(() => IQ_stanzas.filter(
  1667. iq => iq.querySelector(
  1668. `iq[to="${muc_jid}"] query[xmlns="http://jabber.org/protocol/disco#info"]`
  1669. )).pop()
  1670. );
  1671. // We pretend this is a new room, so no disco info is returned.
  1672. const features_stanza = $iq({
  1673. from: 'lounge@montague.lit',
  1674. 'id': stanza.getAttribute('id'),
  1675. 'to': 'romeo@montague.lit/desktop',
  1676. 'type': 'error'
  1677. }).c('error', {'type': 'cancel'})
  1678. .c('item-not-found', {'xmlns': "urn:ietf:params:xml:ns:xmpp-stanzas"});
  1679. _converse.connection._dataRecv(mock.createRequest(features_stanza));
  1680. /* <iq from='hag66@shakespeare.lit/pda'
  1681. * id='getnick1'
  1682. * to='coven@chat.shakespeare.lit'
  1683. * type='get'>
  1684. * <query xmlns='http://jabber.org/protocol/disco#info'
  1685. * node='x-roomuser-item'/>
  1686. * </iq>
  1687. */
  1688. const iq = await u.waitUntil(() => _.filter(
  1689. IQ_stanzas,
  1690. s => sizzle(`iq[to="${muc_jid}"] query[node="x-roomuser-item"]`, s).length
  1691. ).pop()
  1692. );
  1693. expect(Strophe.serialize(iq)).toBe(
  1694. `<iq from="romeo@montague.lit/orchard" id="${iq.getAttribute('id')}" to="lounge@montague.lit" `+
  1695. `type="get" xmlns="jabber:client">`+
  1696. `<query node="x-roomuser-item" xmlns="http://jabber.org/protocol/disco#info"/></iq>`);
  1697. /* <iq from='coven@chat.shakespeare.lit'
  1698. * id='getnick1'
  1699. * to='hag66@shakespeare.lit/pda'
  1700. * type='result'>
  1701. * <query xmlns='http://jabber.org/protocol/disco#info'
  1702. * node='x-roomuser-item'>
  1703. * <identity
  1704. * category='conference'
  1705. * name='thirdwitch'
  1706. * type='text'/>
  1707. * </query>
  1708. * </iq>
  1709. */
  1710. const view = _converse.chatboxviews.get('lounge@montague.lit');
  1711. stanza = $iq({
  1712. 'type': 'result',
  1713. 'id': iq.getAttribute('id'),
  1714. 'from': view.model.get('jid'),
  1715. 'to': _converse.connection.jid
  1716. }).c('query', {'xmlns': 'http://jabber.org/protocol/disco#info', 'node': 'x-roomuser-item'})
  1717. .c('identity', {'category': 'conference', 'name': 'thirdwitch', 'type': 'text'});
  1718. _converse.connection._dataRecv(mock.createRequest(stanza));
  1719. // The user has just entered the groupchat (because join was called)
  1720. // and receives their own presence from the server.
  1721. // See example 24:
  1722. // https://xmpp.org/extensions/xep-0045.html#enter-pres
  1723. const presence = $pres({
  1724. to:'romeo@montague.lit/orchard',
  1725. from:'lounge@montague.lit/thirdwitch',
  1726. id:'DC352437-C019-40EC-B590-AF29E879AF97'
  1727. }).c('x').attrs({xmlns:'http://jabber.org/protocol/muc#user'})
  1728. .c('item').attrs({
  1729. affiliation: 'member',
  1730. jid: 'romeo@montague.lit/orchard',
  1731. role: 'participant'
  1732. }).up()
  1733. .c('status').attrs({code:'110'}).up()
  1734. .c('status').attrs({code:'210'}).nodeTree;
  1735. _converse.connection._dataRecv(mock.createRequest(presence));
  1736. await u.waitUntil(() => (view.model.session.get('connection_status') === converse.ROOMSTATUS.ENTERED));
  1737. await mock.returnMemberLists(_converse, muc_jid, [], ['member', 'admin', 'owner']);
  1738. await u.waitUntil(() => view.querySelectorAll('.chat-content .chat-info').length);
  1739. const info_text = sizzle('.chat-content .chat-info:first', view).pop().textContent.trim();
  1740. expect(info_text).toBe('Your nickname has been automatically set to thirdwitch');
  1741. done();
  1742. }));
  1743. it("allows the user to invite their roster contacts to enter the groupchat",
  1744. mock.initConverse(['chatBoxesFetched'], {'view_mode': 'fullscreen'}, async function (done, _converse) {
  1745. // We need roster contacts, so that we have someone to invite
  1746. await mock.waitForRoster(_converse, 'current');
  1747. const features = [
  1748. 'http://jabber.org/protocol/muc',
  1749. 'jabber:iq:register',
  1750. 'muc_passwordprotected',
  1751. 'muc_hidden',
  1752. 'muc_temporary',
  1753. 'muc_membersonly',
  1754. 'muc_unmoderated',
  1755. 'muc_anonymous'
  1756. ]
  1757. await mock.openAndEnterChatRoom(_converse, 'lounge@montague.lit', 'romeo', features);
  1758. const view = _converse.chatboxviews.get('lounge@montague.lit');
  1759. expect(view.model.getOwnAffiliation()).toBe('owner');
  1760. expect(view.model.features.get('open')).toBe(false);
  1761. expect(view.querySelector('.open-invite-modal')).not.toBe(null);
  1762. // Members can't invite if the room isn't open
  1763. view.model.getOwnOccupant().set('affiliation', 'member');
  1764. await u.waitUntil(() => view.querySelector('.open-invite-modal') === null);
  1765. view.model.features.set('open', 'true');
  1766. await u.waitUntil(() => view.querySelector('.open-invite-modal'));
  1767. view.querySelector('.open-invite-modal').click();
  1768. const modal = _converse.api.modal.get('muc-invite-modal');
  1769. await u.waitUntil(() => u.isVisible(modal.el), 1000)
  1770. expect(modal.el.querySelectorAll('#invitee_jids').length).toBe(1);
  1771. expect(modal.el.querySelectorAll('textarea').length).toBe(1);
  1772. spyOn(view.model, 'directInvite').and.callThrough();
  1773. const input = modal.el.querySelector('#invitee_jids');
  1774. input.value = "Balt";
  1775. modal.el.querySelector('button[type="submit"]').click();
  1776. await u.waitUntil(() => modal.el.querySelector('.error'));
  1777. const error = modal.el.querySelector('.error');
  1778. expect(error.textContent).toBe('Please enter a valid XMPP address');
  1779. let evt = new Event('input');
  1780. input.dispatchEvent(evt);
  1781. let sent_stanza;
  1782. spyOn(_converse.connection, 'send').and.callFake(stanza => (sent_stanza = stanza));
  1783. const hint = await u.waitUntil(() => modal.el.querySelector('.suggestion-box__results li'));
  1784. expect(input.value).toBe('Balt');
  1785. expect(hint.textContent.trim()).toBe('Balthasar');
  1786. evt = new Event('mousedown', {'bubbles': true});
  1787. evt.button = 0;
  1788. hint.dispatchEvent(evt);
  1789. const textarea = modal.el.querySelector('textarea');
  1790. textarea.value = "Please join!";
  1791. modal.el.querySelector('button[type="submit"]').click();
  1792. expect(view.model.directInvite).toHaveBeenCalled();
  1793. expect(sent_stanza.toLocaleString()).toBe(
  1794. `<message from="romeo@montague.lit/orchard" `+
  1795. `id="${sent_stanza.nodeTree.getAttribute("id")}" `+
  1796. `to="balthasar@montague.lit" `+
  1797. `xmlns="jabber:client">`+
  1798. `<x jid="lounge@montague.lit" reason="Please join!" xmlns="jabber:x:conference"/>`+
  1799. `</message>`
  1800. );
  1801. done();
  1802. }));
  1803. it("can be joined automatically, based upon a received invite",
  1804. mock.initConverse([], {}, async function (done, _converse) {
  1805. await mock.waitForRoster(_converse, 'current'); // We need roster contacts, who can invite us
  1806. const name = mock.cur_names[0];
  1807. const from_jid = name.replace(/ /g,'.').toLowerCase() + '@montague.lit';
  1808. await u.waitUntil(() => _converse.roster.get(from_jid).vcard.get('fullname'));
  1809. spyOn(window, 'confirm').and.callFake(() => true);
  1810. await mock.openAndEnterChatRoom(_converse, 'lounge@montague.lit', 'romeo');
  1811. const view = _converse.chatboxviews.get('lounge@montague.lit');
  1812. await view.close(); // Hack, otherwise we have to mock stanzas.
  1813. const muc_jid = 'lounge@montague.lit';
  1814. const reason = "Please join this groupchat";
  1815. expect(_converse.chatboxes.models.length).toBe(1);
  1816. expect(_converse.chatboxes.models[0].id).toBe("controlbox");
  1817. const stanza = u.toStanza(`
  1818. <message xmlns="jabber:client" to="${_converse.bare_jid}" from="${from_jid}" id="9bceb415-f34b-4fa4-80d5-c0d076a24231">
  1819. <x xmlns="jabber:x:conference" jid="${muc_jid}" reason="${reason}"/>
  1820. </message>`);
  1821. await _converse.onDirectMUCInvitation(stanza);
  1822. expect(window.confirm).toHaveBeenCalledWith(
  1823. name + ' has invited you to join a groupchat: '+ muc_jid +
  1824. ', and left the following reason: "'+reason+'"');
  1825. expect(_converse.chatboxes.models.length).toBe(2);
  1826. expect(_converse.chatboxes.models[0].id).toBe('controlbox');
  1827. expect(_converse.chatboxes.models[1].id).toBe(muc_jid);
  1828. done();
  1829. }));
  1830. it("shows received groupchat messages",
  1831. mock.initConverse([], {}, async function (done, _converse) {
  1832. const text = 'This is a received message';
  1833. await mock.openAndEnterChatRoom(_converse, 'lounge@montague.lit', 'romeo');
  1834. spyOn(_converse.api, "trigger").and.callThrough();
  1835. const view = _converse.chatboxviews.get('lounge@montague.lit');
  1836. if (!view.querySelectorAll('.chat-area').length) {
  1837. view.renderChatArea();
  1838. }
  1839. var nick = mock.chatroom_names[0];
  1840. view.model.occupants.create({
  1841. 'nick': nick,
  1842. 'muc_jid': `${view.model.get('jid')}/${nick}`
  1843. });
  1844. const message = $msg({
  1845. from: 'lounge@montague.lit/'+nick,
  1846. id: '1',
  1847. to: 'romeo@montague.lit',
  1848. type: 'groupchat'
  1849. }).c('body').t(text);
  1850. await view.model.handleMessageStanza(message.nodeTree);
  1851. await u.waitUntil(() => view.querySelectorAll('.chat-msg').length);
  1852. expect(view.content.querySelectorAll('.chat-msg').length).toBe(1);
  1853. expect(view.content.querySelector('.chat-msg__text').textContent.trim()).toBe(text);
  1854. expect(_converse.api.trigger).toHaveBeenCalledWith('message', jasmine.any(Object));
  1855. done();
  1856. }));
  1857. it("shows sent groupchat messages", mock.initConverse([], {}, async function (done, _converse) {
  1858. await mock.openAndEnterChatRoom(_converse, 'lounge@montague.lit', 'romeo');
  1859. spyOn(_converse.api, "trigger").and.callThrough();
  1860. const view = _converse.chatboxviews.get('lounge@montague.lit');
  1861. if (!view.querySelectorAll('.chat-area').length) {
  1862. view.renderChatArea();
  1863. }
  1864. const text = 'This is a sent message';
  1865. const textarea = view.querySelector('.chat-textarea');
  1866. textarea.value = text;
  1867. view.onKeyDown({
  1868. target: textarea,
  1869. preventDefault: function preventDefault () {},
  1870. keyCode: 13
  1871. });
  1872. await new Promise(resolve => view.model.messages.once('rendered', resolve));
  1873. expect(_converse.api.trigger).toHaveBeenCalledWith('messageSend', jasmine.any(_converse.Message));
  1874. expect(view.content.querySelectorAll('.chat-msg').length).toBe(1);
  1875. // Let's check that if we receive the same message again, it's
  1876. // not shown.
  1877. const stanza = u.toStanza(`
  1878. <message xmlns="jabber:client"
  1879. from="lounge@montague.lit/romeo"
  1880. to="${_converse.connection.jid}"
  1881. type="groupchat">
  1882. <body>${text}</body>
  1883. <stanza-id xmlns="urn:xmpp:sid:0"
  1884. id="5f3dbc5e-e1d3-4077-a492-693f3769c7ad"
  1885. by="lounge@montague.lit"/>
  1886. <origin-id xmlns="urn:xmpp:sid:0" id="${view.model.messages.at(0).get('origin_id')}"/>
  1887. </message>`);
  1888. await view.model.handleMessageStanza(stanza);
  1889. expect(view.content.querySelectorAll('.chat-msg').length).toBe(1);
  1890. expect(sizzle('.chat-msg__text:last').pop().textContent.trim()).toBe(text);
  1891. expect(view.model.messages.length).toBe(1);
  1892. // We don't emit an event if it's our own message
  1893. expect(_converse.api.trigger.calls.count(), 1);
  1894. done();
  1895. }));
  1896. it("will cause the chat area to be scrolled down only if it was at the bottom already",
  1897. mock.initConverse([], {}, async function (done, _converse) {
  1898. const message = 'This message is received while the chat area is scrolled up';
  1899. await mock.openAndEnterChatRoom(_converse, 'lounge@montague.lit', 'romeo');
  1900. const view = _converse.chatboxviews.get('lounge@montague.lit');
  1901. spyOn(view, 'scrollDown').and.callThrough();
  1902. // Create enough messages so that there's a scrollbar.
  1903. const promises = [];
  1904. for (let i=0; i<20; i++) {
  1905. promises.push(
  1906. view.model.handleMessageStanza(
  1907. $msg({
  1908. from: 'lounge@montague.lit/someone',
  1909. to: 'romeo@montague.lit.com',
  1910. type: 'groupchat',
  1911. id: u.getUniqueId(),
  1912. }).c('body').t('Message: '+i).tree())
  1913. );
  1914. }
  1915. await Promise.all(promises);
  1916. // Give enough time for `markScrolled` to have been called
  1917. setTimeout(async () => {
  1918. view.content.scrollTop = 0;
  1919. await view.model.handleMessageStanza(
  1920. $msg({
  1921. from: 'lounge@montague.lit/someone',
  1922. to: 'romeo@montague.lit.com',
  1923. type: 'groupchat',
  1924. id: u.getUniqueId(),
  1925. }).c('body').t(message).tree());
  1926. await new Promise(resolve => view.model.messages.once('rendered', resolve));
  1927. // Now check that the message appears inside the chatbox in the DOM
  1928. const msg_txt = sizzle('.chat-msg:last .chat-msg__text', view.content).pop().textContent;
  1929. expect(msg_txt).toEqual(message);
  1930. expect(view.content.scrollTop).toBe(0);
  1931. done();
  1932. }, 500);
  1933. }));
  1934. it("reconnects when no-acceptable error is returned when sending a message",
  1935. mock.initConverse([], {}, async function (done, _converse) {
  1936. const muc_jid = 'coven@chat.shakespeare.lit';
  1937. await mock.openAndEnterChatRoom(_converse, muc_jid, 'romeo');
  1938. const view = _converse.chatboxviews.get(muc_jid);
  1939. expect(view.model.session.get('connection_status')).toBe(converse.ROOMSTATUS.ENTERED);
  1940. await mock.sendMessage(view, 'hello world');
  1941. const stanza = u.toStanza(`
  1942. <message xmlns='jabber:client'
  1943. from='${muc_jid}'
  1944. type='error'
  1945. to='${_converse.bare_jid}'>
  1946. <error type='cancel'>
  1947. <not-acceptable xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/>
  1948. </error>
  1949. </message>`);
  1950. _converse.connection._dataRecv(mock.createRequest(stanza));
  1951. let sent_stanzas = _converse.connection.sent_stanzas;
  1952. const iq = await u.waitUntil(() => sent_stanzas.filter(s => sizzle(`[xmlns="${Strophe.NS.PING}"]`, s).length).pop());
  1953. expect(Strophe.serialize(iq)).toBe(
  1954. `<iq id="${iq.getAttribute('id')}" to="coven@chat.shakespeare.lit/romeo" type="get" xmlns="jabber:client">`+
  1955. `<ping xmlns="urn:xmpp:ping"/>`+
  1956. `</iq>`);
  1957. const result = u.toStanza(`
  1958. <iq from='${muc_jid}'
  1959. id='${iq.getAttribute('id')}'
  1960. to='${_converse.bare_jid}'
  1961. type='error'>
  1962. <error type='cancel'>
  1963. <not-acceptable xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/>
  1964. </error>
  1965. </iq>`);
  1966. sent_stanzas = _converse.connection.sent_stanzas;
  1967. const index = sent_stanzas.length -1;
  1968. _converse.connection.IQ_stanzas = [];
  1969. _converse.connection._dataRecv(mock.createRequest(result));
  1970. await mock.getRoomFeatures(_converse, muc_jid);
  1971. const pres = await u.waitUntil(
  1972. () => sent_stanzas.slice(index).filter(s => s.nodeName === 'presence').pop());
  1973. expect(Strophe.serialize(pres)).toBe(
  1974. `<presence from="${_converse.jid}" to="coven@chat.shakespeare.lit/romeo" xmlns="jabber:client">`+
  1975. `<x xmlns="http://jabber.org/protocol/muc"><history maxstanzas="0"/></x>`+
  1976. `</presence>`);
  1977. done();
  1978. }));
  1979. it("informs users if the room configuration has changed",
  1980. mock.initConverse([], {}, async function (done, _converse) {
  1981. const muc_jid = 'coven@chat.shakespeare.lit';
  1982. await mock.openAndEnterChatRoom(_converse, 'coven@chat.shakespeare.lit', 'romeo');
  1983. const view = _converse.chatboxviews.get(muc_jid);
  1984. expect(view.model.session.get('connection_status')).toBe(converse.ROOMSTATUS.ENTERED);
  1985. const stanza = u.toStanza(`
  1986. <message from='${muc_jid}'
  1987. id='80349046-F26A-44F3-A7A6-54825064DD9E'
  1988. to='${_converse.jid}'
  1989. type='groupchat'>
  1990. <x xmlns='http://jabber.org/protocol/muc#user'>
  1991. <status code='170'/>
  1992. </x>
  1993. </message>`);
  1994. _converse.connection._dataRecv(mock.createRequest(stanza));
  1995. await u.waitUntil(() => view.querySelectorAll('.chat-content .chat-info').length);
  1996. const info_messages = view.querySelectorAll('.chat-content .chat-info');
  1997. expect(info_messages[0].textContent.trim()).toBe('Groupchat logging is now enabled');
  1998. done();
  1999. }));
  2000. it("informs users if their nicknames have been changed.",
  2001. mock.initConverse([], {}, async function (done, _converse) {
  2002. /* The service then sends two presence stanzas to the full JID
  2003. * of each occupant (including the occupant who is changing his
  2004. * or her room nickname), one of type "unavailable" for the old
  2005. * nickname and one indicating availability for the new
  2006. * nickname.
  2007. *
  2008. * See: https://xmpp.org/extensions/xep-0045.html#changenick
  2009. *
  2010. * <presence
  2011. * from='coven@montague.lit/thirdwitch'
  2012. * id='DC352437-C019-40EC-B590-AF29E879AF98'
  2013. * to='hag66@shakespeare.lit/pda'
  2014. * type='unavailable'>
  2015. * <x xmlns='http://jabber.org/protocol/muc#user'>
  2016. * <item affiliation='member'
  2017. * jid='hag66@shakespeare.lit/pda'
  2018. * nick='oldhag'
  2019. * role='participant'/>
  2020. * <status code='303'/>
  2021. * <status code='110'/>
  2022. * </x>
  2023. * </presence>
  2024. *
  2025. * <presence
  2026. * from='coven@montague.lit/oldhag'
  2027. * id='5B4F27A4-25ED-43F7-A699-382C6B4AFC67'
  2028. * to='hag66@shakespeare.lit/pda'>
  2029. * <x xmlns='http://jabber.org/protocol/muc#user'>
  2030. * <item affiliation='member'
  2031. * jid='hag66@shakespeare.lit/pda'
  2032. * role='participant'/>
  2033. * <status code='110'/>
  2034. * </x>
  2035. * </presence>
  2036. */
  2037. const __ = _converse.__;
  2038. await mock.openAndEnterChatRoom(_converse, 'lounge@montague.lit', 'oldnick');
  2039. const view = _converse.chatboxviews.get('lounge@montague.lit');
  2040. expect(view.model.session.get('connection_status')).toBe(converse.ROOMSTATUS.ENTERED);
  2041. await u.waitUntil(() => view.querySelectorAll('li .occupant-nick').length, 500);
  2042. let occupants = view.querySelector('.occupant-list');
  2043. expect(occupants.childElementCount).toBe(1);
  2044. expect(occupants.firstElementChild.querySelector('.occupant-nick').textContent.trim()).toBe("oldnick");
  2045. const csntext = await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent);
  2046. expect(csntext.trim()).toEqual("oldnick has entered the groupchat");
  2047. let presence = $pres().attrs({
  2048. from:'lounge@montague.lit/oldnick',
  2049. id:'DC352437-C019-40EC-B590-AF29E879AF98',
  2050. to:'romeo@montague.lit/pda',
  2051. type:'unavailable'
  2052. })
  2053. .c('x').attrs({xmlns:'http://jabber.org/protocol/muc#user'})
  2054. .c('item').attrs({
  2055. affiliation: 'owner',
  2056. jid: 'romeo@montague.lit/pda',
  2057. nick: 'newnick',
  2058. role: 'moderator'
  2059. }).up()
  2060. .c('status').attrs({code:'303'}).up()
  2061. .c('status').attrs({code:'110'}).nodeTree;
  2062. _converse.connection._dataRecv(mock.createRequest(presence));
  2063. await u.waitUntil(() => view.querySelectorAll('.chat-info').length);
  2064. expect(sizzle('div.chat-info:last').pop().textContent.trim()).toBe(
  2065. __(_converse.muc.new_nickname_messages["303"], "newnick")
  2066. );
  2067. expect(view.model.session.get('connection_status')).toBe(converse.ROOMSTATUS.ENTERED);
  2068. occupants = view.querySelector('.occupant-list');
  2069. expect(occupants.childElementCount).toBe(1);
  2070. presence = $pres().attrs({
  2071. from:'lounge@montague.lit/newnick',
  2072. id:'5B4F27A4-25ED-43F7-A699-382C6B4AFC67',
  2073. to:'romeo@montague.lit/pda'
  2074. })
  2075. .c('x').attrs({xmlns:'http://jabber.org/protocol/muc#user'})
  2076. .c('item').attrs({
  2077. affiliation: 'owner',
  2078. jid: 'romeo@montague.lit/pda',
  2079. role: 'moderator'
  2080. }).up()
  2081. .c('status').attrs({code:'110'}).nodeTree;
  2082. _converse.connection._dataRecv(mock.createRequest(presence));
  2083. expect(view.model.session.get('connection_status')).toBe(converse.ROOMSTATUS.ENTERED);
  2084. expect(view.content.querySelectorAll('div.chat-info').length).toBe(1);
  2085. expect(sizzle('div.chat-info', view.content)[0].textContent.trim()).toBe(
  2086. __(_converse.muc.new_nickname_messages["303"], "newnick")
  2087. );
  2088. occupants = view.querySelector('.occupant-list');
  2089. expect(occupants.childElementCount).toBe(1);
  2090. expect(sizzle('.occupant-nick:first', occupants).pop().textContent.trim()).toBe("newnick");
  2091. done();
  2092. }));
  2093. it("queries for the groupchat information before attempting to join the user",
  2094. mock.initConverse(['chatBoxesFetched'], {}, async function (done, _converse) {
  2095. const nick = "some1";
  2096. const IQ_stanzas = _converse.connection.IQ_stanzas;
  2097. const muc_jid = 'coven@chat.shakespeare.lit';
  2098. await _converse.api.rooms.open(muc_jid, { nick });
  2099. const stanza = await u.waitUntil(() => _.filter(
  2100. IQ_stanzas,
  2101. iq => iq.querySelector(
  2102. `iq[to="${muc_jid}"] query[xmlns="http://jabber.org/protocol/disco#info"]`
  2103. )).pop());
  2104. // Check that the groupchat queried for the feautures.
  2105. expect(Strophe.serialize(stanza)).toBe(
  2106. `<iq from="romeo@montague.lit/orchard" id="${stanza.getAttribute("id")}" to="${muc_jid}" type="get" xmlns="jabber:client">`+
  2107. `<query xmlns="http://jabber.org/protocol/disco#info"/>`+
  2108. `</iq>`);
  2109. /* <iq from='coven@chat.shakespeare.lit'
  2110. * id='ik3vs715'
  2111. * to='hag66@shakespeare.lit/pda'
  2112. * type='result'>
  2113. * <query xmlns='http://jabber.org/protocol/disco#info'>
  2114. * <identity
  2115. * category='conference'
  2116. * name='A Dark Cave'
  2117. * type='text'/>
  2118. * <feature var='http://jabber.org/protocol/muc'/>
  2119. * <feature var='muc_passwordprotected'/>
  2120. * <feature var='muc_hidden'/>
  2121. * <feature var='muc_temporary'/>
  2122. * <feature var='muc_open'/>
  2123. * <feature var='muc_unmoderated'/>
  2124. * <feature var='muc_nonanonymous'/>
  2125. * </query>
  2126. * </iq>
  2127. */
  2128. const features_stanza = $iq({
  2129. 'from': muc_jid,
  2130. 'id': stanza.getAttribute('id'),
  2131. 'to': 'romeo@montague.lit/desktop',
  2132. 'type': 'result'
  2133. })
  2134. .c('query', { 'xmlns': 'http://jabber.org/protocol/disco#info'})
  2135. .c('identity', {
  2136. 'category': 'conference',
  2137. 'name': 'A Dark Cave',
  2138. 'type': 'text'
  2139. }).up()
  2140. .c('feature', {'var': 'http://jabber.org/protocol/muc'}).up()
  2141. .c('feature', {'var': 'muc_passwordprotected'}).up()
  2142. .c('feature', {'var': 'muc_hidden'}).up()
  2143. .c('feature', {'var': 'muc_temporary'}).up()
  2144. .c('feature', {'var': 'muc_open'}).up()
  2145. .c('feature', {'var': 'muc_unmoderated'}).up()
  2146. .c('feature', {'var': 'muc_nonanonymous'});
  2147. _converse.connection._dataRecv(mock.createRequest(features_stanza));
  2148. let view = _converse.chatboxviews.get('coven@chat.shakespeare.lit');
  2149. const sent_stanzas = _converse.connection.sent_stanzas;
  2150. await u.waitUntil(() => sent_stanzas.filter(s => s.matches(`presence[to="${muc_jid}/${nick}"]`)).pop());
  2151. view = _converse.chatboxviews.get('coven@chat.shakespeare.lit');
  2152. expect(view.model.features.get('fetched')).toBeTruthy();
  2153. expect(view.model.features.get('passwordprotected')).toBe(true);
  2154. expect(view.model.features.get('hidden')).toBe(true);
  2155. expect(view.model.features.get('temporary')).toBe(true);
  2156. expect(view.model.features.get('open')).toBe(true);
  2157. expect(view.model.features.get('unmoderated')).toBe(true);
  2158. expect(view.model.features.get('nonanonymous')).toBe(true);
  2159. done();
  2160. }));
  2161. it("updates the shown features when the groupchat configuration has changed",
  2162. mock.initConverse([], {'view_mode': 'fullscreen'}, async function (done, _converse) {
  2163. let features = [
  2164. 'http://jabber.org/protocol/muc',
  2165. 'jabber:iq:register',
  2166. 'muc_passwordprotected',
  2167. 'muc_publicroom',
  2168. 'muc_temporary',
  2169. 'muc_open',
  2170. 'muc_unmoderated',
  2171. 'muc_nonanonymous'
  2172. ];
  2173. await mock.openAndEnterChatRoom(_converse, 'room@conference.example.org', 'romeo', features);
  2174. const jid = 'room@conference.example.org';
  2175. const view = _converse.chatboxviews.get(jid);
  2176. const info_el = view.querySelector(".show-muc-details-modal");
  2177. info_el.click();
  2178. const modal = _converse.api.modal.get('muc-details-modal');
  2179. await u.waitUntil(() => u.isVisible(modal.el), 1000);
  2180. let features_list = modal.el.querySelector('.features-list');
  2181. let features_shown = features_list.textContent.split('\n').map(s => s.trim()).filter(s => s);
  2182. expect(features_shown.join(' ')).toBe(
  2183. 'Password protected - This groupchat requires a password before entry '+
  2184. 'Open - Anyone can join this groupchat '+
  2185. 'Temporary - This groupchat will disappear once the last person leaves '+
  2186. 'Not anonymous - All other groupchat participants can see your XMPP address '+
  2187. 'Not moderated - Participants entering this groupchat can write right away');
  2188. expect(view.model.features.get('hidden')).toBe(false);
  2189. expect(view.model.features.get('mam_enabled')).toBe(false);
  2190. expect(view.model.features.get('membersonly')).toBe(false);
  2191. expect(view.model.features.get('moderated')).toBe(false);
  2192. expect(view.model.features.get('nonanonymous')).toBe(true);
  2193. expect(view.model.features.get('open')).toBe(true);
  2194. expect(view.model.features.get('passwordprotected')).toBe(true);
  2195. expect(view.model.features.get('persistent')).toBe(false);
  2196. expect(view.model.features.get('publicroom')).toBe(true);
  2197. expect(view.model.features.get('semianonymous')).toBe(false);
  2198. expect(view.model.features.get('temporary')).toBe(true);
  2199. expect(view.model.features.get('unmoderated')).toBe(true);
  2200. expect(view.model.features.get('unsecured')).toBe(false);
  2201. expect(view.querySelector('.chatbox-title__text').textContent.trim()).toBe('Room');
  2202. view.querySelector('.configure-chatroom-button').click();
  2203. const IQs = _converse.connection.IQ_stanzas;
  2204. let iq = await u.waitUntil(() => _.filter(
  2205. IQs,
  2206. iq => iq.querySelector(
  2207. `iq[to="${jid}"] query[xmlns="${Strophe.NS.MUC_OWNER}"]`
  2208. )).pop());
  2209. const response_el = u.toStanza(
  2210. `<iq xmlns="jabber:client"
  2211. type="result"
  2212. to="romeo@montague.lit/pda"
  2213. from="room@conference.example.org" id="${iq.getAttribute('id')}">
  2214. <query xmlns="http://jabber.org/protocol/muc#owner">
  2215. <x xmlns="jabber:x:data" type="form">
  2216. <title>Configuration for room@conference.example.org</title>
  2217. <instructions>Complete and submit this form to configure the room.</instructions>
  2218. <field var="FORM_TYPE" type="hidden">
  2219. <value>http://jabber.org/protocol/muc#roomconfig</value>
  2220. </field>
  2221. <field type="fixed">
  2222. <value>Room information</value>
  2223. </field>
  2224. <field var="muc#roomconfig_roomname" type="text-single" label="Title">
  2225. <value>Room</value>
  2226. </field>
  2227. <field var="muc#roomconfig_roomdesc" type="text-single" label="Description">
  2228. <desc>A brief description of the room</desc>
  2229. <value>This room is used in tests</value>
  2230. </field>
  2231. <field var="muc#roomconfig_lang" type="text-single" label="Language tag for room (e.g. 'en', 'de', 'fr' etc.)">
  2232. <desc>Indicate the primary language spoken in this room</desc>
  2233. <value>en</value>
  2234. </field>
  2235. <field var="muc#roomconfig_persistentroom" type="boolean" label="Persistent (room should remain even when it is empty)">
  2236. <desc>Rooms are automatically deleted when they are empty, unless this option is enabled</desc>
  2237. <value>1</value>
  2238. </field>
  2239. <field var="muc#roomconfig_publicroom" type="boolean" label="Include room information in public lists">
  2240. <desc>Enable this to allow people to find the room</desc>
  2241. <value>1</value>
  2242. </field>
  2243. <field type="fixed"><value>Access to the room</value></field>
  2244. <field var="muc#roomconfig_roomsecret" type="text-private" label="Password"><value/></field>
  2245. <field var="muc#roomconfig_membersonly" type="boolean" label="Only allow members to join">
  2246. <desc>Enable this to only allow access for room owners, admins and members</desc>
  2247. </field>
  2248. <field var="{http://prosody.im/protocol/muc}roomconfig_allowmemberinvites" type="boolean" label="Allow members to invite new members"/>
  2249. <field type="fixed"><value>Permissions in the room</value>
  2250. </field>
  2251. <field var="muc#roomconfig_changesubject" type="boolean" label="Allow anyone to set the room's subject">
  2252. <desc>Choose whether anyone, or only moderators, may set the room's subject</desc>
  2253. </field>
  2254. <field var="muc#roomconfig_moderatedroom" type="boolean" label="Moderated (require permission to speak)">
  2255. <desc>In moderated rooms occupants must be given permission to speak by a room moderator</desc>
  2256. </field>
  2257. <field var="muc#roomconfig_whois" type="list-single" label="Addresses (JIDs) of room occupants may be viewed by:">
  2258. <option label="Moderators only"><value>moderators</value></option>
  2259. <option label="Anyone"><value>anyone</value></option>
  2260. <value>anyone</value>
  2261. </field>
  2262. <field type="fixed"><value>Other options</value></field>
  2263. <field var="muc#roomconfig_historylength" type="text-single" label="Maximum number of history messages returned by room">
  2264. <desc>Specify the maximum number of previous messages that should be sent to users when they join the room</desc>
  2265. <value>50</value>
  2266. </field>
  2267. <field var="muc#roomconfig_defaulthistorymessages" type="text-single" label="Default number of history messages returned by room">
  2268. <desc>Specify the number of previous messages sent to new users when they join the room</desc>
  2269. <value>20</value>
  2270. </field>
  2271. </x>
  2272. </query>
  2273. </iq>`);
  2274. _converse.connection._dataRecv(mock.createRequest(response_el));
  2275. const el = await u.waitUntil(() => document.querySelector('.chatroom-form legend'));
  2276. expect(el.textContent.trim()).toBe("Configuration for room@conference.example.org");
  2277. sizzle('[name="muc#roomconfig_membersonly"]', view).pop().click();
  2278. sizzle('[name="muc#roomconfig_roomname"]', view).pop().value = "New room name"
  2279. view.querySelector('.chatroom-form input[type="submit"]').click();
  2280. iq = await u.waitUntil(() => _.filter(IQs, iq => u.matchesSelector(iq, `iq[to="${jid}"][type="set"]`)).pop());
  2281. const result = $iq({
  2282. "xmlns": "jabber:client",
  2283. "type": "result",
  2284. "to": "romeo@montague.lit/orchard",
  2285. "from": "lounge@muc.montague.lit",
  2286. "id": iq.getAttribute('id')
  2287. });
  2288. IQs.length = 0; // Empty the array
  2289. _converse.connection._dataRecv(mock.createRequest(result));
  2290. iq = await u.waitUntil(() => _.filter(
  2291. IQs,
  2292. iq => iq.querySelector(
  2293. `iq[to="${jid}"] query[xmlns="http://jabber.org/protocol/disco#info"]`
  2294. )).pop());
  2295. const features_stanza = $iq({
  2296. 'from': jid,
  2297. 'id': iq.getAttribute('id'),
  2298. 'to': 'romeo@montague.lit/desktop',
  2299. 'type': 'result'
  2300. }).c('query', { 'xmlns': 'http://jabber.org/protocol/disco#info'})
  2301. .c('identity', {
  2302. 'category': 'conference',
  2303. 'name': 'New room name',
  2304. 'type': 'text'
  2305. }).up();
  2306. features = [
  2307. 'http://jabber.org/protocol/muc',
  2308. 'jabber:iq:register',
  2309. 'muc_passwordprotected',
  2310. 'muc_hidden',
  2311. 'muc_temporary',
  2312. 'muc_membersonly',
  2313. 'muc_unmoderated',
  2314. 'muc_nonanonymous'
  2315. ];
  2316. features.forEach(f => features_stanza.c('feature', {'var': f}).up());
  2317. features_stanza.c('x', { 'xmlns':'jabber:x:data', 'type':'result'})
  2318. .c('field', {'var':'FORM_TYPE', 'type':'hidden'})
  2319. .c('value').t('http://jabber.org/protocol/muc#roominfo').up().up()
  2320. .c('field', {'type':'text-single', 'var':'muc#roominfo_description', 'label':'Description'})
  2321. .c('value').t('This is the description').up().up()
  2322. .c('field', {'type':'text-single', 'var':'muc#roominfo_occupants', 'label':'Number of occupants'})
  2323. .c('value').t(0);
  2324. _converse.connection._dataRecv(mock.createRequest(features_stanza));
  2325. await u.waitUntil(() => new Promise(success => view.model.features.on('change', success)));
  2326. features_list = modal.el.querySelector('.features-list');
  2327. features_shown = features_list.textContent.split('\n').map(s => s.trim()).filter(s => s);
  2328. expect(features_shown.join(' ')).toBe(
  2329. 'Password protected - This groupchat requires a password before entry '+
  2330. 'Hidden - This groupchat is not publicly searchable '+
  2331. 'Members only - This groupchat is restricted to members only '+
  2332. 'Temporary - This groupchat will disappear once the last person leaves '+
  2333. 'Not anonymous - All other groupchat participants can see your XMPP address '+
  2334. 'Not moderated - Participants entering this groupchat can write right away');
  2335. expect(view.model.features.get('hidden')).toBe(true);
  2336. expect(view.model.features.get('mam_enabled')).toBe(false);
  2337. expect(view.model.features.get('membersonly')).toBe(true);
  2338. expect(view.model.features.get('moderated')).toBe(false);
  2339. expect(view.model.features.get('nonanonymous')).toBe(true);
  2340. expect(view.model.features.get('open')).toBe(false);
  2341. expect(view.model.features.get('passwordprotected')).toBe(true);
  2342. expect(view.model.features.get('persistent')).toBe(false);
  2343. expect(view.model.features.get('publicroom')).toBe(false);
  2344. expect(view.model.features.get('semianonymous')).toBe(false);
  2345. expect(view.model.features.get('temporary')).toBe(true);
  2346. expect(view.model.features.get('unmoderated')).toBe(true);
  2347. expect(view.model.features.get('unsecured')).toBe(false);
  2348. await u.waitUntil(() => view.querySelector('.chatbox-title__text')?.textContent.trim() === 'New room name');
  2349. done();
  2350. }));
  2351. it("indicates when a room is no longer anonymous",
  2352. mock.initConverse([], {}, async function (done, _converse) {
  2353. let IQ_id;
  2354. const sendIQ = _converse.connection.sendIQ;
  2355. await mock.openAndEnterChatRoom(_converse, 'coven@chat.shakespeare.lit', 'some1');
  2356. spyOn(_converse.connection, 'sendIQ').and.callFake(function (iq, callback, errback) {
  2357. IQ_id = sendIQ.bind(this)(iq, callback, errback);
  2358. });
  2359. // We pretend this is a new room, so no disco info is returned.
  2360. const features_stanza = $iq({
  2361. from: 'coven@chat.shakespeare.lit',
  2362. 'id': IQ_id,
  2363. 'to': 'romeo@montague.lit/desktop',
  2364. 'type': 'error'
  2365. }).c('error', {'type': 'cancel'})
  2366. .c('item-not-found', {'xmlns': "urn:ietf:params:xml:ns:xmpp-stanzas"});
  2367. _converse.connection._dataRecv(mock.createRequest(features_stanza));
  2368. const view = _converse.chatboxviews.get('coven@chat.shakespeare.lit');
  2369. /* <message xmlns="jabber:client"
  2370. * type="groupchat"
  2371. * to="romeo@montague.lit/_converse.js-27854181"
  2372. * from="coven@chat.shakespeare.lit">
  2373. * <x xmlns="http://jabber.org/protocol/muc#user">
  2374. * <status code="104"/>
  2375. * <status code="172"/>
  2376. * </x>
  2377. * </message>
  2378. */
  2379. const message = $msg({
  2380. type:'groupchat',
  2381. to: 'romeo@montague.lit/_converse.js-27854181',
  2382. from: 'coven@chat.shakespeare.lit'
  2383. }).c('x', {xmlns: Strophe.NS.MUC_USER})
  2384. .c('status', {code: '104'}).up()
  2385. .c('status', {code: '172'});
  2386. _converse.connection._dataRecv(mock.createRequest(message));
  2387. await u.waitUntil(() => view.querySelectorAll('.chat-content .chat-info').length);
  2388. const chat_body = view.querySelector('.chatroom-body');
  2389. expect(sizzle('.message:last', chat_body).pop().textContent.trim())
  2390. .toBe('This groupchat is now no longer anonymous');
  2391. done();
  2392. }));
  2393. it("informs users if they have been kicked out of the groupchat",
  2394. mock.initConverse([], {}, async function (done, _converse) {
  2395. /* <presence
  2396. * from='harfleur@chat.shakespeare.lit/pistol'
  2397. * to='pistol@shakespeare.lit/harfleur'
  2398. * type='unavailable'>
  2399. * <x xmlns='http://jabber.org/protocol/muc#user'>
  2400. * <item affiliation='none' role='none'>
  2401. * <actor nick='Fluellen'/>
  2402. * <reason>Avaunt, you cullion!</reason>
  2403. * </item>
  2404. * <status code='110'/>
  2405. * <status code='307'/>
  2406. * </x>
  2407. * </presence>
  2408. */
  2409. await mock.openAndEnterChatRoom(_converse, 'lounge@montague.lit', 'romeo');
  2410. const view = _converse.chatboxviews.get('lounge@montague.lit');
  2411. expect(view.model.session.get('connection_status')).toBe(converse.ROOMSTATUS.ENTERED);
  2412. const presence = $pres().attrs({
  2413. from:'lounge@montague.lit/romeo',
  2414. to:'romeo@montague.lit/pda',
  2415. type:'unavailable'
  2416. })
  2417. .c('x').attrs({xmlns:'http://jabber.org/protocol/muc#user'})
  2418. .c('item').attrs({
  2419. affiliation: 'none',
  2420. jid: 'romeo@montague.lit/pda',
  2421. role: 'none'
  2422. })
  2423. .c('actor').attrs({nick: 'Fluellen'}).up()
  2424. .c('reason').t('Avaunt, you cullion!').up()
  2425. .up()
  2426. .c('status').attrs({code:'110'}).up()
  2427. .c('status').attrs({code:'307'}).nodeTree;
  2428. _converse.connection._dataRecv(mock.createRequest(presence));
  2429. await u.waitUntil(() => !u.isVisible(view.querySelector('.chat-area')));
  2430. expect(u.isVisible(view.querySelector('.occupants'))).toBeFalsy();
  2431. const chat_body = view.querySelector('.chatroom-body');
  2432. expect(chat_body.querySelectorAll('.disconnect-msg').length).toBe(3);
  2433. expect(chat_body.querySelector('.disconnect-msg:first-child').textContent.trim()).toBe(
  2434. 'You have been kicked from this groupchat');
  2435. expect(chat_body.querySelector('.disconnect-msg:nth-child(2)').textContent.trim()).toBe(
  2436. 'This action was done by Fluellen.');
  2437. expect(chat_body.querySelector('.disconnect-msg:nth-child(3)').textContent.trim()).toBe(
  2438. 'The reason given is: "Avaunt, you cullion!".');
  2439. expect(view.model.session.get('connection_status')).toBe(converse.ROOMSTATUS.DISCONNECTED);
  2440. done();
  2441. }));
  2442. it("informs users if they have exited the groupchat due to a technical reason",
  2443. mock.initConverse([], {}, async function (done, _converse) {
  2444. /* <presence
  2445. * from='harfleur@chat.shakespeare.lit/pistol'
  2446. * to='pistol@shakespeare.lit/harfleur'
  2447. * type='unavailable'>
  2448. * <x xmlns='http://jabber.org/protocol/muc#user'>
  2449. * <item affiliation='none' role='none'>
  2450. * <actor nick='Fluellen'/>
  2451. * <reason>Avaunt, you cullion!</reason>
  2452. * </item>
  2453. * <status code='110'/>
  2454. * <status code='307'/>
  2455. * </x>
  2456. * </presence>
  2457. */
  2458. await mock.openAndEnterChatRoom(_converse, 'lounge@montague.lit', 'romeo');
  2459. var presence = $pres().attrs({
  2460. from:'lounge@montague.lit/romeo',
  2461. to:'romeo@montague.lit/pda',
  2462. type:'unavailable'
  2463. })
  2464. .c('x').attrs({xmlns:'http://jabber.org/protocol/muc#user'})
  2465. .c('item').attrs({
  2466. affiliation: 'none',
  2467. jid: 'romeo@montague.lit/pda',
  2468. role: 'none'
  2469. })
  2470. .c('reason').t('Flux capacitor overload!').up()
  2471. .up()
  2472. .c('status').attrs({code:'110'}).up()
  2473. .c('status').attrs({code:'333'}).up()
  2474. .c('status').attrs({code:'307'}).nodeTree;
  2475. _converse.connection._dataRecv(mock.createRequest(presence));
  2476. const view = _converse.chatboxviews.get('lounge@montague.lit');
  2477. await u.waitUntil(() => !u.isVisible(view.querySelector('.chat-area')));
  2478. expect(u.isVisible(view.querySelector('.occupants'))).toBeFalsy();
  2479. const chat_body = view.querySelector('.chatroom-body');
  2480. expect(chat_body.querySelectorAll('.disconnect-msg').length).toBe(2);
  2481. expect(chat_body.querySelector('.disconnect-msg:first-child').textContent.trim()).toBe(
  2482. 'You have exited this groupchat due to a technical problem');
  2483. expect(chat_body.querySelector('.disconnect-msg:nth-child(2)').textContent.trim()).toBe(
  2484. 'The reason given is: "Flux capacitor overload!".');
  2485. done();
  2486. }));
  2487. it("can be saved to, and retrieved from, browserStorage",
  2488. mock.initConverse(['chatBoxesFetched'], {}, async function (done, _converse) {
  2489. await mock.openChatRoom(_converse, 'lounge', 'montague.lit', 'romeo');
  2490. // We instantiate a new ChatBoxes collection, which by default
  2491. // will be empty.
  2492. await mock.openControlBox(_converse);
  2493. const newchatboxes = new _converse.ChatBoxes();
  2494. expect(newchatboxes.length).toEqual(0);
  2495. // The chatboxes will then be fetched from browserStorage inside the
  2496. // onConnected method
  2497. newchatboxes.onConnected();
  2498. await new Promise(resolve => _converse.api.listen.once('chatBoxesFetched', resolve));
  2499. expect(newchatboxes.length).toEqual(2);
  2500. // Check that the chatrooms retrieved from browserStorage
  2501. // have the same attributes values as the original ones.
  2502. const attrs = ['id', 'box_id', 'visible'];
  2503. let new_attrs, old_attrs;
  2504. for (var i=0; i<attrs.length; i++) {
  2505. new_attrs = _.map(_.map(newchatboxes.models, 'attributes'), attrs[i]);
  2506. old_attrs = _.map(_.map(_converse.chatboxes.models, 'attributes'), attrs[i]);
  2507. // FIXME: should have have to sort here? Order must
  2508. // probably be the same...
  2509. // This should be fixed once the controlbox always opens
  2510. // only on the right.
  2511. expect(_.isEqual(new_attrs.sort(), old_attrs.sort())).toEqual(true);
  2512. }
  2513. done();
  2514. }));
  2515. it("can be closed again by clicking a DOM element with class 'close-chatbox-button'",
  2516. mock.initConverse(['chatBoxesFetched'], {}, async function (done, _converse) {
  2517. await mock.openChatRoom(_converse, 'lounge', 'montague.lit', 'romeo');
  2518. const view = _converse.chatboxviews.get('lounge@montague.lit');
  2519. spyOn(view, 'close').and.callThrough();
  2520. spyOn(_converse.api, "trigger").and.callThrough();
  2521. spyOn(view.model, 'leave');
  2522. view.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  2523. spyOn(_converse.api, 'confirm').and.callFake(() => Promise.resolve(true));
  2524. const button = await u.waitUntil(() => view.querySelector('.close-chatbox-button'));
  2525. button.click();
  2526. await u.waitUntil(() => view.close.calls.count());
  2527. expect(view.model.leave).toHaveBeenCalled();
  2528. await u.waitUntil(() => _converse.api.trigger.calls.count());
  2529. expect(_converse.api.trigger).toHaveBeenCalledWith('chatBoxClosed', jasmine.any(Object));
  2530. done();
  2531. }));
  2532. it("informs users of role and affiliation changes",
  2533. mock.initConverse([], {}, async function (done, _converse) {
  2534. const muc_jid = 'lounge@montague.lit';
  2535. await mock.openAndEnterChatRoom(_converse, muc_jid, 'romeo');
  2536. const view = _converse.api.chatviews.get(muc_jid);
  2537. let presence = $pres({
  2538. 'from': 'lounge@montague.lit/annoyingGuy',
  2539. 'id':'27C55F89-1C6A-459A-9EB5-77690145D624',
  2540. 'to': 'romeo@montague.lit/desktop'
  2541. })
  2542. .c('x', { 'xmlns': 'http://jabber.org/protocol/muc#user'})
  2543. .c('item', {
  2544. 'jid': 'annoyingguy@montague.lit',
  2545. 'affiliation': 'member',
  2546. 'role': 'participant'
  2547. });
  2548. _converse.connection._dataRecv(mock.createRequest(presence));
  2549. const csntext = await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent);
  2550. expect(csntext.trim()).toEqual("romeo and annoyingGuy have entered the groupchat");
  2551. presence = $pres({
  2552. 'from': 'lounge@montague.lit/annoyingGuy',
  2553. 'to': 'romeo@montague.lit/desktop'
  2554. })
  2555. .c('x', { 'xmlns': 'http://jabber.org/protocol/muc#user'})
  2556. .c('item', {
  2557. 'jid': 'annoyingguy@montague.lit',
  2558. 'affiliation': 'member',
  2559. 'role': 'visitor'
  2560. });
  2561. _converse.connection._dataRecv(mock.createRequest(presence));
  2562. await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent.trim() ===
  2563. "romeo has entered the groupchat\nannoyingGuy has been muted");
  2564. presence = $pres({
  2565. 'from': 'lounge@montague.lit/annoyingGuy',
  2566. 'to': 'romeo@montague.lit/desktop'
  2567. })
  2568. .c('x', { 'xmlns': 'http://jabber.org/protocol/muc#user'})
  2569. .c('item', {
  2570. 'jid': 'annoyingguy@montague.lit',
  2571. 'affiliation': 'member',
  2572. 'role': 'participant'
  2573. });
  2574. _converse.connection._dataRecv(mock.createRequest(presence));
  2575. await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent.trim() ===
  2576. "romeo has entered the groupchat\nannoyingGuy has been given a voice");
  2577. // Check that we don't see an info message concerning the role,
  2578. // if the affiliation has changed.
  2579. presence = $pres({
  2580. 'from': 'lounge@montague.lit/annoyingGuy',
  2581. 'to': 'romeo@montague.lit/desktop'
  2582. })
  2583. .c('x', { 'xmlns': 'http://jabber.org/protocol/muc#user'})
  2584. .c('item', {
  2585. 'jid': 'annoyingguy@montague.lit',
  2586. 'affiliation': 'none',
  2587. 'role': 'visitor'
  2588. });
  2589. _converse.connection._dataRecv(mock.createRequest(presence));
  2590. await u.waitUntil(() =>
  2591. Array.from(view.querySelectorAll('.chat-info__message')).pop()?.textContent.trim() ===
  2592. "annoyingGuy is no longer a member of this groupchat"
  2593. );
  2594. done();
  2595. }));
  2596. it("notifies users of role and affiliation changes for members not currently in the groupchat",
  2597. mock.initConverse([], {}, async function (done, _converse) {
  2598. const muc_jid = 'lounge@montague.lit';
  2599. await mock.openAndEnterChatRoom(_converse, muc_jid, 'romeo');
  2600. const view = _converse.api.chatviews.get(muc_jid);
  2601. let message = $msg({
  2602. from: 'lounge@montague.lit',
  2603. id: '2CF9013B-E8A8-42A1-9633-85AD7CA12F40',
  2604. to: 'romeo@montague.lit'
  2605. })
  2606. .c('x', { 'xmlns': 'http://jabber.org/protocol/muc#user'})
  2607. .c('item', {
  2608. 'jid': 'absentguy@montague.lit',
  2609. 'affiliation': 'member',
  2610. 'role': 'none'
  2611. });
  2612. _converse.connection._dataRecv(mock.createRequest(message));
  2613. await u.waitUntil(() => view.model.occupants.length > 1);
  2614. expect(view.model.occupants.length).toBe(2);
  2615. expect(view.model.occupants.findWhere({'jid': 'absentguy@montague.lit'}).get('affiliation')).toBe('member');
  2616. message = $msg({
  2617. from: 'lounge@montague.lit',
  2618. id: '2CF9013B-E8A8-42A1-9633-85AD7CA12F41',
  2619. to: 'romeo@montague.lit'
  2620. })
  2621. .c('x', { 'xmlns': 'http://jabber.org/protocol/muc#user'})
  2622. .c('item', {
  2623. 'jid': 'absentguy@montague.lit',
  2624. 'affiliation': 'none',
  2625. 'role': 'none'
  2626. });
  2627. _converse.connection._dataRecv(mock.createRequest(message));
  2628. expect(view.model.occupants.length).toBe(2);
  2629. expect(view.model.occupants.findWhere({'jid': 'absentguy@montague.lit'}).get('affiliation')).toBe('none');
  2630. done();
  2631. }));
  2632. });
  2633. describe("Each chat groupchat can take special commands", function () {
  2634. it("takes /help to show the available commands",
  2635. mock.initConverse([], {}, async function (done, _converse) {
  2636. spyOn(window, 'confirm').and.callFake(() => true);
  2637. await mock.openAndEnterChatRoom(_converse, 'lounge@montague.lit', 'romeo');
  2638. const view = _converse.chatboxviews.get('lounge@montague.lit');
  2639. let textarea = view.querySelector('.chat-textarea');
  2640. const enter = { 'target': textarea, 'preventDefault': function preventDefault () {}, 'keyCode': 13 };
  2641. textarea.value = '/help';
  2642. view.onKeyDown(enter);
  2643. await u.waitUntil(() => sizzle('converse-chat-help .chat-info', view).length);
  2644. const chat_help_el = view.querySelector('converse-chat-help');
  2645. let info_messages = sizzle('.chat-info', chat_help_el);
  2646. expect(info_messages.length).toBe(19);
  2647. expect(info_messages.pop().textContent.trim()).toBe('/voice: Allow muted user to post messages');
  2648. expect(info_messages.pop().textContent.trim()).toBe('/topic: Set groupchat subject (alias for /subject)');
  2649. expect(info_messages.pop().textContent.trim()).toBe('/subject: Set groupchat subject');
  2650. expect(info_messages.pop().textContent.trim()).toBe('/revoke: Revoke the user\'s current affiliation');
  2651. expect(info_messages.pop().textContent.trim()).toBe('/register: Register your nickname');
  2652. expect(info_messages.pop().textContent.trim()).toBe('/owner: Grant ownership of this groupchat');
  2653. expect(info_messages.pop().textContent.trim()).toBe('/op: Grant moderator role to user');
  2654. expect(info_messages.pop().textContent.trim()).toBe('/nick: Change your nickname');
  2655. expect(info_messages.pop().textContent.trim()).toBe('/mute: Remove user\'s ability to post messages');
  2656. expect(info_messages.pop().textContent.trim()).toBe('/modtools: Opens up the moderator tools GUI');
  2657. expect(info_messages.pop().textContent.trim()).toBe('/member: Grant membership to a user');
  2658. expect(info_messages.pop().textContent.trim()).toBe('/me: Write in 3rd person');
  2659. expect(info_messages.pop().textContent.trim()).toBe('/kick: Kick user from groupchat');
  2660. expect(info_messages.pop().textContent.trim()).toBe('/help: Show this menu');
  2661. expect(info_messages.pop().textContent.trim()).toBe('/destroy: Remove this groupchat');
  2662. expect(info_messages.pop().textContent.trim()).toBe('/deop: Change user role to participant');
  2663. expect(info_messages.pop().textContent.trim()).toBe('/clear: Clear the chat area');
  2664. expect(info_messages.pop().textContent.trim()).toBe('/ban: Ban user by changing their affiliation to outcast');
  2665. expect(info_messages.pop().textContent.trim()).toBe('/admin: Change user\'s affiliation to admin');
  2666. const occupant = view.model.occupants.findWhere({'jid': _converse.bare_jid});
  2667. occupant.set('affiliation', 'admin');
  2668. view.querySelector('.close-chat-help').click();
  2669. await u.waitUntil(() => chat_help_el.hidden);
  2670. textarea.value = '/help';
  2671. view.onKeyDown(enter);
  2672. await u.waitUntil(() => !chat_help_el.hidden);
  2673. info_messages = sizzle('.chat-info', chat_help_el);
  2674. expect(info_messages.length).toBe(18);
  2675. let commands = info_messages.map(m => m.textContent.replace(/:.*$/, ''));
  2676. expect(commands).toEqual([
  2677. "/admin", "/ban", "/clear", "/deop", "/destroy",
  2678. "/help", "/kick", "/me", "/member", "/modtools", "/mute", "/nick",
  2679. "/op", "/register", "/revoke", "/subject", "/topic", "/voice"
  2680. ]);
  2681. occupant.set('affiliation', 'member');
  2682. view.querySelector('.close-chat-help').click();
  2683. await u.waitUntil(() => chat_help_el.hidden);
  2684. textarea.value = '/help';
  2685. view.onKeyDown(enter);
  2686. await u.waitUntil(() => !chat_help_el.hidden);
  2687. info_messages = sizzle('.chat-info', chat_help_el);
  2688. expect(info_messages.length).toBe(9);
  2689. commands = info_messages.map(m => m.textContent.replace(/:.*$/, ''));
  2690. expect(commands).toEqual(["/clear", "/help", "/kick", "/me", "/modtools", "/mute", "/nick", "/register", "/voice"]);
  2691. view.querySelector('.close-chat-help').click();
  2692. await u.waitUntil(() => chat_help_el.hidden);
  2693. expect(view.model.get('show_help_messages')).toBe(false);
  2694. occupant.set('role', 'participant');
  2695. // Role changes causes rerender, so we need to get the new textarea
  2696. textarea = view.querySelector('.chat-textarea');
  2697. textarea.value = '/help';
  2698. view.onKeyDown(enter);
  2699. await u.waitUntil(() => view.model.get('show_help_messages'));
  2700. await u.waitUntil(() => !chat_help_el.hidden);
  2701. info_messages = sizzle('.chat-info', chat_help_el);
  2702. expect(info_messages.length).toBe(5);
  2703. commands = info_messages.map(m => m.textContent.replace(/:.*$/, ''));
  2704. expect(commands).toEqual(["/clear", "/help", "/me", "/nick", "/register"]);
  2705. // Test that /topic is available if all users may change the subject
  2706. // Note: we're making a shortcut here, this value should never be set manually
  2707. view.model.config.set('changesubject', true);
  2708. view.querySelector('.close-chat-help').click();
  2709. await u.waitUntil(() => chat_help_el.hidden);
  2710. textarea.value = '/help';
  2711. view.onKeyDown(enter);
  2712. await u.waitUntil(() => !chat_help_el.hidden, 1000);
  2713. info_messages = sizzle('.chat-info', chat_help_el);
  2714. expect(info_messages.length).toBe(7);
  2715. commands = info_messages.map(m => m.textContent.replace(/:.*$/, ''));
  2716. expect(commands).toEqual(["/clear", "/help", "/me", "/nick", "/register", "/subject", "/topic"]);
  2717. done();
  2718. }));
  2719. it("takes /help to show the available commands and commands can be disabled by config",
  2720. mock.initConverse([], {muc_disable_slash_commands: ['mute', 'voice']}, async function (done, _converse) {
  2721. await mock.openAndEnterChatRoom(_converse, 'lounge@montague.lit', 'romeo');
  2722. const view = _converse.chatboxviews.get('lounge@montague.lit');
  2723. var textarea = view.querySelector('.chat-textarea');
  2724. const enter = { 'target': textarea, 'preventDefault': function () {}, 'keyCode': 13 };
  2725. spyOn(window, 'confirm').and.callFake(() => true);
  2726. textarea.value = '/clear';
  2727. view.onKeyDown(enter);
  2728. textarea.value = '/help';
  2729. view.onKeyDown(enter);
  2730. await u.waitUntil(() => sizzle('.chat-info:not(.chat-event)', view).length);
  2731. const info_messages = sizzle('.chat-info:not(.chat-event)', view);
  2732. expect(info_messages.length).toBe(17);
  2733. expect(info_messages.pop().textContent.trim()).toBe('/topic: Set groupchat subject (alias for /subject)');
  2734. expect(info_messages.pop().textContent.trim()).toBe('/subject: Set groupchat subject');
  2735. expect(info_messages.pop().textContent.trim()).toBe('/revoke: Revoke the user\'s current affiliation');
  2736. expect(info_messages.pop().textContent.trim()).toBe('/register: Register your nickname');
  2737. expect(info_messages.pop().textContent.trim()).toBe('/owner: Grant ownership of this groupchat');
  2738. expect(info_messages.pop().textContent.trim()).toBe('/op: Grant moderator role to user');
  2739. expect(info_messages.pop().textContent.trim()).toBe('/nick: Change your nickname');
  2740. expect(info_messages.pop().textContent.trim()).toBe('/modtools: Opens up the moderator tools GUI');
  2741. expect(info_messages.pop().textContent.trim()).toBe('/member: Grant membership to a user');
  2742. expect(info_messages.pop().textContent.trim()).toBe('/me: Write in 3rd person');
  2743. expect(info_messages.pop().textContent.trim()).toBe('/kick: Kick user from groupchat');
  2744. expect(info_messages.pop().textContent.trim()).toBe('/help: Show this menu');
  2745. expect(info_messages.pop().textContent.trim()).toBe('/destroy: Remove this groupchat');
  2746. expect(info_messages.pop().textContent.trim()).toBe('/deop: Change user role to participant');
  2747. expect(info_messages.pop().textContent.trim()).toBe('/clear: Clear the chat area');
  2748. expect(info_messages.pop().textContent.trim()).toBe('/ban: Ban user by changing their affiliation to outcast');
  2749. expect(info_messages.pop().textContent.trim()).toBe('/admin: Change user\'s affiliation to admin');
  2750. done();
  2751. }));
  2752. it("takes /member to make an occupant a member",
  2753. mock.initConverse([], {}, async function (done, _converse) {
  2754. let iq_stanza;
  2755. await mock.openAndEnterChatRoom(_converse, 'lounge@muc.montague.lit', 'romeo');
  2756. const view = _converse.chatboxviews.get('lounge@muc.montague.lit');
  2757. /* We don't show join/leave messages for existing occupants. We
  2758. * know about them because we receive their presences before we
  2759. * receive our own.
  2760. */
  2761. const presence = $pres({
  2762. to: 'romeo@montague.lit/orchard',
  2763. from: 'lounge@muc.montague.lit/marc'
  2764. }).c('x', {xmlns: Strophe.NS.MUC_USER})
  2765. .c('item', {
  2766. 'affiliation': 'none',
  2767. 'jid': 'marc@montague.lit/_converse.js-290929789',
  2768. 'role': 'participant'
  2769. });
  2770. _converse.connection._dataRecv(mock.createRequest(presence));
  2771. expect(view.model.occupants.length).toBe(2);
  2772. const textarea = view.querySelector('.chat-textarea');
  2773. let sent_stanza;
  2774. spyOn(_converse.connection, 'send').and.callFake((stanza) => {
  2775. sent_stanza = stanza;
  2776. });
  2777. // First check that an error message appears when a
  2778. // non-existent nick is used.
  2779. textarea.value = '/member chris Welcome to the club!';
  2780. view.onKeyDown({
  2781. target: textarea,
  2782. preventDefault: function preventDefault () {},
  2783. keyCode: 13
  2784. });
  2785. expect(_converse.connection.send).not.toHaveBeenCalled();
  2786. await u.waitUntil(() => view.querySelectorAll('.chat-error').length);
  2787. expect(view.querySelector('.chat-error').textContent.trim())
  2788. .toBe('Error: couldn\'t find a groupchat participant based on your arguments');
  2789. // Now test with an existing nick
  2790. textarea.value = '/member marc Welcome to the club!';
  2791. view.onKeyDown({
  2792. target: textarea,
  2793. preventDefault: function preventDefault () {},
  2794. keyCode: 13
  2795. });
  2796. expect(_converse.connection.send).toHaveBeenCalled();
  2797. expect(Strophe.serialize(sent_stanza)).toBe(
  2798. `<iq id="${sent_stanza.getAttribute('id')}" to="lounge@muc.montague.lit" type="set" xmlns="jabber:client">`+
  2799. `<query xmlns="http://jabber.org/protocol/muc#admin">`+
  2800. `<item affiliation="member" jid="marc@montague.lit">`+
  2801. `<reason>Welcome to the club!</reason>`+
  2802. `</item>`+
  2803. `</query>`+
  2804. `</iq>`);
  2805. let result = $iq({
  2806. "xmlns": "jabber:client",
  2807. "type": "result",
  2808. "to": "romeo@montague.lit/orchard",
  2809. "from": "lounge@muc.montague.lit",
  2810. "id": sent_stanza.getAttribute('id')
  2811. });
  2812. _converse.connection.IQ_stanzas = [];
  2813. _converse.connection._dataRecv(mock.createRequest(result));
  2814. iq_stanza = await u.waitUntil(() => _.filter(
  2815. _converse.connection.IQ_stanzas,
  2816. iq => iq.querySelector('iq[to="lounge@muc.montague.lit"][type="get"] item[affiliation="member"]')).pop()
  2817. );
  2818. expect(Strophe.serialize(iq_stanza)).toBe(
  2819. `<iq id="${iq_stanza.getAttribute('id')}" to="lounge@muc.montague.lit" type="get" xmlns="jabber:client">`+
  2820. `<query xmlns="http://jabber.org/protocol/muc#admin">`+
  2821. `<item affiliation="member"/>`+
  2822. `</query>`+
  2823. `</iq>`)
  2824. expect(view.model.occupants.length).toBe(2);
  2825. result = $iq({
  2826. "xmlns": "jabber:client",
  2827. "type": "result",
  2828. "to": "romeo@montague.lit/orchard",
  2829. "from": "lounge@muc.montague.lit",
  2830. "id": iq_stanza.getAttribute("id")
  2831. }).c("query", {"xmlns": "http://jabber.org/protocol/muc#admin"})
  2832. .c("item", {"jid": "marc", "affiliation": "member"});
  2833. _converse.connection._dataRecv(mock.createRequest(result));
  2834. expect(view.model.occupants.length).toBe(2);
  2835. iq_stanza = await u.waitUntil(() => _.filter(
  2836. _converse.connection.IQ_stanzas,
  2837. iq => iq.querySelector('iq[to="lounge@muc.montague.lit"][type="get"] item[affiliation="owner"]')).pop()
  2838. );
  2839. expect(Strophe.serialize(iq_stanza)).toBe(
  2840. `<iq id="${iq_stanza.getAttribute('id')}" to="lounge@muc.montague.lit" type="get" xmlns="jabber:client">`+
  2841. `<query xmlns="http://jabber.org/protocol/muc#admin">`+
  2842. `<item affiliation="owner"/>`+
  2843. `</query>`+
  2844. `</iq>`)
  2845. expect(view.model.occupants.length).toBe(2);
  2846. result = $iq({
  2847. "xmlns": "jabber:client",
  2848. "type": "result",
  2849. "to": "romeo@montague.lit/orchard",
  2850. "from": "lounge@muc.montague.lit",
  2851. "id": iq_stanza.getAttribute("id")
  2852. }).c("query", {"xmlns": "http://jabber.org/protocol/muc#admin"})
  2853. .c("item", {"jid": "romeo@montague.lit", "affiliation": "owner"});
  2854. _converse.connection._dataRecv(mock.createRequest(result));
  2855. expect(view.model.occupants.length).toBe(2);
  2856. iq_stanza = await u.waitUntil(() => _.filter(
  2857. _converse.connection.IQ_stanzas,
  2858. iq => iq.querySelector('iq[to="lounge@muc.montague.lit"][type="get"] item[affiliation="admin"]')).pop()
  2859. );
  2860. expect(Strophe.serialize(iq_stanza)).toBe(
  2861. `<iq id="${iq_stanza.getAttribute('id')}" to="lounge@muc.montague.lit" type="get" xmlns="jabber:client">`+
  2862. `<query xmlns="http://jabber.org/protocol/muc#admin">`+
  2863. `<item affiliation="admin"/>`+
  2864. `</query>`+
  2865. `</iq>`)
  2866. expect(view.model.occupants.length).toBe(2);
  2867. result = $iq({
  2868. "xmlns": "jabber:client",
  2869. "type": "result",
  2870. "to": "romeo@montague.lit/orchard",
  2871. "from": "lounge@muc.montague.lit",
  2872. "id": iq_stanza.getAttribute("id")
  2873. }).c("query", {"xmlns": "http://jabber.org/protocol/muc#admin"})
  2874. _converse.connection._dataRecv(mock.createRequest(result));
  2875. await u.waitUntil(() => view.querySelectorAll('.occupant').length, 500);
  2876. await u.waitUntil(() => view.querySelectorAll('.badge').length > 1);
  2877. expect(view.model.occupants.length).toBe(2);
  2878. expect(view.querySelectorAll('.occupant').length).toBe(2);
  2879. done();
  2880. }));
  2881. it("takes /topic to set the groupchat topic", mock.initConverse([], {}, async function (done, _converse) {
  2882. await mock.openAndEnterChatRoom(_converse, 'lounge@montague.lit', 'romeo');
  2883. const view = _converse.chatboxviews.get('lounge@montague.lit');
  2884. spyOn(view, 'clearMessages');
  2885. let sent_stanza;
  2886. spyOn(_converse.connection, 'send').and.callFake(function (stanza) {
  2887. sent_stanza = stanza;
  2888. });
  2889. // Check the alias /topic
  2890. const textarea = view.querySelector('.chat-textarea');
  2891. textarea.value = '/topic This is the groupchat subject';
  2892. view.onKeyDown({
  2893. target: textarea,
  2894. preventDefault: function preventDefault () {},
  2895. keyCode: 13
  2896. });
  2897. expect(_converse.connection.send).toHaveBeenCalled();
  2898. expect(sent_stanza.textContent.trim()).toBe('This is the groupchat subject');
  2899. // Check /subject
  2900. textarea.value = '/subject This is a new subject';
  2901. view.onKeyDown({
  2902. target: textarea,
  2903. preventDefault: function preventDefault () {},
  2904. keyCode: 13
  2905. });
  2906. expect(sent_stanza.textContent.trim()).toBe('This is a new subject');
  2907. expect(Strophe.serialize(sent_stanza).toLocaleString()).toBe(
  2908. '<message from="romeo@montague.lit/orchard" to="lounge@montague.lit" type="groupchat" xmlns="jabber:client">'+
  2909. '<subject xmlns="jabber:client">This is a new subject</subject>'+
  2910. '</message>');
  2911. // Check case insensitivity
  2912. textarea.value = '/Subject This is yet another subject';
  2913. view.onKeyDown({
  2914. target: textarea,
  2915. preventDefault: function preventDefault () {},
  2916. keyCode: 13
  2917. });
  2918. expect(sent_stanza.textContent.trim()).toBe('This is yet another subject');
  2919. expect(Strophe.serialize(sent_stanza).toLocaleString()).toBe(
  2920. '<message from="romeo@montague.lit/orchard" to="lounge@montague.lit" type="groupchat" xmlns="jabber:client">'+
  2921. '<subject xmlns="jabber:client">This is yet another subject</subject>'+
  2922. '</message>');
  2923. // Check unsetting the topic
  2924. textarea.value = '/topic';
  2925. view.onKeyDown({
  2926. target: textarea,
  2927. preventDefault: function preventDefault () {},
  2928. keyCode: 13
  2929. });
  2930. expect(Strophe.serialize(sent_stanza).toLocaleString()).toBe(
  2931. '<message from="romeo@montague.lit/orchard" to="lounge@montague.lit" type="groupchat" xmlns="jabber:client">'+
  2932. '<subject xmlns="jabber:client"></subject>'+
  2933. '</message>');
  2934. done();
  2935. }));
  2936. it("takes /clear to clear messages", mock.initConverse([], {}, async function (done, _converse) {
  2937. await mock.openAndEnterChatRoom(_converse, 'lounge@montague.lit', 'romeo');
  2938. const view = _converse.chatboxviews.get('lounge@montague.lit');
  2939. spyOn(view, 'clearMessages');
  2940. const textarea = view.querySelector('.chat-textarea')
  2941. textarea.value = '/clear';
  2942. view.onKeyDown({
  2943. target: textarea,
  2944. preventDefault: function preventDefault () {},
  2945. keyCode: 13
  2946. });
  2947. expect(view.clearMessages).toHaveBeenCalled();
  2948. done();
  2949. }));
  2950. it("takes /owner to make a user an owner", mock.initConverse([], {}, async function (done, _converse) {
  2951. let sent_IQ, IQ_id;
  2952. const sendIQ = _converse.connection.sendIQ;
  2953. spyOn(_converse.connection, 'sendIQ').and.callFake(function (iq, callback, errback) {
  2954. sent_IQ = iq;
  2955. IQ_id = sendIQ.bind(this)(iq, callback, errback);
  2956. });
  2957. await mock.openAndEnterChatRoom(_converse, 'lounge@montague.lit', 'romeo');
  2958. const view = _converse.chatboxviews.get('lounge@montague.lit');
  2959. spyOn(view.model, 'setAffiliation').and.callThrough();
  2960. spyOn(view, 'validateRoleOrAffiliationChangeArgs').and.callThrough();
  2961. let presence = $pres({
  2962. 'from': 'lounge@montague.lit/annoyingGuy',
  2963. 'id':'27C55F89-1C6A-459A-9EB5-77690145D624',
  2964. 'to': 'romeo@montague.lit/desktop'
  2965. })
  2966. .c('x', { 'xmlns': 'http://jabber.org/protocol/muc#user'})
  2967. .c('item', {
  2968. 'jid': 'annoyingguy@montague.lit',
  2969. 'affiliation': 'member',
  2970. 'role': 'participant'
  2971. });
  2972. _converse.connection._dataRecv(mock.createRequest(presence));
  2973. var textarea = view.querySelector('.chat-textarea')
  2974. textarea.value = '/owner';
  2975. view.onKeyDown({
  2976. target: textarea,
  2977. preventDefault: function preventDefault () {},
  2978. keyCode: 13
  2979. });
  2980. expect(view.validateRoleOrAffiliationChangeArgs).toHaveBeenCalled();
  2981. const err_msg = await u.waitUntil(() => view.querySelector('.chat-error'));
  2982. expect(err_msg.textContent.trim()).toBe(
  2983. "Error: the \"owner\" command takes two arguments, the user's nickname and optionally a reason.");
  2984. expect(view.model.setAffiliation).not.toHaveBeenCalled();
  2985. // XXX: Calling onFormSubmitted directly, trying
  2986. // again via triggering Event doesn't work for some weird
  2987. // reason.
  2988. textarea.value = '/owner nobody You\'re responsible';
  2989. view.onFormSubmitted(new Event('submit'));
  2990. await u.waitUntil(() => view.querySelectorAll('.chat-error').length === 2);
  2991. expect(Array.from(view.querySelectorAll('.chat-error')).pop().textContent.trim()).toBe(
  2992. "Error: couldn't find a groupchat participant based on your arguments");
  2993. expect(view.model.setAffiliation).not.toHaveBeenCalled();
  2994. // Call now with the correct of arguments.
  2995. // XXX: Calling onFormSubmitted directly, trying
  2996. // again via triggering Event doesn't work for some weird
  2997. // reason.
  2998. textarea.value = '/owner annoyingGuy You\'re responsible';
  2999. view.onFormSubmitted(new Event('submit'));
  3000. expect(view.validateRoleOrAffiliationChangeArgs.calls.count()).toBe(3);
  3001. expect(view.model.setAffiliation).toHaveBeenCalled();
  3002. // Check that the member list now gets updated
  3003. expect(Strophe.serialize(sent_IQ)).toBe(
  3004. `<iq id="${IQ_id}" to="lounge@montague.lit" type="set" xmlns="jabber:client">`+
  3005. `<query xmlns="http://jabber.org/protocol/muc#admin">`+
  3006. `<item affiliation="owner" jid="annoyingguy@montague.lit">`+
  3007. `<reason>You&apos;re responsible</reason>`+
  3008. `</item>`+
  3009. `</query>`+
  3010. `</iq>`);
  3011. presence = $pres({
  3012. 'from': 'lounge@montague.lit/annoyingGuy',
  3013. 'id':'27C55F89-1C6A-459A-9EB5-77690145D628',
  3014. 'to': 'romeo@montague.lit/desktop'
  3015. })
  3016. .c('x', { 'xmlns': 'http://jabber.org/protocol/muc#user'})
  3017. .c('item', {
  3018. 'jid': 'annoyingguy@montague.lit',
  3019. 'affiliation': 'owner',
  3020. 'role': 'participant'
  3021. });
  3022. _converse.connection._dataRecv(mock.createRequest(presence));
  3023. await u.waitUntil(() =>
  3024. Array.from(view.querySelectorAll('.chat-info__message')).pop()?.textContent.trim() ===
  3025. "annoyingGuy is now an owner of this groupchat"
  3026. );
  3027. done();
  3028. }));
  3029. it("takes /ban to ban a user", mock.initConverse([], {}, async function (done, _converse) {
  3030. let sent_IQ, IQ_id;
  3031. const sendIQ = _converse.connection.sendIQ;
  3032. spyOn(_converse.connection, 'sendIQ').and.callFake(function (iq, callback, errback) {
  3033. sent_IQ = iq;
  3034. IQ_id = sendIQ.bind(this)(iq, callback, errback);
  3035. });
  3036. await mock.openAndEnterChatRoom(_converse, 'lounge@montague.lit', 'romeo');
  3037. const view = _converse.chatboxviews.get('lounge@montague.lit');
  3038. spyOn(view.model, 'setAffiliation').and.callThrough();
  3039. spyOn(view, 'validateRoleOrAffiliationChangeArgs').and.callThrough();
  3040. let presence = $pres({
  3041. 'from': 'lounge@montague.lit/annoyingGuy',
  3042. 'id':'27C55F89-1C6A-459A-9EB5-77690145D624',
  3043. 'to': 'romeo@montague.lit/desktop'
  3044. })
  3045. .c('x', { 'xmlns': 'http://jabber.org/protocol/muc#user'})
  3046. .c('item', {
  3047. 'jid': 'annoyingguy@montague.lit',
  3048. 'affiliation': 'member',
  3049. 'role': 'participant'
  3050. });
  3051. _converse.connection._dataRecv(mock.createRequest(presence));
  3052. const textarea = view.querySelector('.chat-textarea')
  3053. textarea.value = '/ban';
  3054. view.onKeyDown({
  3055. target: textarea,
  3056. preventDefault: function preventDefault () {},
  3057. keyCode: 13
  3058. });
  3059. expect(view.validateRoleOrAffiliationChangeArgs).toHaveBeenCalled();
  3060. await u.waitUntil(() => view.querySelector('.message:last-child')?.textContent?.trim() ===
  3061. "Error: the \"ban\" command takes two arguments, the user's nickname and optionally a reason.");
  3062. expect(view.model.setAffiliation).not.toHaveBeenCalled();
  3063. // Call now with the correct amount of arguments.
  3064. // XXX: Calling onFormSubmitted directly, trying
  3065. // again via triggering Event doesn't work for some weird
  3066. // reason.
  3067. textarea.value = '/ban annoyingGuy You\'re annoying';
  3068. view.onFormSubmitted(new Event('submit'));
  3069. expect(view.validateRoleOrAffiliationChangeArgs.calls.count()).toBe(2);
  3070. expect(view.model.setAffiliation).toHaveBeenCalled();
  3071. // Check that the member list now gets updated
  3072. expect(Strophe.serialize(sent_IQ)).toBe(
  3073. `<iq id="${IQ_id}" to="lounge@montague.lit" type="set" xmlns="jabber:client">`+
  3074. `<query xmlns="http://jabber.org/protocol/muc#admin">`+
  3075. `<item affiliation="outcast" jid="annoyingguy@montague.lit">`+
  3076. `<reason>You&apos;re annoying</reason>`+
  3077. `</item>`+
  3078. `</query>`+
  3079. `</iq>`);
  3080. presence = $pres({
  3081. 'from': 'lounge@montague.lit/annoyingGuy',
  3082. 'id':'27C55F89-1C6A-459A-9EB5-77690145D628',
  3083. 'to': 'romeo@montague.lit/desktop'
  3084. }).c('x', { 'xmlns': 'http://jabber.org/protocol/muc#user'})
  3085. .c('item', {
  3086. 'jid': 'annoyingguy@montague.lit',
  3087. 'affiliation': 'outcast',
  3088. 'role': 'participant'
  3089. }).c('actor', {'nick': 'romeo'}).up()
  3090. .c('reason').t("You're annoying").up().up()
  3091. .c('status', {'code': '301'});
  3092. _converse.connection._dataRecv(mock.createRequest(presence));
  3093. await u.waitUntil(() => view.querySelectorAll('.chat-info').length === 2);
  3094. expect(view.querySelectorAll('.chat-info__message')[1].textContent.trim()).toBe("annoyingGuy has been banned by romeo");
  3095. expect(view.querySelector('.chat-info:last-child q').textContent.trim()).toBe("You're annoying");
  3096. presence = $pres({
  3097. 'from': 'lounge@montague.lit/joe2',
  3098. 'id':'27C55F89-1C6A-459A-9EB5-77690145D624',
  3099. 'to': 'romeo@montague.lit/desktop'
  3100. })
  3101. .c('x', { 'xmlns': 'http://jabber.org/protocol/muc#user'})
  3102. .c('item', {
  3103. 'jid': 'joe2@montague.lit',
  3104. 'affiliation': 'member',
  3105. 'role': 'participant'
  3106. });
  3107. _converse.connection._dataRecv(mock.createRequest(presence));
  3108. textarea.value = '/ban joe22';
  3109. view.onFormSubmitted(new Event('submit'));
  3110. await u.waitUntil(() => view.querySelector('converse-chat-message:last-child')?.textContent?.trim() ===
  3111. "Error: couldn't find a groupchat participant based on your arguments");
  3112. done();
  3113. }));
  3114. it("takes a /kick command to kick a user", mock.initConverse([], {}, async function (done, _converse) {
  3115. let sent_IQ, IQ_id;
  3116. const sendIQ = _converse.connection.sendIQ;
  3117. spyOn(_converse.connection, 'sendIQ').and.callFake(function (iq, callback, errback) {
  3118. sent_IQ = iq;
  3119. IQ_id = sendIQ.bind(this)(iq, callback, errback);
  3120. });
  3121. const muc_jid = 'lounge@montague.lit';
  3122. await mock.openAndEnterChatRoom(_converse, muc_jid, 'romeo');
  3123. const view = _converse.api.chatviews.get(muc_jid);
  3124. spyOn(view.model, 'setRole').and.callThrough();
  3125. spyOn(view, 'validateRoleOrAffiliationChangeArgs').and.callThrough();
  3126. let presence = $pres({
  3127. 'from': 'lounge@montague.lit/annoying guy',
  3128. 'id':'27C55F89-1C6A-459A-9EB5-77690145D624',
  3129. 'to': 'romeo@montague.lit/desktop'
  3130. })
  3131. .c('x', { 'xmlns': 'http://jabber.org/protocol/muc#user'})
  3132. .c('item', {
  3133. 'jid': 'annoyingguy@montague.lit',
  3134. 'affiliation': 'none',
  3135. 'role': 'participant'
  3136. });
  3137. _converse.connection._dataRecv(mock.createRequest(presence));
  3138. const textarea = view.querySelector('.chat-textarea')
  3139. textarea.value = '/kick';
  3140. view.onKeyDown({
  3141. target: textarea,
  3142. preventDefault: function preventDefault () {},
  3143. keyCode: 13
  3144. });
  3145. expect(view.validateRoleOrAffiliationChangeArgs).toHaveBeenCalled();
  3146. await u.waitUntil(() => view.querySelector('.message:last-child')?.textContent?.trim() ===
  3147. "Error: the \"kick\" command takes two arguments, the user's nickname and optionally a reason.");
  3148. expect(view.model.setRole).not.toHaveBeenCalled();
  3149. // Call now with the correct amount of arguments.
  3150. // XXX: Calling onFormSubmitted directly, trying
  3151. // again via triggering Event doesn't work for some weird
  3152. // reason.
  3153. textarea.value = '/kick @annoying guy You\'re annoying';
  3154. view.onFormSubmitted(new Event('submit'));
  3155. expect(view.validateRoleOrAffiliationChangeArgs.calls.count()).toBe(2);
  3156. expect(view.model.setRole).toHaveBeenCalled();
  3157. expect(Strophe.serialize(sent_IQ)).toBe(
  3158. `<iq id="${IQ_id}" to="lounge@montague.lit" type="set" xmlns="jabber:client">`+
  3159. `<query xmlns="http://jabber.org/protocol/muc#admin">`+
  3160. `<item nick="annoying guy" role="none">`+
  3161. `<reason>You&apos;re annoying</reason>`+
  3162. `</item>`+
  3163. `</query>`+
  3164. `</iq>`);
  3165. /* <presence
  3166. * from='harfleur@chat.shakespeare.lit/pistol'
  3167. * to='gower@shakespeare.lit/cell'
  3168. * type='unavailable'>
  3169. * <x xmlns='http://jabber.org/protocol/muc#user'>
  3170. * <item affiliation='none' role='none'/>
  3171. * <status code='307'/>
  3172. * </x>
  3173. * </presence>
  3174. */
  3175. presence = $pres({
  3176. 'from': 'lounge@montague.lit/annoying guy',
  3177. 'to': 'romeo@montague.lit/desktop',
  3178. 'type': 'unavailable'
  3179. })
  3180. .c('x', { 'xmlns': 'http://jabber.org/protocol/muc#user'})
  3181. .c('item', {
  3182. 'affiliation': 'none',
  3183. 'role': 'none'
  3184. }).c('actor', {'nick': 'romeo'}).up()
  3185. .c('reason').t("You're annoying").up().up()
  3186. .c('status', {'code': '307'});
  3187. _converse.connection._dataRecv(mock.createRequest(presence));
  3188. await u.waitUntil(() => view.querySelectorAll('.chat-info').length === 2);
  3189. expect(view.querySelectorAll('.chat-info__message')[1].textContent.trim()).toBe("annoying guy has been kicked out by romeo");
  3190. expect(view.querySelector('.chat-info:last-child q').textContent.trim()).toBe("You're annoying");
  3191. done();
  3192. }));
  3193. it("takes /op and /deop to make a user a moderator or not",
  3194. mock.initConverse([], {}, async function (done, _converse) {
  3195. const muc_jid = 'lounge@montague.lit';
  3196. await mock.openAndEnterChatRoom(_converse, muc_jid, 'romeo');
  3197. const view = _converse.api.chatviews.get(muc_jid);
  3198. let sent_IQ, IQ_id;
  3199. const sendIQ = _converse.connection.sendIQ;
  3200. spyOn(_converse.connection, 'sendIQ').and.callFake(function (iq, callback, errback) {
  3201. sent_IQ = iq;
  3202. IQ_id = sendIQ.bind(this)(iq, callback, errback);
  3203. });
  3204. spyOn(view.model, 'setRole').and.callThrough();
  3205. spyOn(view, 'validateRoleOrAffiliationChangeArgs').and.callThrough();
  3206. // New user enters the groupchat
  3207. /* <presence
  3208. * from='coven@chat.shakespeare.lit/thirdwitch'
  3209. * id='27C55F89-1C6A-459A-9EB5-77690145D624'
  3210. * to='crone1@shakespeare.lit/desktop'>
  3211. * <x xmlns='http://jabber.org/protocol/muc#user'>
  3212. * <item affiliation='member' role='moderator'/>
  3213. * </x>
  3214. * </presence>
  3215. */
  3216. let presence = $pres({
  3217. 'from': 'lounge@montague.lit/trustworthyguy',
  3218. 'id':'27C55F89-1C6A-459A-9EB5-77690145D624',
  3219. 'to': 'romeo@montague.lit/desktop'
  3220. })
  3221. .c('x', { 'xmlns': 'http://jabber.org/protocol/muc#user'})
  3222. .c('item', {
  3223. 'jid': 'trustworthyguy@montague.lit',
  3224. 'affiliation': 'member',
  3225. 'role': 'participant'
  3226. });
  3227. _converse.connection._dataRecv(mock.createRequest(presence));
  3228. const csntext = await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent);
  3229. expect(csntext.trim()).toEqual("romeo and trustworthyguy have entered the groupchat");
  3230. const textarea = view.querySelector('.chat-textarea')
  3231. textarea.value = '/op';
  3232. view.onKeyDown({
  3233. target: textarea,
  3234. preventDefault: function preventDefault () {},
  3235. keyCode: 13
  3236. });
  3237. expect(view.validateRoleOrAffiliationChangeArgs).toHaveBeenCalled();
  3238. await u.waitUntil(() => view.querySelector('.message:last-child')?.textContent?.trim() ===
  3239. "Error: the \"op\" command takes two arguments, the user's nickname and optionally a reason.");
  3240. expect(view.model.setRole).not.toHaveBeenCalled();
  3241. // Call now with the correct amount of arguments.
  3242. // XXX: Calling onFormSubmitted directly, trying
  3243. // again via triggering Event doesn't work for some weird
  3244. // reason.
  3245. textarea.value = '/op trustworthyguy You\'re trustworthy';
  3246. view.onFormSubmitted(new Event('submit'));
  3247. expect(view.validateRoleOrAffiliationChangeArgs.calls.count()).toBe(2);
  3248. expect(view.model.setRole).toHaveBeenCalled();
  3249. expect(Strophe.serialize(sent_IQ)).toBe(
  3250. `<iq id="${IQ_id}" to="lounge@montague.lit" type="set" xmlns="jabber:client">`+
  3251. `<query xmlns="http://jabber.org/protocol/muc#admin">`+
  3252. `<item nick="trustworthyguy" role="moderator">`+
  3253. `<reason>You&apos;re trustworthy</reason>`+
  3254. `</item>`+
  3255. `</query>`+
  3256. `</iq>`);
  3257. /* <presence
  3258. * from='coven@chat.shakespeare.lit/thirdwitch'
  3259. * to='crone1@shakespeare.lit/desktop'>
  3260. * <x xmlns='http://jabber.org/protocol/muc#user'>
  3261. * <item affiliation='member'
  3262. * jid='hag66@shakespeare.lit/pda'
  3263. * role='moderator'/>
  3264. * </x>
  3265. * </presence>
  3266. */
  3267. presence = $pres({
  3268. 'from': 'lounge@montague.lit/trustworthyguy',
  3269. 'to': 'romeo@montague.lit/desktop'
  3270. })
  3271. .c('x', { 'xmlns': 'http://jabber.org/protocol/muc#user'})
  3272. .c('item', {
  3273. 'jid': 'trustworthyguy@montague.lit',
  3274. 'affiliation': 'member',
  3275. 'role': 'moderator'
  3276. });
  3277. _converse.connection._dataRecv(mock.createRequest(presence));
  3278. // Check now that things get restored when the user is given a voice
  3279. await u.waitUntil(
  3280. () => view.querySelector('.chat-content__notifications').textContent.split('\n', 2).pop()?.trim() ===
  3281. "trustworthyguy is now a moderator");
  3282. // Call now with the correct amount of arguments.
  3283. // XXX: Calling onFormSubmitted directly, trying
  3284. // again via triggering Event doesn't work for some weird
  3285. // reason.
  3286. textarea.value = '/deop trustworthyguy Perhaps not';
  3287. view.onFormSubmitted(new Event('submit'));
  3288. expect(view.validateRoleOrAffiliationChangeArgs.calls.count()).toBe(3);
  3289. expect(view.model.setRole).toHaveBeenCalled();
  3290. expect(Strophe.serialize(sent_IQ)).toBe(
  3291. `<iq id="${IQ_id}" to="lounge@montague.lit" type="set" xmlns="jabber:client">`+
  3292. `<query xmlns="http://jabber.org/protocol/muc#admin">`+
  3293. `<item nick="trustworthyguy" role="participant">`+
  3294. `<reason>Perhaps not</reason>`+
  3295. `</item>`+
  3296. `</query>`+
  3297. `</iq>`);
  3298. /* <presence
  3299. * from='coven@chat.shakespeare.lit/thirdwitch'
  3300. * to='crone1@shakespeare.lit/desktop'>
  3301. * <x xmlns='http://jabber.org/protocol/muc#user'>
  3302. * <item affiliation='member'
  3303. * jid='hag66@shakespeare.lit/pda'
  3304. * role='participant'/>
  3305. * </x>
  3306. * </presence>
  3307. */
  3308. presence = $pres({
  3309. 'from': 'lounge@montague.lit/trustworthyguy',
  3310. 'to': 'romeo@montague.lit/desktop'
  3311. }).c('x', { 'xmlns': 'http://jabber.org/protocol/muc#user'})
  3312. .c('item', {
  3313. 'jid': 'trustworthyguy@montague.lit',
  3314. 'affiliation': 'member',
  3315. 'role': 'participant'
  3316. });
  3317. _converse.connection._dataRecv(mock.createRequest(presence));
  3318. await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent.includes("trustworthyguy is no longer a moderator"));
  3319. done();
  3320. }));
  3321. it("takes /mute and /voice to mute and unmute a user",
  3322. mock.initConverse([], {}, async function (done, _converse) {
  3323. const muc_jid = 'lounge@montague.lit';
  3324. await mock.openAndEnterChatRoom(_converse, muc_jid, 'romeo');
  3325. const view = _converse.api.chatviews.get(muc_jid);
  3326. var sent_IQ, IQ_id;
  3327. var sendIQ = _converse.connection.sendIQ;
  3328. spyOn(_converse.connection, 'sendIQ').and.callFake(function (iq, callback, errback) {
  3329. sent_IQ = iq;
  3330. IQ_id = sendIQ.bind(this)(iq, callback, errback);
  3331. });
  3332. spyOn(view.model, 'setRole').and.callThrough();
  3333. spyOn(view, 'validateRoleOrAffiliationChangeArgs').and.callThrough();
  3334. // New user enters the groupchat
  3335. /* <presence
  3336. * from='coven@chat.shakespeare.lit/thirdwitch'
  3337. * id='27C55F89-1C6A-459A-9EB5-77690145D624'
  3338. * to='crone1@shakespeare.lit/desktop'>
  3339. * <x xmlns='http://jabber.org/protocol/muc#user'>
  3340. * <item affiliation='member' role='participant'/>
  3341. * </x>
  3342. * </presence>
  3343. */
  3344. let presence = $pres({
  3345. 'from': 'lounge@montague.lit/annoyingGuy',
  3346. 'id':'27C55F89-1C6A-459A-9EB5-77690145D624',
  3347. 'to': 'romeo@montague.lit/desktop'
  3348. })
  3349. .c('x', { 'xmlns': 'http://jabber.org/protocol/muc#user'})
  3350. .c('item', {
  3351. 'jid': 'annoyingguy@montague.lit',
  3352. 'affiliation': 'member',
  3353. 'role': 'participant'
  3354. });
  3355. _converse.connection._dataRecv(mock.createRequest(presence));
  3356. const csntext = await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent);
  3357. expect(csntext.trim()).toEqual("romeo and annoyingGuy have entered the groupchat");
  3358. const textarea = view.querySelector('.chat-textarea')
  3359. textarea.value = '/mute';
  3360. view.onKeyDown({
  3361. target: textarea,
  3362. preventDefault: function preventDefault () {},
  3363. keyCode: 13
  3364. });
  3365. expect(view.validateRoleOrAffiliationChangeArgs).toHaveBeenCalled();
  3366. await u.waitUntil(() => view.querySelector('.message:last-child')?.textContent?.trim() ===
  3367. "Error: the \"mute\" command takes two arguments, the user's nickname and optionally a reason.");
  3368. expect(view.model.setRole).not.toHaveBeenCalled();
  3369. // Call now with the correct amount of arguments.
  3370. // XXX: Calling onFormSubmitted directly, trying
  3371. // again via triggering Event doesn't work for some weird
  3372. // reason.
  3373. textarea.value = '/mute annoyingGuy You\'re annoying';
  3374. view.onFormSubmitted(new Event('submit'));
  3375. expect(view.validateRoleOrAffiliationChangeArgs.calls.count()).toBe(2);
  3376. expect(view.model.setRole).toHaveBeenCalled();
  3377. expect(Strophe.serialize(sent_IQ)).toBe(
  3378. `<iq id="${IQ_id}" to="lounge@montague.lit" type="set" xmlns="jabber:client">`+
  3379. `<query xmlns="http://jabber.org/protocol/muc#admin">`+
  3380. `<item nick="annoyingGuy" role="visitor">`+
  3381. `<reason>You&apos;re annoying</reason>`+
  3382. `</item>`+
  3383. `</query>`+
  3384. `</iq>`);
  3385. /* <presence
  3386. * from='coven@chat.shakespeare.lit/thirdwitch'
  3387. * to='crone1@shakespeare.lit/desktop'>
  3388. * <x xmlns='http://jabber.org/protocol/muc#user'>
  3389. * <item affiliation='member'
  3390. * jid='hag66@shakespeare.lit/pda'
  3391. * role='visitor'/>
  3392. * </x>
  3393. * </presence>
  3394. */
  3395. presence = $pres({
  3396. 'from': 'lounge@montague.lit/annoyingGuy',
  3397. 'to': 'romeo@montague.lit/desktop'
  3398. })
  3399. .c('x', { 'xmlns': 'http://jabber.org/protocol/muc#user'})
  3400. .c('item', {
  3401. 'jid': 'annoyingguy@montague.lit',
  3402. 'affiliation': 'member',
  3403. 'role': 'visitor'
  3404. });
  3405. _converse.connection._dataRecv(mock.createRequest(presence));
  3406. await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent.includes("annoyingGuy has been muted"));
  3407. // Call now with the correct of arguments.
  3408. // XXX: Calling onFormSubmitted directly, trying
  3409. // again via triggering Event doesn't work for some weird
  3410. // reason.
  3411. textarea.value = '/voice annoyingGuy Now you can talk again';
  3412. view.onFormSubmitted(new Event('submit'));
  3413. expect(view.validateRoleOrAffiliationChangeArgs.calls.count()).toBe(3);
  3414. expect(view.model.setRole).toHaveBeenCalled();
  3415. expect(Strophe.serialize(sent_IQ)).toBe(
  3416. `<iq id="${IQ_id}" to="lounge@montague.lit" type="set" xmlns="jabber:client">`+
  3417. `<query xmlns="http://jabber.org/protocol/muc#admin">`+
  3418. `<item nick="annoyingGuy" role="participant">`+
  3419. `<reason>Now you can talk again</reason>`+
  3420. `</item>`+
  3421. `</query>`+
  3422. `</iq>`);
  3423. /* <presence
  3424. * from='coven@chat.shakespeare.lit/thirdwitch'
  3425. * to='crone1@shakespeare.lit/desktop'>
  3426. * <x xmlns='http://jabber.org/protocol/muc#user'>
  3427. * <item affiliation='member'
  3428. * jid='hag66@shakespeare.lit/pda'
  3429. * role='visitor'/>
  3430. * </x>
  3431. * </presence>
  3432. */
  3433. presence = $pres({
  3434. 'from': 'lounge@montague.lit/annoyingGuy',
  3435. 'to': 'romeo@montague.lit/desktop'
  3436. })
  3437. .c('x', { 'xmlns': 'http://jabber.org/protocol/muc#user'})
  3438. .c('item', {
  3439. 'jid': 'annoyingguy@montague.lit',
  3440. 'affiliation': 'member',
  3441. 'role': 'participant'
  3442. });
  3443. _converse.connection._dataRecv(mock.createRequest(presence));
  3444. await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent.includes("annoyingGuy has been given a voice"));
  3445. done();
  3446. }));
  3447. it("takes /destroy to destroy a muc",
  3448. mock.initConverse([], {}, async function (done, _converse) {
  3449. const muc_jid = 'lounge@montague.lit';
  3450. const new_muc_jid = 'foyer@montague.lit';
  3451. await mock.openAndEnterChatRoom(_converse, muc_jid, 'romeo');
  3452. let view = _converse.api.chatviews.get(muc_jid);
  3453. spyOn(_converse.api, 'confirm').and.callThrough();
  3454. let textarea = view.querySelector('.chat-textarea');
  3455. textarea.value = '/destroy';
  3456. view.onFormSubmitted(new Event('submit'));
  3457. let modal = await u.waitUntil(() => document.querySelector('.modal-dialog'));
  3458. await u.waitUntil(() => u.isVisible(modal));
  3459. let challenge_el = modal.querySelector('[name="challenge"]');
  3460. challenge_el.value = muc_jid+'e';
  3461. const reason_el = modal.querySelector('[name="reason"]');
  3462. reason_el.value = 'Moved to a new location';
  3463. const newjid_el = modal.querySelector('[name="newjid"]');
  3464. newjid_el.value = new_muc_jid;
  3465. let submit = modal.querySelector('[type="submit"]');
  3466. submit.click();
  3467. expect(u.isVisible(modal)).toBeTruthy();
  3468. expect(u.hasClass('error', challenge_el)).toBeTruthy();
  3469. challenge_el.value = muc_jid;
  3470. submit.click();
  3471. let sent_IQs = _converse.connection.IQ_stanzas;
  3472. let sent_IQ = await u.waitUntil(() => sent_IQs.filter(iq => iq.querySelector('destroy')).pop());
  3473. expect(Strophe.serialize(sent_IQ)).toBe(
  3474. `<iq id="${sent_IQ.getAttribute('id')}" to="${muc_jid}" type="set" xmlns="jabber:client">`+
  3475. `<query xmlns="http://jabber.org/protocol/muc#owner">`+
  3476. `<destroy jid="${new_muc_jid}">`+
  3477. `<reason>`+
  3478. `Moved to a new location`+
  3479. `</reason>`+
  3480. `</destroy>`+
  3481. `</query>`+
  3482. `</iq>`);
  3483. let result_stanza = $iq({
  3484. 'type': 'result',
  3485. 'id': sent_IQ.getAttribute('id'),
  3486. 'from': view.model.get('jid'),
  3487. 'to': _converse.connection.jid
  3488. });
  3489. spyOn(_converse.api, "trigger").and.callThrough();
  3490. expect(_converse.chatboxes.length).toBe(2);
  3491. _converse.connection._dataRecv(mock.createRequest(result_stanza));
  3492. await u.waitUntil(() => (view.model.session.get('connection_status') === converse.ROOMSTATUS.DISCONNECTED));
  3493. await u.waitUntil(() => _converse.chatboxes.length === 1);
  3494. expect(_converse.api.trigger).toHaveBeenCalledWith('chatBoxClosed', jasmine.any(Object));
  3495. // Try again without reason or new JID
  3496. _converse.connection.IQ_stanzas = [];
  3497. sent_IQs = _converse.connection.IQ_stanzas;
  3498. await mock.openAndEnterChatRoom(_converse, new_muc_jid, 'romeo');
  3499. view = _converse.api.chatviews.get(new_muc_jid);
  3500. textarea = view.querySelector('.chat-textarea');
  3501. textarea.value = '/destroy';
  3502. view.onFormSubmitted(new Event('submit'));
  3503. modal = await u.waitUntil(() => document.querySelector('.modal-dialog'));
  3504. await u.waitUntil(() => u.isVisible(modal));
  3505. challenge_el = modal.querySelector('[name="challenge"]');
  3506. challenge_el.value = new_muc_jid;
  3507. submit = modal.querySelector('[type="submit"]');
  3508. submit.click();
  3509. sent_IQ = await u.waitUntil(() => sent_IQs.filter(iq => iq.querySelector('destroy')).pop());
  3510. expect(Strophe.serialize(sent_IQ)).toBe(
  3511. `<iq id="${sent_IQ.getAttribute('id')}" to="${new_muc_jid}" type="set" xmlns="jabber:client">`+
  3512. `<query xmlns="http://jabber.org/protocol/muc#owner">`+
  3513. `<destroy/>`+
  3514. `</query>`+
  3515. `</iq>`);
  3516. result_stanza = $iq({
  3517. 'type': 'result',
  3518. 'id': sent_IQ.getAttribute('id'),
  3519. 'from': view.model.get('jid'),
  3520. 'to': _converse.connection.jid
  3521. });
  3522. expect(_converse.chatboxes.length).toBe(2);
  3523. _converse.connection._dataRecv(mock.createRequest(result_stanza));
  3524. await u.waitUntil(() => (view.model.session.get('connection_status') === converse.ROOMSTATUS.DISCONNECTED));
  3525. await u.waitUntil(() => _converse.chatboxes.length === 1);
  3526. done();
  3527. }));
  3528. });
  3529. describe("When attempting to enter a groupchat", function () {
  3530. it("will use the nickname set in the global settings if the user doesn't have a VCard nickname",
  3531. mock.initConverse(['chatBoxesFetched'], {'nickname': 'Benedict-Cucumberpatch'},
  3532. async function (done, _converse) {
  3533. await mock.openChatRoomViaModal(_converse, 'roomy@muc.montague.lit');
  3534. const view = _converse.chatboxviews.get('roomy@muc.montague.lit');
  3535. expect(view.model.get('nick')).toBe('Benedict-Cucumberpatch');
  3536. done();
  3537. }));
  3538. it("will show an error message if the groupchat requires a password",
  3539. mock.initConverse(['chatBoxesFetched'], {}, async function (done, _converse) {
  3540. const muc_jid = 'protected';
  3541. await mock.openChatRoomViaModal(_converse, muc_jid, 'romeo');
  3542. const view = _converse.chatboxviews.get(muc_jid);
  3543. spyOn(view, 'renderPasswordForm').and.callThrough();
  3544. const presence = $pres().attrs({
  3545. 'from': `${muc_jid}/romeo`,
  3546. 'id': u.getUniqueId(),
  3547. 'to': 'romeo@montague.lit/pda',
  3548. 'type': 'error'
  3549. }).c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
  3550. .c('error').attrs({by:'lounge@montague.lit', type:'auth'})
  3551. .c('not-authorized').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'});
  3552. _converse.connection._dataRecv(mock.createRequest(presence));
  3553. const chat_body = view.querySelector('.chatroom-body');
  3554. expect(view.renderPasswordForm).toHaveBeenCalled();
  3555. expect(chat_body.querySelectorAll('form.chatroom-form').length).toBe(1);
  3556. expect(chat_body.querySelector('.chatroom-form label').textContent.trim())
  3557. .toBe('This groupchat requires a password');
  3558. // Let's submit the form
  3559. spyOn(view.model, 'join');
  3560. const input_el = view.querySelector('[name="password"]');
  3561. input_el.value = 'secret';
  3562. view.querySelector('input[type=submit]').click();
  3563. expect(view.model.join).toHaveBeenCalledWith('romeo', 'secret');
  3564. done();
  3565. }));
  3566. it("will show an error message if the groupchat is members-only and the user not included",
  3567. mock.initConverse([], {}, async function (done, _converse) {
  3568. const muc_jid = 'members-only@muc.montague.lit'
  3569. await mock.openChatRoomViaModal(_converse, muc_jid, 'romeo');
  3570. const view = _converse.chatboxviews.get(muc_jid);
  3571. const iq = await u.waitUntil(() => _.filter(
  3572. _converse.connection.IQ_stanzas,
  3573. iq => iq.querySelector(
  3574. `iq[to="${muc_jid}"] query[xmlns="http://jabber.org/protocol/disco#info"]`
  3575. )).pop());
  3576. // State that the chat is members-only via the features IQ
  3577. const features_stanza = $iq({
  3578. 'from': muc_jid,
  3579. 'id': iq.getAttribute('id'),
  3580. 'to': 'romeo@montague.lit/desktop',
  3581. 'type': 'result'
  3582. })
  3583. .c('query', { 'xmlns': 'http://jabber.org/protocol/disco#info'})
  3584. .c('identity', {
  3585. 'category': 'conference',
  3586. 'name': 'A Dark Cave',
  3587. 'type': 'text'
  3588. }).up()
  3589. .c('feature', {'var': 'http://jabber.org/protocol/muc'}).up()
  3590. .c('feature', {'var': 'muc_hidden'}).up()
  3591. .c('feature', {'var': 'muc_temporary'}).up()
  3592. .c('feature', {'var': 'muc_membersonly'}).up();
  3593. _converse.connection._dataRecv(mock.createRequest(features_stanza));
  3594. await u.waitUntil(() => view.model.session.get('connection_status') === converse.ROOMSTATUS.CONNECTING);
  3595. const presence = $pres().attrs({
  3596. from: `${muc_jid}/romeo`,
  3597. id: u.getUniqueId(),
  3598. to: 'romeo@montague.lit/pda',
  3599. type: 'error'
  3600. }).c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
  3601. .c('error').attrs({by:'lounge@montague.lit', type:'auth'})
  3602. .c('registration-required').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
  3603. _converse.connection._dataRecv(mock.createRequest(presence));
  3604. expect(view.querySelector('.chatroom-body .disconnect-container .disconnect-msg:last-child').textContent.trim())
  3605. .toBe('You are not on the member list of this groupchat.');
  3606. done();
  3607. }));
  3608. it("will show an error message if the user has been banned",
  3609. mock.initConverse([], {}, async function (done, _converse) {
  3610. const muc_jid = 'off-limits@muc.montague.lit'
  3611. await mock.openChatRoomViaModal(_converse, muc_jid, 'romeo');
  3612. const iq = await u.waitUntil(() => _.filter(
  3613. _converse.connection.IQ_stanzas,
  3614. iq => iq.querySelector(
  3615. `iq[to="${muc_jid}"] query[xmlns="http://jabber.org/protocol/disco#info"]`
  3616. )).pop());
  3617. const features_stanza = $iq({
  3618. 'from': muc_jid,
  3619. 'id': iq.getAttribute('id'),
  3620. 'to': 'romeo@montague.lit/desktop',
  3621. 'type': 'result'
  3622. })
  3623. .c('query', { 'xmlns': 'http://jabber.org/protocol/disco#info'})
  3624. .c('identity', {'category': 'conference', 'name': 'A Dark Cave', 'type': 'text'}).up()
  3625. .c('feature', {'var': 'http://jabber.org/protocol/muc'}).up()
  3626. .c('feature', {'var': 'muc_hidden'}).up()
  3627. .c('feature', {'var': 'muc_temporary'}).up()
  3628. _converse.connection._dataRecv(mock.createRequest(features_stanza));
  3629. const view = _converse.chatboxviews.get(muc_jid);
  3630. await u.waitUntil(() => view.model.session.get('connection_status') === converse.ROOMSTATUS.CONNECTING);
  3631. const presence = $pres().attrs({
  3632. from: `${muc_jid}/romeo`,
  3633. id: u.getUniqueId(),
  3634. to: 'romeo@montague.lit/pda',
  3635. type: 'error'
  3636. }).c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
  3637. .c('error').attrs({by:'lounge@montague.lit', type:'auth'})
  3638. .c('forbidden').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
  3639. _converse.connection._dataRecv(mock.createRequest(presence));
  3640. expect(view.querySelector('.chatroom-body .disconnect-container .disconnect-msg:last-child').textContent.trim())
  3641. .toBe('You have been banned from this groupchat.');
  3642. done();
  3643. }));
  3644. it("will render a nickname form if a nickname conflict happens and muc_nickname_from_jid=false",
  3645. mock.initConverse([], {}, async function (done, _converse) {
  3646. const muc_jid = 'conflicted@muc.montague.lit';
  3647. await mock.openChatRoomViaModal(_converse, muc_jid, 'romeo');
  3648. const iq = await u.waitUntil(() => _.filter(
  3649. _converse.connection.IQ_stanzas,
  3650. iq => iq.querySelector(
  3651. `iq[to="${muc_jid}"] query[xmlns="http://jabber.org/protocol/disco#info"]`
  3652. )).pop());
  3653. const features_stanza = $iq({
  3654. 'from': muc_jid,
  3655. 'id': iq.getAttribute('id'),
  3656. 'to': 'romeo@montague.lit/desktop',
  3657. 'type': 'result'
  3658. })
  3659. .c('query', { 'xmlns': 'http://jabber.org/protocol/disco#info'})
  3660. .c('identity', {'category': 'conference', 'name': 'A Dark Cave', 'type': 'text'}).up()
  3661. .c('feature', {'var': 'http://jabber.org/protocol/muc'}).up()
  3662. .c('feature', {'var': 'muc_hidden'}).up()
  3663. .c('feature', {'var': 'muc_temporary'}).up()
  3664. _converse.connection._dataRecv(mock.createRequest(features_stanza));
  3665. const view = _converse.chatboxviews.get(muc_jid);
  3666. await u.waitUntil(() => view.model.session.get('connection_status') === converse.ROOMSTATUS.CONNECTING);
  3667. const presence = $pres().attrs({
  3668. from: `${muc_jid}/romeo`,
  3669. id: u.getUniqueId(),
  3670. to: 'romeo@montague.lit/pda',
  3671. type: 'error'
  3672. }).c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
  3673. .c('error').attrs({by: muc_jid, type:'cancel'})
  3674. .c('conflict').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
  3675. _converse.connection._dataRecv(mock.createRequest(presence));
  3676. expect(view.querySelector('.muc-nickname-form .validation-message').textContent.trim())
  3677. .toBe('The nickname you chose is reserved or currently in use, please choose a different one.');
  3678. done();
  3679. }));
  3680. it("will automatically choose a new nickname if a nickname conflict happens and muc_nickname_from_jid=true",
  3681. mock.initConverse(['chatBoxesFetched'], {}, async function (done, _converse) {
  3682. const muc_jid = 'conflicting@muc.montague.lit'
  3683. await mock.openChatRoomViaModal(_converse, muc_jid, 'romeo');
  3684. /* <presence
  3685. * from='coven@chat.shakespeare.lit/thirdwitch'
  3686. * id='n13mt3l'
  3687. * to='hag66@shakespeare.lit/pda'
  3688. * type='error'>
  3689. * <x xmlns='http://jabber.org/protocol/muc'/>
  3690. * <error by='coven@chat.shakespeare.lit' type='cancel'>
  3691. * <conflict xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/>
  3692. * </error>
  3693. * </presence>
  3694. */
  3695. _converse.muc_nickname_from_jid = true;
  3696. const attrs = {
  3697. 'from': `${muc_jid}/romeo`,
  3698. 'id': u.getUniqueId(),
  3699. 'to': 'romeo@montague.lit/pda',
  3700. 'type': 'error'
  3701. };
  3702. let presence = $pres().attrs(attrs)
  3703. .c('x').attrs({'xmlns':'http://jabber.org/protocol/muc'}).up()
  3704. .c('error').attrs({'by': muc_jid, 'type':'cancel'})
  3705. .c('conflict').attrs({'xmlns':'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
  3706. const view = _converse.chatboxviews.get(muc_jid);
  3707. spyOn(view.model, 'join').and.callThrough();
  3708. // Simulate repeatedly that there's already someone in the groupchat
  3709. // with that nickname
  3710. _converse.connection._dataRecv(mock.createRequest(presence));
  3711. expect(view.model.join).toHaveBeenCalledWith('romeo-2');
  3712. attrs.from = `${muc_jid}/romeo-2`;
  3713. attrs.id = u.getUniqueId();
  3714. presence = $pres().attrs(attrs)
  3715. .c('x').attrs({'xmlns':'http://jabber.org/protocol/muc'}).up()
  3716. .c('error').attrs({'by': muc_jid, type:'cancel'})
  3717. .c('conflict').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
  3718. _converse.connection._dataRecv(mock.createRequest(presence));
  3719. expect(view.model.join).toHaveBeenCalledWith('romeo-3');
  3720. attrs.from = `${muc_jid}/romeo-3`;
  3721. attrs.id = new Date().getTime();
  3722. presence = $pres().attrs(attrs)
  3723. .c('x').attrs({'xmlns': 'http://jabber.org/protocol/muc'}).up()
  3724. .c('error').attrs({'by': muc_jid, 'type': 'cancel'})
  3725. .c('conflict').attrs({'xmlns':'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
  3726. _converse.connection._dataRecv(mock.createRequest(presence));
  3727. expect(view.model.join).toHaveBeenCalledWith('romeo-4');
  3728. done();
  3729. }));
  3730. it("will show an error message if the user is not allowed to have created the groupchat",
  3731. mock.initConverse([], {}, async function (done, _converse) {
  3732. const muc_jid = 'impermissable@muc.montague.lit'
  3733. await mock.openChatRoomViaModal(_converse, muc_jid, 'romeo')
  3734. // We pretend this is a new room, so no disco info is returned.
  3735. const iq = await u.waitUntil(() => _.filter(
  3736. _converse.connection.IQ_stanzas,
  3737. iq => iq.querySelector(
  3738. `iq[to="${muc_jid}"] query[xmlns="http://jabber.org/protocol/disco#info"]`
  3739. )).pop());
  3740. const features_stanza = $iq({
  3741. 'from': 'room@conference.example.org',
  3742. 'id': iq.getAttribute('id'),
  3743. 'to': 'romeo@montague.lit/desktop',
  3744. 'type': 'error'
  3745. }).c('error', {'type': 'cancel'})
  3746. .c('item-not-found', {'xmlns': "urn:ietf:params:xml:ns:xmpp-stanzas"});
  3747. _converse.connection._dataRecv(mock.createRequest(features_stanza));
  3748. const view = _converse.chatboxviews.get(muc_jid);
  3749. await u.waitUntil(() => (view.model.session.get('connection_status') === converse.ROOMSTATUS.CONNECTING));
  3750. const presence = $pres().attrs({
  3751. from: `${muc_jid}/romeo`,
  3752. id: u.getUniqueId(),
  3753. to:'romeo@montague.lit/pda',
  3754. type:'error'
  3755. }).c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
  3756. .c('error').attrs({by:'lounge@montague.lit', type:'cancel'})
  3757. .c('not-allowed').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
  3758. _converse.connection._dataRecv(mock.createRequest(presence));
  3759. expect(view.querySelector('.chatroom-body .disconnect-container .disconnect-msg:last-child').textContent.trim())
  3760. .toBe('You are not allowed to create new groupchats.');
  3761. done();
  3762. }));
  3763. it("will show an error message if the user's nickname doesn't conform to groupchat policy",
  3764. mock.initConverse([], {}, async function (done, _converse) {
  3765. const muc_jid = 'conformist@muc.montague.lit'
  3766. await mock.openChatRoomViaModal(_converse, muc_jid, 'romeo');
  3767. const iq = await u.waitUntil(() => _.filter(
  3768. _converse.connection.IQ_stanzas,
  3769. iq => iq.querySelector(
  3770. `iq[to="${muc_jid}"] query[xmlns="http://jabber.org/protocol/disco#info"]`
  3771. )).pop());
  3772. const features_stanza = $iq({
  3773. 'from': muc_jid,
  3774. 'id': iq.getAttribute('id'),
  3775. 'to': 'romeo@montague.lit/desktop',
  3776. 'type': 'result'
  3777. }).c('query', { 'xmlns': 'http://jabber.org/protocol/disco#info'})
  3778. .c('identity', {'category': 'conference', 'name': 'A Dark Cave', 'type': 'text'}).up()
  3779. .c('feature', {'var': 'http://jabber.org/protocol/muc'}).up()
  3780. _converse.connection._dataRecv(mock.createRequest(features_stanza));
  3781. const view = _converse.chatboxviews.get(muc_jid);
  3782. await u.waitUntil(() => (view.model.session.get('connection_status') === converse.ROOMSTATUS.CONNECTING));
  3783. const presence = $pres().attrs({
  3784. from: `${muc_jid}/romeo`,
  3785. id: u.getUniqueId(),
  3786. to:'romeo@montague.lit/pda',
  3787. type:'error'
  3788. }).c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
  3789. .c('error').attrs({by:'lounge@montague.lit', type:'cancel'})
  3790. .c('not-acceptable').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
  3791. _converse.connection._dataRecv(mock.createRequest(presence));
  3792. expect(view.querySelector('.chatroom-body .disconnect-container .disconnect-msg:last-child').textContent.trim())
  3793. .toBe("Your nickname doesn't conform to this groupchat's policies.");
  3794. done();
  3795. }));
  3796. it("will show an error message if the groupchat doesn't yet exist",
  3797. mock.initConverse([], {}, async function (done, _converse) {
  3798. const muc_jid = 'nonexistent@muc.montague.lit'
  3799. await mock.openChatRoomViaModal(_converse, muc_jid, 'romeo');
  3800. const iq = await u.waitUntil(() => _.filter(
  3801. _converse.connection.IQ_stanzas,
  3802. iq => iq.querySelector(
  3803. `iq[to="${muc_jid}"] query[xmlns="http://jabber.org/protocol/disco#info"]`
  3804. )).pop());
  3805. const features_stanza = $iq({
  3806. 'from': muc_jid,
  3807. 'id': iq.getAttribute('id'),
  3808. 'to': 'romeo@montague.lit/desktop',
  3809. 'type': 'result'
  3810. }).c('query', { 'xmlns': 'http://jabber.org/protocol/disco#info'})
  3811. .c('identity', {'category': 'conference', 'name': 'A Dark Cave', 'type': 'text'}).up()
  3812. .c('feature', {'var': 'http://jabber.org/protocol/muc'}).up()
  3813. _converse.connection._dataRecv(mock.createRequest(features_stanza));
  3814. const view = _converse.chatboxviews.get(muc_jid);
  3815. await u.waitUntil(() => (view.model.session.get('connection_status') === converse.ROOMSTATUS.CONNECTING));
  3816. const presence = $pres().attrs({
  3817. from: `${muc_jid}/romeo`,
  3818. id: u.getUniqueId(),
  3819. to: 'romeo@montague.lit/pda',
  3820. type:'error'
  3821. }).c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
  3822. .c('error').attrs({by:'lounge@montague.lit', type:'cancel'})
  3823. .c('item-not-found').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
  3824. _converse.connection._dataRecv(mock.createRequest(presence));
  3825. expect(view.querySelector('.chatroom-body .disconnect-container .disconnect-msg:last-child').textContent.trim())
  3826. .toBe("This groupchat does not (yet) exist.");
  3827. done();
  3828. }));
  3829. it("will show an error message if the groupchat has reached its maximum number of participants",
  3830. mock.initConverse([], {}, async function (done, _converse) {
  3831. const muc_jid = 'maxed-out@muc.montague.lit'
  3832. await mock.openChatRoomViaModal(_converse, muc_jid, 'romeo')
  3833. const iq = await u.waitUntil(() => _.filter(
  3834. _converse.connection.IQ_stanzas,
  3835. iq => iq.querySelector(
  3836. `iq[to="${muc_jid}"] query[xmlns="http://jabber.org/protocol/disco#info"]`
  3837. )).pop());
  3838. const features_stanza = $iq({
  3839. 'from': muc_jid,
  3840. 'id': iq.getAttribute('id'),
  3841. 'to': 'romeo@montague.lit/desktop',
  3842. 'type': 'result'
  3843. }).c('query', { 'xmlns': 'http://jabber.org/protocol/disco#info'})
  3844. .c('identity', {'category': 'conference', 'name': 'A Dark Cave', 'type': 'text'}).up()
  3845. .c('feature', {'var': 'http://jabber.org/protocol/muc'}).up()
  3846. _converse.connection._dataRecv(mock.createRequest(features_stanza));
  3847. const view = _converse.chatboxviews.get(muc_jid);
  3848. await u.waitUntil(() => (view.model.session.get('connection_status') === converse.ROOMSTATUS.CONNECTING));
  3849. const presence = $pres().attrs({
  3850. from: `${muc_jid}/romeo`,
  3851. id: u.getUniqueId(),
  3852. to:'romeo@montague.lit/pda',
  3853. type:'error'
  3854. }).c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
  3855. .c('error').attrs({by:'lounge@montague.lit', type:'cancel'})
  3856. .c('service-unavailable').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
  3857. _converse.connection._dataRecv(mock.createRequest(presence));
  3858. expect(view.querySelector('.chatroom-body .disconnect-container .disconnect-msg:last-child').textContent.trim())
  3859. .toBe("This groupchat has reached its maximum number of participants.");
  3860. done();
  3861. }));
  3862. });
  3863. describe("Someone being invited to a groupchat", function () {
  3864. it("will first be added to the member list if the groupchat is members only",
  3865. mock.initConverse(['chatBoxesFetched'], {}, async function (done, _converse) {
  3866. await mock.waitForRoster(_converse, 'current', 0);
  3867. spyOn(_converse.ChatRoomOccupants.prototype, 'fetchMembers').and.callThrough();
  3868. const sent_IQs = _converse.connection.IQ_stanzas;
  3869. const muc_jid = 'coven@chat.shakespeare.lit';
  3870. const nick = 'romeo';
  3871. const room_creation_promise = _converse.api.rooms.open(muc_jid, {nick});
  3872. // Check that the groupchat queried for the features.
  3873. let stanza = await u.waitUntil(() => sent_IQs.filter(iq => iq.querySelector(`iq[to="${muc_jid}"] query[xmlns="http://jabber.org/protocol/disco#info"]`)).pop());
  3874. expect(Strophe.serialize(stanza)).toBe(
  3875. `<iq from="romeo@montague.lit/orchard" id="${stanza.getAttribute("id")}" to="${muc_jid}" type="get" xmlns="jabber:client">`+
  3876. `<query xmlns="http://jabber.org/protocol/disco#info"/>`+
  3877. `</iq>`);
  3878. // State that the chat is members-only via the features IQ
  3879. const view = _converse.chatboxviews.get(muc_jid);
  3880. const features_stanza = $iq({
  3881. from: 'coven@chat.shakespeare.lit',
  3882. 'id': stanza.getAttribute('id'),
  3883. 'to': 'romeo@montague.lit/desktop',
  3884. 'type': 'result'
  3885. })
  3886. .c('query', { 'xmlns': 'http://jabber.org/protocol/disco#info'})
  3887. .c('identity', {
  3888. 'category': 'conference',
  3889. 'name': 'A Dark Cave',
  3890. 'type': 'text'
  3891. }).up()
  3892. .c('feature', {'var': 'http://jabber.org/protocol/muc'}).up()
  3893. .c('feature', {'var': 'muc_hidden'}).up()
  3894. .c('feature', {'var': 'muc_temporary'}).up()
  3895. .c('feature', {'var': 'muc_membersonly'}).up();
  3896. _converse.connection._dataRecv(mock.createRequest(features_stanza));
  3897. const sent_stanzas = _converse.connection.sent_stanzas;
  3898. await u.waitUntil(() => sent_stanzas.filter(s => s.matches(`presence[to="${muc_jid}/${nick}"]`)).pop());
  3899. expect(view.model.features.get('membersonly')).toBeTruthy();
  3900. await room_creation_promise;
  3901. await mock.createContacts(_converse, 'current');
  3902. let sent_stanza, sent_id;
  3903. spyOn(_converse.connection, 'send').and.callFake(function (stanza) {
  3904. if (stanza.nodeTree && stanza.nodeTree.nodeName === 'message') {
  3905. sent_id = stanza.nodeTree.getAttribute('id');
  3906. sent_stanza = stanza;
  3907. }
  3908. });
  3909. const invitee_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@montague.lit';
  3910. const reason = "Please join this groupchat";
  3911. view.model.directInvite(invitee_jid, reason);
  3912. // Check in reverse order that we requested all three lists
  3913. const owner_iq = sent_IQs.pop();
  3914. expect(Strophe.serialize(owner_iq)).toBe(
  3915. `<iq id="${owner_iq.getAttribute('id')}" to="coven@chat.shakespeare.lit" type="get" xmlns="jabber:client">`+
  3916. `<query xmlns="http://jabber.org/protocol/muc#admin"><item affiliation="owner"/></query>`+
  3917. `</iq>`);
  3918. const admin_iq = sent_IQs.pop();
  3919. expect(Strophe.serialize(admin_iq)).toBe(
  3920. `<iq id="${admin_iq.getAttribute('id')}" to="coven@chat.shakespeare.lit" type="get" xmlns="jabber:client">`+
  3921. `<query xmlns="http://jabber.org/protocol/muc#admin"><item affiliation="admin"/></query>`+
  3922. `</iq>`);
  3923. const member_iq = sent_IQs.pop();
  3924. expect(Strophe.serialize(member_iq)).toBe(
  3925. `<iq id="${member_iq.getAttribute('id')}" to="coven@chat.shakespeare.lit" type="get" xmlns="jabber:client">`+
  3926. `<query xmlns="http://jabber.org/protocol/muc#admin"><item affiliation="member"/></query>`+
  3927. `</iq>`);
  3928. // Now the service sends the member lists to the user
  3929. const member_list_stanza = $iq({
  3930. 'from': 'coven@chat.shakespeare.lit',
  3931. 'id': member_iq.getAttribute('id'),
  3932. 'to': 'romeo@montague.lit/orchard',
  3933. 'type': 'result'
  3934. }).c('query', {'xmlns': Strophe.NS.MUC_ADMIN})
  3935. .c('item', {
  3936. 'affiliation': 'member',
  3937. 'jid': 'hag66@shakespeare.lit',
  3938. 'nick': 'thirdwitch',
  3939. 'role': 'participant'
  3940. });
  3941. _converse.connection._dataRecv(mock.createRequest(member_list_stanza));
  3942. const admin_list_stanza = $iq({
  3943. 'from': 'coven@chat.shakespeare.lit',
  3944. 'id': admin_iq.getAttribute('id'),
  3945. 'to': 'romeo@montague.lit/orchard',
  3946. 'type': 'result'
  3947. }).c('query', {'xmlns': Strophe.NS.MUC_ADMIN})
  3948. .c('item', {
  3949. 'affiliation': 'admin',
  3950. 'jid': 'wiccarocks@shakespeare.lit',
  3951. 'nick': 'secondwitch'
  3952. });
  3953. _converse.connection._dataRecv(mock.createRequest(admin_list_stanza));
  3954. const owner_list_stanza = $iq({
  3955. 'from': 'coven@chat.shakespeare.lit',
  3956. 'id': owner_iq.getAttribute('id'),
  3957. 'to': 'romeo@montague.lit/orchard',
  3958. 'type': 'result'
  3959. }).c('query', {'xmlns': Strophe.NS.MUC_ADMIN})
  3960. .c('item', {
  3961. 'affiliation': 'owner',
  3962. 'jid': 'crone1@shakespeare.lit',
  3963. });
  3964. _converse.connection._dataRecv(mock.createRequest(owner_list_stanza));
  3965. // Converse puts the user on the member list
  3966. stanza = await u.waitUntil(() => sent_IQs.filter(iq => iq.querySelector(`iq[to="${muc_jid}"] query[xmlns="http://jabber.org/protocol/muc#admin"]`)).pop());
  3967. expect(stanza.outerHTML,
  3968. `<iq id="${stanza.getAttribute('id')}" to="coven@chat.shakespeare.lit" type="set" xmlns="jabber:client">`+
  3969. `<query xmlns="http://jabber.org/protocol/muc#admin">`+
  3970. `<item affiliation="member" jid="${invitee_jid}">`+
  3971. `<reason>Please join this groupchat</reason>`+
  3972. `</item>`+
  3973. `</query>`+
  3974. `</iq>`);
  3975. const result = $iq({
  3976. 'from': 'coven@chat.shakespeare.lit',
  3977. 'id': stanza.getAttribute('id'),
  3978. 'to': 'romeo@montague.lit/orchard',
  3979. 'type': 'result'
  3980. });
  3981. _converse.connection._dataRecv(mock.createRequest(result));
  3982. await u.waitUntil(() => view.model.occupants.fetchMembers.calls.count());
  3983. // Finally check that the user gets invited.
  3984. expect(sent_stanza.toLocaleString()).toBe( // Strophe adds the xmlns attr (although not in spec)
  3985. `<message from="romeo@montague.lit/orchard" id="${sent_id}" to="${invitee_jid}" xmlns="jabber:client">`+
  3986. `<x jid="coven@chat.shakespeare.lit" reason="Please join this groupchat" xmlns="jabber:x:conference"/>`+
  3987. `</message>`
  3988. );
  3989. done();
  3990. }));
  3991. });
  3992. describe("The affiliations delta", function () {
  3993. it("can be computed in various ways", mock.initConverse([], {}, async function (done, _converse) {
  3994. await mock.openChatRoom(_converse, 'coven', 'chat.shakespeare.lit', 'romeo');
  3995. var exclude_existing = false;
  3996. var remove_absentees = false;
  3997. var new_list = [];
  3998. var old_list = [];
  3999. const muc_utils = converse.env.muc_utils;
  4000. var delta = muc_utils.computeAffiliationsDelta(exclude_existing, remove_absentees, new_list, old_list);
  4001. expect(delta.length).toBe(0);
  4002. new_list = [{'jid': 'wiccarocks@shakespeare.lit', 'affiliation': 'member'}];
  4003. old_list = [{'jid': 'wiccarocks@shakespeare.lit', 'affiliation': 'member'}];
  4004. delta = muc_utils.computeAffiliationsDelta(exclude_existing, remove_absentees, new_list, old_list);
  4005. expect(delta.length).toBe(0);
  4006. // When remove_absentees is false, then affiliations in the old
  4007. // list which are not in the new one won't be removed.
  4008. old_list = [{'jid': 'oldhag666@shakespeare.lit', 'affiliation': 'owner'},
  4009. {'jid': 'wiccarocks@shakespeare.lit', 'affiliation': 'member'}];
  4010. delta = muc_utils.computeAffiliationsDelta(exclude_existing, remove_absentees, new_list, old_list);
  4011. expect(delta.length).toBe(0);
  4012. // With exclude_existing set to false, any changed affiliations
  4013. // will be included in the delta (i.e. existing affiliations are included in the comparison).
  4014. old_list = [{'jid': 'wiccarocks@shakespeare.lit', 'affiliation': 'owner'}];
  4015. delta = muc_utils.computeAffiliationsDelta(exclude_existing, remove_absentees, new_list, old_list);
  4016. expect(delta.length).toBe(1);
  4017. expect(delta[0].jid).toBe('wiccarocks@shakespeare.lit');
  4018. expect(delta[0].affiliation).toBe('member');
  4019. // To also remove affiliations from the old list which are not
  4020. // in the new list, we set remove_absentees to true
  4021. remove_absentees = true;
  4022. old_list = [{'jid': 'oldhag666@shakespeare.lit', 'affiliation': 'owner'},
  4023. {'jid': 'wiccarocks@shakespeare.lit', 'affiliation': 'member'}];
  4024. delta = muc_utils.computeAffiliationsDelta(exclude_existing, remove_absentees, new_list, old_list);
  4025. expect(delta.length).toBe(1);
  4026. expect(delta[0].jid).toBe('oldhag666@shakespeare.lit');
  4027. expect(delta[0].affiliation).toBe('none');
  4028. delta = muc_utils.computeAffiliationsDelta(exclude_existing, remove_absentees, [], old_list);
  4029. expect(delta.length).toBe(2);
  4030. expect(delta[0].jid).toBe('oldhag666@shakespeare.lit');
  4031. expect(delta[0].affiliation).toBe('none');
  4032. expect(delta[1].jid).toBe('wiccarocks@shakespeare.lit');
  4033. expect(delta[1].affiliation).toBe('none');
  4034. // To only add a user if they don't already have an
  4035. // affiliation, we set 'exclude_existing' to true
  4036. exclude_existing = true;
  4037. old_list = [{'jid': 'wiccarocks@shakespeare.lit', 'affiliation': 'owner'}];
  4038. delta = muc_utils.computeAffiliationsDelta(exclude_existing, remove_absentees, new_list, old_list);
  4039. expect(delta.length).toBe(0);
  4040. old_list = [{'jid': 'wiccarocks@shakespeare.lit', 'affiliation': 'admin'}];
  4041. delta = muc_utils.computeAffiliationsDelta(exclude_existing, remove_absentees, new_list, old_list);
  4042. expect(delta.length).toBe(0);
  4043. done();
  4044. }));
  4045. });
  4046. describe("The \"Groupchats\" Add modal", function () {
  4047. it("can be opened from a link in the \"Groupchats\" section of the controlbox",
  4048. mock.initConverse(['chatBoxesFetched'], {}, async function (done, _converse) {
  4049. await mock.openControlBox(_converse);
  4050. await mock.waitForRoster(_converse, 'current', 0);
  4051. const roomspanel = _converse.chatboxviews.get('controlbox').querySelector('converse-rooms-list');
  4052. roomspanel.querySelector('.show-add-muc-modal').click();
  4053. mock.closeControlBox(_converse);
  4054. const modal = _converse.api.modal.get('add-chatroom-modal');
  4055. await u.waitUntil(() => u.isVisible(modal.el), 1000)
  4056. let label_name = modal.el.querySelector('label[for="chatroom"]');
  4057. expect(label_name.textContent.trim()).toBe('Groupchat address:');
  4058. let name_input = modal.el.querySelector('input[name="chatroom"]');
  4059. expect(name_input.placeholder).toBe('name@conference.example.org');
  4060. const label_nick = modal.el.querySelector('label[for="nickname"]');
  4061. expect(label_nick.textContent.trim()).toBe('Nickname:');
  4062. const nick_input = modal.el.querySelector('input[name="nickname"]');
  4063. expect(nick_input.value).toBe('');
  4064. nick_input.value = 'romeo';
  4065. expect(modal.el.querySelector('.modal-title').textContent.trim()).toBe('Enter a new Groupchat');
  4066. spyOn(_converse.ChatRoom.prototype, 'getDiscoInfo').and.callFake(() => Promise.resolve());
  4067. roomspanel.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  4068. modal.el.querySelector('input[name="chatroom"]').value = 'lounce@muc.montague.lit';
  4069. modal.el.querySelector('form input[type="submit"]').click();
  4070. await u.waitUntil(() => _converse.chatboxes.length);
  4071. await u.waitUntil(() => sizzle('.chatroom', _converse.el).filter(u.isVisible).length === 1);
  4072. roomspanel.model.set('muc_domain', 'muc.example.org');
  4073. roomspanel.querySelector('.show-add-muc-modal').click();
  4074. label_name = modal.el.querySelector('label[for="chatroom"]');
  4075. expect(label_name.textContent.trim()).toBe('Groupchat address:');
  4076. name_input = modal.el.querySelector('input[name="chatroom"]');
  4077. expect(name_input.placeholder).toBe('name@muc.example.org');
  4078. done();
  4079. }));
  4080. it("doesn't show the nickname field if locked_muc_nickname is true",
  4081. mock.initConverse(['chatBoxesFetched'], {'locked_muc_nickname': true, 'muc_nickname_from_jid': true}, async function (done, _converse) {
  4082. await mock.openControlBox(_converse);
  4083. await mock.waitForRoster(_converse, 'current', 0);
  4084. const roomspanel = _converse.chatboxviews.get('controlbox').querySelector('converse-rooms-list');
  4085. roomspanel.querySelector('.show-add-muc-modal').click();
  4086. mock.closeControlBox(_converse);
  4087. const modal = _converse.api.modal.get('add-chatroom-modal');
  4088. await u.waitUntil(() => u.isVisible(modal.el), 1000)
  4089. const name_input = modal.el.querySelector('input[name="chatroom"]');
  4090. name_input.value = 'lounge@montague.lit';
  4091. expect(modal.el.querySelector('label[for="nickname"]')).toBe(null);
  4092. expect(modal.el.querySelector('input[name="nickname"]')).toBe(null);
  4093. modal.el.querySelector('form input[type="submit"]').click();
  4094. await u.waitUntil(() => _converse.chatboxes.length > 1);
  4095. const chatroom = _converse.chatboxes.get('lounge@montague.lit');
  4096. expect(chatroom.get('nick')).toBe('romeo');
  4097. done();
  4098. }));
  4099. it("uses the JID node if muc_nickname_from_jid is set to true",
  4100. mock.initConverse(['chatBoxesFetched'], {'muc_nickname_from_jid': true}, async function (done, _converse) {
  4101. await mock.openControlBox(_converse);
  4102. await mock.waitForRoster(_converse, 'current', 0);
  4103. const roomspanel = _converse.chatboxviews.get('controlbox').querySelector('converse-rooms-list');
  4104. roomspanel.querySelector('.show-add-muc-modal').click();
  4105. mock.closeControlBox(_converse);
  4106. const modal = _converse.api.modal.get('add-chatroom-modal');
  4107. await u.waitUntil(() => u.isVisible(modal.el), 1000)
  4108. const label_nick = modal.el.querySelector('label[for="nickname"]');
  4109. expect(label_nick.textContent.trim()).toBe('Nickname:');
  4110. const nick_input = modal.el.querySelector('input[name="nickname"]');
  4111. expect(nick_input.value).toBe('romeo');
  4112. done();
  4113. }));
  4114. it("uses the nickname passed in to converse.initialize",
  4115. mock.initConverse(['chatBoxesFetched'], {'nickname': 'st.nick'}, async function (done, _converse) {
  4116. await mock.openControlBox(_converse);
  4117. await mock.waitForRoster(_converse, 'current', 0);
  4118. const roomspanel = _converse.chatboxviews.get('controlbox').querySelector('converse-rooms-list');
  4119. roomspanel.querySelector('.show-add-muc-modal').click();
  4120. mock.closeControlBox(_converse);
  4121. const modal = _converse.api.modal.get('add-chatroom-modal');
  4122. await u.waitUntil(() => u.isVisible(modal.el), 1000)
  4123. const label_nick = modal.el.querySelector('label[for="nickname"]');
  4124. expect(label_nick.textContent.trim()).toBe('Nickname:');
  4125. const nick_input = modal.el.querySelector('input[name="nickname"]');
  4126. expect(nick_input.value).toBe('st.nick');
  4127. done();
  4128. }));
  4129. it("doesn't require the domain when muc_domain is set",
  4130. mock.initConverse(['chatBoxesFetched'], {'muc_domain': 'muc.example.org'}, async function (done, _converse) {
  4131. await mock.openControlBox(_converse);
  4132. const roomspanel = _converse.chatboxviews.get('controlbox').querySelector('converse-rooms-list');
  4133. roomspanel.querySelector('.show-add-muc-modal').click();
  4134. const modal = _converse.api.modal.get('add-chatroom-modal');
  4135. await u.waitUntil(() => u.isVisible(modal.el), 1000)
  4136. expect(modal.el.querySelector('.modal-title').textContent.trim()).toBe('Enter a new Groupchat');
  4137. spyOn(_converse.ChatRoom.prototype, 'getDiscoInfo').and.callFake(() => Promise.resolve());
  4138. roomspanel.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  4139. const label_name = modal.el.querySelector('label[for="chatroom"]');
  4140. expect(label_name.textContent.trim()).toBe('Groupchat name:');
  4141. let name_input = modal.el.querySelector('input[name="chatroom"]');
  4142. expect(name_input.placeholder).toBe('name@muc.example.org');
  4143. name_input.value = 'lounge';
  4144. let nick_input = modal.el.querySelector('input[name="nickname"]');
  4145. nick_input.value = 'max';
  4146. modal.el.querySelector('form input[type="submit"]').click();
  4147. await u.waitUntil(() => _converse.chatboxes.length);
  4148. await u.waitUntil(() => sizzle('.chatroom', _converse.el).filter(u.isVisible).length === 1);
  4149. expect(_.includes(_converse.chatboxes.models.map(m => m.get('id')), 'lounge@muc.example.org')).toBe(true);
  4150. // However, you can still open MUCs with different domains
  4151. roomspanel.querySelector('.show-add-muc-modal').click();
  4152. await u.waitUntil(() => u.isVisible(modal.el), 1000);
  4153. name_input = modal.el.querySelector('input[name="chatroom"]');
  4154. name_input.value = 'lounge@conference.example.org';
  4155. nick_input = modal.el.querySelector('input[name="nickname"]');
  4156. nick_input.value = 'max';
  4157. modal.el.querySelector('form input[type="submit"]').click();
  4158. await u.waitUntil(() => _converse.chatboxes.models.filter(c => c.get('type') === 'chatroom').length === 2);
  4159. await u.waitUntil(() => sizzle('.chatroom', _converse.el).filter(u.isVisible).length === 2);
  4160. expect(_.includes(_converse.chatboxes.models.map(m => m.get('id')), 'lounge@conference.example.org')).toBe(true);
  4161. done();
  4162. }));
  4163. it("only uses the muc_domain is locked_muc_domain is true",
  4164. mock.initConverse(['chatBoxesFetched'], {'muc_domain': 'muc.example.org', 'locked_muc_domain': true},
  4165. async function (done, _converse) {
  4166. await mock.openControlBox(_converse);
  4167. const roomspanel = _converse.chatboxviews.get('controlbox').querySelector('converse-rooms-list');
  4168. roomspanel.querySelector('.show-add-muc-modal').click();
  4169. const modal = _converse.api.modal.get('add-chatroom-modal');
  4170. await u.waitUntil(() => u.isVisible(modal.el), 1000)
  4171. expect(modal.el.querySelector('.modal-title').textContent.trim()).toBe('Enter a new Groupchat');
  4172. spyOn(_converse.ChatRoom.prototype, 'getDiscoInfo').and.callFake(() => Promise.resolve());
  4173. roomspanel.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  4174. const label_name = modal.el.querySelector('label[for="chatroom"]');
  4175. expect(label_name.textContent.trim()).toBe('Groupchat name:');
  4176. let name_input = modal.el.querySelector('input[name="chatroom"]');
  4177. expect(name_input.placeholder).toBe('');
  4178. name_input.value = 'lounge';
  4179. let nick_input = modal.el.querySelector('input[name="nickname"]');
  4180. nick_input.value = 'max';
  4181. modal.el.querySelector('form input[type="submit"]').click();
  4182. await u.waitUntil(() => _converse.chatboxes.length);
  4183. await u.waitUntil(() => sizzle('.chatroom', _converse.el).filter(u.isVisible).length === 1);
  4184. expect(_.includes(_converse.chatboxes.models.map(m => m.get('id')), 'lounge@muc.example.org')).toBe(true);
  4185. // However, you can still open MUCs with different domains
  4186. roomspanel.querySelector('.show-add-muc-modal').click();
  4187. await u.waitUntil(() => u.isVisible(modal.el), 1000);
  4188. name_input = modal.el.querySelector('input[name="chatroom"]');
  4189. name_input.value = 'lounge@conference';
  4190. nick_input = modal.el.querySelector('input[name="nickname"]');
  4191. nick_input.value = 'max';
  4192. modal.el.querySelector('form input[type="submit"]').click();
  4193. await u.waitUntil(() => _converse.chatboxes.models.filter(c => c.get('type') === 'chatroom').length === 2);
  4194. await u.waitUntil(() => sizzle('.chatroom', _converse.el).filter(u.isVisible).length === 2);
  4195. expect(_.includes(_converse.chatboxes.models.map(m => m.get('id')), 'lounge\\40conference@muc.example.org')).toBe(true);
  4196. done();
  4197. }));
  4198. });
  4199. describe("The \"Groupchats\" List modal", function () {
  4200. it("can be opened from a link in the \"Groupchats\" section of the controlbox",
  4201. mock.initConverse(['chatBoxesFetched'], {}, async function (done, _converse) {
  4202. await mock.openControlBox(_converse);
  4203. const roomspanel = _converse.chatboxviews.get('controlbox').querySelector('converse-rooms-list');
  4204. roomspanel.querySelector('.show-list-muc-modal').click();
  4205. mock.closeControlBox(_converse);
  4206. const modal = _converse.api.modal.get('muc-list-modal');
  4207. await u.waitUntil(() => u.isVisible(modal.el), 1000);
  4208. spyOn(_converse.ChatRoom.prototype, 'getDiscoInfo').and.callFake(() => Promise.resolve());
  4209. roomspanel.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  4210. // See: https://xmpp.org/extensions/xep-0045.html#disco-rooms
  4211. expect(modal.el.querySelectorAll('.available-chatrooms li').length).toBe(0);
  4212. const server_input = modal.el.querySelector('input[name="server"]');
  4213. expect(server_input.placeholder).toBe('conference.example.org');
  4214. server_input.value = 'chat.shakespeare.lit';
  4215. modal.el.querySelector('input[type="submit"]').click();
  4216. await u.waitUntil(() => _converse.chatboxes.length);
  4217. const IQ_stanzas = _converse.connection.IQ_stanzas;
  4218. const sent_stanza = await u.waitUntil(
  4219. () => IQ_stanzas.filter(s => sizzle(`query[xmlns="${Strophe.NS.DISCO_ITEMS}"]`, s).length).pop()
  4220. );
  4221. const id = sent_stanza.getAttribute('id');
  4222. expect(Strophe.serialize(sent_stanza)).toBe(
  4223. `<iq from="romeo@montague.lit/orchard" id="${id}" `+
  4224. `to="chat.shakespeare.lit" `+
  4225. `type="get" `+
  4226. `xmlns="jabber:client">`+
  4227. `<query xmlns="http://jabber.org/protocol/disco#items"/>`+
  4228. `</iq>`
  4229. );
  4230. const iq = $iq({
  4231. 'from':'muc.montague.lit',
  4232. 'to':'romeo@montague.lit/pda',
  4233. 'id': id,
  4234. 'type':'result'
  4235. }).c('query')
  4236. .c('item', { jid:'heath@chat.shakespeare.lit', name:'A Lonely Heath'}).up()
  4237. .c('item', { jid:'coven@chat.shakespeare.lit', name:'A Dark Cave'}).up()
  4238. .c('item', { jid:'forres@chat.shakespeare.lit', name:'The Palace'}).up()
  4239. .c('item', { jid:'inverness@chat.shakespeare.lit', name:'Macbeth&apos;s Castle'}).up()
  4240. .c('item', { jid:'orchard@chat.shakespeare.lit', name:'Capulet\'s Orchard'}).up()
  4241. .c('item', { jid:'friar@chat.shakespeare.lit', name:'Friar Laurence\'s cell'}).up()
  4242. .c('item', { jid:'hall@chat.shakespeare.lit', name:'Hall in Capulet\'s house'}).up()
  4243. .c('item', { jid:'chamber@chat.shakespeare.lit', name:'Juliet\'s chamber'}).up()
  4244. .c('item', { jid:'public@chat.shakespeare.lit', name:'A public place'}).up()
  4245. .c('item', { jid:'street@chat.shakespeare.lit', name:'A street'}).nodeTree;
  4246. _converse.connection._dataRecv(mock.createRequest(iq));
  4247. await u.waitUntil(() => modal.el.querySelectorAll('.available-chatrooms li').length === 11);
  4248. const rooms = modal.el.querySelectorAll('.available-chatrooms li');
  4249. expect(rooms[0].textContent.trim()).toBe("Groupchats found");
  4250. expect(rooms[1].textContent.trim()).toBe("A Lonely Heath");
  4251. expect(rooms[2].textContent.trim()).toBe("A Dark Cave");
  4252. expect(rooms[3].textContent.trim()).toBe("The Palace");
  4253. expect(rooms[4].textContent.trim()).toBe("Macbeth's Castle");
  4254. expect(rooms[5].textContent.trim()).toBe('Capulet\'s Orchard');
  4255. expect(rooms[6].textContent.trim()).toBe('Friar Laurence\'s cell');
  4256. expect(rooms[7].textContent.trim()).toBe('Hall in Capulet\'s house');
  4257. expect(rooms[8].textContent.trim()).toBe('Juliet\'s chamber');
  4258. expect(rooms[9].textContent.trim()).toBe('A public place');
  4259. expect(rooms[10].textContent.trim()).toBe('A street');
  4260. rooms[4].querySelector('.open-room').click();
  4261. await u.waitUntil(() => _converse.chatboxes.length > 1);
  4262. expect(sizzle('.chatroom', _converse.el).filter(u.isVisible).length).toBe(1); // There should now be an open chatroom
  4263. var view = _converse.chatboxviews.get('inverness@chat.shakespeare.lit');
  4264. expect(view.querySelector('.chatbox-title__text').textContent.trim()).toBe("Macbeth's Castle");
  4265. done();
  4266. }));
  4267. it("is pre-filled with the muc_domain",
  4268. mock.initConverse(
  4269. ['chatBoxesFetched'],
  4270. {'muc_domain': 'muc.example.org'},
  4271. async function (done, _converse) {
  4272. await mock.openControlBox(_converse);
  4273. const roomspanel = _converse.chatboxviews.get('controlbox').querySelector('converse-rooms-list');
  4274. roomspanel.querySelector('.show-list-muc-modal').click();
  4275. mock.closeControlBox(_converse);
  4276. const modal = _converse.api.modal.get('muc-list-modal');
  4277. await u.waitUntil(() => u.isVisible(modal.el), 1000);
  4278. const server_input = modal.el.querySelector('input[name="server"]');
  4279. expect(server_input.value).toBe('muc.example.org');
  4280. done();
  4281. }));
  4282. it("doesn't let you set the MUC domain if it's locked",
  4283. mock.initConverse(
  4284. ['chatBoxesFetched'],
  4285. {'muc_domain': 'chat.shakespeare.lit', 'locked_muc_domain': true},
  4286. async function (done, _converse) {
  4287. await mock.openControlBox(_converse);
  4288. const roomspanel = _converse.chatboxviews.get('controlbox').querySelector('converse-rooms-list');
  4289. roomspanel.querySelector('.show-list-muc-modal').click();
  4290. mock.closeControlBox(_converse);
  4291. const modal = _converse.api.modal.get('muc-list-modal');
  4292. await u.waitUntil(() => u.isVisible(modal.el), 1000);
  4293. spyOn(_converse.ChatRoom.prototype, 'getDiscoInfo').and.callFake(() => Promise.resolve());
  4294. roomspanel.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  4295. expect(modal.el.querySelector('input[name="server"]')).toBe(null);
  4296. expect(modal.el.querySelector('input[type="submit"]')).toBe(null);
  4297. await u.waitUntil(() => _converse.chatboxes.length);
  4298. const sent_stanza = await u.waitUntil(() =>
  4299. _converse.connection.sent_stanzas.filter(
  4300. s => sizzle(`query[xmlns="http://jabber.org/protocol/disco#items"]`, s).length).pop()
  4301. );
  4302. expect(Strophe.serialize(sent_stanza)).toBe(
  4303. `<iq from="romeo@montague.lit/orchard" id="${sent_stanza.getAttribute('id')}" `+
  4304. `to="chat.shakespeare.lit" type="get" xmlns="jabber:client">`+
  4305. `<query xmlns="http://jabber.org/protocol/disco#items"/>`+
  4306. `</iq>`
  4307. );
  4308. const iq = $iq({
  4309. from:'muc.montague.lit',
  4310. to:'romeo@montague.lit/pda',
  4311. id: sent_stanza.getAttribute('id'),
  4312. type:'result'
  4313. }).c('query')
  4314. .c('item', { jid:'heath@chat.shakespeare.lit', name:'A Lonely Heath'}).up()
  4315. .c('item', { jid:'coven@chat.shakespeare.lit', name:'A Dark Cave'}).up()
  4316. .c('item', { jid:'forres@chat.shakespeare.lit', name:'The Palace'}).up()
  4317. _converse.connection._dataRecv(mock.createRequest(iq));
  4318. await u.waitUntil(() => modal.el.querySelectorAll('.available-chatrooms li').length === 4);
  4319. const rooms = modal.el.querySelectorAll('.available-chatrooms li');
  4320. expect(rooms[0].textContent.trim()).toBe("Groupchats found");
  4321. expect(rooms[1].textContent.trim()).toBe("A Lonely Heath");
  4322. expect(rooms[2].textContent.trim()).toBe("A Dark Cave");
  4323. expect(rooms[3].textContent.trim()).toBe("The Palace");
  4324. done();
  4325. }));
  4326. });
  4327. describe("The \"Groupchats\" section", function () {
  4328. it("shows the number of unread mentions received",
  4329. mock.initConverse(
  4330. [], {'allow_bookmarks': false},
  4331. async function (done, _converse) {
  4332. await mock.openControlBox(_converse);
  4333. const roomspanel = _converse.chatboxviews.get('controlbox').querySelector('converse-rooms-list');
  4334. expect(roomspanel.querySelectorAll('.available-room').length).toBe(0);
  4335. const muc_jid = 'kitchen@conference.shakespeare.lit';
  4336. const message = 'fires: Your attention is required';
  4337. await mock.openAndEnterChatRoom(_converse, muc_jid, 'fires');
  4338. const view = _converse.api.chatviews.get(muc_jid);
  4339. await u.waitUntil(() => roomspanel.querySelectorAll('.available-room').length);
  4340. expect(roomspanel.querySelectorAll('.available-room').length).toBe(1);
  4341. expect(roomspanel.querySelectorAll('.msgs-indicator').length).toBe(0);
  4342. view.model.set({'minimized': true});
  4343. const nick = mock.chatroom_names[0];
  4344. await view.model.handleMessageStanza($msg({
  4345. from: muc_jid+'/'+nick,
  4346. id: u.getUniqueId(),
  4347. to: 'romeo@montague.lit',
  4348. type: 'groupchat'
  4349. }).c('body').t(message).tree());
  4350. await u.waitUntil(() => view.model.messages.length);
  4351. expect(roomspanel.querySelectorAll('.available-room').length).toBe(1);
  4352. expect(roomspanel.querySelectorAll('.msgs-indicator').length).toBe(1);
  4353. expect(roomspanel.querySelector('.msgs-indicator').textContent.trim()).toBe('1');
  4354. await view.model.handleMessageStanza($msg({
  4355. 'from': muc_jid+'/'+nick,
  4356. 'id': u.getUniqueId(),
  4357. 'to': 'romeo@montague.lit',
  4358. 'type': 'groupchat'
  4359. }).c('body').t(message).tree());
  4360. await u.waitUntil(() => view.model.messages.length > 1);
  4361. expect(roomspanel.querySelectorAll('.available-room').length).toBe(1);
  4362. expect(roomspanel.querySelectorAll('.msgs-indicator').length).toBe(1);
  4363. expect(roomspanel.querySelector('.msgs-indicator').textContent.trim()).toBe('2');
  4364. view.model.set({'minimized': false});
  4365. expect(roomspanel.querySelectorAll('.available-room').length).toBe(1);
  4366. expect(roomspanel.querySelectorAll('.msgs-indicator').length).toBe(0);
  4367. done();
  4368. }));
  4369. });
  4370. describe("A XEP-0085 Chat Status Notification", function () {
  4371. it("is is not sent out to a MUC if the user is a visitor in a moderated room",
  4372. mock.initConverse(
  4373. ['chatBoxesFetched'], {},
  4374. async function (done, _converse) {
  4375. spyOn(_converse.ChatRoom.prototype, 'sendChatState').and.callThrough();
  4376. const muc_jid = 'lounge@montague.lit';
  4377. const features = [
  4378. 'http://jabber.org/protocol/muc',
  4379. 'jabber:iq:register',
  4380. 'muc_passwordprotected',
  4381. 'muc_hidden',
  4382. 'muc_temporary',
  4383. 'muc_membersonly',
  4384. 'muc_moderated',
  4385. 'muc_anonymous'
  4386. ]
  4387. await mock.openAndEnterChatRoom(_converse, muc_jid, 'romeo', features);
  4388. const view = _converse.api.chatviews.get(muc_jid);
  4389. view.model.setChatState(_converse.ACTIVE);
  4390. expect(view.model.sendChatState).toHaveBeenCalled();
  4391. const last_stanza = _converse.connection.sent_stanzas.pop();
  4392. expect(Strophe.serialize(last_stanza)).toBe(
  4393. `<message to="lounge@montague.lit" type="groupchat" xmlns="jabber:client">`+
  4394. `<active xmlns="http://jabber.org/protocol/chatstates"/>`+
  4395. `<no-store xmlns="urn:xmpp:hints"/>`+
  4396. `<no-permanent-store xmlns="urn:xmpp:hints"/>`+
  4397. `</message>`);
  4398. // Romeo loses his voice
  4399. const presence = $pres({
  4400. to: 'romeo@montague.lit/orchard',
  4401. from: `${muc_jid}/romeo`
  4402. }).c('x', {xmlns: Strophe.NS.MUC_USER})
  4403. .c('item', {'affiliation': 'none', 'role': 'visitor'}).up()
  4404. .c('status', {code: '110'});
  4405. _converse.connection._dataRecv(mock.createRequest(presence));
  4406. const occupant = view.model.occupants.findWhere({'jid': _converse.bare_jid});
  4407. await u.waitUntil(() => occupant.get('role') === 'visitor');
  4408. spyOn(_converse.connection, 'send');
  4409. view.model.setChatState(_converse.INACTIVE);
  4410. expect(view.model.sendChatState.calls.count()).toBe(2);
  4411. expect(_converse.connection.send).not.toHaveBeenCalled();
  4412. done();
  4413. }));
  4414. describe("A composing notification", function () {
  4415. it("will be shown if received", mock.initConverse([], {}, async function (done, _converse) {
  4416. const muc_jid = 'coven@chat.shakespeare.lit';
  4417. const members = [
  4418. {'affiliation': 'member', 'nick': 'majortom', 'jid': 'majortom@example.org'},
  4419. {'affiliation': 'admin', 'nick': 'groundcontrol', 'jid': 'groundcontrol@example.org'}
  4420. ];
  4421. await mock.openAndEnterChatRoom(_converse, muc_jid, 'some1', [], members);
  4422. const view = _converse.api.chatviews.get(muc_jid);
  4423. let csntext = await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent);
  4424. expect(csntext.trim()).toEqual("some1 has entered the groupchat");
  4425. let presence = $pres({
  4426. to: 'romeo@montague.lit/_converse.js-29092160',
  4427. from: 'coven@chat.shakespeare.lit/newguy'
  4428. })
  4429. .c('x', {xmlns: Strophe.NS.MUC_USER})
  4430. .c('item', {
  4431. 'affiliation': 'none',
  4432. 'jid': 'newguy@montague.lit/_converse.js-290929789',
  4433. 'role': 'participant'
  4434. });
  4435. _converse.connection._dataRecv(mock.createRequest(presence));
  4436. await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent.trim() ===
  4437. "some1 and newguy have entered the groupchat");
  4438. presence = $pres({
  4439. to: 'romeo@montague.lit/_converse.js-29092160',
  4440. from: 'coven@chat.shakespeare.lit/nomorenicks'
  4441. })
  4442. .c('x', {xmlns: Strophe.NS.MUC_USER})
  4443. .c('item', {
  4444. 'affiliation': 'none',
  4445. 'jid': 'nomorenicks@montague.lit/_converse.js-290929789',
  4446. 'role': 'participant'
  4447. });
  4448. _converse.connection._dataRecv(mock.createRequest(presence));
  4449. await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent.trim() ===
  4450. "some1, newguy and nomorenicks have entered the groupchat", 1000);
  4451. // Manually clear so that we can more easily test
  4452. view.model.notifications.set('entered', []);
  4453. await u.waitUntil(() => !view.querySelector('.chat-content__notifications').textContent, 1000);
  4454. // See XEP-0085 https://xmpp.org/extensions/xep-0085.html#definitions
  4455. const remove_notifications_timeouts = [];
  4456. const setTimeout = window.setTimeout;
  4457. spyOn(window, 'setTimeout').and.callFake((f, w) => {
  4458. if (f.toString() === "() => this.removeNotification(actor, state)") {
  4459. remove_notifications_timeouts.push(f)
  4460. }
  4461. setTimeout(f, w);
  4462. });
  4463. // <composing> state
  4464. let msg = $msg({
  4465. from: muc_jid+'/newguy',
  4466. id: u.getUniqueId(),
  4467. to: 'romeo@montague.lit',
  4468. type: 'groupchat'
  4469. }).c('body').c('composing', {'xmlns': Strophe.NS.CHATSTATES}).tree();
  4470. _converse.connection._dataRecv(mock.createRequest(msg));
  4471. csntext = await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent, 1000);
  4472. expect(csntext.trim()).toEqual('newguy is typing');
  4473. expect(remove_notifications_timeouts.length).toBe(1);
  4474. expect(view.querySelector('.chat-content__notifications').textContent.trim()).toEqual('newguy is typing');
  4475. // <composing> state for a different occupant
  4476. msg = $msg({
  4477. from: muc_jid+'/nomorenicks',
  4478. id: u.getUniqueId(),
  4479. to: 'romeo@montague.lit',
  4480. type: 'groupchat'
  4481. }).c('body').c('composing', {'xmlns': Strophe.NS.CHATSTATES}).tree();
  4482. await view.model.handleMessageStanza(msg);
  4483. await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent.trim() === 'newguy and nomorenicks are typing', 1000);
  4484. // <composing> state for a different occupant
  4485. msg = $msg({
  4486. from: muc_jid+'/majortom',
  4487. id: u.getUniqueId(),
  4488. to: 'romeo@montague.lit',
  4489. type: 'groupchat'
  4490. }).c('body').c('composing', {'xmlns': Strophe.NS.CHATSTATES}).tree();
  4491. await view.model.handleMessageStanza(msg);
  4492. await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent.trim() === 'newguy, nomorenicks and majortom are typing', 1000);
  4493. // <composing> state for a different occupant
  4494. msg = $msg({
  4495. from: muc_jid+'/groundcontrol',
  4496. id: u.getUniqueId(),
  4497. to: 'romeo@montague.lit',
  4498. type: 'groupchat'
  4499. }).c('body').c('composing', {'xmlns': Strophe.NS.CHATSTATES}).tree();
  4500. await view.model.handleMessageStanza(msg);
  4501. await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent.trim() === 'newguy, nomorenicks and others are typing', 1000);
  4502. msg = $msg({
  4503. from: `${muc_jid}/some1`,
  4504. id: u.getUniqueId(),
  4505. to: 'romeo@montague.lit',
  4506. type: 'groupchat'
  4507. }).c('body').t('hello world').tree();
  4508. await view.model.handleMessageStanza(msg);
  4509. await u.waitUntil(() => view.querySelectorAll('.chat-msg').length === 1);
  4510. expect(view.querySelector('.chat-msg .chat-msg__text').textContent.trim()).toBe('hello world');
  4511. // Test that the composing notifications get removed via timeout.
  4512. if (remove_notifications_timeouts.length) {
  4513. remove_notifications_timeouts[0]();
  4514. }
  4515. await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent.trim() === 'nomorenicks, majortom and groundcontrol are typing', 1000);
  4516. done();
  4517. }));
  4518. });
  4519. describe("A paused notification", function () {
  4520. it("will be shown if received", mock.initConverse(['chatBoxesFetched'], {}, async function (done, _converse) {
  4521. const muc_jid = 'coven@chat.shakespeare.lit';
  4522. await mock.openAndEnterChatRoom(_converse, muc_jid, 'some1');
  4523. const view = _converse.chatboxviews.get('coven@chat.shakespeare.lit');
  4524. /* <presence to="romeo@montague.lit/_converse.js-29092160"
  4525. * from="coven@chat.shakespeare.lit/some1">
  4526. * <x xmlns="http://jabber.org/protocol/muc#user">
  4527. * <item affiliation="owner" jid="romeo@montague.lit/_converse.js-29092160" role="moderator"/>
  4528. * <status code="110"/>
  4529. * </x>
  4530. * </presence></body>
  4531. */
  4532. let presence = $pres({
  4533. to: 'romeo@montague.lit/_converse.js-29092160',
  4534. from: 'coven@chat.shakespeare.lit/some1'
  4535. }).c('x', {xmlns: Strophe.NS.MUC_USER})
  4536. .c('item', {
  4537. 'affiliation': 'owner',
  4538. 'jid': 'romeo@montague.lit/_converse.js-29092160',
  4539. 'role': 'moderator'
  4540. }).up()
  4541. .c('status', {code: '110'});
  4542. _converse.connection._dataRecv(mock.createRequest(presence));
  4543. const csntext = await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent);
  4544. expect(csntext.trim()).toEqual("some1 has entered the groupchat");
  4545. presence = $pres({
  4546. to: 'romeo@montague.lit/_converse.js-29092160',
  4547. from: 'coven@chat.shakespeare.lit/newguy'
  4548. })
  4549. .c('x', {xmlns: Strophe.NS.MUC_USER})
  4550. .c('item', {
  4551. 'affiliation': 'none',
  4552. 'jid': 'newguy@montague.lit/_converse.js-290929789',
  4553. 'role': 'participant'
  4554. });
  4555. _converse.connection._dataRecv(mock.createRequest(presence));
  4556. await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent.trim() ===
  4557. "some1 and newguy have entered the groupchat");
  4558. presence = $pres({
  4559. to: 'romeo@montague.lit/_converse.js-29092160',
  4560. from: 'coven@chat.shakespeare.lit/nomorenicks'
  4561. })
  4562. .c('x', {xmlns: Strophe.NS.MUC_USER})
  4563. .c('item', {
  4564. 'affiliation': 'none',
  4565. 'jid': 'nomorenicks@montague.lit/_converse.js-290929789',
  4566. 'role': 'participant'
  4567. });
  4568. _converse.connection._dataRecv(mock.createRequest(presence));
  4569. await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent.trim() ===
  4570. "some1, newguy and nomorenicks have entered the groupchat");
  4571. // Manually clear so that we can more easily test
  4572. view.model.notifications.set('entered', []);
  4573. await u.waitUntil(() => !view.querySelector('.chat-content__notifications').textContent);
  4574. // See XEP-0085 https://xmpp.org/extensions/xep-0085.html#definitions
  4575. // <composing> state
  4576. var msg = $msg({
  4577. from: muc_jid+'/newguy',
  4578. id: u.getUniqueId(),
  4579. to: 'romeo@montague.lit',
  4580. type: 'groupchat'
  4581. }).c('body').c('composing', {'xmlns': Strophe.NS.CHATSTATES}).tree();
  4582. await view.model.handleMessageStanza(msg);
  4583. await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent);
  4584. expect(view.querySelector('.chat-content__notifications').textContent.trim()).toBe('newguy is typing');
  4585. // <composing> state for a different occupant
  4586. msg = $msg({
  4587. from: muc_jid+'/nomorenicks',
  4588. id: u.getUniqueId(),
  4589. to: 'romeo@montague.lit',
  4590. type: 'groupchat'
  4591. }).c('body').c('composing', {'xmlns': Strophe.NS.CHATSTATES}).tree();
  4592. await view.model.handleMessageStanza(msg);
  4593. await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent.trim() == 'newguy and nomorenicks are typing');
  4594. // <paused> state from occupant who typed first
  4595. msg = $msg({
  4596. from: muc_jid+'/newguy',
  4597. id: u.getUniqueId(),
  4598. to: 'romeo@montague.lit',
  4599. type: 'groupchat'
  4600. }).c('body').c('paused', {'xmlns': Strophe.NS.CHATSTATES}).tree();
  4601. await view.model.handleMessageStanza(msg);
  4602. await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent.trim() == 'nomorenicks is typing\nnewguy has stopped typing');
  4603. done();
  4604. }));
  4605. });
  4606. });
  4607. describe("A muted user", function () {
  4608. it("will receive a user-friendly error message when trying to send a message",
  4609. mock.initConverse(['chatBoxesFetched'], {}, async function (done, _converse) {
  4610. const muc_jid = 'trollbox@montague.lit';
  4611. await mock.openAndEnterChatRoom(_converse, muc_jid, 'troll');
  4612. const view = _converse.api.chatviews.get(muc_jid);
  4613. const textarea = view.querySelector('.chat-textarea');
  4614. textarea.value = 'Hello world';
  4615. view.onFormSubmitted(new Event('submit'));
  4616. await new Promise(resolve => view.model.messages.once('rendered', resolve));
  4617. let stanza = u.toStanza(`
  4618. <message id="${view.model.messages.at(0).get('msgid')}"
  4619. xmlns="jabber:client"
  4620. type="error"
  4621. to="troll@montague.lit/resource"
  4622. from="trollbox@montague.lit">
  4623. <error type="auth"><forbidden xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"/></error>
  4624. </message>`);
  4625. _converse.connection._dataRecv(mock.createRequest(stanza));
  4626. await u.waitUntil(() => view.querySelector('.chat-msg__error')?.textContent.trim(), 1000);
  4627. expect(view.querySelector('.chat-msg__error').textContent.trim()).toBe(
  4628. "Your message was not delivered because you weren't allowed to send it.");
  4629. textarea.value = 'Hello again';
  4630. view.onFormSubmitted(new Event('submit'));
  4631. await u.waitUntil(() => view.querySelectorAll('.chat-msg__text').length === 2);
  4632. stanza = u.toStanza(`
  4633. <message id="${view.model.messages.at(1).get('msgid')}"
  4634. xmlns="jabber:client"
  4635. type="error"
  4636. to="troll@montague.lit/resource"
  4637. from="trollbox@montague.lit">
  4638. <error type="auth">
  4639. <forbidden xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"/>
  4640. <text xmlns="urn:ietf:params:xml:ns:xmpp-stanzas">Thou shalt not!</text>
  4641. </error>
  4642. </message>`);
  4643. _converse.connection._dataRecv(mock.createRequest(stanza));
  4644. await u.waitUntil(() => view.querySelectorAll('.chat-msg__error').length === 2);
  4645. const sel = 'converse-message-history converse-chat-message:last-child .chat-msg__error';
  4646. await u.waitUntil(() => view.querySelector(sel)?.textContent.trim());
  4647. expect(view.querySelector(sel).textContent.trim()).toBe('Thou shalt not!')
  4648. done();
  4649. }));
  4650. it("will see an explanatory message instead of a textarea",
  4651. mock.initConverse(['chatBoxesFetched'], {}, async function (done, _converse) {
  4652. const features = [
  4653. 'http://jabber.org/protocol/muc',
  4654. 'jabber:iq:register',
  4655. Strophe.NS.SID,
  4656. 'muc_moderated',
  4657. ]
  4658. const muc_jid = 'trollbox@montague.lit';
  4659. await mock.openAndEnterChatRoom(_converse, muc_jid, 'troll', features);
  4660. const view = _converse.api.chatviews.get(muc_jid);
  4661. expect(_.isNull(view.querySelector('.chat-textarea'))).toBe(false);
  4662. let stanza = u.toStanza(`
  4663. <presence
  4664. from='trollbox@montague.lit/troll'
  4665. to='romeo@montague.lit/orchard'>
  4666. <x xmlns='http://jabber.org/protocol/muc#user'>
  4667. <item affiliation='none'
  4668. nick='troll'
  4669. role='visitor'/>
  4670. <status code='110'/>
  4671. </x>
  4672. </presence>`);
  4673. _converse.connection._dataRecv(mock.createRequest(stanza));
  4674. await u.waitUntil(() => view.querySelector('.chat-textarea') === null);
  4675. let bottom_panel = view.querySelector('.muc-bottom-panel');
  4676. expect(bottom_panel.textContent.trim()).toBe("You're not allowed to send messages in this room");
  4677. // This only applies to moderated rooms, so let's check that
  4678. // the textarea becomes visible when the room's
  4679. // configuration changes to be non-moderated
  4680. view.model.features.set('moderated', false);
  4681. expect(view.querySelector('.muc-bottom-panel')).toBe(null);
  4682. let textarea = view.querySelector('.chat-textarea');
  4683. expect(textarea === null).toBe(false);
  4684. view.model.features.set('moderated', true);
  4685. expect(view.querySelector('.chat-textarea')).toBe(null);
  4686. bottom_panel = view.querySelector('.muc-bottom-panel');
  4687. expect(bottom_panel.textContent.trim()).toBe("You're not allowed to send messages in this room");
  4688. // Check now that things get restored when the user is given a voice
  4689. await u.waitUntil(() =>
  4690. Array.from(view.querySelectorAll('.chat-info__message')).pop()?.textContent.trim() ===
  4691. "troll is no longer an owner of this groupchat"
  4692. );
  4693. stanza = u.toStanza(`
  4694. <presence
  4695. from='trollbox@montague.lit/troll'
  4696. to='romeo@montague.lit/orchard'>
  4697. <x xmlns='http://jabber.org/protocol/muc#user'>
  4698. <item affiliation='none'
  4699. nick='troll'
  4700. role='participant'/>
  4701. <status code='110'/>
  4702. </x>
  4703. </presence>`);
  4704. _converse.connection._dataRecv(mock.createRequest(stanza));
  4705. await u.waitUntil(() => view.querySelector('.muc-bottom-panel') === null);
  4706. textarea = view.querySelector('.chat-textarea');
  4707. expect(textarea === null).toBe(false);
  4708. // Check now that things get restored when the user is given a voice
  4709. await u.waitUntil(() => view.querySelector('.chat-content__notifications').textContent.trim() === "troll has been given a voice");
  4710. done();
  4711. }));
  4712. });
  4713. describe("when muc_send_probes is true", function () {
  4714. it("sends presence probes when muc_send_probes is true",
  4715. mock.initConverse([], {'muc_send_probes': true}, async function (done, _converse) {
  4716. const muc_jid = 'lounge@montague.lit';
  4717. await mock.openAndEnterChatRoom(_converse, muc_jid, 'romeo');
  4718. let stanza = u.toStanza(`
  4719. <message xmlns="jabber:client" to="${_converse.jid}" type="groupchat" from="${muc_jid}/ralphm">
  4720. <body>This message will trigger a presence probe</body>
  4721. </message>`);
  4722. _converse.connection._dataRecv(mock.createRequest(stanza));
  4723. const view = _converse.chatboxviews.get(muc_jid);
  4724. await u.waitUntil(() => view.model.messages.length);
  4725. let occupant = view.model.messages.at(0)?.occupant;
  4726. expect(occupant).toBeDefined();
  4727. expect(occupant.get('nick')).toBe('ralphm');
  4728. expect(occupant.get('affiliation')).toBeUndefined();
  4729. expect(occupant.get('role')).toBeUndefined();
  4730. const sent_stanzas = _converse.connection.sent_stanzas;
  4731. let probe = await u.waitUntil(() => sent_stanzas.filter(s => s.matches('presence[type="probe"]')).pop());
  4732. expect(Strophe.serialize(probe)).toBe(
  4733. `<presence to="${muc_jid}/ralphm" type="probe" xmlns="jabber:client">`+
  4734. `<priority>0</priority>`+
  4735. `<c hash="sha-1" node="https://conversejs.org" ver="PxXfr6uz8ClMWIga0OB/MhKNH/M=" xmlns="http://jabber.org/protocol/caps"/>`+
  4736. `</presence>`);
  4737. let presence = u.toStanza(
  4738. `<presence xmlns="jabber:client" to="${converse.jid}" from="${muc_jid}/ralphm">
  4739. <x xmlns="http://jabber.org/protocol/muc#user">
  4740. <item affiliation="member" jid="ralph@example.org/Conversations.ZvLu" role="participant"/>
  4741. </x>
  4742. </presence>`);
  4743. _converse.connection._dataRecv(mock.createRequest(presence));
  4744. expect(occupant.get('affiliation')).toBe('member');
  4745. expect(occupant.get('role')).toBe('participant');
  4746. // Check that unavailable but affiliated occupants don't get destroyed
  4747. stanza = u.toStanza(`
  4748. <message xmlns="jabber:client" to="${_converse.jid}" type="groupchat" from="${muc_jid}/gonePhising">
  4749. <body>This message from an unavailable user will trigger a presence probe</body>
  4750. </message>`);
  4751. _converse.connection._dataRecv(mock.createRequest(stanza));
  4752. await u.waitUntil(() => view.model.messages.length === 2);
  4753. occupant = view.model.messages.at(1)?.occupant;
  4754. expect(occupant).toBeDefined();
  4755. expect(occupant.get('nick')).toBe('gonePhising');
  4756. expect(occupant.get('affiliation')).toBeUndefined();
  4757. expect(occupant.get('role')).toBeUndefined();
  4758. probe = await u.waitUntil(() => sent_stanzas.filter(s => s.matches(`presence[to="${muc_jid}/gonePhising"]`)).pop());
  4759. expect(Strophe.serialize(probe)).toBe(
  4760. `<presence to="${muc_jid}/gonePhising" type="probe" xmlns="jabber:client">`+
  4761. `<priority>0</priority>`+
  4762. `<c hash="sha-1" node="https://conversejs.org" ver="PxXfr6uz8ClMWIga0OB/MhKNH/M=" xmlns="http://jabber.org/protocol/caps"/>`+
  4763. `</presence>`);
  4764. presence = u.toStanza(
  4765. `<presence xmlns="jabber:client" type="unavailable" to="${converse.jid}" from="${muc_jid}/gonePhising">
  4766. <x xmlns="http://jabber.org/protocol/muc#user">
  4767. <item affiliation="member" jid="gonePhishing@example.org/d34dBEEF" role="participant"/>
  4768. </x>
  4769. </presence>`);
  4770. _converse.connection._dataRecv(mock.createRequest(presence));
  4771. expect(view.model.occupants.length).toBe(3);
  4772. expect(occupant.get('affiliation')).toBe('member');
  4773. expect(occupant.get('role')).toBe('participant');
  4774. done();
  4775. }));
  4776. });
  4777. });