html.c 214 KB

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