A social knowledge tool for researchers built on ATProto
at development 1.5 kB view raw
1import { Result, ok, err } from '../../../../shared/core/Result'; 2import { 3 IAtUriResolutionService, 4 AtUriResourceType, 5 AtUriResolutionResult, 6} from '../../domain/services/IAtUriResolutionService'; 7import { CollectionId } from '../../domain/value-objects/CollectionId'; 8import { InMemoryCollectionRepository } from './InMemoryCollectionRepository'; 9 10export class InMemoryAtUriResolutionService implements IAtUriResolutionService { 11 constructor(private collectionRepository: InMemoryCollectionRepository) {} 12 13 async resolveAtUri( 14 atUri: string, 15 ): Promise<Result<AtUriResolutionResult | null>> { 16 try { 17 // Get all collections and check if any have a published record with this URI 18 const allCollections = this.collectionRepository.getAllCollections(); 19 20 for (const collection of allCollections) { 21 if (collection.publishedRecordId?.uri === atUri) { 22 return ok({ 23 type: AtUriResourceType.COLLECTION, 24 id: collection.collectionId, 25 }); 26 } 27 } 28 29 return ok(null); 30 } catch (error) { 31 return err(error as Error); 32 } 33 } 34 35 async resolveCollectionId( 36 atUri: string, 37 ): Promise<Result<CollectionId | null>> { 38 const result = await this.resolveAtUri(atUri); 39 40 if (result.isErr()) { 41 return err(result.error); 42 } 43 44 if (!result.value || result.value.type !== AtUriResourceType.COLLECTION) { 45 return ok(null); 46 } 47 48 return ok(result.value.id as CollectionId); 49 } 50}