html.c 165 KB

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