@@ -2,16 +2,15 @@ import type { Federation } from '@fedify/fedify';
22import type { Account } from 'account/account.entity' ;
33import type { KnexAccountRepository } from 'account/account.repository.knex' ;
44import type { AccountService } from 'account/account.service' ;
5+ import type { FedifyContextFactory } from 'activitypub/fedify-context.factory' ;
56import type { AppContext , ContextData } from 'app' ;
67import { isHandle } from 'helpers/activitypub/actor' ;
8+ import type { Knex } from 'knex' ;
79import { lookupAPIdByHandle } from 'lookup-helpers' ;
810import type { GetProfileDataResult , PostService } from 'post/post.service' ;
9- import {
10- getAccountDTOByHandle ,
11- getAccountDTOFromAccount ,
12- } from './helpers/account' ;
1311import type { AccountDTO } from './types' ;
1412import type { AccountFollowsView } from './views/account.follows.view' ;
13+ import AccountView from './views/account.view' ;
1514
1615/**
1716 * Default number of posts to return in a profile
@@ -23,78 +22,45 @@ const DEFAULT_POSTS_LIMIT = 20;
2322 */
2423const MAX_POSTS_LIMIT = 100 ;
2524
25+ /**
26+ * Keyword to indicate a request is for the current user
27+ */
28+ const CURRENT_USER_KEYWORD = 'me' ;
29+
2630/**
2731 * Create a handler to handle a request for an account
2832 *
29- * @param accountService Account service instance
33+ * @param db Database client
34+ * @param fedifyContextFactory Fedify context factory instance
3035 */
3136export function createGetAccountHandler (
32- accountService : AccountService ,
33- accountRepository : KnexAccountRepository ,
34- fedify : Federation < ContextData > ,
37+ db : Knex ,
38+ fedifyContextFactory : FedifyContextFactory ,
3539) {
3640 /**
3741 * Handle a request for an account
3842 *
3943 * @param ctx App context
4044 */
4145 return async function handleGetAccount ( ctx : AppContext ) {
42- const logger = ctx . get ( 'logger' ) ;
43- const site = ctx . get ( 'site' ) ;
44- let account : Account | null = null ;
45- const db = ctx . get ( 'db' ) ;
46-
47- const apCtx = fedify . createContext ( ctx . req . raw as Request , {
48- db,
49- globaldb : ctx . get ( 'globaldb' ) ,
50- logger,
51- } ) ;
52-
53- const defaultAccount = await accountRepository . getBySite (
54- ctx . get ( 'site' ) ,
55- ) ;
56-
5746 const handle = ctx . req . param ( 'handle' ) ;
58- // We are using the keyword 'me', if we want to get the account of teh current user
59- if ( handle === 'me' ) {
60- account = defaultAccount ;
61- } else {
62- if ( ! isHandle ( handle ) ) {
63- return new Response ( null , { status : 404 } ) ;
64- }
6547
66- const apId = await lookupAPIdByHandle ( apCtx , handle ) ;
67- if ( apId ) {
68- account = await accountRepository . getByApId ( new URL ( apId ) ) ;
69- }
48+ if ( handle !== CURRENT_USER_KEYWORD && ! isHandle ( handle ) ) {
49+ return new Response ( null , { status : 404 } ) ;
7050 }
7151
72- let accountDto : AccountDTO ;
52+ let accountDto : AccountDTO | null = null ;
7353
74- try {
75- //If we found the account in our db and it's an internal account, do an internal lookup
76- if ( account ?. isInternal ) {
77- accountDto = await getAccountDTOFromAccount (
78- account ,
79- defaultAccount ,
80- accountService ,
81- ) ;
82- } else {
83- //Otherwise, do a remote lookup to fetch the updated data
84- accountDto = await getAccountDTOByHandle (
85- handle ,
86- apCtx ,
87- site ,
88- accountService ,
89- ) ;
90- }
91- } catch ( error ) {
92- logger . error ( 'Error getting account: {error}' , { error } ) ;
54+ const accountView = new AccountView ( db , fedifyContextFactory ) ;
9355
94- return new Response ( null , { status : 500 } ) ;
56+ if ( handle === CURRENT_USER_KEYWORD ) {
57+ accountDto = await accountView . viewBySite ( ctx . get ( 'site' ) ) ;
58+ } else {
59+ accountDto = await accountView . viewByHandle ( handle , {
60+ site : ctx . get ( 'site' ) ,
61+ } ) ;
9562 }
9663
97- // Return response
9864 return new Response ( JSON . stringify ( accountDto ) , {
9965 headers : {
10066 'Content-Type' : 'application/json' ,
0 commit comments