html.c 218 KB

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