html.c 213 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970
  1. /* snac - A simple, minimalistic ActivityPub instance */
  2. /* copyright (c) 2022 - 2026 grunfink et al. / MIT license */
  3. #include "xs.h"
  4. #include "xs_io.h"
  5. #include "xs_json.h"
  6. #include "xs_regex.h"
  7. #include "xs_set.h"
  8. #include "xs_openssl.h"
  9. #include "xs_time.h"
  10. #include "xs_mime.h"
  11. #include "xs_match.h"
  12. #include "xs_html.h"
  13. #include "xs_curl.h"
  14. #include "xs_unicode.h"
  15. #include "xs_url.h"
  16. #include "xs_random.h"
  17. #include "xs_http.h"
  18. #include "xs_list_tools.h"
  19. #include "snac.h"
  20. int login(snac *user, const xs_dict *headers)
  21. /* tries a login */
  22. {
  23. int logged_in = 0;
  24. const char *auth = xs_dict_get(headers, "authorization");
  25. if (auth && xs_startswith(auth, "Basic ")) {
  26. int sz;
  27. xs *s1 = xs_crop_i(xs_dup(auth), 6, 0);
  28. xs *s2 = xs_base64_dec(s1, &sz);
  29. xs *l1 = xs_split_n(s2, ":", 1);
  30. if (xs_list_len(l1) == 2) {
  31. const char *uid = xs_list_get(l1, 0);
  32. const char *pwd = xs_list_get(l1, 1);
  33. const char *addr = xs_or(xs_dict_get(headers, "remote-addr"),
  34. xs_dict_get(headers, "x-forwarded-for"));
  35. if (badlogin_check(uid, addr)) {
  36. logged_in = check_password(uid, pwd,
  37. xs_dict_get(user->config, "passwd"));
  38. if (!logged_in)
  39. badlogin_inc(uid, addr);
  40. }
  41. }
  42. }
  43. if (logged_in)
  44. lastlog_write(user, "web");
  45. return logged_in;
  46. }
  47. xs_str *_replace_shortnames(xs_str *s, const xs_list *tag, int ems,
  48. const char *proxy, const xs_list *cl, const char *act_o)
  49. /* replace but also adds a class list and an actor in its alt text.
  50. * Used for emoji reactions */
  51. {
  52. if (!xs_is_null(tag)) {
  53. xs *tag_list = NULL;
  54. if (xs_type(tag) == XSTYPE_DICT) {
  55. /* not a list */
  56. tag_list = xs_list_new();
  57. tag_list = xs_list_append(tag_list, tag);
  58. } else {
  59. /* is a list */
  60. tag_list = xs_dup(tag);
  61. }
  62. xs *style = xs_fmt("max-height: %dem; max-width: %dem;", ems, ems);
  63. xs *class = xs_fmt("snac-emoji snac-emoji-%d-em", ems);
  64. if (cl) {
  65. xs *l = xs_join(cl, " ");
  66. class = xs_str_cat(class, " ", l);
  67. }
  68. int c = 0;
  69. const xs_val *v;
  70. c = 0;
  71. xs_set rep_emoji;
  72. xs_set_init(&rep_emoji);
  73. while (xs_list_next(tag_list, &v, &c)) {
  74. const char *t = xs_dict_get(v, "type");
  75. if (t && strcmp(t, "Emoji") == 0) {
  76. const char *n = xs_dict_get(v, "name");
  77. const xs_dict *i = xs_dict_get(v, "icon");
  78. /* avoid repeated emojis (Misskey seems to return this) */
  79. if (xs_set_add(&rep_emoji, n) == 0)
  80. continue;
  81. if (xs_is_string(n) && xs_is_dict(i)) {
  82. const char *u = xs_dict_get(i, "url");
  83. const char *mt = xs_dict_get(i, "mediaType");
  84. if (xs_is_string(u)) {
  85. // on akkoma instances mediaType is not present.
  86. // but we need to to know if the image is an svg or not.
  87. // for now, i just use the file extention, which may not be the most reliable...
  88. if (!xs_is_string(mt))
  89. mt = xs_mime_by_ext(u);
  90. xs *act = act_o ? xs_fmt("%s\n%s", n, act_o) : xs_fmt("%s", n);
  91. if (strcmp(mt, "image/svg+xml") == 0 && !xs_is_true(xs_dict_get(srv_config, "enable_svg")))
  92. s = xs_replace_i(s, n, "");
  93. else {
  94. xs *url = make_url(u, proxy, 0);
  95. xs_html *img = xs_html_sctag("img",
  96. xs_html_attr("loading", "lazy"),
  97. xs_html_attr("src", url),
  98. xs_html_attr("alt", act),
  99. xs_html_attr("title", act),
  100. xs_html_attr("class", class),
  101. xs_html_attr("style", style));
  102. xs *s1 = xs_html_render(img);
  103. s = xs_replace_i(s, n, s1);
  104. }
  105. }
  106. else
  107. s = xs_replace_i(s, n, "");
  108. }
  109. }
  110. }
  111. xs_set_free(&rep_emoji);
  112. }
  113. return s;
  114. }
  115. xs_str *replace_shortnames(xs_str *s, const xs_list *tag, int ems, const char *proxy)
  116. /* replaces all the :shortnames: with the emojis in tag */
  117. {
  118. return _replace_shortnames(s, tag, ems, proxy, NULL, NULL);
  119. }
  120. xs_str *actor_name(xs_dict *actor, const char *proxy)
  121. /* gets the actor name */
  122. {
  123. const char *v;
  124. if (xs_is_null((v = xs_dict_get(actor, "name"))) || *v == '\0') {
  125. if (xs_is_null(v = xs_dict_get(actor, "preferredUsername")) || *v == '\0') {
  126. v = "anonymous";
  127. }
  128. }
  129. return replace_shortnames(xs_html_encode(v), xs_dict_get(actor, "tag"), 1, proxy);
  130. }
  131. xs_str *actor_pronouns(xs_dict *actor)
  132. /* gets the actor name */
  133. {
  134. const xs_list *attachment;
  135. const xs_dict *d;
  136. const char *pronouns = "";
  137. char *ret;
  138. if (!xs_is_null((attachment = xs_dict_get(actor, "attachment")))) {
  139. xs_list_foreach(attachment, d) {
  140. char *prop = xs_utf8_to_lower(xs_dict_get(d, "name"));
  141. /* make sure that we are reading the correct metadata */
  142. if (strlen(prop) == 8 && strcmp(prop, "pronouns") == 0)
  143. pronouns = xs_dict_get(d, "value");
  144. }
  145. }
  146. /* <p> breaks page, cannot nest them */
  147. ret = xs_replace_i(xs_dup(pronouns), "<p>", "");
  148. ret = xs_replace_i(ret, "</p>", "");
  149. return ret;
  150. }
  151. xs_str *format_text_with_emoji(snac *user, const char *text, int ems, const char *proxy)
  152. /* needed when we have local text with no tags attached */
  153. {
  154. xs *tags = xs_list_new();
  155. xs *name1 = not_really_markdown(text, NULL, &tags);
  156. xs_str *name3;
  157. if (user) {
  158. xs *name2 = process_tags(user, name1, &tags);
  159. name3 = sanitize(name2);
  160. }
  161. else {
  162. name3 = sanitize(name1);
  163. name3 = xs_replace_i(name3, "<br>", "");
  164. }
  165. return replace_shortnames(name3, tags, ems, proxy);
  166. }
  167. xs_html *html_actor_icon(snac *user, xs_dict *actor, const char *date,
  168. const char *udate, const char *url, int scope,
  169. int in_people, const char *proxy, const char *lang,
  170. const char *md5)
  171. {
  172. xs_html *actor_icon = xs_html_tag("p", NULL);
  173. xs *avatar = NULL;
  174. const char *v;
  175. int fwing = 0;
  176. int fwer = 0;
  177. xs *name = actor_name(actor, proxy);
  178. xs *pronouns = actor_pronouns(actor);
  179. char pronouns_c = 0;
  180. if (*pronouns != '\0')
  181. pronouns_c = 1;
  182. /* get the avatar */
  183. if ((v = xs_dict_get(actor, "icon")) != NULL) {
  184. /* if it's a list (Peertube), get the first one */
  185. if (xs_type(v) == XSTYPE_LIST)
  186. v = xs_list_get(v, 0);
  187. if ((v = xs_dict_get(v, "url")) != NULL)
  188. avatar = make_url(v, proxy, 0);
  189. }
  190. if (avatar == NULL)
  191. avatar = xs_fmt("data:image/png;base64, %s", default_avatar_base64());
  192. const char *actor_id = xs_dict_get(actor, "id");
  193. xs *href = NULL;
  194. if (user) {
  195. fwer = follower_check(user, actor_id);
  196. fwing = following_check(user, actor_id);
  197. }
  198. if (user && !in_people) {
  199. /* if this actor is a follower or being followed, create an
  200. anchored link to the people page instead of the actor url */
  201. if (fwer || fwing) {
  202. xs *md5 = xs_md5_hex(actor_id, strlen(actor_id));
  203. href = xs_fmt("%s/people/%s", user->actor, md5);
  204. }
  205. }
  206. if (href == NULL)
  207. href = xs_dup(actor_id);
  208. xs_html_add(actor_icon,
  209. xs_html_sctag("img",
  210. xs_html_attr("loading", "lazy"),
  211. xs_html_attr("class", "snac-avatar"),
  212. xs_html_attr("src", avatar),
  213. xs_html_attr("alt", "[?]")),
  214. xs_html_tag("a",
  215. xs_html_attr("href", href),
  216. xs_html_attr("class", "p-author h-card snac-author"),
  217. xs_html_raw(name))); /* name is already html-escaped */
  218. if (!xs_is_null(url)) {
  219. xs *md5 = xs_md5_hex(url, strlen(url));
  220. xs_html_add(actor_icon,
  221. xs_html_text(" "),
  222. xs_html_tag("a",
  223. xs_html_attr("href", (char *)url),
  224. xs_html_attr("title", md5),
  225. xs_html_text("»")));
  226. }
  227. if (pronouns_c > 0)
  228. xs_html_add(actor_icon,
  229. xs_html_text(" "),
  230. xs_html_tag("span",
  231. xs_html_attr("class", "snac-pronouns"),
  232. xs_html_attr("title", "user's pronouns"),
  233. xs_html_raw(pronouns)));
  234. if (strcmp(xs_dict_get(actor, "type"), "Service") == 0) {
  235. xs_html_add(actor_icon,
  236. xs_html_text(" "),
  237. xs_html_tag("span",
  238. xs_html_attr("title", "bot"),
  239. xs_html_raw("&#129302;")));
  240. }
  241. if (fwing && fwer) {
  242. xs_html_add(actor_icon,
  243. xs_html_text(" "),
  244. xs_html_tag("span",
  245. xs_html_attr("title", "mutual relation"),
  246. xs_html_raw("&#129309;")));
  247. }
  248. if (instance_failure(actor_id, 0) == -1) {
  249. xs_html_add(actor_icon,
  250. xs_html_text(" "),
  251. xs_html_tag("span",
  252. xs_html_attr("title", "broken instance"),
  253. xs_html_raw("&#128148;")));
  254. }
  255. if (actor_failure(actor_id, 0) == -1) {
  256. xs_html_add(actor_icon,
  257. xs_html_text(" "),
  258. xs_html_tag("span",
  259. xs_html_attr("title", "account no longer exists"),
  260. xs_html_raw("&#128128;")));
  261. }
  262. if (scope != -1) {
  263. if (scope == SCOPE_FOLLOWERS) {
  264. xs_html_add(actor_icon,
  265. xs_html_text(" "),
  266. xs_html_tag("span",
  267. xs_html_attr("title", "followers"),
  268. xs_html_raw("&#128274;"))); // emoji of a lock
  269. }
  270. else if (scope == SCOPE_PUBLIC) {
  271. xs_html_add(actor_icon,
  272. xs_html_text(" "),
  273. xs_html_tag("span",
  274. xs_html_attr("title", "public"),
  275. xs_html_raw("&#127760;"))); // emoji of a globe
  276. }
  277. else if (scope == SCOPE_UNLISTED) {
  278. xs_html_add(actor_icon,
  279. xs_html_text(" "),
  280. xs_html_tag("span",
  281. xs_html_attr("title", "unlisted"),
  282. xs_html_raw("&#128275;"))); // emoji of an unlocked lock
  283. }
  284. else if (scope == SCOPE_MENTIONED) {
  285. xs_html_add(actor_icon,
  286. xs_html_text(" "),
  287. xs_html_tag("span",
  288. xs_html_attr("title", "mentioned"),
  289. xs_html_raw("&#9993;&#65039;"))); // emoji of a mail
  290. }
  291. }
  292. if (xs_is_null(date)) {
  293. xs_html_add(actor_icon,
  294. xs_html_raw("&nbsp;"));
  295. }
  296. else {
  297. xs *date_label = xs_crop_i(xs_dup(date), 0, 10);
  298. xs *date_title = xs_dup(date);
  299. if (!xs_is_null(udate)) {
  300. xs *sd = xs_crop_i(xs_dup(udate), 0, 10);
  301. date_label = xs_str_cat(date_label, " / ", sd);
  302. date_title = xs_str_cat(date_title, " / ", udate);
  303. }
  304. if (xs_is_string(lang))
  305. date_title = xs_str_cat(date_title, " (", lang, ")");
  306. xs_html *date_text = xs_html_text(date_label);
  307. if (user && md5) {
  308. xs *lpost_url = xs_fmt("%s/admin/p/%s#%s_entry",
  309. user->actor, md5, md5);
  310. date_text = xs_html_tag("a",
  311. xs_html_attr("href", lpost_url),
  312. xs_html_attr("class", "snac-pubdate"),
  313. date_text);
  314. }
  315. else if (user && url) {
  316. xs *lpost_url = xs_fmt("%s/admin?q=%s",
  317. user->actor, xs_url_enc(url));
  318. date_text = xs_html_tag("a",
  319. xs_html_attr("href", lpost_url),
  320. xs_html_attr("class", "snac-pubdate"),
  321. date_text);
  322. }
  323. xs_html_add(actor_icon,
  324. xs_html_text(" "),
  325. xs_html_tag("time",
  326. xs_html_attr("class", "dt-published snac-pubdate"),
  327. xs_html_attr("title", date_title),
  328. date_text));
  329. }
  330. {
  331. const char *username, *id;
  332. if (xs_is_null(username = xs_dict_get(actor, "preferredUsername")) || *username == '\0') {
  333. /* This should never be reached */
  334. username = "anonymous";
  335. }
  336. if (xs_is_null(id = xs_dict_get(actor, "id")) || *id == '\0') {
  337. /* This should never be reached */
  338. id = "https://social.example.org/anonymous";
  339. }
  340. /* "LIKE AN ANIMAL" */
  341. xs *domain = xs_split(id, "/");
  342. xs *user = xs_fmt("@%s@%s", username, xs_list_get(domain, 2));
  343. xs_html_add(actor_icon,
  344. xs_html_sctag("br", NULL),
  345. xs_html_tag("a",
  346. xs_html_attr("href", xs_dict_get(actor, "id")),
  347. xs_html_attr("class", "p-author-tag h-card snac-author-tag"),
  348. xs_html_text(user)));
  349. }
  350. return actor_icon;
  351. }
  352. xs_html *html_msg_icon(snac *user, const char *actor_id, const xs_dict *msg,
  353. const char *proxy, const char *md5, const char *lang)
  354. {
  355. xs *actor = NULL;
  356. xs_html *actor_icon = NULL;
  357. if (actor_id && valid_status(actor_get_refresh(user, actor_id, &actor))) {
  358. const char *date = NULL;
  359. const char *udate = NULL;
  360. const char *url = NULL;
  361. const char *type = xs_dict_get(msg, "type");
  362. const int scope = get_msg_visibility(msg);
  363. if (xs_match(type, POSTLIKE_OBJECT_TYPE))
  364. url = xs_dict_get(msg, "id");
  365. date = xs_dict_get(msg, "published");
  366. udate = xs_dict_get(msg, "updated");
  367. actor_icon = html_actor_icon(user, actor, date, udate, url, scope, 0, proxy, lang, md5);
  368. }
  369. return actor_icon;
  370. }
  371. void html_note_render_visibility(snac* user, xs_html *form, const int scope)
  372. {
  373. // scopes aren't sorted by value unfortunately, so simple math won't work to limit them. using map-like thing here
  374. static const int public_scopes[5] = {SCOPE_PUBLIC, SCOPE_UNLISTED, SCOPE_FOLLOWERS, SCOPE_MENTIONED, -1};
  375. static const int unlisted_scopes[4] = {SCOPE_UNLISTED, SCOPE_FOLLOWERS, SCOPE_MENTIONED, -1};
  376. static const int followers_scopes[3] = {SCOPE_FOLLOWERS, SCOPE_MENTIONED, -1};
  377. static const int mentioned_scopes[2] = {SCOPE_MENTIONED, -1};
  378. static const int * const scopes[4] = { public_scopes, mentioned_scopes, unlisted_scopes, followers_scopes};
  379. static const char * const scopes_tags[4] = { "public", "mentioned", "unlisted", "followers"};
  380. static const char * const scopes_names[4] = { "Public", "Direct Message", "Unlisted", "Followers-only"};
  381. xs_html *paragraph = xs_html_tag("p", xs_html_text(L("Visibility: ")));
  382. const int* to_render = scopes[scope];
  383. for( int i = 0; to_render[i] != -1; i++ ){
  384. const int scope_i = to_render[i];
  385. const char* value = scopes_tags[scope_i];
  386. const char* name = scopes_names[scope_i];
  387. xs_html_add(paragraph,
  388. xs_html_tag("label",
  389. xs_html_sctag("input",
  390. xs_html_attr("type", "radio"),
  391. xs_html_attr("name", "visibility"),
  392. xs_html_attr("value", value),
  393. xs_html_attr(scope == scope_i ? "checked" : "", NULL)),
  394. xs_html_text(" "),
  395. xs_html_text(L(name)),
  396. xs_html_text(" "))
  397. );
  398. }
  399. xs_html_add(form, paragraph);
  400. }
  401. /* html_note but moddled for emoji's needs. here and not bellow, since the
  402. * other one is already so complex. */
  403. xs_html *html_emoji(snac *user, const char *summary,
  404. const char *div_id, const char *form_id,
  405. const char* placeholder, const char *post_id,
  406. const char* eid)
  407. {
  408. xs *action = xs_fmt("%s/admin/action", user->actor);
  409. xs_html *form;
  410. const int react = eid == NULL ? 0 : 1;
  411. xs_html *note = xs_html_tag("div",
  412. xs_html_tag("details",
  413. xs_html_tag("summary",
  414. xs_html_text(summary)),
  415. xs_html_tag("p", NULL),
  416. xs_html_tag("div",
  417. xs_html_attr("class", "snac-note"),
  418. xs_html_attr("id", div_id),
  419. form = xs_html_tag("form",
  420. xs_html_attr("autocomplete", "off"),
  421. xs_html_attr("method", "post"),
  422. xs_html_attr("action", action),
  423. xs_html_attr("enctype", "multipart/form-data"),
  424. xs_html_attr("id", form_id),
  425. xs_html_sctag("input",
  426. xs_html_attr("type", "hidden"),
  427. xs_html_attr("name", "id"),
  428. xs_html_attr("value", post_id)),
  429. xs_html_sctag("input",
  430. xs_html_attr("type", react ? "hidden" : "text"),
  431. xs_html_attr("name", "eid"),
  432. xs_html_attr(react ? "value" : "placeholder", react ? eid : placeholder)),
  433. xs_html_text(" "),
  434. xs_html_sctag("input",
  435. xs_html_attr("type", "submit"),
  436. xs_html_attr("name", "action"),
  437. xs_html_attr("eid", "action"),
  438. xs_html_attr("value", react ? L("EmojiUnreact") : L("EmojiReact"))),
  439. xs_html_text(" "),
  440. xs_html_tag("p", NULL)))));
  441. return note;
  442. }
  443. xs_html *html_note(snac *user, const char *summary,
  444. const char *div_id, const char *form_id,
  445. const char *ta_plh, const char *ta_content,
  446. const char *edit_id, const char *actor_id,
  447. const xs_val *cw_yn, const char *cw_text,
  448. const int scope, const char *redir,
  449. const char *in_reply_to, int poll,
  450. const xs_list *att_files, const xs_list *att_alt_texts,
  451. int is_draft, const char *published,
  452. const char *note_lang)
  453. /* Yes, this is a FUCKTON of arguments and I'm a bit embarrased */
  454. {
  455. xs *action = xs_fmt("%s/admin/note", user->actor);
  456. xs_html *form;
  457. xs_html *note = xs_html_tag("div",
  458. xs_html_tag("details",
  459. xs_html_tag("summary",
  460. xs_html_text(summary)),
  461. xs_html_tag("p", NULL),
  462. xs_html_tag("div",
  463. xs_html_attr("class", "snac-note"),
  464. xs_html_attr("id", div_id),
  465. form = xs_html_tag("form",
  466. xs_html_attr("autocomplete", "off"),
  467. xs_html_attr("method", "post"),
  468. xs_html_attr("action", action),
  469. xs_html_attr("enctype", "multipart/form-data"),
  470. xs_html_attr("id", form_id),
  471. xs_html_tag("textarea",
  472. xs_html_attr("class", "snac-textarea"),
  473. xs_html_attr("name", "content"),
  474. xs_html_attr("rows", "4"),
  475. xs_html_attr("wrap", "virtual"),
  476. xs_html_attr("required", "required"),
  477. xs_html_attr("placeholder", ta_plh),
  478. xs_html_text(ta_content)),
  479. xs_html_tag("p", NULL),
  480. xs_html_text(L("Sensitive content: ")),
  481. xs_html_sctag("input",
  482. xs_html_attr("type", "checkbox"),
  483. xs_html_attr("name", "sensitive"),
  484. xs_html_attr(xs_type(cw_yn) == XSTYPE_TRUE ? "checked" : "", NULL)),
  485. xs_html_sctag("input",
  486. xs_html_attr("type", "text"),
  487. xs_html_attr("name", "summary"),
  488. xs_html_attr("placeholder", L("Sensitive content description")),
  489. xs_html_attr("value", xs_is_null(cw_text) ? "" : cw_text))))));
  490. if (actor_id)
  491. xs_html_add(form,
  492. xs_html_sctag("input",
  493. xs_html_attr("type", "hidden"),
  494. xs_html_attr("name", "to"),
  495. xs_html_attr("value", actor_id)));
  496. if (edit_id == NULL)
  497. html_note_render_visibility(user, form, scope);
  498. if (redir)
  499. xs_html_add(form,
  500. xs_html_sctag("input",
  501. xs_html_attr("type", "hidden"),
  502. xs_html_attr("name", "redir"),
  503. xs_html_attr("value", redir)));
  504. if (in_reply_to)
  505. xs_html_add(form,
  506. xs_html_sctag("input",
  507. xs_html_attr("type", "hidden"),
  508. xs_html_attr("name", "in_reply_to"),
  509. xs_html_attr("value", in_reply_to)));
  510. else
  511. xs_html_add(form,
  512. xs_html_tag("p", NULL),
  513. xs_html_text(L("Reply to (URL): ")),
  514. xs_html_sctag("input",
  515. xs_html_attr("type", "url"),
  516. xs_html_attr("name", "in_reply_to"),
  517. xs_html_attr("placeholder", L("Optional URL to reply to"))));
  518. xs_html_add(form,
  519. xs_html_tag("p", NULL),
  520. xs_html_tag("span",
  521. xs_html_attr("title", L("Don't send, but store as a draft")),
  522. xs_html_text(L("Draft:")),
  523. xs_html_sctag("input",
  524. xs_html_attr("type", "checkbox"),
  525. xs_html_attr("name", "is_draft"),
  526. xs_html_attr(is_draft ? "checked" : "", NULL))));
  527. const char *post_langs = xs_dict_get(user->config, "post_langs");
  528. if (xs_is_string(post_langs) && *post_langs) {
  529. xs *ll = xs_split(post_langs, " ");
  530. const char *lang;
  531. xs_html *post_lang = xs_html_tag("select",
  532. xs_html_attr("name", "post_lang"));
  533. xs_list_foreach(ll, lang) {
  534. if (*lang) {
  535. if (xs_is_string(note_lang) && strcmp(lang, note_lang) == 0) {
  536. xs_html_add(post_lang,
  537. xs_html_tag("option",
  538. xs_html_text(lang),
  539. xs_html_attr("selected", NULL),
  540. xs_html_attr("value", lang)));
  541. }
  542. else {
  543. xs_html_add(post_lang,
  544. xs_html_tag("option",
  545. xs_html_text(lang),
  546. xs_html_attr("value", lang)));
  547. }
  548. }
  549. }
  550. xs_html_add(form,
  551. xs_html_tag("p", NULL),
  552. xs_html_text(L("Language:")),
  553. xs_html_text(" "),
  554. post_lang);
  555. }
  556. /* post date and time */
  557. xs *post_date = NULL;
  558. xs *post_time = NULL;
  559. if (xs_is_string(published)) {
  560. time_t t = xs_parse_iso_date(published, 0);
  561. if (t > 0) {
  562. post_date = xs_str_time(t, "%Y-%m-%d", 1);
  563. post_time = xs_str_time(t, "%H:%M:%S", 1);
  564. }
  565. }
  566. if (edit_id == NULL || is_draft || is_scheduled(user, edit_id)) {
  567. xs *pdat = xs_fmt(L("Post date and time (timezone: %s):"), user->tz);
  568. xs_html_add(form,
  569. xs_html_tag("p",
  570. xs_html_tag("details",
  571. xs_html_tag("summary",
  572. xs_html_text(L("Scheduled post..."))),
  573. xs_html_tag("p",
  574. xs_html_text(pdat),
  575. xs_html_sctag("br", NULL),
  576. xs_html_sctag("input",
  577. xs_html_attr("type", "date"),
  578. xs_html_attr("value", post_date ? post_date : ""),
  579. xs_html_attr("name", "post_date")),
  580. xs_html_text(" "),
  581. xs_html_sctag("input",
  582. xs_html_attr("type", "time"),
  583. xs_html_attr("value", post_time ? post_time : ""),
  584. xs_html_attr("step", "1"),
  585. xs_html_attr("name", "post_time"))))));
  586. }
  587. if (edit_id)
  588. xs_html_add(form,
  589. xs_html_sctag("input",
  590. xs_html_attr("type", "hidden"),
  591. xs_html_attr("name", "edit_id"),
  592. xs_html_attr("value", edit_id)));
  593. /* attachment controls */
  594. xs_html *att;
  595. xs_html_add(form,
  596. xs_html_tag("p", NULL),
  597. att = xs_html_tag("details",
  598. xs_html_tag("summary",
  599. xs_html_text(L("Attachments..."))),
  600. xs_html_tag("p", NULL)));
  601. int max_attachments = xs_number_get(xs_dict_get_def(srv_config, "max_attachments", "4"));
  602. int att_n = 0;
  603. /* fields for the currently existing attachments */
  604. if (xs_is_list(att_files) && xs_is_list(att_alt_texts)) {
  605. while (att_n < max_attachments) {
  606. const char *att_file = xs_list_get(att_files, att_n);
  607. const char *att_alt_text = xs_list_get(att_alt_texts, att_n);
  608. if (!xs_is_string(att_file) || !xs_is_string(att_alt_text))
  609. break;
  610. xs *att_lbl = xs_fmt("attach_url_%d", att_n);
  611. xs *alt_lbl = xs_fmt("alt_text_%d", att_n);
  612. if (att_n)
  613. xs_html_add(att,
  614. xs_html_sctag("br", NULL));
  615. xs_html_add(att,
  616. xs_html_text(L("File:")),
  617. xs_html_sctag("input",
  618. xs_html_attr("type", "text"),
  619. xs_html_attr("name", att_lbl),
  620. xs_html_attr("title", L("Clear this field to delete the attachment")),
  621. xs_html_attr("value", att_file)));
  622. xs_html_add(att,
  623. xs_html_text(" "),
  624. xs_html_sctag("input",
  625. xs_html_attr("type", "text"),
  626. xs_html_attr("name", alt_lbl),
  627. xs_html_attr("value", att_alt_text),
  628. xs_html_attr("placeholder", L("Attachment description"))));
  629. att_n++;
  630. }
  631. }
  632. /* the rest of possible attachments */
  633. while (att_n < max_attachments) {
  634. xs *att_lbl = xs_fmt("attach_%d", att_n);
  635. xs *alt_lbl = xs_fmt("alt_text_%d", att_n);
  636. if (att_n)
  637. xs_html_add(att,
  638. xs_html_sctag("br", NULL));
  639. xs_html_add(att,
  640. xs_html_sctag("input",
  641. xs_html_attr("type", "file"),
  642. xs_html_attr("name", att_lbl)));
  643. xs_html_add(att,
  644. xs_html_text(" "),
  645. xs_html_sctag("input",
  646. xs_html_attr("type", "text"),
  647. xs_html_attr("name", alt_lbl),
  648. xs_html_attr("placeholder", L("Attachment description"))));
  649. att_n++;
  650. }
  651. /* add poll controls */
  652. if (poll) {
  653. xs_html_add(form,
  654. xs_html_tag("p", NULL),
  655. xs_html_tag("details",
  656. xs_html_tag("summary",
  657. xs_html_text(L("Poll..."))),
  658. xs_html_tag("p",
  659. xs_html_text(L("Poll options (one per line, up to 8):")),
  660. xs_html_sctag("br", NULL),
  661. xs_html_tag("textarea",
  662. xs_html_attr("class", "snac-textarea"),
  663. xs_html_attr("name", "poll_options"),
  664. xs_html_attr("rows", "4"),
  665. xs_html_attr("wrap", "virtual"),
  666. xs_html_attr("placeholder", L("Option 1...\nOption 2...\nOption 3...\n...")))),
  667. xs_html_tag("select",
  668. xs_html_attr("name", "poll_multiple"),
  669. xs_html_tag("option",
  670. xs_html_attr("value", "off"),
  671. xs_html_text(L("One choice"))),
  672. xs_html_tag("option",
  673. xs_html_attr("value", "on"),
  674. xs_html_text(L("Multiple choices")))),
  675. xs_html_text(" "),
  676. xs_html_tag("select",
  677. xs_html_attr("name", "poll_end_secs"),
  678. xs_html_tag("option",
  679. xs_html_attr("value", "300"),
  680. xs_html_text(L("End in 5 minutes"))),
  681. xs_html_tag("option",
  682. xs_html_attr("value", "3600"),
  683. xs_html_attr("selected", NULL),
  684. xs_html_text(L("End in 1 hour"))),
  685. xs_html_tag("option",
  686. xs_html_attr("value", "86400"),
  687. xs_html_text(L("End in 1 day"))))));
  688. }
  689. xs_html_add(form,
  690. xs_html_tag("p", NULL),
  691. xs_html_sctag("input",
  692. xs_html_attr("type", "submit"),
  693. xs_html_attr("class", "button"),
  694. xs_html_attr("value", L("Post"))),
  695. xs_html_tag("p", NULL));
  696. return note;
  697. }
  698. static xs_html *html_base_head(void)
  699. {
  700. xs_html *head = xs_html_tag("head",
  701. xs_html_sctag("meta",
  702. xs_html_attr("name", "viewport"),
  703. xs_html_attr("content", "width=device-width, initial-scale=1")),
  704. xs_html_sctag("meta",
  705. xs_html_attr("name", "generator"),
  706. xs_html_attr("content", USER_AGENT)));
  707. /* add server CSS and favicon */
  708. xs *f = NULL;
  709. const char *favicon = xs_dict_get(srv_config, "favicon_url");
  710. if (xs_is_string(favicon))
  711. f = xs_dup(favicon);
  712. else
  713. f = xs_fmt("%s/favicon.ico", srv_baseurl);
  714. const xs_list *p = xs_dict_get(srv_config, "cssurls");
  715. const char *v;
  716. xs_list_foreach(p, v) {
  717. xs_html_add(head,
  718. xs_html_sctag("link",
  719. xs_html_attr("rel", "stylesheet"),
  720. xs_html_attr("type", "text/css"),
  721. xs_html_attr("href", v)));
  722. }
  723. xs_html_add(head,
  724. xs_html_sctag("link",
  725. xs_html_attr("rel", "icon"),
  726. xs_html_attr("type", "image/x-icon"),
  727. xs_html_attr("href", f)));
  728. return head;
  729. }
  730. xs_html *html_instance_head(void)
  731. {
  732. xs_html *head = html_base_head();
  733. {
  734. FILE *f;
  735. xs *g_css_fn = xs_fmt("%s/style.css", srv_basedir);
  736. if ((f = fopen(g_css_fn, "r")) != NULL) {
  737. xs *css = xs_readall(f);
  738. fclose(f);
  739. xs_html_add(head,
  740. xs_html_tag("style",
  741. xs_html_raw(css)));
  742. }
  743. }
  744. const char *host = xs_dict_get(srv_config, "host");
  745. const char *title = xs_dict_get(srv_config, "title");
  746. xs_html_add(head,
  747. xs_html_tag("title",
  748. xs_html_text(title && *title ? title : host)));
  749. return head;
  750. }
  751. static xs_html *html_instance_body(void)
  752. {
  753. const char *host = xs_dict_get(srv_config, "host");
  754. const char *sdesc = xs_dict_get(srv_config, "short_description");
  755. const char *sdescraw = xs_dict_get(srv_config, "short_description_raw");
  756. const char *email = xs_dict_get(srv_config, "admin_email");
  757. const char *acct = xs_dict_get(srv_config, "admin_account");
  758. /* for L() */
  759. const snac *user = NULL;
  760. xs *blurb = xs_replace(snac_blurb, "%host%", host);
  761. xs_html *dl;
  762. xs_html *body = xs_html_tag("body",
  763. xs_html_tag("div",
  764. xs_html_attr("class", "snac-instance-blurb"),
  765. xs_html_raw(blurb), /* pure html */
  766. dl = xs_html_tag("dl", NULL)));
  767. if (sdesc && *sdesc) {
  768. if (!xs_is_null(sdescraw) && xs_type(sdescraw) == XSTYPE_TRUE) {
  769. xs_html_add(dl,
  770. xs_html_tag("di",
  771. xs_html_tag("dt",
  772. xs_html_text(L("Site description"))),
  773. xs_html_tag("dd",
  774. xs_html_raw(sdesc))));
  775. } else {
  776. xs_html_add(dl,
  777. xs_html_tag("di",
  778. xs_html_tag("dt",
  779. xs_html_text(L("Site description"))),
  780. xs_html_tag("dd",
  781. xs_html_text(sdesc))));
  782. }
  783. }
  784. if (email && *email) {
  785. xs *mailto = xs_fmt("mailto:%s", email);
  786. xs_html_add(dl,
  787. xs_html_tag("di",
  788. xs_html_tag("dt",
  789. xs_html_text(L("Admin email"))),
  790. xs_html_tag("dd",
  791. xs_html_tag("a",
  792. xs_html_attr("href", mailto),
  793. xs_html_text(email)))));
  794. }
  795. if (acct && *acct) {
  796. xs *url = xs_fmt("%s/%s", srv_baseurl, acct);
  797. xs *handle = xs_fmt("@%s@%s", acct, host);
  798. xs_html_add(dl,
  799. xs_html_tag("di",
  800. xs_html_tag("dt",
  801. xs_html_text(L("Admin account"))),
  802. xs_html_tag("dd",
  803. xs_html_tag("a",
  804. xs_html_attr("href", url),
  805. xs_html_text(handle)))));
  806. }
  807. return body;
  808. }
  809. xs_html *html_user_head(snac *user, const char *desc, const char *url)
  810. {
  811. xs_html *head = html_base_head();
  812. /* add the user CSS */
  813. {
  814. xs *css = NULL;
  815. int size;
  816. /* try to open the user css */
  817. if (!valid_status(static_get(user, "style.css", &css, &size, NULL, NULL))) {
  818. /* it's not there; try to open the server-wide css */
  819. FILE *f;
  820. xs *g_css_fn = xs_fmt("%s/style.css", srv_basedir);
  821. if ((f = fopen(g_css_fn, "r")) != NULL) {
  822. css = xs_readall(f);
  823. fclose(f);
  824. }
  825. }
  826. if (css != NULL) {
  827. xs_html_add(head,
  828. xs_html_tag("style",
  829. xs_html_raw(css)));
  830. }
  831. }
  832. /* title */
  833. xs *title = xs_fmt("%s (@%s@%s)", xs_dict_get(user->config, "name"),
  834. user->uid, xs_dict_get(srv_config, "host"));
  835. xs_html_add(head,
  836. xs_html_tag("title",
  837. xs_html_text(title)));
  838. xs *avatar = xs_dup(xs_dict_get(user->config, "avatar"));
  839. if (avatar == NULL || *avatar == '\0') {
  840. xs_free(avatar);
  841. avatar = xs_fmt("%s/susie.png", srv_baseurl);
  842. }
  843. /* create a description field */
  844. xs *s_desc = NULL;
  845. int n;
  846. if (desc == NULL)
  847. s_desc = xs_dup(xs_dict_get(user->config, "bio"));
  848. else
  849. s_desc = xs_dup(desc);
  850. /* show metrics in og:description? */
  851. if (xs_is_true(xs_dict_get(user->config, "show_contact_metrics"))) {
  852. xs *s1 = xs_fmt(L("%d following, %d followers"), following_list_len(user), follower_list_len(user));
  853. s1 = xs_str_cat(s1, " · ");
  854. s_desc = xs_str_prepend_i(s_desc, s1);
  855. }
  856. /* shorten desc to a reasonable size */
  857. for (n = 0; s_desc[n]; n++) {
  858. if (n > 512 && (s_desc[n] == ' ' || s_desc[n] == '\n'))
  859. break;
  860. }
  861. s_desc[n] = '\0';
  862. /* og properties */
  863. xs_html_add(head,
  864. xs_html_sctag("meta",
  865. xs_html_attr("property", "og:site_name"),
  866. xs_html_attr("content", xs_dict_get(srv_config, "host"))),
  867. xs_html_sctag("meta",
  868. xs_html_attr("property", "og:title"),
  869. xs_html_attr("content", title)),
  870. xs_html_sctag("meta",
  871. xs_html_attr("property", "og:description"),
  872. xs_html_attr("content", s_desc)),
  873. xs_html_sctag("meta",
  874. xs_html_attr("property", "og:image"),
  875. xs_html_attr("content", avatar)),
  876. xs_html_sctag("meta",
  877. xs_html_attr("property", "og:width"),
  878. xs_html_attr("content", "300")),
  879. xs_html_sctag("meta",
  880. xs_html_attr("property", "og:height"),
  881. xs_html_attr("content", "300")));
  882. /* RSS link */
  883. xs *rss_url = xs_fmt("%s.rss", user->actor);
  884. xs_html_add(head,
  885. xs_html_sctag("link",
  886. xs_html_attr("rel", "alternate"),
  887. xs_html_attr("type", "application/rss+xml"),
  888. xs_html_attr("title", "RSS"),
  889. xs_html_attr("href", rss_url)));
  890. /* ActivityPub alternate link (actor id) */
  891. xs_html_add(head,
  892. xs_html_sctag("link",
  893. xs_html_attr("rel", "alternate"),
  894. xs_html_attr("type", "application/activity+json"),
  895. xs_html_attr("href", url ? url : user->actor)));
  896. /* webmention hook */
  897. xs *wbh = xs_fmt("%s/webmention-hook", srv_baseurl);
  898. xs_html_add(head,
  899. xs_html_sctag("link",
  900. xs_html_attr("rel", "webmention"),
  901. xs_html_attr("href", wbh)));
  902. return head;
  903. }
  904. static xs_html *html_user_body(snac *user, int read_only)
  905. {
  906. const char *proxy = NULL;
  907. if (user && !read_only && xs_is_true(xs_dict_get(srv_config, "proxy_media")))
  908. proxy = user->actor;
  909. xs_html *body = xs_html_tag("body", NULL);
  910. /* top nav */
  911. xs_html *top_nav = xs_html_tag("nav",
  912. xs_html_attr("class", "snac-top-nav"));
  913. xs *avatar = xs_dup(xs_dict_get(user->config, "avatar"));
  914. if (avatar == NULL || *avatar == '\0') {
  915. xs_free(avatar);
  916. avatar = xs_fmt("data:image/png;base64, %s", default_avatar_base64());
  917. }
  918. xs_html_add(top_nav,
  919. xs_html_sctag("img",
  920. xs_html_attr("src", avatar),
  921. xs_html_attr("class", "snac-avatar"),
  922. xs_html_attr("alt", "")));
  923. if (read_only) {
  924. xs *rss_url = xs_fmt("%s.rss", user->actor);
  925. xs *admin_url = xs_fmt("%s/admin", user->actor);
  926. xs_html_add(top_nav,
  927. xs_html_tag("a",
  928. xs_html_attr("href", rss_url),
  929. xs_html_text(L("RSS"))),
  930. xs_html_text(" - "),
  931. xs_html_tag("a",
  932. xs_html_attr("href", admin_url),
  933. xs_html_attr("rel", "nofollow"),
  934. xs_html_text(L("private"))));
  935. }
  936. else {
  937. int n_len = notify_new_num(user);
  938. int p_len = pending_count(user);
  939. xs_html *notify_count = NULL;
  940. xs_html *pending_follow_count = NULL;
  941. /* show the number of new notifications, if there are any */
  942. if (n_len) {
  943. xs *n_len_str = xs_fmt(" %d ", n_len);
  944. notify_count = xs_html_tag("sup",
  945. xs_html_attr("style", "background-color: red; color: white;"),
  946. xs_html_text(n_len_str));
  947. }
  948. else
  949. notify_count = xs_html_text("");
  950. if (p_len) {
  951. xs *s = xs_fmt(" %d ", p_len);
  952. pending_follow_count = xs_html_tag("sup",
  953. xs_html_attr("style", "background-color: red; color: white;"),
  954. xs_html_text(s));
  955. }
  956. else
  957. pending_follow_count = xs_html_text("");
  958. xs *admin_url = xs_fmt("%s/admin", user->actor);
  959. xs *notify_url = xs_fmt("%s/notifications", user->actor);
  960. xs *people_url = xs_fmt("%s/people", user->actor);
  961. xs *instance_url = xs_fmt("%s/instance", user->actor);
  962. xs_html_add(top_nav,
  963. xs_html_tag("a",
  964. xs_html_attr("href", user->actor),
  965. xs_html_text(L("public"))),
  966. xs_html_text(" - "),
  967. xs_html_tag("a",
  968. xs_html_attr("href", admin_url),
  969. xs_html_text(L("private"))),
  970. xs_html_text(" - "),
  971. xs_html_tag("a",
  972. xs_html_attr("href", notify_url),
  973. xs_html_text(L("notifications"))),
  974. notify_count,
  975. xs_html_text(" - "),
  976. xs_html_tag("a",
  977. xs_html_attr("href", people_url),
  978. xs_html_text(L("people"))),
  979. pending_follow_count,
  980. xs_html_text(" - "),
  981. xs_html_tag("a",
  982. xs_html_attr("href", instance_url),
  983. xs_html_text(L("instance"))),
  984. xs_html_text(" "),
  985. xs_html_tag("form",
  986. xs_html_attr("style", "display: inline!important"),
  987. xs_html_attr("class", "snac-search-box"),
  988. xs_html_attr("action", admin_url),
  989. xs_html_sctag("input",
  990. xs_html_attr("type", "text"),
  991. xs_html_attr("name", "q"),
  992. xs_html_attr("title", L("Search posts by URL or content (regular expression), @user@host accounts, or #tag")),
  993. xs_html_attr("placeholder", L("Content search")))));
  994. }
  995. xs_html_add(body,
  996. top_nav);
  997. /* user info */
  998. xs_html *top_user = xs_html_tag("div",
  999. xs_html_attr("class", "h-card snac-top-user"));
  1000. if (read_only) {
  1001. const char *header = xs_dict_get(user->config, "header");
  1002. if (header && *header) {
  1003. xs_html_add(top_user,
  1004. xs_html_tag("div",
  1005. xs_html_attr("class", "snac-top-user-banner"),
  1006. xs_html_attr("style", "clear: both"),
  1007. xs_html_sctag("br", NULL),
  1008. xs_html_sctag("img",
  1009. xs_html_attr("src", header))));
  1010. }
  1011. }
  1012. xs *handle = xs_fmt("@%s@%s",
  1013. xs_dict_get(user->config, "uid"),
  1014. xs_dict_get(srv_config, "host"));
  1015. xs *display_name = format_text_with_emoji(NULL, xs_dict_get(user->config, "name"), 1, proxy);
  1016. xs_html_add(top_user,
  1017. xs_html_tag("p",
  1018. xs_html_attr("class", "p-name snac-top-user-name"),
  1019. xs_html_raw(display_name)),
  1020. xs_html_tag("p",
  1021. xs_html_attr("class", "snac-top-user-id"),
  1022. xs_html_text(handle)));
  1023. /** instance announcement **/
  1024. if (!read_only) {
  1025. double la = 0.0;
  1026. xs *user_la = xs_dup(xs_dict_get(user->config, "last_announcement"));
  1027. if (user_la != NULL)
  1028. la = xs_number_get(user_la);
  1029. const t_announcement *an = announcement(la);
  1030. if (an != NULL && (an->text != NULL)) {
  1031. xs *s = xs_fmt("?da=%.0f", an->timestamp);
  1032. xs_html_add(top_user, xs_html_tag("div",
  1033. xs_html_attr("class", "snac-announcement"),
  1034. xs_html_text(an->text),
  1035. xs_html_text(" "),
  1036. xs_html_tag("a",
  1037. xs_html_attr("href", s),
  1038. xs_html_text("Dismiss"))));
  1039. }
  1040. }
  1041. if (read_only) {
  1042. xs *bio = format_text_with_emoji(user, xs_dict_get(user->config, "bio"), 2, proxy);
  1043. xs_html *top_user_bio = xs_html_tag("div",
  1044. xs_html_attr("class", "p-note snac-top-user-bio"),
  1045. xs_html_raw(bio)); /* already sanitized */
  1046. xs_html_add(top_user,
  1047. top_user_bio);
  1048. xs *metadata = NULL;
  1049. const xs_dict *md = xs_dict_get(user->config, "metadata");
  1050. if (xs_type(md) == XSTYPE_DICT)
  1051. metadata = xs_dup(md);
  1052. else
  1053. if (xs_type(md) == XSTYPE_STRING) {
  1054. /* convert to dict for easier iteration */
  1055. metadata = xs_dict_new();
  1056. xs *l = xs_split(md, "\n");
  1057. const char *ll;
  1058. xs_list_foreach(l, ll) {
  1059. xs *kv = xs_split_n(ll, "=", 1);
  1060. const char *k = xs_list_get(kv, 0);
  1061. const char *v = xs_list_get(kv, 1);
  1062. if (k && v) {
  1063. xs *kk = xs_strip_i(xs_dup(k));
  1064. xs *vv = xs_strip_i(xs_dup(v));
  1065. metadata = xs_dict_set(metadata, kk, vv);
  1066. }
  1067. }
  1068. }
  1069. if (xs_type(metadata) == XSTYPE_DICT) {
  1070. const xs_str *k;
  1071. const xs_str *v;
  1072. xs_dict *val_links = user->links;
  1073. if (xs_is_null(val_links))
  1074. val_links = xs_stock(XSTYPE_DICT);
  1075. xs_html *snac_metadata = xs_html_tag("div",
  1076. xs_html_attr("class", "snac-metadata"));
  1077. int c = 0;
  1078. while (xs_dict_next(metadata, &k, &v, &c)) {
  1079. xs_html *value;
  1080. if (xs_startswith(v, "https:/") || xs_startswith(v, "http:/") || *v == '@') {
  1081. /* is this link validated? */
  1082. xs *verified_link = NULL;
  1083. const xs_number *val_time = xs_dict_get(val_links, v);
  1084. const char *url = NULL;
  1085. if (xs_is_string(val_time)) {
  1086. /* resolve again, as it may be an account handle */
  1087. url = val_time;
  1088. val_time = xs_dict_get(val_links, val_time);
  1089. }
  1090. if (xs_type(val_time) == XSTYPE_NUMBER) {
  1091. time_t t = xs_number_get(val_time);
  1092. if (t > 0) {
  1093. xs *s1 = xs_str_utctime(t, ISO_DATE_SPEC);
  1094. verified_link = xs_fmt("%s (%s)", L("verified link"), s1);
  1095. }
  1096. }
  1097. if (!xs_is_null(verified_link)) {
  1098. value = xs_html_tag("span",
  1099. xs_html_attr("title", verified_link),
  1100. xs_html_raw("&#10004; "),
  1101. xs_html_tag("a",
  1102. xs_html_attr("rel", "me"),
  1103. xs_html_attr("target", "_blank"),
  1104. xs_html_attr("href", url ? url : v),
  1105. xs_html_text(v)));
  1106. }
  1107. else {
  1108. value = xs_html_tag("a",
  1109. xs_html_attr("rel", "me"),
  1110. xs_html_attr("href", url ? url : v),
  1111. xs_html_text(v));
  1112. }
  1113. }
  1114. else
  1115. if (xs_startswith(v, "gemini:/") || xs_startswith(v, "xmpp:")) {
  1116. value = xs_html_tag("a",
  1117. xs_html_attr("rel", "me"),
  1118. xs_html_attr("href", v),
  1119. xs_html_text(v));
  1120. }
  1121. else
  1122. value = xs_html_text(v);
  1123. xs_html_add(snac_metadata,
  1124. xs_html_tag("span",
  1125. xs_html_attr("class", "snac-property-name"),
  1126. xs_html_text(k)),
  1127. xs_html_text(":"),
  1128. xs_html_raw("&nbsp;"),
  1129. xs_html_tag("span",
  1130. xs_html_attr("class", "snac-property-value"),
  1131. value),
  1132. xs_html_sctag("br", NULL));
  1133. }
  1134. xs_html_add(top_user,
  1135. snac_metadata);
  1136. }
  1137. const char *latitude = xs_dict_get_def(user->config, "latitude", "");
  1138. const char *longitude = xs_dict_get_def(user->config, "longitude", "");
  1139. if (*latitude && *longitude) {
  1140. xs *label = xs_fmt("%s,%s", latitude, longitude);
  1141. xs *url = xs_fmt("https://openstreetmap.org/search?query=%s,%s",
  1142. latitude, longitude);
  1143. xs_html_add(top_user,
  1144. xs_html_tag("p",
  1145. xs_html_text(L("Location: ")),
  1146. xs_html_tag("a",
  1147. xs_html_attr("href", url),
  1148. xs_html_attr("target", "_blank"),
  1149. xs_html_text(label))));
  1150. }
  1151. if (xs_is_true(xs_dict_get(user->config, "show_contact_metrics"))) {
  1152. xs *s1 = xs_fmt(L("%d following, %d followers"), following_list_len(user), follower_list_len(user));
  1153. xs_html_add(top_user,
  1154. xs_html_tag("p",
  1155. xs_html_text(s1)));
  1156. }
  1157. }
  1158. xs_html_add(body,
  1159. top_user);
  1160. return body;
  1161. }
  1162. xs_html *html_top_controls(snac *user)
  1163. /* generates the top controls */
  1164. {
  1165. xs *ops_action = xs_fmt("%s/admin/action", user->actor);
  1166. xs_html *top_controls = xs_html_tag("div",
  1167. xs_html_attr("class", "snac-top-controls"),
  1168. /** new post **/
  1169. html_note(user, L("New Post..."),
  1170. "new_post_div", "new_post_form",
  1171. L("What's on your mind?"), "",
  1172. NULL, NULL,
  1173. xs_stock(XSTYPE_FALSE), "",
  1174. SCOPE_PUBLIC, NULL,
  1175. NULL, 1, NULL, NULL, 0, NULL, NULL),
  1176. /** operations **/
  1177. xs_html_tag("details",
  1178. xs_html_tag("summary",
  1179. xs_html_text(L("Operations..."))),
  1180. xs_html_tag("p", NULL),
  1181. xs_html_tag("form",
  1182. xs_html_attr("autocomplete", "off"),
  1183. xs_html_attr("method", "post"),
  1184. xs_html_attr("action", ops_action),
  1185. xs_html_sctag("input",
  1186. xs_html_attr("type", "text"),
  1187. xs_html_attr("name", "actor"),
  1188. xs_html_attr("required", "required"),
  1189. xs_html_attr("placeholder", "bob@example.com")),
  1190. xs_html_text(" "),
  1191. xs_html_sctag("input",
  1192. xs_html_attr("type", "submit"),
  1193. xs_html_attr("name", "action"),
  1194. xs_html_attr("value", L("Follow"))),
  1195. xs_html_text(" "),
  1196. xs_html_text(L("(by URL or user@host)"))),
  1197. xs_html_tag("p", NULL),
  1198. xs_html_tag("form",
  1199. xs_html_attr("autocomplete", "off"),
  1200. xs_html_attr("method", "post"),
  1201. xs_html_attr("action", ops_action),
  1202. xs_html_sctag("input",
  1203. xs_html_attr("type", "url"),
  1204. xs_html_attr("name", "id"),
  1205. xs_html_attr("required", "required"),
  1206. xs_html_attr("placeholder", "https:/" "/fedi.example.com/bob/...")),
  1207. xs_html_text(" "),
  1208. xs_html_sctag("input",
  1209. xs_html_attr("type", "submit"),
  1210. xs_html_attr("name", "action"),
  1211. xs_html_attr("value", L("Boost"))),
  1212. xs_html_text(" "),
  1213. xs_html_text(L("(by URL)"))),
  1214. xs_html_tag("p", NULL),
  1215. xs_html_tag("form",
  1216. xs_html_attr("autocomplete", "off"),
  1217. xs_html_attr("method", "post"),
  1218. xs_html_attr("action", ops_action),
  1219. xs_html_sctag("input",
  1220. xs_html_attr("type", "text"),
  1221. xs_html_attr("name", "id"),
  1222. xs_html_attr("required", "required"),
  1223. xs_html_attr("placeholder", "https:/" "/fedi.example.com/bob/...")),
  1224. xs_html_text(" "),
  1225. xs_html_sctag("input",
  1226. xs_html_attr("type", "submit"),
  1227. xs_html_attr("name", "action"),
  1228. xs_html_attr("value", L("Like"))),
  1229. xs_html_text(" "),
  1230. xs_html_text(L("(by URL)"))),
  1231. xs_html_tag("form",
  1232. xs_html_attr("autocomplete", "off"),
  1233. xs_html_attr("method", "post"),
  1234. xs_html_attr("action", ops_action),
  1235. xs_html_sctag("input",
  1236. xs_html_attr("type", "text"),
  1237. xs_html_attr("name", "eid"),
  1238. xs_html_attr("required", "required"),
  1239. xs_html_attr("placeholder", ":neocat:")),
  1240. xs_html_text(" "),
  1241. xs_html_sctag("input",
  1242. xs_html_attr("type", "text"),
  1243. xs_html_attr("name", "id"),
  1244. xs_html_attr("required", "required"),
  1245. xs_html_attr("placeholder", "https:/" "/fedi.example.com/bob/...")),
  1246. xs_html_text(" "),
  1247. xs_html_sctag("input",
  1248. xs_html_attr("type", "submit"),
  1249. xs_html_attr("name", "action"),
  1250. xs_html_attr("value", L("EmojiReact"))),
  1251. xs_html_text(" "),
  1252. xs_html_text(L("(by URL)"))),
  1253. xs_html_tag("p", NULL)));
  1254. /** user settings **/
  1255. const char *email = "[disabled by admin]";
  1256. if (xs_type(xs_dict_get(srv_config, "disable_email_notifications")) != XSTYPE_TRUE) {
  1257. email = xs_dict_get(user->config_o, "email");
  1258. if (xs_is_null(email)) {
  1259. email = xs_dict_get(user->config, "email");
  1260. if (xs_is_null(email))
  1261. email = "";
  1262. }
  1263. }
  1264. const char *cw = xs_dict_get(user->config, "cw");
  1265. if (xs_is_null(cw))
  1266. cw = "";
  1267. const char *telegram_bot = xs_dict_get(user->config, "telegram_bot");
  1268. if (xs_is_null(telegram_bot))
  1269. telegram_bot = "";
  1270. const char *telegram_chat_id = xs_dict_get(user->config, "telegram_chat_id");
  1271. if (xs_is_null(telegram_chat_id))
  1272. telegram_chat_id = "";
  1273. const char *ntfy_server = xs_dict_get(user->config, "ntfy_server");
  1274. if (xs_is_null(ntfy_server))
  1275. ntfy_server = "";
  1276. const char *ntfy_token = xs_dict_get(user->config, "ntfy_token");
  1277. if (xs_is_null(ntfy_token))
  1278. ntfy_token = "";
  1279. const char *purge_days = xs_dict_get(user->config, "purge_days");
  1280. if (!xs_is_null(purge_days) && xs_type(purge_days) == XSTYPE_NUMBER)
  1281. purge_days = (char *)xs_number_str(purge_days);
  1282. else
  1283. purge_days = "0";
  1284. const xs_val *d_dm_f_u = xs_dict_get(user->config, "drop_dm_from_unknown");
  1285. const xs_val *bot = xs_dict_get(user->config, "bot");
  1286. const xs_val *a_private = xs_dict_get(user->config, "private");
  1287. const xs_val *auto_boost = xs_dict_get(user->config, "auto_boost");
  1288. const xs_val *coll_thrds = xs_dict_get(user->config, "collapse_threads");
  1289. const xs_val *pending = xs_dict_get(user->config, "approve_followers");
  1290. const xs_val *show_foll = xs_dict_get(user->config, "show_contact_metrics");
  1291. const char *latitude = xs_dict_get_def(user->config, "latitude", "");
  1292. const char *longitude = xs_dict_get_def(user->config, "longitude", "");
  1293. const char *webhook = xs_dict_get_def(user->config, "notify_webhook", "");
  1294. const char *post_langs = xs_dict_get_def(user->config, "post_langs", "");
  1295. xs *metadata = NULL;
  1296. const xs_dict *md = xs_dict_get(user->config, "metadata");
  1297. if (xs_type(md) == XSTYPE_DICT) {
  1298. const xs_str *k;
  1299. const xs_str *v;
  1300. metadata = xs_str_new(NULL);
  1301. xs_dict_foreach(md, k, v) {
  1302. xs *kp = xs_fmt("%s=%s", k, v);
  1303. if (*metadata)
  1304. metadata = xs_str_cat(metadata, "\n");
  1305. metadata = xs_str_cat(metadata, kp);
  1306. }
  1307. }
  1308. else
  1309. if (xs_type(md) == XSTYPE_STRING)
  1310. metadata = xs_dup(md);
  1311. else
  1312. metadata = xs_str_new(NULL);
  1313. /* ui language */
  1314. xs_html *lang_select = xs_html_tag("select",
  1315. xs_html_attr("name", "web_ui_lang"));
  1316. const char *u_lang = xs_dict_get_def(user->config, "lang", "en");
  1317. const char *lang;
  1318. const xs_dict *langs;
  1319. xs_dict_foreach(srv_langs, lang, langs) {
  1320. if (strcmp(u_lang, lang) == 0)
  1321. xs_html_add(lang_select,
  1322. xs_html_tag("option",
  1323. xs_html_text(lang),
  1324. xs_html_attr("value", lang),
  1325. xs_html_attr("selected", "selected")));
  1326. else
  1327. xs_html_add(lang_select,
  1328. xs_html_tag("option",
  1329. xs_html_text(lang),
  1330. xs_html_attr("value", lang)));
  1331. }
  1332. /* timezone */
  1333. xs_html *tz_select = xs_html_tag("select",
  1334. xs_html_attr("name", "tz"));
  1335. xs *tzs = xs_tz_list();
  1336. const char *tz;
  1337. xs_list_foreach(tzs, tz) {
  1338. if (strcmp(tz, user->tz) == 0)
  1339. xs_html_add(tz_select,
  1340. xs_html_tag("option",
  1341. xs_html_text(tz),
  1342. xs_html_attr("value", tz),
  1343. xs_html_attr("selected", "selected")));
  1344. else
  1345. xs_html_add(tz_select,
  1346. xs_html_tag("option",
  1347. xs_html_text(tz),
  1348. xs_html_attr("value", tz)));
  1349. }
  1350. xs *user_setup_action = xs_fmt("%s/admin/user-setup", user->actor);
  1351. xs_html_add(top_controls,
  1352. xs_html_tag("details",
  1353. xs_html_tag("summary",
  1354. xs_html_text(L("User Settings..."))),
  1355. xs_html_tag("div",
  1356. xs_html_attr("class", "snac-user-setup"),
  1357. xs_html_tag("form",
  1358. xs_html_attr("autocomplete", "off"),
  1359. xs_html_attr("method", "post"),
  1360. xs_html_attr("action", user_setup_action),
  1361. xs_html_attr("enctype", "multipart/form-data"),
  1362. xs_html_tag("p",
  1363. xs_html_text(L("Display name:")),
  1364. xs_html_sctag("br", NULL),
  1365. xs_html_sctag("input",
  1366. xs_html_attr("type", "text"),
  1367. xs_html_attr("name", "name"),
  1368. xs_html_attr("value", xs_dict_get(user->config, "name")),
  1369. xs_html_attr("placeholder", L("Your name")))),
  1370. xs_html_tag("p",
  1371. xs_html_text(L("Avatar: ")),
  1372. xs_html_sctag("input",
  1373. xs_html_attr("type", "file"),
  1374. xs_html_attr("name", "avatar_file"))),
  1375. xs_html_tag("p",
  1376. xs_html_sctag("input",
  1377. xs_html_attr("type", "checkbox"),
  1378. xs_html_attr("name", "avatar_delete")),
  1379. xs_html_text(L("Delete current avatar"))),
  1380. xs_html_tag("p",
  1381. xs_html_text(L("Header image (banner): ")),
  1382. xs_html_sctag("input",
  1383. xs_html_attr("type", "file"),
  1384. xs_html_attr("name", "header_file"))),
  1385. xs_html_tag("p",
  1386. xs_html_sctag("input",
  1387. xs_html_attr("type", "checkbox"),
  1388. xs_html_attr("name", "header_delete")),
  1389. xs_html_text(L("Delete current header image"))),
  1390. xs_html_tag("p",
  1391. xs_html_text(L("Bio:")),
  1392. xs_html_sctag("br", NULL),
  1393. xs_html_tag("textarea",
  1394. xs_html_attr("name", "bio"),
  1395. xs_html_attr("cols", "40"),
  1396. xs_html_attr("rows", "4"),
  1397. xs_html_attr("placeholder", L("Write about yourself here...")),
  1398. xs_html_text(xs_dict_get(user->config, "bio")))),
  1399. xs_html_sctag("input",
  1400. xs_html_attr("type", "checkbox"),
  1401. xs_html_attr("name", "cw"),
  1402. xs_html_attr("id", "cw"),
  1403. xs_html_attr(strcmp(cw, "open") == 0 ? "checked" : "", NULL)),
  1404. xs_html_tag("label",
  1405. xs_html_attr("for", "cw"),
  1406. xs_html_text(L("Always show sensitive content"))),
  1407. xs_html_tag("p",
  1408. xs_html_text(L("Email address for notifications:")),
  1409. xs_html_sctag("br", NULL),
  1410. xs_html_sctag("input",
  1411. xs_html_attr("type", "text"),
  1412. xs_html_attr("name", "email"),
  1413. xs_html_attr("value", email),
  1414. xs_html_attr("placeholder", "bob@example.com"))),
  1415. xs_html_tag("p",
  1416. xs_html_text(L("Telegram notifications (bot key and chat id):")),
  1417. xs_html_sctag("br", NULL),
  1418. xs_html_sctag("input",
  1419. xs_html_attr("type", "text"),
  1420. xs_html_attr("name", "telegram_bot"),
  1421. xs_html_attr("value", telegram_bot),
  1422. xs_html_attr("placeholder", L("Bot API key"))),
  1423. xs_html_text(" "),
  1424. xs_html_sctag("input",
  1425. xs_html_attr("type", "text"),
  1426. xs_html_attr("name", "telegram_chat_id"),
  1427. xs_html_attr("value", telegram_chat_id),
  1428. xs_html_attr("placeholder", L("Chat id")))),
  1429. xs_html_tag("p",
  1430. xs_html_text(L("ntfy notifications (ntfy server and token):")),
  1431. xs_html_sctag("br", NULL),
  1432. xs_html_sctag("input",
  1433. xs_html_attr("type", "text"),
  1434. xs_html_attr("name", "ntfy_server"),
  1435. xs_html_attr("value", ntfy_server),
  1436. xs_html_attr("placeholder", L("ntfy server - full URL (example: https://ntfy.sh/YourTopic)"))),
  1437. xs_html_text(" "),
  1438. xs_html_sctag("input",
  1439. xs_html_attr("type", "text"),
  1440. xs_html_attr("name", "ntfy_token"),
  1441. xs_html_attr("value", ntfy_token),
  1442. xs_html_attr("placeholder", L("ntfy token - if needed")))),
  1443. xs_html_tag("p",
  1444. xs_html_text(L("Notify webhook:")),
  1445. xs_html_sctag("br", NULL),
  1446. xs_html_sctag("input",
  1447. xs_html_attr("type", "url"),
  1448. xs_html_attr("name", "notify_webhook"),
  1449. xs_html_attr("value", webhook),
  1450. xs_html_attr("placeholder", L("http://example.com/webhook")))),
  1451. xs_html_tag("p",
  1452. xs_html_text(L("Maximum days to keep posts (0: server settings):")),
  1453. xs_html_sctag("br", NULL),
  1454. xs_html_sctag("input",
  1455. xs_html_attr("type", "number"),
  1456. xs_html_attr("name", "purge_days"),
  1457. xs_html_attr("value", purge_days))),
  1458. xs_html_tag("p",
  1459. xs_html_sctag("input",
  1460. xs_html_attr("type", "checkbox"),
  1461. xs_html_attr("name", "drop_dm_from_unknown"),
  1462. xs_html_attr("id", "drop_dm_from_unknown"),
  1463. xs_html_attr(xs_type(d_dm_f_u) == XSTYPE_TRUE ? "checked" : "", NULL)),
  1464. xs_html_tag("label",
  1465. xs_html_attr("for", "drop_dm_from_unknown"),
  1466. xs_html_text(L("Drop direct messages from people you don't follow")))),
  1467. xs_html_tag("p",
  1468. xs_html_sctag("input",
  1469. xs_html_attr("type", "checkbox"),
  1470. xs_html_attr("name", "bot"),
  1471. xs_html_attr("id", "bot"),
  1472. xs_html_attr(xs_type(bot) == XSTYPE_TRUE ? "checked" : "", NULL)),
  1473. xs_html_tag("label",
  1474. xs_html_attr("for", "bot"),
  1475. xs_html_text(L("This account is a bot")))),
  1476. xs_html_tag("p",
  1477. xs_html_sctag("input",
  1478. xs_html_attr("type", "checkbox"),
  1479. xs_html_attr("name", "auto_boost"),
  1480. xs_html_attr("id", "auto_boost"),
  1481. xs_html_attr(xs_is_true(auto_boost) ? "checked" : "", NULL)),
  1482. xs_html_tag("label",
  1483. xs_html_attr("for", "auto_boost"),
  1484. xs_html_text(L("Auto-boost all mentions to this account")))),
  1485. xs_html_tag("p",
  1486. xs_html_sctag("input",
  1487. xs_html_attr("type", "checkbox"),
  1488. xs_html_attr("name", "private"),
  1489. xs_html_attr("id", "private"),
  1490. xs_html_attr(xs_type(a_private) == XSTYPE_TRUE ? "checked" : "", NULL)),
  1491. xs_html_tag("label",
  1492. xs_html_attr("for", "private"),
  1493. xs_html_text(L("This account is private "
  1494. "(posts are not shown through the web)")))),
  1495. xs_html_tag("p",
  1496. xs_html_sctag("input",
  1497. xs_html_attr("type", "checkbox"),
  1498. xs_html_attr("name", "collapse_threads"),
  1499. xs_html_attr("id", "collapse_threads"),
  1500. xs_html_attr(xs_is_true(coll_thrds) ? "checked" : "", NULL)),
  1501. xs_html_tag("label",
  1502. xs_html_attr("for", "collapse_threads"),
  1503. xs_html_text(L("Collapse top threads by default")))),
  1504. xs_html_tag("p",
  1505. xs_html_sctag("input",
  1506. xs_html_attr("type", "checkbox"),
  1507. xs_html_attr("name", "approve_followers"),
  1508. xs_html_attr("id", "approve_followers"),
  1509. xs_html_attr(xs_is_true(pending) ? "checked" : "", NULL)),
  1510. xs_html_tag("label",
  1511. xs_html_attr("for", "approve_followers"),
  1512. xs_html_text(L("Follow requests must be approved")))),
  1513. xs_html_tag("p",
  1514. xs_html_sctag("input",
  1515. xs_html_attr("type", "checkbox"),
  1516. xs_html_attr("name", "show_contact_metrics"),
  1517. xs_html_attr("id", "show_contact_metrics"),
  1518. xs_html_attr(xs_is_true(show_foll) ? "checked" : "", NULL)),
  1519. xs_html_tag("label",
  1520. xs_html_attr("for", "show_contact_metrics"),
  1521. xs_html_text(L("Publish follower and following metrics")))),
  1522. xs_html_tag("p",
  1523. xs_html_text(L("Current location:")),
  1524. xs_html_sctag("br", NULL),
  1525. xs_html_sctag("input",
  1526. xs_html_attr("type", "text"),
  1527. xs_html_attr("name", "latitude"),
  1528. xs_html_attr("value", latitude),
  1529. xs_html_attr("placeholder", "latitude")),
  1530. xs_html_text(" "),
  1531. xs_html_sctag("input",
  1532. xs_html_attr("type", "text"),
  1533. xs_html_attr("name", "longitude"),
  1534. xs_html_attr("value", longitude),
  1535. xs_html_attr("placeholder", "longitude"))),
  1536. xs_html_tag("p",
  1537. xs_html_text(L("Profile metadata (key=value pairs in each line):")),
  1538. xs_html_sctag("br", NULL),
  1539. xs_html_tag("textarea",
  1540. xs_html_attr("name", "metadata"),
  1541. xs_html_attr("cols", "40"),
  1542. xs_html_attr("rows", "4"),
  1543. xs_html_attr("placeholder", "Blog=https:/"
  1544. "/example.com/my-blog\nGPG Key=1FA54\n..."),
  1545. xs_html_text(metadata))),
  1546. xs_html_tag("p",
  1547. xs_html_text(L("Web interface language:")),
  1548. xs_html_sctag("br", NULL),
  1549. lang_select),
  1550. xs_html_tag("p",
  1551. xs_html_text(L("Time zone:")),
  1552. xs_html_sctag("br", NULL),
  1553. tz_select),
  1554. xs_html_tag("p",
  1555. xs_html_text(L("Languages you usually post in:")),
  1556. xs_html_sctag("br", NULL),
  1557. xs_html_sctag("input",
  1558. xs_html_attr("type", "text"),
  1559. xs_html_attr("name", "post_langs"),
  1560. xs_html_attr("value", post_langs),
  1561. xs_html_attr("placeholder", L("en fr es de_AT")))),
  1562. xs_html_tag("p",
  1563. xs_html_text(L("New password:")),
  1564. xs_html_sctag("br", NULL),
  1565. xs_html_sctag("input",
  1566. xs_html_attr("type", "password"),
  1567. xs_html_attr("name", "passwd1"),
  1568. xs_html_attr("value", ""))),
  1569. xs_html_tag("p",
  1570. xs_html_text(L("Repeat new password:")),
  1571. xs_html_sctag("br", NULL),
  1572. xs_html_sctag("input",
  1573. xs_html_attr("type", "password"),
  1574. xs_html_attr("name", "passwd2"),
  1575. xs_html_attr("value", ""))),
  1576. xs_html_sctag("input",
  1577. xs_html_attr("type", "submit"),
  1578. xs_html_attr("class", "button"),
  1579. xs_html_attr("value", L("Update user info"))),
  1580. xs_html_tag("p", NULL)))));
  1581. xs *followed_hashtags_action = xs_fmt("%s/admin/followed-hashtags", user->actor);
  1582. xs *followed_hashtags = xs_join(xs_dict_get_def(user->config,
  1583. "followed_hashtags", xs_stock(XSTYPE_LIST)), "\n");
  1584. xs_html_add(top_controls,
  1585. xs_html_tag("details",
  1586. xs_html_tag("summary",
  1587. xs_html_text(L("Followed hashtags..."))),
  1588. xs_html_tag("p",
  1589. xs_html_text(L("One hashtag per line"))),
  1590. xs_html_tag("div",
  1591. xs_html_attr("class", "snac-followed-hashtags"),
  1592. xs_html_tag("form",
  1593. xs_html_attr("autocomplete", "off"),
  1594. xs_html_attr("method", "post"),
  1595. xs_html_attr("action", followed_hashtags_action),
  1596. xs_html_attr("enctype", "multipart/form-data"),
  1597. xs_html_tag("textarea",
  1598. xs_html_attr("name", "followed_hashtags"),
  1599. xs_html_attr("cols", "40"),
  1600. xs_html_attr("rows", "4"),
  1601. xs_html_attr("placeholder", "#cats\n#windowfriday\n#classicalmusic\nhttps:/"
  1602. "/mastodon.social/tags/dogs"),
  1603. xs_html_text(followed_hashtags)),
  1604. xs_html_tag("br", NULL),
  1605. xs_html_sctag("input",
  1606. xs_html_attr("type", "submit"),
  1607. xs_html_attr("class", "button"),
  1608. xs_html_attr("value", L("Update hashtags")))))));
  1609. xs *blocked_hashtags_action = xs_fmt("%s/admin/blocked-hashtags", user->actor);
  1610. xs *blocked_hashtags = xs_join(xs_dict_get_def(user->config,
  1611. "blocked_hashtags", xs_stock(XSTYPE_LIST)), "\n");
  1612. xs_html_add(top_controls,
  1613. xs_html_tag("details",
  1614. xs_html_tag("summary",
  1615. xs_html_text(L("Blocked hashtags..."))),
  1616. xs_html_tag("p",
  1617. xs_html_text(L("One hashtag per line"))),
  1618. xs_html_tag("div",
  1619. xs_html_attr("class", "snac-blocked-hashtags"),
  1620. xs_html_tag("form",
  1621. xs_html_attr("autocomplete", "off"),
  1622. xs_html_attr("method", "post"),
  1623. xs_html_attr("action", blocked_hashtags_action),
  1624. xs_html_attr("enctype", "multipart/form-data"),
  1625. xs_html_tag("textarea",
  1626. xs_html_attr("name", "blocked_hashtags"),
  1627. xs_html_attr("cols", "40"),
  1628. xs_html_attr("rows", "4"),
  1629. xs_html_attr("placeholder", "#cats\n#windowfriday\n#classicalmusic"),
  1630. xs_html_text(blocked_hashtags)),
  1631. xs_html_tag("br", NULL),
  1632. xs_html_sctag("input",
  1633. xs_html_attr("type", "submit"),
  1634. xs_html_attr("class", "button"),
  1635. xs_html_attr("value", L("Update hashtags")))))));
  1636. xs *muted_words_action = xs_fmt("%s/admin/muted-words", user->actor);
  1637. xs *muted_words = xs_join(xs_dict_get_def(user->config,
  1638. "muted_words", xs_stock(XSTYPE_LIST)), "\n");
  1639. xs_html_add(top_controls,
  1640. xs_html_tag("details",
  1641. xs_html_tag("summary",
  1642. xs_html_text(L("Muted words..."))),
  1643. xs_html_tag("p",
  1644. xs_html_text(L("One word per line, partial matches count"))),
  1645. xs_html_tag("div",
  1646. xs_html_attr("class", "snac-muted-words"),
  1647. xs_html_tag("form",
  1648. xs_html_attr("autocomplete", "off"),
  1649. xs_html_attr("method", "post"),
  1650. xs_html_attr("action", muted_words_action),
  1651. xs_html_attr("enctype", "multipart/form-data"),
  1652. xs_html_tag("textarea",
  1653. xs_html_attr("name", "muted_words"),
  1654. xs_html_attr("cols", "40"),
  1655. xs_html_attr("rows", "4"),
  1656. xs_html_attr("placeholder", "nascar\nsuperbowl\nFIFA"),
  1657. xs_html_text(muted_words)),
  1658. xs_html_tag("br", NULL),
  1659. xs_html_sctag("input",
  1660. xs_html_attr("type", "submit"),
  1661. xs_html_attr("class", "button"),
  1662. xs_html_attr("value", L("Update muted words")))))));
  1663. return top_controls;
  1664. }
  1665. static xs_html *html_button(const char *clss, const char *label, const char *hint)
  1666. {
  1667. xs *c = xs_fmt("snac-btn-%s", clss);
  1668. /* use an NULL tag to separate non-css-classed buttons from one another */
  1669. return xs_html_container(
  1670. xs_html_sctag("input",
  1671. xs_html_attr("type", "submit"),
  1672. xs_html_attr("name", "action"),
  1673. xs_html_attr("class", c),
  1674. xs_html_attr("value", label),
  1675. xs_html_attr("title", hint)),
  1676. xs_html_text("\n"));
  1677. }
  1678. xs_str *build_mentions(snac *user, const xs_dict *msg)
  1679. /* returns a string with the mentions in msg */
  1680. {
  1681. xs_str *s = xs_str_new(NULL);
  1682. const char *list = xs_dict_get(msg, "tag");
  1683. const char *v;
  1684. int c = 0;
  1685. while (xs_list_next(list, &v, &c)) {
  1686. const char *type = xs_dict_get(v, "type");
  1687. const char *href = xs_dict_get(v, "href");
  1688. const char *name = xs_dict_get(v, "name");
  1689. if (type && strcmp(type, "Mention") == 0 &&
  1690. href && strcmp(href, user->actor) != 0 && name) {
  1691. xs *s1 = NULL;
  1692. if (name[0] != '@') {
  1693. s1 = xs_fmt("@%s", name);
  1694. name = s1;
  1695. }
  1696. xs *l = xs_split(name, "@");
  1697. /* is it a name without a host? */
  1698. if (xs_list_len(l) < 3) {
  1699. /* split the href and pick the host name LIKE AN ANIMAL */
  1700. /* would be better to query the webfinger but *won't do that* here */
  1701. xs *l2 = xs_split(href, "/");
  1702. if (xs_list_len(l2) >= 3) {
  1703. xs *s1 = xs_fmt("%s@%s ", name, xs_list_get(l2, 2));
  1704. if (xs_str_in(s, s1) == -1)
  1705. s = xs_str_cat(s, s1);
  1706. }
  1707. }
  1708. else {
  1709. if (xs_str_in(s, name) == -1) {
  1710. s = xs_str_cat(s, name);
  1711. s = xs_str_cat(s, " ");
  1712. }
  1713. }
  1714. }
  1715. }
  1716. if (*s) {
  1717. xs *s1 = s;
  1718. s = xs_fmt("\n\n\nCC: %s", s1);
  1719. }
  1720. return s;
  1721. }
  1722. xs_html *html_entry_controls(snac *user, const char *actor,
  1723. const xs_dict *msg, const char *md5)
  1724. {
  1725. const char *id = xs_dict_get(msg, "id");
  1726. const char *group = xs_dict_get(msg, "audience");
  1727. xs *likes = object_likes(id);
  1728. xs *boosts = object_announces(id);
  1729. xs *action = xs_fmt("%s/admin/action", user->actor);
  1730. xs *redir = xs_fmt("%s_entry", md5);
  1731. xs_html *form;
  1732. xs_html *controls = xs_html_tag("div",
  1733. xs_html_attr("class", "snac-controls"),
  1734. form = xs_html_tag("form",
  1735. xs_html_attr("autocomplete", "off"),
  1736. xs_html_attr("method", "post"),
  1737. xs_html_attr("action", action),
  1738. xs_html_sctag("input",
  1739. xs_html_attr("type", "hidden"),
  1740. xs_html_attr("name", "id"),
  1741. xs_html_attr("value", id)),
  1742. xs_html_sctag("input",
  1743. xs_html_attr("type", "hidden"),
  1744. xs_html_attr("name", "actor"),
  1745. xs_html_attr("value", actor)),
  1746. xs_html_sctag("input",
  1747. xs_html_attr("type", "hidden"),
  1748. xs_html_attr("name", "group"),
  1749. xs_html_attr("value", xs_is_null(group) ? "" : group)),
  1750. xs_html_sctag("input",
  1751. xs_html_attr("type", "hidden"),
  1752. xs_html_attr("name", "redir"),
  1753. xs_html_attr("value", redir))));
  1754. if (!is_msg_mine(user, id)) {
  1755. if (xs_list_in(likes, user->md5) == -1) {
  1756. /* not already liked; add button */
  1757. xs_html_add(form,
  1758. html_button("like", L("Like"), L("Say you like this post")));
  1759. }
  1760. else {
  1761. /* not like it anymore */
  1762. xs_html_add(form,
  1763. html_button("unlike", L("Unlike"), L("Nah don't like it that much")));
  1764. }
  1765. }
  1766. else {
  1767. if (is_pinned(user, id))
  1768. xs_html_add(form,
  1769. html_button("unpin", L("Unpin"), L("Unpin this post from your timeline")));
  1770. else
  1771. xs_html_add(form,
  1772. html_button("pin", L("Pin"), L("Pin this post to the top of your timeline")));
  1773. }
  1774. if (is_msg_public(msg)) {
  1775. if (xs_list_in(boosts, user->md5) == -1) {
  1776. /* not already boosted; add button */
  1777. xs_html_add(form,
  1778. html_button("boost", L("Boost"), L("Announce this post to your followers")));
  1779. }
  1780. else {
  1781. /* already boosted; add button to regret */
  1782. xs_html_add(form,
  1783. html_button("unboost", L("Unboost"), L("I regret I boosted this")));
  1784. }
  1785. }
  1786. if (is_bookmarked(user, id))
  1787. xs_html_add(form,
  1788. html_button("unbookmark", L("Unbookmark"), L("Delete this post from your bookmarks")));
  1789. else
  1790. xs_html_add(form,
  1791. html_button("bookmark", L("Bookmark"), L("Add this post to your bookmarks")));
  1792. if (strcmp(actor, user->actor) != 0) {
  1793. /* controls for other actors than this one */
  1794. if (following_check(user, actor)) {
  1795. xs_html_add(form,
  1796. html_button("unfollow", L("Unfollow"), L("Stop following this user's activity")));
  1797. }
  1798. else {
  1799. xs_html_add(form,
  1800. html_button("follow", L("Follow"), L("Start following this user's activity")));
  1801. }
  1802. if (!xs_is_null(group)) {
  1803. if (following_check(user, group)) {
  1804. xs_html_add(form,
  1805. html_button("unfollow", L("Unfollow Group"),
  1806. L("Stop following this group or channel")));
  1807. }
  1808. else {
  1809. xs_html_add(form,
  1810. html_button("follow", L("Follow Group"),
  1811. L("Start following this group or channel")));
  1812. }
  1813. }
  1814. xs_html_add(form,
  1815. html_button("mute", L("MUTE"),
  1816. L("Block any activity from this user forever")));
  1817. }
  1818. if (!xs_is_true(xs_dict_get(srv_config, "hide_delete_post_button")))
  1819. xs_html_add(form,
  1820. html_button("delete", L("Delete"), L("Delete this post")));
  1821. xs_html_add(form,
  1822. html_button("hide", L("Hide"), L("Hide this post and its children")));
  1823. const char *prev_src = xs_dict_get(msg, "sourceContent");
  1824. if (!xs_is_null(prev_src) && strcmp(actor, user->actor) == 0) { /** edit **/
  1825. /* post can be edited */
  1826. xs *div_id = xs_fmt("%s_edit", md5);
  1827. xs *form_id = xs_fmt("%s_edit_form", md5);
  1828. xs *redir = xs_fmt("%s_entry", md5);
  1829. xs *att_files = xs_list_new();
  1830. xs *att_alt_texts = xs_list_new();
  1831. const xs_list *att_list = xs_dict_get(msg, "attachment");
  1832. if (xs_is_list(att_list)) {
  1833. const xs_dict *d;
  1834. xs_list_foreach(att_list, d) {
  1835. const char *att_file = xs_dict_get(d, "url");
  1836. const char *att_alt_text = xs_dict_get(d, "name");
  1837. if (xs_is_string(att_file) && xs_is_string(att_alt_text)) {
  1838. att_files = xs_list_append(att_files, att_file);
  1839. att_alt_texts = xs_list_append(att_alt_texts, att_alt_text);
  1840. }
  1841. }
  1842. }
  1843. const char *note_lang = NULL;
  1844. const xs_dict *cmap = xs_dict_get(msg, "contentMap");
  1845. if (xs_is_dict(cmap)) {
  1846. const char *dummy;
  1847. int c = 0;
  1848. xs_dict_next(cmap, &note_lang, &dummy, &c);
  1849. }
  1850. const int edit_scope = get_msg_visibility(msg);
  1851. xs_html_add(controls, xs_html_tag("div",
  1852. xs_html_tag("p", NULL),
  1853. html_note(user, L("Edit..."),
  1854. div_id, form_id,
  1855. "", prev_src,
  1856. id, NULL,
  1857. xs_dict_get(msg, "sensitive"), xs_dict_get(msg, "summary"),
  1858. edit_scope, redir,
  1859. NULL, 0, att_files, att_alt_texts, is_draft(user, id),
  1860. xs_dict_get(msg, "published"), note_lang)),
  1861. xs_html_tag("p", NULL));
  1862. }
  1863. if (!xs_is_true(xs_dict_get(srv_config, "disable_emojireact"))) { /** emoji react **/
  1864. /* the post textarea */
  1865. xs *div_id = xs_fmt("%s_reply", md5);
  1866. xs *form_id = xs_fmt("%s_reply_form", md5);
  1867. xs *e_react = emoji_reacted(user, id);
  1868. xs_html_add(controls, xs_html_tag("div",
  1869. xs_html_tag("p", NULL),
  1870. html_emoji(
  1871. user, L("Emoji react..."),
  1872. div_id, form_id,
  1873. ":neocat:", id,
  1874. e_react)),
  1875. xs_html_tag("p", NULL));
  1876. }
  1877. { /** reply **/
  1878. /* the post textarea */
  1879. xs *ct = build_mentions(user, msg);
  1880. xs *div_id = xs_fmt("%s_reply", md5);
  1881. xs *form_id = xs_fmt("%s_reply_form", md5);
  1882. xs *redir = xs_fmt("%s_entry", md5);
  1883. const int scope = get_msg_visibility(msg);
  1884. xs_html_add(controls, xs_html_tag("div",
  1885. xs_html_tag("p", NULL),
  1886. html_note(user, L("Reply..."),
  1887. div_id, form_id,
  1888. "", ct,
  1889. NULL, NULL,
  1890. xs_dict_get(msg, "sensitive"), xs_dict_get(msg, "summary"),
  1891. scope, redir,
  1892. id, 0, NULL, NULL, 0, NULL, NULL)),
  1893. xs_html_tag("p", NULL));
  1894. }
  1895. return controls;
  1896. }
  1897. static const xs_str* words_in_content(const xs_list *words, const xs_val *content)
  1898. /* returns a word that matches any of the words in content */
  1899. {
  1900. if (!xs_is_list(words) || !xs_is_string(content)) {
  1901. return NULL;
  1902. }
  1903. xs *c = xs_split(content, " ");
  1904. xs *sc = xs_list_sort(c, NULL);
  1905. const xs_str *wv;
  1906. const xs_str *cv;
  1907. xs_list_foreach(words, wv) {
  1908. xs_list_foreach(sc, cv) {
  1909. xs_tolower_i((xs_str*)cv);
  1910. if(xs_str_in(cv, wv) != -1){
  1911. return wv;
  1912. }
  1913. }
  1914. }
  1915. return NULL;
  1916. }
  1917. xs_html *html_entry(snac *user, xs_dict *msg, int read_only,
  1918. int level, const char *md5, int hide_children)
  1919. {
  1920. const char *id = xs_dict_get(msg, "id");
  1921. const char *type = xs_dict_get(msg, "type");
  1922. const char *actor;
  1923. const char *v;
  1924. int has_title = 0;
  1925. int collapse_threads = 0;
  1926. const char *proxy = NULL;
  1927. if (user && !read_only && xs_is_true(xs_dict_get(srv_config, "proxy_media")))
  1928. proxy = user->actor;
  1929. /* do not show non-public messages in the public timeline */
  1930. if ((read_only || !user) && !is_msg_public(msg))
  1931. return NULL;
  1932. if (id && is_instance_blocked(id))
  1933. return NULL;
  1934. if (user && level == 0 && xs_is_true(xs_dict_get(user->config, "collapse_threads")))
  1935. collapse_threads = 1;
  1936. /* hidden? do nothing more for this conversation */
  1937. if (user && is_hidden(user, id)) {
  1938. xs *s1 = xs_fmt("%s_entry", md5);
  1939. /* return just an dummy anchor, to keep position after hitting 'Hide' */
  1940. return xs_html_tag("div",
  1941. xs_html_tag("a",
  1942. xs_html_attr("name", s1)));
  1943. }
  1944. /* avoid too deep nesting, as it may be a loop */
  1945. if (level >= MAX_CONVERSATION_LEVELS)
  1946. return xs_html_tag("mark",
  1947. xs_html_text(L("Truncated (too deep)")));
  1948. const char *lang = NULL;
  1949. const xs_dict *cmap = xs_dict_get(msg, "contentMap");
  1950. if (xs_is_dict(cmap)) {
  1951. const char *dummy;
  1952. int c = 0;
  1953. xs_dict_next(cmap, &lang, &dummy, &c);
  1954. }
  1955. if (strcmp(type, "Follow") == 0) {
  1956. return xs_html_tag("div",
  1957. xs_html_attr("class", "snac-post"),
  1958. xs_html_tag("div",
  1959. xs_html_attr("class", "snac-post-header"),
  1960. xs_html_tag("div",
  1961. xs_html_attr("class", "snac-origin"),
  1962. xs_html_text(L("follows you"))),
  1963. html_msg_icon(read_only ? NULL : user, xs_dict_get(msg, "actor"), msg, proxy, NULL, lang)));
  1964. }
  1965. else
  1966. if (!xs_match(type, POSTLIKE_OBJECT_TYPE)) {
  1967. /* skip oddities */
  1968. snac_debug(user, 1, xs_fmt("html_entry: ignoring object type '%s' %s", type, id));
  1969. return NULL;
  1970. }
  1971. /* ignore notes with "name", as they are votes to Questions */
  1972. if (strcmp(type, "Note") == 0 && !xs_is_null(xs_dict_get(msg, "name")))
  1973. return NULL;
  1974. /* get the attributedTo */
  1975. if ((actor = get_atto(msg)) == NULL)
  1976. return NULL;
  1977. /* ignore muted morons immediately */
  1978. if (user && is_muted(user, actor)) {
  1979. xs *s1 = xs_fmt("%s_entry", md5);
  1980. /* return just an dummy anchor, to keep position after hitting 'MUTE' */
  1981. return xs_html_tag("div",
  1982. xs_html_tag("a",
  1983. xs_html_attr("name", s1)));
  1984. }
  1985. /* don't show followers-only notes from not followed users */
  1986. if (user && get_msg_visibility(msg) == SCOPE_FOLLOWERS &&
  1987. strcmp(user->actor, actor) != 0 && following_check(user, actor) == 0) {
  1988. return NULL;
  1989. }
  1990. if ((user == NULL || strcmp(actor, user->actor) != 0)
  1991. && !valid_status(actor_get(actor, NULL))) {
  1992. if (user)
  1993. enqueue_actor_refresh(user, actor, 0);
  1994. return NULL;
  1995. }
  1996. /** html_entry top tag **/
  1997. xs_html *entry_top = xs_html_tag("div", NULL);
  1998. {
  1999. xs *s1 = xs_fmt("%s_entry", md5);
  2000. xs_html_add(entry_top,
  2001. xs_html_tag("a",
  2002. xs_html_attr("name", s1)));
  2003. }
  2004. xs_html *entry = xs_html_tag("div",
  2005. xs_html_attr("class", level == 0 ? "snac-post" : "snac-child"));
  2006. xs_html_add(entry_top,
  2007. entry);
  2008. /** post header **/
  2009. xs_html *score;
  2010. xs_html *post_header = xs_html_tag("div",
  2011. xs_html_attr("class", "snac-post-header"),
  2012. score = xs_html_tag("div",
  2013. xs_html_attr("class", "snac-score")));
  2014. xs_html_add(entry,
  2015. post_header);
  2016. if (user && is_pinned(user, id)) {
  2017. /* add a pin emoji */
  2018. xs_html_add(score,
  2019. xs_html_tag("span",
  2020. xs_html_attr("title", L("Pinned")),
  2021. xs_html_raw(" &#128204; ")));
  2022. }
  2023. if (user && !read_only && is_bookmarked(user, id)) {
  2024. /* add a bookmark emoji */
  2025. xs_html_add(score,
  2026. xs_html_tag("span",
  2027. xs_html_attr("title", L("Bookmarked")),
  2028. xs_html_raw(" &#128278; ")));
  2029. }
  2030. if (strcmp(type, "Question") == 0) {
  2031. /* add the ballot box emoji */
  2032. xs_html_add(score,
  2033. xs_html_tag("span",
  2034. xs_html_attr("title", L("Poll")),
  2035. xs_html_raw(" &#128499; ")));
  2036. if (user && was_question_voted(user, id)) {
  2037. /* add a check to show this poll was voted */
  2038. xs_html_add(score,
  2039. xs_html_tag("span",
  2040. xs_html_attr("title", L("Voted")),
  2041. xs_html_raw(" &#10003; ")));
  2042. }
  2043. }
  2044. if (strcmp(type, "Event") == 0) {
  2045. /* add the calendar emoji */
  2046. xs_html_add(score,
  2047. xs_html_tag("span",
  2048. xs_html_attr("title", L("Event")),
  2049. xs_html_raw(" &#128197; ")));
  2050. }
  2051. /* if it's a user from this same instance, add the score */
  2052. if (xs_startswith(id, srv_baseurl)) {
  2053. int n_likes = object_likes_len(id);
  2054. int n_boosts = object_announces_len(id);
  2055. /* alternate emojis: %d &#128077; %d &#128257; */
  2056. xs *s1 = xs_fmt("%d &#9733; %d &#8634;\n", n_likes, n_boosts);
  2057. xs_html_add(score,
  2058. xs_html_raw(s1));
  2059. }
  2060. xs *boosts = object_announces(id);
  2061. if (xs_list_len(boosts)) {
  2062. /* if somebody boosted this, show as origin */
  2063. const char *p = xs_list_get(boosts, -1);
  2064. xs *actor_r = NULL;
  2065. if (user && xs_list_in(boosts, user->md5) != -1) {
  2066. /* we boosted this */
  2067. xs_html_add(post_header,
  2068. xs_html_tag("div",
  2069. xs_html_attr("class", "snac-origin"),
  2070. xs_html_tag("a",
  2071. xs_html_attr("href", user->actor),
  2072. xs_html_text(xs_dict_get(user->config, "name"))),
  2073. xs_html_text(" "),
  2074. xs_html_text(L("boosted"))));
  2075. }
  2076. else
  2077. if (valid_status(object_get_by_md5(p, &actor_r))) {
  2078. xs *name = actor_name(actor_r, proxy);
  2079. if (!xs_is_null(name)) {
  2080. xs *href = NULL;
  2081. const char *id = xs_dict_get(actor_r, "id");
  2082. int fwers = 0;
  2083. int fwing = 0;
  2084. if (user != NULL) {
  2085. fwers = follower_check(user, id);
  2086. fwing = following_check(user, id);
  2087. }
  2088. if (!read_only && (fwers || fwing))
  2089. href = xs_fmt("%s/people/%s", user->actor, p);
  2090. else
  2091. href = xs_dup(id);
  2092. xs_html_add(post_header,
  2093. xs_html_tag("div",
  2094. xs_html_attr("class", "snac-origin"),
  2095. xs_html_tag("a",
  2096. xs_html_attr("href", href),
  2097. xs_html_raw(name)), /* already sanitized */
  2098. xs_html_text(" "),
  2099. xs_html_text(L("boosted"))));
  2100. }
  2101. }
  2102. }
  2103. if (user && strcmp(type, "Note") == 0) {
  2104. /* is the parent not here? */
  2105. const char *parent = get_in_reply_to(msg);
  2106. if (!xs_is_null(parent) && *parent) {
  2107. if (!timeline_here(user, parent)) {
  2108. xs_html_add(post_header,
  2109. xs_html_tag("div",
  2110. xs_html_attr("class", "snac-origin"),
  2111. xs_html_text(L("in reply to")),
  2112. xs_html_text(" "),
  2113. xs_html_tag("a",
  2114. xs_html_attr("href", parent),
  2115. xs_html_text("»"))));
  2116. }
  2117. }
  2118. }
  2119. xs_html_add(post_header,
  2120. html_msg_icon(read_only ? NULL : user, actor, msg, proxy, md5, lang));
  2121. /** post content **/
  2122. xs_html *snac_content_wrap = xs_html_tag("div",
  2123. xs_html_attr("class", "e-content snac-content"));
  2124. if (xs_is_string(lang))
  2125. xs_html_add(snac_content_wrap,
  2126. xs_html_attr("lang", lang));
  2127. xs_html_add(entry,
  2128. snac_content_wrap);
  2129. if (!has_title && !xs_is_null(v = xs_dict_get(msg, "name"))) {
  2130. xs_html_add(snac_content_wrap,
  2131. xs_html_tag("h3",
  2132. xs_html_attr("class", "snac-entry-title"),
  2133. xs_html_text(v)));
  2134. has_title = 1;
  2135. }
  2136. xs_html *snac_content = NULL;
  2137. v = xs_dict_get(msg, "summary");
  2138. /* if it has summary or marked as sensitive - add CW */
  2139. /* come clients don't set "sensitive" flag properly */
  2140. if ((!xs_is_null(v) && *v) || xs_type(xs_dict_get(msg, "sensitive")) == XSTYPE_TRUE) {
  2141. if (xs_is_null(v) || *v == '\0')
  2142. v = "...";
  2143. const char *cw = "";
  2144. if (user) {
  2145. /* only show it when not in the public timeline and the config setting is "open" */
  2146. cw = xs_dict_get(user->config, "cw");
  2147. if (xs_is_null(cw) || read_only)
  2148. cw = "";
  2149. }
  2150. snac_content = xs_html_tag("details",
  2151. xs_html_attr(cw, NULL),
  2152. xs_html_tag("summary",
  2153. xs_html_text(v),
  2154. xs_html_text(L(" [SENSITIVE CONTENT]"))));
  2155. }
  2156. else
  2157. if (user &&
  2158. /* muted_words is all lowercase and sorted for performance */
  2159. (v = words_in_content(xs_dict_get(user->config, "muted_words"),
  2160. xs_dict_get(msg, "content"))) != NULL) {
  2161. snac_debug(user, 1, xs_fmt("word %s muted by user preferences: %s", v, id));
  2162. snac_content = xs_html_tag("details",
  2163. xs_html_tag("summary",
  2164. xs_html_text(L("Muted: ")),
  2165. xs_html_text(v)));
  2166. }
  2167. else {
  2168. snac_content = xs_html_tag("div", NULL);
  2169. }
  2170. xs_html_add(snac_content_wrap,
  2171. snac_content);
  2172. /* add all emoji reacts */
  2173. int is_emoji = 0;
  2174. if (!xs_is_true(xs_dict_get(srv_config, "disable_emojireact"))) {
  2175. int c = 0;
  2176. const xs_dict *k;
  2177. xs *ls = xs_list_new();
  2178. xs *sfrl = xs_dict_new();
  2179. xs *rl = object_get_emoji_reacts(id);
  2180. while (xs_list_next(rl, &v, &c)) {
  2181. xs *m = NULL;
  2182. if (valid_status(object_get_by_md5(v, &m))) {
  2183. const char *content = xs_dict_get(m, "content");
  2184. const char *actor = xs_dict_get(m, "actor");
  2185. const xs_list *contentl = xs_dict_get(sfrl, content);
  2186. if ((user && is_muted(user, actor)) || is_instance_blocked(actor))
  2187. continue;
  2188. xs *actors = xs_list_new();
  2189. actors = xs_list_append(actors, actor);
  2190. char me = actor && user && strcmp(actor, user->actor) == 0;
  2191. int count = 1;
  2192. if (contentl) {
  2193. count = atoi(xs_list_get(contentl, 0)) + 1;
  2194. const xs_list *actorsc = xs_list_get(contentl, 1);
  2195. if (strncmp(xs_list_get(contentl, 2), "1", 1) == 0)
  2196. me = 1;
  2197. if (xs_list_in(actorsc, actor) != -1) {
  2198. xs_free(actors);
  2199. actors = xs_dup(actorsc);
  2200. }
  2201. else
  2202. actors = xs_list_cat(actors, actorsc);
  2203. }
  2204. xs *fl = xs_list_new();
  2205. xs *c1 = xs_fmt("%d", count);
  2206. xs *c2 = xs_fmt("%d", me);
  2207. fl = xs_list_append(fl, c1, actors, c2);
  2208. sfrl = xs_dict_append(sfrl, content, fl);
  2209. }
  2210. }
  2211. c = 0;
  2212. while (xs_list_next(rl, &k, &c)) {
  2213. xs *m = NULL;
  2214. if (valid_status(object_get_by_md5(k, &m))) {
  2215. const xs_dict *tag = xs_dict_get(m, "tag");
  2216. const xs_dict *ide = xs_dict_get(m, "id");
  2217. const char *content = xs_dict_get(m, "content");
  2218. const char *shortname;
  2219. shortname = xs_dict_get(m, "content");
  2220. const xs_list *items = xs_dict_get(sfrl, content);
  2221. if (!xs_is_null(items)) {
  2222. const char *nb = xs_list_get(items, 0);
  2223. const xs_list *actors = xs_list_get(items, 1);
  2224. const char me = *xs_list_get(items, 2) == '1';
  2225. is_emoji = 1;
  2226. xs *al = xs_join(actors, ",\n\t");
  2227. xs *act = atoi(nb) > 1 ?
  2228. xs_fmt("%d different actors \n\t%s", atoi(nb), al) :
  2229. xs_dup(xs_dict_get(m, "actor"));
  2230. xs *class = xs_list_new();
  2231. class = xs_list_append(class, "snac-reaction");
  2232. xs_html *ret = NULL;
  2233. if (tag && shortname) {
  2234. xs *cl = xs_list_new();
  2235. cl = xs_list_append(cl, "snac-reaction-image");
  2236. xs *emoji = _replace_shortnames(xs_dup(shortname), tag, 2, proxy, cl, act);
  2237. emoji = xs_strip_chars_i(emoji, ":");
  2238. if (me)
  2239. class = xs_list_append(class, "snac-reacted");
  2240. xs *l1 = xs_join(class, " ");
  2241. ret = xs_html_tag("button",
  2242. xs_html_attr("type", "submit"),
  2243. xs_html_attr("name", "action"),
  2244. xs_html_attr("value", me ? L("EmojiReact") : L("EmojiUnreact")),
  2245. xs_html_raw(emoji),
  2246. xs_html_tag("span",
  2247. xs_html_raw(nb),
  2248. xs_html_attr("style", "padding-left: 5px;")),
  2249. xs_html_attr("title", act),
  2250. xs_html_attr("class", l1));
  2251. if (!(ide && xs_startswith(ide, srv_baseurl)))
  2252. xs_html_add(ret, xs_html_attr("disabled", "true"));
  2253. }
  2254. else if (shortname) {
  2255. xs *sn = xs_dup(shortname);
  2256. const char *sna = sn;
  2257. unsigned int utf = xs_utf8_dec((const char **)&sna);
  2258. if (xs_is_emoji(utf)) {
  2259. const char *style = "font-size: large;";
  2260. if (me)
  2261. class = xs_list_append(class, "snac-reacted");
  2262. xs *l1 = xs_join(class, " ");
  2263. xs *s1 = xs_fmt("&#%d", utf);
  2264. ret = xs_html_tag("button",
  2265. xs_html_attr("type", "submit"),
  2266. xs_html_attr("name", "action"),
  2267. xs_html_attr("value", me ? L("EmojiUnreact") : L("EmojiReact")),
  2268. xs_html_raw(s1),
  2269. xs_html_tag("span",
  2270. xs_html_raw(nb),
  2271. xs_html_attr("style", "font-size: initial; padding-left: 5px;")),
  2272. xs_html_attr("title", act),
  2273. xs_html_attr("class", l1),
  2274. xs_html_attr("style", style));
  2275. }
  2276. }
  2277. if (ret) {
  2278. xs *s1;
  2279. if (user) {
  2280. xs *action = xs_fmt("%s/admin/action", user->actor);
  2281. xs *form_id = xs_fmt("%s_reply_form", md5);
  2282. xs_html *form =
  2283. xs_html_tag("form",
  2284. xs_html_attr("autocomplete", "off"),
  2285. xs_html_attr("method", "post"),
  2286. xs_html_attr("action", action),
  2287. xs_html_attr("enctype", "multipart/form-data"),
  2288. xs_html_attr("style", "display: inline-flex;"
  2289. "vertical-align: middle;"),
  2290. xs_html_attr("id", form_id),
  2291. xs_html_sctag("input",
  2292. xs_html_attr("type", "hidden"),
  2293. xs_html_attr("name", "id"),
  2294. xs_html_attr("value", id)),
  2295. xs_html_sctag("input",
  2296. xs_html_attr("type", "hidden"),
  2297. xs_html_attr("name", "eid"),
  2298. xs_html_attr("value", shortname)),
  2299. ret);
  2300. s1 = xs_html_render(form);
  2301. }
  2302. else
  2303. s1 = xs_html_render(ret);
  2304. ls = xs_list_append(ls, s1);
  2305. sfrl = xs_dict_del(sfrl, content);
  2306. }
  2307. }
  2308. }
  2309. }
  2310. c = 0;
  2311. xs_html *emoji_div;
  2312. if (xs_list_len(ls) > 0) {
  2313. emoji_div = xs_html_tag("div", xs_html_text(L("Emoji reactions: ")),
  2314. xs_html_attr("class", "snac-reaction-div"));
  2315. while (ls != NULL && xs_list_next(ls, &k, &c))
  2316. xs_html_add(emoji_div, xs_html_raw(k));
  2317. xs_html_add(snac_content_wrap, emoji_div);
  2318. }
  2319. }
  2320. {
  2321. /** build the content string **/
  2322. const char *content = xs_dict_get(msg, "content");
  2323. if (xs_type(content) != XSTYPE_STRING) {
  2324. if (!xs_is_null(content))
  2325. srv_archive_error("unexpected_content_xstype",
  2326. "content field type", xs_stock(XSTYPE_DICT), msg);
  2327. content = "";
  2328. }
  2329. /* skip ugly line breaks at the beginning */
  2330. while (xs_startswith(content, "<br>"))
  2331. content += 4;
  2332. xs *c = sanitize(content);
  2333. /* do some tweaks to the content */
  2334. c = xs_replace_i(c, "\r", "");
  2335. while (xs_endswith(c, "<br><br>"))
  2336. c = xs_crop_i(c, 0, -4);
  2337. c = xs_replace_i(c, "<br><br>", "<p>");
  2338. if (is_emoji == 0)
  2339. c = xs_str_cat(c, "<p>");
  2340. /* replace the :shortnames: */
  2341. c = replace_shortnames(c, xs_dict_get(msg, "tag"), 2, proxy);
  2342. /* Peertube videos content is in markdown */
  2343. const char *mtype = xs_dict_get(msg, "mediaType");
  2344. if (xs_type(mtype) == XSTYPE_STRING && strcmp(mtype, "text/markdown") == 0) {
  2345. /* a full conversion could be better */
  2346. c = xs_replace_i(c, "\r", "");
  2347. c = xs_replace_i(c, "\n", "<br>");
  2348. }
  2349. /* c contains sanitized HTML */
  2350. xs_html_add(snac_content,
  2351. xs_html_raw(c));
  2352. /* quoted post */
  2353. const char *quoted_id = xs_or(xs_dict_get(msg, "quoteUri"), xs_dict_get(msg, "quoteUrl"));
  2354. if (xs_is_string(quoted_id) && xs_match(quoted_id, "https://*|http://*")) { /** **/
  2355. xs *quoted_post = NULL;
  2356. if (valid_status(object_get(quoted_id, &quoted_post))) {
  2357. xs_html_add(snac_content,
  2358. xs_html_tag("blockquote",
  2359. xs_html_attr("class", "snac-quoted-post"),
  2360. html_entry(user, quoted_post, 1, 1, NULL, 1)));
  2361. }
  2362. else
  2363. if (user)
  2364. enqueue_object_request(user, quoted_id, 0);
  2365. }
  2366. }
  2367. if (strcmp(type, "Question") == 0) { /** question content **/
  2368. const xs_list *oo = xs_dict_get(msg, "oneOf");
  2369. const xs_list *ao = xs_dict_get(msg, "anyOf");
  2370. const xs_list *p;
  2371. const xs_dict *v;
  2372. int closed = 0;
  2373. const char *f_closed = NULL;
  2374. xs_html *poll = xs_html_tag("div", NULL);
  2375. if (read_only)
  2376. closed = 1; /* non-identified page; show as closed */
  2377. else
  2378. if (user && is_msg_mine(user, id))
  2379. closed = 1; /* we questioned; closed for us */
  2380. else
  2381. if (user && was_question_voted(user, id))
  2382. closed = 1; /* we already voted; closed for us */
  2383. if ((f_closed = xs_dict_get(msg, "closed")) != NULL) {
  2384. /* it has a closed date... but is it in the past? */
  2385. time_t t0 = time(NULL);
  2386. time_t t1 = xs_parse_iso_date(f_closed, 0);
  2387. if (t1 < t0)
  2388. closed = 2;
  2389. }
  2390. /* get the appropriate list of options */
  2391. p = oo != NULL ? oo : ao;
  2392. if (closed || user == NULL) {
  2393. /* closed poll */
  2394. xs_html *poll_result = xs_html_tag("table",
  2395. xs_html_attr("class", "snac-poll-result"));
  2396. int c = 0;
  2397. while (xs_list_next(p, &v, &c)) {
  2398. const char *name = xs_dict_get(v, "name");
  2399. const xs_dict *replies = xs_dict_get(v, "replies");
  2400. if (xs_is_string(name) && xs_is_dict(replies)) {
  2401. const char *ti = xs_number_str(xs_dict_get(replies, "totalItems"));
  2402. if (xs_is_string(ti))
  2403. xs_html_add(poll_result,
  2404. xs_html_tag("tr",
  2405. xs_html_tag("td",
  2406. xs_html_text(name),
  2407. xs_html_text(":")),
  2408. xs_html_tag("td",
  2409. xs_html_text(ti))));
  2410. }
  2411. }
  2412. xs_html_add(poll,
  2413. poll_result);
  2414. }
  2415. else {
  2416. /* poll still active */
  2417. xs *vote_action = xs_fmt("%s/admin/vote", user->actor);
  2418. xs_html *form;
  2419. xs_html *poll_form = xs_html_tag("div",
  2420. xs_html_attr("class", "snac-poll-form"),
  2421. form = xs_html_tag("form",
  2422. xs_html_attr("autocomplete", "off"),
  2423. xs_html_attr("method", "post"),
  2424. xs_html_attr("action", vote_action),
  2425. xs_html_sctag("input",
  2426. xs_html_attr("type", "hidden"),
  2427. xs_html_attr("name", "actor"),
  2428. xs_html_attr("value", actor)),
  2429. xs_html_sctag("input",
  2430. xs_html_attr("type", "hidden"),
  2431. xs_html_attr("name", "irt"),
  2432. xs_html_attr("value", id))));
  2433. int c = 0;
  2434. while (xs_list_next(p, &v, &c)) {
  2435. const char *name = xs_dict_get(v, "name");
  2436. const xs_dict *replies = xs_dict_get(v, "replies");
  2437. if (name) {
  2438. char *ti = (char *)xs_number_str(xs_dict_get(replies, "totalItems"));
  2439. xs_html *btn = xs_html_sctag("input",
  2440. xs_html_attr("id", name),
  2441. xs_html_attr("value", name),
  2442. xs_html_attr("name", "question"));
  2443. if (!xs_is_null(oo)) {
  2444. xs_html_add(btn,
  2445. xs_html_attr("type", "radio"),
  2446. xs_html_attr("required", "required"));
  2447. }
  2448. else
  2449. xs_html_add(btn,
  2450. xs_html_attr("type", "checkbox"));
  2451. xs_html_add(form,
  2452. btn,
  2453. xs_html_text(" "),
  2454. xs_html_tag("span",
  2455. xs_html_attr("title", ti),
  2456. xs_html_text(name)),
  2457. xs_html_sctag("br", NULL));
  2458. }
  2459. }
  2460. xs_html_add(form,
  2461. xs_html_tag("p", NULL),
  2462. xs_html_sctag("input",
  2463. xs_html_attr("type", "submit"),
  2464. xs_html_attr("class", "button"),
  2465. xs_html_attr("value", L("Vote"))));
  2466. xs_html_add(poll,
  2467. poll_form);
  2468. }
  2469. /* if it's *really* closed, say it */
  2470. if (closed == 2) {
  2471. xs_html_add(poll,
  2472. xs_html_tag("p",
  2473. xs_html_text(L("Closed"))));
  2474. }
  2475. else {
  2476. /* show when the poll closes */
  2477. const char *end_time = xs_dict_get(msg, "endTime");
  2478. /* Pleroma does not have an endTime field;
  2479. it has a closed time in the future */
  2480. if (xs_is_null(end_time))
  2481. end_time = xs_dict_get(msg, "closed");
  2482. if (!xs_is_null(end_time)) {
  2483. time_t t0 = time(NULL);
  2484. time_t t1 = xs_parse_iso_date(end_time, 0);
  2485. if (t1 > 0 && t1 > t0) {
  2486. time_t diff_time = t1 - t0;
  2487. xs *tf = xs_str_time_diff(diff_time);
  2488. char *p = tf;
  2489. /* skip leading zeros */
  2490. for (; *p == '0' || *p == ':'; p++);
  2491. xs_html_add(poll,
  2492. xs_html_tag("p",
  2493. xs_html_text(L("Closes in")),
  2494. xs_html_text(" "),
  2495. xs_html_text(p)));
  2496. }
  2497. }
  2498. }
  2499. xs_html_add(snac_content,
  2500. poll);
  2501. }
  2502. /** attachments **/
  2503. xs *attach = get_attachments(msg);
  2504. {
  2505. /* make custom css for attachments easier */
  2506. xs_html *content_attachments = xs_html_tag("div",
  2507. xs_html_attr("class", "snac-content-attachments"));
  2508. xs_html_add(snac_content,
  2509. content_attachments);
  2510. const char *content = xs_dict_get(msg, "content");
  2511. int c = 0;
  2512. const xs_dict *a;
  2513. while (xs_list_next(attach, &a, &c)) {
  2514. const char *type = xs_dict_get(a, "type");
  2515. const char *o_href = xs_dict_get(a, "href");
  2516. const char *name = xs_dict_get(a, "name");
  2517. if (!xs_is_string(type) || !xs_is_string(o_href))
  2518. continue;
  2519. /* if this URL is already in the post content, skip */
  2520. if (content && xs_str_in(content, o_href) != -1)
  2521. continue;
  2522. if (strcmp(type, "image/svg+xml") == 0 && !xs_is_true(xs_dict_get(srv_config, "enable_svg")))
  2523. continue;
  2524. /* do this attachment include an icon? */
  2525. const xs_dict *icon = xs_dict_get(a, "icon");
  2526. if (xs_type(icon) == XSTYPE_DICT) {
  2527. const char *icon_mtype = xs_dict_get(icon, "mediaType");
  2528. const char *icon_url = xs_dict_get(icon, "url");
  2529. if (icon_mtype && icon_url && xs_startswith(icon_mtype, "image/")) {
  2530. xs_html_add(content_attachments,
  2531. xs_html_tag("a",
  2532. xs_html_attr("href", icon_url),
  2533. xs_html_attr("target", "_blank"),
  2534. xs_html_sctag("img",
  2535. xs_html_attr("loading", "lazy"),
  2536. xs_html_attr("src", icon_url))));
  2537. }
  2538. }
  2539. xs *href = make_url(o_href, proxy, 0);
  2540. if (xs_startswith(type, "image/") || strcmp(type, "Image") == 0) {
  2541. xs_html_add(content_attachments,
  2542. xs_html_tag("a",
  2543. xs_html_attr("href", href),
  2544. xs_html_attr("target", "_blank"),
  2545. xs_html_sctag("img",
  2546. xs_html_attr("loading", "lazy"),
  2547. xs_html_attr("src", href),
  2548. xs_html_attr("alt", name),
  2549. xs_html_attr("title", name))));
  2550. }
  2551. else
  2552. if (xs_startswith(type, "video/") || strcmp(type, "Video") == 0) {
  2553. xs_html_add(content_attachments,
  2554. xs_html_tag("video",
  2555. xs_html_attr("preload", "none"),
  2556. xs_html_attr("style", "width: 100%"),
  2557. xs_html_attr("class", "snac-embedded-video"),
  2558. xs_html_attr("controls", NULL),
  2559. xs_html_attr("src", href),
  2560. xs_html_text(L("Video")),
  2561. xs_html_text(": "),
  2562. xs_html_tag("a",
  2563. xs_html_attr("href", href),
  2564. xs_html_text(name))));
  2565. }
  2566. else
  2567. if (xs_startswith(type, "audio/")) {
  2568. xs_html_add(content_attachments,
  2569. xs_html_tag("audio",
  2570. xs_html_attr("preload", "none"),
  2571. xs_html_attr("style", "width: 100%"),
  2572. xs_html_attr("class", "snac-embedded-audio"),
  2573. xs_html_attr("controls", NULL),
  2574. xs_html_attr("src", href),
  2575. xs_html_text(L("Audio")),
  2576. xs_html_text(": "),
  2577. xs_html_tag("a",
  2578. xs_html_attr("href", href),
  2579. xs_html_text(name))));
  2580. }
  2581. else
  2582. if (strcmp(type, "Link") == 0) {
  2583. xs_html_add(content_attachments,
  2584. xs_html_tag("p",
  2585. xs_html_tag("a",
  2586. xs_html_attr("href", o_href),
  2587. xs_html_text(href))));
  2588. /* do not generate an Alt... */
  2589. name = NULL;
  2590. }
  2591. else {
  2592. xs *d_href = xs_dup(o_href);
  2593. if (strlen(d_href) > 64) {
  2594. d_href[64] = '\0';
  2595. d_href = xs_str_cat(d_href, "...");
  2596. }
  2597. xs_html_add(content_attachments,
  2598. xs_html_tag("p",
  2599. xs_html_tag("a",
  2600. xs_html_attr("href", o_href),
  2601. xs_html_text(L("Attachment")),
  2602. xs_html_text(": "),
  2603. xs_html_text(d_href))));
  2604. /* do not generate an Alt... */
  2605. name = NULL;
  2606. }
  2607. if (name != NULL && *name) {
  2608. xs_html_add(content_attachments,
  2609. xs_html_tag("p",
  2610. xs_html_attr("class", "snac-alt-text"),
  2611. xs_html_tag("details",
  2612. xs_html_tag("summary",
  2613. xs_html_text(L("Alt..."))),
  2614. xs_html_text(name))));
  2615. }
  2616. }
  2617. }
  2618. /* has this message an audience (i.e., comes from a channel or community)? */
  2619. const char *audience = xs_dict_get(msg, "audience");
  2620. if (strcmp(type, "Page") == 0 && !xs_is_null(audience)) {
  2621. xs_html *au_tag = xs_html_tag("p",
  2622. xs_html_text("("),
  2623. xs_html_tag("a",
  2624. xs_html_attr("href", audience),
  2625. xs_html_attr("title", L("Source channel or community")),
  2626. xs_html_text(audience)),
  2627. xs_html_text(")"));
  2628. xs_html_add(snac_content_wrap,
  2629. au_tag);
  2630. }
  2631. /* does it have a location? */
  2632. const xs_dict *location = xs_dict_get(msg, "location");
  2633. if (xs_type(location) == XSTYPE_DICT) {
  2634. const xs_number *latitude = xs_dict_get(location, "latitude");
  2635. const xs_number *longitude = xs_dict_get(location, "longitude");
  2636. const char *name = xs_dict_get(location, "name");
  2637. const char *address = xs_dict_get(location, "address");
  2638. xs *label_list = xs_list_new();
  2639. if (xs_type(name) == XSTYPE_STRING)
  2640. label_list = xs_list_append(label_list, name);
  2641. if (xs_type(address) == XSTYPE_STRING)
  2642. label_list = xs_list_append(label_list, address);
  2643. if (xs_list_len(label_list)) {
  2644. const char *url = xs_dict_get(location, "url");
  2645. xs *label = xs_join(label_list, ", ");
  2646. if (xs_type(url) == XSTYPE_STRING) {
  2647. xs_html_add(snac_content_wrap,
  2648. xs_html_tag("p",
  2649. xs_html_text(L("Location: ")),
  2650. xs_html_tag("a",
  2651. xs_html_attr("href", url),
  2652. xs_html_attr("target", "_blank"),
  2653. xs_html_text(label))));
  2654. }
  2655. else
  2656. if (!xs_is_null(latitude) && !xs_is_null(longitude)) {
  2657. xs *url = xs_fmt("https://openstreetmap.org/search/?query=%s,%s",
  2658. xs_number_str(latitude), xs_number_str(longitude));
  2659. xs_html_add(snac_content_wrap,
  2660. xs_html_tag("p",
  2661. xs_html_text(L("Location: ")),
  2662. xs_html_tag("a",
  2663. xs_html_attr("href", url),
  2664. xs_html_attr("target", "_blank"),
  2665. xs_html_text(label))));
  2666. }
  2667. else
  2668. xs_html_add(snac_content_wrap,
  2669. xs_html_tag("p",
  2670. xs_html_text(L("Location: ")),
  2671. xs_html_text(label)));
  2672. }
  2673. }
  2674. if (strcmp(type, "Event") == 0) { /** Event start and end times **/
  2675. const char *s_time = xs_dict_get(msg, "startTime");
  2676. if (xs_is_string(s_time) && strlen(s_time) > 20) {
  2677. const char *e_time = xs_dict_get(msg, "endTime");
  2678. const char *tz = xs_dict_get(msg, "timezone");
  2679. xs *s = xs_replace_i(xs_dup(s_time), "T", " ");
  2680. xs *e = NULL;
  2681. if (xs_is_string(e_time) && strlen(e_time) > 20)
  2682. e = xs_replace_i(xs_dup(e_time), "T", " ");
  2683. /* if the event has a timezone, crop the offsets */
  2684. if (xs_is_string(tz)) {
  2685. s = xs_crop_i(s, 0, 19);
  2686. if (e)
  2687. e = xs_crop_i(e, 0, 19);
  2688. }
  2689. else
  2690. tz = "";
  2691. /* if start and end share the same day, crop it from the end */
  2692. if (e && memcmp(s, e, 11) == 0)
  2693. e = xs_crop_i(e, 11, 0);
  2694. if (e)
  2695. s = xs_str_cat(s, " / ", e);
  2696. if (*tz)
  2697. s = xs_str_cat(s, " (", tz, ")");
  2698. /* replace ugly decimals */
  2699. s = xs_replace_i(s, ".000", "");
  2700. xs_html_add(snac_content_wrap,
  2701. xs_html_tag("p",
  2702. xs_html_text(L("Time: ")),
  2703. xs_html_text(s)));
  2704. }
  2705. }
  2706. /* show all hashtags that has not been shown previously in the content */
  2707. const xs_list *tags = xs_dict_get(msg, "tag");
  2708. const char *o_content = xs_dict_get_def(msg, "content", "");
  2709. if (xs_is_string(o_content) && xs_is_list(tags) && xs_list_len(tags)) {
  2710. xs *content = xs_utf8_to_lower(o_content);
  2711. const xs_dict *tag;
  2712. xs_html *add_hashtags = xs_html_tag("ul",
  2713. xs_html_attr("class", "snac-more-hashtags"));
  2714. xs_list_foreach(tags, tag) {
  2715. const char *type = xs_dict_get(tag, "type");
  2716. if (xs_is_string(type) && strcmp(type, "Hashtag") == 0) {
  2717. const char *o_href = xs_dict_get(tag, "href");
  2718. const char *name = xs_dict_get(tag, "name");
  2719. if (xs_is_string(o_href) && xs_is_string(name)) {
  2720. xs *href = xs_utf8_to_lower(o_href);
  2721. if (xs_str_in(content, href) == -1 && xs_str_in(content, name) == -1) {
  2722. /* not in the content: add here */
  2723. xs_html_add(add_hashtags,
  2724. xs_html_tag("li",
  2725. xs_html_tag("a",
  2726. xs_html_attr("href", href),
  2727. xs_html_text(name),
  2728. xs_html_text(" "))));
  2729. }
  2730. }
  2731. }
  2732. }
  2733. xs_html_add(snac_content_wrap,
  2734. add_hashtags);
  2735. }
  2736. /** controls **/
  2737. if (!read_only && user) {
  2738. xs_html_add(entry,
  2739. html_entry_controls(user, actor, msg, md5));
  2740. }
  2741. /** children **/
  2742. if (!hide_children) {
  2743. xs *children = object_children(id);
  2744. int left = xs_list_len(children);
  2745. if (left) {
  2746. xs_html *ch_details = xs_html_tag("details",
  2747. xs_html_attr(collapse_threads ? "" : "open", NULL),
  2748. xs_html_tag("summary",
  2749. xs_html_text("...")));
  2750. xs_html_add(entry,
  2751. ch_details);
  2752. xs_html *fch_container = xs_html_tag("div",
  2753. xs_html_attr("class", "snac-thread-cont"));
  2754. xs_html_add(ch_details,
  2755. fch_container);
  2756. xs_html *ch_container = xs_html_tag("div",
  2757. xs_html_attr("class", level < 4 ? "snac-children" : "snac-children-too-deep"));
  2758. xs_html_add(ch_details,
  2759. ch_container);
  2760. xs_html *ch_older = NULL;
  2761. if (left > 3) {
  2762. xs_html_add(ch_container,
  2763. ch_older = xs_html_tag("details",
  2764. xs_html_tag("summary",
  2765. xs_html_text(L("Older...")))));
  2766. }
  2767. int ctxt = 0;
  2768. const char *cmd5;
  2769. int cnt = 0;
  2770. int o_cnt = 0;
  2771. int f_cnt = 0;
  2772. /* get the first child */
  2773. xs_list_next(children, &cmd5, &ctxt);
  2774. xs *f_chd = NULL;
  2775. if (user)
  2776. timeline_get_by_md5(user, cmd5, &f_chd);
  2777. else
  2778. object_get_by_md5(cmd5, &f_chd);
  2779. if (f_chd != NULL && xs_is_null(xs_dict_get(f_chd, "name"))) {
  2780. const char *p_author = get_atto(msg);
  2781. const char *author = get_atto(f_chd);
  2782. /* is the first child from the same author? */
  2783. if (xs_is_string(p_author) && xs_is_string(author) && strcmp(p_author, author) == 0) {
  2784. /* then, don't add it to the children container,
  2785. so that it appears unindented just before the parent
  2786. like a fucking Twitter-like thread */
  2787. xs_html_add(fch_container,
  2788. html_entry(user, f_chd, read_only, level + 1, cmd5, hide_children));
  2789. cnt++;
  2790. f_cnt++;
  2791. left--;
  2792. }
  2793. else
  2794. ctxt = 0; /* restart from the beginning */
  2795. }
  2796. while (xs_list_next(children, &cmd5, &ctxt)) {
  2797. xs *chd = NULL;
  2798. if (user)
  2799. timeline_get_by_md5(user, cmd5, &chd);
  2800. else
  2801. object_get_by_md5(cmd5, &chd);
  2802. if (chd != NULL) {
  2803. if (xs_is_null(xs_dict_get(chd, "name"))) {
  2804. xs_html *che = html_entry(user, chd, read_only,
  2805. level + 1, cmd5, hide_children);
  2806. if (che != NULL) {
  2807. if (left > 3) {
  2808. xs_html_add(ch_older,
  2809. che);
  2810. o_cnt++;
  2811. }
  2812. else
  2813. xs_html_add(ch_container,
  2814. che);
  2815. cnt++;
  2816. }
  2817. }
  2818. left--;
  2819. }
  2820. else
  2821. srv_debug(2, xs_fmt("cannot read child %s", cmd5));
  2822. }
  2823. /* if no children were finally added, hide the details */
  2824. if (cnt == 0)
  2825. xs_html_add(ch_details,
  2826. xs_html_attr("style", "display: none"));
  2827. if (o_cnt == 0 && ch_older)
  2828. xs_html_add(ch_older,
  2829. xs_html_attr("style", "display: none"));
  2830. if (f_cnt == 0)
  2831. xs_html_add(fch_container,
  2832. xs_html_attr("style", "display: none"));
  2833. }
  2834. }
  2835. /* add an invisible hr, to help differentiate between posts in text browsers */
  2836. xs_html_add(entry_top,
  2837. xs_html_sctag("hr",
  2838. xs_html_attr("hidden", NULL)));
  2839. return entry_top;
  2840. }
  2841. xs_html *html_footer(const snac *user)
  2842. {
  2843. return xs_html_tag("div",
  2844. xs_html_attr("class", "snac-footer"),
  2845. xs_html_tag("a",
  2846. xs_html_attr("href", srv_baseurl),
  2847. xs_html_text(L("about this site"))),
  2848. xs_html_text(" - "),
  2849. xs_html_text(L("powered by ")),
  2850. xs_html_tag("a",
  2851. xs_html_attr("href", WHAT_IS_SNAC_URL),
  2852. xs_html_tag("abbr",
  2853. xs_html_attr("title", "Social Networks Are Crap"),
  2854. xs_html_text(USER_AGENT))));
  2855. }
  2856. xs_str *html_timeline(snac *user, const xs_list *list, int read_only,
  2857. int skip, int show, int show_more,
  2858. const char *title, const char *page,
  2859. int utl, const char *error, int terse)
  2860. /* returns the HTML for the timeline */
  2861. {
  2862. xs_list *p = (xs_list *)list;
  2863. const char *v;
  2864. double t = ftime();
  2865. int hide_children = xs_is_true(xs_dict_get(srv_config, "strict_public_timelines")) && read_only;
  2866. xs *desc = NULL;
  2867. xs *alternate = NULL;
  2868. if (xs_list_len(list) == 1) {
  2869. /* only one element? pick the description from the source */
  2870. const char *id = xs_list_get(list, 0);
  2871. xs *d = NULL;
  2872. object_get_by_md5(id, &d);
  2873. const char *sc = xs_dict_get(d, "sourceContent");
  2874. if (d && sc != NULL)
  2875. desc = xs_dup(sc);
  2876. alternate = xs_dup(xs_dict_get(d, "id"));
  2877. }
  2878. xs_html *head;
  2879. xs_html *body;
  2880. if (user) {
  2881. head = html_user_head(user, desc, alternate);
  2882. if (terse)
  2883. body = xs_html_tag("body", NULL);
  2884. else
  2885. body = html_user_body(user, read_only);
  2886. }
  2887. else {
  2888. head = html_instance_head();
  2889. body = html_instance_body();
  2890. }
  2891. xs_html *html = xs_html_tag("html",
  2892. head,
  2893. body);
  2894. if (user && !read_only)
  2895. xs_html_add(body,
  2896. html_top_controls(user));
  2897. if (error != NULL) {
  2898. xs_html_add(body,
  2899. xs_html_tag("dialog",
  2900. xs_html_attr("open", NULL),
  2901. xs_html_tag("p",
  2902. xs_html_text(error)),
  2903. xs_html_tag("form",
  2904. xs_html_attr("method", "dialog"),
  2905. xs_html_sctag("input",
  2906. xs_html_attr("type", "submit"),
  2907. xs_html_attr("value", L("Dismiss"))))));
  2908. }
  2909. /* show links to the available lists */
  2910. if (user && !read_only) {
  2911. xs_html *lol = xs_html_tag("ul",
  2912. xs_html_attr("class", "snac-list-of-lists"));
  2913. xs_html_add(body, lol);
  2914. xs *lists = list_maint(user, NULL, 0); /* get list of lists */
  2915. int ct = 0;
  2916. const char *v;
  2917. while (xs_list_next(lists, &v, &ct)) {
  2918. const char *lname = xs_list_get(v, 1);
  2919. xs *url = xs_fmt("%s/list/%s", user->actor, xs_list_get(v, 0));
  2920. xs *ttl = xs_fmt(L("Timeline for list '%s'"), lname);
  2921. xs_html_add(lol,
  2922. xs_html_tag("li",
  2923. xs_html_tag("a",
  2924. xs_html_attr("href", url),
  2925. xs_html_attr("class", "snac-list-link"),
  2926. xs_html_attr("title", ttl),
  2927. xs_html_text(lname))));
  2928. }
  2929. {
  2930. /* show the list of pinned posts */
  2931. xs *url = xs_fmt("%s/pinned", user->actor);
  2932. xs_html_add(lol,
  2933. xs_html_tag("li",
  2934. xs_html_tag("a",
  2935. xs_html_attr("href", url),
  2936. xs_html_attr("class", "snac-list-link"),
  2937. xs_html_attr("title", L("Pinned posts")),
  2938. xs_html_text(L("pinned")))));
  2939. }
  2940. {
  2941. /* show the list of bookmarked posts */
  2942. xs *url = xs_fmt("%s/bookmarks", user->actor);
  2943. xs_html_add(lol,
  2944. xs_html_tag("li",
  2945. xs_html_tag("a",
  2946. xs_html_attr("href", url),
  2947. xs_html_attr("class", "snac-list-link"),
  2948. xs_html_attr("title", L("Bookmarked posts")),
  2949. xs_html_text(L("bookmarks")))));
  2950. }
  2951. {
  2952. /* show the list of drafts */
  2953. xs *url = xs_fmt("%s/drafts", user->actor);
  2954. xs_html_add(lol,
  2955. xs_html_tag("li",
  2956. xs_html_tag("a",
  2957. xs_html_attr("href", url),
  2958. xs_html_attr("class", "snac-list-link"),
  2959. xs_html_attr("title", L("Post drafts")),
  2960. xs_html_text(L("drafts")))));
  2961. }
  2962. {
  2963. /* show the list of scheduled posts */
  2964. xs *url = xs_fmt("%s/sched", user->actor);
  2965. xs_html_add(lol,
  2966. xs_html_tag("li",
  2967. xs_html_tag("a",
  2968. xs_html_attr("href", url),
  2969. xs_html_attr("class", "snac-list-link"),
  2970. xs_html_attr("title", L("Scheduled posts")),
  2971. xs_html_text(L("scheduled posts")))));
  2972. }
  2973. /* the list of followed hashtags */
  2974. const char *followed_hashtags = xs_dict_get(user->config, "followed_hashtags");
  2975. if (xs_is_list(followed_hashtags) && xs_list_len(followed_hashtags)) {
  2976. xs_html *loht = xs_html_tag("ul",
  2977. xs_html_attr("class", "snac-list-of-lists"));
  2978. xs_html_add(body, loht);
  2979. const char *ht;
  2980. xs_list_foreach(followed_hashtags, ht) {
  2981. xs *url = NULL;
  2982. if (!xs_startswith(ht, "https:/""/"))
  2983. url = xs_fmt("%s/admin?q=%s", user->actor, ht);
  2984. else
  2985. url = xs_dup(ht);
  2986. url = xs_replace_i(url, "#", "%23");
  2987. xs_html_add(loht,
  2988. xs_html_tag("li",
  2989. xs_html_tag("a",
  2990. xs_html_attr("href", url),
  2991. xs_html_attr("class", "snac-list-link"),
  2992. xs_html_text(ht))));
  2993. }
  2994. }
  2995. }
  2996. xs_html_add(body,
  2997. xs_html_tag("a",
  2998. xs_html_attr("name", "snac-posts")));
  2999. xs_html *posts = xs_html_tag("div",
  3000. xs_html_attr("class", "snac-posts"));
  3001. if (title) {
  3002. xs_html_add(posts,
  3003. xs_html_tag("h2",
  3004. xs_html_attr("class", "snac-header"),
  3005. xs_html_text(title)));
  3006. }
  3007. xs_html_add(body, posts);
  3008. int mark_shown = 0;
  3009. while (xs_list_iter(&p, &v)) {
  3010. xs *msg = NULL;
  3011. int status;
  3012. /* "already seen" mark? */
  3013. if (strcmp(v, MD5_ALREADY_SEEN_MARK) == 0) {
  3014. if (skip == 0 && !mark_shown) {
  3015. xs *s = xs_fmt("%s/admin", user->actor);
  3016. xs_html_add(posts,
  3017. xs_html_tag("div",
  3018. xs_html_attr("class", "snac-no-more-unseen-posts"),
  3019. xs_html_text(L("No more unseen posts")),
  3020. xs_html_text(" - "),
  3021. xs_html_tag("a",
  3022. xs_html_attr("href", s),
  3023. xs_html_text(L("Back to top")))));
  3024. }
  3025. mark_shown = 1;
  3026. continue;
  3027. }
  3028. if (utl && user && !is_pinned_by_md5(user, v))
  3029. status = timeline_get_by_md5(user, v, &msg);
  3030. else
  3031. status = object_get_by_md5(v, &msg);
  3032. if (!valid_status(status))
  3033. continue;
  3034. /* if it's an instance page, discard messages from private users */
  3035. if (user == NULL && is_msg_from_private_user(msg))
  3036. continue;
  3037. const int scope = get_msg_visibility(msg);
  3038. if (user != NULL && scope != SCOPE_PUBLIC){
  3039. /* is this message a non-public reply? */
  3040. const char *irt = get_in_reply_to(msg);
  3041. /* is it a reply to something not in the storage? */
  3042. if (!xs_is_null(irt) && !object_here(irt)) {
  3043. /* is it for me? */
  3044. const xs_list *to = xs_dict_get_def(msg, "to", xs_stock(XSTYPE_LIST));
  3045. const xs_list *cc = xs_dict_get_def(msg, "cc", xs_stock(XSTYPE_LIST));
  3046. if (xs_list_in(to, user->actor) == -1 && xs_list_in(cc, user->actor) == -1) {
  3047. snac_debug(user, 1, xs_fmt("skipping non-public reply to an unknown post %s", v));
  3048. continue;
  3049. }
  3050. }
  3051. }
  3052. /* hide non-public posts from /instance view */
  3053. if (page != NULL && strcmp(page, "/instance") == 0 && scope != SCOPE_PUBLIC){
  3054. continue;
  3055. }
  3056. /* hide non-public posts viewed from outside */
  3057. if (read_only && (scope != SCOPE_PUBLIC && scope != SCOPE_UNLISTED)) {
  3058. continue;
  3059. }
  3060. xs_html *entry = html_entry(user, msg, read_only, 0, v, (user && !hide_children) ? 0 : 1);
  3061. if (entry != NULL)
  3062. xs_html_add(posts,
  3063. entry);
  3064. }
  3065. if (list && user && read_only) {
  3066. /** history **/
  3067. if (xs_type(xs_dict_get(srv_config, "disable_history")) != XSTYPE_TRUE && !terse) {
  3068. xs_html *ul = xs_html_tag("ul", NULL);
  3069. xs_html *history = xs_html_tag("div",
  3070. xs_html_attr("class", "snac-history"),
  3071. xs_html_tag("p",
  3072. xs_html_attr("class", "snac-history-title"),
  3073. xs_html_text(L("History"))),
  3074. ul);
  3075. xs *list = history_list(user);
  3076. xs_list *p = list;
  3077. const char *v;
  3078. while (xs_list_iter(&p, &v)) {
  3079. xs *fn = xs_replace(v, ".html", "");
  3080. xs *url = xs_fmt("%s/h/%s", user->actor, v);
  3081. xs_html_add(ul,
  3082. xs_html_tag("li",
  3083. xs_html_tag("a",
  3084. xs_html_attr("href", url),
  3085. xs_html_text(fn))));
  3086. }
  3087. xs_html_add(body,
  3088. history);
  3089. }
  3090. }
  3091. {
  3092. xs *s1 = xs_fmt("\n<!-- %lf seconds -->\n", ftime() - t);
  3093. xs_html_add(body,
  3094. xs_html_raw(s1));
  3095. }
  3096. if (show_more) {
  3097. xs *m = NULL;
  3098. xs *ss = xs_fmt("skip=%d&show=%d", skip + show, show);
  3099. xs *url = xs_dup(user == NULL ? srv_baseurl : user->actor);
  3100. if (page != NULL)
  3101. url = xs_str_cat(url, page);
  3102. if (xs_str_in(url, "?") != -1)
  3103. m = xs_fmt("%s&%s", url, ss);
  3104. else
  3105. m = xs_fmt("%s?%s", url, ss);
  3106. xs_html *more_links = xs_html_tag("p",
  3107. xs_html_tag("a",
  3108. xs_html_attr("href", url),
  3109. xs_html_attr("name", "snac-more"),
  3110. xs_html_text(L("Back to top"))),
  3111. xs_html_text(" - "),
  3112. xs_html_tag("a",
  3113. xs_html_attr("href", m),
  3114. xs_html_attr("name", "snac-more"),
  3115. xs_html_text(L("More..."))));
  3116. xs_html_add(body,
  3117. more_links);
  3118. }
  3119. xs_html_add(body,
  3120. html_footer(user));
  3121. return xs_html_render_s(html, "<!DOCTYPE html>\n");
  3122. }
  3123. xs_html *html_people_list(snac *user, xs_list *list, const char *header, const char *t, const char *proxy)
  3124. {
  3125. xs_html *snac_posts;
  3126. xs_html *people = xs_html_tag("div",
  3127. xs_html_tag("h2",
  3128. xs_html_attr("class", "snac-header"),
  3129. xs_html_text(header)),
  3130. snac_posts = xs_html_tag("details",
  3131. xs_html_attr("open", NULL),
  3132. xs_html_tag("summary",
  3133. xs_html_text("..."))));
  3134. xs *redir = xs_fmt("%s/people", user->actor);
  3135. const char *actor_id;
  3136. xs_list_foreach(list, actor_id) {
  3137. xs *md5 = xs_md5_hex(actor_id, strlen(actor_id));
  3138. xs *actor = NULL;
  3139. if (valid_status(actor_get(actor_id, &actor))) {
  3140. xs_html *snac_post = xs_html_tag("div",
  3141. xs_html_attr("class", "snac-post"),
  3142. xs_html_tag("a",
  3143. xs_html_attr("name", md5)),
  3144. xs_html_tag("div",
  3145. xs_html_attr("class", "snac-post-header"),
  3146. html_actor_icon(user, actor, xs_dict_get(actor, "published"),
  3147. NULL, NULL, -1, 1, proxy, NULL, NULL)));
  3148. /* content (user bio) */
  3149. const char *c = xs_dict_get(actor, "summary");
  3150. const xs_val *tag = xs_dict_get(actor, "tag");
  3151. if (!xs_is_null(c)) {
  3152. xs *sc = sanitize(c);
  3153. sc = replace_shortnames(sc, tag, 2, proxy);
  3154. xs_html *snac_content = xs_html_tag("div",
  3155. xs_html_attr("class", "snac-content"));
  3156. if (xs_startswith(sc, "<p>"))
  3157. xs_html_add(snac_content,
  3158. xs_html_raw(sc)); /* already sanitized */
  3159. else
  3160. xs_html_add(snac_content,
  3161. xs_html_tag("p",
  3162. xs_html_raw(sc))); /* already sanitized */
  3163. xs_html_add(snac_post, snac_content);
  3164. }
  3165. /* add user metadata */
  3166. xs_html *snac_metadata = xs_html_tag("div",
  3167. xs_html_attr("class", "snac-metadata"));
  3168. int count = 0;
  3169. const xs_val *address = xs_dict_get(actor, "vcard:Address");
  3170. if (xs_is_string(address)) {
  3171. xs_html_add(snac_metadata,
  3172. xs_html_tag("span",
  3173. xs_html_attr("class", "snac-property-name"),
  3174. xs_html_raw("&#x1F4CD; Location")),
  3175. xs_html_text(":"),
  3176. xs_html_raw("&nbsp;"),
  3177. xs_html_tag("span",
  3178. xs_html_attr("class", "snac-property-value p-adr"),
  3179. xs_html_text(address)),
  3180. xs_html_sctag("br", NULL));
  3181. count++;
  3182. }
  3183. const xs_val *birthday = xs_dict_get(actor, "vcard:bday");
  3184. if (xs_is_string(birthday)) {
  3185. xs_html_add(snac_metadata,
  3186. xs_html_tag("span",
  3187. xs_html_attr("class", "snac-property-name"),
  3188. xs_html_raw("&#x1F382; Birthday")),
  3189. xs_html_text(":"),
  3190. xs_html_raw("&nbsp;"),
  3191. xs_html_tag("time",
  3192. xs_html_attr("class", "snac-property-value dt-bday"),
  3193. xs_html_text(birthday)),
  3194. xs_html_sctag("br", NULL));
  3195. count++;
  3196. }
  3197. const xs_list *attachment = xs_dict_get(actor, "attachment");
  3198. if (count > 0 && xs_list_len(attachment) > 0) {
  3199. xs_html_add(snac_metadata,
  3200. xs_html_sctag("hr",
  3201. xs_html_attr("class", "snac-property-divider")));
  3202. }
  3203. const xs_val *v;
  3204. xs_list_foreach(attachment, v) {
  3205. const char *type = xs_dict_get(v, "type");
  3206. const char *name = xs_dict_get(v, "name");
  3207. const char *value = xs_dict_get(v, "value");
  3208. if (!xs_is_null(type) && !xs_is_null(name) &&
  3209. !xs_is_null(value) && strcmp(type, "PropertyValue") == 0) {
  3210. /* both the name and the value can contain emoji */
  3211. xs *nam = sanitize(name);
  3212. nam = replace_shortnames(nam, tag, 1, proxy);
  3213. /* todo: sometimes the value is transmitted as markdown and not html ._. */
  3214. xs *val = sanitize(value);
  3215. val = replace_shortnames(val, tag, 1, proxy);
  3216. /* delete <p> tags, because some software sends them */
  3217. val = xs_replace_i(val, "<p>", "");
  3218. val = xs_replace_i(val, "</p>", "");
  3219. xs_html_add(snac_metadata,
  3220. xs_html_tag("span",
  3221. xs_html_attr("class", "snac-property-name"),
  3222. xs_html_raw(nam)),
  3223. xs_html_text(":"),
  3224. xs_html_raw("&nbsp;"),
  3225. xs_html_tag("span",
  3226. xs_html_attr("class", "snac-property-value"),
  3227. xs_html_raw(val)),
  3228. xs_html_sctag("br", NULL));
  3229. count++;
  3230. }
  3231. }
  3232. if (count > 0) {
  3233. xs_html_add(snac_post, snac_metadata);
  3234. }
  3235. else {
  3236. /* free the html, by rendering it... */
  3237. xs_free(xs_html_render(snac_metadata));
  3238. }
  3239. /* buttons */
  3240. xs *btn_form_action = xs_fmt("%s/admin/action", user->actor);
  3241. xs_html *snac_controls = xs_html_tag("div",
  3242. xs_html_attr("class", "snac-controls"));
  3243. xs_html *form = xs_html_tag("form",
  3244. xs_html_attr("autocomplete", "off"),
  3245. xs_html_attr("method", "post"),
  3246. xs_html_attr("action", btn_form_action),
  3247. xs_html_sctag("input",
  3248. xs_html_attr("type", "hidden"),
  3249. xs_html_attr("name", "actor"),
  3250. xs_html_attr("value", actor_id)),
  3251. xs_html_sctag("input",
  3252. xs_html_attr("type", "hidden"),
  3253. xs_html_attr("name", "hard-redir"),
  3254. xs_html_attr("value", redir)),
  3255. xs_html_sctag("input",
  3256. xs_html_attr("type", "hidden"),
  3257. xs_html_attr("name", "actor-form"),
  3258. xs_html_attr("value", "yes")));
  3259. xs_html_add(snac_controls, form);
  3260. if (following_check(user, actor_id)) {
  3261. xs_html_add(form,
  3262. html_button("unfollow", L("Unfollow"),
  3263. L("Stop following this user's activity")));
  3264. if (is_limited(user, actor_id))
  3265. xs_html_add(form,
  3266. html_button("unlimit", L("Unlimit"),
  3267. L("Allow announces (boosts) from this user")));
  3268. else
  3269. xs_html_add(form,
  3270. html_button("limit", L("Limit"),
  3271. L("Block announces (boosts) from this user")));
  3272. }
  3273. else {
  3274. xs_html_add(form,
  3275. html_button("follow", L("Follow"),
  3276. L("Start following this user's activity")));
  3277. if (follower_check(user, actor_id))
  3278. xs_html_add(form,
  3279. html_button("delete", L("Delete"), L("Delete this user")));
  3280. }
  3281. if (pending_check(user, actor_id)) {
  3282. xs_html_add(form,
  3283. html_button("approve", L("Approve"),
  3284. L("Approve this follow request")));
  3285. xs_html_add(form,
  3286. html_button("discard", L("Discard"), L("Discard this follow request")));
  3287. }
  3288. if (is_muted(user, actor_id))
  3289. xs_html_add(form,
  3290. html_button("unmute", L("Unmute"),
  3291. L("Stop blocking activities from this user")));
  3292. else
  3293. xs_html_add(form,
  3294. html_button("mute", L("MUTE"),
  3295. L("Block any activity from this user")));
  3296. /* the post textarea */
  3297. xs *dm_div_id = xs_fmt("%s_%s_dm", md5, t);
  3298. xs *dm_form_id = xs_fmt("%s_reply_form", md5);
  3299. xs_html_add(snac_controls,
  3300. xs_html_tag("p", NULL),
  3301. html_note(user, L("Direct Message..."),
  3302. dm_div_id, dm_form_id,
  3303. "", "",
  3304. NULL, actor_id,
  3305. xs_stock(XSTYPE_FALSE), "",
  3306. SCOPE_MENTIONED, NULL,
  3307. NULL, 0, NULL, NULL, 0, NULL, NULL),
  3308. xs_html_tag("p", NULL));
  3309. xs_html_add(snac_post, snac_controls);
  3310. xs_html_add(snac_posts, snac_post);
  3311. }
  3312. }
  3313. return people;
  3314. }
  3315. xs_str *html_people(snac *user)
  3316. {
  3317. const char *proxy = NULL;
  3318. if (xs_is_true(xs_dict_get(srv_config, "proxy_media")))
  3319. proxy = user->actor;
  3320. xs *wing = following_list(user);
  3321. xs *wers = follower_list(user);
  3322. xs *pending = pending_list(user);
  3323. xs_html *lists = xs_html_tag("div",
  3324. xs_html_attr("class", "snac-posts"));
  3325. if (xs_list_len(pending) || xs_is_true(xs_dict_get(user->config, "approve_followers"))) {
  3326. xs_html_add(lists,
  3327. html_people_list(user, pending, L("Pending follow confirmations"), "p", proxy));
  3328. }
  3329. xs_html_add(lists,
  3330. html_people_list(user, wing, L("People you follow"), "i", proxy),
  3331. html_people_list(user, wers, L("People that follow you"), "e", proxy));
  3332. xs_html *html = xs_html_tag("html",
  3333. html_user_head(user, NULL, NULL),
  3334. xs_html_add(html_user_body(user, 0),
  3335. lists,
  3336. html_footer(user)));
  3337. return xs_html_render_s(html, "<!DOCTYPE html>\n");
  3338. }
  3339. /* Filter list to display only posts by actor. We'll probably show
  3340. fewer than show posts. Should we try harder to find some? */
  3341. xs_str *html_people_one(snac *user, const char *actor, const xs_list *list,
  3342. int skip, int show, int show_more, const char *page)
  3343. {
  3344. const char *proxy = NULL;
  3345. xs_list *p = (xs_list *)list;
  3346. const char *v;
  3347. if (xs_is_true(xs_dict_get(srv_config, "proxy_media")))
  3348. proxy = user->actor;
  3349. xs_html *body = html_user_body(user, 0);
  3350. xs_html *lists = xs_html_tag("div",
  3351. xs_html_attr("class", "snac-posts"));
  3352. xs *foll = xs_list_append(xs_list_new(), actor);
  3353. xs_html_add(lists,
  3354. html_people_list(user, foll, L("Contact's posts"), "p", proxy));
  3355. xs_html_add(body, lists);
  3356. while (xs_list_iter(&p, &v)) {
  3357. xs *msg = NULL;
  3358. int status;
  3359. status = timeline_get_by_md5(user, v, &msg);
  3360. if (!valid_status(status))
  3361. continue;
  3362. const char *id = xs_dict_get(msg, "id");
  3363. const char *by = get_atto(msg);
  3364. xs *actor_md5 = NULL;
  3365. xs_list *boosts = NULL;
  3366. xs_list *likes = NULL;
  3367. xs_list *reacts = NULL;
  3368. /* Besides actor's posts, also show actor's boosts, and also
  3369. posts by user with likes or reacts by actor. I.e., any
  3370. actor's actions that user could have seen in the timeline
  3371. or in notifications. */
  3372. if (!(by && strcmp(actor, by) == 0) &&
  3373. xs_list_in((boosts = object_announces(id)),
  3374. (actor_md5 = xs_md5_hex(actor, strlen(actor)))) == -1 &&
  3375. (!(by && strcmp(user->actor, by) == 0) ||
  3376. (xs_list_in((likes = object_likes(id)), actor_md5) == -1 &&
  3377. xs_list_in((reacts = object_get_emoji_reacts(id)), actor_md5) == -1)))
  3378. continue;
  3379. xs_html *entry = html_entry(user, msg, 0, 0, v, 1);
  3380. if (entry != NULL)
  3381. xs_html_add(lists,
  3382. entry);
  3383. }
  3384. if (show_more) {
  3385. xs *m = NULL;
  3386. xs *m10 = NULL;
  3387. xs *ss = xs_fmt("skip=%d&show=%d", skip + show, show);
  3388. xs *url = xs_dup(user == NULL ? srv_baseurl : user->actor);
  3389. if (page != NULL)
  3390. url = xs_str_cat(url, page);
  3391. if (xs_str_in(url, "?") != -1)
  3392. m = xs_fmt("%s&%s", url, ss);
  3393. else
  3394. m = xs_fmt("%s?%s", url, ss);
  3395. m10 = xs_fmt("%s0", m);
  3396. xs_html *more_links = xs_html_tag("p",
  3397. xs_html_tag("a",
  3398. xs_html_attr("href", url),
  3399. xs_html_attr("name", "snac-more"),
  3400. xs_html_text(L("Back to top"))),
  3401. xs_html_text(" - "),
  3402. xs_html_tag("a",
  3403. xs_html_attr("href", m),
  3404. xs_html_attr("name", "snac-more"),
  3405. xs_html_text(L("More..."))),
  3406. xs_html_text(" - "),
  3407. xs_html_tag("a",
  3408. xs_html_attr("href", m10),
  3409. xs_html_attr("name", "snac-more"),
  3410. xs_html_text(L("More (x 10)..."))));
  3411. xs_html_add(body,
  3412. more_links);
  3413. }
  3414. xs_html *html = xs_html_tag("html",
  3415. html_user_head(user, NULL, NULL),
  3416. xs_html_add(body,
  3417. html_footer(user)));
  3418. return xs_html_render_s(html, "<!DOCTYPE html>\n");
  3419. }
  3420. xs_str *html_notifications(snac *user, int skip, int show)
  3421. {
  3422. const char *proxy = NULL;
  3423. if (xs_is_true(xs_dict_get(srv_config, "proxy_media")))
  3424. proxy = user->actor;
  3425. xs *n_list = notify_list(user, skip, show);
  3426. xs *n_time = notify_check_time(user, 0);
  3427. xs_html *body = html_user_body(user, 0);
  3428. xs_html *html = xs_html_tag("html",
  3429. html_user_head(user, NULL, NULL),
  3430. body);
  3431. xs *clear_all_action = xs_fmt("%s/admin/clear-notifications", user->actor);
  3432. xs_html_add(body,
  3433. xs_html_tag("form",
  3434. xs_html_attr("autocomplete", "off"),
  3435. xs_html_attr("method", "post"),
  3436. xs_html_attr("action", clear_all_action),
  3437. xs_html_attr("id", "clear"),
  3438. xs_html_sctag("input",
  3439. xs_html_attr("type", "submit"),
  3440. xs_html_attr("class", "snac-btn-like"),
  3441. xs_html_attr("value", L("Clear all")))));
  3442. xs_html *noti_new = NULL;
  3443. xs_html *noti_seen = NULL;
  3444. xs_html *posts = xs_html_tag("div",
  3445. xs_html_attr("class", "snac-posts"));
  3446. xs_html_add(body, posts);
  3447. xs_set rep;
  3448. xs_set_init(&rep);
  3449. /* dict to store previous notification labels */
  3450. xs *admiration_labels = xs_dict_new();
  3451. const xs_str *v;
  3452. xs_list_foreach(n_list, v) {
  3453. xs *noti = notify_get(user, v);
  3454. if (noti == NULL)
  3455. continue;
  3456. xs *obj = NULL;
  3457. const char *type = xs_dict_get(noti, "type");
  3458. const char *utype = xs_dict_get(noti, "utype");
  3459. const char *id = xs_dict_get(noti, "objid");
  3460. const char *date = xs_dict_get(noti, "date");
  3461. const char *id2 = xs_dict_get_path(noti, "msg.id");
  3462. xs *wrk = NULL;
  3463. if (xs_is_null(id))
  3464. continue;
  3465. if (is_hidden(user, id))
  3466. continue;
  3467. if (xs_is_string(id2) && xs_set_add(&rep, id2) != 1)
  3468. continue;
  3469. if (strcmp(type, "EmojiReact") == 0 && xs_is_true(xs_dict_get(srv_config, "disable_emojireact")))
  3470. continue;
  3471. object_get(id, &obj);
  3472. const char *msg_id = NULL;
  3473. if (xs_is_dict(obj))
  3474. msg_id = xs_dict_get(obj, "id");
  3475. const char *actor_id = xs_dict_get(noti, "actor");
  3476. xs *actor = NULL;
  3477. xs *a_name = NULL;
  3478. if (valid_status(actor_get(actor_id, &actor)))
  3479. a_name = actor_name(actor, proxy);
  3480. else {
  3481. a_name = xs_dup(actor_id);
  3482. /* actor not here: request it */
  3483. enqueue_actor_refresh(user, actor_id, 0);
  3484. }
  3485. xs *label_sanitized = sanitize(type);
  3486. const char *label = label_sanitized;
  3487. if (strcmp(type, "Create") == 0)
  3488. label = L("Mention");
  3489. else
  3490. if (strcmp(type, "Update") == 0 && strcmp(utype, "Question") == 0)
  3491. label = L("Finished poll");
  3492. else
  3493. if (strcmp(type, "Undo") == 0 && strcmp(utype, "Follow") == 0)
  3494. label = L("Unfollow");
  3495. else
  3496. if (strcmp(type, "EmojiReact") == 0 || strcmp(type, "Like") == 0) {
  3497. const char *content = xs_dict_get_path(noti, "msg.content");
  3498. xs *cd = xs_dup(content);
  3499. const char *sna = cd;
  3500. const xs_dict *tag = xs_dict_get_path(noti, "msg.tag");
  3501. unsigned int utf = xs_utf8_dec((const char **)&sna);
  3502. int isEmoji = 0;
  3503. if (xs_is_emoji(utf) || (tag && xs_list_len(tag) > 0))
  3504. isEmoji = 1;
  3505. if (xs_type(content) == XSTYPE_STRING) {
  3506. xs *emoji = replace_shortnames(xs_dup(content), xs_dict_get_path(noti, "msg.tag"), 1, proxy);
  3507. wrk = xs_fmt("%s (%s&#xFE0F;)", isEmoji ? "EmojiReact" : "Like", emoji);
  3508. label = wrk;
  3509. }
  3510. }
  3511. else
  3512. if (strcmp(type, "Follow") == 0 && pending_check(user, actor_id))
  3513. label = L("Follow Request");
  3514. xs *s_date = xs_crop_i(xs_dup(date), 0, 10);
  3515. xs_html *this_html_label = xs_html_container(
  3516. xs_html_tag("b",
  3517. xs_html_raw(label),
  3518. xs_html_text(" by "),
  3519. xs_html_tag("a",
  3520. xs_html_attr("href", actor_id),
  3521. xs_html_raw(a_name))), /* a_name is already sanitized */
  3522. xs_html_text(" "),
  3523. xs_html_tag("time",
  3524. xs_html_attr("class", "dt-published snac-pubdate"),
  3525. xs_html_attr("title", date),
  3526. xs_html_text(s_date)));
  3527. xs_html *html_label = NULL;
  3528. if (xs_is_string(msg_id)) {
  3529. const xs_val *prev_label = xs_dict_get(admiration_labels, msg_id);
  3530. if (xs_type(prev_label) == XSTYPE_DATA) {
  3531. /* there is a previous list of admiration labels! */
  3532. xs_data_get(&html_label, prev_label);
  3533. xs_html_add(html_label,
  3534. xs_html_sctag("br", NULL),
  3535. this_html_label);
  3536. continue;
  3537. }
  3538. }
  3539. xs_html *entry = NULL;
  3540. html_label = xs_html_tag("p",
  3541. this_html_label);
  3542. /* store in the admiration labels dict */
  3543. xs *pl = xs_data_new(&html_label, sizeof(html_label));
  3544. if (xs_is_string(msg_id))
  3545. admiration_labels = xs_dict_set(admiration_labels, msg_id, pl);
  3546. entry = xs_html_tag("div",
  3547. xs_html_attr("class", "snac-post-with-desc"),
  3548. html_label);
  3549. if (strcmp(type, "Block") == 0) {
  3550. if (actor)
  3551. xs_html_add(entry,
  3552. xs_html_tag("div",
  3553. xs_html_attr("class", "snac-post"),
  3554. html_actor_icon(user, actor, NULL, NULL, NULL, -1, 0, proxy, NULL, NULL)));
  3555. }
  3556. else
  3557. if (strcmp(type, "Follow") == 0 || strcmp(utype, "Follow") == 0) {
  3558. if (actor) {
  3559. xs *action = xs_fmt("%s/admin/action", user->actor);
  3560. xs_html *button = NULL;
  3561. if (following_check(user, actor_id))
  3562. button = html_button("unfollow", L("Unfollow"), L("Stop following this user's activity"));
  3563. else
  3564. button = html_button("follow", L("Follow"), L("Start following this user's activity"));
  3565. xs_html_add(entry,
  3566. xs_html_tag("div",
  3567. xs_html_attr("class", "snac-post"),
  3568. html_actor_icon(user, actor, NULL, NULL, NULL, -1, 0, proxy, NULL, NULL),
  3569. xs_html_tag("form",
  3570. xs_html_attr("method", "post"),
  3571. xs_html_attr("action", action),
  3572. xs_html_sctag("input",
  3573. xs_html_attr("type", "hidden"),
  3574. xs_html_attr("name", "actor"),
  3575. xs_html_attr("value", actor_id)),
  3576. button,
  3577. xs_html_sctag("br", NULL))));
  3578. }
  3579. }
  3580. else
  3581. if (strcmp(type, "Move") == 0) {
  3582. const xs_dict *o_msg = xs_dict_get(noti, "msg");
  3583. const char *target;
  3584. if (xs_type(o_msg) == XSTYPE_DICT && (target = xs_dict_get(o_msg, "target"))) {
  3585. xs *old_actor = NULL;
  3586. if (valid_status(actor_get(target, &old_actor))) {
  3587. xs_html_add(entry,
  3588. xs_html_tag("div",
  3589. xs_html_attr("class", "snac-post"),
  3590. html_actor_icon(user, old_actor, NULL, NULL, NULL, -1, 0, proxy, NULL, NULL)));
  3591. }
  3592. }
  3593. }
  3594. else
  3595. if (obj != NULL) {
  3596. xs *md5 = xs_md5_hex(id, strlen(id));
  3597. xs *ctxt = xs_fmt("%s/admin/p/%s#%s_entry", user->actor, md5, md5);
  3598. xs_html *h = html_entry(user, obj, 0, 0, md5, 1);
  3599. if (h != NULL) {
  3600. xs_html_add(entry,
  3601. xs_html_tag("p",
  3602. xs_html_tag("a",
  3603. xs_html_attr("href", ctxt),
  3604. xs_html_text(L("Context")))),
  3605. h);
  3606. }
  3607. else
  3608. xs_html_add(entry,
  3609. xs_html_tag("p",
  3610. xs_html_text(L("Location: ")),
  3611. xs_html_tag("a",
  3612. xs_html_attr("href", id),
  3613. xs_html_text(id))));
  3614. }
  3615. if (strcmp(v, n_time) > 0) {
  3616. /* unseen notification */
  3617. if (noti_new == NULL) {
  3618. noti_new = xs_html_tag("div",
  3619. xs_html_tag("h2",
  3620. xs_html_attr("class", "snac-header"),
  3621. xs_html_text(L("New"))));
  3622. xs_html_add(posts,
  3623. noti_new);
  3624. }
  3625. xs_html_add(noti_new,
  3626. entry);
  3627. }
  3628. else {
  3629. /* already seen notification */
  3630. if (noti_seen == NULL) {
  3631. noti_seen = xs_html_tag("div",
  3632. xs_html_tag("h2",
  3633. xs_html_attr("class", "snac-header"),
  3634. xs_html_text(L("Already seen"))));
  3635. xs_html_add(posts,
  3636. noti_seen);
  3637. }
  3638. xs_html_add(noti_seen,
  3639. entry);
  3640. }
  3641. }
  3642. if (noti_new == NULL && noti_seen == NULL)
  3643. xs_html_add(body,
  3644. xs_html_tag("h2",
  3645. xs_html_attr("class", "snac-header"),
  3646. xs_html_text(L("None"))));
  3647. /* add the navigation footer */
  3648. xs *next_p = notify_list(user, skip + show, 1);
  3649. if (xs_list_len(next_p)) {
  3650. xs *url = xs_fmt("%s/notifications?skip=%d&show=%d",
  3651. user->actor, skip + show, show);
  3652. xs_html_add(body,
  3653. xs_html_tag("p",
  3654. xs_html_tag("a",
  3655. xs_html_attr("href", url),
  3656. xs_html_text(L("More...")))));
  3657. }
  3658. xs_set_free(&rep);
  3659. xs_html_add(body,
  3660. html_footer(user));
  3661. /* set the check time to now */
  3662. xs *dummy = notify_check_time(user, 1);
  3663. dummy = xs_free(dummy);
  3664. timeline_touch(user);
  3665. return xs_html_render_s(html, "<!DOCTYPE html>\n");
  3666. }
  3667. void set_user_lang(snac *user)
  3668. /* sets the language dict according to user configuration */
  3669. {
  3670. user->lang = NULL;
  3671. const char *lang = xs_dict_get(user->config, "lang");
  3672. if (xs_is_string(lang))
  3673. user->lang = xs_dict_get(srv_langs, lang);
  3674. }
  3675. int html_get_handler(const xs_dict *req, const char *q_path,
  3676. char **body, int *b_size, char **ctype,
  3677. xs_str **etag, xs_str **last_modified)
  3678. {
  3679. const char *accept = xs_dict_get(req, "accept");
  3680. int status = HTTP_STATUS_NOT_FOUND;
  3681. const snac *user = NULL;
  3682. snac snac;
  3683. xs *uid = NULL;
  3684. const char *p_path;
  3685. int cache = 1;
  3686. int save = 1;
  3687. int proxy = 0;
  3688. int terse = 0;
  3689. const char *v;
  3690. const xs_dict *q_vars = xs_dict_get(req, "q_vars");
  3691. xs *l = xs_split_n(q_path, "/", 2);
  3692. v = xs_list_get(l, 1);
  3693. if (xs_is_null(v)) {
  3694. srv_log(xs_fmt("html_get_handler bad query '%s'", q_path));
  3695. return HTTP_STATUS_NOT_FOUND;
  3696. }
  3697. if (!xs_is_null(xs_dict_get(q_vars, "terse")))
  3698. terse = 1;
  3699. if (strcmp(v, "share-bridge") == 0) {
  3700. /* temporary redirect for a post */
  3701. const char *login = xs_dict_get(q_vars, "login");
  3702. const char *content = xs_dict_get(q_vars, "content");
  3703. if (xs_type(login) == XSTYPE_STRING && xs_type(content) == XSTYPE_STRING) {
  3704. xs *b64 = xs_base64_enc(content, strlen(content));
  3705. srv_log(xs_fmt("share-bridge for user '%s'", login));
  3706. *body = xs_fmt("%s/%s/share?content=%s", srv_baseurl, login, b64);
  3707. return HTTP_STATUS_SEE_OTHER;
  3708. }
  3709. else
  3710. return HTTP_STATUS_NOT_FOUND;
  3711. }
  3712. else
  3713. if (strcmp(v, "auth-int-bridge") == 0) {
  3714. const char *login = xs_dict_get(q_vars, "login");
  3715. const char *id = xs_dict_get(q_vars, "id");
  3716. const char *action = xs_dict_get(q_vars, "action");
  3717. if (xs_is_string(login) && xs_is_string(id) && xs_is_string(action)) {
  3718. *body = xs_fmt("%s/%s/authorize_interaction?action=%s&id=%s",
  3719. srv_baseurl, login, action, id);
  3720. return HTTP_STATUS_SEE_OTHER;
  3721. }
  3722. else
  3723. return HTTP_STATUS_NOT_FOUND;
  3724. }
  3725. uid = xs_dup(v);
  3726. /* rss extension? */
  3727. if (xs_endswith(uid, ".rss")) {
  3728. uid = xs_crop_i(uid, 0, -4);
  3729. p_path = ".rss";
  3730. }
  3731. else
  3732. p_path = xs_list_get(l, 2);
  3733. if (!uid || !user_open(&snac, uid)) {
  3734. /* invalid user */
  3735. status = grave(uid, 0) ? HTTP_STATUS_GONE : HTTP_STATUS_NOT_FOUND;
  3736. srv_debug(1, xs_fmt("html_get_handler bad user %s %d", uid, status));
  3737. return status;
  3738. }
  3739. user = &snac; /* for L() */
  3740. set_user_lang(&snac);
  3741. if (xs_is_true(xs_dict_get(srv_config, "proxy_media")))
  3742. proxy = 1;
  3743. /* return the RSS if requested by Accept header */
  3744. if (accept != NULL) {
  3745. if (xs_str_in(accept, "text/xml") != -1 ||
  3746. xs_str_in(accept, "application/rss+xml") != -1)
  3747. p_path = ".rss";
  3748. }
  3749. /* check if server config variable 'disable_cache' is set */
  3750. if ((v = xs_dict_get(srv_config, "disable_cache")) && xs_type(v) == XSTYPE_TRUE)
  3751. cache = 0;
  3752. int skip = 0;
  3753. const char *max_show_default = "50";
  3754. int max_show = xs_number_get(xs_dict_get_def(srv_config, "max_timeline_entries",
  3755. max_show_default));
  3756. int def_show = xs_number_get(xs_dict_get_def(srv_config, "def_timeline_entries",
  3757. xs_dict_get_def(srv_config, "max_timeline_entries",
  3758. max_show_default)));
  3759. int show = def_show;
  3760. if ((v = xs_dict_get(q_vars, "skip")) != NULL)
  3761. skip = atoi(v), cache = 0, save = 0;
  3762. if ((v = xs_dict_get(q_vars, "show")) != NULL)
  3763. show = atoi(v), cache = 0, save = 0;
  3764. if ((v = xs_dict_get(q_vars, "da")) != NULL) {
  3765. /* user dismissed an announcement */
  3766. if (login(&snac, req)) {
  3767. double ts = atof(v);
  3768. xs *timestamp = xs_number_new(ts);
  3769. srv_log(xs_fmt("user dismissed announcements until %d", ts));
  3770. snac.config = xs_dict_set(snac.config, "last_announcement", timestamp);
  3771. user_persist(&snac, 0);
  3772. }
  3773. }
  3774. /* get a possible error message */
  3775. const char *error = xs_dict_get(q_vars, "error");
  3776. if (error != NULL)
  3777. cache = 0;
  3778. /* a show of 0 has no sense */
  3779. if (show == 0)
  3780. show = def_show;
  3781. if (show > max_show)
  3782. show = max_show;
  3783. if (p_path == NULL) { /** public timeline **/
  3784. xs *h = xs_str_localtime(0, "%Y-%m.html");
  3785. if (xs_type(xs_dict_get(snac.config, "private")) == XSTYPE_TRUE) {
  3786. /** empty public timeline for private users **/
  3787. *body = html_timeline(&snac, NULL, 1, 0, 0, 0, NULL, "", 1, error, terse);
  3788. *b_size = strlen(*body);
  3789. status = HTTP_STATUS_OK;
  3790. }
  3791. else
  3792. if (cache && history_mtime(&snac, h) > timeline_mtime(&snac)) {
  3793. snac_debug(&snac, 1, xs_fmt("serving cached local timeline"));
  3794. status = history_get(&snac, h, body, b_size,
  3795. xs_dict_get(req, "if-none-match"), etag);
  3796. }
  3797. else {
  3798. xs *list = NULL;
  3799. int more = 0;
  3800. if (xs_is_true(xs_dict_get(srv_config, "strict_public_timelines")))
  3801. list = timeline_simple_list(&snac, "public", skip, show, &more);
  3802. else
  3803. list = timeline_list(&snac, "public", skip, show, &more);
  3804. xs *pins = pinned_list(&snac);
  3805. pins = xs_list_cat(pins, list);
  3806. *body = html_timeline(&snac, pins, 1, skip, show, more, NULL, "", 1, error, terse);
  3807. *b_size = strlen(*body);
  3808. status = HTTP_STATUS_OK;
  3809. if (save)
  3810. history_add(&snac, h, *body, *b_size, etag);
  3811. }
  3812. }
  3813. else
  3814. if (strcmp(p_path, "admin") == 0) { /** private timeline **/
  3815. if (!login(&snac, req)) {
  3816. *body = xs_dup(uid);
  3817. status = HTTP_STATUS_UNAUTHORIZED;
  3818. }
  3819. else {
  3820. const char *q = NULL;
  3821. xs *cq = xs_dup(xs_dict_get(q_vars, "q"));
  3822. xs *url_acct = NULL;
  3823. if (xs_is_string(cq)) {
  3824. cq = xs_strip_i(cq);
  3825. q = cq;
  3826. }
  3827. /* searching for an URL? */
  3828. if (q && xs_match(q, "https://*|http://*")) {
  3829. /* may by an actor; try a webfinger */
  3830. xs *actor_obj = NULL;
  3831. if (valid_status(webfinger_request(q, &actor_obj, &url_acct)) && xs_is_string(url_acct)) {
  3832. /* it's an actor; do the dirty trick of changing q to the account name */
  3833. q = url_acct;
  3834. }
  3835. else {
  3836. /* bring it to the user's timeline */
  3837. xs *object = NULL;
  3838. int status;
  3839. status = activitypub_request(&snac, q, &object);
  3840. snac_debug(&snac, 1, xs_fmt("Request searched URL %s %d", q, status));
  3841. if (valid_status(status)) {
  3842. /* got it; also request the actor */
  3843. const char *attr_to = get_atto(object);
  3844. if (!xs_is_null(attr_to)) {
  3845. status = actor_request(&snac, attr_to, &actor_obj);
  3846. if (valid_status(status)) {
  3847. /* reset the query string to be the real id */
  3848. url_acct = xs_dup(xs_dict_get(object, "id"));
  3849. q = url_acct;
  3850. /* add the post to the timeline */
  3851. if (!timeline_here(&snac, q))
  3852. timeline_add(&snac, q, object);
  3853. }
  3854. }
  3855. else {
  3856. /* retry webfinger, this time with the 'official' id */
  3857. const char *id = xs_dict_get(object, "id");
  3858. if (xs_is_string(id) && valid_status(webfinger_request(id, &actor_obj, &url_acct)) &&
  3859. xs_is_string(url_acct))
  3860. q = url_acct;
  3861. }
  3862. }
  3863. }
  3864. /* fall through */
  3865. }
  3866. if (q && *q) {
  3867. if (xs_regex_match(q, "^@?[a-zA-Z0-9._]+@[a-zA-Z0-9-]+\\.")) {
  3868. /** search account **/
  3869. xs *actor = NULL;
  3870. xs *acct = NULL;
  3871. xs *l = xs_list_new();
  3872. xs_html *page = NULL;
  3873. if (valid_status(webfinger_request(q, &actor, &acct))) {
  3874. xs *actor_obj = NULL;
  3875. if (valid_status(actor_request(&snac, actor, &actor_obj))) {
  3876. actor_add(actor, actor_obj);
  3877. /* create a people list with only one element */
  3878. l = xs_list_append(l, actor);
  3879. xs *title = xs_fmt(L("Search results for account %s"), q);
  3880. page = html_people_list(&snac, l, title, "wf", NULL);
  3881. }
  3882. }
  3883. if (page == NULL) {
  3884. xs *title = xs_fmt(L("Account %s not found"), q);
  3885. page = xs_html_tag("div",
  3886. xs_html_tag("h2",
  3887. xs_html_attr("class", "snac-header"),
  3888. xs_html_text(title)));
  3889. }
  3890. xs_html *html = xs_html_tag("html",
  3891. html_user_head(&snac, NULL, NULL),
  3892. xs_html_add(html_user_body(&snac, 0),
  3893. page,
  3894. html_footer(user)));
  3895. *body = xs_html_render_s(html, "<!DOCTYPE html>\n");
  3896. *b_size = strlen(*body);
  3897. status = HTTP_STATUS_OK;
  3898. }
  3899. else
  3900. if (*q == '#') {
  3901. /** search by tag **/
  3902. xs *tl = tag_search(q, skip, show + 1);
  3903. int more = 0;
  3904. if (xs_list_len(tl) >= show + 1) {
  3905. /* drop the last one */
  3906. tl = xs_list_del(tl, -1);
  3907. more = 1;
  3908. }
  3909. xs *page = xs_fmt("/admin?q=%%23%s", q + 1);
  3910. xs *title = xs_fmt(xs_list_len(tl) ?
  3911. L("Search results for tag %s") : L("Nothing found for tag %s"), q);
  3912. *body = html_timeline(&snac, tl, 0, skip, show, more, title, page, 0, error, terse);
  3913. *b_size = strlen(*body);
  3914. status = HTTP_STATUS_OK;
  3915. }
  3916. else {
  3917. /** search by content **/
  3918. int to = 0;
  3919. int msecs = atoi(xs_dict_get_def(q_vars, "msecs", "0"));
  3920. xs *tl = content_search(&snac, q, 1, skip, show, msecs, &to);
  3921. xs *title = NULL;
  3922. xs *page = xs_fmt("/admin?q=%s&msecs=%d", q, msecs + 10);
  3923. int tl_len = xs_list_len(tl);
  3924. if (to)
  3925. title = xs_fmt(L("Search results for '%s' (may be more)"), q);
  3926. else
  3927. if (tl_len)
  3928. title = xs_fmt(L("Search results for '%s'"), q);
  3929. else
  3930. if (skip)
  3931. title = xs_fmt(L("No more matches for '%s'"), q);
  3932. else
  3933. title = xs_fmt(L("Nothing found for '%s'"), q);
  3934. *body = html_timeline(&snac, tl, 0, skip, tl_len, to || tl_len == show,
  3935. title, page, 0, error, terse);
  3936. *b_size = strlen(*body);
  3937. status = HTTP_STATUS_OK;
  3938. }
  3939. }
  3940. else {
  3941. /** the private timeline **/
  3942. double t = history_mtime(&snac, "timeline.html_");
  3943. /* if enabled by admin, return a cached page if its timestamp is:
  3944. a) newer than the timeline timestamp
  3945. b) newer than the start time of the server
  3946. */
  3947. if (cache && t > timeline_mtime(&snac) && t > p_state->srv_start_time) {
  3948. snac_debug(&snac, 1, xs_fmt("serving cached timeline"));
  3949. status = history_get(&snac, "timeline.html_", body, b_size,
  3950. xs_dict_get(req, "if-none-match"), etag);
  3951. }
  3952. else {
  3953. int more = 0;
  3954. snac_debug(&snac, 1, xs_fmt("building timeline"));
  3955. xs *list = timeline_list(&snac, "private", skip, show, &more);
  3956. *body = html_timeline(&snac, list, 0, skip, show,
  3957. more, NULL, "/admin", 1, error, terse);
  3958. *b_size = strlen(*body);
  3959. status = HTTP_STATUS_OK;
  3960. if (save)
  3961. history_add(&snac, "timeline.html_", *body, *b_size, etag);
  3962. timeline_add_mark(&snac);
  3963. }
  3964. }
  3965. }
  3966. }
  3967. else
  3968. if (xs_startswith(p_path, "admin/p/")) { /** unique post by md5 **/
  3969. if (!login(&snac, req)) {
  3970. *body = xs_dup(uid);
  3971. status = HTTP_STATUS_UNAUTHORIZED;
  3972. }
  3973. else {
  3974. xs *l = xs_split(p_path, "/");
  3975. const char *md5 = xs_list_get(l, -1);
  3976. if (md5 && *md5 && timeline_here_by_md5(&snac, md5)) {
  3977. xs *list0 = xs_list_append(xs_list_new(), md5);
  3978. xs *list = timeline_top_level(&snac, list0);
  3979. *body = html_timeline(&snac, list, 0, 0, 0, 0, NULL, "/admin", 1, error, terse);
  3980. *b_size = strlen(*body);
  3981. status = HTTP_STATUS_OK;
  3982. }
  3983. }
  3984. }
  3985. else
  3986. if (strcmp(p_path, "people") == 0) { /** the list of people **/
  3987. if (!login(&snac, req)) {
  3988. *body = xs_dup(uid);
  3989. status = HTTP_STATUS_UNAUTHORIZED;
  3990. }
  3991. else {
  3992. *body = html_people(&snac);
  3993. *b_size = strlen(*body);
  3994. status = HTTP_STATUS_OK;
  3995. }
  3996. }
  3997. else
  3998. if (xs_startswith(p_path, "people/")) { /** a single actor **/
  3999. if (!login(&snac, req)) {
  4000. *body = xs_dup(uid);
  4001. status = HTTP_STATUS_UNAUTHORIZED;
  4002. }
  4003. else {
  4004. xs *actor_dict = NULL;
  4005. const char *actor_id = NULL;
  4006. xs *actor = NULL;
  4007. xs_list *page_lst = xs_split_n(p_path, "?", 2);
  4008. xs *page = xs_str_cat(xs_str_new("/"), xs_list_get(page_lst, 0));
  4009. xs_list *l = xs_split_n(page, "/", 3);
  4010. const char *actor_md5 = xs_list_get(l, 2);
  4011. if (valid_status(object_get_by_md5(actor_md5, &actor_dict)) &&
  4012. (actor_id = xs_dict_get(actor_dict, "id")) != NULL &&
  4013. valid_status(actor_get(actor_id, &actor))) {
  4014. int more = 0;
  4015. xs *list = timeline_simple_list(&snac, "private", skip, show, &more);
  4016. *body = html_people_one(&snac, actor_id, list, skip, show, more, page);
  4017. *b_size = strlen(*body);
  4018. status = HTTP_STATUS_OK;
  4019. }
  4020. else {
  4021. *body = xs_dup(uid);
  4022. status = HTTP_STATUS_NOT_FOUND;
  4023. }
  4024. }
  4025. }
  4026. else
  4027. if (strcmp(p_path, "notifications") == 0) { /** the list of notifications **/
  4028. if (!login(&snac, req)) {
  4029. *body = xs_dup(uid);
  4030. status = HTTP_STATUS_UNAUTHORIZED;
  4031. }
  4032. else {
  4033. *body = html_notifications(&snac, skip, show);
  4034. *b_size = strlen(*body);
  4035. status = HTTP_STATUS_OK;
  4036. }
  4037. }
  4038. else
  4039. if (strcmp(p_path, "instance") == 0) { /** instance timeline **/
  4040. if (!login(&snac, req)) {
  4041. *body = xs_dup(uid);
  4042. status = HTTP_STATUS_UNAUTHORIZED;
  4043. }
  4044. else {
  4045. xs *list = timeline_instance_list(skip, show);
  4046. xs *next = timeline_instance_list(skip + show, 1);
  4047. *body = html_timeline(&snac, list, 0, skip, show,
  4048. xs_list_len(next), L("Showing instance timeline"), "/instance", 0, error, terse);
  4049. *b_size = strlen(*body);
  4050. status = HTTP_STATUS_OK;
  4051. }
  4052. }
  4053. else
  4054. if (strcmp(p_path, "pinned") == 0) { /** list of pinned posts **/
  4055. if (!login(&snac, req)) {
  4056. *body = xs_dup(uid);
  4057. status = HTTP_STATUS_UNAUTHORIZED;
  4058. }
  4059. else {
  4060. xs *list = pinned_list(&snac);
  4061. *body = html_timeline(&snac, list, 0, skip, show,
  4062. 0, L("Pinned posts"), "", 0, error, terse);
  4063. *b_size = strlen(*body);
  4064. status = HTTP_STATUS_OK;
  4065. }
  4066. }
  4067. else
  4068. if (strcmp(p_path, "bookmarks") == 0) { /** list of bookmarked posts **/
  4069. if (!login(&snac, req)) {
  4070. *body = xs_dup(uid);
  4071. status = HTTP_STATUS_UNAUTHORIZED;
  4072. }
  4073. else {
  4074. xs *list = bookmark_list(&snac);
  4075. *body = html_timeline(&snac, list, 0, skip, show,
  4076. 0, L("Bookmarked posts"), "", 0, error, terse);
  4077. *b_size = strlen(*body);
  4078. status = HTTP_STATUS_OK;
  4079. }
  4080. }
  4081. else
  4082. if (strcmp(p_path, "drafts") == 0) { /** list of drafts **/
  4083. if (!login(&snac, req)) {
  4084. *body = xs_dup(uid);
  4085. status = HTTP_STATUS_UNAUTHORIZED;
  4086. }
  4087. else {
  4088. xs *list = draft_list(&snac);
  4089. *body = html_timeline(&snac, list, 0, skip, show,
  4090. 0, L("Post drafts"), "", 0, error, terse);
  4091. *b_size = strlen(*body);
  4092. status = HTTP_STATUS_OK;
  4093. }
  4094. }
  4095. else
  4096. if (strcmp(p_path, "sched") == 0) { /** list of scheduled posts **/
  4097. if (!login(&snac, req)) {
  4098. *body = xs_dup(uid);
  4099. status = HTTP_STATUS_UNAUTHORIZED;
  4100. }
  4101. else {
  4102. xs *list = scheduled_list(&snac);
  4103. *body = html_timeline(&snac, list, 0, skip, show,
  4104. 0, L("Scheduled posts"), "", 0, error, terse);
  4105. *b_size = strlen(*body);
  4106. status = HTTP_STATUS_OK;
  4107. }
  4108. }
  4109. else
  4110. if (xs_startswith(p_path, "list/")) { /** list timelines **/
  4111. if (!login(&snac, req)) {
  4112. *body = xs_dup(uid);
  4113. status = HTTP_STATUS_UNAUTHORIZED;
  4114. }
  4115. else {
  4116. xs *l = xs_split(p_path, "/");
  4117. const char *lid = xs_list_get(l, -1);
  4118. xs *list = list_timeline(&snac, lid, skip, show);
  4119. xs *next = list_timeline(&snac, lid, skip + show, 1);
  4120. if (list != NULL) {
  4121. xs *ttl = timeline_top_level(&snac, list);
  4122. xs *base = xs_fmt("/list/%s", lid);
  4123. xs *name = list_maint(&snac, lid, 3);
  4124. xs *title = xs_fmt(L("Showing timeline for list '%s'"), name);
  4125. *body = html_timeline(&snac, ttl, 0, skip, show,
  4126. xs_list_len(next), title, base, 1, error, terse);
  4127. *b_size = strlen(*body);
  4128. status = HTTP_STATUS_OK;
  4129. }
  4130. }
  4131. }
  4132. else
  4133. if (xs_startswith(p_path, "p/")) { /** a timeline with just one entry **/
  4134. if (xs_type(xs_dict_get(snac.config, "private")) == XSTYPE_TRUE)
  4135. return HTTP_STATUS_FORBIDDEN;
  4136. xs *id = xs_fmt("%s/%s", snac.actor, p_path);
  4137. xs *msg = NULL;
  4138. if (valid_status(object_get(id, &msg)) && is_msg_public(msg)) {
  4139. xs *md5 = xs_md5_hex(id, strlen(id));
  4140. xs *list = xs_list_new();
  4141. list = xs_list_append(list, md5);
  4142. *body = html_timeline(&snac, list, 1, 0, 0, 0, NULL, "", 1, error, terse);
  4143. *b_size = strlen(*body);
  4144. status = HTTP_STATUS_OK;
  4145. }
  4146. }
  4147. else
  4148. if (xs_startswith(p_path, "s/")) { /** a static file **/
  4149. xs *l = xs_split(p_path, "/");
  4150. const char *id = xs_list_get(l, 1);
  4151. int sz;
  4152. if (id && *id) {
  4153. status = static_get(&snac, id, body, &sz,
  4154. xs_dict_get(req, "if-none-match"), etag);
  4155. if (valid_status(status)) {
  4156. *b_size = sz;
  4157. *ctype = (char *)xs_mime_by_ext(id);
  4158. }
  4159. }
  4160. }
  4161. else
  4162. if (xs_startswith(p_path, "h/")) { /** an entry from the history **/
  4163. if (xs_type(xs_dict_get(snac.config, "private")) == XSTYPE_TRUE)
  4164. return HTTP_STATUS_FORBIDDEN;
  4165. if (xs_type(xs_dict_get(srv_config, "disable_history")) == XSTYPE_TRUE)
  4166. return HTTP_STATUS_FORBIDDEN;
  4167. xs *l = xs_split(p_path, "/");
  4168. const char *id = xs_list_get(l, 1);
  4169. if (id && *id) {
  4170. if (xs_endswith(id, "timeline.html_")) {
  4171. /* Don't let them in */
  4172. *b_size = 0;
  4173. status = HTTP_STATUS_NOT_FOUND;
  4174. }
  4175. else
  4176. status = history_get(&snac, id, body, b_size,
  4177. xs_dict_get(req, "if-none-match"), etag);
  4178. }
  4179. }
  4180. else
  4181. if (strcmp(p_path, ".rss") == 0) { /** public timeline in RSS format **/
  4182. if (xs_type(xs_dict_get(snac.config, "private")) == XSTYPE_TRUE)
  4183. return HTTP_STATUS_FORBIDDEN;
  4184. int cnt = xs_number_get(xs_dict_get_def(srv_config, "max_public_entries", "20"));
  4185. xs *elems = timeline_simple_list(&snac, "public", 0, cnt, NULL);
  4186. xs *bio = xs_dup(xs_dict_get(snac.config, "bio"));
  4187. xs *rss_title = xs_fmt("%s (@%s@%s)",
  4188. xs_dict_get(snac.config, "name"),
  4189. snac.uid,
  4190. xs_dict_get(srv_config, "host"));
  4191. xs *rss_link = xs_fmt("%s.rss", snac.actor);
  4192. *body = rss_from_timeline(&snac, elems, rss_title, rss_link, bio);
  4193. *b_size = strlen(*body);
  4194. *ctype = "application/rss+xml; charset=utf-8";
  4195. status = HTTP_STATUS_OK;
  4196. snac_debug(&snac, 1, xs_fmt("serving RSS"));
  4197. }
  4198. else
  4199. if (proxy && (xs_startswith(p_path, "x/") || xs_startswith(p_path, "y/"))) { /** remote media by proxy **/
  4200. xs *proxy_prefix = NULL;
  4201. if (xs_startswith(p_path, "x/")) {
  4202. /* proxy usage authorized by http basic auth */
  4203. if (login(&snac, req))
  4204. proxy_prefix = xs_str_new("x/");
  4205. else {
  4206. *body = xs_dup(uid);
  4207. status = HTTP_STATUS_UNAUTHORIZED;
  4208. }
  4209. }
  4210. else {
  4211. /* proxy usage authorized by proxy_token */
  4212. xs *tks = xs_fmt("%s:%s", srv_proxy_token_seed, snac.actor);
  4213. xs *tk = xs_md5_hex(tks, strlen(tks));
  4214. xs *p = xs_fmt("y/%s/", tk);
  4215. if (xs_startswith(p_path, p))
  4216. proxy_prefix = xs_dup(p);
  4217. }
  4218. if (proxy_prefix) {
  4219. /* pick the raw path (including optional ? arguments) */
  4220. const char *raw_path = xs_dict_get(req, "raw_path");
  4221. /* skip to where the proxy/ string starts */
  4222. raw_path += xs_str_in(raw_path, proxy_prefix);
  4223. xs *url = xs_replace_n(raw_path, proxy_prefix, "https:/" "/", 1);
  4224. xs *hdrs = xs_dict_new();
  4225. hdrs = xs_dict_append(hdrs, "user-agent", USER_AGENT);
  4226. const char *ims = xs_dict_get(req, "if-modified-since");
  4227. const char *inm = xs_dict_get(req, "if-none-match");
  4228. if (ims) hdrs = xs_dict_append(hdrs, "if-modified-since", ims);
  4229. if (inm) hdrs = xs_dict_append(hdrs, "if-none-match", inm);
  4230. xs *rsp = xs_http_request("GET", url, hdrs,
  4231. NULL, 0, &status, body, b_size, 0);
  4232. if (valid_status(status)) {
  4233. const char *ct = xs_or(xs_dict_get(rsp, "content-type"), "");
  4234. const char *lm = xs_dict_get(rsp, "last-modified");
  4235. const char *et = xs_dict_get(rsp, "etag");
  4236. if (lm) *last_modified = xs_dup(lm);
  4237. if (et) *etag = xs_dup(et);
  4238. /* find the content-type in the static mime types,
  4239. and return that value instead of ct, which will
  4240. be destroyed when out of scope */
  4241. for (int n = 0; xs_mime_types[n]; n += 2) {
  4242. if (strcmp(ct, xs_mime_types[n + 1]) == 0) {
  4243. *ctype = (char *)xs_mime_types[n + 1];
  4244. break;
  4245. }
  4246. }
  4247. }
  4248. snac_debug(&snac, 1, xs_fmt("Proxy for %s %d", url, status));
  4249. }
  4250. }
  4251. else
  4252. if (strcmp(p_path, "share") == 0) { /** direct post **/
  4253. if (!login(&snac, req)) {
  4254. *body = xs_dup(uid);
  4255. status = HTTP_STATUS_UNAUTHORIZED;
  4256. }
  4257. else {
  4258. const char *b64 = xs_dict_get(q_vars, "content");
  4259. int sz;
  4260. xs *content = xs_base64_dec(b64, &sz);
  4261. xs *msg = msg_note(&snac, content, NULL, NULL, NULL, 0, NULL, NULL);
  4262. xs *c_msg = msg_create(&snac, msg);
  4263. timeline_add(&snac, xs_dict_get(msg, "id"), msg);
  4264. enqueue_message(&snac, c_msg);
  4265. snac_debug(&snac, 1, xs_fmt("web action 'share' received"));
  4266. *body = xs_fmt("%s/admin", snac.actor);
  4267. *b_size = strlen(*body);
  4268. status = HTTP_STATUS_SEE_OTHER;
  4269. }
  4270. }
  4271. else
  4272. if (strcmp(p_path, "authorize_interaction") == 0) { /** follow, like or boost from Mastodon **/
  4273. if (!login(&snac, req)) {
  4274. *body = xs_dup(uid);
  4275. status = HTTP_STATUS_UNAUTHORIZED;
  4276. }
  4277. else {
  4278. status = HTTP_STATUS_NOT_FOUND;
  4279. const char *id = xs_dict_get(q_vars, "id");
  4280. const char *action = xs_dict_get(q_vars, "action");
  4281. if (xs_is_string(id) && xs_is_string(action)) {
  4282. if (strcmp(action, "Follow") == 0) {
  4283. xs *msg = msg_follow(&snac, id);
  4284. if (msg != NULL) {
  4285. const char *actor = xs_dict_get(msg, "object");
  4286. following_add(&snac, actor, msg);
  4287. enqueue_output_by_actor(&snac, msg, actor, 0);
  4288. status = HTTP_STATUS_SEE_OTHER;
  4289. }
  4290. }
  4291. else
  4292. if (xs_match(action, "Like|Boost|Announce")) {
  4293. /* bring the post */
  4294. xs *msg = msg_admiration(&snac, id, *action == 'L' ? "Like" : "Announce");
  4295. if (msg != NULL) {
  4296. timeline_admire(&snac, xs_dict_get(msg, "object"), snac.actor, *action == 'L' ? 1 : 0, msg);
  4297. enqueue_message(&snac, msg);
  4298. status = HTTP_STATUS_SEE_OTHER;
  4299. }
  4300. }
  4301. }
  4302. if (status == HTTP_STATUS_SEE_OTHER) {
  4303. *body = xs_fmt("%s/admin", snac.actor);
  4304. *b_size = strlen(*body);
  4305. }
  4306. }
  4307. }
  4308. else
  4309. status = HTTP_STATUS_NOT_FOUND;
  4310. user_free(&snac);
  4311. if (valid_status(status) && *ctype == NULL) {
  4312. *ctype = "text/html; charset=utf-8";
  4313. }
  4314. return status;
  4315. }
  4316. int html_post_handler(const xs_dict *req, const char *q_path,
  4317. char *payload, int p_size,
  4318. char **body, int *b_size, char **ctype)
  4319. {
  4320. (void)p_size;
  4321. (void)ctype;
  4322. int status = 0;
  4323. const snac *user = NULL;
  4324. snac snac;
  4325. const char *uid;
  4326. const char *p_path;
  4327. const xs_dict *p_vars;
  4328. xs *l = xs_split_n(q_path, "/", 2);
  4329. uid = xs_list_get(l, 1);
  4330. if (!uid || !user_open(&snac, uid)) {
  4331. /* invalid user */
  4332. srv_debug(1, xs_fmt("html_post_handler bad user %s", uid));
  4333. return HTTP_STATUS_NOT_FOUND;
  4334. }
  4335. p_path = xs_list_get(l, 2);
  4336. /* all posts must be authenticated */
  4337. if (!login(&snac, req)) {
  4338. user_free(&snac);
  4339. *body = xs_dup(uid);
  4340. return HTTP_STATUS_UNAUTHORIZED;
  4341. }
  4342. user = &snac; /* for L() */
  4343. set_user_lang(&snac);
  4344. p_vars = xs_dict_get(req, "p_vars");
  4345. if (p_path && strcmp(p_path, "admin/note") == 0) { /** **/
  4346. snac_debug(&snac, 1, xs_fmt("web action '%s' received", p_path));
  4347. /* post note */
  4348. const char *content = xs_dict_get(p_vars, "content");
  4349. const char *in_reply_to = xs_dict_get(p_vars, "in_reply_to");
  4350. const char *to = xs_dict_get(p_vars, "to");
  4351. const char *sensitive = xs_dict_get(p_vars, "sensitive");
  4352. const char *summary = xs_dict_get(p_vars, "summary");
  4353. const char *edit_id = xs_dict_get(p_vars, "edit_id");
  4354. const char *post_date = xs_dict_get_def(p_vars, "post_date", "");
  4355. const char *post_time = xs_dict_get_def(p_vars, "post_time", "");
  4356. const char *post_lang = xs_dict_get(p_vars, "post_lang");
  4357. const char *visibility = xs_dict_get(p_vars, "visibility");
  4358. int scope = SCOPE_PUBLIC; /* default to public */
  4359. if (!xs_is_null(visibility)) {
  4360. if (strcmp(visibility, "unlisted") == 0)
  4361. scope = SCOPE_UNLISTED;
  4362. else
  4363. if (strcmp(visibility, "followers") == 0)
  4364. scope = SCOPE_FOLLOWERS;
  4365. else
  4366. if (strcmp(visibility, "mentioned") == 0)
  4367. scope = SCOPE_MENTIONED;
  4368. }
  4369. int store_as_draft = !xs_is_null(xs_dict_get(p_vars, "is_draft"));
  4370. xs *attach_list = xs_list_new();
  4371. /* iterate the attachments */
  4372. int max_attachments = xs_number_get(xs_dict_get_def(srv_config, "max_attachments", "4"));
  4373. for (int att_n = 0; att_n < max_attachments; att_n++) {
  4374. xs *url_lbl = xs_fmt("attach_url_%d", att_n);
  4375. xs *att_lbl = xs_fmt("attach_%d", att_n);
  4376. xs *alt_lbl = xs_fmt("alt_text_%d", att_n);
  4377. const char *attach_url = xs_dict_get(p_vars, url_lbl);
  4378. const xs_list *attach_file = xs_dict_get(p_vars, att_lbl);
  4379. const char *alt_text = xs_dict_get_def(p_vars, alt_lbl, "");
  4380. if (xs_is_string(attach_url) && *attach_url != '\0') {
  4381. xs *l = xs_list_new();
  4382. l = xs_list_append(l, attach_url);
  4383. l = xs_list_append(l, alt_text);
  4384. attach_list = xs_list_append(attach_list, l);
  4385. }
  4386. else
  4387. if (xs_is_list(attach_file)) {
  4388. const char *fn = xs_list_get(attach_file, 0);
  4389. if (xs_is_string(fn) && *fn != '\0') {
  4390. char rnd[32];
  4391. xs_rnd_buf(rnd, sizeof(rnd));
  4392. const char *ext = strrchr(fn, '.');
  4393. xs *hash = xs_md5_hex(rnd, sizeof(rnd));
  4394. xs *id = xs_fmt("post-%s%s", hash, ext ? ext : "");
  4395. xs *url = xs_fmt("%s/s/%s", snac.actor, id);
  4396. int fo = xs_number_get(xs_list_get(attach_file, 1));
  4397. int fs = xs_number_get(xs_list_get(attach_file, 2));
  4398. /* store */
  4399. static_put(&snac, id, payload + fo, fs);
  4400. xs *l = xs_list_new();
  4401. l = xs_list_append(l, url);
  4402. l = xs_list_append(l, alt_text);
  4403. attach_list = xs_list_append(attach_list, l);
  4404. }
  4405. }
  4406. }
  4407. if (content != NULL) {
  4408. xs *msg = NULL;
  4409. xs *c_msg = NULL;
  4410. xs *content_2 = xs_replace(content, "\r", "");
  4411. xs *poll_opts = NULL;
  4412. /* is there a valid set of poll options? */
  4413. const char *v = xs_dict_get(p_vars, "poll_options");
  4414. if (!xs_is_null(v) && *v) {
  4415. xs *v2 = xs_strip_i(xs_replace(v, "\r", ""));
  4416. poll_opts = xs_split(v2, "\n");
  4417. }
  4418. if (!xs_is_null(poll_opts) && xs_list_len(poll_opts)) {
  4419. /* get the rest of poll configuration */
  4420. const char *p_multiple = xs_dict_get(p_vars, "poll_multiple");
  4421. const char *p_end_secs = xs_dict_get(p_vars, "poll_end_secs");
  4422. int multiple = 0;
  4423. int end_secs = atoi(!xs_is_null(p_end_secs) ? p_end_secs : "60");
  4424. if (!xs_is_null(p_multiple) && strcmp(p_multiple, "on") == 0)
  4425. multiple = 1;
  4426. msg = msg_question(&snac, content_2, attach_list,
  4427. poll_opts, multiple, end_secs);
  4428. enqueue_close_question(&snac, xs_dict_get(msg, "id"), end_secs);
  4429. }
  4430. else
  4431. msg = msg_note(&snac, content_2, to, in_reply_to, attach_list, scope, post_lang, NULL);
  4432. if (sensitive != NULL) {
  4433. msg = xs_dict_set(msg, "sensitive", xs_stock(XSTYPE_TRUE));
  4434. msg = xs_dict_set(msg, "summary", xs_is_null(summary) ? "..." : summary);
  4435. }
  4436. if (xs_is_string(post_date) && *post_date) {
  4437. xs *post_pubdate = xs_fmt("%sT%s", post_date,
  4438. xs_is_string(post_time) && *post_time ? post_time : "00:00:00");
  4439. time_t t = xs_parse_iso_date(post_pubdate, 0);
  4440. if (t != 0) {
  4441. t -= xs_tz_offset(snac.tz);
  4442. xs *iso_date = xs_str_iso_date(t);
  4443. msg = xs_dict_set(msg, "published", iso_date);
  4444. snac_debug(&snac, 1, xs_fmt("Published date: [%s]", iso_date));
  4445. }
  4446. else
  4447. snac_log(&snac, xs_fmt("Invalid post date: [%s]", post_pubdate));
  4448. }
  4449. /* is the published date from the future? */
  4450. int future_post = 0;
  4451. xs *right_now = xs_str_utctime(0, ISO_DATE_SPEC);
  4452. if (strcmp(xs_dict_get(msg, "published"), right_now) > 0)
  4453. future_post = 1;
  4454. if (xs_is_null(edit_id)) {
  4455. /* new message */
  4456. const char *id = xs_dict_get(msg, "id");
  4457. if (store_as_draft) {
  4458. draft_add(&snac, id, msg);
  4459. }
  4460. else
  4461. if (future_post) {
  4462. schedule_add(&snac, id, msg);
  4463. }
  4464. else {
  4465. c_msg = msg_create(&snac, msg);
  4466. timeline_add(&snac, id, msg);
  4467. }
  4468. }
  4469. else {
  4470. /* an edition of a previous message */
  4471. xs *p_msg = NULL;
  4472. if (valid_status(object_get(edit_id, &p_msg))) {
  4473. /* copy relevant fields from previous version */
  4474. char *fields[] = { "id", "context", "url",
  4475. "to", "inReplyTo", NULL };
  4476. int n;
  4477. for (n = 0; fields[n]; n++) {
  4478. const char *v = xs_dict_get(p_msg, fields[n]);
  4479. msg = xs_dict_set(msg, fields[n], v);
  4480. }
  4481. if (store_as_draft) {
  4482. draft_add(&snac, edit_id, msg);
  4483. }
  4484. else
  4485. if (is_draft(&snac, edit_id)) {
  4486. /* message was previously a draft; it's a create activity */
  4487. /* if the date is from the past, overwrite it with right_now */
  4488. if (strcmp(xs_dict_get(msg, "published"), right_now) < 0) {
  4489. snac_debug(&snac, 1, xs_fmt("setting draft ancient date to %s", right_now));
  4490. msg = xs_dict_set(msg, "published", right_now);
  4491. }
  4492. /* overwrite object */
  4493. object_add_ow(edit_id, msg);
  4494. if (future_post) {
  4495. schedule_add(&snac, edit_id, msg);
  4496. }
  4497. else {
  4498. c_msg = msg_create(&snac, msg);
  4499. timeline_add(&snac, edit_id, msg);
  4500. }
  4501. draft_del(&snac, edit_id);
  4502. }
  4503. else
  4504. if (is_scheduled(&snac, edit_id)) {
  4505. /* editing an scheduled post; just update it */
  4506. schedule_add(&snac, edit_id, msg);
  4507. }
  4508. else {
  4509. /* ignore the (possibly changed) published date */
  4510. msg = xs_dict_set(msg, "published", xs_dict_get(p_msg, "published"));
  4511. /* set the updated field */
  4512. xs *updated = xs_str_utctime(0, ISO_DATE_SPEC);
  4513. msg = xs_dict_set(msg, "updated", updated);
  4514. /* overwrite object, not updating the indexes */
  4515. object_add_ow(edit_id, msg);
  4516. /* index tags */
  4517. tag_index(edit_id, msg);
  4518. /* update message */
  4519. c_msg = msg_update(&snac, msg);
  4520. }
  4521. }
  4522. else
  4523. snac_log(&snac, xs_fmt("cannot get object '%s' for editing", edit_id));
  4524. }
  4525. if (c_msg != NULL) {
  4526. enqueue_message(&snac, c_msg);
  4527. enqueue_webmention(msg);
  4528. }
  4529. history_del(&snac, "timeline.html_");
  4530. }
  4531. status = HTTP_STATUS_SEE_OTHER;
  4532. }
  4533. else
  4534. if (p_path && strcmp(p_path, "admin/action") == 0) { /** **/
  4535. /* action on an entry */
  4536. const char *id = xs_dict_get(p_vars, "id");
  4537. const char *actor = xs_dict_get(p_vars, "actor");
  4538. const char *action = xs_dict_get(p_vars, "action");
  4539. const char *group = xs_dict_get(p_vars, "group");
  4540. if (action == NULL)
  4541. return HTTP_STATUS_NOT_FOUND;
  4542. snac_debug(&snac, 1, xs_fmt("web action '%s' received", action));
  4543. status = HTTP_STATUS_SEE_OTHER;
  4544. if (strcmp(action, L("EmojiUnreact")) == 0) { /** **/
  4545. const char *eid = xs_dict_get(p_vars, "eid");
  4546. if (eid != NULL) {
  4547. xs *n_msg = msg_emoji_unreact(&snac, id, eid);
  4548. if (n_msg != NULL)
  4549. enqueue_message(&snac, n_msg);
  4550. }
  4551. }
  4552. else
  4553. if (strcmp(action, L("EmojiReact")) == 0) { /** **/
  4554. xs *eid = xs_dup(xs_dict_get(p_vars, "eid"));
  4555. eid = xs_strip_chars_i(eid, ":");
  4556. const xs_dict *ret = msg_emoji_init(&snac, id, eid);
  4557. /* fails if either invalid or already reacted */
  4558. if (!ret)
  4559. ret = msg_emoji_unreact(&snac, id, eid);
  4560. if (!ret)
  4561. status = HTTP_STATUS_NOT_FOUND;
  4562. }
  4563. else
  4564. if (strcmp(action, L("Like")) == 0) { /** **/
  4565. xs *msg = msg_admiration(&snac, id, "Like");
  4566. if (msg != NULL) {
  4567. timeline_admire(&snac, xs_dict_get(msg, "object"), snac.actor, 1, msg);
  4568. enqueue_message(&snac, msg);
  4569. }
  4570. }
  4571. else
  4572. if (strcmp(action, L("Boost")) == 0) { /** **/
  4573. xs *msg = msg_admiration(&snac, id, "Announce");
  4574. if (msg != NULL) {
  4575. timeline_admire(&snac, xs_dict_get(msg, "object"), snac.actor, 0, msg);
  4576. enqueue_message(&snac, msg);
  4577. }
  4578. }
  4579. else
  4580. if (strcmp(action, L("Unlike")) == 0) { /** **/
  4581. xs *msg = msg_repulsion(&snac, id, "Like");
  4582. if (msg != NULL) {
  4583. enqueue_message(&snac, msg);
  4584. }
  4585. }
  4586. else
  4587. if (strcmp(action, L("Unboost")) == 0) { /** **/
  4588. xs *msg = msg_repulsion(&snac, id, "Announce");
  4589. if (msg != NULL) {
  4590. enqueue_message(&snac, msg);
  4591. }
  4592. }
  4593. else
  4594. if (strcmp(action, L("MUTE")) == 0) { /** **/
  4595. mute(&snac, actor);
  4596. }
  4597. else
  4598. if (strcmp(action, L("Unmute")) == 0) { /** **/
  4599. unmute(&snac, actor);
  4600. }
  4601. else
  4602. if (strcmp(action, L("Hide")) == 0) { /** **/
  4603. if (is_draft(&snac, id))
  4604. draft_del(&snac, id);
  4605. else
  4606. if (is_scheduled(&snac, id))
  4607. schedule_del(&snac, id);
  4608. else
  4609. hide(&snac, id);
  4610. }
  4611. else
  4612. if (strcmp(action, L("Limit")) == 0) { /** **/
  4613. limit(&snac, actor);
  4614. }
  4615. else
  4616. if (strcmp(action, L("Unlimit")) == 0) { /** **/
  4617. unlimit(&snac, actor);
  4618. }
  4619. else
  4620. if (strcmp(action, L("Follow")) == 0) { /** **/
  4621. xs *msg = msg_follow(&snac, actor);
  4622. if (msg != NULL) {
  4623. /* reload the actor from the message, in may be different */
  4624. actor = xs_dict_get(msg, "object");
  4625. following_add(&snac, actor, msg);
  4626. enqueue_output_by_actor(&snac, msg, actor, 0);
  4627. }
  4628. }
  4629. else
  4630. if (strcmp(action, L("Unfollow")) == 0) { /** **/
  4631. /* get the following object */
  4632. xs *object = NULL;
  4633. if (valid_status(following_get(&snac, actor, &object))) {
  4634. xs *msg = msg_undo(&snac, xs_dict_get(object, "object"));
  4635. following_del(&snac, actor);
  4636. enqueue_output_by_actor(&snac, msg, actor, 0);
  4637. snac_log(&snac, xs_fmt("unfollowed actor %s", actor));
  4638. }
  4639. else
  4640. snac_log(&snac, xs_fmt("actor is not being followed %s", actor));
  4641. }
  4642. else
  4643. if (strcmp(action, L("Follow Group")) == 0) { /** **/
  4644. xs *msg = msg_follow(&snac, group);
  4645. if (msg != NULL) {
  4646. /* reload the group from the message, in may be different */
  4647. group = xs_dict_get(msg, "object");
  4648. following_add(&snac, group, msg);
  4649. enqueue_output_by_actor(&snac, msg, group, 0);
  4650. }
  4651. }
  4652. else
  4653. if (strcmp(action, L("Unfollow Group")) == 0) { /** **/
  4654. /* get the following object */
  4655. xs *object = NULL;
  4656. if (valid_status(following_get(&snac, group, &object))) {
  4657. xs *msg = msg_undo(&snac, xs_dict_get(object, "object"));
  4658. following_del(&snac, group);
  4659. enqueue_output_by_actor(&snac, msg, group, 0);
  4660. snac_log(&snac, xs_fmt("unfollowed group %s", group));
  4661. }
  4662. else
  4663. snac_log(&snac, xs_fmt("actor is not being followed %s", actor));
  4664. }
  4665. else
  4666. if (strcmp(action, L("Delete")) == 0) { /** **/
  4667. const char *actor_form = xs_dict_get(p_vars, "actor-form");
  4668. if (actor_form != NULL) {
  4669. /* delete follower */
  4670. if (valid_status(follower_del(&snac, actor)))
  4671. snac_log(&snac, xs_fmt("deleted follower %s", actor));
  4672. else
  4673. snac_log(&snac, xs_fmt("error deleting follower %s", actor));
  4674. }
  4675. else {
  4676. /* delete an entry */
  4677. if (is_msg_mine(&snac, id) && !is_draft(&snac, id)) {
  4678. /* it's a post by us: generate a delete */
  4679. xs *msg = msg_delete(&snac, id);
  4680. enqueue_message(&snac, msg);
  4681. snac_log(&snac, xs_fmt("posted tombstone for %s", id));
  4682. }
  4683. timeline_del(&snac, id);
  4684. draft_del(&snac, id);
  4685. schedule_del(&snac, id);
  4686. snac_log(&snac, xs_fmt("deleted entry %s", id));
  4687. }
  4688. }
  4689. else
  4690. if (strcmp(action, L("Pin")) == 0) { /** **/
  4691. pin(&snac, id);
  4692. timeline_touch(&snac);
  4693. }
  4694. else
  4695. if (strcmp(action, L("Unpin")) == 0) { /** **/
  4696. unpin(&snac, id);
  4697. timeline_touch(&snac);
  4698. }
  4699. else
  4700. if (strcmp(action, L("Bookmark")) == 0) { /** **/
  4701. bookmark(&snac, id);
  4702. timeline_touch(&snac);
  4703. }
  4704. else
  4705. if (strcmp(action, L("Unbookmark")) == 0) { /** **/
  4706. unbookmark(&snac, id);
  4707. timeline_touch(&snac);
  4708. }
  4709. else
  4710. if (strcmp(action, L("Approve")) == 0) { /** **/
  4711. xs *fwreq = pending_get(&snac, actor);
  4712. if (fwreq != NULL) {
  4713. xs *reply = msg_accept(&snac, fwreq, actor);
  4714. enqueue_message(&snac, reply);
  4715. if (xs_is_null(xs_dict_get(fwreq, "published"))) {
  4716. /* add a date if it doesn't include one (Mastodon) */
  4717. xs *date = xs_str_utctime(0, ISO_DATE_SPEC);
  4718. fwreq = xs_dict_set(fwreq, "published", date);
  4719. }
  4720. timeline_add(&snac, xs_dict_get(fwreq, "id"), fwreq);
  4721. follower_add(&snac, actor);
  4722. pending_del(&snac, actor);
  4723. snac_log(&snac, xs_fmt("new follower %s", actor));
  4724. }
  4725. }
  4726. else
  4727. if (strcmp(action, L("Discard")) == 0) { /** **/
  4728. pending_del(&snac, actor);
  4729. }
  4730. else
  4731. status = HTTP_STATUS_NOT_FOUND;
  4732. /* delete the cached timeline */
  4733. if (status == HTTP_STATUS_SEE_OTHER)
  4734. history_del(&snac, "timeline.html_");
  4735. }
  4736. else
  4737. if (p_path && strcmp(p_path, "admin/user-setup") == 0) { /** **/
  4738. /* change of user data */
  4739. const char *v;
  4740. const char *p1, *p2;
  4741. if ((v = xs_dict_get(p_vars, "name")) != NULL)
  4742. snac.config = xs_dict_set(snac.config, "name", v);
  4743. if ((v = xs_dict_get(p_vars, "avatar")) != NULL)
  4744. snac.config = xs_dict_set(snac.config, "avatar", v);
  4745. if ((v = xs_dict_get(p_vars, "bio")) != NULL)
  4746. snac.config = xs_dict_set(snac.config, "bio", v);
  4747. if ((v = xs_dict_get(p_vars, "cw")) != NULL &&
  4748. strcmp(v, "on") == 0) {
  4749. snac.config = xs_dict_set(snac.config, "cw", "open");
  4750. } else { /* if the checkbox is not set, the parameter is missing */
  4751. snac.config = xs_dict_set(snac.config, "cw", "");
  4752. }
  4753. if ((v = xs_dict_get(p_vars, "email")) != NULL)
  4754. snac.config = xs_dict_set(snac.config, "email", v);
  4755. if ((v = xs_dict_get(p_vars, "telegram_bot")) != NULL)
  4756. snac.config = xs_dict_set(snac.config, "telegram_bot", v);
  4757. if ((v = xs_dict_get(p_vars, "telegram_chat_id")) != NULL)
  4758. snac.config = xs_dict_set(snac.config, "telegram_chat_id", v);
  4759. if ((v = xs_dict_get(p_vars, "ntfy_server")) != NULL)
  4760. snac.config = xs_dict_set(snac.config, "ntfy_server", v);
  4761. if ((v = xs_dict_get(p_vars, "ntfy_token")) != NULL)
  4762. snac.config = xs_dict_set(snac.config, "ntfy_token", v);
  4763. if ((v = xs_dict_get(p_vars, "purge_days")) != NULL) {
  4764. xs *days = xs_number_new(atof(v));
  4765. snac.config = xs_dict_set(snac.config, "purge_days", days);
  4766. }
  4767. if ((v = xs_dict_get(p_vars, "drop_dm_from_unknown")) != NULL && strcmp(v, "on") == 0)
  4768. snac.config = xs_dict_set(snac.config, "drop_dm_from_unknown", xs_stock(XSTYPE_TRUE));
  4769. else
  4770. snac.config = xs_dict_set(snac.config, "drop_dm_from_unknown", xs_stock(XSTYPE_FALSE));
  4771. if ((v = xs_dict_get(p_vars, "bot")) != NULL && strcmp(v, "on") == 0)
  4772. snac.config = xs_dict_set(snac.config, "bot", xs_stock(XSTYPE_TRUE));
  4773. else
  4774. snac.config = xs_dict_set(snac.config, "bot", xs_stock(XSTYPE_FALSE));
  4775. if ((v = xs_dict_get(p_vars, "private")) != NULL && strcmp(v, "on") == 0)
  4776. snac.config = xs_dict_set(snac.config, "private", xs_stock(XSTYPE_TRUE));
  4777. else
  4778. snac.config = xs_dict_set(snac.config, "private", xs_stock(XSTYPE_FALSE));
  4779. if ((v = xs_dict_get(p_vars, "auto_boost")) != NULL && strcmp(v, "on") == 0)
  4780. snac.config = xs_dict_set(snac.config, "auto_boost", xs_stock(XSTYPE_TRUE));
  4781. else
  4782. snac.config = xs_dict_set(snac.config, "auto_boost", xs_stock(XSTYPE_FALSE));
  4783. if ((v = xs_dict_get(p_vars, "collapse_threads")) != NULL && strcmp(v, "on") == 0)
  4784. snac.config = xs_dict_set(snac.config, "collapse_threads", xs_stock(XSTYPE_TRUE));
  4785. else
  4786. snac.config = xs_dict_set(snac.config, "collapse_threads", xs_stock(XSTYPE_FALSE));
  4787. if ((v = xs_dict_get(p_vars, "approve_followers")) != NULL && strcmp(v, "on") == 0)
  4788. snac.config = xs_dict_set(snac.config, "approve_followers", xs_stock(XSTYPE_TRUE));
  4789. else
  4790. snac.config = xs_dict_set(snac.config, "approve_followers", xs_stock(XSTYPE_FALSE));
  4791. if ((v = xs_dict_get(p_vars, "show_contact_metrics")) != NULL && strcmp(v, "on") == 0)
  4792. snac.config = xs_dict_set(snac.config, "show_contact_metrics", xs_stock(XSTYPE_TRUE));
  4793. else
  4794. snac.config = xs_dict_set(snac.config, "show_contact_metrics", xs_stock(XSTYPE_FALSE));
  4795. if ((v = xs_dict_get(p_vars, "web_ui_lang")) != NULL)
  4796. snac.config = xs_dict_set(snac.config, "lang", v);
  4797. if ((v = xs_dict_get(p_vars, "tz")) != NULL)
  4798. snac.config = xs_dict_set(snac.config, "tz", v);
  4799. if ((v = xs_dict_get(p_vars, "post_langs")) != NULL)
  4800. snac.config = xs_dict_set(snac.config, "post_langs", v);
  4801. snac.config = xs_dict_set(snac.config, "latitude", xs_dict_get_def(p_vars, "latitude", ""));
  4802. snac.config = xs_dict_set(snac.config, "longitude", xs_dict_get_def(p_vars, "longitude", ""));
  4803. snac.config = xs_dict_set(snac.config, "notify_webhook", xs_dict_get_def(p_vars, "notify_webhook", ""));
  4804. if ((v = xs_dict_get(p_vars, "metadata")) != NULL)
  4805. snac.config = xs_dict_set(snac.config, "metadata", v);
  4806. /* uploads */
  4807. const char *uploads[] = { "avatar", "header", NULL };
  4808. int n;
  4809. for (n = 0; uploads[n]; n++) {
  4810. xs *var_name = xs_fmt("%s_file", uploads[n]);
  4811. const xs_list *uploaded_file = xs_dict_get(p_vars, var_name);
  4812. if (xs_type(uploaded_file) == XSTYPE_LIST) {
  4813. const char *fn = xs_list_get(uploaded_file, 0);
  4814. if (fn && *fn) {
  4815. const char *mimetype = xs_mime_by_ext(fn);
  4816. if (xs_startswith(mimetype, "image/")) {
  4817. const char *ext = strrchr(fn, '.');
  4818. xs *hash = xs_md5_hex(fn, strlen(fn));
  4819. xs *id = xs_fmt("%s-%s%s", uploads[n], hash, ext ? ext : "");
  4820. xs *url = xs_fmt("%s/s/%s", snac.actor, id);
  4821. int fo = xs_number_get(xs_list_get(uploaded_file, 1));
  4822. int fs = xs_number_get(xs_list_get(uploaded_file, 2));
  4823. /* store */
  4824. static_put(&snac, id, payload + fo, fs);
  4825. snac.config = xs_dict_set(snac.config, uploads[n], url);
  4826. }
  4827. }
  4828. }
  4829. }
  4830. /* delete images by removing url from user.json */
  4831. for (n = 0; uploads[n]; n++) {
  4832. xs *var_name = xs_fmt("%s_delete", uploads[n]);
  4833. const char *delete_var = xs_dict_get(p_vars, var_name);
  4834. if (delete_var != NULL && strcmp(delete_var, "on") == 0) {
  4835. snac.config = xs_dict_set(snac.config, uploads[n], "");
  4836. }
  4837. }
  4838. /* password change? */
  4839. if ((p1 = xs_dict_get(p_vars, "passwd1")) != NULL &&
  4840. (p2 = xs_dict_get(p_vars, "passwd2")) != NULL &&
  4841. *p1 && strcmp(p1, p2) == 0) {
  4842. xs *pw = hash_password(snac.uid, p1, NULL);
  4843. snac.config = xs_dict_set(snac.config, "passwd", pw);
  4844. }
  4845. user_persist(&snac, 1);
  4846. status = HTTP_STATUS_SEE_OTHER;
  4847. }
  4848. else
  4849. if (p_path && strcmp(p_path, "admin/clear-notifications") == 0) { /** **/
  4850. notify_clear(&snac);
  4851. timeline_touch(&snac);
  4852. status = HTTP_STATUS_SEE_OTHER;
  4853. }
  4854. else
  4855. if (p_path && strcmp(p_path, "admin/vote") == 0) { /** **/
  4856. const char *irt = xs_dict_get(p_vars, "irt");
  4857. const char *opt = xs_dict_get(p_vars, "question");
  4858. const char *actor = xs_dict_get(p_vars, "actor");
  4859. xs *ls = NULL;
  4860. /* multiple choices? */
  4861. if (xs_type(opt) == XSTYPE_LIST)
  4862. ls = xs_dup(opt);
  4863. else
  4864. if (xs_type(opt) == XSTYPE_STRING) {
  4865. ls = xs_list_new();
  4866. ls = xs_list_append(ls, opt);
  4867. }
  4868. const xs_str *v;
  4869. int c = 0;
  4870. while (xs_list_next(ls, &v, &c)) {
  4871. xs *msg = msg_note(&snac, "", actor, irt, NULL, 1, NULL, NULL);
  4872. /* set the option */
  4873. msg = xs_dict_append(msg, "name", v);
  4874. /* delete the content */
  4875. msg = xs_dict_del(msg, "content");
  4876. xs *c_msg = msg_create(&snac, msg);
  4877. enqueue_message(&snac, c_msg);
  4878. timeline_add(&snac, xs_dict_get(msg, "id"), msg);
  4879. }
  4880. if (ls != NULL) {
  4881. /* get the poll object */
  4882. xs *poll = NULL;
  4883. if (valid_status(object_get(irt, &poll))) {
  4884. const char *date = xs_dict_get(poll, "endTime");
  4885. if (xs_is_null(date))
  4886. date = xs_dict_get(poll, "closed");
  4887. if (!xs_is_null(date)) {
  4888. time_t t = xs_parse_iso_date(date, 0) - time(NULL);
  4889. /* request the poll when it's closed;
  4890. Pleroma does not send and update when the poll closes */
  4891. enqueue_object_request(&snac, irt, t + 2);
  4892. }
  4893. }
  4894. }
  4895. status = HTTP_STATUS_SEE_OTHER;
  4896. }
  4897. else
  4898. if (p_path && strcmp(p_path, "admin/followed-hashtags") == 0) { /** **/
  4899. const char *followed_hashtags = xs_dict_get(p_vars, "followed_hashtags");
  4900. if (xs_is_string(followed_hashtags)) {
  4901. xs *new_hashtags = xs_list_new();
  4902. xs *l = xs_split(followed_hashtags, "\n");
  4903. const char *v;
  4904. xs_list_foreach(l, v) {
  4905. xs *s1 = xs_strip_i(xs_dup(v));
  4906. s1 = xs_replace_i(s1, " ", "");
  4907. if (*s1 == '\0')
  4908. continue;
  4909. xs *s2 = NULL;
  4910. if (xs_startswith(s1, "https:/"))
  4911. s2 = xs_dup(s1);
  4912. else {
  4913. s2 = xs_utf8_to_lower(s1);
  4914. if (*s2 != '#')
  4915. s2 = xs_str_prepend_i(s2, "#");
  4916. }
  4917. new_hashtags = xs_list_append(new_hashtags, s2);
  4918. }
  4919. snac.config = xs_dict_set(snac.config, "followed_hashtags", new_hashtags);
  4920. user_persist(&snac, 0);
  4921. }
  4922. status = HTTP_STATUS_SEE_OTHER;
  4923. }
  4924. else
  4925. if (p_path && strcmp(p_path, "admin/blocked-hashtags") == 0) { /** **/
  4926. const char *hashtags = xs_dict_get(p_vars, "blocked_hashtags");
  4927. if (xs_is_string(hashtags)) {
  4928. xs *new_hashtags = xs_list_new();
  4929. xs *l = xs_split(hashtags, "\n");
  4930. const char *v;
  4931. xs_list_foreach(l, v) {
  4932. xs *s1 = xs_strip_i(xs_dup(v));
  4933. s1 = xs_replace_i(s1, " ", "");
  4934. if (*s1 == '\0')
  4935. continue;
  4936. xs *s2 = xs_utf8_to_lower(s1);
  4937. if (*s2 != '#')
  4938. s2 = xs_str_prepend_i(s2, "#");
  4939. new_hashtags = xs_list_append(new_hashtags, s2);
  4940. }
  4941. snac.config = xs_dict_set(snac.config, "blocked_hashtags", new_hashtags);
  4942. user_persist(&snac, 0);
  4943. }
  4944. status = HTTP_STATUS_SEE_OTHER;
  4945. }
  4946. else
  4947. if (p_path && strcmp(p_path, "admin/muted-words") == 0) {
  4948. const char *words = xs_dict_get(p_vars, "muted_words");
  4949. if (xs_is_string(words)) {
  4950. xs *new_words = xs_list_new();
  4951. xs *l = xs_split(words, "\n");
  4952. const char *v;
  4953. xs_list_foreach(l, v) {
  4954. xs *s1 = xs_strip_i(xs_dup(v));
  4955. s1 = xs_replace_i(s1, " ", "");
  4956. if (*s1 == '\0')
  4957. continue;
  4958. xs *s2 = xs_utf8_to_lower(s1);
  4959. new_words = xs_list_insert_sorted(new_words, s2);
  4960. }
  4961. snac.config = xs_dict_set(snac.config, "muted_words", new_words);
  4962. user_persist(&snac, 0);
  4963. }
  4964. status = HTTP_STATUS_SEE_OTHER;
  4965. }
  4966. if (status == HTTP_STATUS_SEE_OTHER) {
  4967. const char *hard_redir = xs_dict_get(p_vars, "hard-redir");
  4968. if (xs_is_string(hard_redir))
  4969. *body = xs_dup(hard_redir);
  4970. else {
  4971. const char *redir = xs_dict_get(p_vars, "redir");
  4972. if (xs_is_null(redir))
  4973. redir = "top";
  4974. *body = xs_fmt("%s/admin#%s", snac.actor, redir);
  4975. }
  4976. *b_size = strlen(*body);
  4977. }
  4978. user_free(&snac);
  4979. return status;
  4980. }