From cc783c157f31e7e713c8b83be67449b1859dac27 Mon Sep 17 00:00:00 2001 From: Marc Coquand Date: Wed, 20 Dec 2023 20:43:25 -0600 Subject: Initial commit --- lib/activitypub/common.ml | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 lib/activitypub/common.ml (limited to 'lib/activitypub/common.ml') diff --git a/lib/activitypub/common.ml b/lib/activitypub/common.ml new file mode 100644 index 0000000..59c7099 --- /dev/null +++ b/lib/activitypub/common.ml @@ -0,0 +1,47 @@ +module D = Decoders_yojson.Safe.Decode + +let timestamp = + let open D in + let* time = string in + match Ptime.of_rfc3339 time |> Ptime.rfc3339_error_to_msg with + | Ok (t, _, _) -> succeed t + | Error `Msg err -> fail err + +let singleton_or_list dec = + D.(one_of ["singleton", (dec >|= fun v -> [v]); + "list", list dec; + "null", null >|= fun () -> []]) + +let lossy_list_of dec = + let open D in + list (one_of ["known", (dec >|= fun v -> `Value v); "unknown", value >|= fun v -> `Raw v]) + + +let constant ?msg target = + let open D in + let* str = string in + if String.equal str target + then succeed () + else match msg with + | None -> fail (Printf.sprintf "expected %s received %s" target str) + | Some msg -> fail (Printf.sprintf msg str) + +let field_or_default field' decoder default = + let open D in + let+ field = field_opt field' decoder in + Option.value ~default field + +let list_ignoring_unknown ty = + let open D in + list (maybe ty) >|= fun v -> List.filter_map Fun.id v + +let items obj = + let open D in + one_of [ + ("ordered items", + let* items = field "orderedItems" (list obj) in + succeed (true, items)); + "items", + let* items = field "items" (list obj) in + succeed (false, items) + ] -- cgit v1.2.3