html.c 223 KB

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