html.c 213 KB

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