html.c 221 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176
  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 xs_val *show_unlisted = xs_dict_get(user->config, "show_unlisted");
  1350. const char *latitude = xs_dict_get_def(user->config, "latitude", "");
  1351. const char *longitude = xs_dict_get_def(user->config, "longitude", "");
  1352. const char *webhook = xs_dict_get_def(user->config, "notify_webhook", "");
  1353. const char *post_langs = xs_dict_get_def(user->config, "post_langs", "");
  1354. xs *metadata = NULL;
  1355. const xs_dict *md = xs_dict_get(user->config, "metadata");
  1356. if (xs_type(md) == XSTYPE_DICT) {
  1357. const xs_str *k;
  1358. const xs_str *v;
  1359. metadata = xs_str_new(NULL);
  1360. xs_dict_foreach(md, k, v) {
  1361. xs *kp = xs_fmt("%s=%s", k, v);
  1362. if (*metadata)
  1363. metadata = xs_str_cat(metadata, "\n");
  1364. metadata = xs_str_cat(metadata, kp);
  1365. }
  1366. }
  1367. else
  1368. if (xs_type(md) == XSTYPE_STRING)
  1369. metadata = xs_dup(md);
  1370. else
  1371. metadata = xs_str_new(NULL);
  1372. /* ui language */
  1373. xs_html *lang_select = xs_html_tag("select",
  1374. xs_html_attr("name", "web_ui_lang"));
  1375. const char *u_lang = xs_dict_get_def(user->config, "lang", "en");
  1376. const char *lang;
  1377. const xs_dict *langs;
  1378. xs_dict_foreach(srv_langs, lang, langs) {
  1379. if (strcmp(u_lang, lang) == 0)
  1380. xs_html_add(lang_select,
  1381. xs_html_tag("option",
  1382. xs_html_text(lang),
  1383. xs_html_attr("value", lang),
  1384. xs_html_attr("selected", "selected")));
  1385. else
  1386. xs_html_add(lang_select,
  1387. xs_html_tag("option",
  1388. xs_html_text(lang),
  1389. xs_html_attr("value", lang)));
  1390. }
  1391. /* timezone */
  1392. xs_html *tz_select = xs_html_tag("select",
  1393. xs_html_attr("name", "tz"));
  1394. xs *tzs = xs_tz_list();
  1395. const char *tz;
  1396. xs_list_foreach(tzs, tz) {
  1397. if (strcmp(tz, user->tz) == 0)
  1398. xs_html_add(tz_select,
  1399. xs_html_tag("option",
  1400. xs_html_text(tz),
  1401. xs_html_attr("value", tz),
  1402. xs_html_attr("selected", "selected")));
  1403. else
  1404. xs_html_add(tz_select,
  1405. xs_html_tag("option",
  1406. xs_html_text(tz),
  1407. xs_html_attr("value", tz)));
  1408. }
  1409. xs *user_setup_action = xs_fmt("%s/admin/user-setup", user->actor);
  1410. xs_html_add(top_controls,
  1411. xs_html_tag("details",
  1412. xs_html_tag("summary",
  1413. xs_html_text(L("User Settings..."))),
  1414. xs_html_tag("div",
  1415. xs_html_attr("class", "snac-user-setup"),
  1416. xs_html_tag("form",
  1417. xs_html_attr("autocomplete", "off"),
  1418. xs_html_attr("method", "post"),
  1419. xs_html_attr("action", user_setup_action),
  1420. xs_html_attr("enctype", "multipart/form-data"),
  1421. xs_html_tag("p",
  1422. xs_html_text(L("Display name:")),
  1423. xs_html_sctag("br", NULL),
  1424. xs_html_sctag("input",
  1425. xs_html_attr("type", "text"),
  1426. xs_html_attr("name", "name"),
  1427. xs_html_attr("value", xs_dict_get(user->config, "name")),
  1428. xs_html_attr("placeholder", L("Your name")))),
  1429. xs_html_tag("p",
  1430. xs_html_text(L("Avatar: ")),
  1431. xs_html_sctag("input",
  1432. xs_html_attr("type", "file"),
  1433. xs_html_attr("name", "avatar_file"))),
  1434. xs_html_tag("p",
  1435. xs_html_sctag("input",
  1436. xs_html_attr("type", "checkbox"),
  1437. xs_html_attr("name", "avatar_delete")),
  1438. xs_html_text(L("Delete current avatar"))),
  1439. xs_html_tag("p",
  1440. xs_html_text(L("Header image (banner): ")),
  1441. xs_html_sctag("input",
  1442. xs_html_attr("type", "file"),
  1443. xs_html_attr("name", "header_file"))),
  1444. xs_html_tag("p",
  1445. xs_html_sctag("input",
  1446. xs_html_attr("type", "checkbox"),
  1447. xs_html_attr("name", "header_delete")),
  1448. xs_html_text(L("Delete current header image"))),
  1449. xs_html_tag("p",
  1450. xs_html_text(L("Bio:")),
  1451. xs_html_sctag("br", NULL),
  1452. xs_html_tag("textarea",
  1453. xs_html_attr("name", "bio"),
  1454. xs_html_attr("cols", "40"),
  1455. xs_html_attr("rows", "4"),
  1456. xs_html_attr("placeholder", L("Write about yourself here...")),
  1457. xs_html_text(xs_dict_get(user->config, "bio")))),
  1458. xs_html_sctag("input",
  1459. xs_html_attr("type", "checkbox"),
  1460. xs_html_attr("name", "cw"),
  1461. xs_html_attr("id", "cw"),
  1462. xs_html_attr(strcmp(cw, "open") == 0 ? "checked" : "", NULL)),
  1463. xs_html_tag("label",
  1464. xs_html_attr("for", "cw"),
  1465. xs_html_text(L("Always show sensitive content"))),
  1466. xs_html_tag("p",
  1467. xs_html_text(L("Email address for notifications:")),
  1468. xs_html_sctag("br", NULL),
  1469. xs_html_sctag("input",
  1470. xs_html_attr("type", "text"),
  1471. xs_html_attr("name", "email"),
  1472. xs_html_attr("value", email),
  1473. xs_html_attr("placeholder", "bob@example.com"))),
  1474. xs_html_tag("p",
  1475. xs_html_text(L("Telegram notifications (bot key and chat id):")),
  1476. xs_html_sctag("br", NULL),
  1477. xs_html_sctag("input",
  1478. xs_html_attr("type", "text"),
  1479. xs_html_attr("name", "telegram_bot"),
  1480. xs_html_attr("value", telegram_bot),
  1481. xs_html_attr("placeholder", L("Bot API key"))),
  1482. xs_html_text(" "),
  1483. xs_html_sctag("input",
  1484. xs_html_attr("type", "text"),
  1485. xs_html_attr("name", "telegram_chat_id"),
  1486. xs_html_attr("value", telegram_chat_id),
  1487. xs_html_attr("placeholder", L("Chat id")))),
  1488. xs_html_tag("p",
  1489. xs_html_text(L("ntfy notifications (ntfy server and token):")),
  1490. xs_html_sctag("br", NULL),
  1491. xs_html_sctag("input",
  1492. xs_html_attr("type", "text"),
  1493. xs_html_attr("name", "ntfy_server"),
  1494. xs_html_attr("value", ntfy_server),
  1495. xs_html_attr("placeholder", L("ntfy server - full URL (example: https://ntfy.sh/YourTopic)"))),
  1496. xs_html_text(" "),
  1497. xs_html_sctag("input",
  1498. xs_html_attr("type", "text"),
  1499. xs_html_attr("name", "ntfy_token"),
  1500. xs_html_attr("value", ntfy_token),
  1501. xs_html_attr("placeholder", L("ntfy token - if needed")))),
  1502. xs_html_tag("p",
  1503. xs_html_text(L("Notify webhook:")),
  1504. xs_html_sctag("br", NULL),
  1505. xs_html_sctag("input",
  1506. xs_html_attr("type", "url"),
  1507. xs_html_attr("name", "notify_webhook"),
  1508. xs_html_attr("value", webhook),
  1509. xs_html_attr("placeholder", L("http://example.com/webhook")))),
  1510. xs_html_tag("p",
  1511. xs_html_text(L("Maximum days to keep posts (0: server settings):")),
  1512. xs_html_sctag("br", NULL),
  1513. xs_html_sctag("input",
  1514. xs_html_attr("type", "number"),
  1515. xs_html_attr("name", "purge_days"),
  1516. xs_html_attr("value", purge_days))),
  1517. html_checkbox("drop_dm_from_unknown", L("Drop direct messages from people you don't follow"),
  1518. xs_is_true(d_dm_f_u)),
  1519. html_checkbox("bot", L("This account is a bot"),
  1520. xs_is_true(bot)),
  1521. html_checkbox("auto_boost", L("Auto-boost all mentions to this account"),
  1522. xs_is_true(auto_boost)),
  1523. html_checkbox("private", L("This account is private "
  1524. "(posts are not shown through the web)"),
  1525. xs_is_true(a_private)),
  1526. html_checkbox("collapse_threads", L("Collapse top threads by default"),
  1527. xs_is_true(coll_thrds)),
  1528. html_checkbox("approve_followers", L("Follow requests must be approved"),
  1529. xs_is_true(pending)),
  1530. html_checkbox("show_contact_metrics", L("Publish follower and following metrics"),
  1531. xs_is_true(show_foll)),
  1532. html_checkbox("show_unlisted", L("Show unlisted posts in the public page and RSS feed"),
  1533. xs_is_true(show_unlisted)),
  1534. xs_html_tag("p",
  1535. xs_html_text(L("Current location:")),
  1536. xs_html_sctag("br", NULL),
  1537. xs_html_sctag("input",
  1538. xs_html_attr("type", "text"),
  1539. xs_html_attr("name", "latitude"),
  1540. xs_html_attr("value", latitude),
  1541. xs_html_attr("placeholder", "latitude")),
  1542. xs_html_text(" "),
  1543. xs_html_sctag("input",
  1544. xs_html_attr("type", "text"),
  1545. xs_html_attr("name", "longitude"),
  1546. xs_html_attr("value", longitude),
  1547. xs_html_attr("placeholder", "longitude"))),
  1548. xs_html_tag("p",
  1549. xs_html_text(L("Profile metadata (key=value pairs in each line):")),
  1550. xs_html_sctag("br", NULL),
  1551. xs_html_tag("textarea",
  1552. xs_html_attr("name", "metadata"),
  1553. xs_html_attr("cols", "40"),
  1554. xs_html_attr("rows", "4"),
  1555. xs_html_attr("placeholder", "Blog=https:/"
  1556. "/example.com/my-blog\nGPG Key=1FA54\n..."),
  1557. xs_html_text(metadata))),
  1558. xs_html_tag("p",
  1559. xs_html_text(L("Web interface language:")),
  1560. xs_html_sctag("br", NULL),
  1561. lang_select),
  1562. xs_html_tag("p",
  1563. xs_html_text(L("Time zone:")),
  1564. xs_html_sctag("br", NULL),
  1565. tz_select),
  1566. xs_html_tag("p",
  1567. xs_html_text(L("Languages you usually post in:")),
  1568. xs_html_sctag("br", NULL),
  1569. xs_html_sctag("input",
  1570. xs_html_attr("type", "text"),
  1571. xs_html_attr("name", "post_langs"),
  1572. xs_html_attr("value", post_langs),
  1573. xs_html_attr("placeholder", L("en fr es de_AT")))),
  1574. xs_html_tag("p",
  1575. xs_html_text(L("New password:")),
  1576. xs_html_sctag("br", NULL),
  1577. xs_html_sctag("input",
  1578. xs_html_attr("type", "password"),
  1579. xs_html_attr("name", "passwd1"),
  1580. xs_html_attr("value", ""))),
  1581. xs_html_tag("p",
  1582. xs_html_text(L("Repeat new password:")),
  1583. xs_html_sctag("br", NULL),
  1584. xs_html_sctag("input",
  1585. xs_html_attr("type", "password"),
  1586. xs_html_attr("name", "passwd2"),
  1587. xs_html_attr("value", ""))),
  1588. xs_html_sctag("input",
  1589. xs_html_attr("type", "submit"),
  1590. xs_html_attr("class", "button"),
  1591. xs_html_attr("value", L("Update user info"))),
  1592. xs_html_tag("p", NULL)))));
  1593. xs *followed_hashtags_action = xs_fmt("%s/admin/followed-hashtags", user->actor);
  1594. xs *followed_hashtags = xs_join(xs_dict_get_def(user->config,
  1595. "followed_hashtags", xs_stock(XSTYPE_LIST)), "\n");
  1596. xs_html_add(top_controls,
  1597. xs_html_tag("details",
  1598. xs_html_tag("summary",
  1599. xs_html_text(L("Followed hashtags..."))),
  1600. xs_html_tag("p",
  1601. xs_html_text(L("One hashtag per line"))),
  1602. xs_html_tag("div",
  1603. xs_html_attr("class", "snac-followed-hashtags"),
  1604. xs_html_tag("form",
  1605. xs_html_attr("autocomplete", "off"),
  1606. xs_html_attr("method", "post"),
  1607. xs_html_attr("action", followed_hashtags_action),
  1608. xs_html_attr("enctype", "multipart/form-data"),
  1609. xs_html_tag("textarea",
  1610. xs_html_attr("name", "followed_hashtags"),
  1611. xs_html_attr("cols", "40"),
  1612. xs_html_attr("rows", "4"),
  1613. xs_html_attr("placeholder", "#cats\n#windowfriday\n#classicalmusic\nhttps:/"
  1614. "/mastodon.social/tags/dogs"),
  1615. xs_html_text(followed_hashtags)),
  1616. xs_html_tag("br", NULL),
  1617. xs_html_sctag("input",
  1618. xs_html_attr("type", "submit"),
  1619. xs_html_attr("class", "button"),
  1620. xs_html_attr("value", L("Update hashtags")))))));
  1621. xs *blocked_hashtags_action = xs_fmt("%s/admin/blocked-hashtags", user->actor);
  1622. xs *blocked_hashtags = xs_join(xs_dict_get_def(user->config,
  1623. "blocked_hashtags", xs_stock(XSTYPE_LIST)), "\n");
  1624. xs_html_add(top_controls,
  1625. xs_html_tag("details",
  1626. xs_html_tag("summary",
  1627. xs_html_text(L("Blocked hashtags..."))),
  1628. xs_html_tag("p",
  1629. xs_html_text(L("One hashtag per line"))),
  1630. xs_html_tag("div",
  1631. xs_html_attr("class", "snac-blocked-hashtags"),
  1632. xs_html_tag("form",
  1633. xs_html_attr("autocomplete", "off"),
  1634. xs_html_attr("method", "post"),
  1635. xs_html_attr("action", blocked_hashtags_action),
  1636. xs_html_attr("enctype", "multipart/form-data"),
  1637. xs_html_tag("textarea",
  1638. xs_html_attr("name", "blocked_hashtags"),
  1639. xs_html_attr("cols", "40"),
  1640. xs_html_attr("rows", "4"),
  1641. xs_html_attr("placeholder", "#cats\n#windowfriday\n#classicalmusic"),
  1642. xs_html_text(blocked_hashtags)),
  1643. xs_html_tag("br", NULL),
  1644. xs_html_sctag("input",
  1645. xs_html_attr("type", "submit"),
  1646. xs_html_attr("class", "button"),
  1647. xs_html_attr("value", L("Update hashtags")))))));
  1648. xs *muted_words_action = xs_fmt("%s/admin/muted-words", user->actor);
  1649. xs *muted_words = xs_join(xs_dict_get_def(user->config,
  1650. "muted_words", xs_stock(XSTYPE_LIST)), "\n");
  1651. xs_html_add(top_controls,
  1652. xs_html_tag("details",
  1653. xs_html_tag("summary",
  1654. xs_html_text(L("Muted words..."))),
  1655. xs_html_tag("p",
  1656. xs_html_text(L("One word per line, partial matches count"))),
  1657. xs_html_tag("div",
  1658. xs_html_attr("class", "snac-muted-words"),
  1659. xs_html_tag("form",
  1660. xs_html_attr("autocomplete", "off"),
  1661. xs_html_attr("method", "post"),
  1662. xs_html_attr("action", muted_words_action),
  1663. xs_html_attr("enctype", "multipart/form-data"),
  1664. xs_html_tag("textarea",
  1665. xs_html_attr("name", "muted_words"),
  1666. xs_html_attr("cols", "40"),
  1667. xs_html_attr("rows", "4"),
  1668. xs_html_attr("placeholder", "nascar\nsuperbowl\nFIFA"),
  1669. xs_html_text(muted_words)),
  1670. xs_html_tag("br", NULL),
  1671. xs_html_sctag("input",
  1672. xs_html_attr("type", "submit"),
  1673. xs_html_attr("class", "button"),
  1674. xs_html_attr("value", L("Update muted words")))))));
  1675. return top_controls;
  1676. }
  1677. static xs_html *html_button(const char *clss, const char *label, const char *hint)
  1678. {
  1679. xs *c = xs_fmt("snac-btn-%s", clss);
  1680. /* use an NULL tag to separate non-css-classed buttons from one another */
  1681. return xs_html_container(
  1682. xs_html_sctag("input",
  1683. xs_html_attr("type", "submit"),
  1684. xs_html_attr("name", "action"),
  1685. xs_html_attr("class", c),
  1686. xs_html_attr("value", label),
  1687. xs_html_attr("title", hint)),
  1688. xs_html_text("\n"));
  1689. }
  1690. xs_str *build_mentions(snac *user, const xs_dict *msg)
  1691. /* returns a string with the mentions in msg */
  1692. {
  1693. xs_str *s = xs_str_new(NULL);
  1694. const char *list = xs_dict_get(msg, "tag");
  1695. const char *v;
  1696. int c = 0;
  1697. while (xs_list_next(list, &v, &c)) {
  1698. const char *type = xs_dict_get(v, "type");
  1699. const char *href = xs_dict_get(v, "href");
  1700. const char *name = xs_dict_get(v, "name");
  1701. if (type && strcmp(type, "Mention") == 0 &&
  1702. href && strcmp(href, user->actor) != 0 && name) {
  1703. xs *s1 = NULL;
  1704. if (name[0] != '@') {
  1705. s1 = xs_fmt("@%s", name);
  1706. name = s1;
  1707. }
  1708. xs *l = xs_split(name, "@");
  1709. /* is it a name without a host? */
  1710. if (xs_list_len(l) < 3) {
  1711. /* split the href and pick the host name LIKE AN ANIMAL */
  1712. /* would be better to query the webfinger but *won't do that* here */
  1713. xs *l2 = xs_split(href, "/");
  1714. if (xs_list_len(l2) >= 3) {
  1715. xs *s1 = xs_fmt("%s@%s ", name, xs_list_get(l2, 2));
  1716. if (xs_str_in(s, s1) == -1)
  1717. s = xs_str_cat(s, s1);
  1718. }
  1719. }
  1720. else {
  1721. if (xs_str_in(s, name) == -1) {
  1722. s = xs_str_cat(s, name);
  1723. s = xs_str_cat(s, " ");
  1724. }
  1725. }
  1726. }
  1727. }
  1728. if (*s) {
  1729. xs *s1 = s;
  1730. s = xs_fmt("\n\n\nCC: %s", s1);
  1731. }
  1732. return s;
  1733. }
  1734. xs_html *html_entry_controls(snac *user, const char *actor,
  1735. const xs_dict *msg, const char *md5)
  1736. {
  1737. const char *id = xs_dict_get(msg, "id");
  1738. const char *group = xs_dict_get(msg, "audience");
  1739. xs *likes = object_likes(id);
  1740. xs *boosts = object_announces(id);
  1741. xs *action = xs_fmt("%s/admin/action", user->actor);
  1742. xs *redir = xs_fmt("%s_entry", md5);
  1743. xs_html *form;
  1744. xs_html *controls = xs_html_tag("div",
  1745. xs_html_attr("class", "snac-controls"),
  1746. form = xs_html_tag("form",
  1747. xs_html_attr("autocomplete", "off"),
  1748. xs_html_attr("method", "post"),
  1749. xs_html_attr("action", action),
  1750. xs_html_sctag("input",
  1751. xs_html_attr("type", "hidden"),
  1752. xs_html_attr("name", "id"),
  1753. xs_html_attr("value", id)),
  1754. xs_html_sctag("input",
  1755. xs_html_attr("type", "hidden"),
  1756. xs_html_attr("name", "actor"),
  1757. xs_html_attr("value", actor)),
  1758. xs_html_sctag("input",
  1759. xs_html_attr("type", "hidden"),
  1760. xs_html_attr("name", "group"),
  1761. xs_html_attr("value", xs_is_null(group) ? "" : group)),
  1762. xs_html_sctag("input",
  1763. xs_html_attr("type", "hidden"),
  1764. xs_html_attr("name", "redir"),
  1765. xs_html_attr("value", redir))));
  1766. if (!is_msg_mine(user, id)) {
  1767. if (xs_list_in(likes, user->md5) == -1) {
  1768. /* not already liked; add button */
  1769. xs_html_add(form,
  1770. html_button("like", L("Like"), L("Say you like this post")));
  1771. }
  1772. else {
  1773. /* not like it anymore */
  1774. xs_html_add(form,
  1775. html_button("unlike", L("Unlike"), L("Nah don't like it that much")));
  1776. }
  1777. }
  1778. else {
  1779. if (is_pinned(user, id))
  1780. xs_html_add(form,
  1781. html_button("unpin", L("Unpin"), L("Unpin this post from your timeline")));
  1782. else
  1783. xs_html_add(form,
  1784. html_button("pin", L("Pin"), L("Pin this post to the top of your timeline")));
  1785. }
  1786. if (is_msg_public(msg)) {
  1787. if (xs_list_in(boosts, user->md5) == -1) {
  1788. /* not already boosted; add button */
  1789. xs_html_add(form,
  1790. html_button("boost", L("Boost"), L("Announce this post to your followers")));
  1791. }
  1792. else {
  1793. /* already boosted; add button to regret */
  1794. xs_html_add(form,
  1795. html_button("unboost", L("Unboost"), L("I regret I boosted this")));
  1796. }
  1797. }
  1798. if (is_bookmarked(user, id))
  1799. xs_html_add(form,
  1800. html_button("unbookmark", L("Unbookmark"), L("Delete this post from your bookmarks")));
  1801. else
  1802. xs_html_add(form,
  1803. html_button("bookmark", L("Bookmark"), L("Add this post to your bookmarks")));
  1804. if (strcmp(actor, user->actor) != 0) {
  1805. /* controls for other actors than this one */
  1806. if (following_check(user, actor)) {
  1807. xs_html_add(form,
  1808. html_button("unfollow", L("Unfollow"), L("Stop following this user's activity")));
  1809. }
  1810. else {
  1811. xs_html_add(form,
  1812. html_button("follow", L("Follow"), L("Start following this user's activity")));
  1813. }
  1814. if (!xs_is_null(group)) {
  1815. if (following_check(user, group)) {
  1816. xs_html_add(form,
  1817. html_button("unfollow", L("Unfollow Group"),
  1818. L("Stop following this group or channel")));
  1819. }
  1820. else {
  1821. xs_html_add(form,
  1822. html_button("follow", L("Follow Group"),
  1823. L("Start following this group or channel")));
  1824. }
  1825. }
  1826. xs_html_add(form,
  1827. html_button("mute", L("MUTE"),
  1828. L("Block any activity from this user forever")));
  1829. }
  1830. if (!xs_is_true(xs_dict_get(srv_config, "hide_delete_post_button")))
  1831. xs_html_add(form,
  1832. html_button("delete", L("Delete"), L("Delete this post")));
  1833. xs_html_add(form,
  1834. html_button("hide", L("Hide"), L("Hide this post and its children")));
  1835. const char *prev_src = xs_dict_get(msg, "sourceContent");
  1836. if (!xs_is_null(prev_src) && strcmp(actor, user->actor) == 0) { /** edit **/
  1837. /* post can be edited */
  1838. xs *div_id = xs_fmt("%s_edit", md5);
  1839. xs *form_id = xs_fmt("%s_edit_form", md5);
  1840. xs *redir = xs_fmt("%s_entry", md5);
  1841. xs *att_files = xs_list_new();
  1842. xs *att_alt_texts = xs_list_new();
  1843. const xs_list *att_list = xs_dict_get(msg, "attachment");
  1844. if (xs_is_list(att_list)) {
  1845. const xs_dict *d;
  1846. xs_list_foreach(att_list, d) {
  1847. const char *att_file = xs_dict_get(d, "url");
  1848. const char *att_alt_text = xs_dict_get(d, "name");
  1849. if (xs_is_string(att_file) && xs_is_string(att_alt_text)) {
  1850. att_files = xs_list_append(att_files, att_file);
  1851. att_alt_texts = xs_list_append(att_alt_texts, att_alt_text);
  1852. }
  1853. }
  1854. }
  1855. const char *note_lang = NULL;
  1856. const xs_dict *cmap = xs_dict_get(msg, "contentMap");
  1857. if (xs_is_dict(cmap)) {
  1858. const char *dummy;
  1859. int c = 0;
  1860. xs_dict_next(cmap, &note_lang, &dummy, &c);
  1861. }
  1862. const int edit_scope = get_msg_visibility(msg);
  1863. xs_html_add(controls, xs_html_tag("div",
  1864. xs_html_tag("p", NULL),
  1865. html_note(user, L("Edit..."),
  1866. div_id, form_id,
  1867. "", prev_src,
  1868. id, NULL,
  1869. xs_dict_get(msg, "sensitive"), xs_dict_get(msg, "summary"),
  1870. edit_scope, redir,
  1871. NULL, 0, att_files, att_alt_texts, is_draft(user, id),
  1872. xs_dict_get(msg, "published"), note_lang)),
  1873. xs_html_tag("p", NULL));
  1874. }
  1875. if (!xs_is_true(xs_dict_get(srv_config, "disable_emojireact"))) { /** emoji react **/
  1876. /* the post textarea */
  1877. xs *div_id = xs_fmt("%s_reply", md5);
  1878. xs *form_id = xs_fmt("%s_reply_form", md5);
  1879. xs *e_react = emoji_reacted(user, id);
  1880. xs_html_add(controls, xs_html_tag("div",
  1881. xs_html_tag("p", NULL),
  1882. html_emoji(
  1883. user, L("Emoji react..."),
  1884. div_id, form_id,
  1885. ":neocat:", id,
  1886. e_react)),
  1887. xs_html_tag("p", NULL));
  1888. }
  1889. { /** reply **/
  1890. /* the post textarea */
  1891. xs *ct = build_mentions(user, msg);
  1892. xs *div_id = xs_fmt("%s_reply", md5);
  1893. xs *form_id = xs_fmt("%s_reply_form", md5);
  1894. xs *redir = xs_fmt("%s_entry", md5);
  1895. const int scope = get_msg_visibility(msg);
  1896. xs_html_add(controls, xs_html_tag("div",
  1897. xs_html_tag("p", NULL),
  1898. html_note(user, L("Reply..."),
  1899. div_id, form_id,
  1900. "", ct,
  1901. NULL, NULL,
  1902. xs_dict_get(msg, "sensitive"), xs_dict_get(msg, "summary"),
  1903. scope, redir,
  1904. id, 0, NULL, NULL, 0, NULL, NULL)),
  1905. xs_html_tag("p", NULL));
  1906. }
  1907. return controls;
  1908. }
  1909. static const xs_str* words_in_content(const xs_list *words, const xs_val *content)
  1910. /* returns a word that matches any of the words in content */
  1911. {
  1912. if (!xs_is_list(words) || !xs_is_string(content)) {
  1913. return NULL;
  1914. }
  1915. xs *c = xs_split(content, " ");
  1916. xs *sc = xs_list_sort(c, NULL);
  1917. const xs_str *wv;
  1918. const xs_str *cv;
  1919. xs_list_foreach(words, wv) {
  1920. xs_list_foreach(sc, cv) {
  1921. xs_tolower_i((xs_str*)cv);
  1922. if(xs_str_in(cv, wv) != -1)
  1923. return wv;
  1924. }
  1925. }
  1926. return NULL;
  1927. }
  1928. xs_html *html_entry(snac *user, xs_dict *msg, int read_only,
  1929. int level, const char *md5, int hide_children)
  1930. {
  1931. const char *id = xs_dict_get(msg, "id");
  1932. const char *type = xs_dict_get(msg, "type");
  1933. const char *actor;
  1934. const char *v;
  1935. int has_title = 0;
  1936. int collapse_threads = 0;
  1937. const char *proxy = NULL;
  1938. if (user && !read_only && xs_is_true(xs_dict_get(srv_config, "proxy_media")))
  1939. proxy = user->actor;
  1940. /* do not show non-public messages in the public timeline */
  1941. if ((read_only || !user) && !is_msg_public(msg))
  1942. return NULL;
  1943. if (id && is_instance_blocked(id))
  1944. return NULL;
  1945. if (user && level == 0 && xs_is_true(xs_dict_get(user->config, "collapse_threads")))
  1946. collapse_threads = 1;
  1947. /* hidden? do nothing more for this conversation */
  1948. if (user && is_hidden(user, id)) {
  1949. xs *s1 = xs_fmt("%s_entry", md5);
  1950. /* return just an dummy anchor, to keep position after hitting 'Hide' */
  1951. return xs_html_tag("div",
  1952. xs_html_tag("a",
  1953. xs_html_attr("name", s1)));
  1954. }
  1955. /* avoid too deep nesting, as it may be a loop */
  1956. if (level >= MAX_CONVERSATION_LEVELS)
  1957. return xs_html_tag("mark",
  1958. xs_html_text(L("Truncated (too deep)")));
  1959. const char *lang = NULL;
  1960. const xs_dict *cmap = xs_dict_get(msg, "contentMap");
  1961. if (xs_is_dict(cmap)) {
  1962. const char *dummy;
  1963. int c = 0;
  1964. xs_dict_next(cmap, &lang, &dummy, &c);
  1965. }
  1966. if (strcmp(type, "Follow") == 0) {
  1967. return xs_html_tag("div",
  1968. xs_html_attr("class", "snac-post"),
  1969. xs_html_tag("div",
  1970. xs_html_attr("class", "snac-post-header"),
  1971. xs_html_tag("div",
  1972. xs_html_attr("class", "snac-origin"),
  1973. xs_html_text(L("follows you"))),
  1974. html_msg_icon(read_only ? NULL : user, xs_dict_get(msg, "actor"), msg, proxy, NULL, lang)));
  1975. }
  1976. else
  1977. if (!xs_match(type, POSTLIKE_OBJECT_TYPE)) {
  1978. /* skip oddities */
  1979. snac_debug(user, 1, xs_fmt("html_entry: ignoring object type '%s' %s", type, id));
  1980. return NULL;
  1981. }
  1982. /* ignore notes with "name", as they are votes to Questions */
  1983. if (strcmp(type, "Note") == 0 && !xs_is_null(xs_dict_get(msg, "name")))
  1984. return NULL;
  1985. /* get the attributedTo */
  1986. if ((actor = get_atto(msg)) == NULL)
  1987. return NULL;
  1988. /* ignore muted morons immediately */
  1989. if (user && is_muted(user, actor)) {
  1990. xs *s1 = xs_fmt("%s_entry", md5);
  1991. /* return just an dummy anchor, to keep position after hitting 'MUTE' */
  1992. return xs_html_tag("div",
  1993. xs_html_tag("a",
  1994. xs_html_attr("name", s1)));
  1995. }
  1996. /* don't show followers-only notes from not followed users */
  1997. if (user && get_msg_visibility(msg) == SCOPE_FOLLOWERS &&
  1998. strcmp(user->actor, actor) != 0 && following_check(user, actor) == 0) {
  1999. return NULL;
  2000. }
  2001. if ((user == NULL || strcmp(actor, user->actor) != 0)
  2002. && !valid_status(actor_get(actor, NULL))) {
  2003. if (user)
  2004. enqueue_actor_refresh(user, actor, 0);
  2005. return NULL;
  2006. }
  2007. /** html_entry top tag **/
  2008. xs_html *entry_top = xs_html_tag("div", NULL);
  2009. {
  2010. xs *s1 = xs_fmt("%s_entry", md5);
  2011. xs_html_add(entry_top,
  2012. xs_html_tag("a",
  2013. xs_html_attr("name", s1)));
  2014. }
  2015. xs_html *entry = xs_html_tag("div",
  2016. xs_html_attr("class", level == 0 ? "snac-post" : "snac-child"));
  2017. xs_html_add(entry_top,
  2018. entry);
  2019. /** post header **/
  2020. xs_html *score;
  2021. xs_html *post_header = xs_html_tag("div",
  2022. xs_html_attr("class", "snac-post-header"),
  2023. score = xs_html_tag("div",
  2024. xs_html_attr("class", "snac-score")));
  2025. xs_html_add(entry,
  2026. post_header);
  2027. if (user && is_pinned(user, id)) {
  2028. /* add a pin emoji */
  2029. xs_html_add(score,
  2030. xs_html_tag("span",
  2031. xs_html_attr("title", L("Pinned")),
  2032. xs_html_raw(" &#128204; ")));
  2033. }
  2034. if (user && !read_only && is_bookmarked(user, id)) {
  2035. /* add a bookmark emoji */
  2036. xs_html_add(score,
  2037. xs_html_tag("span",
  2038. xs_html_attr("title", L("Bookmarked")),
  2039. xs_html_raw(" &#128278; ")));
  2040. }
  2041. if (strcmp(type, "Question") == 0) {
  2042. /* add the ballot box emoji */
  2043. xs_html_add(score,
  2044. xs_html_tag("span",
  2045. xs_html_attr("title", L("Poll")),
  2046. xs_html_raw(" &#128499; ")));
  2047. if (user && was_question_voted(user, id)) {
  2048. /* add a check to show this poll was voted */
  2049. xs_html_add(score,
  2050. xs_html_tag("span",
  2051. xs_html_attr("title", L("Voted")),
  2052. xs_html_raw(" &#10003; ")));
  2053. }
  2054. }
  2055. if (strcmp(type, "Event") == 0) {
  2056. /* add the calendar emoji */
  2057. xs_html_add(score,
  2058. xs_html_tag("span",
  2059. xs_html_attr("title", L("Event")),
  2060. xs_html_raw(" &#128197; ")));
  2061. }
  2062. /* if it's a user from this same instance, add the score */
  2063. if (xs_startswith(id, srv_baseurl)) {
  2064. int n_likes = object_likes_len(id);
  2065. int n_boosts = object_announces_len(id);
  2066. /* alternate emojis: %d &#128077; %d &#128257; */
  2067. xs *s1 = xs_fmt("%d &#9733; %d &#8634;\n", n_likes, n_boosts);
  2068. xs_html_add(score,
  2069. xs_html_raw(s1));
  2070. }
  2071. xs *boosts = object_announces(id);
  2072. if (xs_list_len(boosts)) {
  2073. /* if somebody boosted this, show as origin */
  2074. const char *p = xs_list_get(boosts, -1);
  2075. xs *actor_r = NULL;
  2076. if (user && xs_list_in(boosts, user->md5) != -1) {
  2077. /* we boosted this */
  2078. xs_html_add(post_header,
  2079. xs_html_tag("div",
  2080. xs_html_attr("class", "snac-origin"),
  2081. xs_html_tag("a",
  2082. xs_html_attr("href", user->actor),
  2083. xs_html_text(xs_dict_get(user->config, "name"))),
  2084. xs_html_text(" "),
  2085. xs_html_text(L("boosted"))));
  2086. }
  2087. else
  2088. if (valid_status(object_get_by_md5(p, &actor_r))) {
  2089. xs *name = actor_name(actor_r, proxy);
  2090. if (!xs_is_null(name)) {
  2091. xs *href = NULL;
  2092. const char *id = xs_dict_get(actor_r, "id");
  2093. int fwers = 0;
  2094. int fwing = 0;
  2095. if (user != NULL) {
  2096. fwers = follower_check(user, id);
  2097. fwing = following_check(user, id);
  2098. }
  2099. if (!read_only && (fwers || fwing))
  2100. href = xs_fmt("%s/people/%s", user->actor, p);
  2101. else
  2102. href = xs_dup(id);
  2103. xs_html_add(post_header,
  2104. xs_html_tag("div",
  2105. xs_html_attr("class", "snac-origin"),
  2106. xs_html_tag("a",
  2107. xs_html_attr("href", href),
  2108. xs_html_raw(name)), /* already sanitized */
  2109. xs_html_text(" "),
  2110. xs_html_text(L("boosted"))));
  2111. }
  2112. }
  2113. }
  2114. if (user && strcmp(type, "Note") == 0) {
  2115. /* is the parent not here? */
  2116. const char *parent = get_in_reply_to(msg);
  2117. if (!xs_is_null(parent) && *parent) {
  2118. if (!timeline_here(user, parent)) {
  2119. xs_html_add(post_header,
  2120. xs_html_tag("div",
  2121. xs_html_attr("class", "snac-origin"),
  2122. xs_html_text(L("in reply to")),
  2123. xs_html_text(" "),
  2124. xs_html_tag("a",
  2125. xs_html_attr("href", parent),
  2126. xs_html_text("»"))));
  2127. }
  2128. }
  2129. }
  2130. xs_html_add(post_header,
  2131. html_msg_icon(read_only ? NULL : user, actor, msg, proxy, md5, lang));
  2132. /** post content **/
  2133. xs_html *snac_content_wrap = xs_html_tag("div",
  2134. xs_html_attr("class", "e-content snac-content"));
  2135. if (xs_is_string(lang))
  2136. xs_html_add(snac_content_wrap,
  2137. xs_html_attr("lang", lang));
  2138. xs_html_add(entry,
  2139. snac_content_wrap);
  2140. if (!has_title && !xs_is_null(v = xs_dict_get(msg, "name"))) {
  2141. xs_html_add(snac_content_wrap,
  2142. xs_html_tag("h3",
  2143. xs_html_attr("class", "snac-entry-title"),
  2144. xs_html_text(v)));
  2145. has_title = 1;
  2146. }
  2147. xs_html *snac_content = NULL;
  2148. v = xs_dict_get(msg, "summary");
  2149. /* if it has summary or marked as sensitive - add CW */
  2150. /* come clients don't set "sensitive" flag properly */
  2151. if ((!xs_is_null(v) && *v) || xs_type(xs_dict_get(msg, "sensitive")) == XSTYPE_TRUE) {
  2152. if (xs_is_null(v) || *v == '\0')
  2153. v = "...";
  2154. const char *cw = "";
  2155. if (user) {
  2156. /* only show it when not in the public timeline and the config setting is "open" */
  2157. cw = xs_dict_get(user->config, "cw");
  2158. if (xs_is_null(cw) || read_only)
  2159. cw = "";
  2160. }
  2161. /* summary can't contain HTML */
  2162. xs *s1 = xs_regex_replace(v, "<[^>]+>", "");
  2163. /* crop stupidly long summaries */
  2164. if (xs_utf8_len(s1) > 1024) {
  2165. s1 = xs_utf8_crop_i(s1, 0, 1024);
  2166. s1 = xs_str_cat(s1, "...");
  2167. }
  2168. snac_content = xs_html_tag("details",
  2169. xs_html_attr(cw, NULL),
  2170. xs_html_tag("summary",
  2171. xs_html_text(s1),
  2172. xs_html_text(L(" [SENSITIVE CONTENT]"))));
  2173. }
  2174. else
  2175. if (user &&
  2176. /* muted_words is all lowercase and sorted for performance */
  2177. (v = words_in_content(xs_dict_get(user->config, "muted_words"),
  2178. xs_dict_get(msg, "content"))) != NULL) {
  2179. snac_debug(user, 1, xs_fmt("word %s muted by user preferences: %s", v, id));
  2180. snac_content = xs_html_tag("details",
  2181. xs_html_tag("summary",
  2182. xs_html_text(L("Muted: ")),
  2183. xs_html_text(v)));
  2184. }
  2185. else {
  2186. snac_content = xs_html_tag("div", NULL);
  2187. }
  2188. xs_html_add(snac_content_wrap,
  2189. snac_content);
  2190. /* add all emoji reacts */
  2191. int is_emoji = 0;
  2192. if (!xs_is_true(xs_dict_get(srv_config, "disable_emojireact"))) {
  2193. int c = 0;
  2194. const xs_dict *k;
  2195. xs *ls = xs_list_new();
  2196. xs *sfrl = xs_dict_new();
  2197. xs *rl = object_get_emoji_reacts(id);
  2198. while (xs_list_next(rl, &v, &c)) {
  2199. xs *m = NULL;
  2200. if (valid_status(object_get_by_md5(v, &m))) {
  2201. const char *content = xs_dict_get(m, "content");
  2202. const char *actor = xs_dict_get(m, "actor");
  2203. const xs_list *contentl = xs_dict_get(sfrl, content);
  2204. if ((user && is_muted(user, actor)) || is_instance_blocked(actor))
  2205. continue;
  2206. xs *actors = xs_list_new();
  2207. actors = xs_list_append(actors, actor);
  2208. char me = actor && user && strcmp(actor, user->actor) == 0;
  2209. int count = 1;
  2210. if (contentl) {
  2211. count = atoi(xs_list_get(contentl, 0)) + 1;
  2212. const xs_list *actorsc = xs_list_get(contentl, 1);
  2213. if (strncmp(xs_list_get(contentl, 2), "1", 1) == 0)
  2214. me = 1;
  2215. if (xs_list_in(actorsc, actor) != -1) {
  2216. xs_free(actors);
  2217. actors = xs_dup(actorsc);
  2218. }
  2219. else
  2220. actors = xs_list_cat(actors, actorsc);
  2221. }
  2222. xs *fl = xs_list_new();
  2223. xs *c1 = xs_fmt("%d", count);
  2224. xs *c2 = xs_fmt("%d", me);
  2225. fl = xs_list_append(fl, c1, actors, c2);
  2226. sfrl = xs_dict_append(sfrl, content, fl);
  2227. }
  2228. }
  2229. c = 0;
  2230. while (xs_list_next(rl, &k, &c)) {
  2231. xs *m = NULL;
  2232. if (valid_status(object_get_by_md5(k, &m))) {
  2233. const xs_dict *tag = xs_dict_get(m, "tag");
  2234. const xs_dict *ide = xs_dict_get(m, "id");
  2235. const char *content = xs_dict_get(m, "content");
  2236. const char *shortname;
  2237. shortname = xs_dict_get(m, "content");
  2238. const xs_list *items = xs_dict_get(sfrl, content);
  2239. if (!xs_is_null(items)) {
  2240. const char *nb = xs_list_get(items, 0);
  2241. const xs_list *actors = xs_list_get(items, 1);
  2242. const char me = *xs_list_get(items, 2) == '1';
  2243. is_emoji = 1;
  2244. xs *al = xs_join(actors, ",\n\t");
  2245. xs *act = atoi(nb) > 1 ?
  2246. xs_fmt("%d different actors \n\t%s", atoi(nb), al) :
  2247. xs_dup(xs_dict_get(m, "actor"));
  2248. xs *class = xs_list_new();
  2249. class = xs_list_append(class, "snac-reaction");
  2250. xs_html *ret = NULL;
  2251. if (tag && shortname) {
  2252. xs *cl = xs_list_new();
  2253. cl = xs_list_append(cl, "snac-reaction-image");
  2254. xs *emoji = _replace_shortnames(xs_dup(shortname), tag, 2, proxy, cl, act);
  2255. emoji = xs_strip_chars_i(emoji, ":");
  2256. if (me)
  2257. class = xs_list_append(class, "snac-reacted");
  2258. xs *l1 = xs_join(class, " ");
  2259. ret = xs_html_tag("button",
  2260. xs_html_attr("type", "submit"),
  2261. xs_html_attr("name", "action"),
  2262. xs_html_attr("value", me ? L("EmojiReact") : L("EmojiUnreact")),
  2263. xs_html_raw(emoji),
  2264. xs_html_tag("span",
  2265. xs_html_raw(nb),
  2266. xs_html_attr("style", "padding-left: 5px;")),
  2267. xs_html_attr("title", act),
  2268. xs_html_attr("class", l1));
  2269. if (!(ide && xs_startswith(ide, srv_baseurl)))
  2270. xs_html_add(ret, xs_html_attr("disabled", "true"));
  2271. }
  2272. else if (shortname) {
  2273. xs *sn = xs_dup(shortname);
  2274. const char *sna = sn;
  2275. unsigned int utf = xs_utf8_dec((const char **)&sna);
  2276. if (xs_is_emoji(utf)) {
  2277. const char *style = "font-size: large;";
  2278. if (me)
  2279. class = xs_list_append(class, "snac-reacted");
  2280. xs *l1 = xs_join(class, " ");
  2281. xs *s1 = xs_fmt("&#%d", utf);
  2282. ret = xs_html_tag("button",
  2283. xs_html_attr("type", "submit"),
  2284. xs_html_attr("name", "action"),
  2285. xs_html_attr("value", me ? L("EmojiUnreact") : L("EmojiReact")),
  2286. xs_html_raw(s1),
  2287. xs_html_tag("span",
  2288. xs_html_raw(nb),
  2289. xs_html_attr("style", "font-size: initial; padding-left: 5px;")),
  2290. xs_html_attr("title", act),
  2291. xs_html_attr("class", l1),
  2292. xs_html_attr("style", style));
  2293. }
  2294. }
  2295. if (ret) {
  2296. xs *s1;
  2297. if (user) {
  2298. xs *action = xs_fmt("%s/admin/action", user->actor);
  2299. xs *form_id = xs_fmt("%s_reply_form", md5);
  2300. xs_html *form =
  2301. xs_html_tag("form",
  2302. xs_html_attr("autocomplete", "off"),
  2303. xs_html_attr("method", "post"),
  2304. xs_html_attr("action", action),
  2305. xs_html_attr("enctype", "multipart/form-data"),
  2306. xs_html_attr("style", "display: inline-flex;"
  2307. "vertical-align: middle;"),
  2308. xs_html_attr("id", form_id),
  2309. xs_html_sctag("input",
  2310. xs_html_attr("type", "hidden"),
  2311. xs_html_attr("name", "id"),
  2312. xs_html_attr("value", id)),
  2313. xs_html_sctag("input",
  2314. xs_html_attr("type", "hidden"),
  2315. xs_html_attr("name", "eid"),
  2316. xs_html_attr("value", shortname)),
  2317. ret);
  2318. s1 = xs_html_render(form);
  2319. }
  2320. else
  2321. s1 = xs_html_render(ret);
  2322. ls = xs_list_append(ls, s1);
  2323. sfrl = xs_dict_del(sfrl, content);
  2324. }
  2325. }
  2326. }
  2327. }
  2328. c = 0;
  2329. xs_html *emoji_div;
  2330. if (xs_list_len(ls) > 0) {
  2331. emoji_div = xs_html_tag("div", xs_html_text(L("Emoji reactions: ")),
  2332. xs_html_attr("class", "snac-reaction-div"));
  2333. while (ls != NULL && xs_list_next(ls, &k, &c))
  2334. xs_html_add(emoji_div, xs_html_raw(k));
  2335. xs_html_add(snac_content_wrap, emoji_div);
  2336. }
  2337. }
  2338. {
  2339. /** build the content string **/
  2340. const char *content = xs_dict_get(msg, "content");
  2341. if (xs_type(content) != XSTYPE_STRING) {
  2342. if (!xs_is_null(content))
  2343. srv_archive_error("unexpected_content_xstype",
  2344. "content field type", xs_stock(XSTYPE_DICT), msg);
  2345. content = "";
  2346. }
  2347. /* skip ugly line breaks at the beginning */
  2348. while (xs_startswith(content, "<br>"))
  2349. content += 4;
  2350. xs *c = sanitize(content);
  2351. /* do some tweaks to the content */
  2352. c = xs_replace_i(c, "\r", "");
  2353. while (xs_endswith(c, "<br><br>"))
  2354. c = xs_crop_i(c, 0, -4);
  2355. c = xs_replace_i(c, "<br><br>", "<p>");
  2356. if (is_emoji == 0)
  2357. c = xs_str_cat(c, "<p>");
  2358. /* replace the :shortnames: */
  2359. c = replace_shortnames(c, xs_dict_get(msg, "tag"), 2, proxy);
  2360. /* Peertube videos content is in markdown */
  2361. const char *mtype = xs_dict_get(msg, "mediaType");
  2362. if (xs_type(mtype) == XSTYPE_STRING && strcmp(mtype, "text/markdown") == 0) {
  2363. /* a full conversion could be better */
  2364. c = xs_replace_i(c, "\r", "");
  2365. c = xs_replace_i(c, "\n", "<br>");
  2366. }
  2367. /* c contains sanitized HTML */
  2368. xs_html_add(snac_content,
  2369. xs_html_raw(c));
  2370. /* quoted post */
  2371. const char *quoted_id = xs_or(xs_dict_get(msg, "quoteUri"), xs_dict_get(msg, "quoteUrl"));
  2372. if (xs_is_string(quoted_id) && xs_match(quoted_id, "https://*|http://*")) { /** **/
  2373. xs *quoted_post = NULL;
  2374. if (valid_status(object_get(quoted_id, &quoted_post))) {
  2375. xs *md5 = xs_md5_hex(quoted_id, strlen(quoted_id));
  2376. xs_html_add(snac_content,
  2377. xs_html_tag("blockquote",
  2378. xs_html_attr("class", "snac-quoted-post"),
  2379. html_entry(user, quoted_post, 1, level + 1, md5, 1)));
  2380. }
  2381. else
  2382. if (user)
  2383. enqueue_object_request(user, quoted_id, 0);
  2384. }
  2385. }
  2386. if (strcmp(type, "Question") == 0) { /** question content **/
  2387. const xs_list *oo = xs_dict_get(msg, "oneOf");
  2388. const xs_list *ao = xs_dict_get(msg, "anyOf");
  2389. const xs_list *p;
  2390. const xs_dict *v;
  2391. int closed = 0;
  2392. const char *f_closed = NULL;
  2393. xs_html *poll = xs_html_tag("div", NULL);
  2394. if (read_only)
  2395. closed = 1; /* non-identified page; show as closed */
  2396. else
  2397. if (user && is_msg_mine(user, id))
  2398. closed = 1; /* we questioned; closed for us */
  2399. else
  2400. if (user && was_question_voted(user, id))
  2401. closed = 1; /* we already voted; closed for us */
  2402. if ((f_closed = xs_dict_get(msg, "closed")) != NULL) {
  2403. /* it has a closed date... but is it in the past? */
  2404. time_t t0 = time(NULL);
  2405. time_t t1 = xs_parse_iso_date(f_closed, 0);
  2406. if (t1 < t0)
  2407. closed = 2;
  2408. }
  2409. /* get the appropriate list of options */
  2410. p = oo != NULL ? oo : ao;
  2411. if (closed || user == NULL) {
  2412. /* closed poll */
  2413. xs_html *poll_result = xs_html_tag("table",
  2414. xs_html_attr("class", "snac-poll-result"));
  2415. int c = 0;
  2416. while (xs_list_next(p, &v, &c)) {
  2417. const char *name = xs_dict_get(v, "name");
  2418. const xs_dict *replies = xs_dict_get(v, "replies");
  2419. if (xs_is_string(name) && xs_is_dict(replies)) {
  2420. const char *ti = xs_number_str(xs_dict_get(replies, "totalItems"));
  2421. if (xs_is_string(ti))
  2422. xs_html_add(poll_result,
  2423. xs_html_tag("tr",
  2424. xs_html_tag("td",
  2425. xs_html_text(name),
  2426. xs_html_text(":")),
  2427. xs_html_tag("td",
  2428. xs_html_text(ti))));
  2429. }
  2430. }
  2431. xs_html_add(poll,
  2432. poll_result);
  2433. }
  2434. else {
  2435. /* poll still active */
  2436. xs *vote_action = xs_fmt("%s/admin/vote", user->actor);
  2437. xs_html *form;
  2438. xs_html *poll_form = xs_html_tag("div",
  2439. xs_html_attr("class", "snac-poll-form"),
  2440. form = xs_html_tag("form",
  2441. xs_html_attr("autocomplete", "off"),
  2442. xs_html_attr("method", "post"),
  2443. xs_html_attr("action", vote_action),
  2444. xs_html_sctag("input",
  2445. xs_html_attr("type", "hidden"),
  2446. xs_html_attr("name", "actor"),
  2447. xs_html_attr("value", actor)),
  2448. xs_html_sctag("input",
  2449. xs_html_attr("type", "hidden"),
  2450. xs_html_attr("name", "irt"),
  2451. xs_html_attr("value", id))));
  2452. int c = 0;
  2453. while (xs_list_next(p, &v, &c)) {
  2454. const char *name = xs_dict_get(v, "name");
  2455. const xs_dict *replies = xs_dict_get(v, "replies");
  2456. if (name) {
  2457. char *ti = (char *)xs_number_str(xs_dict_get(replies, "totalItems"));
  2458. xs_html *btn = xs_html_sctag("input",
  2459. xs_html_attr("id", name),
  2460. xs_html_attr("value", name),
  2461. xs_html_attr("name", "question"));
  2462. if (!xs_is_null(oo)) {
  2463. xs_html_add(btn,
  2464. xs_html_attr("type", "radio"),
  2465. xs_html_attr("required", "required"));
  2466. }
  2467. else
  2468. xs_html_add(btn,
  2469. xs_html_attr("type", "checkbox"));
  2470. xs_html_add(form,
  2471. btn,
  2472. xs_html_text(" "),
  2473. xs_html_tag("span",
  2474. xs_html_attr("title", ti),
  2475. xs_html_text(name)),
  2476. xs_html_sctag("br", NULL));
  2477. }
  2478. }
  2479. xs_html_add(form,
  2480. xs_html_tag("p", NULL),
  2481. xs_html_sctag("input",
  2482. xs_html_attr("type", "submit"),
  2483. xs_html_attr("class", "button"),
  2484. xs_html_attr("value", L("Vote"))));
  2485. xs_html_add(poll,
  2486. poll_form);
  2487. }
  2488. /* if it's *really* closed, say it */
  2489. if (closed == 2) {
  2490. xs_html_add(poll,
  2491. xs_html_tag("p",
  2492. xs_html_text(L("Closed"))));
  2493. }
  2494. else {
  2495. /* show when the poll closes */
  2496. const char *end_time = xs_dict_get(msg, "endTime");
  2497. /* Pleroma does not have an endTime field;
  2498. it has a closed time in the future */
  2499. if (xs_is_null(end_time))
  2500. end_time = xs_dict_get(msg, "closed");
  2501. if (!xs_is_null(end_time)) {
  2502. time_t t0 = time(NULL);
  2503. time_t t1 = xs_parse_iso_date(end_time, 0);
  2504. if (t1 > 0 && t1 > t0) {
  2505. time_t diff_time = t1 - t0;
  2506. xs *tf = xs_str_time_diff(diff_time);
  2507. char *p = tf;
  2508. /* skip leading zeros */
  2509. for (; *p == '0' || *p == ':'; p++);
  2510. xs_html_add(poll,
  2511. xs_html_tag("p",
  2512. xs_html_text(L("Closes in")),
  2513. xs_html_text(" "),
  2514. xs_html_text(p)));
  2515. }
  2516. }
  2517. }
  2518. xs_html_add(snac_content,
  2519. poll);
  2520. }
  2521. /** attachments **/
  2522. xs *attach = get_attachments(msg);
  2523. {
  2524. /* make custom css for attachments easier */
  2525. xs_html *content_attachments = xs_html_tag("div",
  2526. xs_html_attr("class", "snac-content-attachments"));
  2527. xs_html_add(snac_content,
  2528. content_attachments);
  2529. const char *content = xs_dict_get(msg, "content");
  2530. int c = 0;
  2531. const xs_dict *a;
  2532. while (xs_list_next(attach, &a, &c)) {
  2533. const char *type = xs_dict_get(a, "type");
  2534. const char *o_href = xs_dict_get(a, "href");
  2535. const char *name = xs_dict_get(a, "name");
  2536. if (!xs_is_string(type) || !xs_is_string(o_href))
  2537. continue;
  2538. /* if this URL is already in the post content, skip */
  2539. if (content && xs_str_in(content, o_href) != -1)
  2540. continue;
  2541. if (strcmp(type, "image/svg+xml") == 0 && !xs_is_true(xs_dict_get(srv_config, "enable_svg")))
  2542. continue;
  2543. /* do this attachment include an icon? */
  2544. const xs_dict *icon = xs_dict_get(a, "icon");
  2545. if (xs_type(icon) == XSTYPE_DICT) {
  2546. const char *icon_mtype = xs_dict_get(icon, "mediaType");
  2547. const char *icon_url = xs_dict_get(icon, "url");
  2548. if (icon_mtype && icon_url && xs_startswith(icon_mtype, "image/")) {
  2549. xs_html_add(content_attachments,
  2550. xs_html_tag("a",
  2551. xs_html_attr("href", icon_url),
  2552. xs_html_attr("target", "_blank"),
  2553. xs_html_sctag("img",
  2554. xs_html_attr("loading", "lazy"),
  2555. xs_html_attr("src", icon_url))));
  2556. }
  2557. }
  2558. xs *href = make_url(o_href, proxy, 0);
  2559. if (xs_startswith(type, "image/") || strcmp(type, "Image") == 0) {
  2560. xs_html_add(content_attachments,
  2561. xs_html_tag("a",
  2562. xs_html_attr("href", href),
  2563. xs_html_attr("target", "_blank"),
  2564. xs_html_sctag("img",
  2565. xs_html_attr("loading", "lazy"),
  2566. xs_html_attr("src", href),
  2567. xs_html_attr("alt", name),
  2568. xs_html_attr("title", name))));
  2569. }
  2570. else
  2571. if (xs_startswith(type, "video/") || strcmp(type, "Video") == 0) {
  2572. xs_html_add(content_attachments,
  2573. xs_html_tag("video",
  2574. xs_html_attr("preload", "none"),
  2575. xs_html_attr("style", "width: 100%"),
  2576. xs_html_attr("class", "snac-embedded-video"),
  2577. xs_html_attr("controls", NULL),
  2578. xs_html_attr("src", href),
  2579. xs_html_text(L("Video")),
  2580. xs_html_text(": "),
  2581. xs_html_tag("a",
  2582. xs_html_attr("href", href),
  2583. xs_html_text(name))));
  2584. }
  2585. else
  2586. if (xs_startswith(type, "audio/")) {
  2587. xs_html_add(content_attachments,
  2588. xs_html_tag("audio",
  2589. xs_html_attr("preload", "none"),
  2590. xs_html_attr("style", "width: 100%"),
  2591. xs_html_attr("class", "snac-embedded-audio"),
  2592. xs_html_attr("controls", NULL),
  2593. xs_html_attr("src", href),
  2594. xs_html_text(L("Audio")),
  2595. xs_html_text(": "),
  2596. xs_html_tag("a",
  2597. xs_html_attr("href", href),
  2598. xs_html_text(name))));
  2599. }
  2600. else
  2601. if (strcmp(type, "Link") == 0) {
  2602. xs_html_add(content_attachments,
  2603. xs_html_tag("p",
  2604. xs_html_tag("a",
  2605. xs_html_attr("href", o_href),
  2606. xs_html_text(href))));
  2607. /* do not generate an Alt... */
  2608. name = NULL;
  2609. }
  2610. else {
  2611. xs *d_href = xs_dup(o_href);
  2612. if (strlen(d_href) > 64) {
  2613. d_href[64] = '\0';
  2614. d_href = xs_str_cat(d_href, "...");
  2615. }
  2616. xs_html_add(content_attachments,
  2617. xs_html_tag("p",
  2618. xs_html_tag("a",
  2619. xs_html_attr("href", o_href),
  2620. xs_html_text(L("Attachment")),
  2621. xs_html_text(": "),
  2622. xs_html_text(d_href))));
  2623. /* do not generate an Alt... */
  2624. name = NULL;
  2625. }
  2626. if (name != NULL && *name) {
  2627. xs_html_add(content_attachments,
  2628. xs_html_tag("p",
  2629. xs_html_attr("class", "snac-alt-text"),
  2630. xs_html_tag("details",
  2631. xs_html_tag("summary",
  2632. xs_html_text(L("Alt..."))),
  2633. xs_html_text(name))));
  2634. }
  2635. }
  2636. }
  2637. /* has this message an audience (i.e., comes from a channel or community)? */
  2638. const char *audience = xs_dict_get(msg, "audience");
  2639. if (strcmp(type, "Page") == 0 && !xs_is_null(audience)) {
  2640. xs_html *au_tag = xs_html_tag("p",
  2641. xs_html_text("("),
  2642. xs_html_tag("a",
  2643. xs_html_attr("href", audience),
  2644. xs_html_attr("title", L("Source channel or community")),
  2645. xs_html_text(audience)),
  2646. xs_html_text(")"));
  2647. xs_html_add(snac_content_wrap,
  2648. au_tag);
  2649. }
  2650. /* does it have a location? */
  2651. const xs_dict *location = xs_dict_get(msg, "location");
  2652. if (xs_type(location) == XSTYPE_DICT) {
  2653. const xs_number *latitude = xs_dict_get(location, "latitude");
  2654. const xs_number *longitude = xs_dict_get(location, "longitude");
  2655. const char *name = xs_dict_get(location, "name");
  2656. const char *address = xs_dict_get(location, "address");
  2657. xs *label_list = xs_list_new();
  2658. if (xs_type(name) == XSTYPE_STRING)
  2659. label_list = xs_list_append(label_list, name);
  2660. if (xs_type(address) == XSTYPE_STRING)
  2661. label_list = xs_list_append(label_list, address);
  2662. if (xs_list_len(label_list)) {
  2663. const char *url = xs_dict_get(location, "url");
  2664. xs *label = xs_join(label_list, ", ");
  2665. if (xs_type(url) == XSTYPE_STRING) {
  2666. xs_html_add(snac_content_wrap,
  2667. xs_html_tag("p",
  2668. xs_html_text(L("Location: ")),
  2669. xs_html_tag("a",
  2670. xs_html_attr("href", url),
  2671. xs_html_attr("target", "_blank"),
  2672. xs_html_text(label))));
  2673. }
  2674. else
  2675. if (!xs_is_null(latitude) && !xs_is_null(longitude)) {
  2676. xs *url = xs_fmt("https://openstreetmap.org/search/?query=%s,%s",
  2677. xs_or(xs_number_str(latitude), latitude), xs_or(xs_number_str(longitude), longitude));
  2678. xs_html_add(snac_content_wrap,
  2679. xs_html_tag("p",
  2680. xs_html_text(L("Location: ")),
  2681. xs_html_tag("a",
  2682. xs_html_attr("href", url),
  2683. xs_html_attr("target", "_blank"),
  2684. xs_html_text(label))));
  2685. }
  2686. else
  2687. xs_html_add(snac_content_wrap,
  2688. xs_html_tag("p",
  2689. xs_html_text(L("Location: ")),
  2690. xs_html_text(label)));
  2691. }
  2692. }
  2693. if (strcmp(type, "Event") == 0) { /** Event start and end times **/
  2694. const char *s_time = xs_dict_get(msg, "startTime");
  2695. if (xs_is_string(s_time) && strlen(s_time) > 20) {
  2696. const char *e_time = xs_dict_get(msg, "endTime");
  2697. const char *tz = xs_dict_get(msg, "timezone");
  2698. xs *s = xs_replace_i(xs_dup(s_time), "T", " ");
  2699. xs *e = NULL;
  2700. if (xs_is_string(e_time) && strlen(e_time) > 20)
  2701. e = xs_replace_i(xs_dup(e_time), "T", " ");
  2702. /* if the event has a timezone, crop the offsets */
  2703. if (xs_is_string(tz)) {
  2704. s = xs_crop_i(s, 0, 19);
  2705. if (e)
  2706. e = xs_crop_i(e, 0, 19);
  2707. }
  2708. else
  2709. tz = "";
  2710. /* if start and end share the same day, crop it from the end */
  2711. if (e && memcmp(s, e, 11) == 0)
  2712. e = xs_crop_i(e, 11, 0);
  2713. if (e)
  2714. s = xs_str_cat(s, " / ", e);
  2715. if (*tz)
  2716. s = xs_str_cat(s, " (", tz, ")");
  2717. /* replace ugly decimals */
  2718. s = xs_replace_i(s, ".000", "");
  2719. xs_html_add(snac_content_wrap,
  2720. xs_html_tag("p",
  2721. xs_html_text(L("Time: ")),
  2722. xs_html_text(s)));
  2723. }
  2724. }
  2725. /* show all hashtags that has not been shown previously in the content */
  2726. const xs_list *tags = xs_dict_get(msg, "tag");
  2727. const char *o_content = xs_dict_get_def(msg, "content", "");
  2728. if (xs_is_string(o_content) && xs_is_list(tags) && xs_list_len(tags)) {
  2729. xs *content = xs_utf8_to_lower(o_content);
  2730. const xs_dict *tag;
  2731. xs_html *add_hashtags = xs_html_tag("ul",
  2732. xs_html_attr("class", "snac-more-hashtags"));
  2733. xs_list_foreach(tags, tag) {
  2734. const char *type = xs_dict_get(tag, "type");
  2735. if (xs_is_string(type) && strcmp(type, "Hashtag") == 0) {
  2736. const char *o_href = xs_dict_get(tag, "href");
  2737. const char *name = xs_dict_get(tag, "name");
  2738. if (xs_is_string(o_href) && xs_is_string(name)) {
  2739. xs *href = xs_utf8_to_lower(o_href);
  2740. if (xs_str_in(content, href) == -1 && xs_str_in(content, name) == -1) {
  2741. /* not in the content: add here */
  2742. xs_html_add(add_hashtags,
  2743. xs_html_tag("li",
  2744. xs_html_tag("a",
  2745. xs_html_attr("href", href),
  2746. xs_html_text(name),
  2747. xs_html_text(" "))));
  2748. }
  2749. }
  2750. }
  2751. }
  2752. xs_html_add(snac_content_wrap,
  2753. add_hashtags);
  2754. }
  2755. /** controls **/
  2756. if (!read_only && user) {
  2757. xs_html_add(entry,
  2758. html_entry_controls(user, actor, msg, md5));
  2759. }
  2760. /** children **/
  2761. if (!hide_children) {
  2762. xs *children = object_children(id);
  2763. int left = xs_list_len(children);
  2764. if (left) {
  2765. xs_html *ch_details = xs_html_tag("details",
  2766. xs_html_attr(collapse_threads ? "" : "open", NULL),
  2767. xs_html_tag("summary",
  2768. xs_html_text("...")));
  2769. xs_html_add(entry,
  2770. ch_details);
  2771. xs_html *fch_container = xs_html_tag("div",
  2772. xs_html_attr("class", "snac-thread-cont"));
  2773. xs_html_add(ch_details,
  2774. fch_container);
  2775. xs_html *ch_container = xs_html_tag("div",
  2776. xs_html_attr("class", level < 4 ? "snac-children" : "snac-children-too-deep"));
  2777. xs_html_add(ch_details,
  2778. ch_container);
  2779. xs_html *ch_older = NULL;
  2780. if (left > 3) {
  2781. xs_html_add(ch_container,
  2782. ch_older = xs_html_tag("details",
  2783. xs_html_tag("summary",
  2784. xs_html_text(L("Older...")))));
  2785. }
  2786. int ctxt = 0;
  2787. const char *cmd5;
  2788. int cnt = 0;
  2789. int o_cnt = 0;
  2790. int f_cnt = 0;
  2791. /* get the first child */
  2792. xs_list_next(children, &cmd5, &ctxt);
  2793. xs *f_chd = NULL;
  2794. if (user)
  2795. timeline_get_by_md5(user, cmd5, &f_chd);
  2796. else
  2797. object_get_by_md5(cmd5, &f_chd);
  2798. if (f_chd != NULL && xs_is_null(xs_dict_get(f_chd, "name"))) {
  2799. const char *p_author = get_atto(msg);
  2800. const char *author = get_atto(f_chd);
  2801. /* is the first child from the same author? */
  2802. if (xs_is_string(p_author) && xs_is_string(author) && strcmp(p_author, author) == 0) {
  2803. /* then, don't add it to the children container,
  2804. so that it appears unindented just before the parent
  2805. like a fucking Twitter-like thread */
  2806. xs_html_add(fch_container,
  2807. html_entry(user, f_chd, read_only, level + 1, cmd5, hide_children));
  2808. cnt++;
  2809. f_cnt++;
  2810. left--;
  2811. }
  2812. else
  2813. ctxt = 0; /* restart from the beginning */
  2814. }
  2815. while (xs_list_next(children, &cmd5, &ctxt)) {
  2816. xs *chd = NULL;
  2817. if (user)
  2818. timeline_get_by_md5(user, cmd5, &chd);
  2819. else
  2820. object_get_by_md5(cmd5, &chd);
  2821. if (chd != NULL) {
  2822. if (xs_is_null(xs_dict_get(chd, "name"))) {
  2823. xs_html *che = html_entry(user, chd, read_only,
  2824. level + 1, cmd5, hide_children);
  2825. if (che != NULL) {
  2826. if (left > 3) {
  2827. xs_html_add(ch_older,
  2828. che);
  2829. o_cnt++;
  2830. }
  2831. else
  2832. xs_html_add(ch_container,
  2833. che);
  2834. cnt++;
  2835. }
  2836. }
  2837. left--;
  2838. }
  2839. else
  2840. srv_debug(2, xs_fmt("cannot read child %s", cmd5));
  2841. }
  2842. /* if no children were finally added, hide the details */
  2843. if (cnt == 0)
  2844. xs_html_add(ch_details,
  2845. xs_html_attr("style", "display: none"));
  2846. if (o_cnt == 0 && ch_older)
  2847. xs_html_add(ch_older,
  2848. xs_html_attr("style", "display: none"));
  2849. if (f_cnt == 0)
  2850. xs_html_add(fch_container,
  2851. xs_html_attr("style", "display: none"));
  2852. }
  2853. }
  2854. /* add an invisible hr, to help differentiate between posts in text browsers */
  2855. xs_html_add(entry_top,
  2856. xs_html_sctag("hr",
  2857. xs_html_attr("hidden", NULL)));
  2858. return entry_top;
  2859. }
  2860. xs_html *html_footer(const snac *user)
  2861. {
  2862. return xs_html_tag("div",
  2863. xs_html_attr("class", "snac-footer"),
  2864. xs_html_tag("a",
  2865. xs_html_attr("href", srv_baseurl),
  2866. xs_html_text(L("about this site"))),
  2867. xs_html_text(" - "),
  2868. xs_html_text(L("powered by ")),
  2869. xs_html_tag("a",
  2870. xs_html_attr("href", WHAT_IS_SNAC_URL),
  2871. xs_html_tag("abbr",
  2872. xs_html_attr("title", "Social Networks Are Crap"),
  2873. xs_html_text(USER_AGENT))));
  2874. }
  2875. xs_str *html_timeline(snac *user, const xs_list *list, int read_only,
  2876. int skip, int show, int show_more,
  2877. const char *title, const char *page,
  2878. int utl, const char *error, int terse)
  2879. /* returns the HTML for the timeline */
  2880. {
  2881. xs_list *p = (xs_list *)list;
  2882. const char *v;
  2883. double t = ftime();
  2884. int hide_children = xs_is_true(xs_dict_get(srv_config, "strict_public_timelines")) && read_only;
  2885. xs *desc = NULL;
  2886. xs *alternate = NULL;
  2887. if (xs_list_len(list) == 1) {
  2888. /* only one element? pick the description from the source */
  2889. const char *id = xs_list_get(list, 0);
  2890. xs *d = NULL;
  2891. object_get_by_md5(id, &d);
  2892. const char *sc = xs_dict_get(d, "sourceContent");
  2893. if (d && sc != NULL)
  2894. desc = xs_dup(sc);
  2895. alternate = xs_dup(xs_dict_get(d, "id"));
  2896. }
  2897. xs_html *head;
  2898. xs_html *body;
  2899. if (user) {
  2900. head = html_user_head(user, desc, alternate);
  2901. if (terse)
  2902. body = xs_html_tag("body", NULL);
  2903. else
  2904. body = html_user_body(user, read_only);
  2905. }
  2906. else {
  2907. head = html_instance_head();
  2908. body = html_instance_body();
  2909. }
  2910. xs_html *html = xs_html_tag("html",
  2911. head,
  2912. body);
  2913. if (user && !read_only)
  2914. xs_html_add(body,
  2915. html_top_controls(user));
  2916. if (error != NULL) {
  2917. xs_html_add(body,
  2918. xs_html_tag("dialog",
  2919. xs_html_attr("open", NULL),
  2920. xs_html_tag("p",
  2921. xs_html_text(error)),
  2922. xs_html_tag("form",
  2923. xs_html_attr("method", "dialog"),
  2924. xs_html_sctag("input",
  2925. xs_html_attr("type", "submit"),
  2926. xs_html_attr("value", L("Dismiss"))))));
  2927. }
  2928. /* show links to the available lists */
  2929. if (user && !read_only) {
  2930. xs_html *lol = xs_html_tag("ul",
  2931. xs_html_attr("class", "snac-list-of-lists"));
  2932. xs_html_add(body, lol);
  2933. xs *lists = list_maint(user, NULL, 0); /* get list of lists */
  2934. int ct = 0;
  2935. const char *v;
  2936. while (xs_list_next(lists, &v, &ct)) {
  2937. const char *lname = xs_list_get(v, 1);
  2938. xs *url = xs_fmt("%s/list/%s", user->actor, xs_list_get(v, 0));
  2939. xs *ttl = xs_fmt(L("Timeline for list '%s'"), lname);
  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", ttl),
  2946. xs_html_text(lname))));
  2947. }
  2948. {
  2949. /* show the list of pinned posts */
  2950. xs *url = xs_fmt("%s/pinned", 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("Pinned posts")),
  2957. xs_html_text(L("pinned")))));
  2958. }
  2959. {
  2960. /* show the list of bookmarked posts */
  2961. xs *url = xs_fmt("%s/bookmarks", 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("Bookmarked posts")),
  2968. xs_html_text(L("bookmarks")))));
  2969. }
  2970. {
  2971. /* show the list of drafts */
  2972. xs *url = xs_fmt("%s/drafts", 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("Post drafts")),
  2979. xs_html_text(L("drafts")))));
  2980. }
  2981. {
  2982. /* show the list of scheduled posts */
  2983. xs *url = xs_fmt("%s/sched", user->actor);
  2984. xs_html_add(lol,
  2985. xs_html_tag("li",
  2986. xs_html_tag("a",
  2987. xs_html_attr("href", url),
  2988. xs_html_attr("class", "snac-list-link"),
  2989. xs_html_attr("title", L("Scheduled posts")),
  2990. xs_html_text(L("scheduled posts")))));
  2991. }
  2992. /* the list of followed hashtags */
  2993. const char *followed_hashtags = xs_dict_get(user->config, "followed_hashtags");
  2994. if (xs_is_list(followed_hashtags) && xs_list_len(followed_hashtags)) {
  2995. xs_html *loht = xs_html_tag("ul",
  2996. xs_html_attr("class", "snac-list-of-lists"));
  2997. xs_html_add(body, loht);
  2998. const char *ht;
  2999. xs_list_foreach(followed_hashtags, ht) {
  3000. xs *url = NULL;
  3001. if (!xs_startswith(ht, "https:/""/"))
  3002. url = xs_fmt("%s/admin?q=%s", user->actor, ht);
  3003. else
  3004. url = xs_dup(ht);
  3005. url = xs_replace_i(url, "#", "%23");
  3006. xs_html_add(loht,
  3007. xs_html_tag("li",
  3008. xs_html_tag("a",
  3009. xs_html_attr("href", url),
  3010. xs_html_attr("class", "snac-list-link"),
  3011. xs_html_text(ht))));
  3012. }
  3013. }
  3014. }
  3015. xs_html_add(body,
  3016. xs_html_tag("a",
  3017. xs_html_attr("name", "snac-posts")));
  3018. xs_html *posts = xs_html_tag("div",
  3019. xs_html_attr("class", "snac-posts"));
  3020. if (title) {
  3021. xs_html_add(posts,
  3022. xs_html_tag("h2",
  3023. xs_html_attr("class", "snac-header"),
  3024. xs_html_text(title)));
  3025. }
  3026. xs_html_add(body, posts);
  3027. int mark_shown = 0;
  3028. int show_unlisted = user ? xs_is_true(xs_dict_get(user->config, "show_unlisted")) : 0;
  3029. while (xs_list_iter(&p, &v)) {
  3030. xs *msg = NULL;
  3031. int status;
  3032. /* "already seen" mark? */
  3033. if (strcmp(v, MD5_ALREADY_SEEN_MARK) == 0) {
  3034. if (skip == 0 && !mark_shown) {
  3035. xs *s = xs_fmt("%s/admin", user->actor);
  3036. xs_html_add(posts,
  3037. xs_html_tag("div",
  3038. xs_html_attr("class", "snac-no-more-unseen-posts"),
  3039. xs_html_text(L("No more unseen posts")),
  3040. xs_html_text(" - "),
  3041. xs_html_tag("a",
  3042. xs_html_attr("href", s),
  3043. xs_html_text(L("Back to top")))));
  3044. }
  3045. mark_shown = 1;
  3046. continue;
  3047. }
  3048. if (utl && user && !is_pinned_by_md5(user, v))
  3049. status = timeline_get_by_md5(user, v, &msg);
  3050. else
  3051. status = object_get_by_md5(v, &msg);
  3052. if (!valid_status(status))
  3053. continue;
  3054. /* if it's an instance page, discard messages from private users */
  3055. if (user == NULL && is_msg_from_private_user(msg))
  3056. continue;
  3057. const int scope = get_msg_visibility(msg);
  3058. if (user != NULL && scope != SCOPE_PUBLIC && !is_msg_mine(user, xs_dict_get(msg, "id"))) {
  3059. /* is this message a non-public reply? */
  3060. const char *irt = get_in_reply_to(msg);
  3061. /* is it a reply to something not in the storage? */
  3062. if (!xs_is_null(irt) && !object_here(irt)) {
  3063. /* is it for me? */
  3064. const xs_list *to = xs_dict_get_def(msg, "to", xs_stock(XSTYPE_LIST));
  3065. const xs_list *cc = xs_dict_get_def(msg, "cc", xs_stock(XSTYPE_LIST));
  3066. if (xs_list_in(to, user->actor) == -1 && xs_list_in(cc, user->actor) == -1) {
  3067. snac_debug(user, 1, xs_fmt("skipping non-public reply to an unknown post %s", v));
  3068. continue;
  3069. }
  3070. }
  3071. }
  3072. /* hide non-public posts from /instance view */
  3073. if (page != NULL && strcmp(page, "/instance") == 0 && scope != SCOPE_PUBLIC)
  3074. continue;
  3075. /* hide non-public posts viewed from outside */
  3076. if (read_only) {
  3077. if (scope == SCOPE_MENTIONED || scope == SCOPE_FOLLOWERS)
  3078. continue;
  3079. if (scope == SCOPE_UNLISTED && !show_unlisted)
  3080. continue;
  3081. }
  3082. xs_html *entry = html_entry(user, msg, read_only, 0, v, (user && !hide_children) ? 0 : 1);
  3083. if (entry != NULL)
  3084. xs_html_add(posts,
  3085. entry);
  3086. }
  3087. if (list && user && read_only) {
  3088. /** history **/
  3089. if (xs_type(xs_dict_get(srv_config, "disable_history")) != XSTYPE_TRUE && !terse) {
  3090. xs_html *ul = xs_html_tag("ul", NULL);
  3091. xs_html *history = xs_html_tag("div",
  3092. xs_html_attr("class", "snac-history"),
  3093. xs_html_tag("p",
  3094. xs_html_attr("class", "snac-history-title"),
  3095. xs_html_text(L("History"))),
  3096. ul);
  3097. xs *list = history_list(user);
  3098. xs_list *p = list;
  3099. const char *v;
  3100. while (xs_list_iter(&p, &v)) {
  3101. xs *fn = xs_replace(v, ".html", "");
  3102. xs *url = xs_fmt("%s/h/%s", user->actor, v);
  3103. xs_html_add(ul,
  3104. xs_html_tag("li",
  3105. xs_html_tag("a",
  3106. xs_html_attr("href", url),
  3107. xs_html_text(fn))));
  3108. }
  3109. xs_html_add(body,
  3110. history);
  3111. }
  3112. }
  3113. {
  3114. xs *s1 = xs_fmt("\n<!-- %lf seconds -->\n", ftime() - t);
  3115. xs_html_add(body,
  3116. xs_html_raw(s1));
  3117. }
  3118. if (show_more) {
  3119. xs *m = NULL;
  3120. xs *ss = xs_fmt("skip=%d&show=%d", skip + show, show);
  3121. xs *url = xs_dup(user == NULL ? srv_baseurl : user->actor);
  3122. if (page != NULL)
  3123. url = xs_str_cat(url, page);
  3124. if (xs_str_in(url, "?") != -1)
  3125. m = xs_fmt("%s&%s", url, ss);
  3126. else
  3127. m = xs_fmt("%s?%s", url, ss);
  3128. xs_html *more_links = xs_html_tag("p",
  3129. xs_html_tag("a",
  3130. xs_html_attr("href", url),
  3131. xs_html_attr("name", "snac-more"),
  3132. xs_html_text(L("Back to top"))),
  3133. xs_html_text(" - "),
  3134. xs_html_tag("a",
  3135. xs_html_attr("href", m),
  3136. xs_html_attr("name", "snac-more"),
  3137. xs_html_text(L("More..."))));
  3138. xs_html_add(body,
  3139. more_links);
  3140. }
  3141. xs_html_add(body,
  3142. html_footer(user));
  3143. return xs_html_render_s(html, "<!DOCTYPE html>\n");
  3144. }
  3145. xs_html *html_people_list(snac *user, xs_list *list, const char *header, const char *t, const char *proxy, int count)
  3146. {
  3147. xs_html *snac_posts;
  3148. xs *header_cnt;
  3149. if (count > -1)
  3150. header_cnt = xs_fmt("%s - %d\n", header, count);
  3151. else
  3152. header_cnt = xs_fmt("%s\n", header);
  3153. xs_html *people = xs_html_tag("div",
  3154. xs_html_tag("h2",
  3155. xs_html_attr("class", "snac-header"),
  3156. xs_html_text(header_cnt)),
  3157. snac_posts = xs_html_tag("details",
  3158. xs_html_attr("open", NULL),
  3159. xs_html_tag("summary",
  3160. xs_html_text("..."))));
  3161. xs *redir = xs_fmt("%s/people", user->actor);
  3162. const char *actor_id;
  3163. xs_list_foreach(list, actor_id) {
  3164. xs *md5 = xs_md5_hex(actor_id, strlen(actor_id));
  3165. xs *actor = NULL;
  3166. if (valid_status(actor_get(actor_id, &actor))) {
  3167. xs_html *snac_post = xs_html_tag("div",
  3168. xs_html_attr("class", "snac-post"),
  3169. xs_html_tag("a",
  3170. xs_html_attr("name", md5)),
  3171. xs_html_tag("div",
  3172. xs_html_attr("class", "snac-post-header"),
  3173. html_actor_icon(user, actor, xs_dict_get(actor, "published"),
  3174. NULL, NULL, -1, 0, proxy, NULL, NULL)));
  3175. /* content (user bio) */
  3176. const char *c = xs_dict_get(actor, "summary");
  3177. const xs_val *tag = xs_dict_get(actor, "tag");
  3178. if (!xs_is_null(c)) {
  3179. xs *sc = sanitize(c);
  3180. sc = replace_shortnames(sc, tag, 2, proxy);
  3181. xs_html *snac_content = xs_html_tag("div",
  3182. xs_html_attr("class", "snac-content"));
  3183. if (xs_startswith(sc, "<p>"))
  3184. xs_html_add(snac_content,
  3185. xs_html_raw(sc)); /* already sanitized */
  3186. else
  3187. xs_html_add(snac_content,
  3188. xs_html_tag("p",
  3189. xs_html_raw(sc))); /* already sanitized */
  3190. xs_html_add(snac_post, snac_content);
  3191. }
  3192. /* does it have account metrics in the object? */
  3193. const xs_val *fwing = xs_dict_get(actor, "following_count");
  3194. const xs_val *fwers = xs_dict_get(actor, "followers_count");
  3195. if (!xs_is_null(fwing) && !xs_is_null(fwers)) {
  3196. int i_fwing = (int) xs_number_get_l(fwing);
  3197. int i_fwers = (int) xs_number_get_l(fwers);
  3198. if (i_fwing || i_fwers) {
  3199. xs *s = xs_fmt(L("%d following, %d followers"), i_fwing, i_fwers);
  3200. xs_html_add(snac_post,
  3201. xs_html_tag("div",
  3202. xs_html_tag("p",
  3203. xs_html_text(s))));
  3204. }
  3205. }
  3206. /* does it have a location? */
  3207. const xs_dict *location = xs_dict_get(actor, "location");
  3208. if (xs_is_dict(location)) {
  3209. const xs_val *latitude = xs_dict_get(location, "latitude");
  3210. const xs_val *longitude = xs_dict_get(location, "longitude");
  3211. if (latitude && longitude) {
  3212. const char *name = xs_dict_get_def(location, "name", "Home");
  3213. const char *la = xs_is_string(latitude) ? latitude : xs_number_str(latitude);
  3214. const char *lo = xs_is_string(longitude) ? longitude : xs_number_str(longitude);
  3215. if (xs_is_string(la) && xs_is_string(lo)) {
  3216. xs *label = xs_fmt("%s,%s", la, lo);
  3217. xs *url = xs_fmt("https://openstreetmap.org/search?query=%s,%s", la, lo);
  3218. xs_html_add(snac_post,
  3219. xs_html_tag("p",
  3220. xs_html_text(name),
  3221. xs_html_text(": "),
  3222. xs_html_tag("a",
  3223. xs_html_attr("href", url),
  3224. xs_html_attr("target", "_blank"),
  3225. xs_html_text(label))));
  3226. }
  3227. }
  3228. }
  3229. /* add user metadata */
  3230. xs_html *snac_metadata = xs_html_tag("div",
  3231. xs_html_attr("class", "snac-metadata"));
  3232. int count = 0;
  3233. const xs_val *address = xs_dict_get(actor, "vcard:Address");
  3234. if (xs_is_string(address)) {
  3235. xs_html_add(snac_metadata,
  3236. xs_html_tag("span",
  3237. xs_html_attr("class", "snac-property-name"),
  3238. xs_html_raw("&#x1F4CD; Location")),
  3239. xs_html_text(":"),
  3240. xs_html_raw("&nbsp;"),
  3241. xs_html_tag("span",
  3242. xs_html_attr("class", "snac-property-value p-adr"),
  3243. xs_html_text(address)),
  3244. xs_html_sctag("br", NULL));
  3245. count++;
  3246. }
  3247. const xs_val *birthday = xs_dict_get(actor, "vcard:bday");
  3248. if (xs_is_string(birthday)) {
  3249. xs_html_add(snac_metadata,
  3250. xs_html_tag("span",
  3251. xs_html_attr("class", "snac-property-name"),
  3252. xs_html_raw("&#x1F382; Birthday")),
  3253. xs_html_text(":"),
  3254. xs_html_raw("&nbsp;"),
  3255. xs_html_tag("time",
  3256. xs_html_attr("class", "snac-property-value dt-bday"),
  3257. xs_html_text(birthday)),
  3258. xs_html_sctag("br", NULL));
  3259. count++;
  3260. }
  3261. const xs_list *attachment = xs_dict_get(actor, "attachment");
  3262. if (count > 0 && xs_list_len(attachment) > 0) {
  3263. xs_html_add(snac_metadata,
  3264. xs_html_sctag("hr",
  3265. xs_html_attr("class", "snac-property-divider")));
  3266. }
  3267. const xs_val *v;
  3268. xs_list_foreach(attachment, v) {
  3269. const char *type = xs_dict_get(v, "type");
  3270. const char *name = xs_dict_get(v, "name");
  3271. const char *value = xs_dict_get(v, "value");
  3272. if (!xs_is_null(type) && !xs_is_null(name) &&
  3273. !xs_is_null(value) && strcmp(type, "PropertyValue") == 0) {
  3274. /* both the name and the value can contain emoji */
  3275. xs *nam = sanitize(name);
  3276. nam = replace_shortnames(nam, tag, 1, proxy);
  3277. /* todo: sometimes the value is transmitted as markdown and not html ._. */
  3278. xs *val = sanitize(value);
  3279. val = replace_shortnames(val, tag, 1, proxy);
  3280. /* delete <p> tags, because some software sends them */
  3281. val = xs_replace_i(val, "<p>", "");
  3282. val = xs_replace_i(val, "</p>", "");
  3283. xs_html_add(snac_metadata,
  3284. xs_html_tag("span",
  3285. xs_html_attr("class", "snac-property-name"),
  3286. xs_html_raw(nam)),
  3287. xs_html_text(":"),
  3288. xs_html_raw("&nbsp;"),
  3289. xs_html_tag("span",
  3290. xs_html_attr("class", "snac-property-value"),
  3291. xs_html_raw(val)),
  3292. xs_html_sctag("br", NULL));
  3293. count++;
  3294. }
  3295. }
  3296. if (count > 0) {
  3297. xs_html_add(snac_post, snac_metadata);
  3298. }
  3299. else {
  3300. /* free the html, by rendering it... */
  3301. xs_free(xs_html_render(snac_metadata));
  3302. }
  3303. /* buttons */
  3304. xs *btn_form_action = xs_fmt("%s/admin/action", user->actor);
  3305. xs_html *snac_controls = xs_html_tag("div",
  3306. xs_html_attr("class", "snac-controls"));
  3307. xs_html *form = xs_html_tag("form",
  3308. xs_html_attr("autocomplete", "off"),
  3309. xs_html_attr("method", "post"),
  3310. xs_html_attr("action", btn_form_action),
  3311. xs_html_sctag("input",
  3312. xs_html_attr("type", "hidden"),
  3313. xs_html_attr("name", "actor"),
  3314. xs_html_attr("value", actor_id)),
  3315. xs_html_sctag("input",
  3316. xs_html_attr("type", "hidden"),
  3317. xs_html_attr("name", "hard-redir"),
  3318. xs_html_attr("value", redir)),
  3319. xs_html_sctag("input",
  3320. xs_html_attr("type", "hidden"),
  3321. xs_html_attr("name", "actor-form"),
  3322. xs_html_attr("value", "yes")));
  3323. xs_html_add(snac_controls, form);
  3324. if (following_check(user, actor_id)) {
  3325. xs_html_add(form,
  3326. html_button("unfollow", L("Unfollow"),
  3327. L("Stop following this user's activity")));
  3328. if (is_limited(user, actor_id))
  3329. xs_html_add(form,
  3330. html_button("unlimit", L("Unlimit"),
  3331. L("Allow announces (boosts) from this user")));
  3332. else
  3333. xs_html_add(form,
  3334. html_button("limit", L("Limit"),
  3335. L("Block announces (boosts) from this user")));
  3336. }
  3337. else {
  3338. xs_html_add(form,
  3339. html_button("follow", L("Follow"),
  3340. L("Start following this user's activity")));
  3341. if (follower_check(user, actor_id))
  3342. xs_html_add(form,
  3343. html_button("delete", L("Delete"), L("Delete this user")));
  3344. }
  3345. if (pending_check(user, actor_id)) {
  3346. xs_html_add(form,
  3347. html_button("approve", L("Approve"),
  3348. L("Approve this follow request")));
  3349. xs_html_add(form,
  3350. html_button("discard", L("Discard"), L("Discard this follow request")));
  3351. }
  3352. if (is_muted(user, actor_id))
  3353. xs_html_add(form,
  3354. html_button("unmute", L("Unmute"),
  3355. L("Stop blocking activities from this user")));
  3356. else
  3357. xs_html_add(form,
  3358. html_button("mute", L("MUTE"),
  3359. L("Block any activity from this user")));
  3360. /* the post textarea */
  3361. xs *dm_div_id = xs_fmt("%s_%s_dm", md5, t);
  3362. xs *dm_form_id = xs_fmt("%s_reply_form", md5);
  3363. xs_html_add(snac_controls,
  3364. xs_html_tag("p", NULL),
  3365. html_note(user, L("Direct Message..."),
  3366. dm_div_id, dm_form_id,
  3367. "", "",
  3368. NULL, actor_id,
  3369. xs_stock(XSTYPE_FALSE), "",
  3370. SCOPE_MENTIONED, NULL,
  3371. NULL, 0, NULL, NULL, 0, NULL, NULL),
  3372. xs_html_tag("p", NULL));
  3373. xs_html_add(snac_post, snac_controls);
  3374. xs_html_add(snac_posts, snac_post);
  3375. }
  3376. }
  3377. return people;
  3378. }
  3379. xs_str *html_people(snac *user)
  3380. {
  3381. const char *proxy = NULL;
  3382. if (xs_is_true(xs_dict_get(srv_config, "proxy_media")))
  3383. proxy = user->actor;
  3384. xs *wing = following_list(user);
  3385. xs *wers = follower_list(user);
  3386. xs *pending = pending_list(user);
  3387. xs_html *lists = xs_html_tag("div",
  3388. xs_html_attr("class", "snac-posts"));
  3389. if (xs_list_len(pending) || xs_is_true(xs_dict_get(user->config, "approve_followers"))) {
  3390. xs_html_add(lists,
  3391. html_people_list(user, pending, L("Pending follow confirmations"), "p", proxy, xs_list_len(pending)));
  3392. }
  3393. xs_html_add(lists,
  3394. html_people_list(user, wing, L("People you follow"), "i", proxy, following_list_len(user)),
  3395. html_people_list(user, wers, L("People that follow you"), "e", proxy, xs_list_len(wers)));
  3396. xs_html *html = xs_html_tag("html",
  3397. html_user_head(user, NULL, NULL),
  3398. xs_html_add(html_user_body(user, 0),
  3399. lists,
  3400. html_footer(user)));
  3401. return xs_html_render_s(html, "<!DOCTYPE html>\n");
  3402. }
  3403. /* Filter list to display only posts by actor. We'll probably show
  3404. fewer than show posts. Should we try harder to find some? */
  3405. xs_str *html_people_one(snac *user, const char *actor, const xs_list *list,
  3406. int skip, int show, int show_more, const char *page)
  3407. {
  3408. const char *proxy = NULL;
  3409. xs_list *p = (xs_list *)list;
  3410. const char *v;
  3411. enqueue_actor_refresh(user, actor, 0);
  3412. if (xs_is_true(xs_dict_get(srv_config, "proxy_media")))
  3413. proxy = user->actor;
  3414. xs_html *body = html_user_body(user, 0);
  3415. xs_html *lists = xs_html_tag("div",
  3416. xs_html_attr("class", "snac-posts"));
  3417. xs *foll = xs_list_append(xs_list_new(), actor);
  3418. xs_html_add(lists,
  3419. html_people_list(user, foll, L("Contact's posts"), "p", proxy, -1));
  3420. xs_html_add(body, lists);
  3421. while (xs_list_iter(&p, &v)) {
  3422. xs *msg = NULL;
  3423. int status;
  3424. status = timeline_get_by_md5(user, v, &msg);
  3425. if (!valid_status(status))
  3426. continue;
  3427. const char *id = xs_dict_get(msg, "id");
  3428. const char *by = get_atto(msg);
  3429. xs *actor_md5 = NULL;
  3430. xs_list *boosts = NULL;
  3431. xs_list *likes = NULL;
  3432. xs_list *reacts = NULL;
  3433. /* Besides actor's posts, also show actor's boosts, and also
  3434. posts by user with likes or reacts by actor. I.e., any
  3435. actor's actions that user could have seen in the timeline
  3436. or in notifications. */
  3437. if (!(by && strcmp(actor, by) == 0) &&
  3438. xs_list_in((boosts = object_announces(id)),
  3439. (actor_md5 = xs_md5_hex(actor, strlen(actor)))) == -1 &&
  3440. (!(by && strcmp(user->actor, by) == 0) ||
  3441. (xs_list_in((likes = object_likes(id)), actor_md5) == -1 &&
  3442. xs_list_in((reacts = object_get_emoji_reacts(id)), actor_md5) == -1)))
  3443. continue;
  3444. xs_html *entry = html_entry(user, msg, 0, 0, v, 1);
  3445. if (entry != NULL)
  3446. xs_html_add(lists,
  3447. entry);
  3448. }
  3449. if (show_more) {
  3450. xs *m = NULL;
  3451. xs *m10 = NULL;
  3452. xs *ss = xs_fmt("skip=%d&show=%d", skip + show, show);
  3453. xs *url = xs_dup(user == NULL ? srv_baseurl : user->actor);
  3454. if (page != NULL)
  3455. url = xs_str_cat(url, page);
  3456. if (xs_str_in(url, "?") != -1)
  3457. m = xs_fmt("%s&%s", url, ss);
  3458. else
  3459. m = xs_fmt("%s?%s", url, ss);
  3460. m10 = xs_fmt("%s0", m);
  3461. xs_html *more_links = xs_html_tag("p",
  3462. xs_html_tag("a",
  3463. xs_html_attr("href", url),
  3464. xs_html_attr("name", "snac-more"),
  3465. xs_html_text(L("Back to top"))),
  3466. xs_html_text(" - "),
  3467. xs_html_tag("a",
  3468. xs_html_attr("href", m),
  3469. xs_html_attr("name", "snac-more"),
  3470. xs_html_text(L("More..."))),
  3471. xs_html_text(" - "),
  3472. xs_html_tag("a",
  3473. xs_html_attr("href", m10),
  3474. xs_html_attr("name", "snac-more"),
  3475. xs_html_text(L("More (x 10)..."))));
  3476. xs_html_add(body,
  3477. more_links);
  3478. }
  3479. xs_html *html = xs_html_tag("html",
  3480. html_user_head(user, NULL, NULL),
  3481. xs_html_add(body,
  3482. html_footer(user)));
  3483. return xs_html_render_s(html, "<!DOCTYPE html>\n");
  3484. }
  3485. void notify_filter(snac *user, const xs_dict *p_vars)
  3486. /* sets filter for notifications */
  3487. {
  3488. const char *v;
  3489. int likes_on = (v = xs_dict_get(p_vars, "likes_on")) ? strcmp(v, "on") == 0 : 0;
  3490. int reacts_on = (v = xs_dict_get(p_vars, "reacts_on")) ? strcmp(v, "on") == 0 : 0;
  3491. int ments_on = (v = xs_dict_get(p_vars, "mentions_on")) ? strcmp(v, "on") == 0 : 0;
  3492. int ann_on = (v = xs_dict_get(p_vars, "announces_on")) ? strcmp(v, "on") == 0 : 0;
  3493. int foll_on = (v = xs_dict_get(p_vars, "follows_on")) ? strcmp(v, "on") == 0 : 0;
  3494. int unfoll_on = (v = xs_dict_get(p_vars, "unfollows_on")) ? strcmp(v, "on") == 0 : 0;
  3495. int folreq_on = (v = xs_dict_get(p_vars, "folreqs_on")) ? strcmp(v, "on") == 0 : 0;
  3496. int blocks_on = (v = xs_dict_get(p_vars, "blocks_on")) ? strcmp(v, "on") == 0 : 0;
  3497. int polls_on = (v = xs_dict_get(p_vars, "polls_on")) ? strcmp(v, "on") == 0 : 0;
  3498. xs *filter = xs_dict_new();
  3499. filter = xs_dict_set(filter, "likes", xs_stock(likes_on ? XSTYPE_TRUE : XSTYPE_FALSE));
  3500. filter = xs_dict_set(filter, "reacts", xs_stock(reacts_on ? XSTYPE_TRUE : XSTYPE_FALSE));
  3501. filter = xs_dict_set(filter, "mentions", xs_stock(ments_on ? XSTYPE_TRUE : XSTYPE_FALSE));
  3502. filter = xs_dict_set(filter, "announces", xs_stock(ann_on ? XSTYPE_TRUE : XSTYPE_FALSE));
  3503. filter = xs_dict_set(filter, "follows", xs_stock(foll_on ? XSTYPE_TRUE : XSTYPE_FALSE));
  3504. filter = xs_dict_set(filter, "unfollows", xs_stock(unfoll_on ? XSTYPE_TRUE : XSTYPE_FALSE));
  3505. filter = xs_dict_set(filter, "folreqs", xs_stock(folreq_on ? XSTYPE_TRUE : XSTYPE_FALSE));
  3506. filter = xs_dict_set(filter, "blocks", xs_stock(blocks_on ? XSTYPE_TRUE : XSTYPE_FALSE));
  3507. filter = xs_dict_set(filter, "polls", xs_stock(polls_on ? XSTYPE_TRUE : XSTYPE_FALSE));
  3508. user->config = xs_dict_set(user->config, "notify_filter", filter);
  3509. user->tz = xs_dict_get_def(user->config, "tz", "UTC"); // previous line invalidates user->tz
  3510. }
  3511. xs_str *html_notifications(snac *user, int skip, int show)
  3512. {
  3513. const char *proxy = NULL;
  3514. if (xs_is_true(xs_dict_get(srv_config, "proxy_media")))
  3515. proxy = user->actor;
  3516. xs *n_list_unfilt = notify_list(user, skip, show);
  3517. xs *n_time = notify_check_time(user, 0);
  3518. xs_html *body = html_user_body(user, 0);
  3519. const xs_dict *n_filter = xs_dict_get(user->config, "notify_filter");
  3520. if (!n_filter) {
  3521. xs *filter = xs_dict_new();
  3522. user->config = xs_dict_set(user->config, "notify_filter", filter);
  3523. user->tz = xs_dict_get_def(user->config, "tz", "UTC"); // previous line invalidates user->tz
  3524. n_filter = xs_dict_get(user->config, "notify_filter");
  3525. }
  3526. xs *n_list = notify_filter_list(user, n_list_unfilt);
  3527. /* all filters are true by default */
  3528. const xs_val *n_def = xs_stock( XSTYPE_TRUE );
  3529. int n_likes_on = xs_is_true(xs_dict_get_def(n_filter, "likes", n_def));
  3530. int n_reacts_on = xs_is_true(xs_dict_get_def(n_filter, "reacts", n_def));
  3531. int n_ments_on = xs_is_true(xs_dict_get_def(n_filter, "mentions", n_def));
  3532. int n_ann_on = xs_is_true(xs_dict_get_def(n_filter, "announces", n_def));
  3533. int n_fol_on = xs_is_true(xs_dict_get_def(n_filter, "follows", n_def));
  3534. int n_unfol_on = xs_is_true(xs_dict_get_def(n_filter, "unfollows", n_def));
  3535. int n_folreq_on = xs_is_true(xs_dict_get_def(n_filter, "folreqs", n_def));
  3536. int n_blocks_on = xs_is_true(xs_dict_get_def(n_filter, "blocks", n_def));
  3537. int n_polls_on = xs_is_true(xs_dict_get_def(n_filter, "polls", n_def));
  3538. xs_html *html = xs_html_tag("html",
  3539. html_user_head(user, NULL, NULL),
  3540. body);
  3541. xs *filter_notifs_action = xs_fmt("%s/admin/filter-notifications", user->actor);
  3542. xs *notifs_action = xs_fmt("%s/notifications", user->actor);
  3543. xs_html *notifs_form = xs_html_tag("form",
  3544. xs_html_attr("autocomplete", "off"),
  3545. xs_html_attr("method", "post"),
  3546. xs_html_attr("action", filter_notifs_action),
  3547. xs_html_attr("enctype", "multipart/form-data"),
  3548. xs_html_attr("id", "filter"),
  3549. xs_html_sctag("input",
  3550. xs_html_attr("type", "hidden"),
  3551. xs_html_attr("name", "hard-redir"),
  3552. xs_html_attr("value", notifs_action)),
  3553. html_checkbox("likes_on", L("Likes"), n_likes_on),
  3554. html_checkbox("reacts_on", L("Emoji reacts"), n_reacts_on),
  3555. html_checkbox("mentions_on", L("Mentions"), n_ments_on),
  3556. html_checkbox("announces_on", L("Announces"), n_ann_on),
  3557. html_checkbox("follows_on", L("Follows"), n_fol_on),
  3558. html_checkbox("unfollows_on", L("Unfollows"), n_unfol_on),
  3559. html_checkbox("folreqs_on", L("Follow requests"), n_folreq_on),
  3560. html_checkbox("blocks_on", L("Blocks"), n_blocks_on),
  3561. html_checkbox("polls_on", L("Polls"), n_polls_on),
  3562. xs_html_sctag("input",
  3563. xs_html_attr("type", "submit"),
  3564. xs_html_attr("class", "button"),
  3565. xs_html_attr("value", L("Save"))));
  3566. xs_html_add(body,
  3567. xs_html_tag("p",
  3568. xs_html_tag("div",
  3569. xs_html_attr("class", "snac-notify-filter"),
  3570. xs_html_tag("details",
  3571. xs_html_tag("summary",
  3572. xs_html_text(L("Notifications filter..."))),
  3573. notifs_form))));
  3574. xs *clear_all_action = xs_fmt("%s/admin/clear-notifications", user->actor);
  3575. xs_html_add(body,
  3576. xs_html_tag("form",
  3577. xs_html_attr("autocomplete", "off"),
  3578. xs_html_attr("method", "post"),
  3579. xs_html_attr("action", clear_all_action),
  3580. xs_html_attr("id", "clear"),
  3581. xs_html_sctag("input",
  3582. xs_html_attr("type", "submit"),
  3583. xs_html_attr("class", "snac-btn-like"),
  3584. xs_html_attr("value", L("Clear all")))));
  3585. xs_html *noti_new = NULL;
  3586. xs_html *noti_seen = NULL;
  3587. xs_html *posts = xs_html_tag("div",
  3588. xs_html_attr("class", "snac-posts"));
  3589. xs_html_add(body, posts);
  3590. xs_set rep;
  3591. xs_set_init(&rep);
  3592. /* dict to store previous notification labels */
  3593. xs *admiration_labels = xs_dict_new();
  3594. const xs_str *v;
  3595. xs_list_foreach(n_list, v) {
  3596. xs *noti = notify_get(user, v);
  3597. if (noti == NULL)
  3598. continue;
  3599. xs *obj = NULL;
  3600. const char *type = xs_dict_get(noti, "type");
  3601. const char *utype = xs_dict_get(noti, "utype");
  3602. const char *id = xs_dict_get(noti, "objid");
  3603. const char *date = xs_dict_get(noti, "date");
  3604. const char *id2 = xs_dict_get_path(noti, "msg.id");
  3605. xs *wrk = NULL;
  3606. if (xs_is_null(id))
  3607. continue;
  3608. if (is_hidden(user, id))
  3609. continue;
  3610. if (xs_is_string(id2) && xs_set_add(&rep, id2) != 1)
  3611. continue;
  3612. if (strcmp(type, "EmojiReact") == 0 && xs_is_true(xs_dict_get(srv_config, "disable_emojireact")))
  3613. continue;
  3614. object_get(id, &obj);
  3615. const char *msg_id = NULL;
  3616. if (xs_is_dict(obj))
  3617. msg_id = xs_dict_get(obj, "id");
  3618. const char *actor_id = xs_dict_get(noti, "actor");
  3619. xs *actor = NULL;
  3620. xs *a_name = NULL;
  3621. if (valid_status(actor_get(actor_id, &actor)))
  3622. a_name = actor_name(actor, proxy);
  3623. else {
  3624. a_name = xs_dup(actor_id);
  3625. /* actor not here: request it */
  3626. enqueue_actor_refresh(user, actor_id, 0);
  3627. }
  3628. const char *html_url = xs_dict_get_def(actor, "url", actor_id);
  3629. if (xs_is_list(html_url))
  3630. html_url = xs_list_get(html_url, 0);
  3631. if (!xs_is_string(html_url))
  3632. html_url = actor_id;
  3633. xs *label_sanitized = sanitize(type);
  3634. const char *label = label_sanitized;
  3635. if (strcmp(type, "Create") == 0)
  3636. label = L("Mention");
  3637. else
  3638. if (strcmp(type, "Update") == 0 && strcmp(utype, "Question") == 0)
  3639. label = L("Finished poll");
  3640. else
  3641. if (strcmp(type, "Undo") == 0 && strcmp(utype, "Follow") == 0)
  3642. label = L("Unfollow");
  3643. else
  3644. if (strcmp(type, "EmojiReact") == 0 || strcmp(type, "Like") == 0) {
  3645. const char *content = xs_dict_get_path(noti, "msg.content");
  3646. xs *cd = xs_dup(content);
  3647. const char *sna = cd;
  3648. const xs_dict *tag = xs_dict_get_path(noti, "msg.tag");
  3649. unsigned int utf = xs_utf8_dec((const char **)&sna);
  3650. int isEmoji = 0;
  3651. if (xs_is_emoji(utf) || (tag && xs_list_len(tag) > 0))
  3652. isEmoji = 1;
  3653. if (xs_type(content) == XSTYPE_STRING) {
  3654. if (!isEmoji && !n_likes_on)
  3655. continue;
  3656. xs *emoji = replace_shortnames(xs_dup(content), xs_dict_get_path(noti, "msg.tag"), 1, proxy);
  3657. wrk = xs_fmt("%s (%s&#xFE0F;)", isEmoji ? "EmojiReact" : "Like", emoji);
  3658. label = wrk;
  3659. }
  3660. }
  3661. else
  3662. if (strcmp(type, "Follow") == 0 && pending_check(user, actor_id))
  3663. label = L("Follow Request");
  3664. xs *s_date = html_date_label(user, date);
  3665. xs_html *this_html_label = xs_html_container(
  3666. xs_html_tag("b",
  3667. xs_html_raw(label),
  3668. xs_html_text(" by "),
  3669. xs_html_tag("a",
  3670. xs_html_attr("href", html_url),
  3671. xs_html_raw(a_name))), /* a_name is already sanitized */
  3672. xs_html_text(" "),
  3673. xs_html_tag("time",
  3674. xs_html_attr("class", "dt-published snac-pubdate"),
  3675. xs_html_attr("title", date),
  3676. xs_html_text(s_date)));
  3677. xs_html *html_label = NULL;
  3678. if (xs_is_string(msg_id)) {
  3679. const xs_val *prev_label = xs_dict_get(admiration_labels, msg_id);
  3680. if (xs_type(prev_label) == XSTYPE_DATA) {
  3681. /* there is a previous list of admiration labels! */
  3682. xs_data_get(&html_label, prev_label);
  3683. xs_html_add(html_label,
  3684. xs_html_sctag("br", NULL),
  3685. this_html_label);
  3686. continue;
  3687. }
  3688. }
  3689. xs_html *entry = NULL;
  3690. html_label = xs_html_tag("p",
  3691. this_html_label);
  3692. /* store in the admiration labels dict */
  3693. xs *pl = xs_data_new(&html_label, sizeof(html_label));
  3694. if (xs_is_string(msg_id))
  3695. admiration_labels = xs_dict_set(admiration_labels, msg_id, pl);
  3696. entry = xs_html_tag("div",
  3697. xs_html_attr("class", "snac-post-with-desc"),
  3698. html_label);
  3699. if (strcmp(type, "Block") == 0) {
  3700. if (actor)
  3701. xs_html_add(entry,
  3702. xs_html_tag("div",
  3703. xs_html_attr("class", "snac-post"),
  3704. html_actor_icon(user, actor, NULL, NULL, NULL, -1, 0, proxy, NULL, NULL)));
  3705. }
  3706. else
  3707. if (strcmp(type, "Follow") == 0 || strcmp(utype, "Follow") == 0) {
  3708. if (actor) {
  3709. xs *action = xs_fmt("%s/admin/action", user->actor);
  3710. xs_html *button = NULL;
  3711. if (following_check(user, actor_id))
  3712. button = html_button("unfollow", L("Unfollow"), L("Stop following this user's activity"));
  3713. else
  3714. button = html_button("follow", L("Follow"), L("Start following this user's activity"));
  3715. xs_html_add(entry,
  3716. xs_html_tag("div",
  3717. xs_html_attr("class", "snac-post"),
  3718. html_actor_icon(user, actor, NULL, NULL, NULL, -1, 0, proxy, NULL, NULL),
  3719. xs_html_tag("form",
  3720. xs_html_attr("method", "post"),
  3721. xs_html_attr("action", action),
  3722. xs_html_sctag("input",
  3723. xs_html_attr("type", "hidden"),
  3724. xs_html_attr("name", "actor"),
  3725. xs_html_attr("value", actor_id)),
  3726. button,
  3727. xs_html_sctag("br", NULL))));
  3728. }
  3729. }
  3730. else
  3731. if (strcmp(type, "Move") == 0) {
  3732. const xs_dict *o_msg = xs_dict_get(noti, "msg");
  3733. const char *target;
  3734. if (xs_type(o_msg) == XSTYPE_DICT && (target = xs_dict_get(o_msg, "target"))) {
  3735. xs *old_actor = NULL;
  3736. if (valid_status(actor_get(target, &old_actor))) {
  3737. xs_html_add(entry,
  3738. xs_html_tag("div",
  3739. xs_html_attr("class", "snac-post"),
  3740. html_actor_icon(user, old_actor, NULL, NULL, NULL, -1, 0, proxy, NULL, NULL)));
  3741. }
  3742. }
  3743. }
  3744. else
  3745. if (obj != NULL) {
  3746. xs *md5 = xs_md5_hex(id, strlen(id));
  3747. xs *ctxt = xs_fmt("%s/admin/p/%s#%s_entry", user->actor, md5, md5);
  3748. xs_html *h = html_entry(user, obj, 0, 0, md5, 1);
  3749. if (h != NULL) {
  3750. xs_html_add(entry,
  3751. xs_html_tag("p",
  3752. xs_html_tag("a",
  3753. xs_html_attr("href", ctxt),
  3754. xs_html_text(L("Context")))),
  3755. h);
  3756. }
  3757. else
  3758. xs_html_add(entry,
  3759. xs_html_tag("p",
  3760. xs_html_text(L("Location: ")),
  3761. xs_html_tag("a",
  3762. xs_html_attr("href", id),
  3763. xs_html_text(id))));
  3764. }
  3765. if (strcmp(v, n_time) > 0) {
  3766. /* unseen notification */
  3767. if (noti_new == NULL) {
  3768. noti_new = xs_html_tag("div",
  3769. xs_html_tag("h2",
  3770. xs_html_attr("class", "snac-header"),
  3771. xs_html_text(L("New"))));
  3772. xs_html_add(posts,
  3773. noti_new);
  3774. }
  3775. xs_html_add(noti_new,
  3776. entry);
  3777. }
  3778. else {
  3779. /* already seen notification */
  3780. if (noti_seen == NULL) {
  3781. noti_seen = xs_html_tag("div",
  3782. xs_html_tag("h2",
  3783. xs_html_attr("class", "snac-header"),
  3784. xs_html_text(L("Already seen"))));
  3785. xs_html_add(posts,
  3786. noti_seen);
  3787. }
  3788. xs_html_add(noti_seen,
  3789. entry);
  3790. }
  3791. }
  3792. if (noti_new == NULL && noti_seen == NULL)
  3793. xs_html_add(body,
  3794. xs_html_tag("h2",
  3795. xs_html_attr("class", "snac-header"),
  3796. xs_html_text(L("None"))));
  3797. /* add the navigation footer */
  3798. xs *next_p = notify_list(user, skip + show, 1);
  3799. if (xs_list_len(next_p)) {
  3800. xs *url = xs_fmt("%s/notifications?skip=%d&show=%d",
  3801. user->actor, skip + show, show);
  3802. xs_html_add(body,
  3803. xs_html_tag("p",
  3804. xs_html_tag("a",
  3805. xs_html_attr("href", url),
  3806. xs_html_text(L("More...")))));
  3807. }
  3808. xs_set_free(&rep);
  3809. xs_html_add(body,
  3810. html_footer(user));
  3811. /* set the check time to now */
  3812. xs *dummy = notify_check_time(user, 1);
  3813. dummy = xs_free(dummy);
  3814. timeline_touch(user);
  3815. return xs_html_render_s(html, "<!DOCTYPE html>\n");
  3816. }
  3817. void set_user_lang(snac *user)
  3818. /* sets the language dict according to user configuration */
  3819. {
  3820. user->lang = NULL;
  3821. const char *lang = xs_dict_get(user->config, "lang");
  3822. if (xs_is_string(lang))
  3823. user->lang = xs_dict_get(srv_langs, lang);
  3824. }
  3825. int html_get_handler(const xs_dict *req, const char *q_path,
  3826. char **body, int *b_size, char **ctype,
  3827. xs_str **etag, xs_str **last_modified)
  3828. {
  3829. const char *accept = xs_dict_get(req, "accept");
  3830. int status = HTTP_STATUS_NOT_FOUND;
  3831. const snac *user = NULL;
  3832. snac snac;
  3833. xs *uid = NULL;
  3834. const char *p_path;
  3835. int cache = 1;
  3836. int save = 1;
  3837. int proxy = 0;
  3838. int terse = 0;
  3839. const char *v;
  3840. const xs_dict *q_vars = xs_dict_get(req, "q_vars");
  3841. xs *l = xs_split_n(q_path, "/", 2);
  3842. v = xs_list_get(l, 1);
  3843. if (xs_is_null(v)) {
  3844. srv_log(xs_fmt("html_get_handler bad query '%s'", q_path));
  3845. return HTTP_STATUS_NOT_FOUND;
  3846. }
  3847. if (!xs_is_null(xs_dict_get(q_vars, "terse")))
  3848. terse = 1;
  3849. if (strcmp(v, "share-bridge") == 0) {
  3850. /* temporary redirect for a post */
  3851. const char *login = xs_dict_get(q_vars, "login");
  3852. const char *content = xs_dict_get(q_vars, "content");
  3853. if (xs_type(login) == XSTYPE_STRING && xs_type(content) == XSTYPE_STRING) {
  3854. xs *b64 = xs_base64_enc(content, strlen(content));
  3855. srv_log(xs_fmt("share-bridge for user '%s'", login));
  3856. *body = xs_fmt("%s/%s/share?content=%s", srv_baseurl, login, b64);
  3857. return HTTP_STATUS_SEE_OTHER;
  3858. }
  3859. else
  3860. return HTTP_STATUS_NOT_FOUND;
  3861. }
  3862. else
  3863. if (strcmp(v, "auth-int-bridge") == 0) {
  3864. const char *login = xs_dict_get(q_vars, "login");
  3865. const char *id = xs_dict_get(q_vars, "id");
  3866. const char *action = xs_dict_get(q_vars, "action");
  3867. if (xs_is_string(login) && xs_is_string(id) && xs_is_string(action)) {
  3868. *body = xs_fmt("%s/%s/authorize_interaction?action=%s&id=%s",
  3869. srv_baseurl, login, action, id);
  3870. return HTTP_STATUS_SEE_OTHER;
  3871. }
  3872. else
  3873. return HTTP_STATUS_NOT_FOUND;
  3874. }
  3875. uid = xs_dup(v);
  3876. /* rss extension? */
  3877. if (xs_endswith(uid, ".rss")) {
  3878. uid = xs_crop_i(uid, 0, -4);
  3879. p_path = ".rss";
  3880. }
  3881. else
  3882. p_path = xs_list_get(l, 2);
  3883. if (!uid || !user_open(&snac, uid)) {
  3884. /* invalid user */
  3885. status = grave(uid, 0) ? HTTP_STATUS_GONE : HTTP_STATUS_NOT_FOUND;
  3886. srv_debug(2, xs_fmt("html_get_handler bad user '%s' %d", xs_or(uid, "(null)"), status));
  3887. return status;
  3888. }
  3889. user = &snac; /* for L() */
  3890. set_user_lang(&snac);
  3891. if (xs_is_true(xs_dict_get(srv_config, "proxy_media")))
  3892. proxy = 1;
  3893. /* return the RSS if requested by Accept header */
  3894. if (accept != NULL) {
  3895. if (xs_str_in(accept, "text/xml") != -1 ||
  3896. xs_str_in(accept, "application/rss+xml") != -1)
  3897. p_path = ".rss";
  3898. }
  3899. /* check if server config variable 'disable_cache' is set */
  3900. if ((v = xs_dict_get(srv_config, "disable_cache")) && xs_type(v) == XSTYPE_TRUE)
  3901. cache = 0;
  3902. int skip = 0;
  3903. const char *max_show_default = "50";
  3904. int max_show = xs_number_get(xs_dict_get_def(srv_config, "max_timeline_entries",
  3905. max_show_default));
  3906. int def_show = xs_number_get(xs_dict_get_def(srv_config, "def_timeline_entries",
  3907. xs_dict_get_def(srv_config, "max_timeline_entries",
  3908. max_show_default)));
  3909. int show = def_show;
  3910. if ((v = xs_dict_get(q_vars, "skip")) != NULL)
  3911. skip = atoi(v), cache = 0, save = 0;
  3912. if ((v = xs_dict_get(q_vars, "show")) != NULL)
  3913. show = atoi(v), cache = 0, save = 0;
  3914. if ((v = xs_dict_get(q_vars, "da")) != NULL) {
  3915. /* user dismissed an announcement */
  3916. if (login(&snac, req)) {
  3917. double ts = atof(v);
  3918. xs *timestamp = xs_number_new(ts);
  3919. srv_log(xs_fmt("user dismissed announcements until %d", ts));
  3920. snac.config = xs_dict_set(snac.config, "last_announcement", timestamp);
  3921. user_persist(&snac, 0);
  3922. }
  3923. }
  3924. /* get a possible error message */
  3925. const char *error = xs_dict_get(q_vars, "error");
  3926. if (error != NULL)
  3927. cache = 0;
  3928. /* a show of 0 has no sense */
  3929. if (show == 0)
  3930. show = def_show;
  3931. if (show > max_show)
  3932. show = max_show;
  3933. if (p_path == NULL) { /** public timeline **/
  3934. xs *h = xs_str_localtime(0, "%Y-%m.html");
  3935. if (xs_type(xs_dict_get(snac.config, "private")) == XSTYPE_TRUE) {
  3936. /** empty public timeline for private users **/
  3937. *body = html_timeline(&snac, NULL, 1, 0, 0, 0, NULL, "", 1, error, terse);
  3938. *b_size = strlen(*body);
  3939. status = HTTP_STATUS_OK;
  3940. }
  3941. else
  3942. if (cache && history_mtime(&snac, h) > timeline_mtime(&snac)) {
  3943. snac_debug(&snac, 1, xs_fmt("serving cached local timeline"));
  3944. status = history_get(&snac, h, body, b_size,
  3945. xs_dict_get(req, "if-none-match"), etag);
  3946. }
  3947. else {
  3948. xs *list = NULL;
  3949. int more = 0;
  3950. if (xs_is_true(xs_dict_get(srv_config, "strict_public_timelines")))
  3951. list = timeline_simple_list(&snac, "public", skip, show, &more);
  3952. else
  3953. list = timeline_list(&snac, "public", skip, show, &more);
  3954. xs *pins = pinned_list(&snac);
  3955. pins = xs_list_cat(pins, list);
  3956. *body = html_timeline(&snac, pins, 1, skip, show, more, NULL, "", 1, error, terse);
  3957. *b_size = strlen(*body);
  3958. status = HTTP_STATUS_OK;
  3959. if (save)
  3960. history_add(&snac, h, *body, *b_size, etag);
  3961. }
  3962. }
  3963. else
  3964. if (strcmp(p_path, "admin") == 0) { /** private timeline **/
  3965. if (!login(&snac, req)) {
  3966. *body = xs_dup(uid);
  3967. status = HTTP_STATUS_UNAUTHORIZED;
  3968. }
  3969. else {
  3970. const char *q = NULL;
  3971. xs *cq = xs_dup(xs_dict_get(q_vars, "q"));
  3972. xs *url_acct = NULL;
  3973. if (xs_is_string(cq)) {
  3974. cq = xs_strip_i(cq);
  3975. q = cq;
  3976. }
  3977. /* searching for an URL? */
  3978. if (q && xs_match(q, "https://*|http://*")) {
  3979. /* may by an actor; try a webfinger */
  3980. xs *actor_obj = NULL;
  3981. if (valid_status(webfinger_request(q, &actor_obj, &url_acct)) && xs_is_string(url_acct)) {
  3982. /* it's an actor; do the dirty trick of changing q to the account name */
  3983. q = url_acct;
  3984. }
  3985. else {
  3986. /* bring it to the user's timeline */
  3987. xs *object = NULL;
  3988. int status;
  3989. status = activitypub_request(&snac, q, &object);
  3990. snac_debug(&snac, 1, xs_fmt("Request searched URL %s %d", q, status));
  3991. if (valid_status(status)) {
  3992. /* got it; also request the actor */
  3993. const char *attr_to = get_atto(object);
  3994. if (!xs_is_null(attr_to)) {
  3995. status = actor_request(&snac, attr_to, &actor_obj);
  3996. if (valid_status(status)) {
  3997. /* reset the query string to be the real id */
  3998. url_acct = xs_dup(xs_dict_get(object, "id"));
  3999. q = url_acct;
  4000. /* add the post to the timeline */
  4001. if (!timeline_here(&snac, q))
  4002. timeline_add(&snac, q, object);
  4003. }
  4004. }
  4005. else {
  4006. /* retry webfinger, this time with the 'official' id */
  4007. const char *id = xs_dict_get(object, "id");
  4008. if (xs_is_string(id) && valid_status(webfinger_request(id, &actor_obj, &url_acct)) &&
  4009. xs_is_string(url_acct))
  4010. q = url_acct;
  4011. }
  4012. }
  4013. }
  4014. /* fall through */
  4015. }
  4016. if (q && *q) {
  4017. if (xs_regex_match(q, "^@?[a-zA-Z0-9._]+@[a-zA-Z0-9-]+\\.")) {
  4018. /** search account **/
  4019. xs *actor = NULL;
  4020. xs *acct = NULL;
  4021. xs *l = xs_list_new();
  4022. xs_html *page = NULL;
  4023. if (valid_status(webfinger_request(q, &actor, &acct))) {
  4024. xs *actor_obj = NULL;
  4025. if (valid_status(actor_request(&snac, actor, &actor_obj))) {
  4026. actor_add(actor, actor_obj);
  4027. /* create a people list with only one element */
  4028. l = xs_list_append(l, actor);
  4029. xs *title = xs_fmt(L("Search results for account %s"), q);
  4030. page = html_people_list(&snac, l, title, "wf", NULL, 1);
  4031. }
  4032. }
  4033. if (page == NULL) {
  4034. xs *title = xs_fmt(L("Account %s not found"), q);
  4035. page = xs_html_tag("div",
  4036. xs_html_tag("h2",
  4037. xs_html_attr("class", "snac-header"),
  4038. xs_html_text(title)));
  4039. }
  4040. xs_html *html = xs_html_tag("html",
  4041. html_user_head(&snac, NULL, NULL),
  4042. xs_html_add(html_user_body(&snac, 0),
  4043. page,
  4044. html_footer(user)));
  4045. *body = xs_html_render_s(html, "<!DOCTYPE html>\n");
  4046. *b_size = strlen(*body);
  4047. status = HTTP_STATUS_OK;
  4048. }
  4049. else
  4050. if (*q == '#') {
  4051. /** search by tag **/
  4052. xs *tl = tag_search(q, skip, show + 1);
  4053. int more = 0;
  4054. if (xs_list_len(tl) >= show + 1) {
  4055. /* drop the last one */
  4056. tl = xs_list_del(tl, -1);
  4057. more = 1;
  4058. }
  4059. xs *page = xs_fmt("/admin?q=%%23%s", q + 1);
  4060. xs *title = xs_fmt(xs_list_len(tl) ?
  4061. L("Search results for tag %s") : L("Nothing found for tag %s"), q);
  4062. *body = html_timeline(&snac, tl, 0, skip, show, more, title, page, 0, error, terse);
  4063. *b_size = strlen(*body);
  4064. status = HTTP_STATUS_OK;
  4065. }
  4066. else {
  4067. /** search by content **/
  4068. int to = 0;
  4069. int msecs = atoi(xs_dict_get_def(q_vars, "msecs", "0"));
  4070. xs *tl = content_search(&snac, q, 1, skip, show, msecs, &to);
  4071. xs *title = NULL;
  4072. xs *page = xs_fmt("/admin?q=%s&msecs=%d", q, msecs + 10);
  4073. int tl_len = xs_list_len(tl);
  4074. if (to)
  4075. title = xs_fmt(L("Search results for '%s' (may be more)"), q);
  4076. else
  4077. if (tl_len)
  4078. title = xs_fmt(L("Search results for '%s'"), q);
  4079. else
  4080. if (skip)
  4081. title = xs_fmt(L("No more matches for '%s'"), q);
  4082. else
  4083. title = xs_fmt(L("Nothing found for '%s'"), q);
  4084. *body = html_timeline(&snac, tl, 0, skip, tl_len, to || tl_len == show,
  4085. title, page, 0, error, terse);
  4086. *b_size = strlen(*body);
  4087. status = HTTP_STATUS_OK;
  4088. }
  4089. }
  4090. else {
  4091. /** the private timeline **/
  4092. double t = history_mtime(&snac, "timeline.html_");
  4093. /* if enabled by admin, return a cached page if its timestamp is:
  4094. a) newer than the timeline timestamp
  4095. b) newer than the start time of the server
  4096. */
  4097. if (cache && t > timeline_mtime(&snac) && t > p_state->srv_start_time) {
  4098. snac_debug(&snac, 1, xs_fmt("serving cached timeline"));
  4099. status = history_get(&snac, "timeline.html_", body, b_size,
  4100. xs_dict_get(req, "if-none-match"), etag);
  4101. }
  4102. else {
  4103. int more = 0;
  4104. snac_debug(&snac, 1, xs_fmt("building timeline"));
  4105. xs *list = timeline_list(&snac, "private", skip, show, &more);
  4106. *body = html_timeline(&snac, list, 0, skip, show,
  4107. more, NULL, "/admin", 1, error, terse);
  4108. *b_size = strlen(*body);
  4109. status = HTTP_STATUS_OK;
  4110. if (save)
  4111. history_add(&snac, "timeline.html_", *body, *b_size, etag);
  4112. timeline_add_mark(&snac);
  4113. }
  4114. }
  4115. }
  4116. }
  4117. else
  4118. if (xs_startswith(p_path, "admin/p/")) { /** unique post by md5 **/
  4119. if (!login(&snac, req)) {
  4120. *body = xs_dup(uid);
  4121. status = HTTP_STATUS_UNAUTHORIZED;
  4122. }
  4123. else {
  4124. xs *l = xs_split(p_path, "/");
  4125. const char *md5 = xs_list_get(l, -1);
  4126. if (md5 && *md5 && timeline_here_by_md5(&snac, md5)) {
  4127. xs *list0 = xs_list_append(xs_list_new(), md5);
  4128. xs *list = timeline_top_level(&snac, list0);
  4129. *body = html_timeline(&snac, list, 0, 0, 0, 0, NULL, "/admin", 1, error, terse);
  4130. *b_size = strlen(*body);
  4131. status = HTTP_STATUS_OK;
  4132. }
  4133. }
  4134. }
  4135. else
  4136. if (strcmp(p_path, "people") == 0) { /** the list of people **/
  4137. if (!login(&snac, req)) {
  4138. *body = xs_dup(uid);
  4139. status = HTTP_STATUS_UNAUTHORIZED;
  4140. }
  4141. else {
  4142. *body = html_people(&snac);
  4143. *b_size = strlen(*body);
  4144. status = HTTP_STATUS_OK;
  4145. }
  4146. }
  4147. else
  4148. if (xs_startswith(p_path, "people/")) { /** a single actor **/
  4149. if (!login(&snac, req)) {
  4150. *body = xs_dup(uid);
  4151. status = HTTP_STATUS_UNAUTHORIZED;
  4152. }
  4153. else {
  4154. xs *actor_dict = NULL;
  4155. const char *actor_id = NULL;
  4156. xs *actor = NULL;
  4157. xs_list *page_lst = xs_split_n(p_path, "?", 2);
  4158. xs *page = xs_str_cat(xs_str_new("/"), xs_list_get(page_lst, 0));
  4159. xs_list *l = xs_split_n(page, "/", 3);
  4160. const char *actor_md5 = xs_list_get(l, 2);
  4161. if (valid_status(object_get_by_md5(actor_md5, &actor_dict)) &&
  4162. (actor_id = xs_dict_get(actor_dict, "id")) != NULL &&
  4163. valid_status(actor_get(actor_id, &actor))) {
  4164. int more = 0;
  4165. xs *list = timeline_simple_list(&snac, "private", skip, show, &more);
  4166. *body = html_people_one(&snac, actor_id, list, skip, show, more, page);
  4167. *b_size = strlen(*body);
  4168. status = HTTP_STATUS_OK;
  4169. }
  4170. else {
  4171. *body = xs_dup(uid);
  4172. status = HTTP_STATUS_NOT_FOUND;
  4173. }
  4174. }
  4175. }
  4176. else
  4177. if (strcmp(p_path, "notifications") == 0) { /** the list of notifications **/
  4178. if (!login(&snac, req)) {
  4179. *body = xs_dup(uid);
  4180. status = HTTP_STATUS_UNAUTHORIZED;
  4181. }
  4182. else {
  4183. *body = html_notifications(&snac, skip, show);
  4184. *b_size = strlen(*body);
  4185. status = HTTP_STATUS_OK;
  4186. }
  4187. }
  4188. else
  4189. if (strcmp(p_path, "instance") == 0) { /** instance timeline **/
  4190. if (!login(&snac, req)) {
  4191. *body = xs_dup(uid);
  4192. status = HTTP_STATUS_UNAUTHORIZED;
  4193. }
  4194. else {
  4195. xs *list = timeline_instance_list(skip, show);
  4196. xs *next = timeline_instance_list(skip + show, 1);
  4197. *body = html_timeline(&snac, list, 0, skip, show,
  4198. xs_list_len(next), L("Showing instance timeline"), "/instance", 0, error, terse);
  4199. *b_size = strlen(*body);
  4200. status = HTTP_STATUS_OK;
  4201. }
  4202. }
  4203. else
  4204. if (strcmp(p_path, "pinned") == 0) { /** list of pinned posts **/
  4205. if (!login(&snac, req)) {
  4206. *body = xs_dup(uid);
  4207. status = HTTP_STATUS_UNAUTHORIZED;
  4208. }
  4209. else {
  4210. xs *list = pinned_list(&snac);
  4211. *body = html_timeline(&snac, list, 0, skip, show,
  4212. 0, L("Pinned posts"), "", 0, error, terse);
  4213. *b_size = strlen(*body);
  4214. status = HTTP_STATUS_OK;
  4215. }
  4216. }
  4217. else
  4218. if (strcmp(p_path, "bookmarks") == 0) { /** list of bookmarked posts **/
  4219. if (!login(&snac, req)) {
  4220. *body = xs_dup(uid);
  4221. status = HTTP_STATUS_UNAUTHORIZED;
  4222. }
  4223. else {
  4224. xs *list = bookmark_list(&snac);
  4225. *body = html_timeline(&snac, list, 0, skip, show,
  4226. 0, L("Bookmarked posts"), "", 0, error, terse);
  4227. *b_size = strlen(*body);
  4228. status = HTTP_STATUS_OK;
  4229. }
  4230. }
  4231. else
  4232. if (strcmp(p_path, "drafts") == 0) { /** list of drafts **/
  4233. if (!login(&snac, req)) {
  4234. *body = xs_dup(uid);
  4235. status = HTTP_STATUS_UNAUTHORIZED;
  4236. }
  4237. else {
  4238. xs *list = draft_list(&snac);
  4239. *body = html_timeline(&snac, list, 0, skip, show,
  4240. 0, L("Post drafts"), "", 0, error, terse);
  4241. *b_size = strlen(*body);
  4242. status = HTTP_STATUS_OK;
  4243. }
  4244. }
  4245. else
  4246. if (strcmp(p_path, "sched") == 0) { /** list of scheduled posts **/
  4247. if (!login(&snac, req)) {
  4248. *body = xs_dup(uid);
  4249. status = HTTP_STATUS_UNAUTHORIZED;
  4250. }
  4251. else {
  4252. xs *list = scheduled_list(&snac);
  4253. *body = html_timeline(&snac, list, 0, skip, show,
  4254. 0, L("Scheduled posts"), "", 0, error, terse);
  4255. *b_size = strlen(*body);
  4256. status = HTTP_STATUS_OK;
  4257. }
  4258. }
  4259. else
  4260. if (xs_startswith(p_path, "list/")) { /** list timelines **/
  4261. if (!login(&snac, req)) {
  4262. *body = xs_dup(uid);
  4263. status = HTTP_STATUS_UNAUTHORIZED;
  4264. }
  4265. else {
  4266. xs *l = xs_split(p_path, "/");
  4267. const char *lid = xs_list_get(l, -1);
  4268. xs *list = list_timeline(&snac, lid, skip, show);
  4269. xs *next = list_timeline(&snac, lid, skip + show, 1);
  4270. if (list != NULL) {
  4271. xs *ttl = timeline_top_level(&snac, list);
  4272. xs *base = xs_fmt("/list/%s", lid);
  4273. xs *name = list_maint(&snac, lid, 3);
  4274. xs *title = xs_fmt(L("Showing timeline for list '%s'"), name);
  4275. *body = html_timeline(&snac, ttl, 0, skip, show,
  4276. xs_list_len(next), title, base, 1, error, terse);
  4277. *b_size = strlen(*body);
  4278. status = HTTP_STATUS_OK;
  4279. }
  4280. }
  4281. }
  4282. else
  4283. if (xs_startswith(p_path, "p/")) { /** a timeline with just one entry **/
  4284. if (xs_type(xs_dict_get(snac.config, "private")) == XSTYPE_TRUE)
  4285. return HTTP_STATUS_FORBIDDEN;
  4286. xs *id = xs_fmt("%s/%s", snac.actor, p_path);
  4287. xs *msg = NULL;
  4288. if (valid_status(object_get(id, &msg)) && is_msg_public(msg)) {
  4289. xs *md5 = xs_md5_hex(id, strlen(id));
  4290. xs *list = xs_list_new();
  4291. list = xs_list_append(list, md5);
  4292. *body = html_timeline(&snac, list, 1, 0, 0, 0, NULL, "", 1, error, terse);
  4293. *b_size = strlen(*body);
  4294. status = HTTP_STATUS_OK;
  4295. }
  4296. }
  4297. else
  4298. if (xs_startswith(p_path, "s/")) { /** a static file **/
  4299. xs *l = xs_split_n(p_path, "/", 1);
  4300. const char *id = xs_list_get(l, 1);
  4301. int sz;
  4302. if (id && *id) {
  4303. status = static_get(&snac, id, body, &sz,
  4304. xs_dict_get(req, "if-none-match"), etag);
  4305. if (valid_status(status)) {
  4306. *b_size = sz;
  4307. *ctype = (char *)xs_mime_by_ext(id);
  4308. }
  4309. }
  4310. }
  4311. else
  4312. if (xs_startswith(p_path, "h/")) { /** an entry from the history **/
  4313. if (xs_type(xs_dict_get(snac.config, "private")) == XSTYPE_TRUE)
  4314. return HTTP_STATUS_FORBIDDEN;
  4315. if (xs_type(xs_dict_get(srv_config, "disable_history")) == XSTYPE_TRUE)
  4316. return HTTP_STATUS_FORBIDDEN;
  4317. xs *l = xs_split(p_path, "/");
  4318. const char *id = xs_list_get(l, 1);
  4319. if (id && *id) {
  4320. if (xs_endswith(id, "timeline.html_")) {
  4321. /* Don't let them in */
  4322. *b_size = 0;
  4323. status = HTTP_STATUS_NOT_FOUND;
  4324. }
  4325. else
  4326. status = history_get(&snac, id, body, b_size,
  4327. xs_dict_get(req, "if-none-match"), etag);
  4328. }
  4329. }
  4330. else
  4331. if (strcmp(p_path, ".rss") == 0) { /** public timeline in RSS format **/
  4332. if (xs_type(xs_dict_get(snac.config, "private")) == XSTYPE_TRUE)
  4333. return HTTP_STATUS_FORBIDDEN;
  4334. int cnt = xs_number_get(xs_dict_get_def(srv_config, "max_public_entries", "20"));
  4335. xs *elems = timeline_simple_list(&snac, "public", 0, cnt, NULL);
  4336. xs *bio = xs_dup(xs_dict_get(snac.config, "bio"));
  4337. xs *rss_title = xs_fmt("%s (@%s@%s)",
  4338. xs_dict_get(snac.config, "name"),
  4339. snac.uid,
  4340. xs_dict_get(srv_config, "host"));
  4341. xs *rss_link = xs_fmt("%s.rss", snac.actor);
  4342. *body = rss_from_timeline(&snac, elems, rss_title, rss_link, bio);
  4343. *b_size = strlen(*body);
  4344. *ctype = "application/rss+xml; charset=utf-8";
  4345. status = HTTP_STATUS_OK;
  4346. snac_debug(&snac, 1, xs_fmt("serving RSS"));
  4347. }
  4348. else
  4349. if (proxy && (xs_startswith(p_path, "x/") || xs_startswith(p_path, "y/"))) { /** remote media by proxy **/
  4350. xs *proxy_prefix = NULL;
  4351. if (xs_startswith(p_path, "x/")) {
  4352. /* proxy usage authorized by http basic auth */
  4353. if (login(&snac, req))
  4354. proxy_prefix = xs_str_new("x/");
  4355. else {
  4356. *body = xs_dup(uid);
  4357. status = HTTP_STATUS_UNAUTHORIZED;
  4358. }
  4359. }
  4360. else {
  4361. /* proxy usage authorized by proxy_token */
  4362. xs *tks = xs_fmt("%s:%s", srv_proxy_token_seed, snac.actor);
  4363. xs *tk = xs_md5_hex(tks, strlen(tks));
  4364. xs *p = xs_fmt("y/%s/", tk);
  4365. if (xs_startswith(p_path, p))
  4366. proxy_prefix = xs_dup(p);
  4367. }
  4368. if (proxy_prefix) {
  4369. /* pick the raw path (including optional ? arguments) */
  4370. const char *raw_path = xs_dict_get(req, "raw_path");
  4371. /* skip to where the proxy/ string starts */
  4372. raw_path += xs_str_in(raw_path, proxy_prefix);
  4373. xs *url = xs_replace_n(raw_path, proxy_prefix, "https:/" "/", 1);
  4374. xs *hdrs = xs_dict_new();
  4375. hdrs = xs_dict_append(hdrs, "user-agent", USER_AGENT);
  4376. const char *ims = xs_dict_get(req, "if-modified-since");
  4377. const char *inm = xs_dict_get(req, "if-none-match");
  4378. if (ims) hdrs = xs_dict_append(hdrs, "if-modified-since", ims);
  4379. if (inm) hdrs = xs_dict_append(hdrs, "if-none-match", inm);
  4380. xs *rsp = xs_http_request("GET", url, hdrs,
  4381. NULL, 0, &status, body, b_size, 0);
  4382. if (valid_status(status)) {
  4383. const char *ct = xs_or(xs_dict_get(rsp, "content-type"), "");
  4384. const char *lm = xs_dict_get(rsp, "last-modified");
  4385. const char *et = xs_dict_get(rsp, "etag");
  4386. if (lm) *last_modified = xs_dup(lm);
  4387. if (et) *etag = xs_dup(et);
  4388. /* find the content-type in the static mime types,
  4389. and return that value instead of ct, which will
  4390. be destroyed when out of scope */
  4391. for (int n = 0; xs_mime_types[n]; n += 2) {
  4392. if (strcmp(ct, xs_mime_types[n + 1]) == 0) {
  4393. *ctype = (char *)xs_mime_types[n + 1];
  4394. break;
  4395. }
  4396. }
  4397. }
  4398. snac_debug(&snac, 1, xs_fmt("Proxy for %s %d", url, status));
  4399. }
  4400. }
  4401. else
  4402. if (strcmp(p_path, "share") == 0) { /** direct post **/
  4403. if (!login(&snac, req)) {
  4404. *body = xs_dup(uid);
  4405. status = HTTP_STATUS_UNAUTHORIZED;
  4406. }
  4407. else {
  4408. const char *b64 = xs_dict_get(q_vars, "content");
  4409. xs *b64_2 = xs_replace(b64, " ", "+");
  4410. int sz;
  4411. xs *content = xs_base64_dec(b64_2, &sz);
  4412. xs *msg = msg_note(&snac, content, NULL, NULL, NULL, 0, NULL, NULL);
  4413. xs *c_msg = msg_create(&snac, msg);
  4414. timeline_add(&snac, xs_dict_get(msg, "id"), msg);
  4415. enqueue_message(&snac, c_msg);
  4416. snac_debug(&snac, 1, xs_fmt("web action 'share' received"));
  4417. *body = xs_fmt("%s/admin", snac.actor);
  4418. *b_size = strlen(*body);
  4419. status = HTTP_STATUS_SEE_OTHER;
  4420. }
  4421. }
  4422. else
  4423. if (strcmp(p_path, "authorize_interaction") == 0) { /** follow, like or boost from Mastodon **/
  4424. if (!login(&snac, req)) {
  4425. *body = xs_dup(uid);
  4426. status = HTTP_STATUS_UNAUTHORIZED;
  4427. }
  4428. else {
  4429. status = HTTP_STATUS_NOT_FOUND;
  4430. const char *id = xs_dict_get(q_vars, "id");
  4431. const char *action = xs_dict_get(q_vars, "action");
  4432. if (xs_is_string(id) && xs_is_string(action)) {
  4433. if (strcmp(action, "Follow") == 0) {
  4434. xs *msg = msg_follow(&snac, id);
  4435. if (msg != NULL) {
  4436. const char *actor = xs_dict_get(msg, "object");
  4437. following_add(&snac, actor, msg);
  4438. enqueue_output_by_actor(&snac, msg, actor, 0);
  4439. status = HTTP_STATUS_SEE_OTHER;
  4440. }
  4441. }
  4442. else
  4443. if (xs_match(action, "Like|Boost|Announce")) {
  4444. /* bring the post */
  4445. xs *msg = msg_admiration(&snac, id, *action == 'L' ? "Like" : "Announce");
  4446. if (msg != NULL) {
  4447. timeline_admire(&snac, xs_dict_get(msg, "object"), snac.actor, *action == 'L' ? 1 : 0, msg);
  4448. enqueue_message(&snac, msg);
  4449. status = HTTP_STATUS_SEE_OTHER;
  4450. }
  4451. }
  4452. }
  4453. if (status == HTTP_STATUS_SEE_OTHER) {
  4454. *body = xs_fmt("%s/admin", snac.actor);
  4455. *b_size = strlen(*body);
  4456. }
  4457. }
  4458. }
  4459. else
  4460. status = HTTP_STATUS_NOT_FOUND;
  4461. user_free(&snac);
  4462. if (valid_status(status) && *ctype == NULL) {
  4463. *ctype = "text/html; charset=utf-8";
  4464. }
  4465. return status;
  4466. }
  4467. int html_post_handler(const xs_dict *req, const char *q_path,
  4468. char *payload, int p_size,
  4469. char **body, int *b_size, char **ctype)
  4470. {
  4471. (void)p_size;
  4472. (void)ctype;
  4473. int status = 0;
  4474. const snac *user = NULL;
  4475. snac snac;
  4476. const char *uid;
  4477. const char *p_path;
  4478. const xs_dict *p_vars;
  4479. xs *l = xs_split_n(q_path, "/", 2);
  4480. uid = xs_list_get(l, 1);
  4481. if (!uid || !user_open(&snac, uid)) {
  4482. /* invalid user */
  4483. srv_debug(1, xs_fmt("html_post_handler bad user %s", xs_or(uid, "(null)")));
  4484. return HTTP_STATUS_NOT_FOUND;
  4485. }
  4486. p_path = xs_list_get(l, 2);
  4487. /* all posts must be authenticated */
  4488. if (!login(&snac, req)) {
  4489. user_free(&snac);
  4490. *body = xs_dup(uid);
  4491. return HTTP_STATUS_UNAUTHORIZED;
  4492. }
  4493. user = &snac; /* for L() */
  4494. set_user_lang(&snac);
  4495. p_vars = xs_dict_get(req, "p_vars");
  4496. if (p_path && strcmp(p_path, "admin/note") == 0) { /** **/
  4497. snac_debug(&snac, 1, xs_fmt("web action '%s' received", p_path));
  4498. /* post note */
  4499. const char *content = xs_dict_get(p_vars, "content");
  4500. const char *in_reply_to = xs_dict_get(p_vars, "in_reply_to");
  4501. const char *to = xs_dict_get(p_vars, "to");
  4502. const char *sensitive = xs_dict_get(p_vars, "sensitive");
  4503. const char *summary = xs_dict_get(p_vars, "summary");
  4504. const char *edit_id = xs_dict_get(p_vars, "edit_id");
  4505. const char *post_date = xs_dict_get_def(p_vars, "post_date", "");
  4506. const char *post_time = xs_dict_get_def(p_vars, "post_time", "");
  4507. const char *post_lang = xs_dict_get(p_vars, "post_lang");
  4508. const char *visibility = xs_dict_get(p_vars, "visibility");
  4509. int scope = SCOPE_PUBLIC; /* default to public */
  4510. if (!xs_is_null(visibility)) {
  4511. if (strcmp(visibility, "unlisted") == 0)
  4512. scope = SCOPE_UNLISTED;
  4513. else
  4514. if (strcmp(visibility, "followers") == 0)
  4515. scope = SCOPE_FOLLOWERS;
  4516. else
  4517. if (strcmp(visibility, "mentioned") == 0)
  4518. scope = SCOPE_MENTIONED;
  4519. }
  4520. int store_as_draft = !xs_is_null(xs_dict_get(p_vars, "is_draft"));
  4521. xs *attach_list = xs_list_new();
  4522. /* iterate the attachments */
  4523. int max_attachments = xs_number_get(xs_dict_get_def(srv_config, "max_attachments", "4"));
  4524. for (int att_n = 0; att_n < max_attachments; att_n++) {
  4525. xs *url_lbl = xs_fmt("attach_url_%d", att_n);
  4526. xs *att_lbl = xs_fmt("attach_%d", att_n);
  4527. xs *alt_lbl = xs_fmt("alt_text_%d", att_n);
  4528. const char *attach_url = xs_dict_get(p_vars, url_lbl);
  4529. const xs_list *attach_file = xs_dict_get(p_vars, att_lbl);
  4530. const char *alt_text = xs_dict_get_def(p_vars, alt_lbl, "");
  4531. if (xs_is_string(attach_url) && *attach_url != '\0') {
  4532. xs *l = xs_list_new();
  4533. l = xs_list_append(l, attach_url);
  4534. l = xs_list_append(l, alt_text);
  4535. attach_list = xs_list_append(attach_list, l);
  4536. }
  4537. else
  4538. if (xs_is_list(attach_file)) {
  4539. const char *fn = xs_list_get(attach_file, 0);
  4540. if (xs_is_string(fn) && *fn != '\0') {
  4541. char rnd[32];
  4542. xs_rnd_buf(rnd, sizeof(rnd));
  4543. const char *ext = strrchr(fn, '.');
  4544. xs *hash = xs_md5_hex(rnd, sizeof(rnd));
  4545. xs *id = xs_fmt("post-%s%s", hash, ext ? ext : "");
  4546. xs *url = xs_fmt("%s/s/%s", snac.actor, id);
  4547. int fo = xs_number_get(xs_list_get(attach_file, 1));
  4548. int fs = xs_number_get(xs_list_get(attach_file, 2));
  4549. /* store */
  4550. static_put(&snac, id, payload + fo, fs);
  4551. xs *l = xs_list_new();
  4552. l = xs_list_append(l, url);
  4553. l = xs_list_append(l, alt_text);
  4554. attach_list = xs_list_append(attach_list, l);
  4555. }
  4556. }
  4557. }
  4558. if (content != NULL) {
  4559. xs *msg = NULL;
  4560. xs *c_msg = NULL;
  4561. xs *content_2 = xs_replace(content, "\r", "");
  4562. xs *poll_opts = NULL;
  4563. /* is there a valid set of poll options? */
  4564. const char *v = xs_dict_get(p_vars, "poll_options");
  4565. if (!xs_is_null(v) && *v) {
  4566. xs *v2 = xs_strip_i(xs_replace(v, "\r", ""));
  4567. poll_opts = xs_split(v2, "\n");
  4568. }
  4569. if (!xs_is_null(poll_opts) && xs_list_len(poll_opts)) {
  4570. /* get the rest of poll configuration */
  4571. const char *p_multiple = xs_dict_get(p_vars, "poll_multiple");
  4572. const char *p_end_secs = xs_dict_get(p_vars, "poll_end_secs");
  4573. int multiple = 0;
  4574. int end_secs = atoi(!xs_is_null(p_end_secs) ? p_end_secs : "60");
  4575. if (!xs_is_null(p_multiple) && strcmp(p_multiple, "on") == 0)
  4576. multiple = 1;
  4577. msg = msg_question(&snac, content_2, attach_list,
  4578. poll_opts, multiple, end_secs);
  4579. enqueue_close_question(&snac, xs_dict_get(msg, "id"), end_secs);
  4580. }
  4581. else
  4582. msg = msg_note(&snac, content_2, to, in_reply_to, attach_list, scope, post_lang, NULL);
  4583. if (sensitive != NULL) {
  4584. msg = xs_dict_set(msg, "sensitive", xs_stock(XSTYPE_TRUE));
  4585. msg = xs_dict_set(msg, "summary", xs_is_null(summary) ? "..." : summary);
  4586. }
  4587. if (xs_is_string(post_date) && *post_date) {
  4588. xs *post_pubdate = xs_fmt("%sT%s", post_date,
  4589. xs_is_string(post_time) && *post_time ? post_time : "00:00:00");
  4590. time_t t = xs_parse_iso_date(post_pubdate, 0);
  4591. if (t != 0) {
  4592. t -= xs_tz_offset(snac.tz);
  4593. xs *iso_date = xs_str_iso_date(t);
  4594. msg = xs_dict_set(msg, "published", iso_date);
  4595. snac_debug(&snac, 1, xs_fmt("Published date: [%s]", iso_date));
  4596. }
  4597. else
  4598. snac_log(&snac, xs_fmt("Invalid post date: [%s]", post_pubdate));
  4599. }
  4600. /* is the published date from the future? */
  4601. int future_post = 0;
  4602. xs *right_now = xs_str_utctime(0, ISO_DATE_SPEC);
  4603. if (strcmp(xs_dict_get(msg, "published"), right_now) > 0)
  4604. future_post = 1;
  4605. if (xs_is_null(edit_id)) {
  4606. /* new message */
  4607. const char *id = xs_dict_get(msg, "id");
  4608. if (store_as_draft) {
  4609. draft_add(&snac, id, msg);
  4610. }
  4611. else
  4612. if (future_post) {
  4613. schedule_add(&snac, id, msg);
  4614. }
  4615. else {
  4616. c_msg = msg_create(&snac, msg);
  4617. timeline_add(&snac, id, msg);
  4618. }
  4619. }
  4620. else {
  4621. /* an edition of a previous message */
  4622. xs *p_msg = NULL;
  4623. if (valid_status(object_get(edit_id, &p_msg))) {
  4624. /* copy relevant fields from previous version */
  4625. char *fields[] = { "id", "context", "url",
  4626. "cc", "attributedTo",
  4627. "to", "inReplyTo", NULL };
  4628. int n;
  4629. for (n = 0; fields[n]; n++) {
  4630. const char *v = xs_dict_get(p_msg, fields[n]);
  4631. msg = xs_dict_set(msg, fields[n], v);
  4632. }
  4633. if (store_as_draft) {
  4634. draft_add(&snac, edit_id, msg);
  4635. }
  4636. else
  4637. if (is_draft(&snac, edit_id)) {
  4638. /* message was previously a draft; it's a create activity */
  4639. /* if the date is from the past, overwrite it with right_now */
  4640. if (strcmp(xs_dict_get(msg, "published"), right_now) < 0) {
  4641. snac_debug(&snac, 1, xs_fmt("setting draft ancient date to %s", right_now));
  4642. msg = xs_dict_set(msg, "published", right_now);
  4643. }
  4644. /* overwrite object */
  4645. object_add_ow(edit_id, msg);
  4646. if (future_post) {
  4647. schedule_add(&snac, edit_id, msg);
  4648. }
  4649. else {
  4650. c_msg = msg_create(&snac, msg);
  4651. timeline_add(&snac, edit_id, msg);
  4652. }
  4653. draft_del(&snac, edit_id);
  4654. }
  4655. else
  4656. if (is_scheduled(&snac, edit_id)) {
  4657. /* editing an scheduled post; just update it */
  4658. schedule_add(&snac, edit_id, msg);
  4659. }
  4660. else {
  4661. /* ignore the (possibly changed) published date */
  4662. msg = xs_dict_set(msg, "published", xs_dict_get(p_msg, "published"));
  4663. /* set the updated field */
  4664. xs *updated = xs_str_utctime(0, ISO_DATE_SPEC);
  4665. msg = xs_dict_set(msg, "updated", updated);
  4666. /* overwrite object, not updating the indexes */
  4667. object_add_ow(edit_id, msg);
  4668. /* index tags */
  4669. tag_index(edit_id, msg);
  4670. /* update message */
  4671. c_msg = msg_update(&snac, msg);
  4672. }
  4673. }
  4674. else
  4675. snac_log(&snac, xs_fmt("cannot get object '%s' for editing", edit_id));
  4676. }
  4677. if (c_msg != NULL) {
  4678. enqueue_message(&snac, c_msg);
  4679. enqueue_webmention(msg);
  4680. }
  4681. history_del(&snac, "timeline.html_");
  4682. }
  4683. status = HTTP_STATUS_SEE_OTHER;
  4684. }
  4685. else
  4686. if (p_path && strcmp(p_path, "admin/action") == 0) { /** **/
  4687. /* action on an entry */
  4688. const char *id = xs_dict_get(p_vars, "id");
  4689. const char *actor = xs_dict_get(p_vars, "actor");
  4690. const char *action = xs_dict_get(p_vars, "action");
  4691. const char *group = xs_dict_get(p_vars, "group");
  4692. if (action == NULL)
  4693. return HTTP_STATUS_NOT_FOUND;
  4694. snac_debug(&snac, 1, xs_fmt("web action '%s' received", action));
  4695. status = HTTP_STATUS_SEE_OTHER;
  4696. if (strcmp(action, L("EmojiUnreact")) == 0) { /** **/
  4697. const char *eid = xs_dict_get(p_vars, "eid");
  4698. if (eid != NULL) {
  4699. xs *n_msg = msg_emoji_unreact(&snac, id, eid);
  4700. if (n_msg != NULL)
  4701. enqueue_message(&snac, n_msg);
  4702. }
  4703. }
  4704. else
  4705. if (strcmp(action, L("EmojiReact")) == 0) { /** **/
  4706. xs *eid = xs_dup(xs_dict_get(p_vars, "eid"));
  4707. eid = xs_strip_chars_i(eid, ":");
  4708. xs *ret = msg_emoji_init(&snac, id, eid);
  4709. /* fails if either invalid or already reacted */
  4710. if (!ret)
  4711. ret = msg_emoji_unreact(&snac, id, eid);
  4712. if (!ret)
  4713. status = HTTP_STATUS_NOT_FOUND;
  4714. }
  4715. else
  4716. if (strcmp(action, L("Like")) == 0) { /** **/
  4717. xs *msg = msg_admiration(&snac, id, "Like");
  4718. if (msg != NULL) {
  4719. timeline_admire(&snac, xs_dict_get(msg, "object"), snac.actor, 1, msg);
  4720. enqueue_message(&snac, msg);
  4721. }
  4722. }
  4723. else
  4724. if (strcmp(action, L("Boost")) == 0) { /** **/
  4725. xs *msg = msg_admiration(&snac, id, "Announce");
  4726. if (msg != NULL) {
  4727. timeline_admire(&snac, xs_dict_get(msg, "object"), snac.actor, 0, msg);
  4728. enqueue_message(&snac, msg);
  4729. }
  4730. }
  4731. else
  4732. if (strcmp(action, L("Unlike")) == 0) { /** **/
  4733. xs *msg = msg_repulsion(&snac, id, "Like");
  4734. if (msg != NULL) {
  4735. enqueue_message(&snac, msg);
  4736. }
  4737. }
  4738. else
  4739. if (strcmp(action, L("Unboost")) == 0) { /** **/
  4740. xs *msg = msg_repulsion(&snac, id, "Announce");
  4741. if (msg != NULL) {
  4742. enqueue_message(&snac, msg);
  4743. }
  4744. }
  4745. else
  4746. if (strcmp(action, L("MUTE")) == 0) { /** **/
  4747. mute(&snac, actor);
  4748. }
  4749. else
  4750. if (strcmp(action, L("Unmute")) == 0) { /** **/
  4751. unmute(&snac, actor);
  4752. }
  4753. else
  4754. if (strcmp(action, L("Hide")) == 0) { /** **/
  4755. if (is_draft(&snac, id))
  4756. draft_del(&snac, id);
  4757. else
  4758. if (is_scheduled(&snac, id))
  4759. schedule_del(&snac, id);
  4760. else
  4761. hide(&snac, id);
  4762. }
  4763. else
  4764. if (strcmp(action, L("Limit")) == 0) { /** **/
  4765. limit(&snac, actor);
  4766. }
  4767. else
  4768. if (strcmp(action, L("Unlimit")) == 0) { /** **/
  4769. unlimit(&snac, actor);
  4770. }
  4771. else
  4772. if (strcmp(action, L("Follow")) == 0) { /** **/
  4773. xs *msg = msg_follow(&snac, actor);
  4774. if (msg != NULL) {
  4775. /* reload the actor from the message, in may be different */
  4776. actor = xs_dict_get(msg, "object");
  4777. following_add(&snac, actor, msg);
  4778. enqueue_output_by_actor(&snac, msg, actor, 0);
  4779. }
  4780. }
  4781. else
  4782. if (strcmp(action, L("Unfollow")) == 0) { /** **/
  4783. /* get the following object */
  4784. xs *object = NULL;
  4785. if (valid_status(following_get(&snac, actor, &object))) {
  4786. xs *msg = msg_undo(&snac, xs_dict_get(object, "object"));
  4787. following_del(&snac, actor);
  4788. enqueue_output_by_actor(&snac, msg, actor, 0);
  4789. snac_log(&snac, xs_fmt("unfollowed actor %s", actor));
  4790. }
  4791. else
  4792. snac_log(&snac, xs_fmt("actor is not being followed %s", actor));
  4793. }
  4794. else
  4795. if (strcmp(action, L("Follow Group")) == 0) { /** **/
  4796. xs *msg = msg_follow(&snac, group);
  4797. if (msg != NULL) {
  4798. /* reload the group from the message, in may be different */
  4799. group = xs_dict_get(msg, "object");
  4800. following_add(&snac, group, msg);
  4801. enqueue_output_by_actor(&snac, msg, group, 0);
  4802. }
  4803. }
  4804. else
  4805. if (strcmp(action, L("Unfollow Group")) == 0) { /** **/
  4806. /* get the following object */
  4807. xs *object = NULL;
  4808. if (valid_status(following_get(&snac, group, &object))) {
  4809. xs *msg = msg_undo(&snac, xs_dict_get(object, "object"));
  4810. following_del(&snac, group);
  4811. enqueue_output_by_actor(&snac, msg, group, 0);
  4812. snac_log(&snac, xs_fmt("unfollowed group %s", group));
  4813. }
  4814. else
  4815. snac_log(&snac, xs_fmt("actor is not being followed %s", actor));
  4816. }
  4817. else
  4818. if (strcmp(action, L("Delete")) == 0) { /** **/
  4819. const char *actor_form = xs_dict_get(p_vars, "actor-form");
  4820. if (actor_form != NULL) {
  4821. /* delete follower */
  4822. if (valid_status(follower_del(&snac, actor)))
  4823. snac_log(&snac, xs_fmt("deleted follower %s", actor));
  4824. else
  4825. snac_log(&snac, xs_fmt("error deleting follower %s", actor));
  4826. }
  4827. else {
  4828. /* delete an entry */
  4829. if (is_msg_mine(&snac, id) && !is_draft(&snac, id)) {
  4830. /* it's a post by us: generate a delete */
  4831. xs *msg = msg_delete(&snac, id);
  4832. enqueue_message(&snac, msg);
  4833. snac_log(&snac, xs_fmt("posted tombstone for %s", id));
  4834. }
  4835. timeline_del(&snac, id);
  4836. draft_del(&snac, id);
  4837. schedule_del(&snac, id);
  4838. snac_log(&snac, xs_fmt("deleted entry %s", id));
  4839. }
  4840. }
  4841. else
  4842. if (strcmp(action, L("Pin")) == 0) { /** **/
  4843. pin(&snac, id);
  4844. timeline_touch(&snac);
  4845. }
  4846. else
  4847. if (strcmp(action, L("Unpin")) == 0) { /** **/
  4848. unpin(&snac, id);
  4849. timeline_touch(&snac);
  4850. }
  4851. else
  4852. if (strcmp(action, L("Bookmark")) == 0) { /** **/
  4853. bookmark(&snac, id);
  4854. timeline_touch(&snac);
  4855. }
  4856. else
  4857. if (strcmp(action, L("Unbookmark")) == 0) { /** **/
  4858. unbookmark(&snac, id);
  4859. timeline_touch(&snac);
  4860. }
  4861. else
  4862. if (strcmp(action, L("Approve")) == 0) { /** **/
  4863. xs *fwreq = pending_get(&snac, actor);
  4864. if (fwreq != NULL) {
  4865. xs *reply = msg_accept(&snac, fwreq, actor);
  4866. enqueue_message(&snac, reply);
  4867. if (xs_is_null(xs_dict_get(fwreq, "published"))) {
  4868. /* add a date if it doesn't include one (Mastodon) */
  4869. xs *date = xs_str_utctime(0, ISO_DATE_SPEC);
  4870. fwreq = xs_dict_set(fwreq, "published", date);
  4871. }
  4872. timeline_add(&snac, xs_dict_get(fwreq, "id"), fwreq);
  4873. follower_add(&snac, actor);
  4874. pending_del(&snac, actor);
  4875. snac_log(&snac, xs_fmt("new follower %s", actor));
  4876. }
  4877. }
  4878. else
  4879. if (strcmp(action, L("Discard")) == 0) { /** **/
  4880. pending_del(&snac, actor);
  4881. }
  4882. else
  4883. status = HTTP_STATUS_NOT_FOUND;
  4884. /* delete the cached timeline */
  4885. if (status == HTTP_STATUS_SEE_OTHER)
  4886. history_del(&snac, "timeline.html_");
  4887. }
  4888. else
  4889. if (p_path && strcmp(p_path, "admin/user-setup") == 0) { /** **/
  4890. /* change of user data */
  4891. const char *v;
  4892. const char *p1, *p2;
  4893. if ((v = xs_dict_get(p_vars, "name")) != NULL)
  4894. snac.config = xs_dict_set(snac.config, "name", v);
  4895. if ((v = xs_dict_get(p_vars, "avatar")) != NULL)
  4896. snac.config = xs_dict_set(snac.config, "avatar", v);
  4897. if ((v = xs_dict_get(p_vars, "bio")) != NULL)
  4898. snac.config = xs_dict_set(snac.config, "bio", v);
  4899. if ((v = xs_dict_get(p_vars, "cw")) != NULL &&
  4900. strcmp(v, "on") == 0) {
  4901. snac.config = xs_dict_set(snac.config, "cw", "open");
  4902. } else { /* if the checkbox is not set, the parameter is missing */
  4903. snac.config = xs_dict_set(snac.config, "cw", "");
  4904. }
  4905. if ((v = xs_dict_get(p_vars, "email")) != NULL)
  4906. snac.config = xs_dict_set(snac.config, "email", v);
  4907. if ((v = xs_dict_get(p_vars, "telegram_bot")) != NULL)
  4908. snac.config = xs_dict_set(snac.config, "telegram_bot", v);
  4909. if ((v = xs_dict_get(p_vars, "telegram_chat_id")) != NULL)
  4910. snac.config = xs_dict_set(snac.config, "telegram_chat_id", v);
  4911. if ((v = xs_dict_get(p_vars, "ntfy_server")) != NULL)
  4912. snac.config = xs_dict_set(snac.config, "ntfy_server", v);
  4913. if ((v = xs_dict_get(p_vars, "ntfy_token")) != NULL)
  4914. snac.config = xs_dict_set(snac.config, "ntfy_token", v);
  4915. if ((v = xs_dict_get(p_vars, "purge_days")) != NULL) {
  4916. xs *days = xs_number_new(atof(v));
  4917. snac.config = xs_dict_set(snac.config, "purge_days", days);
  4918. }
  4919. if ((v = xs_dict_get(p_vars, "drop_dm_from_unknown")) != NULL && strcmp(v, "on") == 0)
  4920. snac.config = xs_dict_set(snac.config, "drop_dm_from_unknown", xs_stock(XSTYPE_TRUE));
  4921. else
  4922. snac.config = xs_dict_set(snac.config, "drop_dm_from_unknown", xs_stock(XSTYPE_FALSE));
  4923. if ((v = xs_dict_get(p_vars, "bot")) != NULL && strcmp(v, "on") == 0)
  4924. snac.config = xs_dict_set(snac.config, "bot", xs_stock(XSTYPE_TRUE));
  4925. else
  4926. snac.config = xs_dict_set(snac.config, "bot", xs_stock(XSTYPE_FALSE));
  4927. if ((v = xs_dict_get(p_vars, "private")) != NULL && strcmp(v, "on") == 0)
  4928. snac.config = xs_dict_set(snac.config, "private", xs_stock(XSTYPE_TRUE));
  4929. else
  4930. snac.config = xs_dict_set(snac.config, "private", xs_stock(XSTYPE_FALSE));
  4931. if ((v = xs_dict_get(p_vars, "auto_boost")) != NULL && strcmp(v, "on") == 0)
  4932. snac.config = xs_dict_set(snac.config, "auto_boost", xs_stock(XSTYPE_TRUE));
  4933. else
  4934. snac.config = xs_dict_set(snac.config, "auto_boost", xs_stock(XSTYPE_FALSE));
  4935. if ((v = xs_dict_get(p_vars, "collapse_threads")) != NULL && strcmp(v, "on") == 0)
  4936. snac.config = xs_dict_set(snac.config, "collapse_threads", xs_stock(XSTYPE_TRUE));
  4937. else
  4938. snac.config = xs_dict_set(snac.config, "collapse_threads", xs_stock(XSTYPE_FALSE));
  4939. if ((v = xs_dict_get(p_vars, "approve_followers")) != NULL && strcmp(v, "on") == 0)
  4940. snac.config = xs_dict_set(snac.config, "approve_followers", xs_stock(XSTYPE_TRUE));
  4941. else
  4942. snac.config = xs_dict_set(snac.config, "approve_followers", xs_stock(XSTYPE_FALSE));
  4943. if ((v = xs_dict_get(p_vars, "show_contact_metrics")) != NULL && strcmp(v, "on") == 0)
  4944. snac.config = xs_dict_set(snac.config, "show_contact_metrics", xs_stock(XSTYPE_TRUE));
  4945. else
  4946. snac.config = xs_dict_set(snac.config, "show_contact_metrics", xs_stock(XSTYPE_FALSE));
  4947. if ((v = xs_dict_get(p_vars, "show_unlisted")) != NULL && strcmp(v, "on") == 0)
  4948. snac.config = xs_dict_set(snac.config, "show_unlisted", xs_stock(XSTYPE_TRUE));
  4949. else
  4950. snac.config = xs_dict_set(snac.config, "show_unlisted", xs_stock(XSTYPE_FALSE));
  4951. if ((v = xs_dict_get(p_vars, "web_ui_lang")) != NULL)
  4952. snac.config = xs_dict_set(snac.config, "lang", v);
  4953. if ((v = xs_dict_get(p_vars, "tz")) != NULL)
  4954. snac.config = xs_dict_set(snac.config, "tz", v);
  4955. if ((v = xs_dict_get(p_vars, "post_langs")) != NULL)
  4956. snac.config = xs_dict_set(snac.config, "post_langs", v);
  4957. snac.config = xs_dict_set(snac.config, "latitude", xs_dict_get_def(p_vars, "latitude", ""));
  4958. snac.config = xs_dict_set(snac.config, "longitude", xs_dict_get_def(p_vars, "longitude", ""));
  4959. snac.config = xs_dict_set(snac.config, "notify_webhook", xs_dict_get_def(p_vars, "notify_webhook", ""));
  4960. if ((v = xs_dict_get(p_vars, "metadata")) != NULL)
  4961. snac.config = xs_dict_set(snac.config, "metadata", v);
  4962. /* uploads */
  4963. const char *uploads[] = { "avatar", "header", NULL };
  4964. int n;
  4965. for (n = 0; uploads[n]; n++) {
  4966. xs *var_name = xs_fmt("%s_file", uploads[n]);
  4967. const xs_list *uploaded_file = xs_dict_get(p_vars, var_name);
  4968. if (xs_type(uploaded_file) == XSTYPE_LIST) {
  4969. const char *fn = xs_list_get(uploaded_file, 0);
  4970. if (fn && *fn) {
  4971. const char *mimetype = xs_mime_by_ext(fn);
  4972. if (xs_startswith(mimetype, "image/")) {
  4973. const char *ext = strrchr(fn, '.');
  4974. xs *hash = xs_md5_hex(fn, strlen(fn));
  4975. xs *id = xs_fmt("%s-%s%s", uploads[n], hash, ext ? ext : "");
  4976. xs *url = xs_fmt("%s/s/%s", snac.actor, id);
  4977. int fo = xs_number_get(xs_list_get(uploaded_file, 1));
  4978. int fs = xs_number_get(xs_list_get(uploaded_file, 2));
  4979. /* store */
  4980. static_put(&snac, id, payload + fo, fs);
  4981. snac.config = xs_dict_set(snac.config, uploads[n], url);
  4982. }
  4983. }
  4984. }
  4985. }
  4986. /* delete images by removing url from user.json */
  4987. for (n = 0; uploads[n]; n++) {
  4988. xs *var_name = xs_fmt("%s_delete", uploads[n]);
  4989. const char *delete_var = xs_dict_get(p_vars, var_name);
  4990. if (delete_var != NULL && strcmp(delete_var, "on") == 0) {
  4991. snac.config = xs_dict_set(snac.config, uploads[n], "");
  4992. }
  4993. }
  4994. /* password change? */
  4995. if ((p1 = xs_dict_get(p_vars, "passwd1")) != NULL &&
  4996. (p2 = xs_dict_get(p_vars, "passwd2")) != NULL &&
  4997. *p1 && strcmp(p1, p2) == 0) {
  4998. xs *pw = hash_password(snac.uid, p1, NULL);
  4999. snac.config = xs_dict_set(snac.config, "passwd", pw);
  5000. }
  5001. user_persist(&snac, 1);
  5002. status = HTTP_STATUS_SEE_OTHER;
  5003. }
  5004. else
  5005. if (p_path && strcmp(p_path, "admin/filter-notifications") == 0) { /** **/
  5006. notify_filter(&snac, p_vars);
  5007. user_persist(&snac, 0);
  5008. status = HTTP_STATUS_SEE_OTHER;
  5009. }
  5010. else
  5011. if (p_path && strcmp(p_path, "admin/clear-notifications") == 0) { /** **/
  5012. notify_clear(&snac);
  5013. timeline_touch(&snac);
  5014. status = HTTP_STATUS_SEE_OTHER;
  5015. }
  5016. else
  5017. if (p_path && strcmp(p_path, "admin/vote") == 0) { /** **/
  5018. const char *irt = xs_dict_get(p_vars, "irt");
  5019. const char *opt = xs_dict_get(p_vars, "question");
  5020. const char *actor = xs_dict_get(p_vars, "actor");
  5021. xs *ls = NULL;
  5022. /* multiple choices? */
  5023. if (xs_type(opt) == XSTYPE_LIST)
  5024. ls = xs_dup(opt);
  5025. else
  5026. if (xs_type(opt) == XSTYPE_STRING) {
  5027. ls = xs_list_new();
  5028. ls = xs_list_append(ls, opt);
  5029. }
  5030. const xs_str *v;
  5031. int c = 0;
  5032. while (xs_list_next(ls, &v, &c)) {
  5033. xs *msg = msg_note(&snac, "", actor, irt, NULL, 1, NULL, NULL);
  5034. /* set the option */
  5035. msg = xs_dict_append(msg, "name", v);
  5036. /* delete the content */
  5037. msg = xs_dict_del(msg, "content");
  5038. xs *c_msg = msg_create(&snac, msg);
  5039. enqueue_message(&snac, c_msg);
  5040. timeline_add(&snac, xs_dict_get(msg, "id"), msg);
  5041. }
  5042. if (ls != NULL) {
  5043. /* get the poll object */
  5044. xs *poll = NULL;
  5045. if (valid_status(object_get(irt, &poll))) {
  5046. const char *date = xs_dict_get(poll, "endTime");
  5047. if (xs_is_null(date))
  5048. date = xs_dict_get(poll, "closed");
  5049. if (!xs_is_null(date)) {
  5050. time_t t = xs_parse_iso_date(date, 0) - time(NULL);
  5051. /* request the poll when it's closed;
  5052. Pleroma does not send and update when the poll closes */
  5053. enqueue_object_request(&snac, irt, t + 2);
  5054. }
  5055. }
  5056. }
  5057. status = HTTP_STATUS_SEE_OTHER;
  5058. }
  5059. else
  5060. if (p_path && strcmp(p_path, "admin/followed-hashtags") == 0) { /** **/
  5061. const char *followed_hashtags = xs_dict_get(p_vars, "followed_hashtags");
  5062. if (xs_is_null(followed_hashtags))
  5063. followed_hashtags = "";
  5064. if (xs_is_string(followed_hashtags)) {
  5065. xs *new_hashtags = xs_list_new();
  5066. xs *l = xs_split(followed_hashtags, "\n");
  5067. const char *v;
  5068. xs_list_foreach(l, v) {
  5069. xs *s1 = xs_strip_i(xs_dup(v));
  5070. s1 = xs_replace_i(s1, " ", "");
  5071. if (*s1 == '\0')
  5072. continue;
  5073. xs *s2 = NULL;
  5074. if (xs_startswith(s1, "https:/"))
  5075. s2 = xs_dup(s1);
  5076. else {
  5077. s2 = xs_utf8_to_lower(s1);
  5078. if (*s2 != '#')
  5079. s2 = xs_str_prepend_i(s2, "#");
  5080. }
  5081. new_hashtags = xs_list_append(new_hashtags, s2);
  5082. }
  5083. snac.config = xs_dict_set(snac.config, "followed_hashtags", new_hashtags);
  5084. user_persist(&snac, 0);
  5085. }
  5086. status = HTTP_STATUS_SEE_OTHER;
  5087. }
  5088. else
  5089. if (p_path && strcmp(p_path, "admin/blocked-hashtags") == 0) { /** **/
  5090. const char *hashtags = xs_dict_get(p_vars, "blocked_hashtags");
  5091. if (xs_is_null(hashtags))
  5092. hashtags = "";
  5093. if (xs_is_string(hashtags)) {
  5094. xs *new_hashtags = xs_list_new();
  5095. xs *l = xs_split(hashtags, "\n");
  5096. const char *v;
  5097. xs_list_foreach(l, v) {
  5098. xs *s1 = xs_strip_i(xs_dup(v));
  5099. s1 = xs_replace_i(s1, " ", "");
  5100. if (*s1 == '\0')
  5101. continue;
  5102. xs *s2 = xs_utf8_to_lower(s1);
  5103. if (*s2 != '#')
  5104. s2 = xs_str_prepend_i(s2, "#");
  5105. new_hashtags = xs_list_append(new_hashtags, s2);
  5106. }
  5107. snac.config = xs_dict_set(snac.config, "blocked_hashtags", new_hashtags);
  5108. user_persist(&snac, 0);
  5109. }
  5110. status = HTTP_STATUS_SEE_OTHER;
  5111. }
  5112. else
  5113. if (p_path && strcmp(p_path, "admin/muted-words") == 0) {
  5114. const char *words = xs_dict_get(p_vars, "muted_words");
  5115. if (xs_is_string(words)) {
  5116. xs *new_words = xs_list_new();
  5117. xs *l = xs_split(words, "\n");
  5118. const char *v;
  5119. xs_list_foreach(l, v) {
  5120. xs *s1 = xs_strip_i(xs_dup(v));
  5121. s1 = xs_replace_i(s1, " ", "");
  5122. if (*s1 == '\0')
  5123. continue;
  5124. xs *s2 = xs_utf8_to_lower(s1);
  5125. new_words = xs_list_insert_sorted(new_words, s2);
  5126. }
  5127. snac.config = xs_dict_set(snac.config, "muted_words", new_words);
  5128. user_persist(&snac, 0);
  5129. }
  5130. status = HTTP_STATUS_SEE_OTHER;
  5131. }
  5132. if (status == HTTP_STATUS_SEE_OTHER) {
  5133. const char *hard_redir = xs_dict_get(p_vars, "hard-redir");
  5134. if (xs_is_string(hard_redir))
  5135. *body = xs_dup(hard_redir);
  5136. else {
  5137. const char *redir = xs_dict_get(p_vars, "redir");
  5138. if (xs_is_null(redir))
  5139. redir = "top";
  5140. *body = xs_fmt("%s/admin#%s", snac.actor, redir);
  5141. }
  5142. *b_size = strlen(*body);
  5143. }
  5144. user_free(&snac);
  5145. return status;
  5146. }