html.c 161 KB

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