Something went wrong. Try again.
Correctness- and interoperability-oriented Tangled CLI
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415open Camlet_domain
let ( let* ) = Result.bind
let protocol_error ~code ~message = App_error.make ~category:Protocol ~code ~message
let consistency_error ~code ~message = App_error.make ~category:Consistency ~code ~message
let authority = "The remote service"let parse_json ~code = Json_fields.parse ~authority ~codelet object_fields ~code = Json_fields.object_fields ~authority ~codelet required_field ~code = Json_fields.required_field ~authority ~codelet optional_field ~code = Json_fields.optional_field ~authority ~codelet required_string ~code = Json_fields.required_string ~authority ~codelet optional_string ~code = Json_fields.optional_string ~authority ~code
let at_uri ~code ~what value = match At_uri.of_string value with | Ok value -> Ok value | Error _ -> Error (protocol_error ~code ~message: ("The remote service returned an invalid " ^ what ^ " AT URI"))
let did ~code ~what value = match Did.of_string value with | Ok value -> Ok value | Error _ -> Error (protocol_error ~code ~message: ("The remote service returned an unsupported " ^ what ^ " DID"))
let generic_did ~code ~what value = if not (String.starts_with ~prefix:"did:" value) then Error (protocol_error ~code ~message:("The remote service returned an invalid " ^ what ^ " DID")) else match At_uri.of_string ("at://" ^ value) with | Ok uri when String.equal (At_uri.authority uri) value -> Ok value | Ok _ | Error _ -> Error (protocol_error ~code ~message:("The remote service returned an invalid " ^ what ^ " DID"))
let repository_did ~code value = match Repository_did.of_string value with | Ok value -> Ok value | Error _ -> Error (protocol_error ~code ~message:"The issue record contains an invalid repository DID")
let cid ~code ~what value = match Cid.of_string value with | Ok value -> Ok value | Error _ -> Error (protocol_error ~code ~message:("The remote service returned an invalid " ^ what ^ " CID"))
let optional_cid ~code ~what fields = let* value = optional_string ~code "cid" fields in match value with | None -> Ok None | Some value -> Result.map Option.some (cid ~code ~what value)
let timestamp ~code ~what value = match Timestamp.of_string value with | Ok value -> Ok value | Error _ -> Error (protocol_error ~code ~message: ("The remote service returned an invalid " ^ what ^ " timestamp"))
let record_identity ~code ~collection fields = let* uri_text = required_string ~code "uri" fields in let* uri = at_uri ~code ~what:"record" uri_text in match (At_uri.collection uri, At_uri.record_key uri) with | Some returned, Some record_key when String.equal returned collection -> let* author_did = did ~code ~what:"record-author" (At_uri.authority uri) in Ok (uri, record_key, author_did) | _ -> Error (protocol_error ~code ~message: "The remote service returned a record in the wrong collection")
let record_type ?(allow_duplicate_identical = false) ~code ~expected fields = let values = List.filter_map (fun (name, value) -> if String.equal name "$type" then Some value else None) fields in match values with | [ `String value ] when String.equal value expected -> Ok () | [ `String first; `String second ] when allow_duplicate_identical && String.equal first expected && String.equal second expected -> Ok () | [] -> Error (protocol_error ~code ~message: "The remote service response is missing required field $type") | _ -> Error (protocol_error ~code ~message:"The remote service returned the wrong embedded record type")
let bounded_text ~code ~what ~minimum ~maximum value = let length = String.length value in if length >= minimum && length <= maximum && Json_fields.valid_utf8 value then Ok value else Error (protocol_error ~code ~message:("The remote " ^ what ^ " exceeds Camlet's supported bounds"))
let decode_array ~code ~name ~maximum ~decode fields = let* value = optional_field ~code name fields in match value with | None -> Ok [] | Some (`List values) when List.length values <= maximum -> let rec loop reversed = function | [] -> Ok (List.rev reversed) | value :: rest -> let* value = decode value in loop (value :: reversed) rest in loop [] values | Some (`List _) -> Error (protocol_error ~code ~message: ("The remote " ^ name ^ " array exceeds Camlet's supported bounds")) | Some _ -> Error (protocol_error ~code ~message:("The remote " ^ name ^ " field must be an array"))
let int64 ~code ~what = function | `Int value -> Ok (Int64.of_int value) | `Intlit value -> ( match Int64.of_string_opt value with | Some value -> Ok value | None -> Error (protocol_error ~code ~message:(what ^ " must be an integer"))) | _ -> Error (protocol_error ~code ~message:(what ^ " must be an integer"))
let blob ~code json = let* fields = object_fields ~code json in let* () = record_type ~code ~expected:"blob" fields in let* reference = required_field ~code "ref" fields in let* reference_fields = object_fields ~code reference in let* cid_text = required_string ~code "$link" reference_fields in let* cid = cid ~code ~what:"blob" cid_text in let* mime_type = required_string ~code "mimeType" fields in let* size_json = required_field ~code "size" fields in let* size = int64 ~code ~what:"The remote blob size" size_json in if (not (String.starts_with ~prefix:"image/" mime_type)) || String.length mime_type > 255 || Int64.compare size 0L < 0 || Int64.compare size 1_000_000L > 0 then Error (protocol_error ~code ~message:"The remote image blob violates the supported profile") else Ok Issue_view.{ cid; mime_type; size }
let string_array ~code ~name ~maximum ~decode fields = decode_array ~code ~name ~maximum ~decode:(function | `String value -> decode value | _ -> Error (protocol_error ~code ~message: ("The remote " ^ name ^ " array contains a non-string value"))) fields
let decode_issue_json ~code ~requested json = let* fields = object_fields ~code json in let* uri, record_key, author_did = record_identity ~code ~collection:"sh.tangled.repo.issue" fields in let* cid = optional_cid ~code ~what:"issue" fields in let* value = required_field ~code "value" fields in let* value_fields = object_fields ~code value in let* () = record_type ~code ~expected:"sh.tangled.repo.issue" value_fields in let* repo_text = required_string ~code "repo" value_fields in let* repository_did = repository_did ~code repo_text in let* title = required_string ~code "title" value_fields in let* title = bounded_text ~code ~what:"issue title" ~minimum:1 ~maximum:(16 * 1024) title in let* body = optional_string ~code "body" value_fields in let* body = match body with | None -> Ok None | Some value -> Result.map Option.some (bounded_text ~code ~what:"issue body" ~minimum:0 ~maximum:(1024 * 1024) value) in let* created_text = required_string ~code "createdAt" value_fields in let* created_at = timestamp ~code ~what:"issue creation" created_text in let* mentions = string_array ~code ~name:"mentions" ~maximum:256 ~decode:(generic_did ~code ~what:"mention") value_fields in let* references = string_array ~code ~name:"references" ~maximum:256 ~decode:(at_uri ~code ~what:"reference") value_fields in let* blobs = decode_array ~code ~name:"blobs" ~maximum:64 ~decode:(blob ~code) value_fields in if not (String.equal (At_uri.to_string uri) (At_uri.to_string requested)) then Error (consistency_error ~code:"issue.view_uri_mismatch" ~message:"The issue-author PDS returned a different record URI") else Ok Issue_view. { record_key ; uri ; cid ; author_did ; repository_did ; title ; body ; created_at ; mentions ; references ; blobs }
let safe_state ~code value = let safe = function | 'a' .. 'z' | 'A' .. 'Z' | '0' .. '9' | '.' | '_' | ':' | '-' -> true | _ -> false in if String.length value = 0 || String.length value > 256 || not (String.for_all safe value) then Error (protocol_error ~code ~message:"The issue state value is invalid") else Ok (match value with | "sh.tangled.repo.issue.state.open" -> Issue_list.Open | "sh.tangled.repo.issue.state.closed" -> Closed | value -> Unknown value)
let decode_state_json ?(allow_duplicate_type = false) ~code json = let* fields = object_fields ~code json in let* uri, _, author_did = record_identity ~code ~collection:"sh.tangled.repo.issue.state" fields in let* cid = optional_cid ~code ~what:"issue-state" fields in let* value = required_field ~code "value" fields in let* value_fields = object_fields ~code value in let* () = record_type ~allow_duplicate_identical:allow_duplicate_type ~code ~expected:"sh.tangled.repo.issue.state" value_fields in let* issue_text = required_string ~code "issue" value_fields in let* issue_uri = at_uri ~code ~what:"issue subject" issue_text in let* state_text = required_string ~code "state" value_fields in let* state = safe_state ~code state_text in let* created_text = required_string ~code "createdAt" value_fields in let* created_at = timestamp ~code ~what:"issue-state creation" created_text in Ok Issue_view.{ uri; cid; author_did; issue_uri; state; created_at }
let strong_ref ~code ~what json = let* fields = object_fields ~code json in let* uri_text = required_string ~code "uri" fields in let* uri = at_uri ~code ~what:(what ^ " reference") uri_text in let* cid_text = required_string ~code "cid" fields in let* cid = cid ~code ~what:(what ^ " reference") cid_text in Ok Issue_view.{ uri; cid }
let markdown ~code json = let* fields = object_fields ~code json in let* () = record_type ~code ~expected:"sh.tangled.markup.markdown" fields in let* text = required_string ~code "text" fields in let* text = bounded_text ~code ~what:"comment text" ~minimum:0 ~maximum:(1024 * 1024) text in let* original = optional_string ~code "original" fields in let* original = match original with | None -> Ok None | Some value -> Result.map Option.some (bounded_text ~code ~what:"original comment text" ~minimum:0 ~maximum:(1024 * 1024) value) in let* blobs = decode_array ~code ~name:"blobs" ~maximum:64 ~decode:(blob ~code) fields in Ok Issue_view.{ text; original; blobs }
let decode_comment_json ?(allow_duplicate_type = false) ~code json = let* fields = object_fields ~code json in let* uri, _, author_did = record_identity ~code ~collection:"sh.tangled.feed.comment" fields in let* cid = optional_cid ~code ~what:"comment" fields in let* value = required_field ~code "value" fields in let* value_fields = object_fields ~code value in let* () = record_type ~allow_duplicate_identical:allow_duplicate_type ~code ~expected:"sh.tangled.feed.comment" value_fields in let* subject_json = required_field ~code "subject" value_fields in let* subject = strong_ref ~code ~what:"comment subject" subject_json in let* body_json = required_field ~code "body" value_fields in let* body = markdown ~code body_json in let* created_text = required_string ~code "createdAt" value_fields in let* created_at = timestamp ~code ~what:"comment creation" created_text in let* reply_json = optional_field ~code "replyTo" value_fields in let* reply_to = match reply_json with | None -> Ok None | Some value -> Result.map Option.some (strong_ref ~code ~what:"reply" value) in Ok Issue_view.{ uri; cid; author_did; subject; body; created_at; reply_to }
let cursor ~code fields = let* cursor = Json_fields.optional_or_null_string ~authority ~code "cursor" fields in match cursor with | Some value when String.length value > 4 * 1024 -> Error (protocol_error ~code ~message:"Bobbin returned an oversized relation cursor") | value -> Ok value
let decode_page ~code ~maximum ~decode json = let* fields = object_fields ~code json in let* items_json = required_field ~code "items" fields in let* items = match items_json with | `List values when List.length values <= maximum -> let rec loop reversed = function | [] -> Ok (List.rev reversed) | value :: rest -> let* value = decode ~code value in loop (value :: reversed) rest in loop [] values | `List _ -> Error (protocol_error ~code ~message:"Bobbin returned too many related records in one page") | _ -> Error (protocol_error ~code ~message:"Bobbin items must be an array") in let* cursor = cursor ~code fields in Ok (items, cursor)
let decode_issue_record ~requested body = let code = "issue.view_record_invalid" in let* json = parse_json ~code body in decode_issue_json ~code ~requested json
let decode_state_record body = let code = "issue.view_state_invalid" in let* json = parse_json ~code body in decode_state_json ~code json
let decode_comment_record body = let code = "issue.view_comment_invalid" in let* json = parse_json ~code body in decode_comment_json ~code json
let decode_state_page body = let code = "issue.view_state_page_invalid" in let* json = parse_json ~code body in let* states, cursor = decode_page ~code ~maximum:100 ~decode:(fun ~code json -> decode_state_json ~allow_duplicate_type:true ~code json) json in Ok Issue_view.{ states; cursor }
let decode_comment_page body = let code = "issue.view_comment_page_invalid" in let* json = parse_json ~code body in let* comments, cursor = decode_page ~code ~maximum:100 ~decode:(fun ~code json -> decode_comment_json ~allow_duplicate_type:true ~code json) json in Ok Issue_view.{ comments; cursor }