html.c 213 KB

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