html.c 144 KB

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