html.c 218 KB

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