Auto-indexing service and GraphQL API for AT Protocol Records quickslice.slices.network/
atproto gleam graphql
at main 78 lines 2.4 kB view raw
1import backfill 2import database/executor.{type Executor} 3import database/repositories/actors 4import gleam/list 5import gleam/string 6import logging 7 8/// Ensures that an actor exists in the database. If the actor is not found, 9/// it will be resolved from the PLC directory and added to the database. 10/// 11/// Returns Ok(True) if a new actor was created. 12/// Returns Ok(False) if the actor already existed. 13/// Returns Error(String) if the actor could not be resolved or created. 14pub fn ensure_actor_exists( 15 db: Executor, 16 did: String, 17 plc_url: String, 18) -> Result(Bool, String) { 19 // Check if actor already exists 20 case actors.get(db, did) { 21 Ok(actors) -> { 22 case list.is_empty(actors) { 23 False -> { 24 // Actor exists, nothing to do 25 logging.log(logging.Debug, "Actor already exists: " <> did) 26 Ok(False) 27 } 28 True -> { 29 // Actor not found, need to resolve and create 30 logging.log(logging.Info, "Actor not found, resolving DID: " <> did) 31 32 case backfill.resolve_did(did, plc_url) { 33 Ok(atp_data) -> { 34 logging.log( 35 logging.Info, 36 "Resolved DID " <> did <> " to handle: " <> atp_data.handle, 37 ) 38 39 // Create actor in database 40 case actors.upsert(db, atp_data.did, atp_data.handle) { 41 Ok(_) -> { 42 logging.log( 43 logging.Info, 44 "Successfully created actor: " <> did, 45 ) 46 Ok(True) 47 } 48 Error(err) -> { 49 let error_msg = 50 "Failed to create actor in database: " 51 <> did 52 <> " - " 53 <> string.inspect(err) 54 logging.log(logging.Error, error_msg) 55 Error(error_msg) 56 } 57 } 58 } 59 Error(err) -> { 60 let error_msg = "Failed to resolve DID: " <> did <> " - " <> err 61 logging.log(logging.Error, error_msg) 62 Error(error_msg) 63 } 64 } 65 } 66 } 67 } 68 Error(err) -> { 69 let error_msg = 70 "Database error checking for actor: " 71 <> did 72 <> " - " 73 <> string.inspect(err) 74 logging.log(logging.Error, error_msg) 75 Error(error_msg) 76 } 77 } 78}