11import type { Federation } from '@fedify/fedify' ;
2- import type { Account } from 'account/account.entity' ;
2+ import type { Account , PersistedAccount } from 'account/account.entity' ;
33import type { KnexAccountRepository } from 'account/account.repository.knex' ;
44import type { AccountService } from 'account/account.service' ;
55import type { FedifyContextFactory } from 'activitypub/fedify-context.factory' ;
66import type { AppContext , ContextData } from 'app' ;
77import { exhaustiveCheck , getError , getValue , isError } from 'core/result' ;
88import { isHandle } from 'helpers/activitypub/actor' ;
99import { lookupAPIdByHandle } from 'lookup-helpers' ;
10- import type { GetProfileDataResult , PostService } from 'post/post.service' ;
1110import { z } from 'zod' ;
1211import {
1312 getAccountDTOByHandle ,
@@ -18,7 +17,10 @@ import type {
1817 AccountFollows ,
1918 AccountFollowsView ,
2019} from './views/account.follows.view' ;
21-
20+ import type {
21+ AccountPosts ,
22+ AccountPostsView ,
23+ } from './views/account.posts.view' ;
2224/**
2325 * Default number of posts to return in a profile
2426 */
@@ -218,9 +220,9 @@ function validateRequestParams(ctx: AppContext) {
218220 * @param profileService Profile service instance
219221 */
220222export function createGetAccountPostsHandler (
221- postService : PostService ,
222223 accountRepository : KnexAccountRepository ,
223- fedify : Federation < ContextData > ,
224+ accountPostsView : AccountPostsView ,
225+ fedifyContextFactory : FedifyContextFactory ,
224226) {
225227 /**
226228 * Handle a request for a list of posts by an account
@@ -234,119 +236,86 @@ export function createGetAccountPostsHandler(
234236 }
235237
236238 const logger = ctx . get ( 'logger' ) ;
237- let account : Account | null = null ;
238- const db = ctx . get ( 'db' ) ;
239-
240- const apCtx = fedify . createContext ( ctx . req . raw as Request , {
241- db,
242- globaldb : ctx . get ( 'globaldb' ) ,
243- logger,
244- } ) ;
239+ const site = ctx . get ( 'site' ) ;
245240
246241 const handle = ctx . req . param ( 'handle' ) ;
247242 if ( ! handle ) {
248243 return new Response ( null , { status : 400 } ) ;
249244 }
250245
251- const defaultAccount = await accountRepository . getBySite (
252- ctx . get ( ' site' ) ,
253- ) ;
246+ const currentContextAccount = ( await accountRepository . getBySite (
247+ site ,
248+ ) ) as PersistedAccount ;
254249
255- if ( ! defaultAccount || ! defaultAccount . id ) {
256- return new Response ( null , { status : 400 } ) ;
257- }
250+ let accountPosts : AccountPosts ;
258251
259252 // We are using the keyword 'me', if we want to get the posts of the current user
260253 if ( handle === 'me' ) {
261- account = defaultAccount ;
254+ accountPosts = await accountPostsView . getPostsByAccount (
255+ currentContextAccount . id ,
256+ currentContextAccount . id ,
257+ params . limit ,
258+ params . cursor ,
259+ ) ;
262260 } else {
263- if ( ! isHandle ( handle ) ) {
264- return new Response ( null , { status : 400 } ) ;
265- }
261+ const ctx = fedifyContextFactory . getFedifyContext ( ) ;
262+ const apId = await lookupAPIdByHandle ( ctx , handle ) ;
266263
267- const apId = await lookupAPIdByHandle ( apCtx , handle ) ;
268- if ( apId ) {
269- account = await accountRepository . getByApId ( new URL ( apId ) ) ;
264+ if ( ! apId ) {
265+ return new Response ( `AP ID not found for handle: ${ handle } ` , {
266+ status : 400 ,
267+ } ) ;
270268 }
271- }
272-
273- const result : GetProfileDataResult = {
274- results : [ ] ,
275- nextCursor : null ,
276- } ;
277269
278- try {
279- //If we found the account in our db and it's an internal account, do an internal lookup
280- if ( account ?. isInternal && account . id ) {
281- const postResult = await postService . getPostsByAccount (
282- account . id ,
283- defaultAccount . id ,
284- params . limit ,
285- params . cursor ,
286- ) ;
270+ const account = ( await accountRepository . getByApId (
271+ new URL ( apId ) ,
272+ ) ) as PersistedAccount ;
287273
288- result . results = postResult . results ;
289- result . nextCursor = postResult . nextCursor ;
290- } else {
291- //Otherwise, do a remote lookup to fetch the posts
292- const postResult = await postService . getPostsByRemoteLookUp (
293- defaultAccount . id ,
294- defaultAccount . apId ,
295- handle ,
296- params . cursor || '' ,
297- ) ;
298- if ( postResult instanceof Error ) {
299- throw postResult ;
300- }
301- if ( isError ( postResult ) ) {
302- const error = getError ( postResult ) ;
303- switch ( error ) {
304- case 'invalid-next-parameter' :
305- logger . error ( 'Invalid next parameter' ) ;
306- return new Response ( null , { status : 400 } ) ;
307- case 'not-an-actor' :
308- logger . error ( `Actor not found for ${ handle } ` ) ;
309- return new Response ( null , { status : 404 } ) ;
310- case 'error-getting-outbox' :
311- logger . error ( `Error getting outbox for ${ handle } ` ) ;
312- return new Response (
313- JSON . stringify ( {
314- posts : [ ] ,
315- next : null ,
316- } ) ,
317- { status : 200 } ,
318- ) ;
319- case 'no-page-found' :
320- logger . error (
321- `No page found in outbox for ${ handle } ` ,
322- ) ;
323- return new Response (
324- JSON . stringify ( {
325- posts : [ ] ,
326- next : null ,
327- } ) ,
328- { status : 200 } ,
329- ) ;
330- default :
331- return exhaustiveCheck ( error ) ;
332- }
274+ const result = await accountPostsView . getPostsByApId (
275+ new URL ( apId ) ,
276+ account ,
277+ currentContextAccount ,
278+ params . limit ,
279+ params . cursor ,
280+ ) ;
281+ if ( isError ( result ) ) {
282+ const error = getError ( result ) ;
283+ switch ( error ) {
284+ case 'invalid-next-parameter' :
285+ logger . error ( 'Invalid next parameter' ) ;
286+ return new Response ( null , { status : 400 } ) ;
287+ case 'not-an-actor' :
288+ logger . error ( `Actor not found for ${ handle } ` ) ;
289+ return new Response ( null , { status : 404 } ) ;
290+ case 'error-getting-outbox' :
291+ logger . error ( `Error getting outbox for ${ handle } ` ) ;
292+ return new Response (
293+ JSON . stringify ( {
294+ posts : [ ] ,
295+ next : null ,
296+ } ) ,
297+ { status : 200 } ,
298+ ) ;
299+ case 'no-page-found' :
300+ logger . error ( `No page found in outbox for ${ handle } ` ) ;
301+ return new Response (
302+ JSON . stringify ( {
303+ posts : [ ] ,
304+ next : null ,
305+ } ) ,
306+ { status : 200 } ,
307+ ) ;
308+ default :
309+ return exhaustiveCheck ( error ) ;
333310 }
334- const posts = getValue ( postResult ) ;
335- result . results = posts . results ;
336- result . nextCursor = posts . nextCursor ;
337311 }
338- } catch ( error ) {
339- logger . error ( `Error getting posts for ${ handle } : {error}` , {
340- error,
341- } ) ;
342-
343- return new Response ( null , { status : 500 } ) ;
312+ accountPosts = getValue ( result ) ;
344313 }
345314
346315 return new Response (
347316 JSON . stringify ( {
348- posts : result . results ,
349- next : result . nextCursor ,
317+ posts : accountPosts . results ,
318+ next : accountPosts . nextCursor ,
350319 } ) ,
351320 { status : 200 } ,
352321 ) ;
@@ -361,7 +330,7 @@ export function createGetAccountPostsHandler(
361330 */
362331export function createGetAccountLikedPostsHandler (
363332 accountService : AccountService ,
364- postService : PostService ,
333+ accountPostsView : AccountPostsView ,
365334) {
366335 /**
367336 * Handle a request for a list of posts liked by an account
@@ -383,7 +352,7 @@ export function createGetAccountLikedPostsHandler(
383352 }
384353
385354 const { results, nextCursor } =
386- await postService . getPostsLikedByAccount (
355+ await accountPostsView . getPostsLikedByAccount (
387356 account . id ,
388357 params . limit ,
389358 params . cursor ,
0 commit comments