html.c 131 KB

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