html.c 221 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180
  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. if (*s) {
  1736. xs *s1 = s;
  1737. s = xs_fmt("\n\n\nCC: %s", s1);
  1738. }
  1739. return s;
  1740. }
  1741. xs_html *html_entry_controls(snac *user, const char *actor,
  1742. const xs_dict *msg, const char *md5)
  1743. {
  1744. const char *id = xs_dict_get(msg, "id");
  1745. const char *group = xs_dict_get(msg, "audience");
  1746. xs *likes = object_likes(id);
  1747. xs *boosts = object_announces(id);
  1748. xs *action = xs_fmt("%s/admin/action", user->actor);
  1749. xs *redir = xs_fmt("%s_entry", md5);
  1750. xs_html *form;
  1751. xs_html *controls = xs_html_tag("div",
  1752. xs_html_attr("class", "snac-controls"),
  1753. form = xs_html_tag("form",
  1754. xs_html_attr("autocomplete", "off"),
  1755. xs_html_attr("method", "post"),
  1756. xs_html_attr("action", action),
  1757. xs_html_sctag("input",
  1758. xs_html_attr("type", "hidden"),
  1759. xs_html_attr("name", "id"),
  1760. xs_html_attr("value", id)),
  1761. xs_html_sctag("input",
  1762. xs_html_attr("type", "hidden"),
  1763. xs_html_attr("name", "actor"),
  1764. xs_html_attr("value", actor)),
  1765. xs_html_sctag("input",
  1766. xs_html_attr("type", "hidden"),
  1767. xs_html_attr("name", "group"),
  1768. xs_html_attr("value", xs_is_null(group) ? "" : group)),
  1769. xs_html_sctag("input",
  1770. xs_html_attr("type", "hidden"),
  1771. xs_html_attr("name", "redir"),
  1772. xs_html_attr("value", redir))));
  1773. if (!is_msg_mine(user, id)) {
  1774. if (xs_list_in(likes, user->md5) == -1) {
  1775. /* not already liked; add button */
  1776. xs_html_add(form,
  1777. html_button("like", L("Like"), L("Say you like this post")));
  1778. }
  1779. else {
  1780. /* not like it anymore */
  1781. xs_html_add(form,
  1782. html_button("unlike", L("Unlike"), L("Nah don't like it that much")));
  1783. }
  1784. }
  1785. else {
  1786. if (is_pinned(user, id))
  1787. xs_html_add(form,
  1788. html_button("unpin", L("Unpin"), L("Unpin this post from your timeline")));
  1789. else
  1790. xs_html_add(form,
  1791. html_button("pin", L("Pin"), L("Pin this post to the top of your timeline")));
  1792. }
  1793. if (is_msg_public(msg)) {
  1794. if (xs_list_in(boosts, user->md5) == -1) {
  1795. /* not already boosted; add button */
  1796. xs_html_add(form,
  1797. html_button("boost", L("Boost"), L("Announce this post to your followers")));
  1798. }
  1799. else {
  1800. /* already boosted; add button to regret */
  1801. xs_html_add(form,
  1802. html_button("unboost", L("Unboost"), L("I regret I boosted this")));
  1803. }
  1804. }
  1805. if (is_bookmarked(user, id))
  1806. xs_html_add(form,
  1807. html_button("unbookmark", L("Unbookmark"), L("Delete this post from your bookmarks")));
  1808. else
  1809. xs_html_add(form,
  1810. html_button("bookmark", L("Bookmark"), L("Add this post to your bookmarks")));
  1811. if (strcmp(actor, user->actor) != 0) {
  1812. /* controls for other actors than this one */
  1813. if (following_check(user, actor)) {
  1814. xs_html_add(form,
  1815. html_button("unfollow", L("Unfollow"), L("Stop following this user's activity")));
  1816. }
  1817. else {
  1818. xs_html_add(form,
  1819. html_button("follow", L("Follow"), L("Start following this user's activity")));
  1820. }
  1821. if (!xs_is_null(group)) {
  1822. if (following_check(user, group)) {
  1823. xs_html_add(form,
  1824. html_button("unfollow", L("Unfollow Group"),
  1825. L("Stop following this group or channel")));
  1826. }
  1827. else {
  1828. xs_html_add(form,
  1829. html_button("follow", L("Follow Group"),
  1830. L("Start following this group or channel")));
  1831. }
  1832. }
  1833. xs_html_add(form,
  1834. html_button("mute", L("MUTE"),
  1835. L("Block any activity from this user forever")));
  1836. }
  1837. if (!xs_is_true(xs_dict_get(srv_config, "hide_delete_post_button")))
  1838. xs_html_add(form,
  1839. html_button("delete", L("Delete"), L("Delete this post")));
  1840. xs_html_add(form,
  1841. html_button("hide", L("Hide"), L("Hide this post and its children")));
  1842. const char *prev_src = xs_dict_get(msg, "sourceContent");
  1843. if (!xs_is_null(prev_src) && strcmp(actor, user->actor) == 0) { /** edit **/
  1844. /* post can be edited */
  1845. xs *div_id = xs_fmt("%s_edit", md5);
  1846. xs *form_id = xs_fmt("%s_edit_form", md5);
  1847. xs *redir = xs_fmt("%s_entry", md5);
  1848. xs *att_files = xs_list_new();
  1849. xs *att_alt_texts = xs_list_new();
  1850. const xs_list *att_list = xs_dict_get(msg, "attachment");
  1851. if (xs_is_list(att_list)) {
  1852. const xs_dict *d;
  1853. xs_list_foreach(att_list, d) {
  1854. const char *att_file = xs_dict_get(d, "url");
  1855. const char *att_alt_text = xs_dict_get(d, "name");
  1856. if (xs_is_string(att_file) && xs_is_string(att_alt_text)) {
  1857. att_files = xs_list_append(att_files, att_file);
  1858. att_alt_texts = xs_list_append(att_alt_texts, att_alt_text);
  1859. }
  1860. }
  1861. }
  1862. const char *note_lang = NULL;
  1863. const xs_dict *cmap = xs_dict_get(msg, "contentMap");
  1864. if (xs_is_dict(cmap)) {
  1865. const char *dummy;
  1866. int c = 0;
  1867. xs_dict_next(cmap, &note_lang, &dummy, &c);
  1868. }
  1869. const int edit_scope = get_msg_visibility(msg);
  1870. xs_html_add(controls, xs_html_tag("div",
  1871. xs_html_tag("p", NULL),
  1872. html_note(user, L("Edit..."),
  1873. div_id, form_id,
  1874. "", prev_src,
  1875. id, NULL,
  1876. xs_dict_get(msg, "sensitive"), xs_dict_get(msg, "summary"),
  1877. edit_scope, redir,
  1878. NULL, 0, att_files, att_alt_texts, is_draft(user, id),
  1879. xs_dict_get(msg, "published"), note_lang)),
  1880. xs_html_tag("p", NULL));
  1881. }
  1882. if (!xs_is_true(xs_dict_get(srv_config, "disable_emojireact"))) { /** emoji react **/
  1883. /* the post textarea */
  1884. xs *div_id = xs_fmt("%s_reply", md5);
  1885. xs *form_id = xs_fmt("%s_reply_form", md5);
  1886. xs *e_react = emoji_reacted(user, id);
  1887. xs_html_add(controls, xs_html_tag("div",
  1888. xs_html_tag("p", NULL),
  1889. html_emoji(
  1890. user, L("Emoji react..."),
  1891. div_id, form_id,
  1892. ":neocat:", id,
  1893. e_react)),
  1894. xs_html_tag("p", NULL));
  1895. }
  1896. { /** reply **/
  1897. /* the post textarea */
  1898. xs *ct = build_mentions(user, msg);
  1899. xs *div_id = xs_fmt("%s_reply", md5);
  1900. xs *form_id = xs_fmt("%s_reply_form", md5);
  1901. xs *redir = xs_fmt("%s_entry", md5);
  1902. const int scope = get_msg_visibility(msg);
  1903. xs_html_add(controls, xs_html_tag("div",
  1904. xs_html_tag("p", NULL),
  1905. html_note(user, L("Reply..."),
  1906. div_id, form_id,
  1907. "", ct,
  1908. NULL, NULL,
  1909. xs_dict_get(msg, "sensitive"), xs_dict_get(msg, "summary"),
  1910. scope, redir,
  1911. id, 0, NULL, NULL, 0, NULL, NULL)),
  1912. xs_html_tag("p", NULL));
  1913. }
  1914. return controls;
  1915. }
  1916. static const xs_str* words_in_content(const xs_list *words, const xs_val *content)
  1917. /* returns a word that matches any of the words in content */
  1918. {
  1919. if (!xs_is_list(words) || !xs_is_string(content)) {
  1920. return NULL;
  1921. }
  1922. xs *c = xs_split(content, " ");
  1923. xs *sc = xs_list_sort(c, NULL);
  1924. const xs_str *wv;
  1925. const xs_str *cv;
  1926. xs_list_foreach(words, wv) {
  1927. xs_list_foreach(sc, cv) {
  1928. xs_tolower_i((xs_str*)cv);
  1929. if(xs_str_in(cv, wv) != -1)
  1930. return wv;
  1931. }
  1932. }
  1933. return NULL;
  1934. }
  1935. xs_html *html_entry(snac *user, xs_dict *msg, int read_only,
  1936. int level, const char *md5, int hide_children)
  1937. {
  1938. const char *id = xs_dict_get(msg, "id");
  1939. const char *type = xs_dict_get(msg, "type");
  1940. const char *actor;
  1941. const char *v;
  1942. int has_title = 0;
  1943. int collapse_threads = 0;
  1944. const char *proxy = NULL;
  1945. if (user && !read_only && xs_is_true(xs_dict_get(srv_config, "proxy_media")))
  1946. proxy = user->actor;
  1947. /* do not show non-public messages in the public timeline */
  1948. if ((read_only || !user) && !is_msg_public(msg))
  1949. return NULL;
  1950. if (id && is_instance_blocked(id))
  1951. return NULL;
  1952. if (user && level == 0 && xs_is_true(xs_dict_get(user->config, "collapse_threads")))
  1953. collapse_threads = 1;
  1954. /* hidden? do nothing more for this conversation */
  1955. if (user && is_hidden(user, id)) {
  1956. xs *s1 = xs_fmt("%s_entry", md5);
  1957. /* return just an dummy anchor, to keep position after hitting 'Hide' */
  1958. return xs_html_tag("div",
  1959. xs_html_tag("a",
  1960. xs_html_attr("name", s1)));
  1961. }
  1962. /* avoid too deep nesting, as it may be a loop */
  1963. if (level >= MAX_CONVERSATION_LEVELS)
  1964. return xs_html_tag("mark",
  1965. xs_html_text(L("Truncated (too deep)")));
  1966. const char *lang = NULL;
  1967. const xs_dict *cmap = xs_dict_get(msg, "contentMap");
  1968. if (xs_is_dict(cmap)) {
  1969. const char *dummy;
  1970. int c = 0;
  1971. xs_dict_next(cmap, &lang, &dummy, &c);
  1972. }
  1973. if (strcmp(type, "Follow") == 0) {
  1974. return xs_html_tag("div",
  1975. xs_html_attr("class", "snac-post"),
  1976. xs_html_tag("div",
  1977. xs_html_attr("class", "snac-post-header"),
  1978. xs_html_tag("div",
  1979. xs_html_attr("class", "snac-origin"),
  1980. xs_html_text(L("follows you"))),
  1981. html_msg_icon(read_only ? NULL : user, xs_dict_get(msg, "actor"), msg, proxy, NULL, lang)));
  1982. }
  1983. else
  1984. if (!xs_match(type, POSTLIKE_OBJECT_TYPE)) {
  1985. /* skip oddities */
  1986. snac_debug(user, 1, xs_fmt("html_entry: ignoring object type '%s' %s", type, id));
  1987. return NULL;
  1988. }
  1989. /* ignore notes with "name", as they are votes to Questions */
  1990. if (strcmp(type, "Note") == 0 && !xs_is_null(xs_dict_get(msg, "name")))
  1991. return NULL;
  1992. /* get the attributedTo */
  1993. if ((actor = get_atto(msg)) == NULL)
  1994. return NULL;
  1995. /* ignore muted morons immediately */
  1996. if (user && is_muted(user, actor)) {
  1997. xs *s1 = xs_fmt("%s_entry", md5);
  1998. /* return just an dummy anchor, to keep position after hitting 'MUTE' */
  1999. return xs_html_tag("div",
  2000. xs_html_tag("a",
  2001. xs_html_attr("name", s1)));
  2002. }
  2003. /* don't show followers-only notes from not followed users */
  2004. if (user && get_msg_visibility(msg) == SCOPE_FOLLOWERS &&
  2005. strcmp(user->actor, actor) != 0 && following_check(user, actor) == 0) {
  2006. return NULL;
  2007. }
  2008. if ((user == NULL || strcmp(actor, user->actor) != 0)
  2009. && !valid_status(actor_get(actor, NULL))) {
  2010. if (user)
  2011. enqueue_actor_refresh(user, actor, 0);
  2012. return NULL;
  2013. }
  2014. /** html_entry top tag **/
  2015. xs_html *entry_top = xs_html_tag("div", NULL);
  2016. {
  2017. xs *s1 = xs_fmt("%s_entry", md5);
  2018. xs_html_add(entry_top,
  2019. xs_html_tag("a",
  2020. xs_html_attr("name", s1)));
  2021. }
  2022. xs_html *entry = xs_html_tag("div",
  2023. xs_html_attr("class", level == 0 ? "snac-post" : "snac-child"));
  2024. xs_html_add(entry_top,
  2025. entry);
  2026. /** post header **/
  2027. xs_html *score;
  2028. xs_html *post_header = xs_html_tag("div",
  2029. xs_html_attr("class", "snac-post-header"),
  2030. score = xs_html_tag("div",
  2031. xs_html_attr("class", "snac-score")));
  2032. xs_html_add(entry,
  2033. post_header);
  2034. if (user && is_pinned(user, id)) {
  2035. /* add a pin emoji */
  2036. xs_html_add(score,
  2037. xs_html_tag("span",
  2038. xs_html_attr("title", L("Pinned")),
  2039. xs_html_raw(" &#128204; ")));
  2040. }
  2041. if (user && !read_only && is_bookmarked(user, id)) {
  2042. /* add a bookmark emoji */
  2043. xs_html_add(score,
  2044. xs_html_tag("span",
  2045. xs_html_attr("title", L("Bookmarked")),
  2046. xs_html_raw(" &#128278; ")));
  2047. }
  2048. if (strcmp(type, "Question") == 0) {
  2049. /* add the ballot box emoji */
  2050. xs_html_add(score,
  2051. xs_html_tag("span",
  2052. xs_html_attr("title", L("Poll")),
  2053. xs_html_raw(" &#128499; ")));
  2054. if (user && was_question_voted(user, id)) {
  2055. /* add a check to show this poll was voted */
  2056. xs_html_add(score,
  2057. xs_html_tag("span",
  2058. xs_html_attr("title", L("Voted")),
  2059. xs_html_raw(" &#10003; ")));
  2060. }
  2061. }
  2062. if (strcmp(type, "Event") == 0) {
  2063. /* add the calendar emoji */
  2064. xs_html_add(score,
  2065. xs_html_tag("span",
  2066. xs_html_attr("title", L("Event")),
  2067. xs_html_raw(" &#128197; ")));
  2068. }
  2069. /* if it's a user from this same instance, add the score */
  2070. if (xs_startswith(id, srv_baseurl)) {
  2071. int n_likes = object_likes_len(id);
  2072. int n_boosts = object_announces_len(id);
  2073. /* alternate emojis: %d &#128077; %d &#128257; */
  2074. xs *s1 = xs_fmt("%d &#9733; %d &#8634;\n", n_likes, n_boosts);
  2075. xs_html_add(score,
  2076. xs_html_raw(s1));
  2077. }
  2078. xs *boosts = object_announces(id);
  2079. if (xs_list_len(boosts)) {
  2080. /* if somebody boosted this, show as origin */
  2081. const char *p = xs_list_get(boosts, -1);
  2082. xs *actor_r = NULL;
  2083. if (user && xs_list_in(boosts, user->md5) != -1) {
  2084. /* we boosted this */
  2085. xs_html_add(post_header,
  2086. xs_html_tag("div",
  2087. xs_html_attr("class", "snac-origin"),
  2088. xs_html_tag("a",
  2089. xs_html_attr("href", user->actor),
  2090. xs_html_text(xs_dict_get(user->config, "name"))),
  2091. xs_html_text(" "),
  2092. xs_html_text(L("boosted"))));
  2093. }
  2094. else
  2095. if (valid_status(object_get_by_md5(p, &actor_r))) {
  2096. xs *name = actor_name(actor_r, proxy);
  2097. if (!xs_is_null(name)) {
  2098. xs *href = NULL;
  2099. const char *id = xs_dict_get(actor_r, "id");
  2100. int fwers = 0;
  2101. int fwing = 0;
  2102. if (user != NULL) {
  2103. fwers = follower_check(user, id);
  2104. fwing = following_check(user, id);
  2105. }
  2106. if (!read_only && (fwers || fwing))
  2107. href = xs_fmt("%s/people/%s", user->actor, p);
  2108. else
  2109. href = xs_dup(id);
  2110. xs_html_add(post_header,
  2111. xs_html_tag("div",
  2112. xs_html_attr("class", "snac-origin"),
  2113. xs_html_tag("a",
  2114. xs_html_attr("href", href),
  2115. xs_html_raw(name)), /* already sanitized */
  2116. xs_html_text(" "),
  2117. xs_html_text(L("boosted"))));
  2118. }
  2119. }
  2120. }
  2121. if (user && strcmp(type, "Note") == 0) {
  2122. /* is the parent not here? */
  2123. const char *parent = get_in_reply_to(msg);
  2124. if (!xs_is_null(parent) && *parent) {
  2125. if (!timeline_here(user, parent)) {
  2126. xs_html_add(post_header,
  2127. xs_html_tag("div",
  2128. xs_html_attr("class", "snac-origin"),
  2129. xs_html_text(L("in reply to")),
  2130. xs_html_text(" "),
  2131. xs_html_tag("a",
  2132. xs_html_attr("href", parent),
  2133. xs_html_text("»"))));
  2134. }
  2135. }
  2136. }
  2137. xs_html_add(post_header,
  2138. html_msg_icon(read_only ? NULL : user, actor, msg, proxy, md5, lang));
  2139. /** post content **/
  2140. xs_html *snac_content_wrap = xs_html_tag("div",
  2141. xs_html_attr("class", "e-content snac-content"));
  2142. if (xs_is_string(lang))
  2143. xs_html_add(snac_content_wrap,
  2144. xs_html_attr("lang", lang));
  2145. xs_html_add(entry,
  2146. snac_content_wrap);
  2147. if (!has_title && !xs_is_null(v = xs_dict_get(msg, "name"))) {
  2148. xs_html_add(snac_content_wrap,
  2149. xs_html_tag("h3",
  2150. xs_html_attr("class", "snac-entry-title"),
  2151. xs_html_text(v)));
  2152. has_title = 1;
  2153. }
  2154. xs_html *snac_content = NULL;
  2155. v = xs_dict_get(msg, "summary");
  2156. /* if it has summary or marked as sensitive - add CW */
  2157. /* come clients don't set "sensitive" flag properly */
  2158. if ((!xs_is_null(v) && *v) || xs_type(xs_dict_get(msg, "sensitive")) == XSTYPE_TRUE) {
  2159. if (xs_is_null(v) || *v == '\0')
  2160. v = "...";
  2161. const char *cw = "";
  2162. if (user) {
  2163. /* only show it when not in the public timeline and the config setting is "open" */
  2164. cw = xs_dict_get(user->config, "cw");
  2165. if (xs_is_null(cw) || read_only)
  2166. cw = "";
  2167. }
  2168. /* summary can't contain HTML */
  2169. xs *s1 = xs_regex_replace(v, "<[^>]+>", "");
  2170. /* crop stupidly long summaries */
  2171. if (xs_utf8_len(s1) > 1024) {
  2172. s1 = xs_utf8_crop_i(s1, 0, 1024);
  2173. s1 = xs_str_cat(s1, "...");
  2174. }
  2175. snac_content = xs_html_tag("details",
  2176. xs_html_attr(cw, NULL),
  2177. xs_html_tag("summary",
  2178. xs_html_text(s1),
  2179. xs_html_text(L(" [SENSITIVE CONTENT]"))));
  2180. }
  2181. else
  2182. if (user &&
  2183. /* muted_words is all lowercase and sorted for performance */
  2184. (v = words_in_content(xs_dict_get(user->config, "muted_words"),
  2185. xs_dict_get(msg, "content"))) != NULL) {
  2186. snac_debug(user, 1, xs_fmt("word %s muted by user preferences: %s", v, id));
  2187. snac_content = xs_html_tag("details",
  2188. xs_html_tag("summary",
  2189. xs_html_text(L("Muted: ")),
  2190. xs_html_text(v)));
  2191. }
  2192. else {
  2193. snac_content = xs_html_tag("div", NULL);
  2194. }
  2195. xs_html_add(snac_content_wrap,
  2196. snac_content);
  2197. /* add all emoji reacts */
  2198. int is_emoji = 0;
  2199. if (!xs_is_true(xs_dict_get(srv_config, "disable_emojireact"))) {
  2200. int c = 0;
  2201. const xs_dict *k;
  2202. xs *ls = xs_list_new();
  2203. xs *sfrl = xs_dict_new();
  2204. xs *rl = object_get_emoji_reacts(id);
  2205. while (xs_list_next(rl, &v, &c)) {
  2206. xs *m = NULL;
  2207. if (valid_status(object_get_by_md5(v, &m))) {
  2208. const char *content = xs_dict_get(m, "content");
  2209. const char *actor = xs_dict_get(m, "actor");
  2210. const xs_list *contentl = xs_dict_get(sfrl, content);
  2211. if ((user && is_muted(user, actor)) || is_instance_blocked(actor))
  2212. continue;
  2213. xs *actors = xs_list_new();
  2214. actors = xs_list_append(actors, actor);
  2215. char me = actor && user && strcmp(actor, user->actor) == 0;
  2216. int count = 1;
  2217. if (contentl) {
  2218. count = atoi(xs_list_get(contentl, 0)) + 1;
  2219. const xs_list *actorsc = xs_list_get(contentl, 1);
  2220. if (strncmp(xs_list_get(contentl, 2), "1", 1) == 0)
  2221. me = 1;
  2222. if (xs_list_in(actorsc, actor) != -1) {
  2223. xs_free(actors);
  2224. actors = xs_dup(actorsc);
  2225. }
  2226. else
  2227. actors = xs_list_cat(actors, actorsc);
  2228. }
  2229. xs *fl = xs_list_new();
  2230. xs *c1 = xs_fmt("%d", count);
  2231. xs *c2 = xs_fmt("%d", me);
  2232. fl = xs_list_append(fl, c1, actors, c2);
  2233. sfrl = xs_dict_append(sfrl, content, fl);
  2234. }
  2235. }
  2236. c = 0;
  2237. while (xs_list_next(rl, &k, &c)) {
  2238. xs *m = NULL;
  2239. if (valid_status(object_get_by_md5(k, &m))) {
  2240. const xs_dict *tag = xs_dict_get(m, "tag");
  2241. const xs_dict *ide = xs_dict_get(m, "id");
  2242. const char *content = xs_dict_get(m, "content");
  2243. const char *shortname;
  2244. shortname = xs_dict_get(m, "content");
  2245. const xs_list *items = xs_dict_get(sfrl, content);
  2246. if (!xs_is_null(items)) {
  2247. const char *nb = xs_list_get(items, 0);
  2248. const xs_list *actors = xs_list_get(items, 1);
  2249. const char me = *xs_list_get(items, 2) == '1';
  2250. is_emoji = 1;
  2251. xs *al = xs_join(actors, ",\n\t");
  2252. xs *act = atoi(nb) > 1 ?
  2253. xs_fmt("%d different actors \n\t%s", atoi(nb), al) :
  2254. xs_dup(xs_dict_get(m, "actor"));
  2255. xs *class = xs_list_new();
  2256. class = xs_list_append(class, "snac-reaction");
  2257. xs_html *ret = NULL;
  2258. if (tag && shortname) {
  2259. xs *cl = xs_list_new();
  2260. cl = xs_list_append(cl, "snac-reaction-image");
  2261. xs *emoji = _replace_shortnames(xs_dup(shortname), tag, 2, proxy, cl, act);
  2262. emoji = xs_strip_chars_i(emoji, ":");
  2263. if (me)
  2264. class = xs_list_append(class, "snac-reacted");
  2265. xs *l1 = xs_join(class, " ");
  2266. ret = xs_html_tag("button",
  2267. xs_html_attr("type", "submit"),
  2268. xs_html_attr("name", "action"),
  2269. xs_html_attr("value", me ? L("EmojiReact") : L("EmojiUnreact")),
  2270. xs_html_raw(emoji),
  2271. xs_html_tag("span",
  2272. xs_html_raw(nb),
  2273. xs_html_attr("style", "padding-left: 5px;")),
  2274. xs_html_attr("title", act),
  2275. xs_html_attr("class", l1));
  2276. if (!(ide && xs_startswith(ide, srv_baseurl)))
  2277. xs_html_add(ret, xs_html_attr("disabled", "true"));
  2278. }
  2279. else if (shortname) {
  2280. xs *sn = xs_dup(shortname);
  2281. const char *sna = sn;
  2282. unsigned int utf = xs_utf8_dec((const char **)&sna);
  2283. if (xs_is_emoji(utf)) {
  2284. const char *style = "font-size: large;";
  2285. if (me)
  2286. class = xs_list_append(class, "snac-reacted");
  2287. xs *l1 = xs_join(class, " ");
  2288. xs *s1 = xs_fmt("&#%d", utf);
  2289. ret = xs_html_tag("button",
  2290. xs_html_attr("type", "submit"),
  2291. xs_html_attr("name", "action"),
  2292. xs_html_attr("value", me ? L("EmojiUnreact") : L("EmojiReact")),
  2293. xs_html_raw(s1),
  2294. xs_html_tag("span",
  2295. xs_html_raw(nb),
  2296. xs_html_attr("style", "font-size: initial; padding-left: 5px;")),
  2297. xs_html_attr("title", act),
  2298. xs_html_attr("class", l1),
  2299. xs_html_attr("style", style));
  2300. }
  2301. }
  2302. if (ret) {
  2303. xs *s1;
  2304. if (user) {
  2305. xs *action = xs_fmt("%s/admin/action", user->actor);
  2306. xs *form_id = xs_fmt("%s_reply_form", md5);
  2307. xs_html *form =
  2308. xs_html_tag("form",
  2309. xs_html_attr("autocomplete", "off"),
  2310. xs_html_attr("method", "post"),
  2311. xs_html_attr("action", action),
  2312. xs_html_attr("enctype", "multipart/form-data"),
  2313. xs_html_attr("style", "display: inline-flex;"
  2314. "vertical-align: middle;"),
  2315. xs_html_attr("id", form_id),
  2316. xs_html_sctag("input",
  2317. xs_html_attr("type", "hidden"),
  2318. xs_html_attr("name", "id"),
  2319. xs_html_attr("value", id)),
  2320. xs_html_sctag("input",
  2321. xs_html_attr("type", "hidden"),
  2322. xs_html_attr("name", "eid"),
  2323. xs_html_attr("value", shortname)),
  2324. ret);
  2325. s1 = xs_html_render(form);
  2326. }
  2327. else
  2328. s1 = xs_html_render(ret);
  2329. ls = xs_list_append(ls, s1);
  2330. sfrl = xs_dict_del(sfrl, content);
  2331. }
  2332. }
  2333. }
  2334. }
  2335. c = 0;
  2336. xs_html *emoji_div;
  2337. if (xs_list_len(ls) > 0) {
  2338. emoji_div = xs_html_tag("div", xs_html_text(L("Emoji reactions: ")),
  2339. xs_html_attr("class", "snac-reaction-div"));
  2340. while (ls != NULL && xs_list_next(ls, &k, &c))
  2341. xs_html_add(emoji_div, xs_html_raw(k));
  2342. xs_html_add(snac_content_wrap, emoji_div);
  2343. }
  2344. }
  2345. {
  2346. /** build the content string **/
  2347. const char *content = xs_dict_get(msg, "content");
  2348. if (xs_type(content) != XSTYPE_STRING) {
  2349. if (!xs_is_null(content))
  2350. srv_archive_error("unexpected_content_xstype",
  2351. "content field type", xs_stock(XSTYPE_DICT), msg);
  2352. content = "";
  2353. }
  2354. /* skip ugly line breaks at the beginning */
  2355. while (xs_startswith(content, "<br>"))
  2356. content += 4;
  2357. xs *c = sanitize(content);
  2358. /* do some tweaks to the content */
  2359. c = xs_replace_i(c, "\r", "");
  2360. while (xs_endswith(c, "<br><br>"))
  2361. c = xs_crop_i(c, 0, -4);
  2362. c = xs_replace_i(c, "<br><br>", "<p>");
  2363. if (is_emoji == 0)
  2364. c = xs_str_cat(c, "<p>");
  2365. /* replace the :shortnames: */
  2366. c = replace_shortnames(c, xs_dict_get(msg, "tag"), 2, proxy);
  2367. /* Peertube videos content is in markdown */
  2368. const char *mtype = xs_dict_get(msg, "mediaType");
  2369. if (xs_type(mtype) == XSTYPE_STRING && strcmp(mtype, "text/markdown") == 0) {
  2370. /* a full conversion could be better */
  2371. c = xs_replace_i(c, "\r", "");
  2372. c = xs_replace_i(c, "\n", "<br>");
  2373. }
  2374. /* c contains sanitized HTML */
  2375. xs_html_add(snac_content,
  2376. xs_html_raw(c));
  2377. /* quoted post */
  2378. const char *quoted_id = xs_or(xs_dict_get(msg, "quoteUri"), xs_dict_get(msg, "quoteUrl"));
  2379. if (level < 3 && xs_is_string(quoted_id) && xs_match(quoted_id, "https://*|http://*")) { /** **/
  2380. xs *quoted_post = NULL;
  2381. if (valid_status(object_get(quoted_id, &quoted_post))) {
  2382. xs *md5 = xs_md5_hex(quoted_id, strlen(quoted_id));
  2383. xs_html_add(snac_content,
  2384. xs_html_tag("blockquote",
  2385. xs_html_attr("class", "snac-quoted-post"),
  2386. html_entry(user, quoted_post, 1, level + 1, md5, 1)));
  2387. }
  2388. else
  2389. if (user)
  2390. enqueue_object_request(user, quoted_id, 0);
  2391. }
  2392. }
  2393. if (strcmp(type, "Question") == 0) { /** question content **/
  2394. const xs_list *oo = xs_dict_get(msg, "oneOf");
  2395. const xs_list *ao = xs_dict_get(msg, "anyOf");
  2396. const xs_list *p;
  2397. const xs_dict *v;
  2398. int closed = 0;
  2399. const char *f_closed = NULL;
  2400. xs_html *poll = xs_html_tag("div", NULL);
  2401. if (read_only)
  2402. closed = 1; /* non-identified page; show as closed */
  2403. else
  2404. if (user && is_msg_mine(user, id))
  2405. closed = 1; /* we questioned; closed for us */
  2406. else
  2407. if (user && was_question_voted(user, id))
  2408. closed = 1; /* we already voted; closed for us */
  2409. if ((f_closed = xs_dict_get(msg, "closed")) != NULL) {
  2410. /* it has a closed date... but is it in the past? */
  2411. time_t t0 = time(NULL);
  2412. time_t t1 = xs_parse_iso_date(f_closed, 0);
  2413. if (t1 < t0)
  2414. closed = 2;
  2415. }
  2416. /* get the appropriate list of options */
  2417. p = oo != NULL ? oo : ao;
  2418. if (closed || user == NULL) {
  2419. /* closed poll */
  2420. xs_html *poll_result = xs_html_tag("table",
  2421. xs_html_attr("class", "snac-poll-result"));
  2422. int c = 0;
  2423. while (xs_list_next(p, &v, &c)) {
  2424. const char *name = xs_dict_get(v, "name");
  2425. const xs_dict *replies = xs_dict_get(v, "replies");
  2426. if (xs_is_string(name) && xs_is_dict(replies)) {
  2427. const char *ti = xs_number_str(xs_dict_get(replies, "totalItems"));
  2428. if (xs_is_string(ti))
  2429. xs_html_add(poll_result,
  2430. xs_html_tag("tr",
  2431. xs_html_tag("td",
  2432. xs_html_text(name),
  2433. xs_html_text(":")),
  2434. xs_html_tag("td",
  2435. xs_html_text(ti))));
  2436. }
  2437. }
  2438. xs_html_add(poll,
  2439. poll_result);
  2440. }
  2441. else {
  2442. /* poll still active */
  2443. xs *vote_action = xs_fmt("%s/admin/vote", user->actor);
  2444. xs_html *form;
  2445. xs_html *poll_form = xs_html_tag("div",
  2446. xs_html_attr("class", "snac-poll-form"),
  2447. form = xs_html_tag("form",
  2448. xs_html_attr("autocomplete", "off"),
  2449. xs_html_attr("method", "post"),
  2450. xs_html_attr("action", vote_action),
  2451. xs_html_sctag("input",
  2452. xs_html_attr("type", "hidden"),
  2453. xs_html_attr("name", "actor"),
  2454. xs_html_attr("value", actor)),
  2455. xs_html_sctag("input",
  2456. xs_html_attr("type", "hidden"),
  2457. xs_html_attr("name", "irt"),
  2458. xs_html_attr("value", id))));
  2459. int c = 0;
  2460. while (xs_list_next(p, &v, &c)) {
  2461. const char *name = xs_dict_get(v, "name");
  2462. const xs_dict *replies = xs_dict_get(v, "replies");
  2463. if (name) {
  2464. char *ti = (char *)xs_number_str(xs_dict_get(replies, "totalItems"));
  2465. xs_html *btn = xs_html_sctag("input",
  2466. xs_html_attr("id", name),
  2467. xs_html_attr("value", name),
  2468. xs_html_attr("name", "question"));
  2469. if (!xs_is_null(oo)) {
  2470. xs_html_add(btn,
  2471. xs_html_attr("type", "radio"),
  2472. xs_html_attr("required", "required"));
  2473. }
  2474. else
  2475. xs_html_add(btn,
  2476. xs_html_attr("type", "checkbox"));
  2477. xs_html_add(form,
  2478. btn,
  2479. xs_html_text(" "),
  2480. xs_html_tag("span",
  2481. xs_html_attr("title", ti),
  2482. xs_html_text(name)),
  2483. xs_html_sctag("br", NULL));
  2484. }
  2485. }
  2486. xs_html_add(form,
  2487. xs_html_tag("p", NULL),
  2488. xs_html_sctag("input",
  2489. xs_html_attr("type", "submit"),
  2490. xs_html_attr("class", "button"),
  2491. xs_html_attr("value", L("Vote"))));
  2492. xs_html_add(poll,
  2493. poll_form);
  2494. }
  2495. /* if it's *really* closed, say it */
  2496. if (closed == 2) {
  2497. xs_html_add(poll,
  2498. xs_html_tag("p",
  2499. xs_html_text(L("Closed"))));
  2500. }
  2501. else {
  2502. /* show when the poll closes */
  2503. const char *end_time = xs_dict_get(msg, "endTime");
  2504. /* Pleroma does not have an endTime field;
  2505. it has a closed time in the future */
  2506. if (xs_is_null(end_time))
  2507. end_time = xs_dict_get(msg, "closed");
  2508. if (!xs_is_null(end_time)) {
  2509. time_t t0 = time(NULL);
  2510. time_t t1 = xs_parse_iso_date(end_time, 0);
  2511. if (t1 > 0 && t1 > t0) {
  2512. time_t diff_time = t1 - t0;
  2513. xs *tf = xs_str_time_diff(diff_time);
  2514. char *p = tf;
  2515. /* skip leading zeros */
  2516. for (; *p == '0' || *p == ':'; p++);
  2517. xs_html_add(poll,
  2518. xs_html_tag("p",
  2519. xs_html_text(L("Closes in")),
  2520. xs_html_text(" "),
  2521. xs_html_text(p)));
  2522. }
  2523. }
  2524. }
  2525. xs_html_add(snac_content,
  2526. poll);
  2527. }
  2528. /** attachments **/
  2529. xs *attach = get_attachments(msg);
  2530. {
  2531. /* make custom css for attachments easier */
  2532. xs_html *content_attachments = xs_html_tag("div",
  2533. xs_html_attr("class", "snac-content-attachments"));
  2534. xs_html_add(snac_content,
  2535. content_attachments);
  2536. const char *content = xs_dict_get(msg, "content");
  2537. int c = 0;
  2538. const xs_dict *a;
  2539. while (xs_list_next(attach, &a, &c)) {
  2540. const char *type = xs_dict_get(a, "type");
  2541. const char *o_href = xs_dict_get(a, "href");
  2542. const char *name = xs_dict_get(a, "name");
  2543. if (!xs_is_string(type) || !xs_is_string(o_href))
  2544. continue;
  2545. /* if this URL is already in the post content, skip */
  2546. if (content && xs_str_in(content, o_href) != -1)
  2547. continue;
  2548. if (strcmp(type, "image/svg+xml") == 0 && !xs_is_true(xs_dict_get(srv_config, "enable_svg")))
  2549. continue;
  2550. /* do this attachment include an icon? */
  2551. const xs_dict *icon = xs_dict_get(a, "icon");
  2552. if (xs_type(icon) == XSTYPE_DICT) {
  2553. const char *icon_mtype = xs_dict_get(icon, "mediaType");
  2554. const char *icon_url = xs_dict_get(icon, "url");
  2555. if (icon_mtype && icon_url && xs_startswith(icon_mtype, "image/")) {
  2556. xs_html_add(content_attachments,
  2557. xs_html_tag("a",
  2558. xs_html_attr("href", icon_url),
  2559. xs_html_attr("target", "_blank"),
  2560. xs_html_sctag("img",
  2561. xs_html_attr("loading", "lazy"),
  2562. xs_html_attr("src", icon_url))));
  2563. }
  2564. }
  2565. xs *href = make_url(o_href, proxy, 0);
  2566. if (xs_startswith(type, "image/") || strcmp(type, "Image") == 0) {
  2567. xs_html_add(content_attachments,
  2568. xs_html_tag("a",
  2569. xs_html_attr("href", href),
  2570. xs_html_attr("target", "_blank"),
  2571. xs_html_sctag("img",
  2572. xs_html_attr("loading", "lazy"),
  2573. xs_html_attr("src", href),
  2574. xs_html_attr("alt", name),
  2575. xs_html_attr("title", name))));
  2576. }
  2577. else
  2578. if (xs_startswith(type, "video/") || strcmp(type, "Video") == 0) {
  2579. xs_html_add(content_attachments,
  2580. xs_html_tag("video",
  2581. xs_html_attr("preload", "none"),
  2582. xs_html_attr("style", "width: 100%"),
  2583. xs_html_attr("class", "snac-embedded-video"),
  2584. xs_html_attr("controls", NULL),
  2585. xs_html_attr("src", href),
  2586. xs_html_text(L("Video")),
  2587. xs_html_text(": "),
  2588. xs_html_tag("a",
  2589. xs_html_attr("href", href),
  2590. xs_html_text(name))));
  2591. }
  2592. else
  2593. if (xs_startswith(type, "audio/")) {
  2594. xs_html_add(content_attachments,
  2595. xs_html_tag("audio",
  2596. xs_html_attr("preload", "none"),
  2597. xs_html_attr("style", "width: 100%"),
  2598. xs_html_attr("class", "snac-embedded-audio"),
  2599. xs_html_attr("controls", NULL),
  2600. xs_html_attr("src", href),
  2601. xs_html_text(L("Audio")),
  2602. xs_html_text(": "),
  2603. xs_html_tag("a",
  2604. xs_html_attr("href", href),
  2605. xs_html_text(name))));
  2606. }
  2607. else
  2608. if (strcmp(type, "Link") == 0) {
  2609. xs_html_add(content_attachments,
  2610. xs_html_tag("p",
  2611. xs_html_tag("a",
  2612. xs_html_attr("href", o_href),
  2613. xs_html_text(href))));
  2614. /* do not generate an Alt... */
  2615. name = NULL;
  2616. }
  2617. else {
  2618. xs *d_href = xs_dup(o_href);
  2619. if (strlen(d_href) > 64) {
  2620. d_href[64] = '\0';
  2621. d_href = xs_str_cat(d_href, "...");
  2622. }
  2623. xs_html_add(content_attachments,
  2624. xs_html_tag("p",
  2625. xs_html_tag("a",
  2626. xs_html_attr("href", o_href),
  2627. xs_html_text(L("Attachment")),
  2628. xs_html_text(": "),
  2629. xs_html_text(d_href))));
  2630. /* do not generate an Alt... */
  2631. name = NULL;
  2632. }
  2633. if (name != NULL && *name) {
  2634. xs_html_add(content_attachments,
  2635. xs_html_tag("p",
  2636. xs_html_attr("class", "snac-alt-text"),
  2637. xs_html_tag("details",
  2638. xs_html_tag("summary",
  2639. xs_html_text(L("Alt..."))),
  2640. xs_html_text(name))));
  2641. }
  2642. }
  2643. }
  2644. /* has this message an audience (i.e., comes from a channel or community)? */
  2645. const char *audience = xs_dict_get(msg, "audience");
  2646. if (strcmp(type, "Page") == 0 && !xs_is_null(audience)) {
  2647. xs_html *au_tag = xs_html_tag("p",
  2648. xs_html_text("("),
  2649. xs_html_tag("a",
  2650. xs_html_attr("href", audience),
  2651. xs_html_attr("title", L("Source channel or community")),
  2652. xs_html_text(audience)),
  2653. xs_html_text(")"));
  2654. xs_html_add(snac_content_wrap,
  2655. au_tag);
  2656. }
  2657. /* does it have a location? */
  2658. const xs_dict *location = xs_dict_get(msg, "location");
  2659. if (xs_type(location) == XSTYPE_DICT) {
  2660. const xs_number *latitude = xs_dict_get(location, "latitude");
  2661. const xs_number *longitude = xs_dict_get(location, "longitude");
  2662. const char *name = xs_dict_get(location, "name");
  2663. const char *address = xs_dict_get(location, "address");
  2664. xs *label_list = xs_list_new();
  2665. if (xs_type(name) == XSTYPE_STRING)
  2666. label_list = xs_list_append(label_list, name);
  2667. if (xs_type(address) == XSTYPE_STRING)
  2668. label_list = xs_list_append(label_list, address);
  2669. if (xs_list_len(label_list)) {
  2670. const char *url = xs_dict_get(location, "url");
  2671. xs *label = xs_join(label_list, ", ");
  2672. if (xs_type(url) == XSTYPE_STRING) {
  2673. xs_html_add(snac_content_wrap,
  2674. xs_html_tag("p",
  2675. xs_html_text(L("Location: ")),
  2676. xs_html_tag("a",
  2677. xs_html_attr("href", url),
  2678. xs_html_attr("target", "_blank"),
  2679. xs_html_text(label))));
  2680. }
  2681. else
  2682. if (!xs_is_null(latitude) && !xs_is_null(longitude)) {
  2683. xs *url = xs_fmt("https://openstreetmap.org/search/?query=%s,%s",
  2684. xs_or(xs_number_str(latitude), latitude), xs_or(xs_number_str(longitude), longitude));
  2685. xs_html_add(snac_content_wrap,
  2686. xs_html_tag("p",
  2687. xs_html_text(L("Location: ")),
  2688. xs_html_tag("a",
  2689. xs_html_attr("href", url),
  2690. xs_html_attr("target", "_blank"),
  2691. xs_html_text(label))));
  2692. }
  2693. else
  2694. xs_html_add(snac_content_wrap,
  2695. xs_html_tag("p",
  2696. xs_html_text(L("Location: ")),
  2697. xs_html_text(label)));
  2698. }
  2699. }
  2700. if (strcmp(type, "Event") == 0) { /** Event start and end times **/
  2701. const char *s_time = xs_dict_get(msg, "startTime");
  2702. if (xs_is_string(s_time) && strlen(s_time) > 20) {
  2703. const char *e_time = xs_dict_get(msg, "endTime");
  2704. const char *tz = xs_dict_get(msg, "timezone");
  2705. xs *s = xs_replace_i(xs_dup(s_time), "T", " ");
  2706. xs *e = NULL;
  2707. if (xs_is_string(e_time) && strlen(e_time) > 20)
  2708. e = xs_replace_i(xs_dup(e_time), "T", " ");
  2709. /* if the event has a timezone, crop the offsets */
  2710. if (xs_is_string(tz)) {
  2711. s = xs_crop_i(s, 0, 19);
  2712. if (e)
  2713. e = xs_crop_i(e, 0, 19);
  2714. }
  2715. else
  2716. tz = "";
  2717. /* if start and end share the same day, crop it from the end */
  2718. if (e && memcmp(s, e, 11) == 0)
  2719. e = xs_crop_i(e, 11, 0);
  2720. if (e)
  2721. s = xs_str_cat(s, " / ", e);
  2722. if (*tz)
  2723. s = xs_str_cat(s, " (", tz, ")");
  2724. /* replace ugly decimals */
  2725. s = xs_replace_i(s, ".000", "");
  2726. xs_html_add(snac_content_wrap,
  2727. xs_html_tag("p",
  2728. xs_html_text(L("Time: ")),
  2729. xs_html_text(s)));
  2730. }
  2731. }
  2732. /* show all hashtags that has not been shown previously in the content */
  2733. const xs_list *tags = xs_dict_get(msg, "tag");
  2734. const char *o_content = xs_dict_get_def(msg, "content", "");
  2735. if (xs_is_string(o_content) && xs_is_list(tags) && xs_list_len(tags)) {
  2736. xs *content = xs_utf8_to_lower(o_content);
  2737. const xs_dict *tag;
  2738. xs_html *add_hashtags = xs_html_tag("ul",
  2739. xs_html_attr("class", "snac-more-hashtags"));
  2740. xs_list_foreach(tags, tag) {
  2741. const char *type = xs_dict_get(tag, "type");
  2742. if (xs_is_string(type) && strcmp(type, "Hashtag") == 0) {
  2743. const char *o_href = xs_dict_get(tag, "href");
  2744. const char *name = xs_dict_get(tag, "name");
  2745. if (xs_is_string(o_href) && xs_is_string(name)) {
  2746. xs *href = xs_utf8_to_lower(o_href);
  2747. if (xs_str_in(content, href) == -1 && xs_str_in(content, name) == -1) {
  2748. /* not in the content: add here */
  2749. xs_html_add(add_hashtags,
  2750. xs_html_tag("li",
  2751. xs_html_tag("a",
  2752. xs_html_attr("href", href),
  2753. xs_html_text(name),
  2754. xs_html_text(" "))));
  2755. }
  2756. }
  2757. }
  2758. }
  2759. xs_html_add(snac_content_wrap,
  2760. add_hashtags);
  2761. }
  2762. /** controls **/
  2763. if (!read_only && user) {
  2764. xs_html_add(entry,
  2765. html_entry_controls(user, actor, msg, md5));
  2766. }
  2767. /** children **/
  2768. if (!hide_children) {
  2769. xs *children = object_children(id);
  2770. int left = xs_list_len(children);
  2771. if (left) {
  2772. xs_html *ch_details = xs_html_tag("details",
  2773. xs_html_attr(collapse_threads ? "" : "open", NULL),
  2774. xs_html_tag("summary",
  2775. xs_html_text("...")));
  2776. xs_html_add(entry,
  2777. ch_details);
  2778. xs_html *fch_container = xs_html_tag("div",
  2779. xs_html_attr("class", "snac-thread-cont"));
  2780. xs_html_add(ch_details,
  2781. fch_container);
  2782. xs_html *ch_container = xs_html_tag("div",
  2783. xs_html_attr("class", level < 4 ? "snac-children" : "snac-children-too-deep"));
  2784. xs_html_add(ch_details,
  2785. ch_container);
  2786. xs_html *ch_older = NULL;
  2787. if (left > 3) {
  2788. xs_html_add(ch_container,
  2789. ch_older = xs_html_tag("details",
  2790. xs_html_tag("summary",
  2791. xs_html_text(L("Older...")))));
  2792. }
  2793. int ctxt = 0;
  2794. const char *cmd5;
  2795. int cnt = 0;
  2796. int o_cnt = 0;
  2797. int f_cnt = 0;
  2798. /* get the first child */
  2799. xs_list_next(children, &cmd5, &ctxt);
  2800. xs *f_chd = NULL;
  2801. if (user)
  2802. timeline_get_by_md5(user, cmd5, &f_chd);
  2803. else
  2804. object_get_by_md5(cmd5, &f_chd);
  2805. if (f_chd != NULL && xs_is_null(xs_dict_get(f_chd, "name"))) {
  2806. const char *p_author = get_atto(msg);
  2807. const char *author = get_atto(f_chd);
  2808. /* is the first child from the same author? */
  2809. if (xs_is_string(p_author) && xs_is_string(author) && strcmp(p_author, author) == 0) {
  2810. /* then, don't add it to the children container,
  2811. so that it appears unindented just before the parent
  2812. like a fucking Twitter-like thread */
  2813. xs_html_add(fch_container,
  2814. html_entry(user, f_chd, read_only, level + 1, cmd5, hide_children));
  2815. cnt++;
  2816. f_cnt++;
  2817. left--;
  2818. }
  2819. else
  2820. ctxt = 0; /* restart from the beginning */
  2821. }
  2822. while (xs_list_next(children, &cmd5, &ctxt)) {
  2823. xs *chd = NULL;
  2824. if (user)
  2825. timeline_get_by_md5(user, cmd5, &chd);
  2826. else
  2827. object_get_by_md5(cmd5, &chd);
  2828. if (chd != NULL) {
  2829. if (xs_is_null(xs_dict_get(chd, "name"))) {
  2830. xs_html *che = html_entry(user, chd, read_only,
  2831. level + 1, cmd5, hide_children);
  2832. if (che != NULL) {
  2833. if (left > 3) {
  2834. xs_html_add(ch_older,
  2835. che);
  2836. o_cnt++;
  2837. }
  2838. else
  2839. xs_html_add(ch_container,
  2840. che);
  2841. cnt++;
  2842. }
  2843. }
  2844. left--;
  2845. }
  2846. else
  2847. srv_debug(2, xs_fmt("cannot read child %s", cmd5));
  2848. }
  2849. /* if no children were finally added, hide the details */
  2850. if (cnt == 0)
  2851. xs_html_add(ch_details,
  2852. xs_html_attr("style", "display: none"));
  2853. if (o_cnt == 0 && ch_older)
  2854. xs_html_add(ch_older,
  2855. xs_html_attr("style", "display: none"));
  2856. if (f_cnt == 0)
  2857. xs_html_add(fch_container,
  2858. xs_html_attr("style", "display: none"));
  2859. }
  2860. }
  2861. /* add an invisible hr, to help differentiate between posts in text browsers */
  2862. xs_html_add(entry_top,
  2863. xs_html_sctag("hr",
  2864. xs_html_attr("hidden", NULL)));
  2865. return entry_top;
  2866. }
  2867. xs_html *html_footer(const snac *user)
  2868. {
  2869. return xs_html_tag("div",
  2870. xs_html_attr("class", "snac-footer"),
  2871. xs_html_tag("a",
  2872. xs_html_attr("href", srv_baseurl),
  2873. xs_html_text(L("about this site"))),
  2874. xs_html_text(" - "),
  2875. xs_html_text(L("powered by ")),
  2876. xs_html_tag("a",
  2877. xs_html_attr("href", WHAT_IS_SNAC_URL),
  2878. xs_html_tag("abbr",
  2879. xs_html_attr("title", "Social Networks Are Crap"),
  2880. xs_html_text(USER_AGENT))));
  2881. }
  2882. xs_str *html_timeline(snac *user, const xs_list *list, int read_only,
  2883. int skip, int show, int show_more,
  2884. const char *title, const char *page,
  2885. int utl, const char *error, int terse)
  2886. /* returns the HTML for the timeline */
  2887. {
  2888. xs_list *p = (xs_list *)list;
  2889. const char *v;
  2890. double t = ftime();
  2891. int hide_children = xs_is_true(xs_dict_get(srv_config, "strict_public_timelines")) && read_only;
  2892. xs *desc = NULL;
  2893. xs *alternate = NULL;
  2894. if (xs_list_len(list) == 1) {
  2895. /* only one element? pick the description from the source */
  2896. const char *id = xs_list_get(list, 0);
  2897. xs *d = NULL;
  2898. object_get_by_md5(id, &d);
  2899. const char *sc = xs_dict_get(d, "sourceContent");
  2900. if (d && sc != NULL)
  2901. desc = xs_dup(sc);
  2902. alternate = xs_dup(xs_dict_get(d, "id"));
  2903. }
  2904. xs_html *head;
  2905. xs_html *body;
  2906. if (user) {
  2907. head = html_user_head(user, desc, alternate);
  2908. if (terse)
  2909. body = xs_html_tag("body", NULL);
  2910. else
  2911. body = html_user_body(user, read_only);
  2912. }
  2913. else {
  2914. head = html_instance_head();
  2915. body = html_instance_body();
  2916. }
  2917. xs_html *html = xs_html_tag("html",
  2918. head,
  2919. body);
  2920. if (user && !read_only)
  2921. xs_html_add(body,
  2922. html_top_controls(user));
  2923. if (error != NULL) {
  2924. xs_html_add(body,
  2925. xs_html_tag("dialog",
  2926. xs_html_attr("open", NULL),
  2927. xs_html_tag("p",
  2928. xs_html_text(error)),
  2929. xs_html_tag("form",
  2930. xs_html_attr("method", "dialog"),
  2931. xs_html_sctag("input",
  2932. xs_html_attr("type", "submit"),
  2933. xs_html_attr("value", L("Dismiss"))))));
  2934. }
  2935. /* show links to the available lists */
  2936. if (user && !read_only) {
  2937. xs_html *lol = xs_html_tag("ul",
  2938. xs_html_attr("class", "snac-list-of-lists"));
  2939. xs_html_add(body, lol);
  2940. xs *lists = list_maint(user, NULL, 0); /* get list of lists */
  2941. int ct = 0;
  2942. const char *v;
  2943. while (xs_list_next(lists, &v, &ct)) {
  2944. const char *lname = xs_list_get(v, 1);
  2945. xs *url = xs_fmt("%s/list/%s", user->actor, xs_list_get(v, 0));
  2946. xs *ttl = xs_fmt(L("Timeline for list '%s'"), lname);
  2947. xs_html_add(lol,
  2948. xs_html_tag("li",
  2949. xs_html_tag("a",
  2950. xs_html_attr("href", url),
  2951. xs_html_attr("class", "snac-list-link"),
  2952. xs_html_attr("title", ttl),
  2953. xs_html_text(lname))));
  2954. }
  2955. {
  2956. /* show the list of pinned posts */
  2957. xs *url = xs_fmt("%s/pinned", user->actor);
  2958. xs_html_add(lol,
  2959. xs_html_tag("li",
  2960. xs_html_tag("a",
  2961. xs_html_attr("href", url),
  2962. xs_html_attr("class", "snac-list-link"),
  2963. xs_html_attr("title", L("Pinned posts")),
  2964. xs_html_text(L("pinned")))));
  2965. }
  2966. {
  2967. /* show the list of bookmarked posts */
  2968. xs *url = xs_fmt("%s/bookmarks", user->actor);
  2969. xs_html_add(lol,
  2970. xs_html_tag("li",
  2971. xs_html_tag("a",
  2972. xs_html_attr("href", url),
  2973. xs_html_attr("class", "snac-list-link"),
  2974. xs_html_attr("title", L("Bookmarked posts")),
  2975. xs_html_text(L("bookmarks")))));
  2976. }
  2977. {
  2978. /* show the list of drafts */
  2979. xs *url = xs_fmt("%s/drafts", user->actor);
  2980. xs_html_add(lol,
  2981. xs_html_tag("li",
  2982. xs_html_tag("a",
  2983. xs_html_attr("href", url),
  2984. xs_html_attr("class", "snac-list-link"),
  2985. xs_html_attr("title", L("Post drafts")),
  2986. xs_html_text(L("drafts")))));
  2987. }
  2988. {
  2989. /* show the list of scheduled posts */
  2990. xs *url = xs_fmt("%s/sched", user->actor);
  2991. xs_html_add(lol,
  2992. xs_html_tag("li",
  2993. xs_html_tag("a",
  2994. xs_html_attr("href", url),
  2995. xs_html_attr("class", "snac-list-link"),
  2996. xs_html_attr("title", L("Scheduled posts")),
  2997. xs_html_text(L("scheduled posts")))));
  2998. }
  2999. /* the list of followed hashtags */
  3000. const char *followed_hashtags = xs_dict_get(user->config, "followed_hashtags");
  3001. if (xs_is_list(followed_hashtags) && xs_list_len(followed_hashtags)) {
  3002. xs_html *loht = xs_html_tag("ul",
  3003. xs_html_attr("class", "snac-list-of-lists"));
  3004. xs_html_add(body, loht);
  3005. const char *ht;
  3006. xs_list_foreach(followed_hashtags, ht) {
  3007. xs *url = NULL;
  3008. if (!xs_startswith(ht, "https:/""/"))
  3009. url = xs_fmt("%s/admin?q=%s", user->actor, ht);
  3010. else
  3011. url = xs_dup(ht);
  3012. url = xs_replace_i(url, "#", "%23");
  3013. xs_html_add(loht,
  3014. xs_html_tag("li",
  3015. xs_html_tag("a",
  3016. xs_html_attr("href", url),
  3017. xs_html_attr("class", "snac-list-link"),
  3018. xs_html_text(ht))));
  3019. }
  3020. }
  3021. }
  3022. xs_html_add(body,
  3023. xs_html_tag("a",
  3024. xs_html_attr("name", "snac-posts")));
  3025. xs_html *posts = xs_html_tag("div",
  3026. xs_html_attr("class", "snac-posts"));
  3027. if (title) {
  3028. xs_html_add(posts,
  3029. xs_html_tag("h2",
  3030. xs_html_attr("class", "snac-header"),
  3031. xs_html_text(title)));
  3032. }
  3033. xs_html_add(body, posts);
  3034. int mark_shown = 0;
  3035. int show_unlisted = user ? xs_is_true(xs_dict_get(user->config, "show_unlisted")) : 0;
  3036. while (xs_list_iter(&p, &v)) {
  3037. xs *msg = NULL;
  3038. int status;
  3039. /* "already seen" mark? */
  3040. if (strcmp(v, MD5_ALREADY_SEEN_MARK) == 0) {
  3041. if (skip == 0 && !mark_shown) {
  3042. xs *s = xs_fmt("%s/admin", user->actor);
  3043. xs_html_add(posts,
  3044. xs_html_tag("div",
  3045. xs_html_attr("class", "snac-no-more-unseen-posts"),
  3046. xs_html_text(L("No more unseen posts")),
  3047. xs_html_text(" - "),
  3048. xs_html_tag("a",
  3049. xs_html_attr("href", s),
  3050. xs_html_text(L("Back to top")))));
  3051. }
  3052. mark_shown = 1;
  3053. continue;
  3054. }
  3055. if (utl && user && !is_pinned_by_md5(user, v))
  3056. status = timeline_get_by_md5(user, v, &msg);
  3057. else
  3058. status = object_get_by_md5(v, &msg);
  3059. if (!valid_status(status))
  3060. continue;
  3061. /* if it's an instance page, discard messages from private users */
  3062. if (user == NULL && is_msg_from_private_user(msg))
  3063. continue;
  3064. const int scope = get_msg_visibility(msg);
  3065. if (user != NULL && scope != SCOPE_PUBLIC && !is_msg_mine(user, xs_dict_get(msg, "id"))) {
  3066. /* is this message a non-public reply? */
  3067. const char *irt = get_in_reply_to(msg);
  3068. /* is it a reply to something not in the storage? */
  3069. if (!xs_is_null(irt) && !object_here(irt)) {
  3070. /* is it for me? */
  3071. const xs_list *to = xs_dict_get_def(msg, "to", xs_stock(XSTYPE_LIST));
  3072. const xs_list *cc = xs_dict_get_def(msg, "cc", xs_stock(XSTYPE_LIST));
  3073. if (xs_list_in(to, user->actor) == -1 && xs_list_in(cc, user->actor) == -1) {
  3074. snac_debug(user, 1, xs_fmt("skipping non-public reply to an unknown post %s", v));
  3075. continue;
  3076. }
  3077. }
  3078. }
  3079. /* hide non-public posts from /instance view */
  3080. if (page != NULL && strcmp(page, "/instance") == 0 && scope != SCOPE_PUBLIC)
  3081. continue;
  3082. /* hide non-public posts viewed from outside */
  3083. if (read_only) {
  3084. if (scope == SCOPE_MENTIONED || scope == SCOPE_FOLLOWERS)
  3085. continue;
  3086. if (scope == SCOPE_UNLISTED && !show_unlisted)
  3087. continue;
  3088. }
  3089. xs_html *entry = html_entry(user, msg, read_only, 0, v, (user && !hide_children) ? 0 : 1);
  3090. if (entry != NULL)
  3091. xs_html_add(posts,
  3092. entry);
  3093. }
  3094. if (list && user && read_only) {
  3095. /** history **/
  3096. if (xs_type(xs_dict_get(srv_config, "disable_history")) != XSTYPE_TRUE && !terse) {
  3097. xs_html *ul = xs_html_tag("ul", NULL);
  3098. xs_html *history = xs_html_tag("div",
  3099. xs_html_attr("class", "snac-history"),
  3100. xs_html_tag("p",
  3101. xs_html_attr("class", "snac-history-title"),
  3102. xs_html_text(L("History"))),
  3103. ul);
  3104. xs *list = history_list(user);
  3105. xs_list *p = list;
  3106. const char *v;
  3107. while (xs_list_iter(&p, &v)) {
  3108. xs *fn = xs_replace(v, ".html", "");
  3109. xs *url = xs_fmt("%s/h/%s", user->actor, v);
  3110. xs_html_add(ul,
  3111. xs_html_tag("li",
  3112. xs_html_tag("a",
  3113. xs_html_attr("href", url),
  3114. xs_html_text(fn))));
  3115. }
  3116. xs_html_add(body,
  3117. history);
  3118. }
  3119. }
  3120. {
  3121. xs *s1 = xs_fmt("\n<!-- %lf seconds -->\n", ftime() - t);
  3122. xs_html_add(body,
  3123. xs_html_raw(s1));
  3124. }
  3125. if (show_more) {
  3126. xs *m = NULL;
  3127. xs *ss = xs_fmt("skip=%d&show=%d", skip + show, show);
  3128. xs *url = xs_dup(user == NULL ? srv_baseurl : user->actor);
  3129. if (page != NULL)
  3130. url = xs_str_cat(url, page);
  3131. if (xs_str_in(url, "?") != -1)
  3132. m = xs_fmt("%s&%s", url, ss);
  3133. else
  3134. m = xs_fmt("%s?%s", url, ss);
  3135. xs_html *more_links = xs_html_tag("p",
  3136. xs_html_tag("a",
  3137. xs_html_attr("href", url),
  3138. xs_html_attr("name", "snac-more"),
  3139. xs_html_text(L("Back to top"))),
  3140. xs_html_text(" - "),
  3141. xs_html_tag("a",
  3142. xs_html_attr("href", m),
  3143. xs_html_attr("name", "snac-more"),
  3144. xs_html_text(L("More..."))));
  3145. xs_html_add(body,
  3146. more_links);
  3147. }
  3148. xs_html_add(body,
  3149. html_footer(user));
  3150. return xs_html_render_s(html, "<!DOCTYPE html>\n");
  3151. }
  3152. xs_html *html_people_list(snac *user, xs_list *list, const char *header, const char *t, const char *proxy, int count)
  3153. {
  3154. xs_html *snac_posts;
  3155. xs *header_cnt;
  3156. if (count > -1)
  3157. header_cnt = xs_fmt("%s - %d\n", header, count);
  3158. else
  3159. header_cnt = xs_fmt("%s\n", header);
  3160. xs_html *people = xs_html_tag("div",
  3161. xs_html_tag("h2",
  3162. xs_html_attr("class", "snac-header"),
  3163. xs_html_text(header_cnt)),
  3164. snac_posts = xs_html_tag("details",
  3165. xs_html_attr("open", NULL),
  3166. xs_html_tag("summary",
  3167. xs_html_text("..."))));
  3168. xs *redir = xs_fmt("%s/people", user->actor);
  3169. const char *actor_id;
  3170. xs_list_foreach(list, actor_id) {
  3171. xs *md5 = xs_md5_hex(actor_id, strlen(actor_id));
  3172. xs *actor = NULL;
  3173. if (valid_status(actor_get(actor_id, &actor))) {
  3174. xs_html *snac_post = xs_html_tag("div",
  3175. xs_html_attr("class", "snac-post"),
  3176. xs_html_tag("a",
  3177. xs_html_attr("name", md5)),
  3178. xs_html_tag("div",
  3179. xs_html_attr("class", "snac-post-header"),
  3180. html_actor_icon(user, actor, xs_dict_get(actor, "published"),
  3181. NULL, NULL, -1, 0, proxy, NULL, NULL)));
  3182. /* content (user bio) */
  3183. const char *c = xs_dict_get(actor, "summary");
  3184. const xs_val *tag = xs_dict_get(actor, "tag");
  3185. if (!xs_is_null(c)) {
  3186. xs *sc = sanitize(c);
  3187. sc = replace_shortnames(sc, tag, 2, proxy);
  3188. xs_html *snac_content = xs_html_tag("div",
  3189. xs_html_attr("class", "snac-content"));
  3190. if (xs_startswith(sc, "<p>"))
  3191. xs_html_add(snac_content,
  3192. xs_html_raw(sc)); /* already sanitized */
  3193. else
  3194. xs_html_add(snac_content,
  3195. xs_html_tag("p",
  3196. xs_html_raw(sc))); /* already sanitized */
  3197. xs_html_add(snac_post, snac_content);
  3198. }
  3199. /* does it have account metrics in the object? */
  3200. const xs_val *fwing = xs_dict_get(actor, "following_count");
  3201. const xs_val *fwers = xs_dict_get(actor, "followers_count");
  3202. if (!xs_is_null(fwing) && !xs_is_null(fwers)) {
  3203. int i_fwing = (int) xs_number_get_l(fwing);
  3204. int i_fwers = (int) xs_number_get_l(fwers);
  3205. if (i_fwing || i_fwers) {
  3206. xs *s = xs_fmt(L("%d following, %d followers"), i_fwing, i_fwers);
  3207. xs_html_add(snac_post,
  3208. xs_html_tag("div",
  3209. xs_html_tag("p",
  3210. xs_html_text(s))));
  3211. }
  3212. }
  3213. /* does it have a location? */
  3214. const xs_dict *location = xs_dict_get(actor, "location");
  3215. if (xs_is_dict(location)) {
  3216. const xs_val *latitude = xs_dict_get(location, "latitude");
  3217. const xs_val *longitude = xs_dict_get(location, "longitude");
  3218. if (latitude && longitude) {
  3219. const char *name = xs_dict_get_def(location, "name", "Home");
  3220. const char *la = xs_is_string(latitude) ? latitude : xs_number_str(latitude);
  3221. const char *lo = xs_is_string(longitude) ? longitude : xs_number_str(longitude);
  3222. if (xs_is_string(la) && xs_is_string(lo)) {
  3223. xs *label = xs_fmt("%s,%s", la, lo);
  3224. xs *url = xs_fmt("https://openstreetmap.org/search?query=%s,%s", la, lo);
  3225. xs_html_add(snac_post,
  3226. xs_html_tag("p",
  3227. xs_html_text(name),
  3228. xs_html_text(": "),
  3229. xs_html_tag("a",
  3230. xs_html_attr("href", url),
  3231. xs_html_attr("target", "_blank"),
  3232. xs_html_text(label))));
  3233. }
  3234. }
  3235. }
  3236. /* add user metadata */
  3237. xs_html *snac_metadata = xs_html_tag("div",
  3238. xs_html_attr("class", "snac-metadata"));
  3239. int count = 0;
  3240. const xs_val *address = xs_dict_get(actor, "vcard:Address");
  3241. if (xs_is_string(address)) {
  3242. xs_html_add(snac_metadata,
  3243. xs_html_tag("span",
  3244. xs_html_attr("class", "snac-property-name"),
  3245. xs_html_raw("&#x1F4CD; Location")),
  3246. xs_html_text(":"),
  3247. xs_html_raw("&nbsp;"),
  3248. xs_html_tag("span",
  3249. xs_html_attr("class", "snac-property-value p-adr"),
  3250. xs_html_text(address)),
  3251. xs_html_sctag("br", NULL));
  3252. count++;
  3253. }
  3254. const xs_val *birthday = xs_dict_get(actor, "vcard:bday");
  3255. if (xs_is_string(birthday)) {
  3256. xs_html_add(snac_metadata,
  3257. xs_html_tag("span",
  3258. xs_html_attr("class", "snac-property-name"),
  3259. xs_html_raw("&#x1F382; Birthday")),
  3260. xs_html_text(":"),
  3261. xs_html_raw("&nbsp;"),
  3262. xs_html_tag("time",
  3263. xs_html_attr("class", "snac-property-value dt-bday"),
  3264. xs_html_text(birthday)),
  3265. xs_html_sctag("br", NULL));
  3266. count++;
  3267. }
  3268. const xs_list *attachment = xs_dict_get(actor, "attachment");
  3269. if (count > 0 && xs_list_len(attachment) > 0) {
  3270. xs_html_add(snac_metadata,
  3271. xs_html_sctag("hr",
  3272. xs_html_attr("class", "snac-property-divider")));
  3273. }
  3274. const xs_val *v;
  3275. xs_list_foreach(attachment, v) {
  3276. const char *type = xs_dict_get(v, "type");
  3277. const char *name = xs_dict_get(v, "name");
  3278. const char *value = xs_dict_get(v, "value");
  3279. if (!xs_is_null(type) && !xs_is_null(name) &&
  3280. !xs_is_null(value) && strcmp(type, "PropertyValue") == 0) {
  3281. /* both the name and the value can contain emoji */
  3282. xs *nam = sanitize(name);
  3283. nam = replace_shortnames(nam, tag, 1, proxy);
  3284. /* todo: sometimes the value is transmitted as markdown and not html ._. */
  3285. xs *val = sanitize(value);
  3286. val = replace_shortnames(val, tag, 1, proxy);
  3287. /* delete <p> tags, because some software sends them */
  3288. val = xs_replace_i(val, "<p>", "");
  3289. val = xs_replace_i(val, "</p>", "");
  3290. xs_html_add(snac_metadata,
  3291. xs_html_tag("span",
  3292. xs_html_attr("class", "snac-property-name"),
  3293. xs_html_raw(nam)),
  3294. xs_html_text(":"),
  3295. xs_html_raw("&nbsp;"),
  3296. xs_html_tag("span",
  3297. xs_html_attr("class", "snac-property-value"),
  3298. xs_html_raw(val)),
  3299. xs_html_sctag("br", NULL));
  3300. count++;
  3301. }
  3302. }
  3303. if (count > 0) {
  3304. xs_html_add(snac_post, snac_metadata);
  3305. }
  3306. else {
  3307. /* free the html, by rendering it... */
  3308. xs_free(xs_html_render(snac_metadata));
  3309. }
  3310. /* buttons */
  3311. xs *btn_form_action = xs_fmt("%s/admin/action", user->actor);
  3312. xs_html *snac_controls = xs_html_tag("div",
  3313. xs_html_attr("class", "snac-controls"));
  3314. xs_html *form = xs_html_tag("form",
  3315. xs_html_attr("autocomplete", "off"),
  3316. xs_html_attr("method", "post"),
  3317. xs_html_attr("action", btn_form_action),
  3318. xs_html_sctag("input",
  3319. xs_html_attr("type", "hidden"),
  3320. xs_html_attr("name", "actor"),
  3321. xs_html_attr("value", actor_id)),
  3322. xs_html_sctag("input",
  3323. xs_html_attr("type", "hidden"),
  3324. xs_html_attr("name", "hard-redir"),
  3325. xs_html_attr("value", redir)),
  3326. xs_html_sctag("input",
  3327. xs_html_attr("type", "hidden"),
  3328. xs_html_attr("name", "actor-form"),
  3329. xs_html_attr("value", "yes")));
  3330. xs_html_add(snac_controls, form);
  3331. if (following_check(user, actor_id)) {
  3332. xs_html_add(form,
  3333. html_button("unfollow", L("Unfollow"),
  3334. L("Stop following this user's activity")));
  3335. if (is_limited(user, actor_id))
  3336. xs_html_add(form,
  3337. html_button("unlimit", L("Unlimit"),
  3338. L("Allow announces (boosts) from this user")));
  3339. else
  3340. xs_html_add(form,
  3341. html_button("limit", L("Limit"),
  3342. L("Block announces (boosts) from this user")));
  3343. }
  3344. else {
  3345. xs_html_add(form,
  3346. html_button("follow", L("Follow"),
  3347. L("Start following this user's activity")));
  3348. if (follower_check(user, actor_id))
  3349. xs_html_add(form,
  3350. html_button("delete", L("Delete"), L("Delete this user")));
  3351. }
  3352. if (pending_check(user, actor_id)) {
  3353. xs_html_add(form,
  3354. html_button("approve", L("Approve"),
  3355. L("Approve this follow request")));
  3356. xs_html_add(form,
  3357. html_button("discard", L("Discard"), L("Discard this follow request")));
  3358. }
  3359. if (is_muted(user, actor_id))
  3360. xs_html_add(form,
  3361. html_button("unmute", L("Unmute"),
  3362. L("Stop blocking activities from this user")));
  3363. else
  3364. xs_html_add(form,
  3365. html_button("mute", L("MUTE"),
  3366. L("Block any activity from this user")));
  3367. /* the post textarea */
  3368. xs *dm_div_id = xs_fmt("%s_%s_dm", md5, t);
  3369. xs *dm_form_id = xs_fmt("%s_reply_form", md5);
  3370. xs_html_add(snac_controls,
  3371. xs_html_tag("p", NULL),
  3372. html_note(user, L("Direct Message..."),
  3373. dm_div_id, dm_form_id,
  3374. "", "",
  3375. NULL, actor_id,
  3376. xs_stock(XSTYPE_FALSE), "",
  3377. SCOPE_MENTIONED, NULL,
  3378. NULL, 0, NULL, NULL, 0, NULL, NULL),
  3379. xs_html_tag("p", NULL));
  3380. xs_html_add(snac_post, snac_controls);
  3381. xs_html_add(snac_posts, snac_post);
  3382. }
  3383. }
  3384. return people;
  3385. }
  3386. xs_str *html_people(snac *user)
  3387. {
  3388. const char *proxy = NULL;
  3389. if (xs_is_true(xs_dict_get(srv_config, "proxy_media")))
  3390. proxy = user->actor;
  3391. xs *wing = following_list(user);
  3392. xs *wers = follower_list(user);
  3393. xs *pending = pending_list(user);
  3394. xs_html *lists = xs_html_tag("div",
  3395. xs_html_attr("class", "snac-posts"));
  3396. if (xs_list_len(pending) || xs_is_true(xs_dict_get(user->config, "approve_followers"))) {
  3397. xs_html_add(lists,
  3398. html_people_list(user, pending, L("Pending follow confirmations"), "p", proxy, xs_list_len(pending)));
  3399. }
  3400. xs_html_add(lists,
  3401. html_people_list(user, wing, L("People you follow"), "i", proxy, following_list_len(user)),
  3402. html_people_list(user, wers, L("People that follow you"), "e", proxy, xs_list_len(wers)));
  3403. xs_html *html = xs_html_tag("html",
  3404. html_user_head(user, NULL, NULL),
  3405. xs_html_add(html_user_body(user, 0),
  3406. lists,
  3407. html_footer(user)));
  3408. return xs_html_render_s(html, "<!DOCTYPE html>\n");
  3409. }
  3410. /* Filter list to display only posts by actor. We'll probably show
  3411. fewer than show posts. Should we try harder to find some? */
  3412. xs_str *html_people_one(snac *user, const char *actor, const xs_list *list,
  3413. int skip, int show, int show_more, const char *page)
  3414. {
  3415. const char *proxy = NULL;
  3416. xs_list *p = (xs_list *)list;
  3417. const char *v;
  3418. enqueue_actor_refresh(user, actor, 0);
  3419. if (xs_is_true(xs_dict_get(srv_config, "proxy_media")))
  3420. proxy = user->actor;
  3421. xs_html *body = html_user_body(user, 0);
  3422. xs_html *lists = xs_html_tag("div",
  3423. xs_html_attr("class", "snac-posts"));
  3424. xs *foll = xs_list_append(xs_list_new(), actor);
  3425. xs_html_add(lists,
  3426. html_people_list(user, foll, L("Contact's posts"), "p", proxy, -1));
  3427. xs_html_add(body, lists);
  3428. while (xs_list_iter(&p, &v)) {
  3429. xs *msg = NULL;
  3430. int status;
  3431. status = timeline_get_by_md5(user, v, &msg);
  3432. if (!valid_status(status))
  3433. continue;
  3434. const char *id = xs_dict_get(msg, "id");
  3435. const char *by = get_atto(msg);
  3436. xs *actor_md5 = NULL;
  3437. xs_list *boosts = NULL;
  3438. xs_list *likes = NULL;
  3439. xs_list *reacts = NULL;
  3440. /* Besides actor's posts, also show actor's boosts, and also
  3441. posts by user with likes or reacts by actor. I.e., any
  3442. actor's actions that user could have seen in the timeline
  3443. or in notifications. */
  3444. if (!(by && strcmp(actor, by) == 0) &&
  3445. xs_list_in((boosts = object_announces(id)),
  3446. (actor_md5 = xs_md5_hex(actor, strlen(actor)))) == -1 &&
  3447. (!(by && strcmp(user->actor, by) == 0) ||
  3448. (xs_list_in((likes = object_likes(id)), actor_md5) == -1 &&
  3449. xs_list_in((reacts = object_get_emoji_reacts(id)), actor_md5) == -1)))
  3450. continue;
  3451. xs_html *entry = html_entry(user, msg, 0, 0, v, 1);
  3452. if (entry != NULL)
  3453. xs_html_add(lists,
  3454. entry);
  3455. }
  3456. if (show_more) {
  3457. xs *m = NULL;
  3458. xs *m10 = NULL;
  3459. xs *ss = xs_fmt("skip=%d&show=%d", skip + show, show);
  3460. xs *url = xs_dup(user == NULL ? srv_baseurl : user->actor);
  3461. if (page != NULL)
  3462. url = xs_str_cat(url, page);
  3463. if (xs_str_in(url, "?") != -1)
  3464. m = xs_fmt("%s&%s", url, ss);
  3465. else
  3466. m = xs_fmt("%s?%s", url, ss);
  3467. m10 = xs_fmt("%s0", m);
  3468. xs_html *more_links = xs_html_tag("p",
  3469. xs_html_tag("a",
  3470. xs_html_attr("href", url),
  3471. xs_html_attr("name", "snac-more"),
  3472. xs_html_text(L("Back to top"))),
  3473. xs_html_text(" - "),
  3474. xs_html_tag("a",
  3475. xs_html_attr("href", m),
  3476. xs_html_attr("name", "snac-more"),
  3477. xs_html_text(L("More..."))),
  3478. xs_html_text(" - "),
  3479. xs_html_tag("a",
  3480. xs_html_attr("href", m10),
  3481. xs_html_attr("name", "snac-more"),
  3482. xs_html_text(L("More (x 10)..."))));
  3483. xs_html_add(body,
  3484. more_links);
  3485. }
  3486. xs_html *html = xs_html_tag("html",
  3487. html_user_head(user, NULL, NULL),
  3488. xs_html_add(body,
  3489. html_footer(user)));
  3490. return xs_html_render_s(html, "<!DOCTYPE html>\n");
  3491. }
  3492. void notify_filter(snac *user, const xs_dict *p_vars)
  3493. /* sets filter for notifications */
  3494. {
  3495. const char *v;
  3496. int likes_on = (v = xs_dict_get(p_vars, "likes_on")) ? strcmp(v, "on") == 0 : 0;
  3497. int reacts_on = (v = xs_dict_get(p_vars, "reacts_on")) ? strcmp(v, "on") == 0 : 0;
  3498. int ments_on = (v = xs_dict_get(p_vars, "mentions_on")) ? strcmp(v, "on") == 0 : 0;
  3499. int ann_on = (v = xs_dict_get(p_vars, "announces_on")) ? strcmp(v, "on") == 0 : 0;
  3500. int foll_on = (v = xs_dict_get(p_vars, "follows_on")) ? strcmp(v, "on") == 0 : 0;
  3501. int unfoll_on = (v = xs_dict_get(p_vars, "unfollows_on")) ? strcmp(v, "on") == 0 : 0;
  3502. int folreq_on = (v = xs_dict_get(p_vars, "folreqs_on")) ? strcmp(v, "on") == 0 : 0;
  3503. int blocks_on = (v = xs_dict_get(p_vars, "blocks_on")) ? strcmp(v, "on") == 0 : 0;
  3504. int polls_on = (v = xs_dict_get(p_vars, "polls_on")) ? strcmp(v, "on") == 0 : 0;
  3505. xs *filter = xs_dict_new();
  3506. filter = xs_dict_set(filter, "likes", xs_stock(likes_on ? XSTYPE_TRUE : XSTYPE_FALSE));
  3507. filter = xs_dict_set(filter, "reacts", xs_stock(reacts_on ? XSTYPE_TRUE : XSTYPE_FALSE));
  3508. filter = xs_dict_set(filter, "mentions", xs_stock(ments_on ? XSTYPE_TRUE : XSTYPE_FALSE));
  3509. filter = xs_dict_set(filter, "announces", xs_stock(ann_on ? XSTYPE_TRUE : XSTYPE_FALSE));
  3510. filter = xs_dict_set(filter, "follows", xs_stock(foll_on ? XSTYPE_TRUE : XSTYPE_FALSE));
  3511. filter = xs_dict_set(filter, "unfollows", xs_stock(unfoll_on ? XSTYPE_TRUE : XSTYPE_FALSE));
  3512. filter = xs_dict_set(filter, "folreqs", xs_stock(folreq_on ? XSTYPE_TRUE : XSTYPE_FALSE));
  3513. filter = xs_dict_set(filter, "blocks", xs_stock(blocks_on ? XSTYPE_TRUE : XSTYPE_FALSE));
  3514. filter = xs_dict_set(filter, "polls", xs_stock(polls_on ? XSTYPE_TRUE : XSTYPE_FALSE));
  3515. user->config = xs_dict_set(user->config, "notify_filter", filter);
  3516. user->tz = xs_dict_get_def(user->config, "tz", "UTC"); // previous line invalidates user->tz
  3517. }
  3518. xs_str *html_notifications(snac *user, int skip, int show)
  3519. {
  3520. const char *proxy = NULL;
  3521. if (xs_is_true(xs_dict_get(srv_config, "proxy_media")))
  3522. proxy = user->actor;
  3523. xs *n_list_unfilt = notify_list(user, skip, show);
  3524. xs *n_time = notify_check_time(user, 0);
  3525. xs_html *body = html_user_body(user, 0);
  3526. const xs_dict *n_filter = xs_dict_get(user->config, "notify_filter");
  3527. if (!n_filter) {
  3528. xs *filter = xs_dict_new();
  3529. user->config = xs_dict_set(user->config, "notify_filter", filter);
  3530. user->tz = xs_dict_get_def(user->config, "tz", "UTC"); // previous line invalidates user->tz
  3531. n_filter = xs_dict_get(user->config, "notify_filter");
  3532. }
  3533. xs *n_list = notify_filter_list(user, n_list_unfilt);
  3534. /* all filters are true by default */
  3535. const xs_val *n_def = xs_stock( XSTYPE_TRUE );
  3536. int n_likes_on = xs_is_true(xs_dict_get_def(n_filter, "likes", n_def));
  3537. int n_reacts_on = xs_is_true(xs_dict_get_def(n_filter, "reacts", n_def));
  3538. int n_ments_on = xs_is_true(xs_dict_get_def(n_filter, "mentions", n_def));
  3539. int n_ann_on = xs_is_true(xs_dict_get_def(n_filter, "announces", n_def));
  3540. int n_fol_on = xs_is_true(xs_dict_get_def(n_filter, "follows", n_def));
  3541. int n_unfol_on = xs_is_true(xs_dict_get_def(n_filter, "unfollows", n_def));
  3542. int n_folreq_on = xs_is_true(xs_dict_get_def(n_filter, "folreqs", n_def));
  3543. int n_blocks_on = xs_is_true(xs_dict_get_def(n_filter, "blocks", n_def));
  3544. int n_polls_on = xs_is_true(xs_dict_get_def(n_filter, "polls", n_def));
  3545. xs_html *html = xs_html_tag("html",
  3546. html_user_head(user, NULL, NULL),
  3547. body);
  3548. xs *filter_notifs_action = xs_fmt("%s/admin/filter-notifications", user->actor);
  3549. xs *notifs_action = xs_fmt("%s/notifications", user->actor);
  3550. xs_html *notifs_form = xs_html_tag("form",
  3551. xs_html_attr("autocomplete", "off"),
  3552. xs_html_attr("method", "post"),
  3553. xs_html_attr("action", filter_notifs_action),
  3554. xs_html_attr("enctype", "multipart/form-data"),
  3555. xs_html_attr("id", "filter"),
  3556. xs_html_sctag("input",
  3557. xs_html_attr("type", "hidden"),
  3558. xs_html_attr("name", "hard-redir"),
  3559. xs_html_attr("value", notifs_action)),
  3560. html_checkbox("likes_on", L("Likes"), n_likes_on),
  3561. html_checkbox("reacts_on", L("Emoji reacts"), n_reacts_on),
  3562. html_checkbox("mentions_on", L("Mentions"), n_ments_on),
  3563. html_checkbox("announces_on", L("Announces"), n_ann_on),
  3564. html_checkbox("follows_on", L("Follows"), n_fol_on),
  3565. html_checkbox("unfollows_on", L("Unfollows"), n_unfol_on),
  3566. html_checkbox("folreqs_on", L("Follow requests"), n_folreq_on),
  3567. html_checkbox("blocks_on", L("Blocks"), n_blocks_on),
  3568. html_checkbox("polls_on", L("Polls"), n_polls_on),
  3569. xs_html_sctag("input",
  3570. xs_html_attr("type", "submit"),
  3571. xs_html_attr("class", "button"),
  3572. xs_html_attr("value", L("Save"))));
  3573. xs_html_add(body,
  3574. xs_html_tag("p",
  3575. xs_html_tag("div",
  3576. xs_html_attr("class", "snac-notify-filter"),
  3577. xs_html_tag("details",
  3578. xs_html_tag("summary",
  3579. xs_html_text(L("Notifications filter..."))),
  3580. notifs_form))));
  3581. xs *clear_all_action = xs_fmt("%s/admin/clear-notifications", user->actor);
  3582. xs_html_add(body,
  3583. xs_html_tag("form",
  3584. xs_html_attr("autocomplete", "off"),
  3585. xs_html_attr("method", "post"),
  3586. xs_html_attr("action", clear_all_action),
  3587. xs_html_attr("id", "clear"),
  3588. xs_html_sctag("input",
  3589. xs_html_attr("type", "submit"),
  3590. xs_html_attr("class", "snac-btn-like"),
  3591. xs_html_attr("value", L("Clear all")))));
  3592. xs_html *noti_new = NULL;
  3593. xs_html *noti_seen = NULL;
  3594. xs_html *posts = xs_html_tag("div",
  3595. xs_html_attr("class", "snac-posts"));
  3596. xs_html_add(body, posts);
  3597. xs_set rep;
  3598. xs_set_init(&rep);
  3599. /* dict to store previous notification labels */
  3600. xs *admiration_labels = xs_dict_new();
  3601. const xs_str *v;
  3602. xs_list_foreach(n_list, v) {
  3603. xs *noti = notify_get(user, v);
  3604. if (noti == NULL)
  3605. continue;
  3606. xs *obj = NULL;
  3607. const char *type = xs_dict_get(noti, "type");
  3608. const char *utype = xs_dict_get(noti, "utype");
  3609. const char *id = xs_dict_get(noti, "objid");
  3610. const char *date = xs_dict_get(noti, "date");
  3611. const char *id2 = xs_dict_get_path(noti, "msg.id");
  3612. xs *wrk = NULL;
  3613. if (xs_is_null(id))
  3614. continue;
  3615. if (is_hidden(user, id))
  3616. continue;
  3617. if (xs_is_string(id2) && xs_set_add(&rep, id2) != 1)
  3618. continue;
  3619. if (strcmp(type, "EmojiReact") == 0 && xs_is_true(xs_dict_get(srv_config, "disable_emojireact")))
  3620. continue;
  3621. object_get(id, &obj);
  3622. const char *msg_id = NULL;
  3623. if (xs_is_dict(obj))
  3624. msg_id = xs_dict_get(obj, "id");
  3625. const char *actor_id = xs_dict_get(noti, "actor");
  3626. xs *actor = NULL;
  3627. xs *a_name = NULL;
  3628. if (valid_status(actor_get(actor_id, &actor)))
  3629. a_name = actor_name(actor, proxy);
  3630. else {
  3631. a_name = xs_dup(actor_id);
  3632. /* actor not here: request it */
  3633. enqueue_actor_refresh(user, actor_id, 0);
  3634. }
  3635. const char *html_url = xs_dict_get_def(actor, "url", actor_id);
  3636. if (xs_is_list(html_url))
  3637. html_url = xs_list_get(html_url, 0);
  3638. if (!xs_is_string(html_url))
  3639. html_url = actor_id;
  3640. xs *label_sanitized = sanitize(type);
  3641. const char *label = label_sanitized;
  3642. if (strcmp(type, "Create") == 0)
  3643. label = L("Mention");
  3644. else
  3645. if (strcmp(type, "Update") == 0 && strcmp(utype, "Question") == 0)
  3646. label = L("Finished poll");
  3647. else
  3648. if (strcmp(type, "Undo") == 0 && strcmp(utype, "Follow") == 0)
  3649. label = L("Unfollow");
  3650. else
  3651. if (strcmp(type, "EmojiReact") == 0 || strcmp(type, "Like") == 0) {
  3652. const char *content = xs_dict_get_path(noti, "msg.content");
  3653. xs *cd = xs_dup(content);
  3654. const char *sna = cd;
  3655. const xs_dict *tag = xs_dict_get_path(noti, "msg.tag");
  3656. unsigned int utf = xs_utf8_dec((const char **)&sna);
  3657. int isEmoji = 0;
  3658. if (xs_is_emoji(utf) || (tag && xs_list_len(tag) > 0))
  3659. isEmoji = 1;
  3660. if (xs_type(content) == XSTYPE_STRING) {
  3661. if (!isEmoji && !n_likes_on)
  3662. continue;
  3663. xs *emoji = replace_shortnames(xs_dup(content), xs_dict_get_path(noti, "msg.tag"), 1, proxy);
  3664. wrk = xs_fmt("%s (%s&#xFE0F;)", isEmoji ? "EmojiReact" : "Like", emoji);
  3665. label = wrk;
  3666. }
  3667. }
  3668. else
  3669. if (strcmp(type, "Follow") == 0 && pending_check(user, actor_id))
  3670. label = L("Follow Request");
  3671. xs *s_date = html_date_label(user, date);
  3672. xs_html *this_html_label = xs_html_container(
  3673. xs_html_tag("b",
  3674. xs_html_raw(label),
  3675. xs_html_text(" by "),
  3676. xs_html_tag("a",
  3677. xs_html_attr("href", html_url),
  3678. xs_html_raw(a_name))), /* a_name is already sanitized */
  3679. xs_html_text(" "),
  3680. xs_html_tag("time",
  3681. xs_html_attr("class", "dt-published snac-pubdate"),
  3682. xs_html_attr("title", date),
  3683. xs_html_text(s_date)));
  3684. xs_html *html_label = NULL;
  3685. if (xs_is_string(msg_id)) {
  3686. const xs_val *prev_label = xs_dict_get(admiration_labels, msg_id);
  3687. if (xs_type(prev_label) == XSTYPE_DATA) {
  3688. /* there is a previous list of admiration labels! */
  3689. xs_data_get(&html_label, prev_label);
  3690. xs_html_add(html_label,
  3691. xs_html_sctag("br", NULL),
  3692. this_html_label);
  3693. continue;
  3694. }
  3695. }
  3696. xs_html *entry = NULL;
  3697. html_label = xs_html_tag("p",
  3698. this_html_label);
  3699. /* store in the admiration labels dict */
  3700. xs *pl = xs_data_new(&html_label, sizeof(html_label));
  3701. if (xs_is_string(msg_id))
  3702. admiration_labels = xs_dict_set(admiration_labels, msg_id, pl);
  3703. entry = xs_html_tag("div",
  3704. xs_html_attr("class", "snac-post-with-desc"),
  3705. html_label);
  3706. if (strcmp(type, "Block") == 0) {
  3707. if (actor)
  3708. xs_html_add(entry,
  3709. xs_html_tag("div",
  3710. xs_html_attr("class", "snac-post"),
  3711. html_actor_icon(user, actor, NULL, NULL, NULL, -1, 0, proxy, NULL, NULL)));
  3712. }
  3713. else
  3714. if (strcmp(type, "Follow") == 0 || strcmp(utype, "Follow") == 0) {
  3715. if (actor) {
  3716. xs *action = xs_fmt("%s/admin/action", user->actor);
  3717. xs_html *button = NULL;
  3718. if (following_check(user, actor_id))
  3719. button = html_button("unfollow", L("Unfollow"), L("Stop following this user's activity"));
  3720. else
  3721. button = html_button("follow", L("Follow"), L("Start following this user's activity"));
  3722. xs_html_add(entry,
  3723. xs_html_tag("div",
  3724. xs_html_attr("class", "snac-post"),
  3725. html_actor_icon(user, actor, NULL, NULL, NULL, -1, 0, proxy, NULL, NULL),
  3726. xs_html_tag("form",
  3727. xs_html_attr("method", "post"),
  3728. xs_html_attr("action", action),
  3729. xs_html_sctag("input",
  3730. xs_html_attr("type", "hidden"),
  3731. xs_html_attr("name", "actor"),
  3732. xs_html_attr("value", actor_id)),
  3733. button,
  3734. xs_html_sctag("br", NULL))));
  3735. }
  3736. }
  3737. else
  3738. if (strcmp(type, "Move") == 0) {
  3739. const xs_dict *o_msg = xs_dict_get(noti, "msg");
  3740. const char *target;
  3741. if (xs_type(o_msg) == XSTYPE_DICT && (target = xs_dict_get(o_msg, "target"))) {
  3742. xs *old_actor = NULL;
  3743. if (valid_status(actor_get(target, &old_actor))) {
  3744. xs_html_add(entry,
  3745. xs_html_tag("div",
  3746. xs_html_attr("class", "snac-post"),
  3747. html_actor_icon(user, old_actor, NULL, NULL, NULL, -1, 0, proxy, NULL, NULL)));
  3748. }
  3749. }
  3750. }
  3751. else
  3752. if (obj != NULL) {
  3753. xs *md5 = xs_md5_hex(id, strlen(id));
  3754. xs *ctxt = xs_fmt("%s/admin/p/%s#%s_entry", user->actor, md5, md5);
  3755. xs_html *h = html_entry(user, obj, 0, 0, md5, 1);
  3756. if (h != NULL) {
  3757. xs_html_add(entry,
  3758. xs_html_tag("p",
  3759. xs_html_tag("a",
  3760. xs_html_attr("href", ctxt),
  3761. xs_html_text(L("Context")))),
  3762. h);
  3763. }
  3764. else
  3765. xs_html_add(entry,
  3766. xs_html_tag("p",
  3767. xs_html_text(L("Location: ")),
  3768. xs_html_tag("a",
  3769. xs_html_attr("href", id),
  3770. xs_html_text(id))));
  3771. }
  3772. if (strcmp(v, n_time) > 0) {
  3773. /* unseen notification */
  3774. if (noti_new == NULL) {
  3775. noti_new = xs_html_tag("div",
  3776. xs_html_tag("h2",
  3777. xs_html_attr("class", "snac-header"),
  3778. xs_html_text(L("New"))));
  3779. xs_html_add(posts,
  3780. noti_new);
  3781. }
  3782. xs_html_add(noti_new,
  3783. entry);
  3784. }
  3785. else {
  3786. /* already seen notification */
  3787. if (noti_seen == NULL) {
  3788. noti_seen = xs_html_tag("div",
  3789. xs_html_tag("h2",
  3790. xs_html_attr("class", "snac-header"),
  3791. xs_html_text(L("Already seen"))));
  3792. xs_html_add(posts,
  3793. noti_seen);
  3794. }
  3795. xs_html_add(noti_seen,
  3796. entry);
  3797. }
  3798. }
  3799. if (noti_new == NULL && noti_seen == NULL)
  3800. xs_html_add(body,
  3801. xs_html_tag("h2",
  3802. xs_html_attr("class", "snac-header"),
  3803. xs_html_text(L("None"))));
  3804. /* add the navigation footer */
  3805. xs *next_p = notify_list(user, skip + show, 1);
  3806. if (xs_list_len(next_p)) {
  3807. xs *url = xs_fmt("%s/notifications?skip=%d&show=%d",
  3808. user->actor, skip + show, show);
  3809. xs_html_add(body,
  3810. xs_html_tag("p",
  3811. xs_html_tag("a",
  3812. xs_html_attr("href", url),
  3813. xs_html_text(L("More...")))));
  3814. }
  3815. xs_set_free(&rep);
  3816. xs_html_add(body,
  3817. html_footer(user));
  3818. /* set the check time to now */
  3819. xs *dummy = notify_check_time(user, 1);
  3820. dummy = xs_free(dummy);
  3821. timeline_touch(user);
  3822. return xs_html_render_s(html, "<!DOCTYPE html>\n");
  3823. }
  3824. void set_user_lang(snac *user)
  3825. /* sets the language dict according to user configuration */
  3826. {
  3827. user->lang = NULL;
  3828. const char *lang = xs_dict_get(user->config, "lang");
  3829. if (xs_is_string(lang))
  3830. user->lang = xs_dict_get(srv_langs, lang);
  3831. }
  3832. int html_get_handler(const xs_dict *req, const char *q_path,
  3833. char **body, int *b_size, char **ctype,
  3834. xs_str **etag, xs_str **last_modified)
  3835. {
  3836. const char *accept = xs_dict_get(req, "accept");
  3837. int status = HTTP_STATUS_NOT_FOUND;
  3838. const snac *user = NULL;
  3839. snac snac;
  3840. xs *uid = NULL;
  3841. const char *p_path;
  3842. int cache = 1;
  3843. int save = 1;
  3844. int proxy = 0;
  3845. int terse = 0;
  3846. const char *v;
  3847. const xs_dict *q_vars = xs_dict_get(req, "q_vars");
  3848. xs *l = xs_split_n(q_path, "/", 2);
  3849. v = xs_list_get(l, 1);
  3850. if (xs_is_null(v)) {
  3851. srv_log(xs_fmt("html_get_handler bad query '%s'", q_path));
  3852. return HTTP_STATUS_NOT_FOUND;
  3853. }
  3854. if (!xs_is_null(xs_dict_get(q_vars, "terse")))
  3855. terse = 1;
  3856. if (strcmp(v, "share-bridge") == 0) {
  3857. /* temporary redirect for a post */
  3858. const char *login = xs_dict_get(q_vars, "login");
  3859. const char *content = xs_dict_get(q_vars, "content");
  3860. if (xs_type(login) == XSTYPE_STRING && xs_type(content) == XSTYPE_STRING) {
  3861. xs *b64 = xs_base64_enc(content, strlen(content));
  3862. srv_log(xs_fmt("share-bridge for user '%s'", login));
  3863. *body = xs_fmt("%s/%s/share?content=%s", srv_baseurl, login, b64);
  3864. return HTTP_STATUS_SEE_OTHER;
  3865. }
  3866. else
  3867. return HTTP_STATUS_NOT_FOUND;
  3868. }
  3869. else
  3870. if (strcmp(v, "auth-int-bridge") == 0) {
  3871. const char *login = xs_dict_get(q_vars, "login");
  3872. const char *id = xs_dict_get(q_vars, "id");
  3873. const char *action = xs_dict_get(q_vars, "action");
  3874. if (xs_is_string(login) && xs_is_string(id) && xs_is_string(action)) {
  3875. *body = xs_fmt("%s/%s/authorize_interaction?action=%s&id=%s",
  3876. srv_baseurl, login, action, id);
  3877. return HTTP_STATUS_SEE_OTHER;
  3878. }
  3879. else
  3880. return HTTP_STATUS_NOT_FOUND;
  3881. }
  3882. uid = xs_dup(v);
  3883. /* rss extension? */
  3884. if (xs_endswith(uid, ".rss")) {
  3885. uid = xs_crop_i(uid, 0, -4);
  3886. p_path = ".rss";
  3887. }
  3888. else
  3889. p_path = xs_list_get(l, 2);
  3890. if (!uid || !user_open(&snac, uid)) {
  3891. /* invalid user */
  3892. status = grave(uid, 0) ? HTTP_STATUS_GONE : HTTP_STATUS_NOT_FOUND;
  3893. srv_debug(2, xs_fmt("html_get_handler bad user '%s' %d", xs_or(uid, "(null)"), status));
  3894. return status;
  3895. }
  3896. user = &snac; /* for L() */
  3897. set_user_lang(&snac);
  3898. if (xs_is_true(xs_dict_get(srv_config, "proxy_media")))
  3899. proxy = 1;
  3900. /* return the RSS if requested by Accept header */
  3901. if (accept != NULL) {
  3902. if (xs_str_in(accept, "text/xml") != -1 ||
  3903. xs_str_in(accept, "application/rss+xml") != -1)
  3904. p_path = ".rss";
  3905. }
  3906. /* check if server config variable 'disable_cache' is set */
  3907. if ((v = xs_dict_get(srv_config, "disable_cache")) && xs_type(v) == XSTYPE_TRUE)
  3908. cache = 0;
  3909. int skip = 0;
  3910. const char *max_show_default = "50";
  3911. int max_show = xs_number_get(xs_dict_get_def(srv_config, "max_timeline_entries",
  3912. max_show_default));
  3913. int def_show = xs_number_get(xs_dict_get_def(srv_config, "def_timeline_entries",
  3914. xs_dict_get_def(srv_config, "max_timeline_entries",
  3915. max_show_default)));
  3916. int show = def_show;
  3917. if ((v = xs_dict_get(q_vars, "skip")) != NULL)
  3918. skip = atoi(v), cache = 0, save = 0;
  3919. if ((v = xs_dict_get(q_vars, "show")) != NULL)
  3920. show = atoi(v), cache = 0, save = 0;
  3921. if ((v = xs_dict_get(q_vars, "da")) != NULL) {
  3922. /* user dismissed an announcement */
  3923. if (login(&snac, req)) {
  3924. double ts = atof(v);
  3925. xs *timestamp = xs_number_new(ts);
  3926. srv_log(xs_fmt("user dismissed announcements until %d", ts));
  3927. snac.config = xs_dict_set(snac.config, "last_announcement", timestamp);
  3928. user_persist(&snac, 0);
  3929. }
  3930. }
  3931. /* get a possible error message */
  3932. const char *error = xs_dict_get(q_vars, "error");
  3933. if (error != NULL)
  3934. cache = 0;
  3935. /* a show of 0 has no sense */
  3936. if (show == 0)
  3937. show = def_show;
  3938. if (show > max_show)
  3939. show = max_show;
  3940. if (p_path == NULL) { /** public timeline **/
  3941. xs *h = xs_str_localtime(0, "%Y-%m.html");
  3942. if (xs_type(xs_dict_get(snac.config, "private")) == XSTYPE_TRUE) {
  3943. /** empty public timeline for private users **/
  3944. *body = html_timeline(&snac, NULL, 1, 0, 0, 0, NULL, "", 1, error, terse);
  3945. *b_size = strlen(*body);
  3946. status = HTTP_STATUS_OK;
  3947. }
  3948. else
  3949. if (cache && history_mtime(&snac, h) > timeline_mtime(&snac)) {
  3950. snac_debug(&snac, 1, xs_fmt("serving cached local timeline"));
  3951. status = history_get(&snac, h, body, b_size,
  3952. xs_dict_get(req, "if-none-match"), etag);
  3953. }
  3954. else {
  3955. xs *list = NULL;
  3956. int more = 0;
  3957. if (xs_is_true(xs_dict_get(srv_config, "strict_public_timelines")))
  3958. list = timeline_simple_list(&snac, "public", skip, show, &more);
  3959. else
  3960. list = timeline_list(&snac, "public", skip, show, &more);
  3961. xs *pins = pinned_list(&snac);
  3962. pins = xs_list_cat(pins, list);
  3963. *body = html_timeline(&snac, pins, 1, skip, show, more, NULL, "", 1, error, terse);
  3964. *b_size = strlen(*body);
  3965. status = HTTP_STATUS_OK;
  3966. if (save)
  3967. history_add(&snac, h, *body, *b_size, etag);
  3968. }
  3969. }
  3970. else
  3971. if (strcmp(p_path, "admin") == 0) { /** private timeline **/
  3972. if (!login(&snac, req)) {
  3973. *body = xs_dup(uid);
  3974. status = HTTP_STATUS_UNAUTHORIZED;
  3975. }
  3976. else {
  3977. const char *q = NULL;
  3978. xs *cq = xs_dup(xs_dict_get(q_vars, "q"));
  3979. xs *url_acct = NULL;
  3980. if (xs_is_string(cq)) {
  3981. cq = xs_strip_i(cq);
  3982. q = cq;
  3983. }
  3984. /* searching for an URL? */
  3985. if (q && xs_match(q, "https://*|http://*")) {
  3986. /* bring it to the user's timeline */
  3987. xs *actor_obj = NULL;
  3988. xs *object = NULL;
  3989. int status;
  3990. status = activitypub_request(&snac, q, &object);
  3991. snac_debug(&snac, 1, xs_fmt("Request searched URL %s %d", q, status));
  3992. if (valid_status(status) && xs_is_dict(object)) {
  3993. /* got it; also request the actor */
  3994. const char *attr_to = get_atto(object);
  3995. if (!xs_is_null(attr_to)) {
  3996. status = actor_request(&snac, attr_to, &actor_obj);
  3997. if (valid_status(status)) {
  3998. /* reset the query string to be the real id */
  3999. url_acct = xs_dup(xs_dict_get(object, "id"));
  4000. q = url_acct;
  4001. /* add the post to the timeline */
  4002. if (!timeline_here(&snac, q))
  4003. timeline_add(&snac, q, object);
  4004. }
  4005. }
  4006. else {
  4007. /* retry webfinger, this time with the 'official' id */
  4008. const char *id = xs_dict_get(object, "id");
  4009. if (xs_is_string(id) && valid_status(webfinger_request(id, &actor_obj, &url_acct)) &&
  4010. xs_is_string(url_acct))
  4011. q = url_acct;
  4012. }
  4013. }
  4014. else
  4015. /* may by an actor; try a webfinger */
  4016. if (valid_status(webfinger_request(q, &actor_obj, &url_acct)) && xs_is_string(url_acct)) {
  4017. /* it's an actor; do the dirty trick of changing q to the account name */
  4018. q = url_acct;
  4019. }
  4020. /* fall through */
  4021. }
  4022. if (q && *q) {
  4023. if (xs_regex_match(q, "^@?[^@]+@[a-zA-Z0-9-]+\\.")) {
  4024. /** search account **/
  4025. xs *actor = NULL;
  4026. xs *acct = NULL;
  4027. xs *l = xs_list_new();
  4028. xs_html *page = NULL;
  4029. if (valid_status(webfinger_request(q, &actor, &acct))) {
  4030. xs *actor_obj = NULL;
  4031. if (valid_status(actor_request(&snac, actor, &actor_obj))) {
  4032. actor_add(actor, actor_obj);
  4033. /* create a people list with only one element */
  4034. l = xs_list_append(l, actor);
  4035. xs *title = xs_fmt(L("Search results for account %s"), q);
  4036. page = html_people_list(&snac, l, title, "wf", NULL, 1);
  4037. }
  4038. }
  4039. if (page == NULL) {
  4040. xs *title = xs_fmt(L("Account %s not found"), q);
  4041. page = xs_html_tag("div",
  4042. xs_html_tag("h2",
  4043. xs_html_attr("class", "snac-header"),
  4044. xs_html_text(title)));
  4045. }
  4046. xs_html *html = xs_html_tag("html",
  4047. html_user_head(&snac, NULL, NULL),
  4048. xs_html_add(html_user_body(&snac, 0),
  4049. page,
  4050. html_footer(user)));
  4051. *body = xs_html_render_s(html, "<!DOCTYPE html>\n");
  4052. *b_size = strlen(*body);
  4053. status = HTTP_STATUS_OK;
  4054. }
  4055. else
  4056. if (*q == '#') {
  4057. /** search by tag **/
  4058. xs *tl = tag_search(q, skip, show + 1);
  4059. int more = 0;
  4060. if (xs_list_len(tl) >= show + 1) {
  4061. /* drop the last one */
  4062. tl = xs_list_del(tl, -1);
  4063. more = 1;
  4064. }
  4065. xs *page = xs_fmt("/admin?q=%%23%s", q + 1);
  4066. xs *title = xs_fmt(xs_list_len(tl) ?
  4067. L("Search results for tag %s") : L("Nothing found for tag %s"), q);
  4068. *body = html_timeline(&snac, tl, 0, skip, show, more, title, page, 0, error, terse);
  4069. *b_size = strlen(*body);
  4070. status = HTTP_STATUS_OK;
  4071. }
  4072. else {
  4073. /** search by content **/
  4074. int to = 0;
  4075. int msecs = atoi(xs_dict_get_def(q_vars, "msecs", "0"));
  4076. xs *tl = content_search(&snac, q, 1, skip, show, msecs, &to);
  4077. xs *title = NULL;
  4078. xs *page = xs_fmt("/admin?q=%s&msecs=%d", q, msecs + 10);
  4079. int tl_len = xs_list_len(tl);
  4080. if (to)
  4081. title = xs_fmt(L("Search results for '%s' (may be more)"), q);
  4082. else
  4083. if (tl_len)
  4084. title = xs_fmt(L("Search results for '%s'"), q);
  4085. else
  4086. if (skip)
  4087. title = xs_fmt(L("No more matches for '%s'"), q);
  4088. else
  4089. title = xs_fmt(L("Nothing found for '%s'"), q);
  4090. *body = html_timeline(&snac, tl, 0, skip, tl_len, to || tl_len == show,
  4091. title, page, 0, error, terse);
  4092. *b_size = strlen(*body);
  4093. status = HTTP_STATUS_OK;
  4094. }
  4095. }
  4096. else {
  4097. /** the private timeline **/
  4098. double t = history_mtime(&snac, "timeline.html_");
  4099. /* if enabled by admin, return a cached page if its timestamp is:
  4100. a) newer than the timeline timestamp
  4101. b) newer than the start time of the server
  4102. */
  4103. if (cache && t > timeline_mtime(&snac) && t > p_state->srv_start_time) {
  4104. snac_debug(&snac, 1, xs_fmt("serving cached timeline"));
  4105. status = history_get(&snac, "timeline.html_", body, b_size,
  4106. xs_dict_get(req, "if-none-match"), etag);
  4107. }
  4108. else {
  4109. int more = 0;
  4110. snac_debug(&snac, 1, xs_fmt("building timeline"));
  4111. xs *list = timeline_list(&snac, "private", skip, show, &more);
  4112. *body = html_timeline(&snac, list, 0, skip, show,
  4113. more, NULL, "/admin", 1, error, terse);
  4114. *b_size = strlen(*body);
  4115. status = HTTP_STATUS_OK;
  4116. if (save)
  4117. history_add(&snac, "timeline.html_", *body, *b_size, etag);
  4118. timeline_add_mark(&snac);
  4119. }
  4120. }
  4121. }
  4122. }
  4123. else
  4124. if (xs_startswith(p_path, "admin/p/")) { /** unique post by md5 **/
  4125. if (!login(&snac, req)) {
  4126. *body = xs_dup(uid);
  4127. status = HTTP_STATUS_UNAUTHORIZED;
  4128. }
  4129. else {
  4130. xs *l = xs_split(p_path, "/");
  4131. const char *md5 = xs_list_get(l, -1);
  4132. if (md5 && *md5 && timeline_here_by_md5(&snac, md5)) {
  4133. xs *list0 = xs_list_append(xs_list_new(), md5);
  4134. xs *list = timeline_top_level(&snac, list0);
  4135. *body = html_timeline(&snac, list, 0, 0, 0, 0, NULL, "/admin", 1, error, terse);
  4136. *b_size = strlen(*body);
  4137. status = HTTP_STATUS_OK;
  4138. }
  4139. }
  4140. }
  4141. else
  4142. if (strcmp(p_path, "people") == 0) { /** the list of people **/
  4143. if (!login(&snac, req)) {
  4144. *body = xs_dup(uid);
  4145. status = HTTP_STATUS_UNAUTHORIZED;
  4146. }
  4147. else {
  4148. *body = html_people(&snac);
  4149. *b_size = strlen(*body);
  4150. status = HTTP_STATUS_OK;
  4151. }
  4152. }
  4153. else
  4154. if (xs_startswith(p_path, "people/")) { /** a single actor **/
  4155. if (!login(&snac, req)) {
  4156. *body = xs_dup(uid);
  4157. status = HTTP_STATUS_UNAUTHORIZED;
  4158. }
  4159. else {
  4160. xs *actor_dict = NULL;
  4161. const char *actor_id = NULL;
  4162. xs *actor = NULL;
  4163. xs_list *page_lst = xs_split_n(p_path, "?", 2);
  4164. xs *page = xs_str_cat(xs_str_new("/"), xs_list_get(page_lst, 0));
  4165. xs_list *l = xs_split_n(page, "/", 3);
  4166. const char *actor_md5 = xs_list_get(l, 2);
  4167. if (valid_status(object_get_by_md5(actor_md5, &actor_dict)) &&
  4168. (actor_id = xs_dict_get(actor_dict, "id")) != NULL &&
  4169. valid_status(actor_get(actor_id, &actor))) {
  4170. int more = 0;
  4171. xs *list = timeline_simple_list(&snac, "private", skip, show, &more);
  4172. *body = html_people_one(&snac, actor_id, list, skip, show, more, page);
  4173. *b_size = strlen(*body);
  4174. status = HTTP_STATUS_OK;
  4175. }
  4176. else {
  4177. *body = xs_dup(uid);
  4178. status = HTTP_STATUS_NOT_FOUND;
  4179. }
  4180. }
  4181. }
  4182. else
  4183. if (strcmp(p_path, "notifications") == 0) { /** the list of notifications **/
  4184. if (!login(&snac, req)) {
  4185. *body = xs_dup(uid);
  4186. status = HTTP_STATUS_UNAUTHORIZED;
  4187. }
  4188. else {
  4189. *body = html_notifications(&snac, skip, show);
  4190. *b_size = strlen(*body);
  4191. status = HTTP_STATUS_OK;
  4192. }
  4193. }
  4194. else
  4195. if (strcmp(p_path, "instance") == 0) { /** instance timeline **/
  4196. if (!login(&snac, req)) {
  4197. *body = xs_dup(uid);
  4198. status = HTTP_STATUS_UNAUTHORIZED;
  4199. }
  4200. else {
  4201. xs *list = timeline_instance_list(skip, show);
  4202. xs *next = timeline_instance_list(skip + show, 1);
  4203. *body = html_timeline(&snac, list, 0, skip, show,
  4204. xs_list_len(next), L("Showing instance timeline"), "/instance", 0, error, terse);
  4205. *b_size = strlen(*body);
  4206. status = HTTP_STATUS_OK;
  4207. }
  4208. }
  4209. else
  4210. if (strcmp(p_path, "pinned") == 0) { /** list of pinned posts **/
  4211. if (!login(&snac, req)) {
  4212. *body = xs_dup(uid);
  4213. status = HTTP_STATUS_UNAUTHORIZED;
  4214. }
  4215. else {
  4216. xs *list = pinned_list(&snac);
  4217. *body = html_timeline(&snac, list, 0, skip, show,
  4218. 0, L("Pinned posts"), "", 0, error, terse);
  4219. *b_size = strlen(*body);
  4220. status = HTTP_STATUS_OK;
  4221. }
  4222. }
  4223. else
  4224. if (strcmp(p_path, "bookmarks") == 0) { /** list of bookmarked posts **/
  4225. if (!login(&snac, req)) {
  4226. *body = xs_dup(uid);
  4227. status = HTTP_STATUS_UNAUTHORIZED;
  4228. }
  4229. else {
  4230. xs *list = bookmark_list(&snac);
  4231. *body = html_timeline(&snac, list, 0, skip, show,
  4232. 0, L("Bookmarked posts"), "", 0, error, terse);
  4233. *b_size = strlen(*body);
  4234. status = HTTP_STATUS_OK;
  4235. }
  4236. }
  4237. else
  4238. if (strcmp(p_path, "drafts") == 0) { /** list of drafts **/
  4239. if (!login(&snac, req)) {
  4240. *body = xs_dup(uid);
  4241. status = HTTP_STATUS_UNAUTHORIZED;
  4242. }
  4243. else {
  4244. xs *list = draft_list(&snac);
  4245. *body = html_timeline(&snac, list, 0, skip, show,
  4246. 0, L("Post drafts"), "", 0, error, terse);
  4247. *b_size = strlen(*body);
  4248. status = HTTP_STATUS_OK;
  4249. }
  4250. }
  4251. else
  4252. if (strcmp(p_path, "sched") == 0) { /** list of scheduled posts **/
  4253. if (!login(&snac, req)) {
  4254. *body = xs_dup(uid);
  4255. status = HTTP_STATUS_UNAUTHORIZED;
  4256. }
  4257. else {
  4258. xs *list = scheduled_list(&snac);
  4259. *body = html_timeline(&snac, list, 0, skip, show,
  4260. 0, L("Scheduled posts"), "", 0, error, terse);
  4261. *b_size = strlen(*body);
  4262. status = HTTP_STATUS_OK;
  4263. }
  4264. }
  4265. else
  4266. if (xs_startswith(p_path, "list/")) { /** list timelines **/
  4267. if (!login(&snac, req)) {
  4268. *body = xs_dup(uid);
  4269. status = HTTP_STATUS_UNAUTHORIZED;
  4270. }
  4271. else {
  4272. xs *l = xs_split(p_path, "/");
  4273. const char *lid = xs_list_get(l, -1);
  4274. xs *list = list_timeline(&snac, lid, skip, show);
  4275. xs *next = list_timeline(&snac, lid, skip + show, 1);
  4276. if (list != NULL) {
  4277. xs *ttl = timeline_top_level(&snac, list);
  4278. xs *base = xs_fmt("/list/%s", lid);
  4279. xs *name = list_maint(&snac, lid, 3);
  4280. xs *title = xs_fmt(L("Showing timeline for list '%s'"), name);
  4281. *body = html_timeline(&snac, ttl, 0, skip, show,
  4282. xs_list_len(next), title, base, 1, error, terse);
  4283. *b_size = strlen(*body);
  4284. status = HTTP_STATUS_OK;
  4285. }
  4286. }
  4287. }
  4288. else
  4289. if (xs_startswith(p_path, "p/")) { /** a timeline with just one entry **/
  4290. if (xs_type(xs_dict_get(snac.config, "private")) == XSTYPE_TRUE)
  4291. return HTTP_STATUS_FORBIDDEN;
  4292. xs *id = xs_fmt("%s/%s", snac.actor, p_path);
  4293. xs *msg = NULL;
  4294. if (valid_status(object_get(id, &msg)) && is_msg_public(msg)) {
  4295. xs *md5 = xs_md5_hex(id, strlen(id));
  4296. xs *list = xs_list_new();
  4297. list = xs_list_append(list, md5);
  4298. *body = html_timeline(&snac, list, 1, 0, 0, 0, NULL, "", 1, error, terse);
  4299. *b_size = strlen(*body);
  4300. status = HTTP_STATUS_OK;
  4301. }
  4302. }
  4303. else
  4304. if (xs_startswith(p_path, "s/")) { /** a static file **/
  4305. xs *l = xs_split_n(p_path, "/", 1);
  4306. const char *id = xs_list_get(l, 1);
  4307. int sz;
  4308. if (id && *id) {
  4309. status = static_get(&snac, id, body, &sz,
  4310. xs_dict_get(req, "if-none-match"), etag);
  4311. if (valid_status(status)) {
  4312. *b_size = sz;
  4313. *ctype = (char *)xs_mime_by_ext(id);
  4314. }
  4315. }
  4316. }
  4317. else
  4318. if (xs_startswith(p_path, "h/")) { /** an entry from the history **/
  4319. if (xs_type(xs_dict_get(snac.config, "private")) == XSTYPE_TRUE)
  4320. return HTTP_STATUS_FORBIDDEN;
  4321. if (xs_type(xs_dict_get(srv_config, "disable_history")) == XSTYPE_TRUE)
  4322. return HTTP_STATUS_FORBIDDEN;
  4323. xs *l = xs_split(p_path, "/");
  4324. const char *id = xs_list_get(l, 1);
  4325. if (id && *id) {
  4326. if (xs_endswith(id, "timeline.html_")) {
  4327. /* Don't let them in */
  4328. *b_size = 0;
  4329. status = HTTP_STATUS_NOT_FOUND;
  4330. }
  4331. else
  4332. status = history_get(&snac, id, body, b_size,
  4333. xs_dict_get(req, "if-none-match"), etag);
  4334. }
  4335. }
  4336. else
  4337. if (strcmp(p_path, ".rss") == 0) { /** public timeline in RSS format **/
  4338. if (xs_type(xs_dict_get(snac.config, "private")) == XSTYPE_TRUE)
  4339. return HTTP_STATUS_FORBIDDEN;
  4340. int cnt = xs_number_get(xs_dict_get_def(srv_config, "max_public_entries", "20"));
  4341. xs *elems = timeline_simple_list(&snac, "public", 0, cnt, NULL);
  4342. xs *bio = xs_dup(xs_dict_get(snac.config, "bio"));
  4343. xs *rss_title = xs_fmt("%s (@%s@%s)",
  4344. xs_dict_get(snac.config, "name"),
  4345. snac.uid,
  4346. xs_dict_get(srv_config, "host"));
  4347. xs *rss_link = xs_fmt("%s.rss", snac.actor);
  4348. *body = rss_from_timeline(&snac, elems, rss_title, rss_link, bio);
  4349. *b_size = strlen(*body);
  4350. *ctype = "application/rss+xml; charset=utf-8";
  4351. status = HTTP_STATUS_OK;
  4352. snac_debug(&snac, 1, xs_fmt("serving RSS"));
  4353. }
  4354. else
  4355. if (proxy && (xs_startswith(p_path, "x/") || xs_startswith(p_path, "y/"))) { /** remote media by proxy **/
  4356. xs *proxy_prefix = NULL;
  4357. if (xs_startswith(p_path, "x/")) {
  4358. /* proxy usage authorized by http basic auth */
  4359. if (login(&snac, req))
  4360. proxy_prefix = xs_str_new("x/");
  4361. else {
  4362. *body = xs_dup(uid);
  4363. status = HTTP_STATUS_UNAUTHORIZED;
  4364. }
  4365. }
  4366. else {
  4367. /* proxy usage authorized by proxy_token */
  4368. xs *tks = xs_fmt("%s:%s", srv_proxy_token_seed, snac.actor);
  4369. xs *tk = xs_md5_hex(tks, strlen(tks));
  4370. xs *p = xs_fmt("y/%s/", tk);
  4371. if (xs_startswith(p_path, p))
  4372. proxy_prefix = xs_dup(p);
  4373. }
  4374. if (proxy_prefix) {
  4375. /* pick the raw path (including optional ? arguments) */
  4376. const char *raw_path = xs_dict_get(req, "raw_path");
  4377. /* skip to where the proxy/ string starts */
  4378. raw_path += xs_str_in(raw_path, proxy_prefix);
  4379. xs *url = xs_replace_n(raw_path, proxy_prefix, "https:/" "/", 1);
  4380. xs *hdrs = xs_dict_new();
  4381. hdrs = xs_dict_append(hdrs, "user-agent", USER_AGENT);
  4382. const char *ims = xs_dict_get(req, "if-modified-since");
  4383. const char *inm = xs_dict_get(req, "if-none-match");
  4384. if (ims) hdrs = xs_dict_append(hdrs, "if-modified-since", ims);
  4385. if (inm) hdrs = xs_dict_append(hdrs, "if-none-match", inm);
  4386. xs *rsp = xs_http_request("GET", url, hdrs,
  4387. NULL, 0, &status, body, b_size, 0);
  4388. if (valid_status(status)) {
  4389. const char *ct = xs_or(xs_dict_get(rsp, "content-type"), "");
  4390. const char *lm = xs_dict_get(rsp, "last-modified");
  4391. const char *et = xs_dict_get(rsp, "etag");
  4392. if (lm) *last_modified = xs_dup(lm);
  4393. if (et) *etag = xs_dup(et);
  4394. /* find the content-type in the static mime types,
  4395. and return that value instead of ct, which will
  4396. be destroyed when out of scope */
  4397. for (int n = 0; xs_mime_types[n]; n += 2) {
  4398. if (strcmp(ct, xs_mime_types[n + 1]) == 0) {
  4399. *ctype = (char *)xs_mime_types[n + 1];
  4400. break;
  4401. }
  4402. }
  4403. }
  4404. snac_debug(&snac, 1, xs_fmt("Proxy for %s %d", url, status));
  4405. }
  4406. }
  4407. else
  4408. if (strcmp(p_path, "share") == 0) { /** direct post **/
  4409. if (!login(&snac, req)) {
  4410. *body = xs_dup(uid);
  4411. status = HTTP_STATUS_UNAUTHORIZED;
  4412. }
  4413. else {
  4414. const char *b64 = xs_dict_get(q_vars, "content");
  4415. xs *b64_2 = xs_replace(b64, " ", "+");
  4416. int sz;
  4417. xs *content = xs_base64_dec(b64_2, &sz);
  4418. xs *msg = msg_note(&snac, content, NULL, NULL, NULL, 0, NULL, NULL);
  4419. xs *c_msg = msg_create(&snac, msg);
  4420. timeline_add(&snac, xs_dict_get(msg, "id"), msg);
  4421. enqueue_message(&snac, c_msg);
  4422. snac_debug(&snac, 1, xs_fmt("web action 'share' received"));
  4423. *body = xs_fmt("%s/admin", snac.actor);
  4424. *b_size = strlen(*body);
  4425. status = HTTP_STATUS_SEE_OTHER;
  4426. }
  4427. }
  4428. else
  4429. if (strcmp(p_path, "authorize_interaction") == 0) { /** follow, like or boost from Mastodon **/
  4430. if (!login(&snac, req)) {
  4431. *body = xs_dup(uid);
  4432. status = HTTP_STATUS_UNAUTHORIZED;
  4433. }
  4434. else {
  4435. status = HTTP_STATUS_NOT_FOUND;
  4436. const char *id = xs_dict_get(q_vars, "id");
  4437. const char *action = xs_dict_get(q_vars, "action");
  4438. if (xs_is_string(id) && xs_is_string(action)) {
  4439. if (strcmp(action, "Follow") == 0) {
  4440. xs *msg = msg_follow(&snac, id);
  4441. if (msg != NULL) {
  4442. const char *actor = xs_dict_get(msg, "object");
  4443. following_add(&snac, actor, msg);
  4444. enqueue_output_by_actor(&snac, msg, actor, 0);
  4445. status = HTTP_STATUS_SEE_OTHER;
  4446. }
  4447. }
  4448. else
  4449. if (xs_match(action, "Like|Boost|Announce")) {
  4450. /* bring the post */
  4451. xs *msg = msg_admiration(&snac, id, *action == 'L' ? "Like" : "Announce");
  4452. if (msg != NULL) {
  4453. timeline_admire(&snac, xs_dict_get(msg, "object"), snac.actor, *action == 'L' ? 1 : 0, msg);
  4454. enqueue_message(&snac, msg);
  4455. status = HTTP_STATUS_SEE_OTHER;
  4456. }
  4457. }
  4458. }
  4459. if (status == HTTP_STATUS_SEE_OTHER) {
  4460. *body = xs_fmt("%s/admin", snac.actor);
  4461. *b_size = strlen(*body);
  4462. }
  4463. }
  4464. }
  4465. else
  4466. status = HTTP_STATUS_NOT_FOUND;
  4467. user_free(&snac);
  4468. if (valid_status(status) && *ctype == NULL) {
  4469. *ctype = "text/html; charset=utf-8";
  4470. }
  4471. return status;
  4472. }
  4473. int html_post_handler(const xs_dict *req, const char *q_path,
  4474. char *payload, int p_size,
  4475. char **body, int *b_size, char **ctype)
  4476. {
  4477. (void)p_size;
  4478. (void)ctype;
  4479. int status = 0;
  4480. const snac *user = NULL;
  4481. snac snac;
  4482. const char *uid;
  4483. const char *p_path;
  4484. const xs_dict *p_vars;
  4485. xs *l = xs_split_n(q_path, "/", 2);
  4486. uid = xs_list_get(l, 1);
  4487. if (!uid || !user_open(&snac, uid)) {
  4488. /* invalid user */
  4489. srv_debug(1, xs_fmt("html_post_handler bad user %s", xs_or(uid, "(null)")));
  4490. return HTTP_STATUS_NOT_FOUND;
  4491. }
  4492. p_path = xs_list_get(l, 2);
  4493. /* all posts must be authenticated */
  4494. if (!login(&snac, req)) {
  4495. user_free(&snac);
  4496. *body = xs_dup(uid);
  4497. return HTTP_STATUS_UNAUTHORIZED;
  4498. }
  4499. user = &snac; /* for L() */
  4500. set_user_lang(&snac);
  4501. p_vars = xs_dict_get(req, "p_vars");
  4502. if (p_path && strcmp(p_path, "admin/note") == 0) { /** **/
  4503. snac_debug(&snac, 1, xs_fmt("web action '%s' received", p_path));
  4504. /* post note */
  4505. const char *content = xs_dict_get(p_vars, "content");
  4506. const char *in_reply_to = xs_dict_get(p_vars, "in_reply_to");
  4507. const char *to = xs_dict_get(p_vars, "to");
  4508. const char *sensitive = xs_dict_get(p_vars, "sensitive");
  4509. const char *summary = xs_dict_get(p_vars, "summary");
  4510. const char *edit_id = xs_dict_get(p_vars, "edit_id");
  4511. const char *post_date = xs_dict_get_def(p_vars, "post_date", "");
  4512. const char *post_time = xs_dict_get_def(p_vars, "post_time", "");
  4513. const char *post_lang = xs_dict_get(p_vars, "post_lang");
  4514. const char *visibility = xs_dict_get(p_vars, "visibility");
  4515. int scope = SCOPE_PUBLIC; /* default to public */
  4516. if (!xs_is_null(visibility)) {
  4517. if (strcmp(visibility, "unlisted") == 0)
  4518. scope = SCOPE_UNLISTED;
  4519. else
  4520. if (strcmp(visibility, "followers") == 0)
  4521. scope = SCOPE_FOLLOWERS;
  4522. else
  4523. if (strcmp(visibility, "mentioned") == 0)
  4524. scope = SCOPE_MENTIONED;
  4525. }
  4526. int store_as_draft = !xs_is_null(xs_dict_get(p_vars, "is_draft"));
  4527. xs *attach_list = xs_list_new();
  4528. /* iterate the attachments */
  4529. int max_attachments = xs_number_get(xs_dict_get_def(srv_config, "max_attachments", "4"));
  4530. for (int att_n = 0; att_n < max_attachments; att_n++) {
  4531. xs *url_lbl = xs_fmt("attach_url_%d", att_n);
  4532. xs *att_lbl = xs_fmt("attach_%d", att_n);
  4533. xs *alt_lbl = xs_fmt("alt_text_%d", att_n);
  4534. const char *attach_url = xs_dict_get(p_vars, url_lbl);
  4535. const xs_list *attach_file = xs_dict_get(p_vars, att_lbl);
  4536. const char *alt_text = xs_dict_get_def(p_vars, alt_lbl, "");
  4537. if (xs_is_string(attach_url) && *attach_url != '\0') {
  4538. xs *l = xs_list_new();
  4539. l = xs_list_append(l, attach_url);
  4540. l = xs_list_append(l, alt_text);
  4541. attach_list = xs_list_append(attach_list, l);
  4542. }
  4543. else
  4544. if (xs_is_list(attach_file)) {
  4545. const char *fn = xs_list_get(attach_file, 0);
  4546. if (xs_is_string(fn) && *fn != '\0') {
  4547. char rnd[32];
  4548. xs_rnd_buf(rnd, sizeof(rnd));
  4549. const char *ext = strrchr(fn, '.');
  4550. xs *hash = xs_md5_hex(rnd, sizeof(rnd));
  4551. xs *id = xs_fmt("post-%s%s", hash, ext ? ext : "");
  4552. xs *url = xs_fmt("%s/s/%s", snac.actor, id);
  4553. int fo = xs_number_get(xs_list_get(attach_file, 1));
  4554. int fs = xs_number_get(xs_list_get(attach_file, 2));
  4555. /* store */
  4556. static_put(&snac, id, payload + fo, fs);
  4557. xs *l = xs_list_new();
  4558. l = xs_list_append(l, url);
  4559. l = xs_list_append(l, alt_text);
  4560. attach_list = xs_list_append(attach_list, l);
  4561. }
  4562. }
  4563. }
  4564. if (content != NULL) {
  4565. xs *msg = NULL;
  4566. xs *c_msg = NULL;
  4567. xs *content_2 = xs_replace(content, "\r", "");
  4568. xs *poll_opts = NULL;
  4569. /* is there a valid set of poll options? */
  4570. const char *v = xs_dict_get(p_vars, "poll_options");
  4571. if (!xs_is_null(v) && *v) {
  4572. xs *v2 = xs_strip_i(xs_replace(v, "\r", ""));
  4573. poll_opts = xs_split(v2, "\n");
  4574. }
  4575. if (!xs_is_null(poll_opts) && xs_list_len(poll_opts)) {
  4576. /* get the rest of poll configuration */
  4577. const char *p_multiple = xs_dict_get(p_vars, "poll_multiple");
  4578. const char *p_end_secs = xs_dict_get(p_vars, "poll_end_secs");
  4579. int multiple = 0;
  4580. int end_secs = atoi(!xs_is_null(p_end_secs) ? p_end_secs : "60");
  4581. if (!xs_is_null(p_multiple) && strcmp(p_multiple, "on") == 0)
  4582. multiple = 1;
  4583. msg = msg_question(&snac, content_2, attach_list,
  4584. poll_opts, multiple, end_secs);
  4585. enqueue_close_question(&snac, xs_dict_get(msg, "id"), end_secs);
  4586. }
  4587. else
  4588. msg = msg_note(&snac, content_2, to, in_reply_to, attach_list, scope, post_lang, NULL);
  4589. if (sensitive != NULL) {
  4590. msg = xs_dict_set(msg, "sensitive", xs_stock(XSTYPE_TRUE));
  4591. msg = xs_dict_set(msg, "summary", xs_is_null(summary) ? "..." : summary);
  4592. }
  4593. if (xs_is_string(post_date) && *post_date) {
  4594. xs *post_pubdate = xs_fmt("%sT%s", post_date,
  4595. xs_is_string(post_time) && *post_time ? post_time : "00:00:00");
  4596. time_t t = xs_parse_iso_date(post_pubdate, 0);
  4597. if (t != 0) {
  4598. t -= xs_tz_offset(snac.tz);
  4599. xs *iso_date = xs_str_iso_date(t);
  4600. msg = xs_dict_set(msg, "published", iso_date);
  4601. snac_debug(&snac, 1, xs_fmt("Published date: [%s]", iso_date));
  4602. }
  4603. else
  4604. snac_log(&snac, xs_fmt("Invalid post date: [%s]", post_pubdate));
  4605. }
  4606. /* is the published date from the future? */
  4607. int future_post = 0;
  4608. xs *right_now = xs_str_utctime(0, ISO_DATE_SPEC);
  4609. if (strcmp(xs_dict_get(msg, "published"), right_now) > 0)
  4610. future_post = 1;
  4611. if (xs_is_null(edit_id)) {
  4612. /* new message */
  4613. const char *id = xs_dict_get(msg, "id");
  4614. if (store_as_draft) {
  4615. draft_add(&snac, id, msg);
  4616. }
  4617. else
  4618. if (future_post) {
  4619. schedule_add(&snac, id, msg);
  4620. }
  4621. else {
  4622. c_msg = msg_create(&snac, msg);
  4623. timeline_add(&snac, id, msg);
  4624. }
  4625. }
  4626. else {
  4627. /* an edition of a previous message */
  4628. xs *p_msg = NULL;
  4629. if (valid_status(object_get(edit_id, &p_msg))) {
  4630. /* copy relevant fields from previous version */
  4631. char *fields[] = { "id", "context", "url",
  4632. "cc", "attributedTo",
  4633. "to", "inReplyTo", NULL };
  4634. int n;
  4635. for (n = 0; fields[n]; n++) {
  4636. const char *v = xs_dict_get(p_msg, fields[n]);
  4637. msg = xs_dict_set(msg, fields[n], v);
  4638. }
  4639. if (store_as_draft) {
  4640. draft_add(&snac, edit_id, msg);
  4641. }
  4642. else
  4643. if (is_draft(&snac, edit_id)) {
  4644. /* message was previously a draft; it's a create activity */
  4645. /* if the date is from the past, overwrite it with right_now */
  4646. if (strcmp(xs_dict_get(msg, "published"), right_now) < 0) {
  4647. snac_debug(&snac, 1, xs_fmt("setting draft ancient date to %s", right_now));
  4648. msg = xs_dict_set(msg, "published", right_now);
  4649. }
  4650. /* overwrite object */
  4651. object_add_ow(edit_id, msg);
  4652. if (future_post) {
  4653. schedule_add(&snac, edit_id, msg);
  4654. }
  4655. else {
  4656. c_msg = msg_create(&snac, msg);
  4657. timeline_add(&snac, edit_id, msg);
  4658. }
  4659. draft_del(&snac, edit_id);
  4660. }
  4661. else
  4662. if (is_scheduled(&snac, edit_id)) {
  4663. /* editing an scheduled post; just update it */
  4664. schedule_add(&snac, edit_id, msg);
  4665. }
  4666. else {
  4667. /* ignore the (possibly changed) published date */
  4668. msg = xs_dict_set(msg, "published", xs_dict_get(p_msg, "published"));
  4669. /* set the updated field */
  4670. xs *updated = xs_str_utctime(0, ISO_DATE_SPEC);
  4671. msg = xs_dict_set(msg, "updated", updated);
  4672. /* overwrite object, not updating the indexes */
  4673. object_add_ow(edit_id, msg);
  4674. /* index tags */
  4675. tag_index(edit_id, msg);
  4676. /* update message */
  4677. c_msg = msg_update(&snac, msg);
  4678. }
  4679. }
  4680. else
  4681. snac_log(&snac, xs_fmt("cannot get object '%s' for editing", edit_id));
  4682. }
  4683. if (c_msg != NULL) {
  4684. enqueue_message(&snac, c_msg);
  4685. enqueue_webmention(msg);
  4686. }
  4687. history_del(&snac, "timeline.html_");
  4688. }
  4689. status = HTTP_STATUS_SEE_OTHER;
  4690. }
  4691. else
  4692. if (p_path && strcmp(p_path, "admin/action") == 0) { /** **/
  4693. /* action on an entry */
  4694. const char *id = xs_dict_get(p_vars, "id");
  4695. const char *actor = xs_dict_get(p_vars, "actor");
  4696. const char *action = xs_dict_get(p_vars, "action");
  4697. const char *group = xs_dict_get(p_vars, "group");
  4698. if (action == NULL)
  4699. return HTTP_STATUS_NOT_FOUND;
  4700. snac_debug(&snac, 1, xs_fmt("web action '%s' received", action));
  4701. status = HTTP_STATUS_SEE_OTHER;
  4702. if (strcmp(action, L("EmojiUnreact")) == 0) { /** **/
  4703. const char *eid = xs_dict_get(p_vars, "eid");
  4704. if (eid != NULL) {
  4705. xs *n_msg = msg_emoji_unreact(&snac, id, eid);
  4706. if (n_msg != NULL)
  4707. enqueue_message(&snac, n_msg);
  4708. }
  4709. }
  4710. else
  4711. if (strcmp(action, L("EmojiReact")) == 0) { /** **/
  4712. xs *eid = xs_dup(xs_dict_get(p_vars, "eid"));
  4713. eid = xs_strip_chars_i(eid, ":");
  4714. xs *ret = msg_emoji_init(&snac, id, eid);
  4715. /* fails if either invalid or already reacted */
  4716. if (!ret)
  4717. ret = msg_emoji_unreact(&snac, id, eid);
  4718. if (!ret)
  4719. status = HTTP_STATUS_NOT_FOUND;
  4720. }
  4721. else
  4722. if (strcmp(action, L("Like")) == 0) { /** **/
  4723. xs *msg = msg_admiration(&snac, id, "Like");
  4724. if (msg != NULL) {
  4725. timeline_admire(&snac, xs_dict_get(msg, "object"), snac.actor, 1, msg);
  4726. enqueue_message(&snac, msg);
  4727. }
  4728. }
  4729. else
  4730. if (strcmp(action, L("Boost")) == 0) { /** **/
  4731. xs *msg = msg_admiration(&snac, id, "Announce");
  4732. if (msg != NULL) {
  4733. timeline_admire(&snac, xs_dict_get(msg, "object"), snac.actor, 0, msg);
  4734. enqueue_message(&snac, msg);
  4735. }
  4736. }
  4737. else
  4738. if (strcmp(action, L("Unlike")) == 0) { /** **/
  4739. xs *msg = msg_repulsion(&snac, id, "Like");
  4740. if (msg != NULL) {
  4741. enqueue_message(&snac, msg);
  4742. }
  4743. }
  4744. else
  4745. if (strcmp(action, L("Unboost")) == 0) { /** **/
  4746. xs *msg = msg_repulsion(&snac, id, "Announce");
  4747. if (msg != NULL) {
  4748. enqueue_message(&snac, msg);
  4749. }
  4750. }
  4751. else
  4752. if (strcmp(action, L("MUTE")) == 0) { /** **/
  4753. mute(&snac, actor);
  4754. }
  4755. else
  4756. if (strcmp(action, L("Unmute")) == 0) { /** **/
  4757. unmute(&snac, actor);
  4758. }
  4759. else
  4760. if (strcmp(action, L("Hide")) == 0) { /** **/
  4761. if (is_draft(&snac, id))
  4762. draft_del(&snac, id);
  4763. else
  4764. if (is_scheduled(&snac, id))
  4765. schedule_del(&snac, id);
  4766. else
  4767. hide(&snac, id);
  4768. }
  4769. else
  4770. if (strcmp(action, L("Limit")) == 0) { /** **/
  4771. limit(&snac, actor);
  4772. }
  4773. else
  4774. if (strcmp(action, L("Unlimit")) == 0) { /** **/
  4775. unlimit(&snac, actor);
  4776. }
  4777. else
  4778. if (strcmp(action, L("Follow")) == 0) { /** **/
  4779. xs *msg = msg_follow(&snac, actor);
  4780. if (msg != NULL) {
  4781. /* reload the actor from the message, in may be different */
  4782. actor = xs_dict_get(msg, "object");
  4783. following_add(&snac, actor, msg);
  4784. enqueue_output_by_actor(&snac, msg, actor, 0);
  4785. }
  4786. }
  4787. else
  4788. if (strcmp(action, L("Unfollow")) == 0) { /** **/
  4789. /* get the following object */
  4790. xs *object = NULL;
  4791. if (valid_status(following_get(&snac, actor, &object))) {
  4792. xs *msg = msg_undo(&snac, xs_dict_get(object, "object"));
  4793. following_del(&snac, actor);
  4794. enqueue_output_by_actor(&snac, msg, actor, 0);
  4795. snac_log(&snac, xs_fmt("unfollowed actor %s", actor));
  4796. }
  4797. else
  4798. snac_log(&snac, xs_fmt("actor is not being followed %s", actor));
  4799. }
  4800. else
  4801. if (strcmp(action, L("Follow Group")) == 0) { /** **/
  4802. xs *msg = msg_follow(&snac, group);
  4803. if (msg != NULL) {
  4804. /* reload the group from the message, in may be different */
  4805. group = xs_dict_get(msg, "object");
  4806. following_add(&snac, group, msg);
  4807. enqueue_output_by_actor(&snac, msg, group, 0);
  4808. }
  4809. }
  4810. else
  4811. if (strcmp(action, L("Unfollow Group")) == 0) { /** **/
  4812. /* get the following object */
  4813. xs *object = NULL;
  4814. if (valid_status(following_get(&snac, group, &object))) {
  4815. xs *msg = msg_undo(&snac, xs_dict_get(object, "object"));
  4816. following_del(&snac, group);
  4817. enqueue_output_by_actor(&snac, msg, group, 0);
  4818. snac_log(&snac, xs_fmt("unfollowed group %s", group));
  4819. }
  4820. else
  4821. snac_log(&snac, xs_fmt("actor is not being followed %s", actor));
  4822. }
  4823. else
  4824. if (strcmp(action, L("Delete")) == 0) { /** **/
  4825. const char *actor_form = xs_dict_get(p_vars, "actor-form");
  4826. if (actor_form != NULL) {
  4827. /* delete follower */
  4828. if (valid_status(follower_del(&snac, actor)))
  4829. snac_log(&snac, xs_fmt("deleted follower %s", actor));
  4830. else
  4831. snac_log(&snac, xs_fmt("error deleting follower %s", actor));
  4832. }
  4833. else {
  4834. /* delete an entry */
  4835. if (is_msg_mine(&snac, id) && !is_draft(&snac, id)) {
  4836. /* it's a post by us: generate a delete */
  4837. xs *msg = msg_delete(&snac, id);
  4838. enqueue_message(&snac, msg);
  4839. snac_log(&snac, xs_fmt("posted tombstone for %s", id));
  4840. }
  4841. timeline_del(&snac, id);
  4842. draft_del(&snac, id);
  4843. schedule_del(&snac, id);
  4844. snac_log(&snac, xs_fmt("deleted entry %s", id));
  4845. }
  4846. }
  4847. else
  4848. if (strcmp(action, L("Pin")) == 0) { /** **/
  4849. pin(&snac, id);
  4850. timeline_touch(&snac);
  4851. }
  4852. else
  4853. if (strcmp(action, L("Unpin")) == 0) { /** **/
  4854. unpin(&snac, id);
  4855. timeline_touch(&snac);
  4856. }
  4857. else
  4858. if (strcmp(action, L("Bookmark")) == 0) { /** **/
  4859. bookmark(&snac, id);
  4860. timeline_touch(&snac);
  4861. }
  4862. else
  4863. if (strcmp(action, L("Unbookmark")) == 0) { /** **/
  4864. unbookmark(&snac, id);
  4865. timeline_touch(&snac);
  4866. }
  4867. else
  4868. if (strcmp(action, L("Approve")) == 0) { /** **/
  4869. xs *fwreq = pending_get(&snac, actor);
  4870. if (fwreq != NULL) {
  4871. xs *reply = msg_accept(&snac, fwreq, actor);
  4872. enqueue_message(&snac, reply);
  4873. if (xs_is_null(xs_dict_get(fwreq, "published"))) {
  4874. /* add a date if it doesn't include one (Mastodon) */
  4875. xs *date = xs_str_utctime(0, ISO_DATE_SPEC);
  4876. fwreq = xs_dict_set(fwreq, "published", date);
  4877. }
  4878. timeline_add(&snac, xs_dict_get(fwreq, "id"), fwreq);
  4879. follower_add(&snac, actor);
  4880. pending_del(&snac, actor);
  4881. snac_log(&snac, xs_fmt("new follower %s", actor));
  4882. }
  4883. }
  4884. else
  4885. if (strcmp(action, L("Discard")) == 0) { /** **/
  4886. pending_del(&snac, actor);
  4887. }
  4888. else
  4889. status = HTTP_STATUS_NOT_FOUND;
  4890. /* delete the cached timeline */
  4891. if (status == HTTP_STATUS_SEE_OTHER)
  4892. history_del(&snac, "timeline.html_");
  4893. }
  4894. else
  4895. if (p_path && strcmp(p_path, "admin/user-setup") == 0) { /** **/
  4896. /* change of user data */
  4897. const char *v;
  4898. const char *p1, *p2;
  4899. if ((v = xs_dict_get(p_vars, "name")) != NULL)
  4900. snac.config = xs_dict_set(snac.config, "name", v);
  4901. if ((v = xs_dict_get(p_vars, "avatar")) != NULL)
  4902. snac.config = xs_dict_set(snac.config, "avatar", v);
  4903. if ((v = xs_dict_get(p_vars, "bio")) != NULL)
  4904. snac.config = xs_dict_set(snac.config, "bio", v);
  4905. if ((v = xs_dict_get(p_vars, "cw")) != NULL &&
  4906. strcmp(v, "on") == 0) {
  4907. snac.config = xs_dict_set(snac.config, "cw", "open");
  4908. } else { /* if the checkbox is not set, the parameter is missing */
  4909. snac.config = xs_dict_set(snac.config, "cw", "");
  4910. }
  4911. if ((v = xs_dict_get(p_vars, "email")) != NULL)
  4912. snac.config = xs_dict_set(snac.config, "email", v);
  4913. if ((v = xs_dict_get(p_vars, "telegram_bot")) != NULL)
  4914. snac.config = xs_dict_set(snac.config, "telegram_bot", v);
  4915. if ((v = xs_dict_get(p_vars, "telegram_chat_id")) != NULL)
  4916. snac.config = xs_dict_set(snac.config, "telegram_chat_id", v);
  4917. if ((v = xs_dict_get(p_vars, "ntfy_server")) != NULL)
  4918. snac.config = xs_dict_set(snac.config, "ntfy_server", v);
  4919. if ((v = xs_dict_get(p_vars, "ntfy_token")) != NULL)
  4920. snac.config = xs_dict_set(snac.config, "ntfy_token", v);
  4921. if ((v = xs_dict_get(p_vars, "purge_days")) != NULL) {
  4922. xs *days = xs_number_new(atof(v));
  4923. snac.config = xs_dict_set(snac.config, "purge_days", days);
  4924. }
  4925. if ((v = xs_dict_get(p_vars, "drop_dm_from_unknown")) != NULL && strcmp(v, "on") == 0)
  4926. snac.config = xs_dict_set(snac.config, "drop_dm_from_unknown", xs_stock(XSTYPE_TRUE));
  4927. else
  4928. snac.config = xs_dict_set(snac.config, "drop_dm_from_unknown", xs_stock(XSTYPE_FALSE));
  4929. if ((v = xs_dict_get(p_vars, "bot")) != NULL && strcmp(v, "on") == 0)
  4930. snac.config = xs_dict_set(snac.config, "bot", xs_stock(XSTYPE_TRUE));
  4931. else
  4932. snac.config = xs_dict_set(snac.config, "bot", xs_stock(XSTYPE_FALSE));
  4933. if ((v = xs_dict_get(p_vars, "private")) != NULL && strcmp(v, "on") == 0)
  4934. snac.config = xs_dict_set(snac.config, "private", xs_stock(XSTYPE_TRUE));
  4935. else
  4936. snac.config = xs_dict_set(snac.config, "private", xs_stock(XSTYPE_FALSE));
  4937. if ((v = xs_dict_get(p_vars, "auto_boost")) != NULL && strcmp(v, "on") == 0)
  4938. snac.config = xs_dict_set(snac.config, "auto_boost", xs_stock(XSTYPE_TRUE));
  4939. else
  4940. snac.config = xs_dict_set(snac.config, "auto_boost", xs_stock(XSTYPE_FALSE));
  4941. if ((v = xs_dict_get(p_vars, "collapse_threads")) != NULL && strcmp(v, "on") == 0)
  4942. snac.config = xs_dict_set(snac.config, "collapse_threads", xs_stock(XSTYPE_TRUE));
  4943. else
  4944. snac.config = xs_dict_set(snac.config, "collapse_threads", xs_stock(XSTYPE_FALSE));
  4945. if ((v = xs_dict_get(p_vars, "approve_followers")) != NULL && strcmp(v, "on") == 0)
  4946. snac.config = xs_dict_set(snac.config, "approve_followers", xs_stock(XSTYPE_TRUE));
  4947. else
  4948. snac.config = xs_dict_set(snac.config, "approve_followers", xs_stock(XSTYPE_FALSE));
  4949. if ((v = xs_dict_get(p_vars, "show_contact_metrics")) != NULL && strcmp(v, "on") == 0)
  4950. snac.config = xs_dict_set(snac.config, "show_contact_metrics", xs_stock(XSTYPE_TRUE));
  4951. else
  4952. snac.config = xs_dict_set(snac.config, "show_contact_metrics", xs_stock(XSTYPE_FALSE));
  4953. if ((v = xs_dict_get(p_vars, "show_unlisted")) != NULL && strcmp(v, "on") == 0)
  4954. snac.config = xs_dict_set(snac.config, "show_unlisted", xs_stock(XSTYPE_TRUE));
  4955. else
  4956. snac.config = xs_dict_set(snac.config, "show_unlisted", xs_stock(XSTYPE_FALSE));
  4957. if ((v = xs_dict_get(p_vars, "web_ui_lang")) != NULL)
  4958. snac.config = xs_dict_set(snac.config, "lang", v);
  4959. if ((v = xs_dict_get(p_vars, "tz")) != NULL)
  4960. snac.config = xs_dict_set(snac.config, "tz", v);
  4961. if ((v = xs_dict_get(p_vars, "post_langs")) != NULL)
  4962. snac.config = xs_dict_set(snac.config, "post_langs", v);
  4963. snac.config = xs_dict_set(snac.config, "latitude", xs_dict_get_def(p_vars, "latitude", ""));
  4964. snac.config = xs_dict_set(snac.config, "longitude", xs_dict_get_def(p_vars, "longitude", ""));
  4965. snac.config = xs_dict_set(snac.config, "notify_webhook", xs_dict_get_def(p_vars, "notify_webhook", ""));
  4966. if ((v = xs_dict_get(p_vars, "metadata")) != NULL)
  4967. snac.config = xs_dict_set(snac.config, "metadata", v);
  4968. /* uploads */
  4969. const char *uploads[] = { "avatar", "header", NULL };
  4970. int n;
  4971. for (n = 0; uploads[n]; n++) {
  4972. xs *var_name = xs_fmt("%s_file", uploads[n]);
  4973. const xs_list *uploaded_file = xs_dict_get(p_vars, var_name);
  4974. if (xs_type(uploaded_file) == XSTYPE_LIST) {
  4975. const char *fn = xs_list_get(uploaded_file, 0);
  4976. if (fn && *fn) {
  4977. const char *mimetype = xs_mime_by_ext(fn);
  4978. if (xs_startswith(mimetype, "image/")) {
  4979. const char *ext = strrchr(fn, '.');
  4980. xs *hash = xs_md5_hex(fn, strlen(fn));
  4981. xs *id = xs_fmt("%s-%s%s", uploads[n], hash, ext ? ext : "");
  4982. xs *url = xs_fmt("%s/s/%s", snac.actor, id);
  4983. int fo = xs_number_get(xs_list_get(uploaded_file, 1));
  4984. int fs = xs_number_get(xs_list_get(uploaded_file, 2));
  4985. /* store */
  4986. static_put(&snac, id, payload + fo, fs);
  4987. snac.config = xs_dict_set(snac.config, uploads[n], url);
  4988. }
  4989. }
  4990. }
  4991. }
  4992. /* delete images by removing url from user.json */
  4993. for (n = 0; uploads[n]; n++) {
  4994. xs *var_name = xs_fmt("%s_delete", uploads[n]);
  4995. const char *delete_var = xs_dict_get(p_vars, var_name);
  4996. if (delete_var != NULL && strcmp(delete_var, "on") == 0) {
  4997. snac.config = xs_dict_set(snac.config, uploads[n], "");
  4998. }
  4999. }
  5000. /* password change? */
  5001. if ((p1 = xs_dict_get(p_vars, "passwd1")) != NULL &&
  5002. (p2 = xs_dict_get(p_vars, "passwd2")) != NULL &&
  5003. *p1 && strcmp(p1, p2) == 0) {
  5004. xs *pw = hash_password(snac.uid, p1, NULL);
  5005. snac.config = xs_dict_set(snac.config, "passwd", pw);
  5006. }
  5007. user_persist(&snac, 1);
  5008. status = HTTP_STATUS_SEE_OTHER;
  5009. }
  5010. else
  5011. if (p_path && strcmp(p_path, "admin/filter-notifications") == 0) { /** **/
  5012. notify_filter(&snac, p_vars);
  5013. user_persist(&snac, 0);
  5014. status = HTTP_STATUS_SEE_OTHER;
  5015. }
  5016. else
  5017. if (p_path && strcmp(p_path, "admin/clear-notifications") == 0) { /** **/
  5018. notify_clear(&snac);
  5019. timeline_touch(&snac);
  5020. status = HTTP_STATUS_SEE_OTHER;
  5021. }
  5022. else
  5023. if (p_path && strcmp(p_path, "admin/vote") == 0) { /** **/
  5024. const char *irt = xs_dict_get(p_vars, "irt");
  5025. const char *opt = xs_dict_get(p_vars, "question");
  5026. const char *actor = xs_dict_get(p_vars, "actor");
  5027. xs *ls = NULL;
  5028. /* multiple choices? */
  5029. if (xs_type(opt) == XSTYPE_LIST)
  5030. ls = xs_dup(opt);
  5031. else
  5032. if (xs_type(opt) == XSTYPE_STRING) {
  5033. ls = xs_list_new();
  5034. ls = xs_list_append(ls, opt);
  5035. }
  5036. const xs_str *v;
  5037. int c = 0;
  5038. while (xs_list_next(ls, &v, &c)) {
  5039. xs *msg = msg_note(&snac, "", actor, irt, NULL, 1, NULL, NULL);
  5040. /* set the option */
  5041. msg = xs_dict_append(msg, "name", v);
  5042. /* delete the content */
  5043. msg = xs_dict_del(msg, "content");
  5044. xs *c_msg = msg_create(&snac, msg);
  5045. enqueue_message(&snac, c_msg);
  5046. timeline_add(&snac, xs_dict_get(msg, "id"), msg);
  5047. }
  5048. if (ls != NULL) {
  5049. /* get the poll object */
  5050. xs *poll = NULL;
  5051. if (valid_status(object_get(irt, &poll))) {
  5052. const char *date = xs_dict_get(poll, "endTime");
  5053. if (xs_is_null(date))
  5054. date = xs_dict_get(poll, "closed");
  5055. if (!xs_is_null(date)) {
  5056. time_t t = xs_parse_iso_date(date, 0) - time(NULL);
  5057. /* request the poll when it's closed;
  5058. Pleroma does not send and update when the poll closes */
  5059. enqueue_object_request(&snac, irt, t + 2);
  5060. }
  5061. }
  5062. }
  5063. status = HTTP_STATUS_SEE_OTHER;
  5064. }
  5065. else
  5066. if (p_path && strcmp(p_path, "admin/followed-hashtags") == 0) { /** **/
  5067. const char *followed_hashtags = xs_dict_get(p_vars, "followed_hashtags");
  5068. if (xs_is_null(followed_hashtags))
  5069. followed_hashtags = "";
  5070. if (xs_is_string(followed_hashtags)) {
  5071. xs *new_hashtags = xs_list_new();
  5072. xs *l = xs_split(followed_hashtags, "\n");
  5073. const char *v;
  5074. xs_list_foreach(l, v) {
  5075. xs *s1 = xs_strip_i(xs_dup(v));
  5076. s1 = xs_replace_i(s1, " ", "");
  5077. if (*s1 == '\0')
  5078. continue;
  5079. xs *s2 = NULL;
  5080. if (xs_startswith(s1, "https:/"))
  5081. s2 = xs_dup(s1);
  5082. else {
  5083. s2 = xs_utf8_to_lower(s1);
  5084. if (*s2 != '#')
  5085. s2 = xs_str_prepend_i(s2, "#");
  5086. }
  5087. new_hashtags = xs_list_append(new_hashtags, s2);
  5088. }
  5089. snac.config = xs_dict_set(snac.config, "followed_hashtags", new_hashtags);
  5090. user_persist(&snac, 0);
  5091. }
  5092. status = HTTP_STATUS_SEE_OTHER;
  5093. }
  5094. else
  5095. if (p_path && strcmp(p_path, "admin/blocked-hashtags") == 0) { /** **/
  5096. const char *hashtags = xs_dict_get(p_vars, "blocked_hashtags");
  5097. if (xs_is_null(hashtags))
  5098. hashtags = "";
  5099. if (xs_is_string(hashtags)) {
  5100. xs *new_hashtags = xs_list_new();
  5101. xs *l = xs_split(hashtags, "\n");
  5102. const char *v;
  5103. xs_list_foreach(l, v) {
  5104. xs *s1 = xs_strip_i(xs_dup(v));
  5105. s1 = xs_replace_i(s1, " ", "");
  5106. if (*s1 == '\0')
  5107. continue;
  5108. xs *s2 = xs_utf8_to_lower(s1);
  5109. if (*s2 != '#')
  5110. s2 = xs_str_prepend_i(s2, "#");
  5111. new_hashtags = xs_list_append(new_hashtags, s2);
  5112. }
  5113. snac.config = xs_dict_set(snac.config, "blocked_hashtags", new_hashtags);
  5114. user_persist(&snac, 0);
  5115. }
  5116. status = HTTP_STATUS_SEE_OTHER;
  5117. }
  5118. else
  5119. if (p_path && strcmp(p_path, "admin/muted-words") == 0) {
  5120. const char *words = xs_dict_get(p_vars, "muted_words");
  5121. if (xs_is_string(words)) {
  5122. xs *new_words = xs_list_new();
  5123. xs *l = xs_split(words, "\n");
  5124. const char *v;
  5125. xs_list_foreach(l, v) {
  5126. xs *s1 = xs_strip_i(xs_dup(v));
  5127. s1 = xs_replace_i(s1, " ", "");
  5128. if (*s1 == '\0')
  5129. continue;
  5130. xs *s2 = xs_utf8_to_lower(s1);
  5131. new_words = xs_list_insert_sorted(new_words, s2);
  5132. }
  5133. snac.config = xs_dict_set(snac.config, "muted_words", new_words);
  5134. user_persist(&snac, 0);
  5135. }
  5136. status = HTTP_STATUS_SEE_OTHER;
  5137. }
  5138. if (status == HTTP_STATUS_SEE_OTHER) {
  5139. const char *hard_redir = xs_dict_get(p_vars, "hard-redir");
  5140. if (xs_is_string(hard_redir))
  5141. *body = xs_dup(hard_redir);
  5142. else {
  5143. const char *redir = xs_dict_get(p_vars, "redir");
  5144. if (xs_is_null(redir))
  5145. redir = "top";
  5146. *body = xs_fmt("%s/admin#%s", snac.actor, redir);
  5147. }
  5148. *b_size = strlen(*body);
  5149. }
  5150. user_free(&snac);
  5151. return status;
  5152. }