html.c 214 KB

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