html.c 150 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302
  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 "snac.h"
  16. int login(snac *snac, const xs_dict *headers)
  17. /* tries a login */
  18. {
  19. int logged_in = 0;
  20. const char *auth = xs_dict_get(headers, "authorization");
  21. if (auth && xs_startswith(auth, "Basic ")) {
  22. int sz;
  23. xs *s1 = xs_crop_i(xs_dup(auth), 6, 0);
  24. xs *s2 = xs_base64_dec(s1, &sz);
  25. xs *l1 = xs_split_n(s2, ":", 1);
  26. if (xs_list_len(l1) == 2) {
  27. const char *user = xs_list_get(l1, 0);
  28. const char *pwd = xs_list_get(l1, 1);
  29. const char *addr = xs_or(xs_dict_get(headers, "remote-addr"),
  30. xs_dict_get(headers, "x-forwarded-for"));
  31. if (badlogin_check(user, addr)) {
  32. logged_in = check_password(user, pwd,
  33. xs_dict_get(snac->config, "passwd"));
  34. if (!logged_in)
  35. badlogin_inc(user, addr);
  36. }
  37. }
  38. }
  39. if (logged_in)
  40. lastlog_write(snac, "web");
  41. return logged_in;
  42. }
  43. xs_str *replace_shortnames(xs_str *s, const xs_list *tag, int ems, const char *proxy)
  44. /* replaces all the :shortnames: with the emojis in tag */
  45. {
  46. if (!xs_is_null(tag)) {
  47. xs *tag_list = NULL;
  48. if (xs_type(tag) == XSTYPE_DICT) {
  49. /* not a list */
  50. tag_list = xs_list_new();
  51. tag_list = xs_list_append(tag_list, tag);
  52. } else {
  53. /* is a list */
  54. tag_list = xs_dup(tag);
  55. }
  56. xs *style = xs_fmt("height: %dem; width: %dem; vertical-align: middle;", ems, ems);
  57. const char *v;
  58. int c = 0;
  59. while (xs_list_next(tag_list, &v, &c)) {
  60. const char *t = xs_dict_get(v, "type");
  61. if (t && strcmp(t, "Emoji") == 0) {
  62. const char *n = xs_dict_get(v, "name");
  63. const char *i = xs_dict_get(v, "icon");
  64. if (n && i) {
  65. const char *u = xs_dict_get(i, "url");
  66. xs *url = make_url(u, proxy, 0);
  67. xs_html *img = xs_html_sctag("img",
  68. xs_html_attr("loading", "lazy"),
  69. xs_html_attr("src", url),
  70. xs_html_attr("style", style));
  71. xs *s1 = xs_html_render(img);
  72. s = xs_replace_i(s, n, s1);
  73. }
  74. }
  75. }
  76. }
  77. return s;
  78. }
  79. xs_str *actor_name(xs_dict *actor, const char *proxy)
  80. /* gets the actor name */
  81. {
  82. const char *v;
  83. if (xs_is_null((v = xs_dict_get(actor, "name"))) || *v == '\0') {
  84. if (xs_is_null(v = xs_dict_get(actor, "preferredUsername")) || *v == '\0') {
  85. v = "anonymous";
  86. }
  87. }
  88. return replace_shortnames(xs_html_encode(v), xs_dict_get(actor, "tag"), 1, proxy);
  89. }
  90. xs_html *html_actor_icon(snac *user, xs_dict *actor, const char *date,
  91. const char *udate, const char *url, int priv, int in_people, const char *proxy)
  92. {
  93. xs_html *actor_icon = xs_html_tag("p", NULL);
  94. xs *avatar = NULL;
  95. const char *v;
  96. int fwing = 0;
  97. int fwer = 0;
  98. xs *name = actor_name(actor, proxy);
  99. /* get the avatar */
  100. if ((v = xs_dict_get(actor, "icon")) != NULL) {
  101. /* if it's a list (Peertube), get the first one */
  102. if (xs_type(v) == XSTYPE_LIST)
  103. v = xs_list_get(v, 0);
  104. if ((v = xs_dict_get(v, "url")) != NULL)
  105. avatar = make_url(v, proxy, 0);
  106. }
  107. if (avatar == NULL)
  108. avatar = xs_fmt("data:image/png;base64, %s", default_avatar_base64());
  109. const char *actor_id = xs_dict_get(actor, "id");
  110. xs *href = NULL;
  111. if (user) {
  112. fwer = follower_check(user, actor_id);
  113. fwing = following_check(user, actor_id);
  114. }
  115. if (user && !in_people) {
  116. /* if this actor is a follower or being followed, create an
  117. anchored link to the people page instead of the actor url */
  118. if (fwer || fwing) {
  119. xs *md5 = xs_md5_hex(actor_id, strlen(actor_id));
  120. href = xs_fmt("%s/people#%s", user->actor, md5);
  121. }
  122. }
  123. if (href == NULL)
  124. href = xs_dup(actor_id);
  125. xs_html_add(actor_icon,
  126. xs_html_sctag("img",
  127. xs_html_attr("loading", "lazy"),
  128. xs_html_attr("class", "snac-avatar"),
  129. xs_html_attr("src", avatar),
  130. xs_html_attr("alt", "")),
  131. xs_html_tag("a",
  132. xs_html_attr("href", href),
  133. xs_html_attr("class", "p-author h-card snac-author"),
  134. xs_html_raw(name))); /* name is already html-escaped */
  135. if (!xs_is_null(url)) {
  136. xs *md5 = xs_md5_hex(url, strlen(url));
  137. xs_html_add(actor_icon,
  138. xs_html_text(" "),
  139. xs_html_tag("a",
  140. xs_html_attr("href", (char *)url),
  141. xs_html_attr("title", md5),
  142. xs_html_text("»")));
  143. }
  144. if (strcmp(xs_dict_get(actor, "type"), "Service") == 0) {
  145. xs_html_add(actor_icon,
  146. xs_html_text(" "),
  147. xs_html_tag("span",
  148. xs_html_attr("title", "bot"),
  149. xs_html_raw("🤖")));
  150. }
  151. if (fwing && fwer) {
  152. xs_html_add(actor_icon,
  153. xs_html_text(" "),
  154. xs_html_tag("span",
  155. xs_html_attr("title", "mutual relation"),
  156. xs_html_raw("🤝")));
  157. }
  158. if (priv) {
  159. xs_html_add(actor_icon,
  160. xs_html_text(" "),
  161. xs_html_tag("span",
  162. xs_html_attr("title", "private"),
  163. xs_html_raw("🔒")));
  164. }
  165. if (xs_is_null(date)) {
  166. xs_html_add(actor_icon,
  167. xs_html_raw(" "));
  168. }
  169. else {
  170. xs *date_label = xs_crop_i(xs_dup(date), 0, 10);
  171. xs *date_title = xs_dup(date);
  172. if (!xs_is_null(udate)) {
  173. xs *sd = xs_crop_i(xs_dup(udate), 0, 10);
  174. date_label = xs_str_cat(date_label, " / ", sd);
  175. date_title = xs_str_cat(date_title, " / ", udate);
  176. }
  177. xs_html_add(actor_icon,
  178. xs_html_text(" "),
  179. xs_html_tag("time",
  180. xs_html_attr("class", "dt-published snac-pubdate"),
  181. xs_html_attr("title", date_title),
  182. xs_html_text(date_label)));
  183. }
  184. {
  185. const char *username, *id;
  186. if (xs_is_null(username = xs_dict_get(actor, "preferredUsername")) || *username == '\0') {
  187. /* This should never be reached */
  188. username = "anonymous";
  189. }
  190. if (xs_is_null(id = xs_dict_get(actor, "id")) || *id == '\0') {
  191. /* This should never be reached */
  192. id = "https://social.example.org/anonymous";
  193. }
  194. /* "LIKE AN ANIMAL" */
  195. xs *domain = xs_split(id, "/");
  196. xs *user = xs_fmt("@%s@%s", username, xs_list_get(domain, 2));
  197. xs_html_add(actor_icon,
  198. xs_html_sctag("br", NULL),
  199. xs_html_tag("a",
  200. xs_html_attr("href", xs_dict_get(actor, "id")),
  201. xs_html_attr("class", "p-author-tag h-card snac-author-tag"),
  202. xs_html_text(user)));
  203. }
  204. return actor_icon;
  205. }
  206. xs_html *html_msg_icon(snac *user, const char *actor_id, const xs_dict *msg, const char *proxy)
  207. {
  208. xs *actor = NULL;
  209. xs_html *actor_icon = NULL;
  210. if (actor_id && valid_status(actor_get_refresh(user, actor_id, &actor))) {
  211. const char *date = NULL;
  212. const char *udate = NULL;
  213. const char *url = NULL;
  214. int priv = 0;
  215. const char *type = xs_dict_get(msg, "type");
  216. if (xs_match(type, POSTLIKE_OBJECT_TYPE))
  217. url = xs_dict_get(msg, "id");
  218. priv = !is_msg_public(msg);
  219. date = xs_dict_get(msg, "published");
  220. udate = xs_dict_get(msg, "updated");
  221. actor_icon = html_actor_icon(user, actor, date, udate, url, priv, 0, proxy);
  222. }
  223. return actor_icon;
  224. }
  225. xs_html *html_note(snac *user, const char *summary,
  226. const char *div_id, const char *form_id,
  227. const char *ta_plh, const char *ta_content,
  228. const char *edit_id, const char *actor_id,
  229. const xs_val *cw_yn, const char *cw_text,
  230. const xs_val *mnt_only, const char *redir,
  231. const char *in_reply_to, int poll,
  232. const char *att_file, const char *att_alt_text,
  233. int is_draft)
  234. /* Yes, this is a FUCKTON of arguments and I'm a bit embarrased */
  235. {
  236. xs *action = xs_fmt("%s/admin/note", user->actor);
  237. xs_html *form;
  238. xs_html *note = xs_html_tag("div",
  239. xs_html_tag("details",
  240. xs_html_tag("summary",
  241. xs_html_text(summary)),
  242. xs_html_tag("p", NULL),
  243. xs_html_tag("div",
  244. xs_html_attr("class", "snac-note"),
  245. xs_html_attr("id", div_id),
  246. form = xs_html_tag("form",
  247. xs_html_attr("autocomplete", "off"),
  248. xs_html_attr("method", "post"),
  249. xs_html_attr("action", action),
  250. xs_html_attr("enctype", "multipart/form-data"),
  251. xs_html_attr("id", form_id),
  252. xs_html_tag("textarea",
  253. xs_html_attr("class", "snac-textarea"),
  254. xs_html_attr("name", "content"),
  255. xs_html_attr("rows", "4"),
  256. xs_html_attr("wrap", "virtual"),
  257. xs_html_attr("required", "required"),
  258. xs_html_attr("placeholder", ta_plh),
  259. xs_html_text(ta_content)),
  260. xs_html_tag("p", NULL),
  261. xs_html_text(L("Sensitive content: ")),
  262. xs_html_sctag("input",
  263. xs_html_attr("type", "checkbox"),
  264. xs_html_attr("name", "sensitive"),
  265. xs_html_attr(xs_type(cw_yn) == XSTYPE_TRUE ? "checked" : "", NULL)),
  266. xs_html_sctag("input",
  267. xs_html_attr("type", "text"),
  268. xs_html_attr("name", "summary"),
  269. xs_html_attr("placeholder", L("Sensitive content description")),
  270. xs_html_attr("value", xs_is_null(cw_text) ? "" : cw_text))))));
  271. if (actor_id)
  272. xs_html_add(form,
  273. xs_html_sctag("input",
  274. xs_html_attr("type", "hidden"),
  275. xs_html_attr("name", "to"),
  276. xs_html_attr("value", actor_id)));
  277. else {
  278. /* no actor_id; ask for mentioned_only */
  279. xs_html_add(form,
  280. xs_html_tag("p", NULL),
  281. xs_html_text(L("Only for mentioned people: ")),
  282. xs_html_sctag("input",
  283. xs_html_attr("type", "checkbox"),
  284. xs_html_attr("name", "mentioned_only"),
  285. xs_html_attr(xs_type(mnt_only) == XSTYPE_TRUE ? "checked" : "", NULL)));
  286. }
  287. if (redir)
  288. xs_html_add(form,
  289. xs_html_sctag("input",
  290. xs_html_attr("type", "hidden"),
  291. xs_html_attr("name", "redir"),
  292. xs_html_attr("value", redir)));
  293. if (in_reply_to)
  294. xs_html_add(form,
  295. xs_html_sctag("input",
  296. xs_html_attr("type", "hidden"),
  297. xs_html_attr("name", "in_reply_to"),
  298. xs_html_attr("value", in_reply_to)));
  299. else
  300. xs_html_add(form,
  301. xs_html_tag("p", NULL),
  302. xs_html_text(L("Reply to (URL): ")),
  303. xs_html_sctag("input",
  304. xs_html_attr("type", "url"),
  305. xs_html_attr("name", "in_reply_to"),
  306. xs_html_attr("placeholder", "Optional URL to reply to")));
  307. xs_html_add(form,
  308. xs_html_tag("p", NULL),
  309. xs_html_tag("span",
  310. xs_html_attr("title", L("Don't send, but store as a draft")),
  311. xs_html_text(L("Draft:")),
  312. xs_html_sctag("input",
  313. xs_html_attr("type", "checkbox"),
  314. xs_html_attr("name", "is_draft"),
  315. xs_html_attr(is_draft ? "checked" : "", NULL))));
  316. if (edit_id)
  317. xs_html_add(form,
  318. xs_html_sctag("input",
  319. xs_html_attr("type", "hidden"),
  320. xs_html_attr("name", "edit_id"),
  321. xs_html_attr("value", edit_id)));
  322. /* attachment controls */
  323. xs_html *att;
  324. xs_html_add(form,
  325. xs_html_tag("p", NULL),
  326. att = xs_html_tag("details",
  327. xs_html_tag("summary",
  328. xs_html_text(L("Attachment..."))),
  329. xs_html_tag("p", NULL)));
  330. if (att_file && *att_file)
  331. xs_html_add(att,
  332. xs_html_text(L("File:")),
  333. xs_html_sctag("input",
  334. xs_html_attr("type", "text"),
  335. xs_html_attr("name", "attach_url"),
  336. xs_html_attr("title", L("Clear this field to delete the attachment")),
  337. xs_html_attr("value", att_file)));
  338. else
  339. xs_html_add(att,
  340. xs_html_sctag("input",
  341. xs_html_attr("type", "file"),
  342. xs_html_attr("name", "attach")));
  343. xs_html_add(att,
  344. xs_html_text(" "),
  345. xs_html_sctag("input",
  346. xs_html_attr("type", "text"),
  347. xs_html_attr("name", "alt_text"),
  348. xs_html_attr("value", att_alt_text),
  349. xs_html_attr("placeholder", L("Attachment description"))));
  350. /* add poll controls */
  351. if (poll) {
  352. xs_html_add(form,
  353. xs_html_tag("p", NULL),
  354. xs_html_tag("details",
  355. xs_html_tag("summary",
  356. xs_html_text(L("Poll..."))),
  357. xs_html_tag("p",
  358. xs_html_text(L("Poll options (one per line, up to 8):")),
  359. xs_html_sctag("br", NULL),
  360. xs_html_tag("textarea",
  361. xs_html_attr("class", "snac-textarea"),
  362. xs_html_attr("name", "poll_options"),
  363. xs_html_attr("rows", "4"),
  364. xs_html_attr("wrap", "virtual"),
  365. xs_html_attr("placeholder", "Option 1...\nOption 2...\nOption 3...\n..."))),
  366. xs_html_tag("select",
  367. xs_html_attr("name", "poll_multiple"),
  368. xs_html_tag("option",
  369. xs_html_attr("value", "off"),
  370. xs_html_text(L("One choice"))),
  371. xs_html_tag("option",
  372. xs_html_attr("value", "on"),
  373. xs_html_text(L("Multiple choices")))),
  374. xs_html_text(" "),
  375. xs_html_tag("select",
  376. xs_html_attr("name", "poll_end_secs"),
  377. xs_html_tag("option",
  378. xs_html_attr("value", "300"),
  379. xs_html_text(L("End in 5 minutes"))),
  380. xs_html_tag("option",
  381. xs_html_attr("value", "3600"),
  382. xs_html_attr("selected", NULL),
  383. xs_html_text(L("End in 1 hour"))),
  384. xs_html_tag("option",
  385. xs_html_attr("value", "86400"),
  386. xs_html_text(L("End in 1 day"))))));
  387. }
  388. xs_html_add(form,
  389. xs_html_tag("p", NULL),
  390. xs_html_sctag("input",
  391. xs_html_attr("type", "submit"),
  392. xs_html_attr("class", "button"),
  393. xs_html_attr("value", L("Post"))),
  394. xs_html_tag("p", NULL));
  395. return note;
  396. }
  397. static xs_html *html_base_head(void)
  398. {
  399. xs_html *head = xs_html_tag("head",
  400. xs_html_sctag("meta",
  401. xs_html_attr("name", "viewport"),
  402. xs_html_attr("content", "width=device-width, initial-scale=1")),
  403. xs_html_sctag("meta",
  404. xs_html_attr("name", "generator"),
  405. xs_html_attr("content", USER_AGENT)));
  406. /* add server CSS and favicon */
  407. xs *f;
  408. f = xs_fmt("%s/favicon.ico", srv_baseurl);
  409. const xs_list *p = xs_dict_get(srv_config, "cssurls");
  410. const char *v;
  411. int c = 0;
  412. while (xs_list_next(p, &v, &c)) {
  413. xs_html_add(head,
  414. xs_html_sctag("link",
  415. xs_html_attr("rel", "stylesheet"),
  416. xs_html_attr("type", "text/css"),
  417. xs_html_attr("href", v)));
  418. }
  419. xs_html_add(head,
  420. xs_html_sctag("link",
  421. xs_html_attr("rel", "icon"),
  422. xs_html_attr("type", "image/x-icon"),
  423. xs_html_attr("href", f)));
  424. return head;
  425. }
  426. xs_html *html_instance_head(void)
  427. {
  428. xs_html *head = html_base_head();
  429. {
  430. FILE *f;
  431. xs *g_css_fn = xs_fmt("%s/style.css", srv_basedir);
  432. if ((f = fopen(g_css_fn, "r")) != NULL) {
  433. xs *css = xs_readall(f);
  434. fclose(f);
  435. xs_html_add(head,
  436. xs_html_tag("style",
  437. xs_html_raw(css)));
  438. }
  439. }
  440. const char *host = xs_dict_get(srv_config, "host");
  441. const char *title = xs_dict_get(srv_config, "title");
  442. xs_html_add(head,
  443. xs_html_tag("title",
  444. xs_html_text(title && *title ? title : host)));
  445. return head;
  446. }
  447. static xs_html *html_instance_body(void)
  448. {
  449. const char *host = xs_dict_get(srv_config, "host");
  450. const char *sdesc = xs_dict_get(srv_config, "short_description");
  451. const char *email = xs_dict_get(srv_config, "admin_email");
  452. const char *acct = xs_dict_get(srv_config, "admin_account");
  453. xs *blurb = xs_replace(snac_blurb, "%host%", host);
  454. xs_html *dl;
  455. xs_html *body = xs_html_tag("body",
  456. xs_html_tag("div",
  457. xs_html_attr("class", "snac-instance-blurb"),
  458. xs_html_raw(blurb), /* pure html */
  459. dl = xs_html_tag("dl", NULL)));
  460. if (sdesc && *sdesc) {
  461. xs_html_add(dl,
  462. xs_html_tag("di",
  463. xs_html_tag("dt",
  464. xs_html_text(L("Site description"))),
  465. xs_html_tag("dd",
  466. xs_html_text(sdesc))));
  467. }
  468. if (email && *email) {
  469. xs *mailto = xs_fmt("mailto:%s", email);
  470. xs_html_add(dl,
  471. xs_html_tag("di",
  472. xs_html_tag("dt",
  473. xs_html_text(L("Admin email"))),
  474. xs_html_tag("dd",
  475. xs_html_tag("a",
  476. xs_html_attr("href", mailto),
  477. xs_html_text(email)))));
  478. }
  479. if (acct && *acct) {
  480. xs *url = xs_fmt("%s/%s", srv_baseurl, acct);
  481. xs *handle = xs_fmt("@%s@%s", acct, host);
  482. xs_html_add(dl,
  483. xs_html_tag("di",
  484. xs_html_tag("dt",
  485. xs_html_text(L("Admin account"))),
  486. xs_html_tag("dd",
  487. xs_html_tag("a",
  488. xs_html_attr("href", url),
  489. xs_html_text(handle)))));
  490. }
  491. return body;
  492. }
  493. xs_html *html_user_head(snac *user, const char *desc, const char *url)
  494. {
  495. xs_html *head = html_base_head();
  496. /* add the user CSS */
  497. {
  498. xs *css = NULL;
  499. int size;
  500. /* try to open the user css */
  501. if (!valid_status(static_get(user, "style.css", &css, &size, NULL, NULL))) {
  502. /* it's not there; try to open the server-wide css */
  503. FILE *f;
  504. xs *g_css_fn = xs_fmt("%s/style.css", srv_basedir);
  505. if ((f = fopen(g_css_fn, "r")) != NULL) {
  506. css = xs_readall(f);
  507. fclose(f);
  508. }
  509. }
  510. if (css != NULL) {
  511. xs_html_add(head,
  512. xs_html_tag("style",
  513. xs_html_raw(css)));
  514. }
  515. }
  516. /* title */
  517. xs *title = xs_fmt("%s (@%s@%s)", xs_dict_get(user->config, "name"),
  518. user->uid, xs_dict_get(srv_config, "host"));
  519. xs_html_add(head,
  520. xs_html_tag("title",
  521. xs_html_text(title)));
  522. xs *avatar = xs_dup(xs_dict_get(user->config, "avatar"));
  523. if (avatar == NULL || *avatar == '\0') {
  524. xs_free(avatar);
  525. avatar = xs_fmt("%s/susie.png", srv_baseurl);
  526. }
  527. /* create a description field */
  528. xs *s_desc = NULL;
  529. int n;
  530. if (desc == NULL)
  531. s_desc = xs_dup(xs_dict_get(user->config, "bio"));
  532. else
  533. s_desc = xs_dup(desc);
  534. /* show metrics in og:description? */
  535. if (xs_is_true(xs_dict_get(user->config, "show_contact_metrics"))) {
  536. xs *fwers = follower_list(user);
  537. xs *fwing = following_list(user);
  538. xs *s1 = xs_fmt(L("%d following, %d followers · "),
  539. xs_list_len(fwing), xs_list_len(fwers));
  540. s_desc = xs_str_prepend_i(s_desc, s1);
  541. }
  542. /* shorten desc to a reasonable size */
  543. for (n = 0; s_desc[n]; n++) {
  544. if (n > 512 && (s_desc[n] == ' ' || s_desc[n] == '\n'))
  545. break;
  546. }
  547. s_desc[n] = '\0';
  548. /* og properties */
  549. xs_html_add(head,
  550. xs_html_sctag("meta",
  551. xs_html_attr("property", "og:site_name"),
  552. xs_html_attr("content", xs_dict_get(srv_config, "host"))),
  553. xs_html_sctag("meta",
  554. xs_html_attr("property", "og:title"),
  555. xs_html_attr("content", title)),
  556. xs_html_sctag("meta",
  557. xs_html_attr("property", "og:description"),
  558. xs_html_attr("content", s_desc)),
  559. xs_html_sctag("meta",
  560. xs_html_attr("property", "og:image"),
  561. xs_html_attr("content", avatar)),
  562. xs_html_sctag("meta",
  563. xs_html_attr("property", "og:width"),
  564. xs_html_attr("content", "300")),
  565. xs_html_sctag("meta",
  566. xs_html_attr("property", "og:height"),
  567. xs_html_attr("content", "300")));
  568. /* RSS link */
  569. xs *rss_url = xs_fmt("%s.rss", user->actor);
  570. xs_html_add(head,
  571. xs_html_sctag("link",
  572. xs_html_attr("rel", "alternate"),
  573. xs_html_attr("type", "application/rss+xml"),
  574. xs_html_attr("title", "RSS"),
  575. xs_html_attr("href", rss_url)));
  576. /* ActivityPub alternate link (actor id) */
  577. xs_html_add(head,
  578. xs_html_sctag("link",
  579. xs_html_attr("rel", "alternate"),
  580. xs_html_attr("type", "application/activity+json"),
  581. xs_html_attr("href", url ? url : user->actor)));
  582. return head;
  583. }
  584. static xs_html *html_user_body(snac *user, int read_only)
  585. {
  586. const char *proxy = NULL;
  587. if (user && !read_only && xs_is_true(xs_dict_get(srv_config, "proxy_media")))
  588. proxy = user->actor;
  589. xs_html *body = xs_html_tag("body", NULL);
  590. /* top nav */
  591. xs_html *top_nav = xs_html_tag("nav",
  592. xs_html_attr("class", "snac-top-nav"));
  593. xs *avatar = xs_dup(xs_dict_get(user->config, "avatar"));
  594. if (avatar == NULL || *avatar == '\0') {
  595. xs_free(avatar);
  596. avatar = xs_fmt("data:image/png;base64, %s", default_avatar_base64());
  597. }
  598. xs_html_add(top_nav,
  599. xs_html_sctag("img",
  600. xs_html_attr("src", avatar),
  601. xs_html_attr("class", "snac-avatar"),
  602. xs_html_attr("alt", "")));
  603. if (read_only) {
  604. xs *rss_url = xs_fmt("%s.rss", user->actor);
  605. xs *admin_url = xs_fmt("%s/admin", user->actor);
  606. xs_html_add(top_nav,
  607. xs_html_tag("a",
  608. xs_html_attr("href", rss_url),
  609. xs_html_text(L("RSS"))),
  610. xs_html_text(" - "),
  611. xs_html_tag("a",
  612. xs_html_attr("href", admin_url),
  613. xs_html_attr("rel", "nofollow"),
  614. xs_html_text(L("private"))));
  615. }
  616. else {
  617. int n_len = notify_new_num(user);
  618. xs_html *notify_count = NULL;
  619. /* show the number of new notifications, if there are any */
  620. if (n_len) {
  621. xs *n_len_str = xs_fmt(" %d ", n_len);
  622. notify_count = xs_html_tag("sup",
  623. xs_html_attr("style", "background-color: red; color: white;"),
  624. xs_html_text(n_len_str));
  625. }
  626. else
  627. notify_count = xs_html_text("");
  628. xs *admin_url = xs_fmt("%s/admin", user->actor);
  629. xs *notify_url = xs_fmt("%s/notifications", user->actor);
  630. xs *people_url = xs_fmt("%s/people", user->actor);
  631. xs *instance_url = xs_fmt("%s/instance", user->actor);
  632. xs_html_add(top_nav,
  633. xs_html_tag("a",
  634. xs_html_attr("href", user->actor),
  635. xs_html_text(L("public"))),
  636. xs_html_text(" - "),
  637. xs_html_tag("a",
  638. xs_html_attr("href", admin_url),
  639. xs_html_text(L("private"))),
  640. xs_html_text(" - "),
  641. xs_html_tag("a",
  642. xs_html_attr("href", notify_url),
  643. xs_html_text(L("notifications"))),
  644. notify_count,
  645. xs_html_text(" - "),
  646. xs_html_tag("a",
  647. xs_html_attr("href", people_url),
  648. xs_html_text(L("people"))),
  649. xs_html_text(" - "),
  650. xs_html_tag("a",
  651. xs_html_attr("href", instance_url),
  652. xs_html_text(L("instance"))),
  653. xs_html_text(" "),
  654. xs_html_tag("form",
  655. xs_html_attr("style", "display: inline!important"),
  656. xs_html_attr("class", "snac-search-box"),
  657. xs_html_attr("action", admin_url),
  658. xs_html_sctag("input",
  659. xs_html_attr("type", "text"),
  660. xs_html_attr("name", "q"),
  661. xs_html_attr("title", L("Search posts by URL or content (regular expression), @user@host accounts, or #tag")),
  662. xs_html_attr("placeholder", L("Content search")))));
  663. }
  664. xs_html_add(body,
  665. top_nav);
  666. /* user info */
  667. xs_html *top_user = xs_html_tag("div",
  668. xs_html_attr("class", "h-card snac-top-user"));
  669. if (read_only) {
  670. const char *header = xs_dict_get(user->config, "header");
  671. if (header && *header) {
  672. xs_html_add(top_user,
  673. xs_html_tag("div",
  674. xs_html_attr("class", "snac-top-user-banner"),
  675. xs_html_attr("style", "clear: both"),
  676. xs_html_sctag("br", NULL),
  677. xs_html_sctag("img",
  678. xs_html_attr("src", header))));
  679. }
  680. }
  681. xs *handle = xs_fmt("@%s@%s",
  682. xs_dict_get(user->config, "uid"),
  683. xs_dict_get(srv_config, "host"));
  684. xs_html_add(top_user,
  685. xs_html_tag("p",
  686. xs_html_attr("class", "p-name snac-top-user-name"),
  687. xs_html_text(xs_dict_get(user->config, "name"))),
  688. xs_html_tag("p",
  689. xs_html_attr("class", "snac-top-user-id"),
  690. xs_html_text(handle)));
  691. /** instance announcement **/
  692. if (!read_only) {
  693. double la = 0.0;
  694. xs *user_la = xs_dup(xs_dict_get(user->config, "last_announcement"));
  695. if (user_la != NULL)
  696. la = xs_number_get(user_la);
  697. const t_announcement *an = announcement(la);
  698. if (an != NULL && (an->text != NULL)) {
  699. xs *s = xs_fmt("?da=%.0f", an->timestamp);
  700. xs_html_add(top_user, xs_html_tag("div",
  701. xs_html_attr("class", "snac-announcement"),
  702. xs_html_text(an->text),
  703. xs_html_text(" "),
  704. xs_html_tag("a",
  705. xs_html_attr("href", s),
  706. xs_html_text("Dismiss"))));
  707. }
  708. }
  709. if (read_only) {
  710. xs *tags = xs_list_new();
  711. xs *bio1 = not_really_markdown(xs_dict_get(user->config, "bio"), NULL, &tags);
  712. xs *bio2 = process_tags(user, bio1, &tags);
  713. xs *bio3 = sanitize(bio2);
  714. bio3 = replace_shortnames(bio3, tags, 2, proxy);
  715. xs_html *top_user_bio = xs_html_tag("div",
  716. xs_html_attr("class", "p-note snac-top-user-bio"),
  717. xs_html_raw(bio3)); /* already sanitized */
  718. xs_html_add(top_user,
  719. top_user_bio);
  720. xs *metadata = NULL;
  721. const xs_dict *md = xs_dict_get(user->config, "metadata");
  722. if (xs_type(md) == XSTYPE_DICT)
  723. metadata = xs_dup(md);
  724. else
  725. if (xs_type(md) == XSTYPE_STRING) {
  726. /* convert to dict for easier iteration */
  727. metadata = xs_dict_new();
  728. xs *l = xs_split(md, "\n");
  729. const char *ll;
  730. xs_list_foreach(l, ll) {
  731. xs *kv = xs_split_n(ll, "=", 1);
  732. const char *k = xs_list_get(kv, 0);
  733. const char *v = xs_list_get(kv, 1);
  734. if (k && v) {
  735. xs *kk = xs_strip_i(xs_dup(k));
  736. xs *vv = xs_strip_i(xs_dup(v));
  737. metadata = xs_dict_set(metadata, kk, vv);
  738. }
  739. }
  740. }
  741. if (xs_type(metadata) == XSTYPE_DICT) {
  742. const xs_str *k;
  743. const xs_str *v;
  744. xs_dict *val_links = user->links;
  745. if (xs_is_null(val_links))
  746. val_links = xs_stock(XSTYPE_DICT);
  747. xs_html *snac_metadata = xs_html_tag("div",
  748. xs_html_attr("class", "snac-metadata"));
  749. int c = 0;
  750. while (xs_dict_next(metadata, &k, &v, &c)) {
  751. xs_html *value;
  752. if (xs_startswith(v, "https:/") || xs_startswith(v, "http:/")) {
  753. /* is this link validated? */
  754. xs *verified_link = NULL;
  755. const xs_number *val_time = xs_dict_get(val_links, v);
  756. if (xs_type(val_time) == XSTYPE_NUMBER) {
  757. time_t t = xs_number_get(val_time);
  758. if (t > 0) {
  759. xs *s1 = xs_str_utctime(t, ISO_DATE_SPEC);
  760. verified_link = xs_fmt("%s (%s)", L("verified link"), s1);
  761. }
  762. }
  763. if (!xs_is_null(verified_link)) {
  764. value = xs_html_tag("span",
  765. xs_html_attr("title", verified_link),
  766. xs_html_raw("✔ "),
  767. xs_html_tag("a",
  768. xs_html_attr("rel", "me"),
  769. xs_html_attr("href", v),
  770. xs_html_text(v)));
  771. }
  772. else {
  773. value = xs_html_tag("a",
  774. xs_html_attr("rel", "me"),
  775. xs_html_attr("href", v),
  776. xs_html_text(v));
  777. }
  778. }
  779. else
  780. if (xs_startswith(v, "gemini:/") || xs_startswith(v, "xmpp:")) {
  781. value = xs_html_tag("a",
  782. xs_html_attr("rel", "me"),
  783. xs_html_attr("href", v),
  784. xs_html_text(v));
  785. }
  786. else
  787. value = xs_html_text(v);
  788. xs_html_add(snac_metadata,
  789. xs_html_tag("span",
  790. xs_html_attr("class", "snac-property-name"),
  791. xs_html_text(k)),
  792. xs_html_text(":"),
  793. xs_html_raw(" "),
  794. xs_html_tag("span",
  795. xs_html_attr("class", "snac-property-value"),
  796. value),
  797. xs_html_sctag("br", NULL));
  798. }
  799. xs_html_add(top_user,
  800. snac_metadata);
  801. }
  802. if (xs_is_true(xs_dict_get(user->config, "show_contact_metrics"))) {
  803. xs *fwers = follower_list(user);
  804. xs *fwing = following_list(user);
  805. xs *s1 = xs_fmt(L("%d following %d followers"),
  806. xs_list_len(fwing), xs_list_len(fwers));
  807. xs_html_add(top_user,
  808. xs_html_tag("p",
  809. xs_html_text(s1)));
  810. }
  811. }
  812. xs_html_add(body,
  813. top_user);
  814. return body;
  815. }
  816. xs_html *html_top_controls(snac *snac)
  817. /* generates the top controls */
  818. {
  819. xs *ops_action = xs_fmt("%s/admin/action", snac->actor);
  820. xs_html *top_controls = xs_html_tag("div",
  821. xs_html_attr("class", "snac-top-controls"),
  822. /** new post **/
  823. html_note(snac, L("New Post..."),
  824. "new_post_div", "new_post_form",
  825. L("What's on your mind?"), "",
  826. NULL, NULL,
  827. xs_stock(XSTYPE_FALSE), "",
  828. xs_stock(XSTYPE_FALSE), NULL,
  829. NULL, 1, "", "", 0),
  830. /** operations **/
  831. xs_html_tag("details",
  832. xs_html_tag("summary",
  833. xs_html_text(L("Operations..."))),
  834. xs_html_tag("p", NULL),
  835. xs_html_tag("form",
  836. xs_html_attr("autocomplete", "off"),
  837. xs_html_attr("method", "post"),
  838. xs_html_attr("action", ops_action),
  839. xs_html_sctag("input",
  840. xs_html_attr("type", "text"),
  841. xs_html_attr("name", "actor"),
  842. xs_html_attr("required", "required"),
  843. xs_html_attr("placeholder", "bob@example.com")),
  844. xs_html_text(" "),
  845. xs_html_sctag("input",
  846. xs_html_attr("type", "submit"),
  847. xs_html_attr("name", "action"),
  848. xs_html_attr("value", L("Follow"))),
  849. xs_html_text(" "),
  850. xs_html_text(L("(by URL or user@host)"))),
  851. xs_html_tag("p", NULL),
  852. xs_html_tag("form",
  853. xs_html_attr("autocomplete", "off"),
  854. xs_html_attr("method", "post"),
  855. xs_html_attr("action", ops_action),
  856. xs_html_sctag("input",
  857. xs_html_attr("type", "url"),
  858. xs_html_attr("name", "id"),
  859. xs_html_attr("required", "required"),
  860. xs_html_attr("placeholder", "https:/" "/fedi.example.com/bob/...")),
  861. xs_html_text(" "),
  862. xs_html_sctag("input",
  863. xs_html_attr("type", "submit"),
  864. xs_html_attr("name", "action"),
  865. xs_html_attr("value", L("Boost"))),
  866. xs_html_text(" "),
  867. xs_html_text(L("(by URL)"))),
  868. xs_html_tag("p", NULL),
  869. xs_html_tag("form",
  870. xs_html_attr("autocomplete", "off"),
  871. xs_html_attr("method", "post"),
  872. xs_html_attr("action", ops_action),
  873. xs_html_sctag("input",
  874. xs_html_attr("type", "text"),
  875. xs_html_attr("name", "id"),
  876. xs_html_attr("required", "required"),
  877. xs_html_attr("placeholder", "https:/" "/fedi.example.com/bob/...")),
  878. xs_html_text(" "),
  879. xs_html_sctag("input",
  880. xs_html_attr("type", "submit"),
  881. xs_html_attr("name", "action"),
  882. xs_html_attr("value", L("Like"))),
  883. xs_html_text(" "),
  884. xs_html_text(L("(by URL)"))),
  885. xs_html_tag("p", NULL)));
  886. /** user settings **/
  887. const char *email = "[disabled by admin]";
  888. if (xs_type(xs_dict_get(srv_config, "disable_email_notifications")) != XSTYPE_TRUE) {
  889. email = xs_dict_get(snac->config_o, "email");
  890. if (xs_is_null(email)) {
  891. email = xs_dict_get(snac->config, "email");
  892. if (xs_is_null(email))
  893. email = "";
  894. }
  895. }
  896. const char *cw = xs_dict_get(snac->config, "cw");
  897. if (xs_is_null(cw))
  898. cw = "";
  899. const char *telegram_bot = xs_dict_get(snac->config, "telegram_bot");
  900. if (xs_is_null(telegram_bot))
  901. telegram_bot = "";
  902. const char *telegram_chat_id = xs_dict_get(snac->config, "telegram_chat_id");
  903. if (xs_is_null(telegram_chat_id))
  904. telegram_chat_id = "";
  905. const char *ntfy_server = xs_dict_get(snac->config, "ntfy_server");
  906. if (xs_is_null(ntfy_server))
  907. ntfy_server = "";
  908. const char *ntfy_token = xs_dict_get(snac->config, "ntfy_token");
  909. if (xs_is_null(ntfy_token))
  910. ntfy_token = "";
  911. const char *purge_days = xs_dict_get(snac->config, "purge_days");
  912. if (!xs_is_null(purge_days) && xs_type(purge_days) == XSTYPE_NUMBER)
  913. purge_days = (char *)xs_number_str(purge_days);
  914. else
  915. purge_days = "0";
  916. const xs_val *d_dm_f_u = xs_dict_get(snac->config, "drop_dm_from_unknown");
  917. const xs_val *bot = xs_dict_get(snac->config, "bot");
  918. const xs_val *a_private = xs_dict_get(snac->config, "private");
  919. const xs_val *auto_boost = xs_dict_get(snac->config, "auto_boost");
  920. const xs_val *coll_thrds = xs_dict_get(snac->config, "collapse_threads");
  921. const xs_val *pending = xs_dict_get(snac->config, "approve_followers");
  922. const xs_val *show_foll = xs_dict_get(snac->config, "show_contact_metrics");
  923. const char *latitude = xs_dict_get_def(snac->config, "latitude", "");
  924. const char *longitude = xs_dict_get_def(snac->config, "longitude", "");
  925. xs *metadata = NULL;
  926. const xs_dict *md = xs_dict_get(snac->config, "metadata");
  927. if (xs_type(md) == XSTYPE_DICT) {
  928. const xs_str *k;
  929. const xs_str *v;
  930. metadata = xs_str_new(NULL);
  931. xs_dict_foreach(md, k, v) {
  932. xs *kp = xs_fmt("%s=%s", k, v);
  933. if (*metadata)
  934. metadata = xs_str_cat(metadata, "\n");
  935. metadata = xs_str_cat(metadata, kp);
  936. }
  937. }
  938. else
  939. if (xs_type(md) == XSTYPE_STRING)
  940. metadata = xs_dup(md);
  941. else
  942. metadata = xs_str_new(NULL);
  943. xs *user_setup_action = xs_fmt("%s/admin/user-setup", snac->actor);
  944. xs_html_add(top_controls,
  945. xs_html_tag("details",
  946. xs_html_tag("summary",
  947. xs_html_text(L("User Settings..."))),
  948. xs_html_tag("div",
  949. xs_html_attr("class", "snac-user-setup"),
  950. xs_html_tag("form",
  951. xs_html_attr("autocomplete", "off"),
  952. xs_html_attr("method", "post"),
  953. xs_html_attr("action", user_setup_action),
  954. xs_html_attr("enctype", "multipart/form-data"),
  955. xs_html_tag("p",
  956. xs_html_text(L("Display name:")),
  957. xs_html_sctag("br", NULL),
  958. xs_html_sctag("input",
  959. xs_html_attr("type", "text"),
  960. xs_html_attr("name", "name"),
  961. xs_html_attr("value", xs_dict_get(snac->config, "name")),
  962. xs_html_attr("placeholder", L("Your name")))),
  963. xs_html_tag("p",
  964. xs_html_text(L("Avatar: ")),
  965. xs_html_sctag("input",
  966. xs_html_attr("type", "file"),
  967. xs_html_attr("name", "avatar_file"))),
  968. xs_html_tag("p",
  969. xs_html_sctag("input",
  970. xs_html_attr("type", "checkbox"),
  971. xs_html_attr("name", "avatar_delete")),
  972. xs_html_text(L("Delete current avatar"))),
  973. xs_html_tag("p",
  974. xs_html_text(L("Header image (banner): ")),
  975. xs_html_sctag("input",
  976. xs_html_attr("type", "file"),
  977. xs_html_attr("name", "header_file"))),
  978. xs_html_tag("p",
  979. xs_html_sctag("input",
  980. xs_html_attr("type", "checkbox"),
  981. xs_html_attr("name", "header_delete")),
  982. xs_html_text(L("Delete current header image"))),
  983. xs_html_tag("p",
  984. xs_html_text(L("Bio:")),
  985. xs_html_sctag("br", NULL),
  986. xs_html_tag("textarea",
  987. xs_html_attr("name", "bio"),
  988. xs_html_attr("cols", "40"),
  989. xs_html_attr("rows", "4"),
  990. xs_html_attr("placeholder", L("Write about yourself here...")),
  991. xs_html_text(xs_dict_get(snac->config, "bio")))),
  992. xs_html_sctag("input",
  993. xs_html_attr("type", "checkbox"),
  994. xs_html_attr("name", "cw"),
  995. xs_html_attr("id", "cw"),
  996. xs_html_attr(strcmp(cw, "open") == 0 ? "checked" : "", NULL)),
  997. xs_html_tag("label",
  998. xs_html_attr("for", "cw"),
  999. xs_html_text(L("Always show sensitive content"))),
  1000. xs_html_tag("p",
  1001. xs_html_text(L("Email address for notifications:")),
  1002. xs_html_sctag("br", NULL),
  1003. xs_html_sctag("input",
  1004. xs_html_attr("type", "text"),
  1005. xs_html_attr("name", "email"),
  1006. xs_html_attr("value", email),
  1007. xs_html_attr("placeholder", "bob@example.com"))),
  1008. xs_html_tag("p",
  1009. xs_html_text(L("Telegram notifications (bot key and chat id):")),
  1010. xs_html_sctag("br", NULL),
  1011. xs_html_sctag("input",
  1012. xs_html_attr("type", "text"),
  1013. xs_html_attr("name", "telegram_bot"),
  1014. xs_html_attr("value", telegram_bot),
  1015. xs_html_attr("placeholder", "Bot API key")),
  1016. xs_html_text(" "),
  1017. xs_html_sctag("input",
  1018. xs_html_attr("type", "text"),
  1019. xs_html_attr("name", "telegram_chat_id"),
  1020. xs_html_attr("value", telegram_chat_id),
  1021. xs_html_attr("placeholder", "Chat id"))),
  1022. xs_html_tag("p",
  1023. xs_html_text(L("ntfy notifications (ntfy server and token):")),
  1024. xs_html_sctag("br", NULL),
  1025. xs_html_sctag("input",
  1026. xs_html_attr("type", "text"),
  1027. xs_html_attr("name", "ntfy_server"),
  1028. xs_html_attr("value", ntfy_server),
  1029. xs_html_attr("placeholder", "ntfy server - full URL (example: https://ntfy.sh/YourTopic)")),
  1030. xs_html_text(" "),
  1031. xs_html_sctag("input",
  1032. xs_html_attr("type", "text"),
  1033. xs_html_attr("name", "ntfy_token"),
  1034. xs_html_attr("value", ntfy_token),
  1035. xs_html_attr("placeholder", "ntfy token - if needed"))),
  1036. xs_html_tag("p",
  1037. xs_html_text(L("Maximum days to keep posts (0: server settings):")),
  1038. xs_html_sctag("br", NULL),
  1039. xs_html_sctag("input",
  1040. xs_html_attr("type", "number"),
  1041. xs_html_attr("name", "purge_days"),
  1042. xs_html_attr("value", purge_days))),
  1043. xs_html_tag("p",
  1044. xs_html_sctag("input",
  1045. xs_html_attr("type", "checkbox"),
  1046. xs_html_attr("name", "drop_dm_from_unknown"),
  1047. xs_html_attr("id", "drop_dm_from_unknown"),
  1048. xs_html_attr(xs_type(d_dm_f_u) == XSTYPE_TRUE ? "checked" : "", NULL)),
  1049. xs_html_tag("label",
  1050. xs_html_attr("for", "drop_dm_from_unknown"),
  1051. xs_html_text(L("Drop direct messages from people you don't follow")))),
  1052. xs_html_tag("p",
  1053. xs_html_sctag("input",
  1054. xs_html_attr("type", "checkbox"),
  1055. xs_html_attr("name", "bot"),
  1056. xs_html_attr("id", "bot"),
  1057. xs_html_attr(xs_type(bot) == XSTYPE_TRUE ? "checked" : "", NULL)),
  1058. xs_html_tag("label",
  1059. xs_html_attr("for", "bot"),
  1060. xs_html_text(L("This account is a bot")))),
  1061. xs_html_tag("p",
  1062. xs_html_sctag("input",
  1063. xs_html_attr("type", "checkbox"),
  1064. xs_html_attr("name", "auto_boost"),
  1065. xs_html_attr("id", "auto_boost"),
  1066. xs_html_attr(xs_is_true(auto_boost) ? "checked" : "", NULL)),
  1067. xs_html_tag("label",
  1068. xs_html_attr("for", "auto_boost"),
  1069. xs_html_text(L("Auto-boost all mentions to this account")))),
  1070. xs_html_tag("p",
  1071. xs_html_sctag("input",
  1072. xs_html_attr("type", "checkbox"),
  1073. xs_html_attr("name", "private"),
  1074. xs_html_attr("id", "private"),
  1075. xs_html_attr(xs_type(a_private) == XSTYPE_TRUE ? "checked" : "", NULL)),
  1076. xs_html_tag("label",
  1077. xs_html_attr("for", "private"),
  1078. xs_html_text(L("This account is private "
  1079. "(posts are not shown through the web)")))),
  1080. xs_html_tag("p",
  1081. xs_html_sctag("input",
  1082. xs_html_attr("type", "checkbox"),
  1083. xs_html_attr("name", "collapse_threads"),
  1084. xs_html_attr("id", "collapse_threads"),
  1085. xs_html_attr(xs_is_true(coll_thrds) ? "checked" : "", NULL)),
  1086. xs_html_tag("label",
  1087. xs_html_attr("for", "collapse_threads"),
  1088. xs_html_text(L("Collapse top threads by default")))),
  1089. xs_html_tag("p",
  1090. xs_html_sctag("input",
  1091. xs_html_attr("type", "checkbox"),
  1092. xs_html_attr("name", "approve_followers"),
  1093. xs_html_attr("id", "approve_followers"),
  1094. xs_html_attr(xs_is_true(pending) ? "checked" : "", NULL)),
  1095. xs_html_tag("label",
  1096. xs_html_attr("for", "approve_followers"),
  1097. xs_html_text(L("Follow requests must be approved")))),
  1098. xs_html_tag("p",
  1099. xs_html_sctag("input",
  1100. xs_html_attr("type", "checkbox"),
  1101. xs_html_attr("name", "show_contact_metrics"),
  1102. xs_html_attr("id", "show_contact_metrics"),
  1103. xs_html_attr(xs_is_true(show_foll) ? "checked" : "", NULL)),
  1104. xs_html_tag("label",
  1105. xs_html_attr("for", "show_contact_metrics"),
  1106. xs_html_text(L("Publish follower and following metrics")))),
  1107. xs_html_tag("p",
  1108. xs_html_text(L("Home location:")),
  1109. xs_html_sctag("br", NULL),
  1110. xs_html_sctag("input",
  1111. xs_html_attr("type", "text"),
  1112. xs_html_attr("name", "latitude"),
  1113. xs_html_attr("value", latitude),
  1114. xs_html_attr("placeholder", "latitude")),
  1115. xs_html_text(" "),
  1116. xs_html_sctag("input",
  1117. xs_html_attr("type", "text"),
  1118. xs_html_attr("name", "longitude"),
  1119. xs_html_attr("value", longitude),
  1120. xs_html_attr("placeholder", "longitude"))),
  1121. xs_html_tag("p",
  1122. xs_html_text(L("Profile metadata (key=value pairs in each line):")),
  1123. xs_html_sctag("br", NULL),
  1124. xs_html_tag("textarea",
  1125. xs_html_attr("name", "metadata"),
  1126. xs_html_attr("cols", "40"),
  1127. xs_html_attr("rows", "4"),
  1128. xs_html_attr("placeholder", "Blog=https:/"
  1129. "/example.com/my-blog\nGPG Key=1FA54\n..."),
  1130. xs_html_text(metadata))),
  1131. xs_html_tag("p",
  1132. xs_html_text(L("New password:")),
  1133. xs_html_sctag("br", NULL),
  1134. xs_html_sctag("input",
  1135. xs_html_attr("type", "password"),
  1136. xs_html_attr("name", "passwd1"),
  1137. xs_html_attr("value", ""))),
  1138. xs_html_tag("p",
  1139. xs_html_text(L("Repeat new password:")),
  1140. xs_html_sctag("br", NULL),
  1141. xs_html_sctag("input",
  1142. xs_html_attr("type", "password"),
  1143. xs_html_attr("name", "passwd2"),
  1144. xs_html_attr("value", ""))),
  1145. xs_html_sctag("input",
  1146. xs_html_attr("type", "submit"),
  1147. xs_html_attr("class", "button"),
  1148. xs_html_attr("value", L("Update user info")))))));
  1149. return top_controls;
  1150. }
  1151. static xs_html *html_button(char *clss, char *label, char *hint)
  1152. {
  1153. xs *c = xs_fmt("snac-btn-%s", clss);
  1154. /* use an NULL tag to separate non-css-classed buttons from one another */
  1155. return xs_html_container(
  1156. xs_html_sctag("input",
  1157. xs_html_attr("type", "submit"),
  1158. xs_html_attr("name", "action"),
  1159. xs_html_attr("class", c),
  1160. xs_html_attr("value", label),
  1161. xs_html_attr("title", hint)),
  1162. xs_html_text("\n"));
  1163. }
  1164. xs_str *build_mentions(snac *snac, const xs_dict *msg)
  1165. /* returns a string with the mentions in msg */
  1166. {
  1167. xs_str *s = xs_str_new(NULL);
  1168. const char *list = xs_dict_get(msg, "tag");
  1169. const char *v;
  1170. int c = 0;
  1171. while (xs_list_next(list, &v, &c)) {
  1172. const char *type = xs_dict_get(v, "type");
  1173. const char *href = xs_dict_get(v, "href");
  1174. const char *name = xs_dict_get(v, "name");
  1175. if (type && strcmp(type, "Mention") == 0 &&
  1176. href && strcmp(href, snac->actor) != 0 && name) {
  1177. xs *s1 = NULL;
  1178. if (name[0] != '@') {
  1179. s1 = xs_fmt("@%s", name);
  1180. name = s1;
  1181. }
  1182. xs *l = xs_split(name, "@");
  1183. /* is it a name without a host? */
  1184. if (xs_list_len(l) < 3) {
  1185. /* split the href and pick the host name LIKE AN ANIMAL */
  1186. /* would be better to query the webfinger but *won't do that* here */
  1187. xs *l2 = xs_split(href, "/");
  1188. if (xs_list_len(l2) >= 3) {
  1189. xs *s1 = xs_fmt("%s@%s ", name, xs_list_get(l2, 2));
  1190. if (xs_str_in(s, s1) == -1)
  1191. s = xs_str_cat(s, s1);
  1192. }
  1193. }
  1194. else {
  1195. if (xs_str_in(s, name) == -1) {
  1196. s = xs_str_cat(s, name);
  1197. s = xs_str_cat(s, " ");
  1198. }
  1199. }
  1200. }
  1201. }
  1202. if (*s) {
  1203. xs *s1 = s;
  1204. s = xs_fmt("\n\n\nCC: %s", s1);
  1205. }
  1206. return s;
  1207. }
  1208. xs_html *html_entry_controls(snac *snac, const char *actor,
  1209. const xs_dict *msg, const char *md5)
  1210. {
  1211. const char *id = xs_dict_get(msg, "id");
  1212. const char *group = xs_dict_get(msg, "audience");
  1213. xs *likes = object_likes(id);
  1214. xs *boosts = object_announces(id);
  1215. xs *action = xs_fmt("%s/admin/action", snac->actor);
  1216. xs *redir = xs_fmt("%s_entry", md5);
  1217. xs_html *form;
  1218. xs_html *controls = xs_html_tag("div",
  1219. xs_html_attr("class", "snac-controls"),
  1220. form = xs_html_tag("form",
  1221. xs_html_attr("autocomplete", "off"),
  1222. xs_html_attr("method", "post"),
  1223. xs_html_attr("action", action),
  1224. xs_html_sctag("input",
  1225. xs_html_attr("type", "hidden"),
  1226. xs_html_attr("name", "id"),
  1227. xs_html_attr("value", id)),
  1228. xs_html_sctag("input",
  1229. xs_html_attr("type", "hidden"),
  1230. xs_html_attr("name", "actor"),
  1231. xs_html_attr("value", actor)),
  1232. xs_html_sctag("input",
  1233. xs_html_attr("type", "hidden"),
  1234. xs_html_attr("name", "group"),
  1235. xs_html_attr("value", xs_is_null(group) ? "" : group)),
  1236. xs_html_sctag("input",
  1237. xs_html_attr("type", "hidden"),
  1238. xs_html_attr("name", "redir"),
  1239. xs_html_attr("value", redir))));
  1240. if (!xs_startswith(id, snac->actor)) {
  1241. if (xs_list_in(likes, snac->md5) == -1) {
  1242. /* not already liked; add button */
  1243. xs_html_add(form,
  1244. html_button("like", L("Like"), L("Say you like this post")));
  1245. }
  1246. else {
  1247. /* not like it anymore */
  1248. xs_html_add(form,
  1249. html_button("unlike", L("Unlike"), L("Nah don't like it that much")));
  1250. }
  1251. }
  1252. else {
  1253. if (is_pinned(snac, id))
  1254. xs_html_add(form,
  1255. html_button("unpin", L("Unpin"), L("Unpin this post from your timeline")));
  1256. else
  1257. xs_html_add(form,
  1258. html_button("pin", L("Pin"), L("Pin this post to the top of your timeline")));
  1259. }
  1260. if (is_msg_public(msg)) {
  1261. if (xs_list_in(boosts, snac->md5) == -1) {
  1262. /* not already boosted; add button */
  1263. xs_html_add(form,
  1264. html_button("boost", L("Boost"), L("Announce this post to your followers")));
  1265. }
  1266. else {
  1267. /* already boosted; add button to regret */
  1268. xs_html_add(form,
  1269. html_button("unboost", L("Unboost"), L("I regret I boosted this")));
  1270. }
  1271. }
  1272. if (is_bookmarked(snac, id))
  1273. xs_html_add(form,
  1274. html_button("unbookmark", L("Unbookmark"), L("Delete this post from your bookmarks")));
  1275. else
  1276. xs_html_add(form,
  1277. html_button("bookmark", L("Bookmark"), L("Add this post to your bookmarks")));
  1278. if (strcmp(actor, snac->actor) != 0) {
  1279. /* controls for other actors than this one */
  1280. if (following_check(snac, actor)) {
  1281. xs_html_add(form,
  1282. html_button("unfollow", L("Unfollow"), L("Stop following this user's activity")));
  1283. }
  1284. else {
  1285. xs_html_add(form,
  1286. html_button("follow", L("Follow"), L("Start following this user's activity")));
  1287. }
  1288. if (!xs_is_null(group)) {
  1289. if (following_check(snac, group)) {
  1290. xs_html_add(form,
  1291. html_button("unfollow", L("Unfollow Group"),
  1292. L("Stop following this group or channel")));
  1293. }
  1294. else {
  1295. xs_html_add(form,
  1296. html_button("follow", L("Follow Group"),
  1297. L("Start following this group or channel")));
  1298. }
  1299. }
  1300. xs_html_add(form,
  1301. html_button("mute", L("MUTE"),
  1302. L("Block any activity from this user forever")));
  1303. }
  1304. if (!xs_is_true(xs_dict_get(srv_config, "hide_delete_post_button")))
  1305. xs_html_add(form,
  1306. html_button("delete", L("Delete"), L("Delete this post")));
  1307. xs_html_add(form,
  1308. html_button("hide", L("Hide"), L("Hide this post and its children")));
  1309. const char *prev_src = xs_dict_get(msg, "sourceContent");
  1310. if (!xs_is_null(prev_src) && strcmp(actor, snac->actor) == 0) { /** edit **/
  1311. /* post can be edited */
  1312. xs *div_id = xs_fmt("%s_edit", md5);
  1313. xs *form_id = xs_fmt("%s_edit_form", md5);
  1314. xs *redir = xs_fmt("%s_entry", md5);
  1315. const char *att_file = "";
  1316. const char *att_alt_text = "";
  1317. const xs_list *att_list = xs_dict_get(msg, "attachment");
  1318. /* does it have an attachment? */
  1319. if (xs_type(att_list) == XSTYPE_LIST && xs_list_len(att_list)) {
  1320. const xs_dict *d = xs_list_get(att_list, 0);
  1321. if (xs_type(d) == XSTYPE_DICT) {
  1322. att_file = xs_dict_get_def(d, "url", "");
  1323. att_alt_text = xs_dict_get_def(d, "name", "");
  1324. }
  1325. }
  1326. xs_html_add(controls, xs_html_tag("div",
  1327. xs_html_tag("p", NULL),
  1328. html_note(snac, L("Edit..."),
  1329. div_id, form_id,
  1330. "", prev_src,
  1331. id, NULL,
  1332. xs_dict_get(msg, "sensitive"), xs_dict_get(msg, "summary"),
  1333. xs_stock(is_msg_public(msg) ? XSTYPE_FALSE : XSTYPE_TRUE), redir,
  1334. NULL, 0, att_file, att_alt_text, is_draft(snac, id))),
  1335. xs_html_tag("p", NULL));
  1336. }
  1337. { /** reply **/
  1338. /* the post textarea */
  1339. xs *ct = build_mentions(snac, msg);
  1340. xs *div_id = xs_fmt("%s_reply", md5);
  1341. xs *form_id = xs_fmt("%s_reply_form", md5);
  1342. xs *redir = xs_fmt("%s_entry", md5);
  1343. xs_html_add(controls, xs_html_tag("div",
  1344. xs_html_tag("p", NULL),
  1345. html_note(snac, L("Reply..."),
  1346. div_id, form_id,
  1347. "", ct,
  1348. NULL, NULL,
  1349. xs_dict_get(msg, "sensitive"), xs_dict_get(msg, "summary"),
  1350. xs_stock(is_msg_public(msg) ? XSTYPE_FALSE : XSTYPE_TRUE), redir,
  1351. id, 0, "", "", 0)),
  1352. xs_html_tag("p", NULL));
  1353. }
  1354. return controls;
  1355. }
  1356. xs_html *html_entry(snac *user, xs_dict *msg, int read_only,
  1357. int level, const char *md5, int hide_children)
  1358. {
  1359. const char *id = xs_dict_get(msg, "id");
  1360. const char *type = xs_dict_get(msg, "type");
  1361. const char *actor;
  1362. const char *v;
  1363. int has_title = 0;
  1364. int collapse_threads = 0;
  1365. const char *proxy = NULL;
  1366. if (user && !read_only && xs_is_true(xs_dict_get(srv_config, "proxy_media")))
  1367. proxy = user->actor;
  1368. /* do not show non-public messages in the public timeline */
  1369. if ((read_only || !user) && !is_msg_public(msg))
  1370. return NULL;
  1371. if (id && is_instance_blocked(id))
  1372. return NULL;
  1373. if (user && level == 0 && xs_is_true(xs_dict_get(user->config, "collapse_threads")))
  1374. collapse_threads = 1;
  1375. /* hidden? do nothing more for this conversation */
  1376. if (user && is_hidden(user, id)) {
  1377. xs *s1 = xs_fmt("%s_entry", md5);
  1378. /* return just an dummy anchor, to keep position after hitting 'Hide' */
  1379. return xs_html_tag("div",
  1380. xs_html_tag("a",
  1381. xs_html_attr("name", s1)));
  1382. }
  1383. /* avoid too deep nesting, as it may be a loop */
  1384. if (level >= MAX_CONVERSATION_LEVELS)
  1385. return xs_html_tag("mark",
  1386. xs_html_text(L("Truncated (too deep)")));
  1387. if (strcmp(type, "Follow") == 0) {
  1388. return xs_html_tag("div",
  1389. xs_html_attr("class", "snac-post"),
  1390. xs_html_tag("div",
  1391. xs_html_attr("class", "snac-post-header"),
  1392. xs_html_tag("div",
  1393. xs_html_attr("class", "snac-origin"),
  1394. xs_html_text(L("follows you"))),
  1395. html_msg_icon(read_only ? NULL : user, xs_dict_get(msg, "actor"), msg, proxy)));
  1396. }
  1397. else
  1398. if (!xs_match(type, POSTLIKE_OBJECT_TYPE)) {
  1399. /* skip oddities */
  1400. snac_debug(user, 1, xs_fmt("html_entry: ignoring object type '%s' %s", type, id));
  1401. return NULL;
  1402. }
  1403. /* ignore notes with "name", as they are votes to Questions */
  1404. if (strcmp(type, "Note") == 0 && !xs_is_null(xs_dict_get(msg, "name")))
  1405. return NULL;
  1406. /* get the attributedTo */
  1407. if ((actor = get_atto(msg)) == NULL)
  1408. return NULL;
  1409. /* ignore muted morons immediately */
  1410. if (user && is_muted(user, actor)) {
  1411. xs *s1 = xs_fmt("%s_entry", md5);
  1412. /* return just an dummy anchor, to keep position after hitting 'MUTE' */
  1413. return xs_html_tag("div",
  1414. xs_html_tag("a",
  1415. xs_html_attr("name", s1)));
  1416. }
  1417. if ((user == NULL || strcmp(actor, user->actor) != 0)
  1418. && !valid_status(actor_get(actor, NULL)))
  1419. return NULL;
  1420. /** html_entry top tag **/
  1421. xs_html *entry_top = xs_html_tag("div", NULL);
  1422. {
  1423. xs *s1 = xs_fmt("%s_entry", md5);
  1424. xs_html_add(entry_top,
  1425. xs_html_tag("a",
  1426. xs_html_attr("name", s1)));
  1427. }
  1428. xs_html *entry = xs_html_tag("div",
  1429. xs_html_attr("class", level == 0 ? "snac-post" : "snac-child"));
  1430. xs_html_add(entry_top,
  1431. entry);
  1432. /** post header **/
  1433. xs_html *score;
  1434. xs_html *post_header = xs_html_tag("div",
  1435. xs_html_attr("class", "snac-post-header"),
  1436. score = xs_html_tag("div",
  1437. xs_html_attr("class", "snac-score")));
  1438. xs_html_add(entry,
  1439. post_header);
  1440. if (user && is_pinned(user, id)) {
  1441. /* add a pin emoji */
  1442. xs_html_add(score,
  1443. xs_html_tag("span",
  1444. xs_html_attr("title", L("Pinned")),
  1445. xs_html_raw(" &#128204; ")));
  1446. }
  1447. if (user && is_bookmarked(user, id)) {
  1448. /* add a bookmark emoji */
  1449. xs_html_add(score,
  1450. xs_html_tag("span",
  1451. xs_html_attr("title", L("Bookmarked")),
  1452. xs_html_raw(" &#128278; ")));
  1453. }
  1454. if (strcmp(type, "Question") == 0) {
  1455. /* add the ballot box emoji */
  1456. xs_html_add(score,
  1457. xs_html_tag("span",
  1458. xs_html_attr("title", L("Poll")),
  1459. xs_html_raw(" &#128499; ")));
  1460. if (user && was_question_voted(user, id)) {
  1461. /* add a check to show this poll was voted */
  1462. xs_html_add(score,
  1463. xs_html_tag("span",
  1464. xs_html_attr("title", L("Voted")),
  1465. xs_html_raw(" &#10003; ")));
  1466. }
  1467. }
  1468. if (strcmp(type, "Event") == 0) {
  1469. /* add the calendar emoji */
  1470. xs_html_add(score,
  1471. xs_html_tag("span",
  1472. xs_html_attr("title", L("Event")),
  1473. xs_html_raw(" &#128197; ")));
  1474. }
  1475. /* if it's a user from this same instance, add the score */
  1476. if (xs_startswith(id, srv_baseurl)) {
  1477. int n_likes = object_likes_len(id);
  1478. int n_boosts = object_announces_len(id);
  1479. /* alternate emojis: %d &#128077; %d &#128257; */
  1480. xs *s1 = xs_fmt("%d &#9733; %d &#8634;\n", n_likes, n_boosts);
  1481. xs_html_add(score,
  1482. xs_html_raw(s1));
  1483. }
  1484. xs *boosts = object_announces(id);
  1485. if (xs_list_len(boosts)) {
  1486. /* if somebody boosted this, show as origin */
  1487. const char *p = xs_list_get(boosts, -1);
  1488. xs *actor_r = NULL;
  1489. if (user && xs_list_in(boosts, user->md5) != -1) {
  1490. /* we boosted this */
  1491. xs_html_add(post_header,
  1492. xs_html_tag("div",
  1493. xs_html_attr("class", "snac-origin"),
  1494. xs_html_tag("a",
  1495. xs_html_attr("href", user->actor),
  1496. xs_html_text(xs_dict_get(user->config, "name"))),
  1497. xs_html_text(" "),
  1498. xs_html_text(L("boosted"))));
  1499. }
  1500. else
  1501. if (valid_status(object_get_by_md5(p, &actor_r))) {
  1502. xs *name = actor_name(actor_r, proxy);
  1503. if (!xs_is_null(name)) {
  1504. xs *href = NULL;
  1505. const char *id = xs_dict_get(actor_r, "id");
  1506. int fwers = 0;
  1507. int fwing = 0;
  1508. if (user != NULL) {
  1509. fwers = follower_check(user, id);
  1510. fwing = following_check(user, id);
  1511. }
  1512. if (!read_only && (fwers || fwing))
  1513. href = xs_fmt("%s/people#%s", user->actor, p);
  1514. else
  1515. href = xs_dup(id);
  1516. xs_html_add(post_header,
  1517. xs_html_tag("div",
  1518. xs_html_attr("class", "snac-origin"),
  1519. xs_html_tag("a",
  1520. xs_html_attr("href", href),
  1521. xs_html_raw(name)), /* already sanitized */
  1522. xs_html_text(" "),
  1523. xs_html_text(L("boosted"))));
  1524. }
  1525. }
  1526. }
  1527. else
  1528. if (strcmp(type, "Note") == 0) {
  1529. if (level == 0) {
  1530. /* is the parent not here? */
  1531. const char *parent = get_in_reply_to(msg);
  1532. if (user && !xs_is_null(parent) && *parent && !timeline_here(user, parent)) {
  1533. xs_html_add(post_header,
  1534. xs_html_tag("div",
  1535. xs_html_attr("class", "snac-origin"),
  1536. xs_html_text(L("in reply to")),
  1537. xs_html_text(" "),
  1538. xs_html_tag("a",
  1539. xs_html_attr("href", parent),
  1540. xs_html_text("»"))));
  1541. }
  1542. }
  1543. }
  1544. xs_html_add(post_header,
  1545. html_msg_icon(read_only ? NULL : user, actor, msg, proxy));
  1546. /** post content **/
  1547. xs_html *snac_content_wrap = xs_html_tag("div",
  1548. xs_html_attr("class", "e-content snac-content"));
  1549. xs_html_add(entry,
  1550. snac_content_wrap);
  1551. if (!has_title && !xs_is_null(v = xs_dict_get(msg, "name"))) {
  1552. xs_html_add(snac_content_wrap,
  1553. xs_html_tag("h3",
  1554. xs_html_attr("class", "snac-entry-title"),
  1555. xs_html_text(v)));
  1556. has_title = 1;
  1557. }
  1558. xs_html *snac_content = NULL;
  1559. v = xs_dict_get(msg, "summary");
  1560. /* is it sensitive? */
  1561. if (xs_type(xs_dict_get(msg, "sensitive")) == XSTYPE_TRUE) {
  1562. if (xs_is_null(v) || *v == '\0')
  1563. v = "...";
  1564. const char *cw = "";
  1565. if (user) {
  1566. /* only show it when not in the public timeline and the config setting is "open" */
  1567. cw = xs_dict_get(user->config, "cw");
  1568. if (xs_is_null(cw) || read_only)
  1569. cw = "";
  1570. }
  1571. snac_content = xs_html_tag("details",
  1572. xs_html_attr(cw, NULL),
  1573. xs_html_tag("summary",
  1574. xs_html_text(v),
  1575. xs_html_text(L(" [SENSITIVE CONTENT]"))));
  1576. }
  1577. else {
  1578. /* print the summary as a header (sites like e.g. Friendica can contain one) */
  1579. if (!has_title && !xs_is_null(v) && *v) {
  1580. xs_html_add(snac_content_wrap,
  1581. xs_html_tag("h3",
  1582. xs_html_attr("class", "snac-entry-title"),
  1583. xs_html_text(v)));
  1584. has_title = 1;
  1585. }
  1586. snac_content = xs_html_tag("div", NULL);
  1587. }
  1588. xs_html_add(snac_content_wrap,
  1589. snac_content);
  1590. {
  1591. /** build the content string **/
  1592. const char *content = xs_dict_get(msg, "content");
  1593. if (xs_type(content) != XSTYPE_STRING) {
  1594. if (!xs_is_null(content))
  1595. srv_archive_error("unexpected_content_xstype",
  1596. "content field type", xs_stock(XSTYPE_DICT), msg);
  1597. content = "";
  1598. }
  1599. /* skip ugly line breaks at the beginning */
  1600. while (xs_startswith(content, "<br>"))
  1601. content += 4;
  1602. xs *c = sanitize(content);
  1603. /* do some tweaks to the content */
  1604. c = xs_replace_i(c, "\r", "");
  1605. while (xs_endswith(c, "<br><br>"))
  1606. c = xs_crop_i(c, 0, -4);
  1607. c = xs_replace_i(c, "<br><br>", "<p>");
  1608. c = xs_str_cat(c, "<p>");
  1609. /* replace the :shortnames: */
  1610. c = replace_shortnames(c, xs_dict_get(msg, "tag"), 2, proxy);
  1611. /* Peertube videos content is in markdown */
  1612. const char *mtype = xs_dict_get(msg, "mediaType");
  1613. if (xs_type(mtype) == XSTYPE_STRING && strcmp(mtype, "text/markdown") == 0) {
  1614. /* a full conversion could be better */
  1615. c = xs_replace_i(c, "\r", "");
  1616. c = xs_replace_i(c, "\n", "<br>");
  1617. }
  1618. /* c contains sanitized HTML */
  1619. xs_html_add(snac_content,
  1620. xs_html_raw(c));
  1621. }
  1622. if (strcmp(type, "Question") == 0) { /** question content **/
  1623. const xs_list *oo = xs_dict_get(msg, "oneOf");
  1624. const xs_list *ao = xs_dict_get(msg, "anyOf");
  1625. const xs_list *p;
  1626. const xs_dict *v;
  1627. int closed = 0;
  1628. const char *f_closed = NULL;
  1629. xs_html *poll = xs_html_tag("div", NULL);
  1630. if (read_only)
  1631. closed = 1; /* non-identified page; show as closed */
  1632. else
  1633. if (user && xs_startswith(id, user->actor))
  1634. closed = 1; /* we questioned; closed for us */
  1635. else
  1636. if (user && was_question_voted(user, id))
  1637. closed = 1; /* we already voted; closed for us */
  1638. if ((f_closed = xs_dict_get(msg, "closed")) != NULL) {
  1639. /* it has a closed date... but is it in the past? */
  1640. time_t t0 = time(NULL);
  1641. time_t t1 = xs_parse_iso_date(f_closed, 0);
  1642. if (t1 < t0)
  1643. closed = 2;
  1644. }
  1645. /* get the appropriate list of options */
  1646. p = oo != NULL ? oo : ao;
  1647. if (closed || user == NULL) {
  1648. /* closed poll */
  1649. xs_html *poll_result = xs_html_tag("table",
  1650. xs_html_attr("class", "snac-poll-result"));
  1651. int c = 0;
  1652. while (xs_list_next(p, &v, &c)) {
  1653. const char *name = xs_dict_get(v, "name");
  1654. const xs_dict *replies = xs_dict_get(v, "replies");
  1655. if (name && replies) {
  1656. char *ti = (char *)xs_number_str(xs_dict_get(replies, "totalItems"));
  1657. xs_html_add(poll_result,
  1658. xs_html_tag("tr",
  1659. xs_html_tag("td",
  1660. xs_html_text(name),
  1661. xs_html_text(":")),
  1662. xs_html_tag("td",
  1663. xs_html_text(ti))));
  1664. }
  1665. }
  1666. xs_html_add(poll,
  1667. poll_result);
  1668. }
  1669. else {
  1670. /* poll still active */
  1671. xs *vote_action = xs_fmt("%s/admin/vote", user->actor);
  1672. xs_html *form;
  1673. xs_html *poll_form = xs_html_tag("div",
  1674. xs_html_attr("class", "snac-poll-form"),
  1675. form = xs_html_tag("form",
  1676. xs_html_attr("autocomplete", "off"),
  1677. xs_html_attr("method", "post"),
  1678. xs_html_attr("action", vote_action),
  1679. xs_html_sctag("input",
  1680. xs_html_attr("type", "hidden"),
  1681. xs_html_attr("name", "actor"),
  1682. xs_html_attr("value", actor)),
  1683. xs_html_sctag("input",
  1684. xs_html_attr("type", "hidden"),
  1685. xs_html_attr("name", "irt"),
  1686. xs_html_attr("value", id))));
  1687. int c = 0;
  1688. while (xs_list_next(p, &v, &c)) {
  1689. const char *name = xs_dict_get(v, "name");
  1690. const xs_dict *replies = xs_dict_get(v, "replies");
  1691. if (name) {
  1692. char *ti = (char *)xs_number_str(xs_dict_get(replies, "totalItems"));
  1693. xs_html *btn = xs_html_sctag("input",
  1694. xs_html_attr("id", name),
  1695. xs_html_attr("value", name),
  1696. xs_html_attr("name", "question"));
  1697. if (!xs_is_null(oo)) {
  1698. xs_html_add(btn,
  1699. xs_html_attr("type", "radio"),
  1700. xs_html_attr("required", "required"));
  1701. }
  1702. else
  1703. xs_html_add(btn,
  1704. xs_html_attr("type", "checkbox"));
  1705. xs_html_add(form,
  1706. btn,
  1707. xs_html_text(" "),
  1708. xs_html_tag("span",
  1709. xs_html_attr("title", ti),
  1710. xs_html_text(name)),
  1711. xs_html_sctag("br", NULL));
  1712. }
  1713. }
  1714. xs_html_add(form,
  1715. xs_html_tag("p", NULL),
  1716. xs_html_sctag("input",
  1717. xs_html_attr("type", "submit"),
  1718. xs_html_attr("class", "button"),
  1719. xs_html_attr("value", L("Vote"))));
  1720. xs_html_add(poll,
  1721. poll_form);
  1722. }
  1723. /* if it's *really* closed, say it */
  1724. if (closed == 2) {
  1725. xs_html_add(poll,
  1726. xs_html_tag("p",
  1727. xs_html_text(L("Closed"))));
  1728. }
  1729. else {
  1730. /* show when the poll closes */
  1731. const char *end_time = xs_dict_get(msg, "endTime");
  1732. /* Pleroma does not have an endTime field;
  1733. it has a closed time in the future */
  1734. if (xs_is_null(end_time))
  1735. end_time = xs_dict_get(msg, "closed");
  1736. if (!xs_is_null(end_time)) {
  1737. time_t t0 = time(NULL);
  1738. time_t t1 = xs_parse_iso_date(end_time, 0);
  1739. if (t1 > 0 && t1 > t0) {
  1740. time_t diff_time = t1 - t0;
  1741. xs *tf = xs_str_time_diff(diff_time);
  1742. char *p = tf;
  1743. /* skip leading zeros */
  1744. for (; *p == '0' || *p == ':'; p++);
  1745. xs_html_add(poll,
  1746. xs_html_tag("p",
  1747. xs_html_text(L("Closes in")),
  1748. xs_html_text(" "),
  1749. xs_html_text(p)));
  1750. }
  1751. }
  1752. }
  1753. xs_html_add(snac_content,
  1754. poll);
  1755. }
  1756. /** attachments **/
  1757. xs *attach = get_attachments(msg);
  1758. {
  1759. /* make custom css for attachments easier */
  1760. xs_html *content_attachments = xs_html_tag("div",
  1761. xs_html_attr("class", "snac-content-attachments"));
  1762. xs_html_add(snac_content,
  1763. content_attachments);
  1764. const char *content = xs_dict_get(msg, "content");
  1765. int c = 0;
  1766. const xs_dict *a;
  1767. while (xs_list_next(attach, &a, &c)) {
  1768. const char *type = xs_dict_get(a, "type");
  1769. const char *o_href = xs_dict_get(a, "href");
  1770. const char *name = xs_dict_get(a, "name");
  1771. /* if this image is already in the post content, skip */
  1772. if (content && xs_str_in(content, o_href) != -1)
  1773. continue;
  1774. /* do this attachment include an icon? */
  1775. const xs_dict *icon = xs_dict_get(a, "icon");
  1776. if (xs_type(icon) == XSTYPE_DICT) {
  1777. const char *icon_mtype = xs_dict_get(icon, "mediaType");
  1778. const char *icon_url = xs_dict_get(icon, "url");
  1779. if (icon_mtype && icon_url && xs_startswith(icon_mtype, "image/")) {
  1780. xs_html_add(content_attachments,
  1781. xs_html_tag("a",
  1782. xs_html_attr("href", icon_url),
  1783. xs_html_attr("target", "_blank"),
  1784. xs_html_sctag("img",
  1785. xs_html_attr("loading", "lazy"),
  1786. xs_html_attr("src", icon_url))));
  1787. }
  1788. }
  1789. xs *href = make_url(o_href, proxy, 0);
  1790. if (xs_startswith(type, "image/") || strcmp(type, "Image") == 0) {
  1791. xs_html_add(content_attachments,
  1792. xs_html_tag("a",
  1793. xs_html_attr("href", href),
  1794. xs_html_attr("target", "_blank"),
  1795. xs_html_sctag("img",
  1796. xs_html_attr("loading", "lazy"),
  1797. xs_html_attr("src", href),
  1798. xs_html_attr("alt", name),
  1799. xs_html_attr("title", name))));
  1800. }
  1801. else
  1802. if (xs_startswith(type, "video/")) {
  1803. xs_html_add(content_attachments,
  1804. xs_html_tag("video",
  1805. xs_html_attr("preload", "none"),
  1806. xs_html_attr("style", "width: 100%"),
  1807. xs_html_attr("class", "snac-embedded-video"),
  1808. xs_html_attr("controls", NULL),
  1809. xs_html_attr("src", href),
  1810. xs_html_text(L("Video")),
  1811. xs_html_text(": "),
  1812. xs_html_tag("a",
  1813. xs_html_attr("href", href),
  1814. xs_html_text(name))));
  1815. }
  1816. else
  1817. if (xs_startswith(type, "audio/")) {
  1818. xs_html_add(content_attachments,
  1819. xs_html_tag("audio",
  1820. xs_html_attr("preload", "none"),
  1821. xs_html_attr("style", "width: 100%"),
  1822. xs_html_attr("class", "snac-embedded-audio"),
  1823. xs_html_attr("controls", NULL),
  1824. xs_html_attr("src", href),
  1825. xs_html_text(L("Audio")),
  1826. xs_html_text(": "),
  1827. xs_html_tag("a",
  1828. xs_html_attr("href", href),
  1829. xs_html_text(name))));
  1830. }
  1831. else
  1832. if (strcmp(type, "Link") == 0) {
  1833. xs_html_add(content_attachments,
  1834. xs_html_tag("p",
  1835. xs_html_tag("a",
  1836. xs_html_attr("href", o_href),
  1837. xs_html_text(href))));
  1838. /* do not generate an Alt... */
  1839. name = NULL;
  1840. }
  1841. else {
  1842. xs_html_add(content_attachments,
  1843. xs_html_tag("p",
  1844. xs_html_tag("a",
  1845. xs_html_attr("href", o_href),
  1846. xs_html_text(L("Attachment")),
  1847. xs_html_text(": "),
  1848. xs_html_text(o_href))));
  1849. /* do not generate an Alt... */
  1850. name = NULL;
  1851. }
  1852. if (name != NULL && *name) {
  1853. xs_html_add(content_attachments,
  1854. xs_html_tag("p",
  1855. xs_html_attr("class", "snac-alt-text"),
  1856. xs_html_tag("details",
  1857. xs_html_tag("summary",
  1858. xs_html_text(L("Alt..."))),
  1859. xs_html_text(name))));
  1860. }
  1861. }
  1862. }
  1863. /* has this message an audience (i.e., comes from a channel or community)? */
  1864. const char *audience = xs_dict_get(msg, "audience");
  1865. if (strcmp(type, "Page") == 0 && !xs_is_null(audience)) {
  1866. xs_html *au_tag = xs_html_tag("p",
  1867. xs_html_text("("),
  1868. xs_html_tag("a",
  1869. xs_html_attr("href", audience),
  1870. xs_html_attr("title", L("Source channel or community")),
  1871. xs_html_text(audience)),
  1872. xs_html_text(")"));
  1873. xs_html_add(snac_content_wrap,
  1874. au_tag);
  1875. }
  1876. /* show all hashtags that has not been shown previously in the content */
  1877. const xs_list *tags = xs_dict_get(msg, "tag");
  1878. if (xs_type(tags) == XSTYPE_LIST && xs_list_len(tags)) {
  1879. const char *o_content = xs_dict_get(msg, "content");
  1880. xs *content = xs_utf8_to_lower(o_content);
  1881. const xs_dict *tag;
  1882. xs_html *add_hashtags = xs_html_tag("ul",
  1883. xs_html_attr("class", "snac-more-hashtags"));
  1884. xs_list_foreach(tags, tag) {
  1885. const char *type = xs_dict_get(tag, "type");
  1886. if (xs_type(type) == XSTYPE_STRING && strcmp(type, "Hashtag") == 0) {
  1887. const char *o_href = xs_dict_get(tag, "href");
  1888. if (xs_type(o_href) == XSTYPE_STRING) {
  1889. xs *href = xs_utf8_to_lower(o_href);
  1890. if (xs_str_in(content, href) == -1) {
  1891. /* not in the content: add here */
  1892. const char *name = xs_dict_get(tag, "name");
  1893. xs_html_add(add_hashtags,
  1894. xs_html_tag("li",
  1895. xs_html_tag("a",
  1896. xs_html_attr("href", href),
  1897. xs_html_text(name),
  1898. xs_html_text(" "))));
  1899. }
  1900. }
  1901. }
  1902. }
  1903. xs_html_add(snac_content_wrap,
  1904. add_hashtags);
  1905. }
  1906. /** controls **/
  1907. if (!read_only && user) {
  1908. xs_html_add(entry,
  1909. html_entry_controls(user, actor, msg, md5));
  1910. }
  1911. /** children **/
  1912. if (!hide_children) {
  1913. xs *children = object_children(id);
  1914. int left = xs_list_len(children);
  1915. if (left) {
  1916. xs_html *ch_details = xs_html_tag("details",
  1917. xs_html_attr(collapse_threads ? "" : "open", NULL),
  1918. xs_html_tag("summary",
  1919. xs_html_text("...")));
  1920. xs_html_add(entry,
  1921. ch_details);
  1922. xs_html *ch_container = xs_html_tag("div",
  1923. xs_html_attr("class", level < 4 ? "snac-children" : "snac-children-too-deep"));
  1924. xs_html_add(ch_details,
  1925. ch_container);
  1926. xs_html *ch_older = NULL;
  1927. if (left > 3) {
  1928. xs_html_add(ch_container,
  1929. ch_older = xs_html_tag("details",
  1930. xs_html_tag("summary",
  1931. xs_html_text(L("Older...")))));
  1932. }
  1933. xs_list *p = children;
  1934. const char *cmd5;
  1935. int cnt = 0;
  1936. int o_cnt = 0;
  1937. while (xs_list_iter(&p, &cmd5)) {
  1938. xs *chd = NULL;
  1939. if (user)
  1940. timeline_get_by_md5(user, cmd5, &chd);
  1941. else
  1942. object_get_by_md5(cmd5, &chd);
  1943. if (chd != NULL) {
  1944. if (xs_is_null(xs_dict_get(chd, "name"))) {
  1945. xs_html *che = html_entry(user, chd, read_only,
  1946. level + 1, cmd5, hide_children);
  1947. if (che != NULL) {
  1948. if (left > 3) {
  1949. xs_html_add(ch_older,
  1950. che);
  1951. o_cnt++;
  1952. }
  1953. else
  1954. xs_html_add(ch_container,
  1955. che);
  1956. cnt++;
  1957. }
  1958. }
  1959. left--;
  1960. }
  1961. else
  1962. srv_debug(2, xs_fmt("cannot read child %s", cmd5));
  1963. }
  1964. /* if no children were finally added, hide the details */
  1965. if (cnt == 0)
  1966. xs_html_add(ch_details,
  1967. xs_html_attr("style", "display: none"));
  1968. if (o_cnt == 0 && ch_older)
  1969. xs_html_add(ch_older,
  1970. xs_html_attr("style", "display: none"));
  1971. }
  1972. }
  1973. return entry_top;
  1974. }
  1975. xs_html *html_footer(void)
  1976. {
  1977. return xs_html_tag("div",
  1978. xs_html_attr("class", "snac-footer"),
  1979. xs_html_tag("a",
  1980. xs_html_attr("href", srv_baseurl),
  1981. xs_html_text(L("about this site"))),
  1982. xs_html_text(" - "),
  1983. xs_html_text(L("powered by ")),
  1984. xs_html_tag("a",
  1985. xs_html_attr("href", WHAT_IS_SNAC_URL),
  1986. xs_html_tag("abbr",
  1987. xs_html_attr("title", "Social Networks Are Crap"),
  1988. xs_html_text("snac"))));
  1989. }
  1990. xs_str *html_timeline(snac *user, const xs_list *list, int read_only,
  1991. int skip, int show, int show_more,
  1992. const char *title, const char *page,
  1993. int utl, const char *error)
  1994. /* returns the HTML for the timeline */
  1995. {
  1996. xs_list *p = (xs_list *)list;
  1997. const char *v;
  1998. double t = ftime();
  1999. int hide_children = xs_is_true(xs_dict_get(srv_config, "strict_public_timelines")) && read_only;
  2000. xs *desc = NULL;
  2001. xs *alternate = NULL;
  2002. if (xs_list_len(list) == 1) {
  2003. /* only one element? pick the description from the source */
  2004. const char *id = xs_list_get(list, 0);
  2005. xs *d = NULL;
  2006. object_get_by_md5(id, &d);
  2007. const char *sc = xs_dict_get(d, "sourceContent");
  2008. if (d && sc != NULL)
  2009. desc = xs_dup(sc);
  2010. alternate = xs_dup(xs_dict_get(d, "id"));
  2011. }
  2012. xs_html *head;
  2013. xs_html *body;
  2014. if (user) {
  2015. head = html_user_head(user, desc, alternate);
  2016. body = html_user_body(user, read_only);
  2017. }
  2018. else {
  2019. head = html_instance_head();
  2020. body = html_instance_body();
  2021. }
  2022. xs_html *html = xs_html_tag("html",
  2023. head,
  2024. body);
  2025. if (user && !read_only)
  2026. xs_html_add(body,
  2027. html_top_controls(user));
  2028. if (error != NULL) {
  2029. xs_html_add(body,
  2030. xs_html_tag("dialog",
  2031. xs_html_attr("open", NULL),
  2032. xs_html_tag("p",
  2033. xs_html_text(error)),
  2034. xs_html_tag("form",
  2035. xs_html_attr("method", "dialog"),
  2036. xs_html_sctag("input",
  2037. xs_html_attr("type", "submit"),
  2038. xs_html_attr("value", L("Dismiss"))))));
  2039. }
  2040. /* show links to the available lists */
  2041. if (user && !read_only) {
  2042. xs_html *lol = xs_html_tag("ul",
  2043. xs_html_attr("class", "snac-list-of-lists"));
  2044. xs_html_add(body, lol);
  2045. xs *lists = list_maint(user, NULL, 0); /* get list of lists */
  2046. int ct = 0;
  2047. const char *v;
  2048. while (xs_list_next(lists, &v, &ct)) {
  2049. const char *lname = xs_list_get(v, 1);
  2050. xs *url = xs_fmt("%s/list/%s", user->actor, xs_list_get(v, 0));
  2051. xs *ttl = xs_fmt(L("Timeline for list '%s'"), lname);
  2052. xs_html_add(lol,
  2053. xs_html_tag("li",
  2054. xs_html_tag("a",
  2055. xs_html_attr("href", url),
  2056. xs_html_attr("class", "snac-list-link"),
  2057. xs_html_attr("title", ttl),
  2058. xs_html_text(lname))));
  2059. }
  2060. {
  2061. /* show the list of pinned posts */
  2062. xs *url = xs_fmt("%s/pinned", user->actor);
  2063. xs_html_add(lol,
  2064. xs_html_tag("li",
  2065. xs_html_tag("a",
  2066. xs_html_attr("href", url),
  2067. xs_html_attr("class", "snac-list-link"),
  2068. xs_html_attr("title", L("Pinned posts")),
  2069. xs_html_text("pinned"))));
  2070. }
  2071. {
  2072. /* show the list of bookmarked posts */
  2073. xs *url = xs_fmt("%s/bookmarks", user->actor);
  2074. xs_html_add(lol,
  2075. xs_html_tag("li",
  2076. xs_html_tag("a",
  2077. xs_html_attr("href", url),
  2078. xs_html_attr("class", "snac-list-link"),
  2079. xs_html_attr("title", L("Bookmarked posts")),
  2080. xs_html_text("bookmarks"))));
  2081. }
  2082. {
  2083. /* show the list of drafts */
  2084. xs *url = xs_fmt("%s/drafts", user->actor);
  2085. xs_html_add(lol,
  2086. xs_html_tag("li",
  2087. xs_html_tag("a",
  2088. xs_html_attr("href", url),
  2089. xs_html_attr("class", "snac-list-link"),
  2090. xs_html_attr("title", L("Post drafts")),
  2091. xs_html_text("drafts"))));
  2092. }
  2093. }
  2094. xs_html_add(body,
  2095. xs_html_tag("a",
  2096. xs_html_attr("name", "snac-posts")));
  2097. xs_html *posts = xs_html_tag("div",
  2098. xs_html_attr("class", "snac-posts"));
  2099. if (title) {
  2100. xs_html_add(posts,
  2101. xs_html_tag("h2",
  2102. xs_html_attr("class", "snac-header"),
  2103. xs_html_text(title)));
  2104. }
  2105. xs_html_add(body,
  2106. posts);
  2107. while (xs_list_iter(&p, &v)) {
  2108. xs *msg = NULL;
  2109. int status;
  2110. if (utl && user && !is_pinned_by_md5(user, v))
  2111. status = timeline_get_by_md5(user, v, &msg);
  2112. else
  2113. status = object_get_by_md5(v, &msg);
  2114. if (!valid_status(status))
  2115. continue;
  2116. /* if it's an instance page, discard messages from private users */
  2117. if (user == NULL && is_msg_from_private_user(msg))
  2118. continue;
  2119. /* is this message a non-public reply? */
  2120. if (user != NULL && !is_msg_public(msg)) {
  2121. const char *irt = get_in_reply_to(msg);
  2122. /* is it a reply to something not in the storage? */
  2123. if (!xs_is_null(irt) && !object_here(irt)) {
  2124. /* is it for me? */
  2125. const xs_list *to = xs_dict_get_def(msg, "to", xs_stock(XSTYPE_LIST));
  2126. const xs_list *cc = xs_dict_get_def(msg, "cc", xs_stock(XSTYPE_LIST));
  2127. if (xs_list_in(to, user->actor) == -1 && xs_list_in(cc, user->actor) == -1) {
  2128. snac_debug(user, 1, xs_fmt("skipping non-public reply to an unknown post %s", v));
  2129. continue;
  2130. }
  2131. }
  2132. }
  2133. xs_html *entry = html_entry(user, msg, read_only, 0, v, (user && !hide_children) ? 0 : 1);
  2134. if (entry != NULL)
  2135. xs_html_add(posts,
  2136. entry);
  2137. }
  2138. if (list && user && read_only) {
  2139. /** history **/
  2140. if (xs_type(xs_dict_get(srv_config, "disable_history")) != XSTYPE_TRUE) {
  2141. xs_html *ul = xs_html_tag("ul", NULL);
  2142. xs_html *history = xs_html_tag("div",
  2143. xs_html_attr("class", "snac-history"),
  2144. xs_html_tag("p",
  2145. xs_html_attr("class", "snac-history-title"),
  2146. xs_html_text(L("History"))),
  2147. ul);
  2148. xs *list = history_list(user);
  2149. xs_list *p = list;
  2150. const char *v;
  2151. while (xs_list_iter(&p, &v)) {
  2152. xs *fn = xs_replace(v, ".html", "");
  2153. xs *url = xs_fmt("%s/h/%s", user->actor, v);
  2154. xs_html_add(ul,
  2155. xs_html_tag("li",
  2156. xs_html_tag("a",
  2157. xs_html_attr("href", url),
  2158. xs_html_text(fn))));
  2159. }
  2160. xs_html_add(body,
  2161. history);
  2162. }
  2163. }
  2164. {
  2165. xs *s1 = xs_fmt("\n<!-- %lf seconds -->\n", ftime() - t);
  2166. xs_html_add(body,
  2167. xs_html_raw(s1));
  2168. }
  2169. if (show_more) {
  2170. xs *m = NULL;
  2171. xs *ss = xs_fmt("skip=%d&show=%d", skip + show, show);
  2172. xs *url = xs_dup(user == NULL ? srv_baseurl : user->actor);
  2173. if (page != NULL)
  2174. url = xs_str_cat(url, page);
  2175. if (xs_str_in(url, "?") != -1)
  2176. m = xs_fmt("%s&%s", url, ss);
  2177. else
  2178. m = xs_fmt("%s?%s", url, ss);
  2179. xs_html *more_links = xs_html_tag("p",
  2180. xs_html_tag("a",
  2181. xs_html_attr("href", url),
  2182. xs_html_attr("name", "snac-more"),
  2183. xs_html_text(L("Back to top"))),
  2184. xs_html_text(" - "),
  2185. xs_html_tag("a",
  2186. xs_html_attr("href", m),
  2187. xs_html_attr("name", "snac-more"),
  2188. xs_html_text(L("More..."))));
  2189. xs_html_add(body,
  2190. more_links);
  2191. }
  2192. xs_html_add(body,
  2193. html_footer());
  2194. return xs_html_render_s(html, "<!DOCTYPE html>\n");
  2195. }
  2196. xs_html *html_people_list(snac *snac, xs_list *list, char *header, char *t, const char *proxy)
  2197. {
  2198. xs_html *snac_posts;
  2199. xs_html *people = xs_html_tag("div",
  2200. xs_html_tag("h2",
  2201. xs_html_attr("class", "snac-header"),
  2202. xs_html_text(header)),
  2203. snac_posts = xs_html_tag("details",
  2204. xs_html_attr("open", NULL),
  2205. xs_html_tag("summary",
  2206. xs_html_text("..."))));
  2207. const char *actor_id;
  2208. xs_list_foreach(list, actor_id) {
  2209. xs *md5 = xs_md5_hex(actor_id, strlen(actor_id));
  2210. xs *actor = NULL;
  2211. if (valid_status(actor_get(actor_id, &actor))) {
  2212. xs_html *snac_post = xs_html_tag("div",
  2213. xs_html_attr("class", "snac-post"),
  2214. xs_html_tag("a",
  2215. xs_html_attr("name", md5)),
  2216. xs_html_tag("div",
  2217. xs_html_attr("class", "snac-post-header"),
  2218. html_actor_icon(snac, actor, xs_dict_get(actor, "published"),
  2219. NULL, NULL, 0, 1, proxy)));
  2220. /* content (user bio) */
  2221. const char *c = xs_dict_get(actor, "summary");
  2222. if (!xs_is_null(c)) {
  2223. xs *sc = sanitize(c);
  2224. xs_html *snac_content = xs_html_tag("div",
  2225. xs_html_attr("class", "snac-content"));
  2226. if (xs_startswith(sc, "<p>"))
  2227. xs_html_add(snac_content,
  2228. xs_html_raw(sc)); /* already sanitized */
  2229. else
  2230. xs_html_add(snac_content,
  2231. xs_html_tag("p",
  2232. xs_html_raw(sc))); /* already sanitized */
  2233. xs_html_add(snac_post, snac_content);
  2234. }
  2235. /* buttons */
  2236. xs *btn_form_action = xs_fmt("%s/admin/action", snac->actor);
  2237. xs_html *snac_controls = xs_html_tag("div",
  2238. xs_html_attr("class", "snac-controls"));
  2239. xs_html *form = xs_html_tag("form",
  2240. xs_html_attr("autocomplete", "off"),
  2241. xs_html_attr("method", "post"),
  2242. xs_html_attr("action", btn_form_action),
  2243. xs_html_sctag("input",
  2244. xs_html_attr("type", "hidden"),
  2245. xs_html_attr("name", "actor"),
  2246. xs_html_attr("value", actor_id)),
  2247. xs_html_sctag("input",
  2248. xs_html_attr("type", "hidden"),
  2249. xs_html_attr("name", "actor-form"),
  2250. xs_html_attr("value", "yes")));
  2251. xs_html_add(snac_controls, form);
  2252. if (following_check(snac, actor_id)) {
  2253. xs_html_add(form,
  2254. html_button("unfollow", L("Unfollow"),
  2255. L("Stop following this user's activity")));
  2256. if (is_limited(snac, actor_id))
  2257. xs_html_add(form,
  2258. html_button("unlimit", L("Unlimit"),
  2259. L("Allow announces (boosts) from this user")));
  2260. else
  2261. xs_html_add(form,
  2262. html_button("limit", L("Limit"),
  2263. L("Block announces (boosts) from this user")));
  2264. }
  2265. else {
  2266. xs_html_add(form,
  2267. html_button("follow", L("Follow"),
  2268. L("Start following this user's activity")));
  2269. if (follower_check(snac, actor_id))
  2270. xs_html_add(form,
  2271. html_button("delete", L("Delete"), L("Delete this user")));
  2272. }
  2273. if (pending_check(snac, actor_id)) {
  2274. xs_html_add(form,
  2275. html_button("approve", L("Approve"),
  2276. L("Approve this follow request")));
  2277. xs_html_add(form,
  2278. html_button("discard", L("Discard"), L("Discard this follow request")));
  2279. }
  2280. if (is_muted(snac, actor_id))
  2281. xs_html_add(form,
  2282. html_button("unmute", L("Unmute"),
  2283. L("Stop blocking activities from this user")));
  2284. else
  2285. xs_html_add(form,
  2286. html_button("mute", L("MUTE"),
  2287. L("Block any activity from this user")));
  2288. /* the post textarea */
  2289. xs *dm_div_id = xs_fmt("%s_%s_dm", md5, t);
  2290. xs *dm_form_id = xs_fmt("%s_reply_form", md5);
  2291. xs_html_add(snac_controls,
  2292. xs_html_tag("p", NULL),
  2293. html_note(snac, L("Direct Message..."),
  2294. dm_div_id, dm_form_id,
  2295. "", "",
  2296. NULL, actor_id,
  2297. xs_stock(XSTYPE_FALSE), "",
  2298. xs_stock(XSTYPE_FALSE), NULL,
  2299. NULL, 0, "", "", 0),
  2300. xs_html_tag("p", NULL));
  2301. xs_html_add(snac_post, snac_controls);
  2302. xs_html_add(snac_posts, snac_post);
  2303. }
  2304. }
  2305. return people;
  2306. }
  2307. xs_str *html_people(snac *user)
  2308. {
  2309. const char *proxy = NULL;
  2310. if (xs_is_true(xs_dict_get(srv_config, "proxy_media")))
  2311. proxy = user->actor;
  2312. xs *wing = following_list(user);
  2313. xs *wers = follower_list(user);
  2314. xs_html *lists = xs_html_tag("div",
  2315. xs_html_attr("class", "snac-posts"));
  2316. if (xs_is_true(xs_dict_get(user->config, "approve_followers"))) {
  2317. xs *pending = pending_list(user);
  2318. xs_html_add(lists,
  2319. html_people_list(user, pending, L("Pending follow confirmations"), "p", proxy));
  2320. }
  2321. xs_html_add(lists,
  2322. html_people_list(user, wing, L("People you follow"), "i", proxy),
  2323. html_people_list(user, wers, L("People that follow you"), "e", proxy));
  2324. xs_html *html = xs_html_tag("html",
  2325. html_user_head(user, NULL, NULL),
  2326. xs_html_add(html_user_body(user, 0),
  2327. lists,
  2328. html_footer()));
  2329. return xs_html_render_s(html, "<!DOCTYPE html>\n");
  2330. }
  2331. xs_str *html_notifications(snac *user, int skip, int show)
  2332. {
  2333. const char *proxy = NULL;
  2334. if (xs_is_true(xs_dict_get(srv_config, "proxy_media")))
  2335. proxy = user->actor;
  2336. xs *n_list = notify_list(user, skip, show);
  2337. xs *n_time = notify_check_time(user, 0);
  2338. xs_html *body = html_user_body(user, 0);
  2339. xs_html *html = xs_html_tag("html",
  2340. html_user_head(user, NULL, NULL),
  2341. body);
  2342. xs *clear_all_action = xs_fmt("%s/admin/clear-notifications", user->actor);
  2343. xs_html_add(body,
  2344. xs_html_tag("form",
  2345. xs_html_attr("autocomplete", "off"),
  2346. xs_html_attr("method", "post"),
  2347. xs_html_attr("action", clear_all_action),
  2348. xs_html_attr("id", "clear"),
  2349. xs_html_sctag("input",
  2350. xs_html_attr("type", "submit"),
  2351. xs_html_attr("class", "snac-btn-like"),
  2352. xs_html_attr("value", L("Clear all")))));
  2353. xs_html *noti_new = NULL;
  2354. xs_html *noti_seen = NULL;
  2355. xs_html *posts = xs_html_tag("div",
  2356. xs_html_attr("class", "snac-posts"));
  2357. xs_html_add(body, posts);
  2358. xs_list *p = n_list;
  2359. const xs_str *v;
  2360. while (xs_list_iter(&p, &v)) {
  2361. xs *noti = notify_get(user, v);
  2362. if (noti == NULL)
  2363. continue;
  2364. xs *obj = NULL;
  2365. const char *type = xs_dict_get(noti, "type");
  2366. const char *utype = xs_dict_get(noti, "utype");
  2367. const char *id = xs_dict_get(noti, "objid");
  2368. const char *date = xs_dict_get(noti, "date");
  2369. xs *wrk = NULL;
  2370. if (xs_is_null(id))
  2371. continue;
  2372. if (is_hidden(user, id))
  2373. continue;
  2374. object_get(id, &obj);
  2375. const char *actor_id = xs_dict_get(noti, "actor");
  2376. xs *actor = NULL;
  2377. if (!valid_status(actor_get(actor_id, &actor)))
  2378. continue;
  2379. xs *a_name = actor_name(actor, proxy);
  2380. const char *label = type;
  2381. if (strcmp(type, "Create") == 0)
  2382. label = L("Mention");
  2383. else
  2384. if (strcmp(type, "Update") == 0 && strcmp(utype, "Question") == 0)
  2385. label = L("Finished poll");
  2386. else
  2387. if (strcmp(type, "Undo") == 0 && strcmp(utype, "Follow") == 0)
  2388. label = L("Unfollow");
  2389. else
  2390. if (strcmp(type, "EmojiReact") == 0) {
  2391. const char *content = xs_dict_get_path(noti, "msg.content");
  2392. if (xs_type(content) == XSTYPE_STRING) {
  2393. wrk = xs_fmt("%s (%s)", type, content);
  2394. label = wrk;
  2395. }
  2396. }
  2397. else
  2398. if (strcmp(type, "Follow") == 0 && pending_check(user, actor_id))
  2399. label = L("Follow Request");
  2400. xs *s_date = xs_crop_i(xs_dup(date), 0, 10);
  2401. xs_html *entry = xs_html_tag("div",
  2402. xs_html_attr("class", "snac-post-with-desc"),
  2403. xs_html_tag("p",
  2404. xs_html_tag("b",
  2405. xs_html_text(label),
  2406. xs_html_text(" by "),
  2407. xs_html_tag("a",
  2408. xs_html_attr("href", actor_id),
  2409. xs_html_raw(a_name))), /* a_name is already sanitized */
  2410. xs_html_text(" "),
  2411. xs_html_tag("time",
  2412. xs_html_attr("class", "dt-published snac-pubdate"),
  2413. xs_html_attr("title", date),
  2414. xs_html_text(s_date))));
  2415. if (strcmp(type, "Follow") == 0 || strcmp(utype, "Follow") == 0 || strcmp(type, "Block") == 0) {
  2416. xs_html_add(entry,
  2417. xs_html_tag("div",
  2418. xs_html_attr("class", "snac-post"),
  2419. html_actor_icon(user, actor, NULL, NULL, NULL, 0, 0, proxy)));
  2420. }
  2421. else
  2422. if (strcmp(type, "Move") == 0) {
  2423. const xs_dict *o_msg = xs_dict_get(noti, "msg");
  2424. const char *target;
  2425. if (xs_type(o_msg) == XSTYPE_DICT && (target = xs_dict_get(o_msg, "target"))) {
  2426. xs *old_actor = NULL;
  2427. if (valid_status(actor_get(target, &old_actor))) {
  2428. xs_html_add(entry,
  2429. xs_html_tag("div",
  2430. xs_html_attr("class", "snac-post"),
  2431. html_actor_icon(user, old_actor, NULL, NULL, NULL, 0, 0, proxy)));
  2432. }
  2433. }
  2434. }
  2435. else
  2436. if (obj != NULL) {
  2437. xs *md5 = xs_md5_hex(id, strlen(id));
  2438. xs_html *h = html_entry(user, obj, 0, 0, md5, 1);
  2439. if (h != NULL) {
  2440. xs_html_add(entry,
  2441. h);
  2442. }
  2443. }
  2444. if (strcmp(v, n_time) > 0) {
  2445. /* unseen notification */
  2446. if (noti_new == NULL) {
  2447. noti_new = xs_html_tag("div",
  2448. xs_html_tag("h2",
  2449. xs_html_attr("class", "snac-header"),
  2450. xs_html_text(L("New"))));
  2451. xs_html_add(posts,
  2452. noti_new);
  2453. }
  2454. xs_html_add(noti_new,
  2455. entry);
  2456. }
  2457. else {
  2458. /* already seen notification */
  2459. if (noti_seen == NULL) {
  2460. noti_seen = xs_html_tag("div",
  2461. xs_html_tag("h2",
  2462. xs_html_attr("class", "snac-header"),
  2463. xs_html_text(L("Already seen"))));
  2464. xs_html_add(posts,
  2465. noti_seen);
  2466. }
  2467. xs_html_add(noti_seen,
  2468. entry);
  2469. }
  2470. }
  2471. if (noti_new == NULL && noti_seen == NULL)
  2472. xs_html_add(body,
  2473. xs_html_tag("h2",
  2474. xs_html_attr("class", "snac-header"),
  2475. xs_html_text(L("None"))));
  2476. /* add the navigation footer */
  2477. xs *next_p = notify_list(user, skip + show, 1);
  2478. if (xs_list_len(next_p)) {
  2479. xs *url = xs_fmt("%s/notifications?skip=%d&show=%d",
  2480. user->actor, skip + show, show);
  2481. xs_html_add(body,
  2482. xs_html_tag("p",
  2483. xs_html_tag("a",
  2484. xs_html_attr("href", url),
  2485. xs_html_text(L("More...")))));
  2486. }
  2487. xs_html_add(body,
  2488. html_footer());
  2489. /* set the check time to now */
  2490. xs *dummy = notify_check_time(user, 1);
  2491. dummy = xs_free(dummy);
  2492. timeline_touch(user);
  2493. return xs_html_render_s(html, "<!DOCTYPE html>\n");
  2494. }
  2495. int html_get_handler(const xs_dict *req, const char *q_path,
  2496. char **body, int *b_size, char **ctype,
  2497. xs_str **etag, xs_str **last_modified)
  2498. {
  2499. const char *accept = xs_dict_get(req, "accept");
  2500. int status = HTTP_STATUS_NOT_FOUND;
  2501. snac snac;
  2502. xs *uid = NULL;
  2503. const char *p_path;
  2504. int cache = 1;
  2505. int save = 1;
  2506. int proxy = 0;
  2507. const char *v;
  2508. const xs_dict *q_vars = xs_dict_get(req, "q_vars");
  2509. xs *l = xs_split_n(q_path, "/", 2);
  2510. v = xs_list_get(l, 1);
  2511. if (xs_is_null(v)) {
  2512. srv_log(xs_fmt("html_get_handler bad query '%s'", q_path));
  2513. return HTTP_STATUS_NOT_FOUND;
  2514. }
  2515. if (strcmp(v, "share-bridge") == 0) {
  2516. /* temporary redirect for a post */
  2517. const char *login = xs_dict_get(q_vars, "login");
  2518. const char *content = xs_dict_get(q_vars, "content");
  2519. if (xs_type(login) == XSTYPE_STRING && xs_type(content) == XSTYPE_STRING) {
  2520. xs *b64 = xs_base64_enc(content, strlen(content));
  2521. srv_log(xs_fmt("share-bridge for user '%s'", login));
  2522. *body = xs_fmt("%s/%s/share?content=%s", srv_baseurl, login, b64);
  2523. return HTTP_STATUS_SEE_OTHER;
  2524. }
  2525. else
  2526. return HTTP_STATUS_NOT_FOUND;
  2527. }
  2528. uid = xs_dup(v);
  2529. /* rss extension? */
  2530. if (xs_endswith(uid, ".rss")) {
  2531. uid = xs_crop_i(uid, 0, -4);
  2532. p_path = ".rss";
  2533. }
  2534. else
  2535. p_path = xs_list_get(l, 2);
  2536. if (!uid || !user_open(&snac, uid)) {
  2537. /* invalid user */
  2538. srv_debug(1, xs_fmt("html_get_handler bad user %s", uid));
  2539. return HTTP_STATUS_NOT_FOUND;
  2540. }
  2541. if (xs_is_true(xs_dict_get(srv_config, "proxy_media")))
  2542. proxy = 1;
  2543. /* return the RSS if requested by Accept header */
  2544. if (accept != NULL) {
  2545. if (xs_str_in(accept, "text/xml") != -1 ||
  2546. xs_str_in(accept, "application/rss+xml") != -1)
  2547. p_path = ".rss";
  2548. }
  2549. /* check if server config variable 'disable_cache' is set */
  2550. if ((v = xs_dict_get(srv_config, "disable_cache")) && xs_type(v) == XSTYPE_TRUE)
  2551. cache = 0;
  2552. int skip = 0;
  2553. int def_show = xs_number_get(xs_dict_get(srv_config, "max_timeline_entries"));
  2554. int show = def_show;
  2555. if ((v = xs_dict_get(q_vars, "skip")) != NULL)
  2556. skip = atoi(v), cache = 0, save = 0;
  2557. if ((v = xs_dict_get(q_vars, "show")) != NULL)
  2558. show = atoi(v), cache = 0, save = 0;
  2559. if ((v = xs_dict_get(q_vars, "da")) != NULL) {
  2560. /* user dismissed an announcement */
  2561. if (login(&snac, req)) {
  2562. double ts = atof(v);
  2563. xs *timestamp = xs_number_new(ts);
  2564. srv_log(xs_fmt("user dismissed announcements until %d", ts));
  2565. snac.config = xs_dict_set(snac.config, "last_announcement", timestamp);
  2566. user_persist(&snac, 0);
  2567. }
  2568. }
  2569. /* get a possible error message */
  2570. const char *error = xs_dict_get(q_vars, "error");
  2571. if (error != NULL)
  2572. cache = 0;
  2573. /* a show of 0 has no sense */
  2574. if (show == 0)
  2575. show = def_show;
  2576. if (p_path == NULL) { /** public timeline **/
  2577. xs *h = xs_str_localtime(0, "%Y-%m.html");
  2578. if (xs_type(xs_dict_get(snac.config, "private")) == XSTYPE_TRUE) {
  2579. /** empty public timeline for private users **/
  2580. *body = html_timeline(&snac, NULL, 1, 0, 0, 0, NULL, "", 1, error);
  2581. *b_size = strlen(*body);
  2582. status = HTTP_STATUS_OK;
  2583. }
  2584. else
  2585. if (cache && history_mtime(&snac, h) > timeline_mtime(&snac)) {
  2586. snac_debug(&snac, 1, xs_fmt("serving cached local timeline"));
  2587. status = history_get(&snac, h, body, b_size,
  2588. xs_dict_get(req, "if-none-match"), etag);
  2589. }
  2590. else {
  2591. xs *list = NULL;
  2592. xs *next = NULL;
  2593. if (xs_is_true(xs_dict_get(srv_config, "strict_public_timelines"))) {
  2594. list = timeline_simple_list(&snac, "public", skip, show);
  2595. next = timeline_simple_list(&snac, "public", skip + show, 1);
  2596. }
  2597. else {
  2598. list = timeline_list(&snac, "public", skip, show);
  2599. next = timeline_list(&snac, "public", skip + show, 1);
  2600. }
  2601. xs *pins = pinned_list(&snac);
  2602. pins = xs_list_cat(pins, list);
  2603. *body = html_timeline(&snac, pins, 1, skip, show, xs_list_len(next), NULL, "", 1, error);
  2604. *b_size = strlen(*body);
  2605. status = HTTP_STATUS_OK;
  2606. if (save)
  2607. history_add(&snac, h, *body, *b_size, etag);
  2608. }
  2609. }
  2610. else
  2611. if (strcmp(p_path, "admin") == 0) { /** private timeline **/
  2612. if (!login(&snac, req)) {
  2613. *body = xs_dup(uid);
  2614. status = HTTP_STATUS_UNAUTHORIZED;
  2615. }
  2616. else {
  2617. const char *q = xs_dict_get(q_vars, "q");
  2618. xs *url_acct = NULL;
  2619. /* searching for an URL? */
  2620. if (q && xs_match(q, "https://*|http://*")) {
  2621. /* may by an actor; try a webfinger */
  2622. xs *actor_obj = NULL;
  2623. if (valid_status(webfinger_request(q, &actor_obj, &url_acct))) {
  2624. /* it's an actor; do the dirty trick of changing q to the account name */
  2625. q = url_acct;
  2626. }
  2627. else {
  2628. /* bring it to the user's timeline */
  2629. xs *object = NULL;
  2630. int status;
  2631. status = activitypub_request(&snac, q, &object);
  2632. snac_debug(&snac, 1, xs_fmt("Request searched URL %s %d", q, status));
  2633. if (valid_status(status)) {
  2634. /* got it; also request the actor */
  2635. const char *attr_to = get_atto(object);
  2636. if (!xs_is_null(attr_to)) {
  2637. status = actor_request(&snac, attr_to, &actor_obj);
  2638. if (valid_status(status)) {
  2639. /* reset the query string to be the real id */
  2640. url_acct = xs_dup(xs_dict_get(object, "id"));
  2641. q = url_acct;
  2642. /* add the post to the timeline */
  2643. xs *md5 = xs_md5_hex(q, strlen(q));
  2644. if (!timeline_here(&snac, md5))
  2645. timeline_add(&snac, q, object);
  2646. }
  2647. }
  2648. }
  2649. }
  2650. /* fall through */
  2651. }
  2652. if (q && *q) {
  2653. if (xs_regex_match(q, "^@?[a-zA-Z0-9._]+@[a-zA-Z0-9-]+\\.")) {
  2654. /** search account **/
  2655. xs *actor = NULL;
  2656. xs *acct = NULL;
  2657. xs *l = xs_list_new();
  2658. xs_html *page = NULL;
  2659. if (valid_status(webfinger_request(q, &actor, &acct))) {
  2660. xs *actor_obj = NULL;
  2661. if (valid_status(actor_request(&snac, actor, &actor_obj))) {
  2662. actor_add(actor, actor_obj);
  2663. /* create a people list with only one element */
  2664. l = xs_list_append(xs_list_new(), actor);
  2665. xs *title = xs_fmt(L("Search results for account %s"), q);
  2666. page = html_people_list(&snac, l, title, "wf", NULL);
  2667. }
  2668. }
  2669. if (page == NULL) {
  2670. xs *title = xs_fmt(L("Account %s not found"), q);
  2671. page = xs_html_tag("div",
  2672. xs_html_tag("h2",
  2673. xs_html_attr("class", "snac-header"),
  2674. xs_html_text(title)));
  2675. }
  2676. xs_html *html = xs_html_tag("html",
  2677. html_user_head(&snac, NULL, NULL),
  2678. xs_html_add(html_user_body(&snac, 0),
  2679. page,
  2680. html_footer()));
  2681. *body = xs_html_render_s(html, "<!DOCTYPE html>\n");
  2682. *b_size = strlen(*body);
  2683. status = HTTP_STATUS_OK;
  2684. }
  2685. else
  2686. if (*q == '#') {
  2687. /** search by tag **/
  2688. xs *tl = tag_search(q, skip, show + 1);
  2689. int more = 0;
  2690. if (xs_list_len(tl) >= show + 1) {
  2691. /* drop the last one */
  2692. tl = xs_list_del(tl, -1);
  2693. more = 1;
  2694. }
  2695. xs *page = xs_fmt("/admin?q=%%23%s", q + 1);
  2696. xs *title = xs_fmt(xs_list_len(tl) ?
  2697. L("Search results for tag %s") : L("Nothing found for tag %s"), q);
  2698. *body = html_timeline(&snac, tl, 0, skip, show, more, title, page, 0, error);
  2699. *b_size = strlen(*body);
  2700. status = HTTP_STATUS_OK;
  2701. }
  2702. else {
  2703. /** search by content **/
  2704. int to = 0;
  2705. int msecs = atoi(xs_dict_get_def(q_vars, "msecs", "0"));
  2706. xs *tl = content_search(&snac, q, 1, skip, show, msecs, &to);
  2707. xs *title = NULL;
  2708. xs *page = xs_fmt("/admin?q=%s&msecs=%d", q, msecs + 10);
  2709. int tl_len = xs_list_len(tl);
  2710. if (to)
  2711. title = xs_fmt(L("Search results for '%s' (may be more)"), q);
  2712. else
  2713. if (tl_len)
  2714. title = xs_fmt(L("Search results for '%s'"), q);
  2715. else
  2716. if (skip)
  2717. title = xs_fmt(L("No more matches for '%s'"), q);
  2718. else
  2719. title = xs_fmt(L("Nothing found for '%s'"), q);
  2720. *body = html_timeline(&snac, tl, 0, skip, tl_len, to || tl_len == show,
  2721. title, page, 0, error);
  2722. *b_size = strlen(*body);
  2723. status = HTTP_STATUS_OK;
  2724. }
  2725. }
  2726. else {
  2727. double t = history_mtime(&snac, "timeline.html_");
  2728. /* if enabled by admin, return a cached page if its timestamp is:
  2729. a) newer than the timeline timestamp
  2730. b) newer than the start time of the server
  2731. */
  2732. if (cache && t > timeline_mtime(&snac) && t > p_state->srv_start_time) {
  2733. snac_debug(&snac, 1, xs_fmt("serving cached timeline"));
  2734. status = history_get(&snac, "timeline.html_", body, b_size,
  2735. xs_dict_get(req, "if-none-match"), etag);
  2736. }
  2737. else {
  2738. snac_debug(&snac, 1, xs_fmt("building timeline"));
  2739. xs *list = timeline_list(&snac, "private", skip, show);
  2740. xs *next = timeline_list(&snac, "private", skip + show, 1);
  2741. *body = html_timeline(&snac, list, 0, skip, show,
  2742. xs_list_len(next), NULL, "/admin", 1, error);
  2743. *b_size = strlen(*body);
  2744. status = HTTP_STATUS_OK;
  2745. if (save)
  2746. history_add(&snac, "timeline.html_", *body, *b_size, etag);
  2747. }
  2748. }
  2749. }
  2750. }
  2751. else
  2752. if (xs_startswith(p_path, "admin/p/")) { /** unique post by md5 **/
  2753. if (!login(&snac, req)) {
  2754. *body = xs_dup(uid);
  2755. status = HTTP_STATUS_UNAUTHORIZED;
  2756. }
  2757. else {
  2758. xs *l = xs_split(p_path, "/");
  2759. const char *md5 = xs_list_get(l, -1);
  2760. if (md5 && *md5 && timeline_here(&snac, md5)) {
  2761. xs *list = xs_list_append(xs_list_new(), md5);
  2762. *body = html_timeline(&snac, list, 0, 0, 0, 0, NULL, "/admin", 1, error);
  2763. *b_size = strlen(*body);
  2764. status = HTTP_STATUS_OK;
  2765. }
  2766. }
  2767. }
  2768. else
  2769. if (strcmp(p_path, "people") == 0) { /** the list of people **/
  2770. if (!login(&snac, req)) {
  2771. *body = xs_dup(uid);
  2772. status = HTTP_STATUS_UNAUTHORIZED;
  2773. }
  2774. else {
  2775. *body = html_people(&snac);
  2776. *b_size = strlen(*body);
  2777. status = HTTP_STATUS_OK;
  2778. }
  2779. }
  2780. else
  2781. if (strcmp(p_path, "notifications") == 0) { /** the list of notifications **/
  2782. if (!login(&snac, req)) {
  2783. *body = xs_dup(uid);
  2784. status = HTTP_STATUS_UNAUTHORIZED;
  2785. }
  2786. else {
  2787. *body = html_notifications(&snac, skip, show);
  2788. *b_size = strlen(*body);
  2789. status = HTTP_STATUS_OK;
  2790. }
  2791. }
  2792. else
  2793. if (strcmp(p_path, "instance") == 0) { /** instance timeline **/
  2794. if (!login(&snac, req)) {
  2795. *body = xs_dup(uid);
  2796. status = HTTP_STATUS_UNAUTHORIZED;
  2797. }
  2798. else {
  2799. xs *list = timeline_instance_list(skip, show);
  2800. xs *next = timeline_instance_list(skip + show, 1);
  2801. *body = html_timeline(&snac, list, 0, skip, show,
  2802. xs_list_len(next), L("Showing instance timeline"), "/instance", 0, error);
  2803. *b_size = strlen(*body);
  2804. status = HTTP_STATUS_OK;
  2805. }
  2806. }
  2807. else
  2808. if (strcmp(p_path, "pinned") == 0) { /** list of pinned posts **/
  2809. if (!login(&snac, req)) {
  2810. *body = xs_dup(uid);
  2811. status = HTTP_STATUS_UNAUTHORIZED;
  2812. }
  2813. else {
  2814. xs *list = pinned_list(&snac);
  2815. *body = html_timeline(&snac, list, 0, skip, show,
  2816. 0, L("Pinned posts"), "", 0, error);
  2817. *b_size = strlen(*body);
  2818. status = HTTP_STATUS_OK;
  2819. }
  2820. }
  2821. else
  2822. if (strcmp(p_path, "bookmarks") == 0) { /** list of bookmarked posts **/
  2823. if (!login(&snac, req)) {
  2824. *body = xs_dup(uid);
  2825. status = HTTP_STATUS_UNAUTHORIZED;
  2826. }
  2827. else {
  2828. xs *list = bookmark_list(&snac);
  2829. *body = html_timeline(&snac, list, 0, skip, show,
  2830. 0, L("Bookmarked posts"), "", 0, error);
  2831. *b_size = strlen(*body);
  2832. status = HTTP_STATUS_OK;
  2833. }
  2834. }
  2835. else
  2836. if (strcmp(p_path, "drafts") == 0) { /** list of drafts **/
  2837. if (!login(&snac, req)) {
  2838. *body = xs_dup(uid);
  2839. status = HTTP_STATUS_UNAUTHORIZED;
  2840. }
  2841. else {
  2842. xs *list = draft_list(&snac);
  2843. *body = html_timeline(&snac, list, 0, skip, show,
  2844. 0, L("Post drafts"), "", 0, error);
  2845. *b_size = strlen(*body);
  2846. status = HTTP_STATUS_OK;
  2847. }
  2848. }
  2849. else
  2850. if (xs_startswith(p_path, "list/")) { /** list timelines **/
  2851. if (!login(&snac, req)) {
  2852. *body = xs_dup(uid);
  2853. status = HTTP_STATUS_UNAUTHORIZED;
  2854. }
  2855. else {
  2856. xs *l = xs_split(p_path, "/");
  2857. const char *lid = xs_list_get(l, -1);
  2858. xs *list = list_timeline(&snac, lid, skip, show);
  2859. xs *next = list_timeline(&snac, lid, skip + show, 1);
  2860. if (list != NULL) {
  2861. xs *ttl = timeline_top_level(&snac, list);
  2862. xs *base = xs_fmt("/list/%s", lid);
  2863. xs *name = list_maint(&snac, lid, 3);
  2864. xs *title = xs_fmt(L("Showing timeline for list '%s'"), name);
  2865. *body = html_timeline(&snac, ttl, 0, skip, show,
  2866. xs_list_len(next), title, base, 1, error);
  2867. *b_size = strlen(*body);
  2868. status = HTTP_STATUS_OK;
  2869. }
  2870. }
  2871. }
  2872. else
  2873. if (xs_startswith(p_path, "p/")) { /** a timeline with just one entry **/
  2874. if (xs_type(xs_dict_get(snac.config, "private")) == XSTYPE_TRUE)
  2875. return HTTP_STATUS_FORBIDDEN;
  2876. xs *id = xs_fmt("%s/%s", snac.actor, p_path);
  2877. xs *msg = NULL;
  2878. if (valid_status(object_get(id, &msg))) {
  2879. xs *md5 = xs_md5_hex(id, strlen(id));
  2880. xs *list = xs_list_new();
  2881. list = xs_list_append(list, md5);
  2882. *body = html_timeline(&snac, list, 1, 0, 0, 0, NULL, "", 1, error);
  2883. *b_size = strlen(*body);
  2884. status = HTTP_STATUS_OK;
  2885. }
  2886. }
  2887. else
  2888. if (xs_startswith(p_path, "s/")) { /** a static file **/
  2889. xs *l = xs_split(p_path, "/");
  2890. const char *id = xs_list_get(l, 1);
  2891. int sz;
  2892. if (id && *id) {
  2893. status = static_get(&snac, id, body, &sz,
  2894. xs_dict_get(req, "if-none-match"), etag);
  2895. if (valid_status(status)) {
  2896. *b_size = sz;
  2897. *ctype = (char *)xs_mime_by_ext(id);
  2898. }
  2899. }
  2900. }
  2901. else
  2902. if (xs_startswith(p_path, "h/")) { /** an entry from the history **/
  2903. if (xs_type(xs_dict_get(snac.config, "private")) == XSTYPE_TRUE)
  2904. return HTTP_STATUS_FORBIDDEN;
  2905. if (xs_type(xs_dict_get(srv_config, "disable_history")) == XSTYPE_TRUE)
  2906. return HTTP_STATUS_FORBIDDEN;
  2907. xs *l = xs_split(p_path, "/");
  2908. const char *id = xs_list_get(l, 1);
  2909. if (id && *id) {
  2910. if (xs_endswith(id, "timeline.html_")) {
  2911. /* Don't let them in */
  2912. *b_size = 0;
  2913. status = HTTP_STATUS_NOT_FOUND;
  2914. }
  2915. else
  2916. status = history_get(&snac, id, body, b_size,
  2917. xs_dict_get(req, "if-none-match"), etag);
  2918. }
  2919. }
  2920. else
  2921. if (strcmp(p_path, ".rss") == 0) { /** public timeline in RSS format **/
  2922. if (xs_type(xs_dict_get(snac.config, "private")) == XSTYPE_TRUE)
  2923. return HTTP_STATUS_FORBIDDEN;
  2924. int cnt = xs_number_get(xs_dict_get_def(srv_config, "max_public_entries", "20"));
  2925. xs *elems = timeline_simple_list(&snac, "public", 0, cnt);
  2926. xs *bio = xs_dup(xs_dict_get(snac.config, "bio"));
  2927. xs *rss_title = xs_fmt("%s (@%s@%s)",
  2928. xs_dict_get(snac.config, "name"),
  2929. snac.uid,
  2930. xs_dict_get(srv_config, "host"));
  2931. xs *rss_link = xs_fmt("%s.rss", snac.actor);
  2932. *body = timeline_to_rss(&snac, elems, rss_title, rss_link, bio);
  2933. *b_size = strlen(*body);
  2934. *ctype = "application/rss+xml; charset=utf-8";
  2935. status = HTTP_STATUS_OK;
  2936. snac_debug(&snac, 1, xs_fmt("serving RSS"));
  2937. }
  2938. else
  2939. if (proxy && (xs_startswith(p_path, "x/") || xs_startswith(p_path, "y/"))) { /** remote media by proxy **/
  2940. xs *proxy_prefix = NULL;
  2941. if (xs_startswith(p_path, "x/")) {
  2942. /* proxy usage authorized by http basic auth */
  2943. if (login(&snac, req))
  2944. proxy_prefix = xs_str_new("x/");
  2945. else {
  2946. *body = xs_dup(uid);
  2947. status = HTTP_STATUS_UNAUTHORIZED;
  2948. }
  2949. }
  2950. else {
  2951. /* proxy usage authorized by proxy_token */
  2952. xs *tks = xs_fmt("%s:%s", srv_proxy_token_seed, snac.actor);
  2953. xs *tk = xs_md5_hex(tks, strlen(tks));
  2954. xs *p = xs_fmt("y/%s/", tk);
  2955. if (xs_startswith(p_path, p))
  2956. proxy_prefix = xs_dup(p);
  2957. }
  2958. if (proxy_prefix) {
  2959. /* pick the raw path (including optional ? arguments) */
  2960. const char *raw_path = xs_dict_get(req, "raw_path");
  2961. /* skip to where the proxy/ string starts */
  2962. raw_path += xs_str_in(raw_path, proxy_prefix);
  2963. xs *url = xs_replace_n(raw_path, proxy_prefix, "https:/" "/", 1);
  2964. xs *hdrs = xs_dict_new();
  2965. hdrs = xs_dict_append(hdrs, "user-agent", USER_AGENT);
  2966. const char *ims = xs_dict_get(req, "if-modified-since");
  2967. const char *inm = xs_dict_get(req, "if-none-match");
  2968. if (ims) hdrs = xs_dict_append(hdrs, "if-modified-since", ims);
  2969. if (inm) hdrs = xs_dict_append(hdrs, "if-none-match", inm);
  2970. xs *rsp = xs_http_request("GET", url, hdrs,
  2971. NULL, 0, &status, body, b_size, 0);
  2972. if (valid_status(status)) {
  2973. const char *ct = xs_or(xs_dict_get(rsp, "content-type"), "");
  2974. const char *lm = xs_dict_get(rsp, "last-modified");
  2975. const char *et = xs_dict_get(rsp, "etag");
  2976. if (lm) *last_modified = xs_dup(lm);
  2977. if (et) *etag = xs_dup(et);
  2978. /* find the content-type in the static mime types,
  2979. and return that value instead of ct, which will
  2980. be destroyed when out of scope */
  2981. for (int n = 0; xs_mime_types[n]; n += 2) {
  2982. if (strcmp(ct, xs_mime_types[n + 1]) == 0) {
  2983. *ctype = (char *)xs_mime_types[n + 1];
  2984. break;
  2985. }
  2986. }
  2987. }
  2988. snac_debug(&snac, 1, xs_fmt("Proxy for %s %d", url, status));
  2989. }
  2990. }
  2991. else
  2992. if (strcmp(p_path, "share") == 0) { /** direct post **/
  2993. if (!login(&snac, req)) {
  2994. *body = xs_dup(uid);
  2995. status = HTTP_STATUS_UNAUTHORIZED;
  2996. }
  2997. else {
  2998. const char *b64 = xs_dict_get(q_vars, "content");
  2999. int sz;
  3000. xs *content = xs_base64_dec(b64, &sz);
  3001. xs *msg = msg_note(&snac, content, NULL, NULL, NULL, 0, NULL);
  3002. xs *c_msg = msg_create(&snac, msg);
  3003. timeline_add(&snac, xs_dict_get(msg, "id"), msg);
  3004. enqueue_message(&snac, c_msg);
  3005. snac_debug(&snac, 1, xs_fmt("web action 'share' received"));
  3006. *body = xs_fmt("%s/admin", snac.actor);
  3007. *b_size = strlen(*body);
  3008. status = HTTP_STATUS_SEE_OTHER;
  3009. }
  3010. }
  3011. else
  3012. status = HTTP_STATUS_NOT_FOUND;
  3013. user_free(&snac);
  3014. if (valid_status(status) && *ctype == NULL) {
  3015. *ctype = "text/html; charset=utf-8";
  3016. }
  3017. return status;
  3018. }
  3019. int html_post_handler(const xs_dict *req, const char *q_path,
  3020. char *payload, int p_size,
  3021. char **body, int *b_size, char **ctype)
  3022. {
  3023. (void)p_size;
  3024. (void)ctype;
  3025. int status = 0;
  3026. snac snac;
  3027. const char *uid;
  3028. const char *p_path;
  3029. const xs_dict *p_vars;
  3030. xs *l = xs_split_n(q_path, "/", 2);
  3031. uid = xs_list_get(l, 1);
  3032. if (!uid || !user_open(&snac, uid)) {
  3033. /* invalid user */
  3034. srv_debug(1, xs_fmt("html_post_handler bad user %s", uid));
  3035. return HTTP_STATUS_NOT_FOUND;
  3036. }
  3037. p_path = xs_list_get(l, 2);
  3038. /* all posts must be authenticated */
  3039. if (!login(&snac, req)) {
  3040. user_free(&snac);
  3041. *body = xs_dup(uid);
  3042. return HTTP_STATUS_UNAUTHORIZED;
  3043. }
  3044. p_vars = xs_dict_get(req, "p_vars");
  3045. if (p_path && strcmp(p_path, "admin/note") == 0) { /** **/
  3046. snac_debug(&snac, 1, xs_fmt("web action '%s' received", p_path));
  3047. /* post note */
  3048. const xs_str *content = xs_dict_get(p_vars, "content");
  3049. const xs_str *in_reply_to = xs_dict_get(p_vars, "in_reply_to");
  3050. const xs_str *attach_url = xs_dict_get(p_vars, "attach_url");
  3051. const xs_list *attach_file = xs_dict_get(p_vars, "attach");
  3052. const xs_str *to = xs_dict_get(p_vars, "to");
  3053. const xs_str *sensitive = xs_dict_get(p_vars, "sensitive");
  3054. const xs_str *summary = xs_dict_get(p_vars, "summary");
  3055. const xs_str *edit_id = xs_dict_get(p_vars, "edit_id");
  3056. const xs_str *alt_text = xs_dict_get(p_vars, "alt_text");
  3057. int priv = !xs_is_null(xs_dict_get(p_vars, "mentioned_only"));
  3058. int store_as_draft = !xs_is_null(xs_dict_get(p_vars, "is_draft"));
  3059. xs *attach_list = xs_list_new();
  3060. /* default alt text */
  3061. if (xs_is_null(alt_text))
  3062. alt_text = "";
  3063. /* is attach_url set? */
  3064. if (!xs_is_null(attach_url) && *attach_url != '\0') {
  3065. xs *l = xs_list_new();
  3066. l = xs_list_append(l, attach_url);
  3067. l = xs_list_append(l, alt_text);
  3068. attach_list = xs_list_append(attach_list, l);
  3069. }
  3070. /* is attach_file set? */
  3071. if (!xs_is_null(attach_file) && xs_type(attach_file) == XSTYPE_LIST) {
  3072. const char *fn = xs_list_get(attach_file, 0);
  3073. if (*fn != '\0') {
  3074. char *ext = strrchr(fn, '.');
  3075. xs *hash = xs_md5_hex(fn, strlen(fn));
  3076. xs *id = xs_fmt("%s%s", hash, ext);
  3077. xs *url = xs_fmt("%s/s/%s", snac.actor, id);
  3078. int fo = xs_number_get(xs_list_get(attach_file, 1));
  3079. int fs = xs_number_get(xs_list_get(attach_file, 2));
  3080. /* store */
  3081. static_put(&snac, id, payload + fo, fs);
  3082. xs *l = xs_list_new();
  3083. l = xs_list_append(l, url);
  3084. l = xs_list_append(l, alt_text);
  3085. attach_list = xs_list_append(attach_list, l);
  3086. }
  3087. }
  3088. if (content != NULL) {
  3089. xs *msg = NULL;
  3090. xs *c_msg = NULL;
  3091. xs *content_2 = xs_replace(content, "\r", "");
  3092. xs *poll_opts = NULL;
  3093. /* is there a valid set of poll options? */
  3094. const char *v = xs_dict_get(p_vars, "poll_options");
  3095. if (!xs_is_null(v) && *v) {
  3096. xs *v2 = xs_strip_i(xs_replace(v, "\r", ""));
  3097. poll_opts = xs_split(v2, "\n");
  3098. }
  3099. if (!xs_is_null(poll_opts) && xs_list_len(poll_opts)) {
  3100. /* get the rest of poll configuration */
  3101. const char *p_multiple = xs_dict_get(p_vars, "poll_multiple");
  3102. const char *p_end_secs = xs_dict_get(p_vars, "poll_end_secs");
  3103. int multiple = 0;
  3104. int end_secs = atoi(!xs_is_null(p_end_secs) ? p_end_secs : "60");
  3105. if (!xs_is_null(p_multiple) && strcmp(p_multiple, "on") == 0)
  3106. multiple = 1;
  3107. msg = msg_question(&snac, content_2, attach_list,
  3108. poll_opts, multiple, end_secs);
  3109. enqueue_close_question(&snac, xs_dict_get(msg, "id"), end_secs);
  3110. }
  3111. else
  3112. msg = msg_note(&snac, content_2, to, in_reply_to, attach_list, priv, NULL);
  3113. if (sensitive != NULL) {
  3114. msg = xs_dict_set(msg, "sensitive", xs_stock(XSTYPE_TRUE));
  3115. msg = xs_dict_set(msg, "summary", xs_is_null(summary) ? "..." : summary);
  3116. }
  3117. if (xs_is_null(edit_id)) {
  3118. /* new message */
  3119. const char *id = xs_dict_get(msg, "id");
  3120. if (store_as_draft) {
  3121. draft_add(&snac, id, msg);
  3122. }
  3123. else {
  3124. c_msg = msg_create(&snac, msg);
  3125. timeline_add(&snac, id, msg);
  3126. }
  3127. }
  3128. else {
  3129. /* an edition of a previous message */
  3130. xs *p_msg = NULL;
  3131. if (valid_status(object_get(edit_id, &p_msg))) {
  3132. /* copy relevant fields from previous version */
  3133. char *fields[] = { "id", "context", "url", "published",
  3134. "to", "inReplyTo", NULL };
  3135. int n;
  3136. for (n = 0; fields[n]; n++) {
  3137. const char *v = xs_dict_get(p_msg, fields[n]);
  3138. msg = xs_dict_set(msg, fields[n], v);
  3139. }
  3140. if (store_as_draft) {
  3141. draft_add(&snac, edit_id, msg);
  3142. }
  3143. else
  3144. if (is_draft(&snac, edit_id)) {
  3145. /* message was previously a draft; it's a create activity */
  3146. /* set the published field to now */
  3147. xs *published = xs_str_utctime(0, ISO_DATE_SPEC);
  3148. msg = xs_dict_set(msg, "published", published);
  3149. /* overwrite object */
  3150. object_add_ow(edit_id, msg);
  3151. c_msg = msg_create(&snac, msg);
  3152. timeline_add(&snac, edit_id, msg);
  3153. draft_del(&snac, edit_id);
  3154. }
  3155. else {
  3156. /* set the updated field */
  3157. xs *updated = xs_str_utctime(0, ISO_DATE_SPEC);
  3158. msg = xs_dict_set(msg, "updated", updated);
  3159. /* overwrite object, not updating the indexes */
  3160. object_add_ow(edit_id, msg);
  3161. /* update message */
  3162. c_msg = msg_update(&snac, msg);
  3163. }
  3164. }
  3165. else
  3166. snac_log(&snac, xs_fmt("cannot get object '%s' for editing", edit_id));
  3167. }
  3168. if (c_msg != NULL)
  3169. enqueue_message(&snac, c_msg);
  3170. history_del(&snac, "timeline.html_");
  3171. }
  3172. status = HTTP_STATUS_SEE_OTHER;
  3173. }
  3174. else
  3175. if (p_path && strcmp(p_path, "admin/action") == 0) { /** **/
  3176. /* action on an entry */
  3177. const char *id = xs_dict_get(p_vars, "id");
  3178. const char *actor = xs_dict_get(p_vars, "actor");
  3179. const char *action = xs_dict_get(p_vars, "action");
  3180. const char *group = xs_dict_get(p_vars, "group");
  3181. if (action == NULL)
  3182. return HTTP_STATUS_NOT_FOUND;
  3183. snac_debug(&snac, 1, xs_fmt("web action '%s' received", action));
  3184. status = HTTP_STATUS_SEE_OTHER;
  3185. if (strcmp(action, L("Like")) == 0) { /** **/
  3186. xs *msg = msg_admiration(&snac, id, "Like");
  3187. if (msg != NULL) {
  3188. enqueue_message(&snac, msg);
  3189. timeline_admire(&snac, xs_dict_get(msg, "object"), snac.actor, 1);
  3190. }
  3191. }
  3192. else
  3193. if (strcmp(action, L("Boost")) == 0) { /** **/
  3194. xs *msg = msg_admiration(&snac, id, "Announce");
  3195. if (msg != NULL) {
  3196. enqueue_message(&snac, msg);
  3197. timeline_admire(&snac, xs_dict_get(msg, "object"), snac.actor, 0);
  3198. }
  3199. }
  3200. else
  3201. if (strcmp(action, L("Unlike")) == 0) { /** **/
  3202. xs *msg = msg_repulsion(&snac, id, "Like");
  3203. if (msg != NULL) {
  3204. enqueue_message(&snac, msg);
  3205. }
  3206. }
  3207. else
  3208. if (strcmp(action, L("Unboost")) == 0) { /** **/
  3209. xs *msg = msg_repulsion(&snac, id, "Announce");
  3210. if (msg != NULL) {
  3211. enqueue_message(&snac, msg);
  3212. }
  3213. }
  3214. else
  3215. if (strcmp(action, L("MUTE")) == 0) { /** **/
  3216. mute(&snac, actor);
  3217. }
  3218. else
  3219. if (strcmp(action, L("Unmute")) == 0) { /** **/
  3220. unmute(&snac, actor);
  3221. }
  3222. else
  3223. if (strcmp(action, L("Hide")) == 0) { /** **/
  3224. if (is_draft(&snac, id))
  3225. draft_del(&snac, id);
  3226. else
  3227. hide(&snac, id);
  3228. }
  3229. else
  3230. if (strcmp(action, L("Limit")) == 0) { /** **/
  3231. limit(&snac, actor);
  3232. }
  3233. else
  3234. if (strcmp(action, L("Unlimit")) == 0) { /** **/
  3235. unlimit(&snac, actor);
  3236. }
  3237. else
  3238. if (strcmp(action, L("Follow")) == 0) { /** **/
  3239. xs *msg = msg_follow(&snac, actor);
  3240. if (msg != NULL) {
  3241. /* reload the actor from the message, in may be different */
  3242. actor = xs_dict_get(msg, "object");
  3243. following_add(&snac, actor, msg);
  3244. enqueue_output_by_actor(&snac, msg, actor, 0);
  3245. }
  3246. }
  3247. else
  3248. if (strcmp(action, L("Unfollow")) == 0) { /** **/
  3249. /* get the following object */
  3250. xs *object = NULL;
  3251. if (valid_status(following_get(&snac, actor, &object))) {
  3252. xs *msg = msg_undo(&snac, xs_dict_get(object, "object"));
  3253. following_del(&snac, actor);
  3254. enqueue_output_by_actor(&snac, msg, actor, 0);
  3255. snac_log(&snac, xs_fmt("unfollowed actor %s", actor));
  3256. }
  3257. else
  3258. snac_log(&snac, xs_fmt("actor is not being followed %s", actor));
  3259. }
  3260. else
  3261. if (strcmp(action, L("Follow Group")) == 0) { /** **/
  3262. xs *msg = msg_follow(&snac, group);
  3263. if (msg != NULL) {
  3264. /* reload the group from the message, in may be different */
  3265. group = xs_dict_get(msg, "object");
  3266. following_add(&snac, group, msg);
  3267. enqueue_output_by_actor(&snac, msg, group, 0);
  3268. }
  3269. }
  3270. else
  3271. if (strcmp(action, L("Unfollow Group")) == 0) { /** **/
  3272. /* get the following object */
  3273. xs *object = NULL;
  3274. if (valid_status(following_get(&snac, group, &object))) {
  3275. xs *msg = msg_undo(&snac, xs_dict_get(object, "object"));
  3276. following_del(&snac, group);
  3277. enqueue_output_by_actor(&snac, msg, group, 0);
  3278. snac_log(&snac, xs_fmt("unfollowed group %s", group));
  3279. }
  3280. else
  3281. snac_log(&snac, xs_fmt("actor is not being followed %s", actor));
  3282. }
  3283. else
  3284. if (strcmp(action, L("Delete")) == 0) { /** **/
  3285. const char *actor_form = xs_dict_get(p_vars, "actor-form");
  3286. if (actor_form != NULL) {
  3287. /* delete follower */
  3288. if (valid_status(follower_del(&snac, actor)))
  3289. snac_log(&snac, xs_fmt("deleted follower %s", actor));
  3290. else
  3291. snac_log(&snac, xs_fmt("error deleting follower %s", actor));
  3292. }
  3293. else {
  3294. /* delete an entry */
  3295. if (xs_startswith(id, snac.actor) && !is_draft(&snac, id)) {
  3296. /* it's a post by us: generate a delete */
  3297. xs *msg = msg_delete(&snac, id);
  3298. enqueue_message(&snac, msg);
  3299. snac_log(&snac, xs_fmt("posted tombstone for %s", id));
  3300. }
  3301. timeline_del(&snac, id);
  3302. draft_del(&snac, id);
  3303. snac_log(&snac, xs_fmt("deleted entry %s", id));
  3304. }
  3305. }
  3306. else
  3307. if (strcmp(action, L("Pin")) == 0) { /** **/
  3308. pin(&snac, id);
  3309. timeline_touch(&snac);
  3310. }
  3311. else
  3312. if (strcmp(action, L("Unpin")) == 0) { /** **/
  3313. unpin(&snac, id);
  3314. timeline_touch(&snac);
  3315. }
  3316. else
  3317. if (strcmp(action, L("Bookmark")) == 0) { /** **/
  3318. bookmark(&snac, id);
  3319. timeline_touch(&snac);
  3320. }
  3321. else
  3322. if (strcmp(action, L("Unbookmark")) == 0) { /** **/
  3323. unbookmark(&snac, id);
  3324. timeline_touch(&snac);
  3325. }
  3326. else
  3327. if (strcmp(action, L("Approve")) == 0) { /** **/
  3328. xs *fwreq = pending_get(&snac, actor);
  3329. if (fwreq != NULL) {
  3330. xs *reply = msg_accept(&snac, fwreq, actor);
  3331. enqueue_message(&snac, reply);
  3332. if (xs_is_null(xs_dict_get(fwreq, "published"))) {
  3333. /* add a date if it doesn't include one (Mastodon) */
  3334. xs *date = xs_str_utctime(0, ISO_DATE_SPEC);
  3335. fwreq = xs_dict_set(fwreq, "published", date);
  3336. }
  3337. timeline_add(&snac, xs_dict_get(fwreq, "id"), fwreq);
  3338. follower_add(&snac, actor);
  3339. pending_del(&snac, actor);
  3340. snac_log(&snac, xs_fmt("new follower %s", actor));
  3341. }
  3342. }
  3343. else
  3344. if (strcmp(action, L("Discard")) == 0) { /** **/
  3345. pending_del(&snac, actor);
  3346. }
  3347. else
  3348. status = HTTP_STATUS_NOT_FOUND;
  3349. /* delete the cached timeline */
  3350. if (status == HTTP_STATUS_SEE_OTHER)
  3351. history_del(&snac, "timeline.html_");
  3352. }
  3353. else
  3354. if (p_path && strcmp(p_path, "admin/user-setup") == 0) { /** **/
  3355. /* change of user data */
  3356. const char *v;
  3357. const char *p1, *p2;
  3358. if ((v = xs_dict_get(p_vars, "name")) != NULL)
  3359. snac.config = xs_dict_set(snac.config, "name", v);
  3360. if ((v = xs_dict_get(p_vars, "avatar")) != NULL)
  3361. snac.config = xs_dict_set(snac.config, "avatar", v);
  3362. if ((v = xs_dict_get(p_vars, "bio")) != NULL)
  3363. snac.config = xs_dict_set(snac.config, "bio", v);
  3364. if ((v = xs_dict_get(p_vars, "cw")) != NULL &&
  3365. strcmp(v, "on") == 0) {
  3366. snac.config = xs_dict_set(snac.config, "cw", "open");
  3367. } else { /* if the checkbox is not set, the parameter is missing */
  3368. snac.config = xs_dict_set(snac.config, "cw", "");
  3369. }
  3370. if ((v = xs_dict_get(p_vars, "email")) != NULL)
  3371. snac.config = xs_dict_set(snac.config, "email", v);
  3372. if ((v = xs_dict_get(p_vars, "telegram_bot")) != NULL)
  3373. snac.config = xs_dict_set(snac.config, "telegram_bot", v);
  3374. if ((v = xs_dict_get(p_vars, "telegram_chat_id")) != NULL)
  3375. snac.config = xs_dict_set(snac.config, "telegram_chat_id", v);
  3376. if ((v = xs_dict_get(p_vars, "ntfy_server")) != NULL)
  3377. snac.config = xs_dict_set(snac.config, "ntfy_server", v);
  3378. if ((v = xs_dict_get(p_vars, "ntfy_token")) != NULL)
  3379. snac.config = xs_dict_set(snac.config, "ntfy_token", v);
  3380. if ((v = xs_dict_get(p_vars, "purge_days")) != NULL) {
  3381. xs *days = xs_number_new(atof(v));
  3382. snac.config = xs_dict_set(snac.config, "purge_days", days);
  3383. }
  3384. if ((v = xs_dict_get(p_vars, "drop_dm_from_unknown")) != NULL && strcmp(v, "on") == 0)
  3385. snac.config = xs_dict_set(snac.config, "drop_dm_from_unknown", xs_stock(XSTYPE_TRUE));
  3386. else
  3387. snac.config = xs_dict_set(snac.config, "drop_dm_from_unknown", xs_stock(XSTYPE_FALSE));
  3388. if ((v = xs_dict_get(p_vars, "bot")) != NULL && strcmp(v, "on") == 0)
  3389. snac.config = xs_dict_set(snac.config, "bot", xs_stock(XSTYPE_TRUE));
  3390. else
  3391. snac.config = xs_dict_set(snac.config, "bot", xs_stock(XSTYPE_FALSE));
  3392. if ((v = xs_dict_get(p_vars, "private")) != NULL && strcmp(v, "on") == 0)
  3393. snac.config = xs_dict_set(snac.config, "private", xs_stock(XSTYPE_TRUE));
  3394. else
  3395. snac.config = xs_dict_set(snac.config, "private", xs_stock(XSTYPE_FALSE));
  3396. if ((v = xs_dict_get(p_vars, "auto_boost")) != NULL && strcmp(v, "on") == 0)
  3397. snac.config = xs_dict_set(snac.config, "auto_boost", xs_stock(XSTYPE_TRUE));
  3398. else
  3399. snac.config = xs_dict_set(snac.config, "auto_boost", xs_stock(XSTYPE_FALSE));
  3400. if ((v = xs_dict_get(p_vars, "collapse_threads")) != NULL && strcmp(v, "on") == 0)
  3401. snac.config = xs_dict_set(snac.config, "collapse_threads", xs_stock(XSTYPE_TRUE));
  3402. else
  3403. snac.config = xs_dict_set(snac.config, "collapse_threads", xs_stock(XSTYPE_FALSE));
  3404. if ((v = xs_dict_get(p_vars, "approve_followers")) != NULL && strcmp(v, "on") == 0)
  3405. snac.config = xs_dict_set(snac.config, "approve_followers", xs_stock(XSTYPE_TRUE));
  3406. else
  3407. snac.config = xs_dict_set(snac.config, "approve_followers", xs_stock(XSTYPE_FALSE));
  3408. if ((v = xs_dict_get(p_vars, "show_contact_metrics")) != NULL && strcmp(v, "on") == 0)
  3409. snac.config = xs_dict_set(snac.config, "show_contact_metrics", xs_stock(XSTYPE_TRUE));
  3410. else
  3411. snac.config = xs_dict_set(snac.config, "show_contact_metrics", xs_stock(XSTYPE_FALSE));
  3412. snac.config = xs_dict_set(snac.config, "latitude", xs_dict_get_def(p_vars, "latitude", ""));
  3413. snac.config = xs_dict_set(snac.config, "longitude", xs_dict_get_def(p_vars, "longitude", ""));
  3414. if ((v = xs_dict_get(p_vars, "metadata")) != NULL)
  3415. snac.config = xs_dict_set(snac.config, "metadata", v);
  3416. /* uploads */
  3417. const char *uploads[] = { "avatar", "header", NULL };
  3418. int n;
  3419. for (n = 0; uploads[n]; n++) {
  3420. xs *var_name = xs_fmt("%s_file", uploads[n]);
  3421. const xs_list *uploaded_file = xs_dict_get(p_vars, var_name);
  3422. if (xs_type(uploaded_file) == XSTYPE_LIST) {
  3423. const char *fn = xs_list_get(uploaded_file, 0);
  3424. if (fn && *fn) {
  3425. const char *mimetype = xs_mime_by_ext(fn);
  3426. if (xs_startswith(mimetype, "image/")) {
  3427. const char *ext = strrchr(fn, '.');
  3428. xs *hash = xs_md5_hex(fn, strlen(fn));
  3429. xs *id = xs_fmt("%s%s", hash, ext);
  3430. xs *url = xs_fmt("%s/s/%s", snac.actor, id);
  3431. int fo = xs_number_get(xs_list_get(uploaded_file, 1));
  3432. int fs = xs_number_get(xs_list_get(uploaded_file, 2));
  3433. /* store */
  3434. static_put(&snac, id, payload + fo, fs);
  3435. snac.config = xs_dict_set(snac.config, uploads[n], url);
  3436. }
  3437. }
  3438. }
  3439. }
  3440. /* delete images by removing url from user.json */
  3441. for (n = 0; uploads[n]; n++) {
  3442. xs *var_name = xs_fmt("%s_delete", uploads[n]);
  3443. const char *delete_var = xs_dict_get(p_vars, var_name);
  3444. if (delete_var != NULL && strcmp(delete_var, "on") == 0) {
  3445. snac.config = xs_dict_set(snac.config, uploads[n], "");
  3446. }
  3447. }
  3448. /* password change? */
  3449. if ((p1 = xs_dict_get(p_vars, "passwd1")) != NULL &&
  3450. (p2 = xs_dict_get(p_vars, "passwd2")) != NULL &&
  3451. *p1 && strcmp(p1, p2) == 0) {
  3452. xs *pw = hash_password(snac.uid, p1, NULL);
  3453. snac.config = xs_dict_set(snac.config, "passwd", pw);
  3454. }
  3455. user_persist(&snac, 1);
  3456. status = HTTP_STATUS_SEE_OTHER;
  3457. }
  3458. else
  3459. if (p_path && strcmp(p_path, "admin/clear-notifications") == 0) { /** **/
  3460. notify_clear(&snac);
  3461. timeline_touch(&snac);
  3462. status = HTTP_STATUS_SEE_OTHER;
  3463. }
  3464. else
  3465. if (p_path && strcmp(p_path, "admin/vote") == 0) { /** **/
  3466. const char *irt = xs_dict_get(p_vars, "irt");
  3467. const char *opt = xs_dict_get(p_vars, "question");
  3468. const char *actor = xs_dict_get(p_vars, "actor");
  3469. xs *ls = NULL;
  3470. /* multiple choices? */
  3471. if (xs_type(opt) == XSTYPE_LIST)
  3472. ls = xs_dup(opt);
  3473. else
  3474. if (xs_type(opt) == XSTYPE_STRING) {
  3475. ls = xs_list_new();
  3476. ls = xs_list_append(ls, opt);
  3477. }
  3478. const xs_str *v;
  3479. int c = 0;
  3480. while (xs_list_next(ls, &v, &c)) {
  3481. xs *msg = msg_note(&snac, "", actor, irt, NULL, 1, NULL);
  3482. /* set the option */
  3483. msg = xs_dict_append(msg, "name", v);
  3484. xs *c_msg = msg_create(&snac, msg);
  3485. enqueue_message(&snac, c_msg);
  3486. timeline_add(&snac, xs_dict_get(msg, "id"), msg);
  3487. }
  3488. if (ls != NULL) {
  3489. /* get the poll object */
  3490. xs *poll = NULL;
  3491. if (valid_status(object_get(irt, &poll))) {
  3492. const char *date = xs_dict_get(poll, "endTime");
  3493. if (xs_is_null(date))
  3494. date = xs_dict_get(poll, "closed");
  3495. if (!xs_is_null(date)) {
  3496. time_t t = xs_parse_iso_date(date, 0) - time(NULL);
  3497. /* request the poll when it's closed;
  3498. Pleroma does not send and update when the poll closes */
  3499. enqueue_object_request(&snac, irt, t + 2);
  3500. }
  3501. }
  3502. }
  3503. status = HTTP_STATUS_SEE_OTHER;
  3504. }
  3505. if (status == HTTP_STATUS_SEE_OTHER) {
  3506. const char *redir = xs_dict_get(p_vars, "redir");
  3507. if (xs_is_null(redir))
  3508. redir = "top";
  3509. *body = xs_fmt("%s/admin#%s", snac.actor, redir);
  3510. *b_size = strlen(*body);
  3511. }
  3512. user_free(&snac);
  3513. return status;
  3514. }
  3515. xs_str *timeline_to_rss(snac *user, const xs_list *timeline,
  3516. const char *title, const char *link, const char *desc)
  3517. /* converts a timeline to rss */
  3518. {
  3519. xs_html *rss = xs_html_tag("rss",
  3520. xs_html_attr("xmlns:content", "http:/" "/purl.org/rss/1.0/modules/content/"),
  3521. xs_html_attr("version", "2.0"),
  3522. xs_html_attr("xmlns:atom", "http:/" "/www.w3.org/2005/Atom"));
  3523. xs_html *channel = xs_html_tag("channel",
  3524. xs_html_tag("title",
  3525. xs_html_text(title)),
  3526. xs_html_tag("language",
  3527. xs_html_text("en")),
  3528. xs_html_tag("link",
  3529. xs_html_text(link)),
  3530. xs_html_sctag("atom:link",
  3531. xs_html_attr("href", link),
  3532. xs_html_attr("rel", "self"),
  3533. xs_html_attr("type", "application/rss+xml")),
  3534. xs_html_tag("generator",
  3535. xs_html_text(USER_AGENT)),
  3536. xs_html_tag("description",
  3537. xs_html_text(desc)));
  3538. xs_html_add(rss, channel);
  3539. int cnt = 0;
  3540. const char *v;
  3541. xs_list_foreach(timeline, v) {
  3542. xs *msg = NULL;
  3543. if (user) {
  3544. if (!valid_status(timeline_get_by_md5(user, v, &msg)))
  3545. continue;
  3546. }
  3547. else {
  3548. if (!valid_status(object_get_by_md5(v, &msg)))
  3549. continue;
  3550. }
  3551. const char *id = xs_dict_get(msg, "id");
  3552. const char *content = xs_dict_get(msg, "content");
  3553. const char *published = xs_dict_get(msg, "published");
  3554. if (user && !xs_startswith(id, user->actor))
  3555. continue;
  3556. if (!id || !content || !published)
  3557. continue;
  3558. /* create a title with the first line of the content */
  3559. xs *title = xs_replace(content, "<br>", "\n");
  3560. title = xs_regex_replace_i(title, "<[^>]+>", " ");
  3561. title = xs_regex_replace_i(title, "&[^;]+;", " ");
  3562. int i;
  3563. for (i = 0; title[i] && title[i] != '\n' && i < 50; i++);
  3564. if (title[i] != '\0') {
  3565. title[i] = '\0';
  3566. title = xs_str_cat(title, "...");
  3567. }
  3568. title = xs_strip_i(title);
  3569. /* convert the date */
  3570. time_t t = xs_parse_iso_date(published, 0);
  3571. xs *rss_date = xs_str_utctime(t, "%a, %d %b %Y %T +0000");
  3572. /* if it's the first one, add it to the header */
  3573. if (cnt == 0)
  3574. xs_html_add(channel,
  3575. xs_html_tag("lastBuildDate",
  3576. xs_html_text(rss_date)));
  3577. xs_html_add(channel,
  3578. xs_html_tag("item",
  3579. xs_html_tag("title",
  3580. xs_html_text(title)),
  3581. xs_html_tag("link",
  3582. xs_html_text(id)),
  3583. xs_html_tag("guid",
  3584. xs_html_text(id)),
  3585. xs_html_tag("pubDate",
  3586. xs_html_text(rss_date)),
  3587. xs_html_tag("description",
  3588. xs_html_text(content))));
  3589. cnt++;
  3590. }
  3591. return xs_html_render_s(rss, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
  3592. }