html.c 149 KB

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