html.c 145 KB

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