html.c 203 KB

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