package atproto

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file repo.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
open Session
open Cohttp_client
open App
open Embed
open Facet
open Feed
open Notification

(** [com.atproto.repo] — create/put/delete/applyWrites plus typed record parsers. *)
module Repo = struct
  type post_record = {
    text : string;
    record_type : string;
    embed : Embed.embed option;
    facets : Facet.facet list option;
    langs : string list option;
    reply : Notification.reply option;
    tags : string list option;
    self_labels : string list option;
    created_at : string;
  }

  let parse_post_record json : post_record =
    let open Yojson.Safe.Util in
    let text = json |> member "text" |> to_string in
    let record_type = json |> member "$type" |> to_string in
    let embed = Embed.parse_embed_option json in
    let facets = Feed.extract_facets_option json in
    let langs = Feed.extract_langs_option json in
    let reply = Notification.parse_reply_option json in
    let tags = Feed.extract_tags_option json in
    let self_labels = Feed.extract_self_labels_option json in
    let created_at = json |> member "createdAt" |> to_string in
    {
      text;
      record_type;
      embed;
      facets;
      langs;
      reply;
      tags;
      self_labels;
      created_at;
    }

  type commit_meta = { cid : string; rev : string }
  type record_get = { uri : string; cid : string option; value : Yojson.Safe.t }
  type listed_record = { uri : string; cid : string; value : Yojson.Safe.t }
  type listed_records = { cursor : string option; records : listed_record list }

  type repo_description = {
    handle : string;
    did : string;
    did_doc : Yojson.Safe.t option;
    collections : string list;
    handle_is_correct : bool;
  }

  type write_result = {
    uri : string;
    cid : string;
    commit : commit_meta option;
    validation_status : string option;
  }

  type apply_writes_result = {
    commit : commit_meta option;
    results : Yojson.Safe.t list;
  }

  let parse_commit_meta json : commit_meta option =
    match json with
    | `Assoc _ ->
        Some
          {
            cid =
              (match Yojson.Safe.Util.member "cid" json with
              | `String s -> s
              | _ -> "");
            rev =
              (match Yojson.Safe.Util.member "rev" json with
              | `String s -> s
              | _ -> "");
          }
    | _ -> None

  let parse_record_get json : record_get =
    let open Yojson.Safe.Util in
    {
      uri = (match json |> member "uri" with `String s -> s | _ -> "");
      cid = (match json |> member "cid" with `String s -> Some s | _ -> None);
      value = json |> member "value";
    }

  let parse_listed_record json : listed_record =
    let open Yojson.Safe.Util in
    {
      uri = (match json |> member "uri" with `String s -> s | _ -> "");
      cid = (match json |> member "cid" with `String s -> s | _ -> "");
      value = json |> member "value";
    }

  let parse_listed_records json : listed_records =
    let open Yojson.Safe.Util in
    {
      cursor =
        (match json |> member "cursor" with `String s -> Some s | _ -> None);
      records =
        (match json |> member "records" with
        | `List xs -> List.map parse_listed_record xs
        | _ -> []);
    }

  let parse_repo_description json : repo_description =
    let open Yojson.Safe.Util in
    {
      handle = (match json |> member "handle" with `String s -> s | _ -> "");
      did = (match json |> member "did" with `String s -> s | _ -> "");
      did_doc =
        (match json |> member "didDoc" with
        | `Null -> None
        | other -> Some other);
      collections =
        (match json |> member "collections" with
        | `List xs ->
            List.filter_map (function `String s -> Some s | _ -> None) xs
        | _ -> []);
      handle_is_correct =
        (match json |> member "handleIsCorrect" with
        | `Bool b -> b
        | _ -> false);
    }

  let parse_write_result json : write_result =
    let open Yojson.Safe.Util in
    {
      uri = (match json |> member "uri" with `String s -> s | _ -> "");
      cid = (match json |> member "cid" with `String s -> s | _ -> "");
      commit = parse_commit_meta (json |> member "commit");
      validation_status =
        (match json |> member "validationStatus" with
        | `String s -> Some s
        | _ -> None);
    }

  let parse_apply_writes_result json : apply_writes_result =
    let open Yojson.Safe.Util in
    {
      commit = parse_commit_meta (json |> member "commit");
      results = (match json |> member "results" with `List xs -> xs | _ -> []);
    }

  (** Query-string pairs for [com.atproto.repo.describeRepo]. *)
  let describe_repo_body ~repo : (string * string) list = [ ("repo", repo) ]

  (** Query-string pairs for [com.atproto.repo.getRecord]. Optional [cid]
      maps to the lexicon query. *)
  let get_record_body ~repo ~collection ~rkey ?cid () : (string * string) list =
    [ ("repo", repo); ("collection", collection); ("rkey", rkey) ]
    @ Client.Client.opt_pair "cid" cid

  (** Query-string pairs for [com.atproto.repo.listRecords]. Optional
      [limit] / [cursor] / [reverse] map to the lexicon query. *)
  let list_records_body ~repo ~collection ?limit ?cursor ?reverse () :
      (string * string) list =
    [ ("repo", repo); ("collection", collection) ]
    @ Client.Client.opt_int "limit" limit
    @ Client.Client.opt_pair "cursor" cursor
    @ Client.Client.opt_bool "reverse" reverse

  (** Parsed [com.atproto.repo.describeRepo] (handle, DID, collections).
      Works without a session. *)
  let describe_repo_parsed ?session ?host ~repo () : repo_description =
    Client.Client.get_json ?session ?host "com.atproto.repo.describeRepo"
      (describe_repo_body ~repo)
    |> parse_repo_description

  (** Parsed [com.atproto.repo.getRecord] ([uri], optional [cid], [value]).
      Works without a session. *)
  let get_record_parsed ?session ?host ~repo ~collection ~rkey ?cid () :
      record_get =
    Client.Client.get_json ?session ?host "com.atproto.repo.getRecord"
      (get_record_body ~repo ~collection ~rkey ?cid ())
    |> parse_record_get

  (** Parsed [com.atproto.repo.listRecords]. Optional [limit] / [cursor] /
      [reverse] map to the lexicon query. Works without a session. *)
  let list_records_parsed ?session ?host ~repo ~collection ?limit ?cursor
      ?reverse () : listed_records =
    Client.Client.get_json ?session ?host "com.atproto.repo.listRecords"
      (list_records_body ~repo ~collection ?limit ?cursor ?reverse ())
    |> parse_listed_records

  let create_repo_endpoint (query_name : string) : string =
    "com.atproto.repo" ^ "." ^ query_name

  (** Raw JSON from [com.atproto.repo.describeRepo] for [repo]. Shares
      [describe_repo_body] with [describe_repo_parsed]. *)
  let describe_repo (s : Session.session) (repo : string) : string =
    Client.Client.get_text ~session:s "com.atproto.repo.describeRepo"
      (describe_repo_body ~repo)

  (** Fetch a record via [com.atproto.repo.getRecord]. Returns the raw JSON
      body. Shares [get_record_body] with [get_record_parsed]. *)
  let get_record (s : Session.session) (repo : string) (collection : string)
      (rkey : string) (cid : string) : string =
    Client.Client.get_text ~session:s "com.atproto.repo.getRecord"
      (get_record_body ~repo ~collection ~rkey ~cid ())

  (** List records via [com.atproto.repo.listRecords]. Returns the raw JSON
      body. Shares [list_records_body] with [list_records_parsed]. *)
  let list_records (s : Session.session) (repo : string) (collection : string)
      (limit : int) (reverse : bool) : string =
    Client.Client.get_text ~session:s "com.atproto.repo.listRecords"
      (list_records_body ~repo ~collection ~limit ~reverse ())

  let record_json_of_string (record : string) : Yojson.Safe.t =
    try Yojson.Safe.from_string record with _ -> `String record

  (** JSON body for [com.atproto.repo.createRecord]. [record] is Yojson. *)
  let create_record_body ~repo ~collection ?rkey ?(validate = true) ?swap_commit
      (record : Yojson.Safe.t) : Yojson.Safe.t =
    let fields =
      [
        Some ("repo", `String repo);
        Some ("collection", `String collection);
        Some ("record", record);
        Option.map (fun rkey -> ("rkey", `String rkey)) rkey;
        Some ("validate", `Bool validate);
        Option.map
          (fun swap_commit -> ("swapCommit", `String swap_commit))
          swap_commit;
      ]
    in
    `Assoc (List.filter_map Fun.id fields)

  (** JSON body for [com.atproto.repo.putRecord]. [record] is Yojson. *)
  let put_record_body ~repo ~collection ?rkey ?(validate = true) ?swap_record
      ?swap_commit (record : Yojson.Safe.t) : Yojson.Safe.t =
    let fields =
      [
        Some ("repo", `String repo);
        Some ("collection", `String collection);
        Some ("record", record);
        Option.map (fun rkey -> ("rkey", `String rkey)) rkey;
        Some ("validate", `Bool validate);
        Option.map
          (fun swap_record -> ("swapRecord", `String swap_record))
          swap_record;
        Option.map
          (fun swap_commit -> ("swapCommit", `String swap_commit))
          swap_commit;
      ]
    in
    `Assoc (List.filter_map Fun.id fields)

  (* Empty procedure output stays [""] for existing string write callers. *)
  let json_body_string (json : Yojson.Safe.t) : string =
    match json with `Assoc [] -> "" | j -> Yojson.Safe.to_string j

  let post_repo_write (s : Session.session) (query_name : string)
      (body : Yojson.Safe.t) : string =
    Client.Client.post_json ~session:s
      (create_repo_endpoint query_name)
      (Yojson.Safe.to_string body)
    |> json_body_string

  (** Create a record via [com.atproto.repo.createRecord]. [record] is a JSON
      object string; optional [rkey] and [swap_commit] map to the lexicon
      inputs. Shares [create_record_body] with [create_record_json]. *)
  let create_record (s : Session.session) (repo : string) (collection : string)
      ?rkey ?(validate = true) ?swap_commit (record : string) : string =
    post_repo_write s "createRecord"
      (create_record_body ~repo ~collection ?rkey ~validate ?swap_commit
         (record_json_of_string record))

  (** Create a record via [com.atproto.repo.createRecord] from Yojson.
      Same optional labels as [create_record]. Returns the parsed write
      result ([uri] / [cid] / optional [commit]) via [parse_write_result]. *)
  let create_record_json (s : Session.session) (repo : string)
      (collection : string) ?rkey ?(validate = true) ?swap_commit
      (record : Yojson.Safe.t) : write_result =
    post_repo_write s "createRecord"
      (create_record_body ~repo ~collection ?rkey ~validate ?swap_commit record)
    |> Yojson.Safe.from_string |> parse_write_result

  (** Put a record via [com.atproto.repo.putRecord]. [record] is a JSON
      object string; optional [rkey], [swap_record], and [swap_commit] map
      to the lexicon inputs. Shares [put_record_body] with [put_record_json]. *)
  let put_record (s : Session.session) (repo : string) (collection : string)
      ?rkey ?(validate = true) ?swap_record ?swap_commit (record : string) :
      string =
    post_repo_write s "putRecord"
      (put_record_body ~repo ~collection ?rkey ~validate ?swap_record
         ?swap_commit
         (record_json_of_string record))

  (** Put a record via [com.atproto.repo.putRecord] from Yojson. Same
      optional labels as [put_record]. Returns the parsed write result
      ([uri] / [cid] / optional [commit]) via [parse_write_result]. *)
  let put_record_json (s : Session.session) (repo : string)
      (collection : string) ?rkey ?(validate = true) ?swap_record ?swap_commit
      (record : Yojson.Safe.t) : write_result =
    post_repo_write s "putRecord"
      (put_record_body ~repo ~collection ?rkey ~validate ?swap_record
         ?swap_commit record)
    |> Yojson.Safe.from_string |> parse_write_result

  (** JSON body for [com.atproto.repo.deleteRecord]. Same field names as
      [delete_record] ([repo] / [collection] / [rkey] / optional
      [swapRecord] / [swapCommit]). *)
  let delete_record_body ~repo ~collection ~rkey ?swap_record ?swap_commit () :
      Yojson.Safe.t =
    let fields =
      [
        Some ("repo", `String repo);
        Some ("collection", `String collection);
        Some ("rkey", `String rkey);
        Option.map
          (fun swap_record -> ("swapRecord", `String swap_record))
          swap_record;
        Option.map
          (fun swap_commit -> ("swapCommit", `String swap_commit))
          swap_commit;
      ]
    in
    `Assoc (List.filter_map Fun.id fields)

  (** Delete a record via [com.atproto.repo.deleteRecord]. Optional
      [swap_record] / [swap_commit] map to the lexicon inputs. Shares
      [delete_record_body] with callers that need the JSON. *)
  let delete_record (s : Session.session) (repo : string) (collection : string)
      ?swap_record ?swap_commit (rkey : string) : string =
    post_repo_write s "deleteRecord"
      (delete_record_body ~repo ~collection ~rkey ?swap_record ?swap_commit ())

  type write_op =
    | Create of {
        collection : string;
        rkey : string option;
        value : Yojson.Safe.t;
      }
    | Update of { collection : string; rkey : string; value : Yojson.Safe.t }
    | Delete of { collection : string; rkey : string }

  (** JSON for one [com.atproto.repo.applyWrites] create / update / delete
      op. *)
  let write_op_to_json = function
    | Create { collection; rkey; value } ->
        `Assoc
          ([
             ("$type", `String "com.atproto.repo.applyWrites#create");
             ("collection", `String collection);
             ("value", value);
           ]
          @ match rkey with Some r -> [ ("rkey", `String r) ] | None -> [])
    | Update { collection; rkey; value } ->
        `Assoc
          [
            ("$type", `String "com.atproto.repo.applyWrites#update");
            ("collection", `String collection);
            ("rkey", `String rkey);
            ("value", value);
          ]
    | Delete { collection; rkey } ->
        `Assoc
          [
            ("$type", `String "com.atproto.repo.applyWrites#delete");
            ("collection", `String collection);
            ("rkey", `String rkey);
          ]

  (** JSON body for [com.atproto.repo.applyWrites]. *)
  let apply_writes_body ~repo ~writes ?(validate = true) ?swap_commit () :
      Yojson.Safe.t =
    let fields =
      [
        ("repo", `String repo);
        ("validate", `Bool validate);
        ("writes", `List (List.map write_op_to_json writes));
      ]
      @
      match swap_commit with
      | Some cid -> [ ("swapCommit", `String cid) ]
      | None -> []
    in
    `Assoc fields

  (** Apply a batch of create/update/delete ops via
      [com.atproto.repo.applyWrites]. Returns the raw JSON body. *)
  let apply_writes (s : Session.session) ~repo ~writes ?validate ?swap_commit ()
      : string =
    post_repo_write s "applyWrites"
      (apply_writes_body ~repo ~writes ?validate ?swap_commit ())

  (** Apply a batch of create/update/delete ops via
      [com.atproto.repo.applyWrites] and parse the response. Same
      arguments as [apply_writes]. Returns [apply_writes_result]
      ([commit] / [results]) via [parse_apply_writes_result]. *)
  let apply_writes_parsed (s : Session.session) ~repo ~writes ?validate
      ?swap_commit () : apply_writes_result =
    apply_writes s ~repo ~writes ?validate ?swap_commit ()
    |> Yojson.Safe.from_string |> parse_apply_writes_result

  type blob_ref = {
    cid : string;
    mime_type : string;
    size : int;
    original : Yojson.Safe.t;
  }

  (** Blob CID for [bytes] (CIDv1 raw + SHA-256). Optional [expected]
      must match. *)
  let verify_blob_bytes ?expected (bytes : string) : Cid.Cid.t =
    Cid.Cid.verify_blob ?expected bytes

  let parse_blob_ref json : blob_ref =
    let open Yojson.Safe.Util in
    let blob = match json |> member "blob" with `Null -> json | b -> b in
    let cid =
      match blob |> member "ref" with
      | `Assoc _ as ref_ -> (
          match ref_ |> member "$link" with `String s -> s | _ -> "")
      | `String s -> s
      | _ -> ( match blob |> member "cid" with `String s -> s | _ -> "")
    in
    {
      cid;
      mime_type =
        (match blob |> member "mimeType" with `String s -> s | _ -> "");
      size = (match blob |> member "size" with `Int n -> n | _ -> 0);
      original = blob;
    }

  (** Encode [blob_ref] as a lexicon blob object
      ([{ "$type": "blob", "ref": { "$link": cid }, "mimeType", "size" }]).
      Reuses [.original] when it is already a well-formed blob Assoc
      with those fields; otherwise builds from [cid] / [mime_type] /
      [size] via [Embed.blob_to_json]. Does not invent leftover unused
      fields. *)
  let blob_ref_to_json (b : blob_ref) : Yojson.Safe.t =
    let well_formed =
      match b.original with
      | `Assoc _ as json -> (
          let open Yojson.Safe.Util in
          (match json |> member "$type" with
          | `String "blob" -> true
          | _ -> false)
          && (match json |> member "ref" with
             | `Assoc _ as ref_ -> (
                 match ref_ |> member "$link" with
                 | `String s -> s = b.cid
                 | _ -> false)
             | _ -> false)
          && (match json |> member "mimeType" with
             | `String s -> s = b.mime_type
             | _ -> false)
          &&
          match json |> member "size" with `Int n -> n = b.size | _ -> false)
      | _ -> false
    in
    if well_formed then b.original
    else Embed.blob_to_json ~cid:b.cid ~mime_type:b.mime_type ~size:b.size ()

  let upload_blob_url (s : Session.session) : string =
    App.create_endpoint_url (App.create_base_url s)
      (create_repo_endpoint "uploadBlob")

  (** Upload bytes via [com.atproto.repo.uploadBlob]. Optional
      [content_type] defaults to [application/octet-stream]. *)
  let upload_blob (s : Session.session)
      ?(content_type = "application/octet-stream") (bytes : string) : blob_ref =
    let bearer_token = Session.bearer_token_from_session s in
    let headers =
      Cohttp_client.create_headers_from_pairs
        [ ("Content-Type", content_type); bearer_token ]
    in
    let url = upload_blob_url s in
    let body =
      Lwt_main.run (Cohttp_client.post_data_with_headers url bytes headers)
    in
    parse_blob_ref (Yojson.Safe.from_string body)

  type missing_blob = { cid : string; record_uri : string }

  type list_missing_blobs = {
    cursor : string option;
    blobs : missing_blob list;
  }

  let parse_missing_blob json : missing_blob =
    let open Yojson.Safe.Util in
    {
      cid = (match json |> member "cid" with `String s -> s | _ -> "");
      record_uri =
        (match json |> member "recordUri" with `String s -> s | _ -> "");
    }

  let parse_list_missing_blobs json : list_missing_blobs =
    let open Yojson.Safe.Util in
    {
      cursor =
        (match json |> member "cursor" with `String s -> Some s | _ -> None);
      blobs =
        (match json |> member "blobs" with
        | `List items -> List.map parse_missing_blob items
        | _ -> []);
    }

  (** Query-string pairs for [com.atproto.repo.listMissingBlobs]. Optional
      [cursor] / [limit] map to the lexicon query. *)
  let list_missing_blobs_body ?cursor ?limit () : (string * string) list =
    Client.Client.opt_pair "cursor" cursor @ Client.Client.opt_int "limit" limit

  (** Missing blobs via [com.atproto.repo.listMissingBlobs]. Optional
      [cursor] / [limit] map to the lexicon query. *)
  let list_missing_blobs (s : Session.session) ?cursor ?limit () :
      list_missing_blobs =
    Client.Client.get_json ~session:s "com.atproto.repo.listMissingBlobs"
      (list_missing_blobs_body ?cursor ?limit ())
    |> parse_list_missing_blobs

  let import_repo_url (s : Session.session) : string =
    App.create_endpoint_url (App.create_base_url s)
      (create_repo_endpoint "importRepo")

  (** Import a CAR via [com.atproto.repo.importRepo]. *)
  let import_repo (s : Session.session) (car_bytes : string) : string =
    let bearer_token = Session.bearer_token_from_session s in
    let headers =
      Cohttp_client.create_headers_from_pairs
        [ ("Content-Type", "application/vnd.ipld.car"); bearer_token ]
    in
    let url = import_repo_url s in
    Lwt_main.run (Cohttp_client.post_data_with_headers url car_bytes headers)
end