Use atproto actions with ease in iOS shortcuts
at main 2.1 kB view raw
1// 2// GetRemoteInfo.swift 3// shortcut 4// 5// Created by Bailey Townsend on 7/2/25. 6// 7 8import Foundation 9 10struct GetRemoteInfo { 11 public static func getRemoteRepoInfo(possibleRepo: String, atProtoManager: AtProtocolManager) 12 async throws 13 -> RemoteRepoInfo 14 { 15 let lowerCaseRepo = possibleRepo.lowercased() 16 var repo = "" 17 var pdsUrl = "https://bsky.social" 18 var handle = "" 19 20 if lowerCaseRepo.hasPrefix("did:") { 21 do { 22 let didDoc = try await atProtoManager.resolveDidDocument(did: lowerCaseRepo) 23 repo = didDoc.id 24 if let pds = didDoc.getPDSEndpoint() { 25 pdsUrl = pds.absoluteString 26 } 27 if let knownAs = didDoc.alsoKnownAs?.first { 28 handle = knownAs 29 } 30 } catch { 31 throw GenericIntentError.message("Could not resolve DID document") 32 } 33 } else { 34 let cleanedHandle = lowerCaseRepo.replacingOccurrences(of: "@", with: "") 35 36 do { 37 let didDoc = try await atProtoManager.resolveDidDocument(handle: cleanedHandle) 38 repo = didDoc.id 39 if let pds = didDoc.getPDSEndpoint() { 40 pdsUrl = pds.absoluteString 41 } 42 if let knownAs = didDoc.alsoKnownAs?.first { 43 handle = knownAs 44 } 45 } catch let error as LoginError { 46 switch error { 47 case LoginError.handleDoesNotResolve: 48 throw GenericIntentError.message("Cannot resolve handle to a DID") 49 case .didDocumentNotFound: 50 throw GenericIntentError.message("Cannot resolve DID document") 51 } 52 } 53 } 54 55 return RemoteRepoInfo( 56 repo: repo, pdsURL: pdsUrl, handle: handle.replacingOccurrences(of: "at://", with: "")) 57 } 58 59} 60 61struct RemoteRepoInfo { 62 let repo: String 63 let pdsURL: String 64 let handle: String 65}