ap.ml 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606
  1. (*
  2. * _ _ ____ _
  3. * _| || |_/ ___| ___ _ __ _ __ ___ | |
  4. * |_ .. _\___ \ / _ \ '_ \| '_ \ / _ \| |
  5. * |_ _|___) | __/ |_) | |_) | (_) |_|
  6. * |_||_| |____/ \___| .__/| .__/ \___/(_)
  7. * |_| |_|
  8. *
  9. * Personal Social Ap.
  10. *
  11. * Copyright (C) The #Seppo contributors. All rights reserved.
  12. *
  13. * This program is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation, either version 3 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  25. *)
  26. let seppo_cgi' = Cfg.seppo_cgi
  27. let apub = "activitypub/"
  28. let proj = apub ^ "actor.jsa" (* the public actor profile *)
  29. let prox = apub ^ "actor.xml" (* the public actor profile *)
  30. let content_length_max = 10 * 1024
  31. let ( let* ) = Result.bind
  32. let ( >>= ) = Result.bind
  33. let to_result none = Option.to_result ~none
  34. let chain a b =
  35. let f a = Ok (a, b) in
  36. Result.bind a f
  37. let write oc (j : Ezjsonm.t) =
  38. Ezjsonm.to_channel ~minify:false oc j;
  39. Ok ""
  40. let writev oc (j : Ezjsonm.value) =
  41. Ezjsonm.value_to_channel ~minify:false oc j;
  42. Ok ""
  43. let json_from_file fn =
  44. let ic = open_in_gen [ Open_rdonly; Open_binary ] 0 fn in
  45. let j = Ezjsonm.value_from_channel ic in
  46. close_in ic;
  47. Ok j
  48. (** X509.Public_key from PEM. *)
  49. module PubKeyPem = struct
  50. let of_pem s =
  51. s
  52. |> X509.Public_key.decode_pem
  53. let target = apub ^ "id_rsa.pub.pem"
  54. let pk_pem = "app/etc/id_rsa.priv.pem"
  55. let pk_rule : Make.t = {
  56. target = pk_pem;
  57. prerequisites = [];
  58. fresh = Make.Missing;
  59. command = fun _ _ _ ->
  60. File.out_channel_replace (fun oc ->
  61. Logr.debug (fun m -> m "create private key pem.");
  62. (* https://discuss.ocaml.org/t/tls-signature-with-opam-tls/9399/3?u=mro
  63. * $ openssl genrsa -out app/etc/id_rsa.priv.pem 2048
  64. *)
  65. try
  66. `RSA
  67. |> X509.Private_key.generate ~bits:2048
  68. |> X509.Private_key.encode_pem
  69. |> output_string oc;
  70. Ok ""
  71. with _ ->
  72. Logr.err (fun m -> m "%s couldn't create pk" E.e1010);
  73. Error "couldn't create pk")
  74. }
  75. let rule : Make.t = {
  76. target;
  77. prerequisites = [ pk_pem ];
  78. fresh = Make.Outdated;
  79. command = fun _pre _ r ->
  80. File.out_channel_replace (fun oc ->
  81. Logr.debug (fun m -> m "create public key pem." );
  82. match r.prerequisites with
  83. | [ fn_priv ] -> (
  84. assert (fn_priv = pk_pem);
  85. match
  86. fn_priv
  87. |> File.to_string
  88. |> X509.Private_key.decode_pem
  89. with
  90. | Ok (`RSA _ as key) ->
  91. key
  92. |> X509.Private_key.public
  93. |> X509.Public_key.encode_pem
  94. |> output_string oc;
  95. Ok ""
  96. | Ok _ ->
  97. Logr.err (fun m -> m "%s %s" E.e1032 "wrong key flavour, must be RSA.");
  98. Error "wrong key flavour, must be RSA."
  99. | Error (`Msg mm) ->
  100. Logr.err (fun m -> m "%s %s" E.e1033 mm);
  101. Error mm
  102. )
  103. | l ->
  104. Error
  105. (Printf.sprintf
  106. "rule must have exactly one dependency, not %d"
  107. (List.length l)))
  108. }
  109. let rulez = pk_rule :: rule :: []
  110. let make pre =
  111. Make.make ~pre rulez target
  112. let private_of_pem_data pem_data =
  113. match pem_data
  114. |> X509.Private_key.decode_pem with
  115. | Ok ((`RSA _) as pk) -> Ok pk
  116. | Ok _ -> Error "key must be RSA"
  117. | Error (`Msg e) -> Error e
  118. (** load a private key pem from a file *)
  119. let private_of_pem fn =
  120. fn
  121. |> File.to_string
  122. |> private_of_pem_data
  123. (** RSA SHA256 sign data with pk.
  124. returns
  125. algorithm,signature
  126. with algorithm currently being fixed to rsa-sha256.
  127. See https://datatracker.ietf.org/doc/html/draft-cavage-http-signatures-12#autoid-38
  128. *)
  129. let sign pk (data : string) : (string * string) =
  130. (* Logr.debug (fun m -> m "PubKeyPem.sign"); *)
  131. (*
  132. * https://discuss.ocaml.org/t/tls-signature-with-opam-tls/9399/9?u=mro
  133. * https://mirleft.github.io/ocaml-x509/doc/x509/X509/Private_key/#cryptographic-sign-operation
  134. *)
  135. (Http.Signature.RSA_SHA256.name, Http.Signature.RSA_SHA256.sign pk (`Message data)
  136. |> Result.get_ok)
  137. (** https://datatracker.ietf.org/doc/html/draft-cavage-http-signatures-12#autoid-38
  138. *)
  139. let verify ~algo ~inbox ~key ~signature data =
  140. let data = `Message data
  141. and _ = inbox in
  142. match algo with
  143. | "hs2019" -> (* https://datatracker.ietf.org/doc/html/draft-cavage-http-signatures-12#autoid-38 *)
  144. (match Http.Signature.HS2019.verify
  145. ~signature
  146. key
  147. data with
  148. | Error (`Msg "bad signature") ->
  149. (* gotosocial and unnamed other AP implementations seem to use `SHA256 and `RSA_PKCS1
  150. while
  151. https://datatracker.ietf.org/doc/html/draft-cavage-http-signatures-12#autoid-38
  152. and
  153. https://datatracker.ietf.org/doc/id/draft-richanna-http-message-signatures-00.html#name-hs2019
  154. as I understand them recommend `SHA512 and `RSA_PSS. *)
  155. (match Http.Signature.RSA_SHA256.verify
  156. ~signature
  157. key
  158. data with
  159. | Ok _ as o ->
  160. Logr.info (fun m -> m "%s.%s another dadaist http signature" "Ap.PubKeyPem" "verify");
  161. o
  162. | x -> x)
  163. | x -> x)
  164. | "rsa-sha256" ->
  165. Http.Signature.RSA_SHA256.verify
  166. ~signature
  167. key
  168. data
  169. | a ->
  170. Error (`Msg (Printf.sprintf "unknown algorithm: '%s'" a))
  171. (** not key related *)
  172. let digest_base64 s =
  173. Logr.debug (fun m -> m "%s.%s %s" "Ap.PubKeyPem" "digest" "SHA-256");
  174. "SHA-256=" ^ Digestif.SHA256.(s
  175. |> digest_string
  176. |> to_raw_string
  177. |> Base64.encode_exn )
  178. let digest_base64' s =
  179. Some (digest_base64 s)
  180. end
  181. module Actor = struct
  182. let http_get ?(key = None) u =
  183. Logr.debug (fun m -> m "%s.%s %a" "Ap.Actor" "http_get" Uri.pp u);
  184. let%lwt p = u |> Http.get_jsonv ~key Result.ok in
  185. (match p with
  186. | Error _ as e -> e
  187. | Ok (r,j) ->
  188. match r.status with
  189. | #Cohttp.Code.success_status ->
  190. let mape (e : Ezjsonm.value Decoders__Error.t) =
  191. let s = e |> Decoders_ezjsonm.Decode.string_of_error in
  192. Logr.err (fun m -> m "%s %s.%s failed to decode actor %a:\n%s" E.e1002 "Ap.Actor" "http_get" Uri.pp u s);
  193. s in
  194. j
  195. |> As2_vocab.Decode.person
  196. |> Result.map_error mape
  197. | _sta -> Format.asprintf "HTTP %a %a" Http.pp_status r.status Uri.pp u
  198. |> Result.error)
  199. |> Lwt.return
  200. end
  201. let sep n = `Data ("\n" ^ String.make (n*2) ' ')
  202. (** A person actor object. https://www.w3.org/TR/activitypub/#actor-objects *)
  203. module Person = struct
  204. (** generate my key-id from my actor id. *)
  205. let my_key_id me =
  206. Uri.with_fragment me (Some "main-key")
  207. let empty = ({
  208. id = Uri.empty;
  209. inbox = Uri.empty;
  210. outbox = Uri.empty;
  211. followers = None;
  212. following = None;
  213. attachment = [];
  214. discoverable = false;
  215. generator = None;
  216. icon = [];
  217. image = None;
  218. manually_approves_followers= true;
  219. name = None;
  220. name_map = [];
  221. preferred_username = None;
  222. preferred_username_map = [];
  223. public_key = {
  224. id = Uri.empty;
  225. owner = None;
  226. pem = "";
  227. signatureAlgorithm = None;
  228. };
  229. published = None;
  230. summary = None;
  231. summary_map = [];
  232. url = [];
  233. } : As2_vocab.Types.person)
  234. let prsn _pubdate (pem, ((pro : Cfg.Profile.t), (Auth.Uid uid, _base))) =
  235. let Rfc4287.Rfc4646 la = pro.language in
  236. let actor = Uri.make ~path:proj () in
  237. let path u = u |> Http.reso ~base:actor in
  238. ({
  239. id = actor;
  240. inbox = Uri.make ~path:("../" ^ seppo_cgi' ^ "/" ^ apub ^ "inbox.jsa") () |> path;
  241. outbox = Uri.make ~path:"outbox/index.jsa" () |> path;
  242. followers = Some (Uri.make ~path:"subscribers/index.jsa" () |> path);
  243. following = Some (Uri.make ~path:"subscribed_to/index.jsa" () |> path);
  244. attachment = [];
  245. discoverable = true;
  246. generator = Some {href=St.seppo_u; name=(Some St.seppo_c); name_map=[]; rel=None };
  247. icon = [ (Uri.make ~path:"../me-avatar.jpg" () |> path) ];
  248. image = Some (Uri.make ~path:"../me-banner.jpg" () |> path);
  249. manually_approves_followers= false;
  250. name = Some pro.title;
  251. name_map = [];
  252. preferred_username = Some uid;
  253. preferred_username_map = [];
  254. public_key = {
  255. id = actor |> my_key_id;
  256. owner = Some actor; (* add this deprecated property to make mastodon happy *)
  257. pem;
  258. signatureAlgorithm = Some "https://www.w3.org/2001/04/xmldsig-more#rsa-sha256"; (* from hubzilla, e.g. https://im.allmendenetz.de/channel/minetest *)
  259. };
  260. published = None;
  261. summary = Some pro.bio;
  262. summary_map = [(la,pro.bio)];
  263. url = [ Uri.make ~path:"../" () |> path ];
  264. } : As2_vocab.Types.person)
  265. module Json = struct
  266. let decode j =
  267. j
  268. |> As2_vocab.Decode.person
  269. |> Result.map_error (fun _ -> "@TODO aua json")
  270. let encode _pubdate (pem, ((pro : Cfg.Profile.t), (uid, base))) =
  271. let Rfc4287.Rfc4646 l = pro.language in
  272. let lang = Some l in
  273. prsn _pubdate (pem, (pro, (uid, base)))
  274. |> As2_vocab.Encode.person ~base ~lang
  275. |> Result.ok
  276. end
  277. let x2txt v =
  278. Markup.(v
  279. |> string
  280. |> parse_html
  281. |> signals
  282. (* |> filter_map (function
  283. | `Text _ as t -> Some t
  284. | `Start_element ((_,"p"), _) -> Some (`Text ["\n<p>&#0x10;\n"])
  285. | `Start_element ((_,"br"), _) -> Some (`Text ["\n<br>\n"])
  286. | _ -> None)
  287. |> write_html
  288. *)
  289. |> text
  290. |> to_string)
  291. let x2txt' v =
  292. Option.bind v (fun x -> Some (x |> x2txt))
  293. let flatten (p : As2_vocab.Types.person) =
  294. {p with
  295. summary = x2txt' p.summary;
  296. attachment = List.fold_left (fun init (e : As2_vocab.Types.property_value) ->
  297. ({e with value = x2txt e.value}) :: init) [] p.attachment}
  298. let target = proj
  299. let rule : Make.t =
  300. {
  301. target;
  302. prerequisites = [
  303. Auth.fn;
  304. Cfg.Base.fn;
  305. Cfg.Profile.fn;
  306. PubKeyPem.target;
  307. ];
  308. fresh = Make.Outdated;
  309. command = fun pre _ _ ->
  310. File.out_channel_replace (fun oc ->
  311. let now = Ptime_clock.now () in
  312. Cfg.Base.(fn |> from_file)
  313. >>= chain Auth.(fn |> uid_from_file)
  314. >>= chain Cfg.Profile.(fn |> from_file)
  315. >>= chain (PubKeyPem.make pre >>= File.cat)
  316. >>= Json.encode now
  317. >>= writev oc)
  318. }
  319. let rulez = rule :: PubKeyPem.rulez
  320. let make pre = Make.make ~pre rulez target
  321. let from_file fn =
  322. fn
  323. |> json_from_file
  324. >>= Json.decode
  325. module Rdf = struct
  326. let encode' ~base ~lang ({ id; name; name_map; url; inbox; outbox;
  327. preferred_username; preferred_username_map; summary; summary_map;
  328. manually_approves_followers;
  329. discoverable; generator; followers; following;
  330. public_key; published; attachment; icon; image}: As2_vocab.Types.person) : _ Xmlm.frag =
  331. let ns_as = As2_vocab.Constants.ActivityStreams.ns_as ^ "#"
  332. and ns_ldp = "http://www.w3.org/ns/ldp#"
  333. and ns_rdf = "http://www.w3.org/1999/02/22-rdf-syntax-ns#"
  334. and ns_schema = "http://schema.org#"
  335. (* and ns_sec = As2_vocab.Constants.ActivityStreams.ns_sec ^ "#" *)
  336. and ns_toot = "http://joinmastodon.org/ns#"
  337. and ns_xsd = "http://www.w3.org/2001/XMLSchema#" in
  338. let txt ?(lang = None) ?(datatype = None) ns tn (s : string) =
  339. let att = [] in
  340. let att = match lang with
  341. | Some v -> ((Xmlm.ns_xml, "lang"), v) :: att
  342. | None -> att in
  343. let att = match datatype with
  344. | Some v -> ((ns_rdf, "datatype"), v) :: att
  345. | None -> att in
  346. `El (((ns, tn), att), [`Data s]) in
  347. let uri ns tn u = `El (((ns, tn), [ ((ns_rdf, "resource"), u |> Http.reso ~base |> Uri.to_string) ]), []) in
  348. let txt' ns tn none s' = s' |> Option.fold ~none ~some:(fun n -> txt ns tn n :: sep 2 :: none) in
  349. let link_tbd ns tn none s' = s' |> Option.fold ~none ~some:(fun (_ : As2_vocab.Types.link) ->
  350. `El (((ns, tn), []), [ (* @TODO *) ])
  351. :: sep 2 :: none) in
  352. let bool' ns tn none s' = s' |> Option.fold ~none ~some:(fun n -> txt ~datatype:(Some (ns_xsd ^ "boolean")) ns tn (if n then "true" else "false") :: sep 2 :: none) in
  353. let rfc3339' ns tn none s'=s'|> Option.fold ~none ~some:(fun n -> txt ~datatype:(Some (ns_xsd ^ "dateTime")) ns tn (n |> Ptime.to_rfc3339) :: sep 2 :: none) in
  354. let uri' ns tn none s' = s' |> Option.fold ~none ~some:(fun n -> uri ns tn n :: sep 2 :: none) in
  355. let img' _n tn none (u' : Uri.t option) = u' |> Option.fold ~none ~some:(fun u ->
  356. `El (((ns_as, tn), []),
  357. sep 3
  358. :: `El (((ns_as, "Image"), []),
  359. sep 4
  360. :: uri ns_as "url" u
  361. :: [])
  362. :: []) :: sep 2 :: none
  363. ) in
  364. let img'' _n tn none (u' : Uri.t list) = img' _n tn none (List.nth_opt u' 0) in
  365. let lang = lang |> Option.value ~default:"und" in
  366. Logr.debug (fun m -> m "%s.%s %a %s" "Ap.Person.RDF" "encode" Uri.pp base lang);
  367. let _ = public_key in
  368. let f_map name init (lang,value) = txt ~lang:(Some lang) ns_as name value :: sep 3 :: init in
  369. let f_uri name init value = uri ns_as name value :: sep 2 :: init in
  370. let f_att init ({name; name_map; value; value_map} : As2_vocab.Types.property_value) =
  371. let _ = name_map and _ = value_map in (* TODO *)
  372. let sub = sep 4
  373. :: txt ns_as "name" name
  374. :: sep 4
  375. :: txt ns_schema "value" value
  376. :: [] in
  377. let sub = name_map |> List.fold_left (f_map "name") sub in
  378. let sub = value_map |> List.fold_left (f_map "value") sub in
  379. `El (((ns_as, "attachment"), []),
  380. sep 3
  381. :: `El (((ns_schema, "PropertyValue"), []), sub)
  382. :: []) :: sep 2 :: init in
  383. let chi = [] in
  384. let chi = Some outbox |> uri' ns_as "outbox" chi in
  385. let chi = Some inbox |> uri' ns_ldp "inbox" chi in
  386. let chi = followers |> uri' ns_as "followers" chi in
  387. let chi = following |> uri' ns_as "following" chi in
  388. let chi = attachment |> List.fold_left f_att chi in
  389. let chi = image |> img' ns_as "image" chi in
  390. let chi = icon |> img'' ns_as "icon" chi in
  391. let chi = summary |> txt' ns_as "summary" chi in
  392. let chi = summary_map |> List.fold_left (f_map "summary") chi in
  393. let chi = url |> List.fold_left (f_uri "url") chi in
  394. let chi = name |> txt' ns_as "name" chi in
  395. let chi = name_map |> List.fold_left (f_map "name") chi in
  396. let chi = generator |> link_tbd ns_as "generator" chi in
  397. let chi = Some discoverable |> bool' ns_toot "discoverable" chi in
  398. let chi = Some manually_approves_followers |> bool' ns_as "manuallyApprovesFollowers" chi in
  399. let chi = published |> rfc3339' ns_as "published" chi in
  400. let chi = preferred_username |> txt' ns_as "preferredUsername" chi in
  401. let chi = preferred_username_map |> List.fold_left (f_map "preferredUsername") chi in
  402. let chi = Some id |> uri' ns_as "id" chi in
  403. let chi = sep 2 :: chi in
  404. `El (((ns_as, "Person"), [
  405. ((Xmlm.ns_xmlns, "as"), ns_as);
  406. ((Xmlm.ns_xmlns, "ldp"), ns_ldp);
  407. ((Xmlm.ns_xmlns, "schema"), ns_schema);
  408. (* ((Xmlm.ns_xmlns, "sec"), ns_sec); *)
  409. ((Xmlm.ns_xmlns, "toot"), ns_toot);
  410. (* needs to be inline vebose ((Xmlm.ns_xmlns, "xsd"), ns_xsd); *)
  411. ((ns_rdf, "about"), "");
  412. ((Xmlm.ns_xml, "lang"), lang);
  413. ]), chi)
  414. (* Alternatively may want to take a Ap.Feder.t *)
  415. let encode ?(token = None) ?(is_in_subscribers = None) ?(am_subscribed_to = None) ?(blocked = None) ~base ~lang pe : _ Xmlm.frag =
  416. let open Xml in
  417. let txt ?(datatype = None) ns tn (s : string) =
  418. `El (((ns, tn), match datatype with
  419. | Some ty -> [((ns_rdf, "datatype"), ty)]
  420. | None -> []), [`Data s]) in
  421. let txt' ns tn none s' = s' |> Option.fold ~none ~some:(fun n -> txt ns tn n :: sep 2 :: none) in
  422. let noyes' ns tn none s' = s' |> Option.fold ~none ~some:(fun n -> txt ns tn (n |> As2.No_p_yes.to_string) :: sep 2 :: none) in
  423. `El (((ns_rdf, "RDF"), [
  424. ((Xmlm.ns_xmlns, "rdf"), ns_rdf);
  425. ((Xmlm.ns_xmlns, "seppo"), ns_seppo);
  426. ((Xmlm.ns_xml,"base"),base |> Uri.to_string);
  427. ]),
  428. sep 1 ::
  429. `El (((ns_rdf, "Description"), [ (ns_rdf, "about"), "" ]),
  430. sep 2 ::
  431. txt' ns_seppo "token" [] token @
  432. noyes' ns_seppo "is_subscriber" [] is_in_subscribers @
  433. noyes' ns_seppo "am_subscribed_to" [] am_subscribed_to @
  434. noyes' ns_seppo "is_blocked" [] blocked
  435. )
  436. :: sep 1
  437. :: encode' ~base ~lang pe
  438. :: [])
  439. end
  440. end
  441. (* Xml subset of the profle page. *)
  442. module PersonX = struct
  443. let xml_ pubdate (pem, (pro, (uid, base))) =
  444. let Rfc4287.Rfc4646 lang = (pro : Cfg.Profile.t).language in
  445. Person.prsn pubdate (pem, (pro, (uid, base)))
  446. |> Person.Rdf.encode ~base ~lang:(Some lang)
  447. |> Result.ok
  448. let target = prox
  449. let rule = {Person.rule
  450. with target;
  451. command = fun pre _ _ ->
  452. File.out_channel_replace (fun oc ->
  453. let now = Ptime_clock.now () in
  454. let writex oc x =
  455. let xsl = Some "../themes/current/actor.xsl" in
  456. Xml.to_chan ~xsl x oc;
  457. Ok "" in
  458. Cfg.Base.(fn |> from_file)
  459. >>= chain Auth.(fn |> uid_from_file)
  460. >>= chain Cfg.Profile.(fn |> from_file)
  461. >>= chain (PubKeyPem.make pre >>= File.cat)
  462. >>= xml_ now
  463. >>= writex oc) }
  464. let rulez = rule :: PubKeyPem.rulez
  465. let make pre = Make.make ~pre rulez target
  466. end
  467. (**
  468. * https://www.w3.org/TR/activitystreams-core/
  469. * https://www.w3.org/TR/activitystreams-core/#media-type
  470. *)
  471. let send ?(success = `OK) ~key (f_ok : Cohttp.Response.t * string -> unit) to_ msg =
  472. let body = msg |> Ezjsonm.value_to_string in
  473. let signed_headers body = PubKeyPem.(Http.signed_headers key (digest_base64' body) to_) in
  474. let headers = signed_headers body in
  475. let headers = Http.H.add' headers Http.H.ct_jlda in
  476. let headers = Http.H.add' headers Http.H.acc_app_jlda in
  477. (* TODO queue it and re-try in case of failure *)
  478. let%lwt r = Http.post ~headers body to_ in
  479. (match r with
  480. | Ok (res,body') ->
  481. let%lwt body' = body' |> Cohttp_lwt.Body.to_string in
  482. (match res.status with
  483. | #Cohttp.Code.success_status ->
  484. Logr.debug (fun m -> m "%s.%s %a\n%a\n\n%s" "Ap" "send" Uri.pp to_ Cohttp.Response.pp_hum res body');
  485. f_ok (res, body');
  486. Ok (success, [Http.H.ct_plain], Cgi.Response.body "ok")
  487. | sta ->
  488. Logr.warn (fun m -> m "%s.%s %a\n%a\n\n%s" "Ap" "send" Uri.pp to_ Cohttp.Response.pp_hum res body');
  489. Http.s502 ~body:(sta |> Cohttp.Code.string_of_status |> Cgi.Response.body ~ee:E.e1039) ()
  490. ) |> Lwt.return
  491. | Error e ->
  492. Logr.warn (fun m -> m "%s.%s <- %s %a\n%s" "Ap" "send" "post" Uri.pp to_ e);
  493. Http.s500 |> Lwt.return)
  494. let snd_reject
  495. ~uuid
  496. ~base
  497. ~key
  498. me
  499. (siac : As2_vocab.Types.person)
  500. (j : Ezjsonm.value) =
  501. Logr.warn(fun m -> m "%s.%s %a %a" "Ap" "snd_reject" Uuidm.pp uuid Uri.pp siac.inbox);
  502. assert (not (me |> Uri.equal siac.id));
  503. let reject me id =
  504. `O [("@context", `String As2_vocab.Constants.ActivityStreams.ns_as);
  505. ("type", `String "Reject");
  506. ("actor", `String (me |> Http.reso ~base |> Uri.to_string));
  507. ("object", `String (id |> Uri.to_string))]
  508. in
  509. let id = match j with
  510. | `O (_ :: ("id", `String id) :: _) -> id |> Uri.of_string
  511. | _ -> Uri.empty in
  512. id
  513. |> reject me
  514. |> send ~success:`Unprocessable_entity ~key
  515. (fun _ -> Logr.info (fun m -> m "%s.%s Reject %a due to fallthrough to %a" "Ap" "snd_reject" Uri.pp id Uri.pp siac.inbox))
  516. siac.inbox
  517. (** re-used for following as well (there using block, too) *)
  518. module Followers = struct
  519. (** follower tri-state *)
  520. module State = struct
  521. (** Tri-state *)
  522. type t =
  523. | Pending
  524. | Accepted
  525. | Blocked
  526. let of_string = function
  527. | "pending" -> Some Pending
  528. | "accepted" -> Some Accepted
  529. | "blocked" -> Some Blocked
  530. | _ -> None
  531. let to_string = function
  532. | Pending -> "pending"
  533. | Accepted -> "accepted"
  534. | Blocked -> "blocked"
  535. let predicate ?(invert = false) (s : t) =
  536. let r = match s with
  537. | Pending
  538. | Accepted -> true
  539. | Blocked -> false in
  540. if invert
  541. then not r
  542. else r
  543. (** Rich follower state info:
  544. state, timestamp, actor id, name, rfc7565, inbox
  545. *)
  546. type t' = t * Ptime.t * Uri.t * string option * Rfc7565.t option * Uri.t option
  547. let ibox (_,_,ibox,_,_,_ : t') : Uri.t = ibox
  548. (** input to fold_left *)
  549. let ibox' f a (k,v) = f a (k,v |> ibox)
  550. let of_actor tnow st (siac : As2_vocab.Types.person) : t' =
  551. let us = match Uri.host siac.id, siac.preferred_username with
  552. | None,_
  553. | _,None -> None
  554. | Some domain, Some local -> Some Rfc7565.(make ~local ~domain ()) in
  555. (st,tnow,siac.inbox,siac.name,us,List.nth_opt siac.icon 0)
  556. let decode = function
  557. | Csexp.(List [Atom "1"; Atom s; Atom t0; Atom inbox; Atom name; Atom rfc7565; Atom avatar]) ->
  558. Option.bind
  559. (s |> of_string)
  560. (fun s ->
  561. match t0 |> Ptime.of_rfc3339 with
  562. | Ok (t,_,_) ->
  563. let inbox = inbox |> Uri.of_string
  564. and rfc7565 = rfc7565 |> Rfc7565.of_string |> Result.to_option
  565. and avatar = avatar |> Uri.of_string in
  566. let r : t' = (s,t,inbox,Some name,rfc7565,Some avatar) in
  567. Some r
  568. | _ -> None )
  569. (* legacy: *)
  570. (* assume the preferred_username is @ attached to the inbox *)
  571. | Csexp.(List [Atom s; Atom t0; Atom inbox]) ->
  572. Option.bind
  573. (s |> of_string)
  574. (fun s ->
  575. match t0 |> Ptime.of_rfc3339 with
  576. | Ok (t,_,_) ->
  577. let inbox = inbox |> Uri.of_string in
  578. let us = Option.bind
  579. (inbox |> Uri.user)
  580. (fun local -> Some Rfc7565.(make ~local ~domain:(inbox |> Uri.host_with_default ~default:"-") ())) in
  581. let r : t' = (s,t,Uri.with_userinfo inbox None,inbox |> Uri.user,us,None) in
  582. Some r
  583. | _ -> None)
  584. | _ -> None
  585. let decode' = function
  586. | Ok s -> s |> decode
  587. | _ -> None
  588. let encode ((state,t,inbox,name,(us : Rfc7565.t option) ,avatar) : t') =
  589. (* attach the preferred_username to the inbox *)
  590. let state = state |> to_string in
  591. let t0 = t |> Ptime.to_rfc3339 in
  592. let inbox = inbox |> Uri.to_string in
  593. let name = name |> Option.value ~default:"" in
  594. let avatar = avatar
  595. |> Option.value ~default:Uri.empty
  596. |> Uri.to_string in
  597. let rfc7565 = Option.bind us
  598. (fun l -> Some (l |> Rfc7565.to_string))
  599. |> Option.value ~default:"" in
  600. Csexp.(List [Atom "1"; Atom state; Atom t0; Atom inbox; Atom name; Atom rfc7565; Atom avatar])
  601. let is_accepted = function
  602. | None -> As2.No_p_yes.No
  603. | Some (Accepted,_,_,_,_,_) -> As2.No_p_yes.Yes
  604. | Some (Blocked ,_,_,_,_,_) -> As2.No_p_yes.No
  605. | Some (Pending ,_,_,_,_,_) -> As2.No_p_yes.Pending
  606. let is_blocked = function
  607. | None -> As2.No_p_yes.No
  608. | Some (Accepted,_,_,_,_,_) -> As2.No_p_yes.No
  609. | Some (Blocked ,_,_,_,_,_) -> As2.No_p_yes.Yes
  610. | Some (Pending ,_,_,_,_,_) -> As2.No_p_yes.No
  611. end
  612. let fold_left (fkt : 'a -> (Uri.t * State.t') -> 'a) =
  613. let kv f a (k,v) = f a
  614. (k |> Bytes.to_string |> Uri.of_string
  615. ,v |> Bytes.to_string |> Csexp.parse_string |> State.decode') in
  616. let opt f a = function
  617. | (k,None) -> Logr.warn (fun m -> m "%s.%s ignored actor %a" "Ap.Followers" "fold_left" Uri.pp k);
  618. a
  619. | (k,Some v) -> f a (k,v) in
  620. (* caveat, this folding really looks reverse: *)
  621. fkt |> opt |> kv |> Mcdb.fold_left
  622. let cdb = Mcdb.Cdb "app/var/db/subscribers.cdb"
  623. let find
  624. ?(cdb = cdb)
  625. id : State.t' option =
  626. assert (id |> Uri.user |> Option.is_none);
  627. let ke = id |> Uri.to_string in
  628. Option.bind
  629. (Mcdb.find_string_opt ke cdb)
  630. (fun s -> s |> Csexp.parse_string |> State.decode')
  631. let update ?(cdb = cdb) id v =
  632. assert (id |> Uri.user |> Option.is_none);
  633. Mcdb.update_string (id |> Uri.to_string) (v |> State.encode |> Csexp.to_string) cdb
  634. (** remove from cdb *)
  635. let remove ?(cdb = cdb) id =
  636. assert (id |> Uri.user |> Option.is_none);
  637. Mcdb.remove_string (id |> Uri.to_string) cdb
  638. let is_in_subscribers ?(cdb = cdb) id =
  639. assert (id |> Uri.user |> Option.is_none);
  640. id
  641. |> find ~cdb
  642. |> State.is_accepted
  643. (** https://www.rfc-editor.org/rfc/rfc4287#section-4.1.1 *)
  644. module Atom = struct
  645. (** create all from oldest to newest and return newest file name. *)
  646. let of_cdb
  647. ?(cdb = cdb)
  648. ?(predicate = State.predicate ~invert:false)
  649. ~base
  650. ~title
  651. ~xsl
  652. ~rel
  653. ?(page_size = 50)
  654. dir =
  655. Logr.debug (fun m -> m "%s.%s %s" "Ap.Followers.Atom" "of_cdb" dir);
  656. let predicate (s,_,_,_,_,_ : State.t') = s |> predicate in
  657. (** write one page of a paged xml feed *)
  658. let flush_page_xml ~is_last (u,p,i) =
  659. let _ = is_last
  660. and _ : (Uri.t * State.t') list = u in
  661. assert (0 <= p);
  662. assert (dir |> St.is_suffix ~affix:"/");
  663. let fn = Printf.sprintf "%s%d.xml" dir p in
  664. Logr.debug (fun m -> m "%s.%s %s" "Ap.Followers.Atom" "of_cdb.flush" dir);
  665. assert (u |> List.length = i);
  666. let open Xml in
  667. let mk_rel rel i =
  668. let path,title = match rel with
  669. | Rfc4287.Link.(Rel (Single "first")) ->
  670. assert (i == -1);
  671. ".",Some "last"
  672. | _ ->
  673. assert (i >= 0);
  674. Printf.sprintf "%d.xml" i,
  675. Some (Printf.sprintf "%i" (i+1))
  676. and rel = Some rel in
  677. Rfc4287.Link.(Uri.make ~path () |> make ~rel ~title |> to_atom)
  678. in
  679. let self = mk_rel Rfc4287.Link.self p in
  680. let first = mk_rel Rfc4287.Link.first (-1) in
  681. let last = mk_rel Rfc4287.Link.last 0 in
  682. let prev = mk_rel Rfc4287.Link.prev (succ p) in
  683. let add_next i l = match i with
  684. | 0 -> l
  685. | i -> sep 1 :: mk_rel Rfc4287.Link.next (pred i) :: l in
  686. let id_s = Printf.sprintf "%i.xml" p in
  687. let xml : _ Xmlm.frag =
  688. `El (((ns_a, "feed"), [
  689. ((Xmlm.ns_xmlns, "xmlns"), ns_a);
  690. ((Xmlm.ns_xml, "base"), base |> Uri.to_string);
  691. ]),
  692. sep 1
  693. :: `El (((ns_a,"title"), []), [`Data title]) :: sep 1
  694. :: `El (((ns_a,"id"), []), [`Data id_s ])
  695. :: sep 1 :: self
  696. :: sep 1 :: first
  697. :: sep 1 :: last
  698. :: sep 1 :: prev
  699. :: (u
  700. |> List.rev
  701. |> List.fold_left
  702. (fun init (href,(_,_,_,title,us,_unused_icon)) ->
  703. let href = Uri.with_userinfo href None in
  704. let rfc7565 = Option.bind us
  705. (fun us -> Some (us |> Rfc7565.to_string)) in
  706. sep 1
  707. :: Rfc4287.Link.(make ~rel ~title ~rfc7565 href |> to_atom)
  708. :: init)
  709. [`Data "\n"]
  710. |> add_next p) )
  711. in
  712. fn |> File.out_channel_replace (Xml.to_chan ~xsl xml);
  713. Ok fn in
  714. (** fold a filtered list cdb into paged xml files *)
  715. fold_left (fun (l,p,i as init) (href,st as k) ->
  716. if st |> predicate
  717. then (
  718. Logr.debug (fun m -> m "%s.%s %a" "Ap.Followers.Atom" "of_cdb.fold_left" Uri.pp href);
  719. let i = succ i in
  720. if i > page_size
  721. then
  722. let _ = (l,p,i-1) |> flush_page_xml ~is_last:false in
  723. k :: [],p+1,1
  724. else
  725. k :: l,p,i)
  726. else
  727. init)
  728. ([],0,0) cdb
  729. |> flush_page_xml ~is_last:true
  730. let dir = apub ^ "subscribers/"
  731. let target = dir ^ "index.xml"
  732. let rule : Make.t = {
  733. target;
  734. prerequisites = PersonX.rule.target
  735. :: (cdb |> (fun (Mcdb.Cdb v) -> v))
  736. :: [];
  737. fresh = Make.Outdated;
  738. command = fun _pre _ _ _ ->
  739. let* base = Cfg.Base.(from_file fn) in
  740. of_cdb
  741. ~cdb
  742. ~base
  743. ~title:"📣 Subscribers"
  744. ~xsl:(Rfc4287.xsl "subscribers.xsl" target)
  745. ~rel:(Some Rfc4287.Link.subscribers)
  746. ~page_size:50
  747. dir
  748. }
  749. let make = Make.make [rule]
  750. end
  751. (** https://www.w3.org/TR/activitypub/#followers *)
  752. module Json = struct
  753. let to_page ~is_last (i : int) (fs : Uri.t list) : Uri.t As2_vocab.Types.collection_page =
  754. let p i =
  755. let path = i |> Printf.sprintf "%d.jsa" in
  756. Uri.make ~path () in
  757. let self = p i in
  758. let next = if i > 0
  759. then Some (p (pred i))
  760. else None in
  761. let prev = if not is_last
  762. then Some (p (succ i))
  763. else None in
  764. {
  765. id = self;
  766. current = Some self;
  767. first = None;
  768. is_ordered = true;
  769. items = fs;
  770. last = Some (p 0);
  771. next;
  772. part_of = Some (Uri.make ~path:"index.jsa" ());
  773. prev;
  774. total_items= None;
  775. }
  776. (** write one page of an https://www.w3.org/TR/activitystreams-vocabulary/#dfn-orderedcollection *)
  777. let to_page_json ~base _prefix ~is_last (i : int) (ids : Uri.t list) =
  778. to_page ~is_last i ids
  779. |> As2_vocab.Encode.(collection_page ~base (uri ~base))
  780. (** dehydrate into https://www.w3.org/TR/activitystreams-vocabulary/#dfn-orderedcollection
  781. and https://www.w3.org/TR/activitystreams-vocabulary/#dfn-orderedcollectionpage
  782. dst afterwards contains an
  783. index.jsa
  784. index-0.jsa
  785. ...
  786. index-n.jsa
  787. *)
  788. let flush_page_json ~base ~oc prefix ~is_last (tot,pa,lst,_) =
  789. let fn j = j |> Printf.sprintf "%d.jsa" in
  790. Logr.debug (fun m -> m "%s.%s lst#%d" "Ap.Followers" "flush_page" (lst |> List.length));
  791. let js = lst |> List.rev |> to_page_json ~base prefix ~is_last pa in
  792. (prefix ^ (fn pa)) |> File.out_channel_replace (fun ch -> Ezjsonm.value_to_channel ~minify:false ch js);
  793. (if is_last
  794. then
  795. let p i =
  796. let path = fn i in
  797. Uri.make ~path () in
  798. let c : Uri.t As2_vocab.Types.collection =
  799. { id = Uri.make ~path:"index.jsa" ();
  800. current = None;
  801. first = Some (p pa);
  802. is_ordered = true;
  803. items = Some [];
  804. last = Some (p 0);
  805. total_items = Some tot;
  806. } in
  807. c
  808. |> As2_vocab.Encode.(collection ~base (uri ~base))
  809. |> Ezjsonm.value_to_channel ~minify:false oc)
  810. (** paging logic *)
  811. let fold2pages pagesize flush_page (tot,pa,lst,i) id =
  812. Logr.debug (fun m -> m "%s.%s %a" "Ap.Followers" "fold2pages" Uri.pp id );
  813. if i >= pagesize
  814. then (
  815. flush_page ~is_last:false (tot,pa,lst,i);
  816. (tot |> succ,pa |> succ,id :: [],0)
  817. ) else
  818. (tot |> succ,pa,id :: lst,i |> succ)
  819. (** dehydrate the cdb (e.g. followers list) into the current directory
  820. uses fold2pages & flush_page_json
  821. *)
  822. let coll_of_cdb
  823. ~base
  824. ~oc
  825. ?(pagesize = 100)
  826. ?(predicate = State.predicate ~invert:false)
  827. prefix cdb =
  828. assert (0 < pagesize && pagesize < 10_001);
  829. (* Logr.debug (fun m -> m "%s.%s %d %a" "Ap.Followers" "cdb2coll" pagesize Uri.pp base ); *)
  830. let base = Http.reso ~base (Uri.make ~path:prefix ()) in
  831. let* res = fold_left (fun a (k,(s,_,_,_,_,_)) ->
  832. match a with
  833. | Error _ as e ->
  834. Logr.err (fun m -> m "%s %s.%s foohoo" E.e1008 "Ap.Followers" "coll_of_cdb");
  835. e
  836. | Ok ctx ->
  837. Ok (if s |> predicate
  838. then k |> fold2pages pagesize (flush_page_json ~base ~oc prefix) ctx
  839. else (
  840. Logr.debug (fun m -> m "%s.%s ignored %a" "Ap.Followers" "coll_of_cdb.fold_left" Uri.pp k);
  841. ctx) (* just go on *) )
  842. ) (Ok (0,0,[],0)) cdb in
  843. flush_page_json ~base prefix ~oc ~is_last:true res;
  844. Ok (prefix ^ "index.jsa")
  845. let dir = apub ^ "subscribers/"
  846. let target = dir ^ "index.jsa"
  847. let rule = {Atom.rule
  848. with
  849. target;
  850. prerequisites = Person.rule.target
  851. :: (cdb |> (fun (Mcdb.Cdb v) -> v))
  852. :: [];
  853. command = fun _pre _ _ ->
  854. File.out_channel_replace (fun oc ->
  855. let* base = Cfg.Base.(from_file fn) in
  856. coll_of_cdb ~base ~oc dir cdb)
  857. }
  858. let make = Make.make [rule]
  859. end
  860. let span_follow = 92 * 24 * 60 * 60 |> Ptime.Span.of_int_s
  861. (* notify the follower (uri) and do the local effect *)
  862. let snd_accept
  863. ?(tnow = Ptime_clock.now ())
  864. ~uuid
  865. ~base
  866. ~key
  867. ?(cdb = cdb)
  868. me
  869. (siac : As2_vocab.Types.person)
  870. (fo : As2_vocab.Types.follow) =
  871. Logr.warn(fun m -> m "%s.%s %a %a" "Ap.Followers" "snd_accept" Uri.pp fo.actor Uuidm.pp uuid);
  872. assert (not (me |> Uri.equal fo.actor));
  873. let end_time = Ptime.(span_follow |> add_span tnow) in
  874. assert (fo.actor |> Uri.user |> Option.is_none);
  875. let side_ok _ =
  876. let _ = State.of_actor tnow Accepted siac
  877. |> update ~cdb fo.actor
  878. in
  879. let _ = Make.make [Json.rule] Json.target in
  880. let _ = Atom.(make target) in
  881. () in
  882. match Option.bind
  883. (let ke = fo.actor |> Uri.to_string in
  884. Mcdb.find_string_opt ke cdb)
  885. (fun s -> s |> Csexp.parse_string |> State.decode') with
  886. | None ->
  887. (* Immediately accept *)
  888. let msg = ({
  889. id = fo.id;
  890. actor = me;
  891. obj = fo;
  892. published = Some tnow;
  893. end_time;
  894. } : As2_vocab.Types.follow As2_vocab.Types.accept)
  895. |> As2_vocab.Encode.(accept (follow ~base)) ~base in
  896. send ~key side_ok siac.inbox msg
  897. | Some (Accepted,tnow,_,_,_,_)
  898. | Some (Pending,tnow,_,_,_,_) ->
  899. let msg = ({
  900. id = fo.id;
  901. actor = me;
  902. obj = fo;
  903. published = Some tnow;
  904. end_time;
  905. } : As2_vocab.Types.follow As2_vocab.Types.accept)
  906. |> As2_vocab.Encode.(accept (follow ~base)) ~base in
  907. send ~key side_ok siac.inbox msg
  908. | Some (Blocked,_,_tnow,_,_,_) -> Lwt.return Http.s403
  909. (* do the local effect *)
  910. let snd_accept_undo
  911. ?(tnow = Ptime_clock.now ())
  912. ?(cdb = cdb)
  913. ~uuid
  914. ~base
  915. ~key
  916. me
  917. (siac : As2_vocab.Types.person)
  918. (ufo : As2_vocab.Types.follow As2_vocab.Types.undo) =
  919. Logr.warn(fun m -> m "%s.%s %a %a" "Ap.Follower" "snd_accept_undo" Uri.pp ufo.obj.actor Uuidm.pp uuid);
  920. assert (not (me |> Uri.equal ufo.actor));
  921. assert (ufo.actor |> Uri.equal ufo.obj.actor );
  922. assert (ufo.actor |> Uri.equal siac.id);
  923. let _ = remove ~cdb ufo.actor in
  924. let _ = Json.(make target) in
  925. let _ = Atom.(make target) in
  926. let side_ok _ = () (* noop *) in
  927. ({
  928. id = ufo.id;
  929. actor = me;
  930. obj = ufo;
  931. published = Some tnow;
  932. end_time = None;
  933. } : As2_vocab.Types.follow As2_vocab.Types.undo As2_vocab.Types.accept)
  934. |> As2_vocab.Encode.(accept ~base (undo ~base (follow ~base)))
  935. |> send ~key side_ok siac.inbox
  936. end
  937. (** Logic for https://www.w3.org/TR/activitypub/#following *)
  938. module Following = struct
  939. let n = "subscribed_to"
  940. let cdb = Mcdb.Cdb ("app/var/db/" ^ n ^ ".cdb")
  941. let find ?(cdb = cdb) = Followers.find ~cdb
  942. let remove ?(cdb = cdb) = Followers.remove ~cdb
  943. let update ?(cdb = cdb) = Followers.update ~cdb
  944. (** lists whom I subscribed to *)
  945. module Subscribed_to = struct
  946. let dir = apub ^ n ^ "/"
  947. (** Mostly delegates to Followers.Atom.of_cdb *)
  948. module Atom = struct
  949. let target = dir ^ "index.xml"
  950. let rule : Make.t = {
  951. target;
  952. prerequisites = PersonX.rule.target
  953. :: (cdb |> (fun (Mcdb.Cdb v) -> v))
  954. :: [];
  955. fresh = Make.Outdated;
  956. command = fun _pre _ _ _ ->
  957. let* base = Cfg.Base.(from_file fn) in
  958. Followers.Atom.of_cdb
  959. ~cdb
  960. ~base
  961. ~title:"👂 Subscribed to"
  962. ~xsl:(Rfc4287.xsl "subscribed_to.xsl" target)
  963. ~rel:(Some Rfc4287.Link.subscribed_to)
  964. ~page_size:50 dir
  965. }
  966. end
  967. (** Mostly delegates to Followers.Json.coll_of_cdb *)
  968. module Json = struct
  969. let target = dir ^ "index.jsa"
  970. let rule : Make.t = {
  971. target;
  972. prerequisites = Person.rule.target
  973. :: (cdb |> (fun (Mcdb.Cdb v) -> v))
  974. :: [];
  975. fresh = Make.Outdated;
  976. command = fun _pre _ _ ->
  977. File.out_channel_replace (fun oc ->
  978. let* base = Cfg.Base.(from_file fn) in
  979. Followers.Json.coll_of_cdb ~base ~oc dir cdb)
  980. }
  981. end
  982. end
  983. let am_subscribed_to ?(cdb = cdb) id =
  984. assert (id |> Uri.user |> Option.is_none);
  985. id
  986. |> find ~cdb
  987. |> Followers.State.is_accepted
  988. (** lists whom I block *)
  989. module Blocked = struct
  990. let dir = apub ^ "blocked" ^ "/"
  991. (** Mostly delegates to Followers.Atom.of_cdb *)
  992. module Atom = struct
  993. let target = dir ^ "index.xml"
  994. let rule : Make.t = {
  995. target;
  996. prerequisites = PersonX.rule.target
  997. :: (cdb |> (fun (Mcdb.Cdb v) -> v))
  998. :: [];
  999. fresh = Make.Outdated;
  1000. command = fun _pre _ _ _ ->
  1001. let* base = Cfg.Base.(from_file fn) in
  1002. Followers.Atom.of_cdb
  1003. ~cdb
  1004. ~predicate:Followers.State.(predicate ~invert:true)
  1005. ~base
  1006. ~title:"🤐 Blocked"
  1007. ~xsl:(Rfc4287.xsl "blocked.xsl" target)
  1008. ~rel:(Some Rfc4287.Link.blocked)
  1009. ~page_size:50 dir
  1010. }
  1011. end
  1012. (** Mostly delegates to Followers.Json.coll_of_cdb *)
  1013. module Json = struct
  1014. let target = dir ^ "index.jsa"
  1015. let rule : Make.t = {
  1016. target;
  1017. prerequisites = Person.rule.target
  1018. :: (cdb |> (fun (Mcdb.Cdb v) -> v))
  1019. :: [];
  1020. fresh = Make.Outdated;
  1021. command = fun _pre _ _ ->
  1022. File.out_channel_replace (fun oc ->
  1023. let* base = Cfg.Base.(from_file fn) in
  1024. Followers.Json.coll_of_cdb
  1025. ~predicate:Followers.State.(predicate ~invert:true)
  1026. ~base ~oc dir cdb)
  1027. }
  1028. end
  1029. end
  1030. let is_blocked ?(cdb = cdb) id =
  1031. assert (id |> Uri.user |> Option.is_none);
  1032. id
  1033. |> find ~cdb
  1034. |> Followers.State.is_blocked
  1035. let make ?(tnow = Ptime_clock.now ()) ~me ~inbox reac : As2_vocab.Activitypub.Types.follow =
  1036. assert (not (me |> Uri.equal reac));
  1037. let _ = inbox
  1038. and end_time = Ptime.(Followers.span_follow |> add_span tnow) in
  1039. {
  1040. id = Uri.with_fragment me (Some "subscribe");
  1041. actor = me;
  1042. cc = [];
  1043. end_time;
  1044. object_ = reac;
  1045. state = None;
  1046. to_ = [];
  1047. }
  1048. let undo ~me (o : As2_vocab.Types.follow) : As2_vocab.Types.follow As2_vocab.Types.undo =
  1049. assert (not (me |> Uri.equal o.object_));
  1050. assert (me |> Uri.equal o.actor );
  1051. {
  1052. id = Uri.with_fragment o.id (Some "subscribe#undo");
  1053. actor = me;
  1054. obj = o;
  1055. published= None;
  1056. }
  1057. let rcv_accept
  1058. ?(tnow = Ptime_clock.now ())
  1059. ?(subscribed_to = cdb)
  1060. ~uuid
  1061. ~base
  1062. me
  1063. (siac : As2_vocab.Types.person)
  1064. (fo : As2_vocab.Types.follow) =
  1065. Logr.debug (fun m -> m "%s.%s %a %a" "Ap.Following" "rcv_accept" Uuidm.pp uuid Uri.pp fo.object_);
  1066. assert (siac.id |> Uri.equal fo.object_);
  1067. assert (not (me |> Uri.equal siac.id));
  1068. (* assert (me |> Uri.equal fo.actor);
  1069. assert (not (fo.actor |> Uri.equal fo.object_)); *)
  1070. Logr.warn (fun m -> m "%s.%s TODO only take those that I expect" "Ap.Following" "accept");
  1071. let _ = fo.end_time in
  1072. let _ = base in
  1073. let _ = Followers.State.(of_actor tnow Accepted siac)
  1074. |> update ~cdb:subscribed_to siac.id in
  1075. let _ = Subscribed_to.Json.(Make.make [rule] target) in
  1076. let _ = Subscribed_to.Atom.(Make.make [rule] target) in
  1077. Ok (`Created, [Http.H.ct_plain], Cgi.Response.body "created")
  1078. |> Lwt.return
  1079. end
  1080. let rcv_reject
  1081. ?(tnow = Ptime_clock.now ())
  1082. ~uuid
  1083. ~base
  1084. (siac : As2_vocab.Types.person)
  1085. o =
  1086. Logr.debug (fun m -> m "%s.%s %a %a" "Ap" "rcv_reject" Uri.pp siac.id Uuidm.pp uuid);
  1087. let _ = tnow
  1088. and _ = base
  1089. in
  1090. (match o with
  1091. | `Follow (fo : As2_vocab.Types.follow) ->
  1092. Logr.info (fun m -> m "%s.%s Follow request rejected by %a" "Ap" "rcv_reject" Uri.pp fo.object_);
  1093. let _ = Following.remove fo.object_ in
  1094. let _ = Following.Subscribed_to.Json.(Make.make [rule] target) in
  1095. let _ = Following.Subscribed_to.Atom.(Make.make [rule] target) in
  1096. (* @TODO: add a notification to the timeline? *)
  1097. Ok (`OK, [Http.H.ct_plain], Cgi.Response.body "ok")
  1098. | _ ->
  1099. Logr.err (fun m -> m "%s.%s TODO" "Ap" "rcv_reject");
  1100. Http.s501)
  1101. |> Lwt.return
  1102. module Note = struct
  1103. let empty = ({
  1104. id = Uri.empty;
  1105. agent = None;
  1106. attachment = [];
  1107. attributed_to = Uri.empty;
  1108. cc = [];
  1109. content_map = [];
  1110. in_reply_to = [];
  1111. reaction_inbox = None;
  1112. media_type = (Some Http.Mime.text_html); (* https://www.w3.org/TR/activitystreams-vocabulary/#dfn-mediatype *)
  1113. published = None;
  1114. sensitive = false;
  1115. source = None;
  1116. summary_map = [];
  1117. tags = [];
  1118. to_ = [];
  1119. url = [];
  1120. } : As2_vocab.Types.note)
  1121. let actor_from_author _author =
  1122. Uri.make ~path:proj ()
  1123. let followers actor =
  1124. Uri.make ~path:"subscribers/index.jsa" () |> Http.reso ~base:actor
  1125. let of_rfc4287
  1126. ?(to_ = [As2_vocab.Constants.ActivityStreams.public])
  1127. (e : Rfc4287.Entry.t)
  1128. : As2_vocab.Types.note =
  1129. Logr.debug (fun m -> m "%s.%s %a" "Ap.Note" "of_rfc4287" Uri.pp e.id);
  1130. let tag init (lbl,term,base) =
  1131. let ty = `Hashtag in
  1132. let open Rfc4287.Category in
  1133. let Label (Single name) = lbl
  1134. and Term (Single term) = term in
  1135. let path = term ^ "/" in
  1136. let href = Uri.make ~path () |> Http.reso ~base in
  1137. let ta : As2_vocab.Types.tag = {ty; name; href} in
  1138. ta :: init
  1139. in
  1140. let id = e.id in
  1141. let actor = actor_from_author e.author in
  1142. let cc = [actor |> followers] in
  1143. let Rfc3339.T published = e.published in
  1144. let published = match published |> Ptime.of_rfc3339 with
  1145. | Ok (t,_,_) -> Some t
  1146. | _ -> None in
  1147. let tags = e.categories |> List.fold_left tag [] in
  1148. let Rfc4287.Rfc4646 lang = e.lang in
  1149. let summary_map = [lang,e.title] in
  1150. let content_map = [lang,e.content] in
  1151. let url = e.links |> List.fold_left (
  1152. (* sift, use those without a rel *)
  1153. fun i (l : Rfc4287.Link.t) ->
  1154. match l.rel with
  1155. | None -> l.href :: i
  1156. | Some _ -> i) [] in
  1157. {empty with
  1158. id;
  1159. content_map;
  1160. attributed_to = actor;
  1161. cc;
  1162. media_type = Some Http.Mime.text_plain;
  1163. published;
  1164. summary_map;
  1165. tags;
  1166. to_;
  1167. url;
  1168. }
  1169. let to_rfc4287 ~tz ~now (n : As2_vocab.Types.note) : Rfc4287.Entry.t =
  1170. let _ = tz
  1171. and _ = now in
  1172. Logr.debug (fun m -> m "%s.%s %a" "Ap.Note" "to_rfc4287" Uri.pp n.id);
  1173. let published = n.published |> Option.value ~default:now |> Rfc3339.of_ptime ~tz
  1174. and author = {Rfc4287.Person.empty with
  1175. name = (match n.attributed_to |> Uri.user with
  1176. | None -> n.attributed_to |> Uri.to_string
  1177. | Some u -> u );
  1178. uri = Some n.attributed_to} in
  1179. let a (s,_,_) = s in
  1180. let (lang,cont) = n.content_map |> List.hd in
  1181. let sum = try let _,s = n.summary_map |> List.hd in
  1182. Some s
  1183. with Failure _ -> None in
  1184. let links = match n.reaction_inbox with
  1185. | None -> []
  1186. | Some ib -> [Rfc4287.Link.(make ~rel:(Some inbox) ib )]
  1187. in
  1188. {Rfc4287.Entry.empty with
  1189. id = n.id;
  1190. author;
  1191. lang = Rfc4287.Rfc4646 lang;
  1192. title = sum |> Option.value ~default:"" |> Html.to_plain |> a;
  1193. content = cont |> Html.to_plain |> a;
  1194. published;
  1195. links;
  1196. updated = published;
  1197. in_reply_to = n.in_reply_to |> List.map Rfc4287.Inreplyto.make;
  1198. }
  1199. (** Not implemented yet *)
  1200. let plain_to_html s : string =
  1201. (* care about :
  1202. * - newlines
  1203. * - urls
  1204. * - tags
  1205. * - mentions
  1206. *)
  1207. s
  1208. let html_to_plain _s =
  1209. failwith "not implemented yet."
  1210. let sensitive_marker = "⚠️"
  1211. (** Turn text/plain to text/html, add set id as self url
  1212. Mastodon interprets summary as content warning indicator. . *)
  1213. let diluviate (n : As2_vocab.Types.note) =
  1214. let sensitive,summary_map = n.summary_map |> List.fold_left (fun (sen,suma) (l,txt) ->
  1215. let sen = sen || (txt |> Astring.String.is_prefix ~affix:sensitive_marker) in
  1216. let html = txt |> plain_to_html in
  1217. sen,(l,html) :: suma)
  1218. (n.sensitive,[]) in
  1219. (* add all urls before the content (in each language) *)
  1220. let ur = n.url |> List.fold_left (fun i u ->
  1221. let s = u |> Uri.to_string in
  1222. Printf.sprintf "%s<a href='%s'>%s</a><br/>\n" i s s) "" in
  1223. let content_map = n.content_map |> List.fold_left (fun init (l,co) ->
  1224. (* if not warning, fetch summary of content language *)
  1225. let su = match sensitive with
  1226. | true -> ""
  1227. | false -> match summary_map |> List.assoc_opt l with
  1228. | None -> ""
  1229. | Some su -> su ^ "<br/>\n" in
  1230. let txt = su
  1231. ^ ur
  1232. ^ (if su |> String.equal "" && ur |> String.equal ""
  1233. then ""
  1234. else "<br/>\n")
  1235. ^ (co |> plain_to_html) in
  1236. (l,txt) :: init) []
  1237. in
  1238. {n with
  1239. content_map;
  1240. sensitive;
  1241. summary_map = if sensitive then summary_map else [];
  1242. url = [n.id] }
  1243. (** https://www.w3.org/TR/activitypub/#create-activity-outbox *)
  1244. module Create = struct
  1245. let make (obj : As2_vocab.Types.note) : As2_vocab.Types.note As2_vocab.Types.create =
  1246. let frag = match obj.id |> Uri.fragment with
  1247. | None -> Some "Create"
  1248. | Some f -> Some (f ^ "/Create") in
  1249. {
  1250. id = frag |> Uri.with_fragment obj.id;
  1251. actor = obj.attributed_to;
  1252. published = obj.published;
  1253. to_ = obj.to_;
  1254. cc = obj.cc;
  1255. direct_message = false;
  1256. obj = obj; (* {obj with to_ = []; cc = []}; *)
  1257. }
  1258. (** turn an Atom entry into an ActivityPub (Mastodon) Create Note activity. *)
  1259. let to_json ~base n =
  1260. let lang = As2_vocab.Constants.ActivityStreams.und in
  1261. n
  1262. |> of_rfc4287
  1263. |> diluviate
  1264. |> make
  1265. |> As2_vocab.Encode.(create ~base ~lang (note ~base))
  1266. end
  1267. (** Rather use a tombstone? https://www.w3.org/TR/activitypub/#delete-activity-outbox *)
  1268. module Delete = struct
  1269. let make (obj : As2_vocab.Types.note) : As2_vocab.Types.note As2_vocab.Types.delete =
  1270. let frag = match obj.id |> Uri.fragment with
  1271. | None -> Some "Delete"
  1272. | Some f -> Some (f ^ "/Delete") in
  1273. {
  1274. id = frag |> Uri.with_fragment obj.id;
  1275. actor = obj.attributed_to;
  1276. published = obj.published; (* rather use tnow *)
  1277. obj = obj;
  1278. }
  1279. let to_json ~base n =
  1280. n
  1281. |> of_rfc4287
  1282. |> make
  1283. |> As2_vocab.Encode.(delete ~base (note ~base))
  1284. end
  1285. let _5381_63 = 5381 |> Optint.Int63.of_int
  1286. (* http://cr.yp.to/cdb/cdb.txt *)
  1287. let hash63_gen len f_get : Optint.Int63.t =
  1288. let mask = Optint.Int63.max_int
  1289. and ( +. ) = Optint.Int63.add
  1290. and ( << ) = Optint.Int63.shift_left
  1291. and ( ^ ) = Optint.Int63.logxor
  1292. and ( land ) = Optint.Int63.logand in
  1293. let rec fkt (idx : int) (h : Optint.Int63.t) =
  1294. if idx = len
  1295. then h
  1296. else
  1297. let c = idx |> f_get |> Char.code |> Optint.Int63.of_int in
  1298. (((h << 5) +. h) ^ c) land mask
  1299. |> fkt (succ idx)
  1300. in
  1301. fkt 0 _5381_63
  1302. let hash63_str dat : Optint.Int63.t =
  1303. hash63_gen (String.length dat) (String.get dat)
  1304. let uhash ?(off = 0) ?(buf = Bytes.make (Optint.Int63.encoded_size) (Char.chr 0)) u =
  1305. u
  1306. |> Uri.to_string
  1307. |> hash63_str
  1308. |> Optint.Int63.encode buf ~off;
  1309. buf
  1310. |> Bytes.to_string
  1311. |> Base64.encode_string ~pad:false ~alphabet:Base64.uri_safe_alphabet
  1312. let ibc_dir = "app/var/cache/inbox/"
  1313. (** not just Note *)
  1314. let to_file ~msg_id ~prefix ~dir json =
  1315. let fn = msg_id
  1316. |> uhash
  1317. |> Printf.sprintf "%s%s.json" prefix in
  1318. let tmp = dir ^ "tmp/" ^ fn in
  1319. (dir ^ "new/" ^ fn) |> File.out_channel_create ~tmp
  1320. (fun oc ->
  1321. json
  1322. |> Ezjsonm.value_to_channel oc)
  1323. let do_cache
  1324. ?(tnow = Ptime_clock.now ())
  1325. ?(dir = ibc_dir)
  1326. ~(base : Uri.t)
  1327. (a : As2_vocab.Types.note As2_vocab.Types.create) =
  1328. let _ = tnow in
  1329. Logr.debug (fun m -> m "%s.%s TODO %a" "Ap.Note" "do_cache" Uri.pp a.id);
  1330. assert (a.actor |> Uri.user |> Option.is_some);
  1331. assert (a.obj.attributed_to |> Uri.user |> Option.is_some);
  1332. a
  1333. |> As2_vocab.Encode.(create ~base (note ~base))
  1334. |> to_file ~msg_id:a.id ~prefix:"note-" ~dir
  1335. let do_cache'
  1336. ?(tnow = Ptime_clock.now ())
  1337. ?(dir = ibc_dir)
  1338. ~(base : Uri.t)
  1339. (a : As2_vocab.Types.note As2_vocab.Types.update) =
  1340. let _ = tnow in
  1341. Logr.debug (fun m -> m "%s.%s TODO %a" "Ap.Note" "do_cache'" Uri.pp a.id);
  1342. assert (a.actor |> Uri.user |> Option.is_some);
  1343. assert (a.obj.attributed_to |> Uri.user |> Option.is_some);
  1344. a
  1345. |> As2_vocab.Encode.(update ~base (note ~base))
  1346. |> to_file ~msg_id:a.id ~prefix:"note-" ~dir
  1347. let rcv_create
  1348. ?(tnow = Ptime_clock.now ())
  1349. ~uuid
  1350. ~(base : Uri.t)
  1351. (siac : As2_vocab.Types.person)
  1352. (a : As2_vocab.Types.note As2_vocab.Types.create) : Cgi.Response.t' Lwt.t =
  1353. Logr.debug (fun m -> m "%s.%s %a %a" "Ap.Note" "rcv_create" Uri.pp a.obj.attributed_to Uuidm.pp uuid);
  1354. assert (a.actor |> Uri.equal siac.id);
  1355. assert (a.actor |> Uri.equal a.obj.attributed_to);
  1356. let actor = siac.preferred_username |> Uri.with_userinfo a.actor in
  1357. let attributed_to = siac.preferred_username |> Uri.with_userinfo a.obj.attributed_to in
  1358. let a = {a with actor} in
  1359. let a = {a with obj = {a.obj with attributed_to}} in
  1360. let _ = do_cache ~tnow ~base a in
  1361. Ok (`Created, [Http.H.ct_plain], Cgi.Response.body "create")
  1362. |> Lwt.return
  1363. let rcv_update
  1364. ?(tnow = Ptime_clock.now ())
  1365. ~uuid
  1366. ~(base : Uri.t)
  1367. (siac : As2_vocab.Types.person)
  1368. (a : As2_vocab.Types.note As2_vocab.Types.update) : Cgi.Response.t' Lwt.t =
  1369. Logr.debug (fun m -> m "%s.%s %a %a" "Ap.Note" "rcv_update" Uri.pp a.obj.attributed_to Uuidm.pp uuid);
  1370. assert (a.actor |> Uri.equal siac.id);
  1371. assert (a.actor |> Uri.equal a.obj.attributed_to);
  1372. let actor = siac.preferred_username |> Uri.with_userinfo a.actor in
  1373. let attributed_to = siac.preferred_username |> Uri.with_userinfo a.obj.attributed_to in
  1374. let a = {a with actor} in
  1375. let a = {a with obj = {a.obj with attributed_to}} in
  1376. let _ = do_cache' ~tnow ~base a in
  1377. Ok (`Created, [Http.H.ct_plain], Cgi.Response.body "update")
  1378. |> Lwt.return
  1379. end
  1380. module Like = struct
  1381. let do_cache
  1382. ?(tnow = Ptime_clock.now ())
  1383. ?(dir = Note.ibc_dir)
  1384. ~(base : Uri.t)
  1385. (a : As2_vocab.Types.like) =
  1386. let _ = tnow in
  1387. Logr.debug (fun m -> m "%s.%s TODO %a" "Ap.Like" "do_cache" Uri.pp a.id);
  1388. assert (a.actor |> Uri.user |> Option.is_some);
  1389. a
  1390. |> As2_vocab.Encode.like ~base
  1391. |> Note.to_file ~msg_id:a.id ~prefix:"like-" ~dir
  1392. let do_cache'
  1393. ?(tnow = Ptime_clock.now ())
  1394. ?(dir = Note.ibc_dir)
  1395. ~(base : Uri.t)
  1396. (a : As2_vocab.Types.like As2_vocab.Types.undo) =
  1397. let _ = tnow in
  1398. Logr.debug (fun m -> m "%s.%s TODO %a" "Ap.Like" "do_cache'" Uri.pp a.id);
  1399. assert (a.actor |> Uri.user |> Option.is_some);
  1400. a
  1401. |> As2_vocab.Encode.(undo ~base (like ~base))
  1402. |> Note.to_file ~msg_id:a.id ~prefix:"like-" ~dir
  1403. let rcv_like
  1404. ?(tnow = Ptime_clock.now ())
  1405. ~uuid
  1406. ~(base : Uri.t)
  1407. (siac : As2_vocab.Types.person)
  1408. (a : As2_vocab.Types.like) : Cgi.Response.t' Lwt.t =
  1409. Logr.debug (fun m -> m "%s.%s %a %a" "Ap.Like" "rcv_like" Uri.pp a.actor Uuidm.pp uuid);
  1410. assert (a.actor |> Uri.equal siac.id);
  1411. let actor = Uri.with_userinfo a.actor siac.preferred_username in
  1412. let a = {a with actor} in
  1413. let _ = do_cache ~tnow ~base a in
  1414. Ok (`Created, [Http.H.ct_plain], Cgi.Response.body "like")
  1415. |> Lwt.return
  1416. let rcv_like_undo
  1417. ?(tnow = Ptime_clock.now ())
  1418. ~uuid
  1419. ~(base : Uri.t)
  1420. (siac : As2_vocab.Types.person)
  1421. (a : As2_vocab.Types.like As2_vocab.Types.undo) : Cgi.Response.t' Lwt.t =
  1422. Logr.debug (fun m -> m "%s.%s %a %a" "Ap.Like" "rcv_like_undo" Uri.pp a.actor Uuidm.pp uuid);
  1423. assert (a.actor |> Uri.equal siac.id);
  1424. let actor = Uri.with_userinfo a.actor siac.preferred_username in
  1425. let a = {a with actor} in
  1426. let _ = do_cache' ~tnow ~base a in
  1427. Ok (`Created, [Http.H.ct_plain], Cgi.Response.body "like")
  1428. |> Lwt.return
  1429. end
  1430. module Announce = struct
  1431. let do_cache
  1432. ?(tnow = Ptime_clock.now ())
  1433. ?(dir = Note.ibc_dir)
  1434. ~base
  1435. (a : As2_vocab.Types.announce) =
  1436. let _ = tnow in
  1437. Logr.debug (fun m -> m "%s.%s TODO %a" "Ap.Announce" "do_cache" Uri.pp a.id);
  1438. assert (a.actor |> Uri.user |> Option.is_some);
  1439. a
  1440. |> As2_vocab.Encode.announce ~base
  1441. |> Note.to_file ~msg_id:a.id ~prefix:"anno-" ~dir
  1442. let do_cache'
  1443. ?(tnow = Ptime_clock.now ())
  1444. ?(dir = Note.ibc_dir)
  1445. ~base
  1446. (a : As2_vocab.Types.announce As2_vocab.Types.undo) =
  1447. let _ = tnow in
  1448. Logr.debug (fun m -> m "%s.%s TODO %a" "Ap.Announce" "do_cache'" Uri.pp a.id);
  1449. assert (a.actor |> Uri.user |> Option.is_some);
  1450. a
  1451. |> As2_vocab.Encode.(undo ~base (announce ~base))
  1452. |> Note.to_file ~msg_id:a.id ~prefix:"anno-" ~dir
  1453. let rcv_announce
  1454. ?(tnow = Ptime_clock.now ())
  1455. ~uuid
  1456. ~base
  1457. (siac : As2_vocab.Types.person)
  1458. (a : As2_vocab.Types.announce) : Cgi.Response.t' Lwt.t =
  1459. Logr.debug (fun m -> m "%s.%s %a %a" "Ap.Announce" "rcv_announce" Uri.pp a.actor Uuidm.pp uuid);
  1460. assert (a.actor |> Uri.equal siac.id);
  1461. let actor = Uri.with_userinfo a.actor siac.preferred_username in
  1462. {a with actor} |> do_cache ~tnow ~base;
  1463. Ok (`Created, [Http.H.ct_plain], Cgi.Response.body "announce")
  1464. |> Lwt.return
  1465. let rcv_announce_undo
  1466. ?(tnow = Ptime_clock.now ())
  1467. ~uuid
  1468. ~(base : Uri.t)
  1469. (siac : As2_vocab.Types.person)
  1470. (a : As2_vocab.Types.announce As2_vocab.Types.undo) : Cgi.Response.t' Lwt.t =
  1471. Logr.debug (fun m -> m "%s.%s %a %a" "Ap.Announce" "rcv_announce_undo" Uri.pp a.actor Uuidm.pp uuid);
  1472. assert (a.actor |> Uri.equal siac.id);
  1473. let actor = Uri.with_userinfo a.actor siac.preferred_username in
  1474. {a with actor} |> do_cache' ~tnow ~base;
  1475. Ok (`Created, [Http.H.ct_plain], Cgi.Response.body "announce")
  1476. |> Lwt.return
  1477. end