Skip to content

Commit aebe9fc

Browse files
committed
Refactored account retrieval to use AccountView
no ref Refactored account retrieval to use `AccountView`
1 parent 875bd0f commit aebe9fc

File tree

3 files changed

+354
-59
lines changed

3 files changed

+354
-59
lines changed

src/app.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -971,9 +971,7 @@ app.get(
971971
app.get(
972972
'/.ghost/activitypub/account/:handle',
973973
requireRole(GhostRole.Owner, GhostRole.Administrator),
974-
spanWrapper(
975-
createGetAccountHandler(accountService, accountRepository, fedify),
976-
),
974+
spanWrapper(createGetAccountHandler(client, fedifyContextFactory)),
977975
);
978976
app.get(
979977
'/.ghost/activitypub/posts/:handle',

src/http/api/account.ts

Lines changed: 22 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,15 @@ import type { Federation } from '@fedify/fedify';
22
import type { Account } from 'account/account.entity';
33
import type { KnexAccountRepository } from 'account/account.repository.knex';
44
import type { AccountService } from 'account/account.service';
5+
import type { FedifyContextFactory } from 'activitypub/fedify-context.factory';
56
import type { AppContext, ContextData } from 'app';
67
import { isHandle } from 'helpers/activitypub/actor';
8+
import type { Knex } from 'knex';
79
import { lookupAPIdByHandle } from 'lookup-helpers';
810
import type { GetProfileDataResult, PostService } from 'post/post.service';
9-
import {
10-
getAccountDTOByHandle,
11-
getAccountDTOFromAccount,
12-
} from './helpers/account';
1311
import type { AccountDTO } from './types';
1412
import 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
*/
2423
const 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
*/
3136
export 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

Comments
 (0)