1
0

html.c 208 KB

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