html.c 191 KB

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