A social knowledge tool for researchers built on ATProto
1import { UseCase } from 'src/shared/core/UseCase';
2import { IProfileService } from '../../../domain/services/IProfileService';
3import { err, ok, Result } from 'src/shared/core/Result';
4import { DIDOrHandle } from 'src/modules/atproto/domain/DIDOrHandle';
5import { IIdentityResolutionService } from 'src/modules/atproto/domain/services/IIdentityResolutionService';
6
7export interface GetMyProfileQuery {
8 userId: string;
9}
10
11export interface GetMyProfileResult {
12 id: string;
13 name: string;
14 handle: string;
15 description?: string;
16 avatarUrl?: string;
17}
18
19export class ValidationError extends Error {
20 constructor(message: string) {
21 super(message);
22 this.name = 'ValidationError';
23 }
24}
25
26export class GetProfileUseCase
27 implements UseCase<GetMyProfileQuery, Result<GetMyProfileResult>>
28{
29 constructor(
30 private profileService: IProfileService,
31 private identityResolver: IIdentityResolutionService,
32 ) {}
33
34 async execute(query: GetMyProfileQuery): Promise<Result<GetMyProfileResult>> {
35 // Validate user ID
36 if (!query.userId || query.userId.trim().length === 0) {
37 return err(new ValidationError('User ID is required'));
38 }
39
40 // Parse and validate user identifier
41 const identifierResult = DIDOrHandle.create(query.userId);
42 if (identifierResult.isErr()) {
43 return err(new ValidationError('Invalid user identifier'));
44 }
45
46 // Resolve to DID
47 const didResult = await this.identityResolver.resolveToDID(
48 identifierResult.value,
49 );
50 if (didResult.isErr()) {
51 return err(
52 new ValidationError(
53 `Could not resolve user identifier: ${didResult.error.message}`,
54 ),
55 );
56 }
57
58 try {
59 // Fetch user profile using the resolved DID
60 const profileResult = await this.profileService.getProfile(
61 didResult.value.value,
62 );
63
64 if (profileResult.isErr()) {
65 return err(
66 new Error(
67 `Failed to fetch user profile: ${profileResult.error instanceof Error ? profileResult.error.message : 'Unknown error'}`,
68 ),
69 );
70 }
71
72 const profile = profileResult.value;
73
74 return ok({
75 id: profile.id,
76 name: profile.name,
77 handle: profile.handle,
78 description: profile.bio,
79 avatarUrl: profile.avatarUrl,
80 });
81 } catch (error) {
82 return err(
83 new Error(
84 `Failed to get user profile: ${error instanceof Error ? error.message : 'Unknown error'}`,
85 ),
86 );
87 }
88 }
89}