html.c 223 KB

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