-
-
Notifications
You must be signed in to change notification settings - Fork 28
Refactored account retrieval to use AccountView
#505
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
aebe9fc
Refactored account retrieval to use `AccountView`
mike182uk b153afa
Injected AccountView instead of instantiating it
mike182uk e67df95
Returned 404 when Account cannot be retrieved
mike182uk 731f134
Used site default account for context instead of site
mike182uk ddca23d
404'd if actor cannot be resolved from fediverse
mike182uk 35a88dd
Misc refactoring
mike182uk fe3d513
Ensure custom fields are populated
mike182uk 54555d6
Merge branch 'main' into refactor-get-account-to-use-view
mike182uk a54b9f1
Fix failing test
mike182uk 4e57973
Merge branch 'main' into refactor-get-account-to-use-view
mike182uk 0a05e1b
Ensured requested rejected stepdef can only be used for client errors
mike182uk 556b54a
Merge branch 'main' into refactor-get-account-to-use-view
mike182uk b931ec9
Merge branch 'main' into refactor-get-account-to-use-view
mike182uk f275ae0
Added integration tests
mike182uk f5f5617
Merge branch 'main' into refactor-get-account-to-use-view
mike182uk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
src/http/api/__snapshots__/views/AccountView.viewByApId.internal-no-context.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| { | ||
| "attachment": [], | ||
| "avatarUrl": "Test site", | ||
| "bannerImageUrl": "", | ||
| "bio": "Test site", | ||
| "customFields": {}, | ||
| "followedByMe": false, | ||
| "followerCount": 0, | ||
| "followingCount": 0, | ||
| "followsMe": false, | ||
| "handle": "@index@example.com", | ||
| "id": 1, | ||
| "likedCount": 0, | ||
| "name": "Test site", | ||
| "postCount": 0, | ||
| "url": "https://example.com", | ||
| } |
17 changes: 17 additions & 0 deletions
17
src/http/api/__snapshots__/views/AccountView.viewById.no-context.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| { | ||
| "attachment": [], | ||
| "avatarUrl": "Test site", | ||
| "bannerImageUrl": "", | ||
| "bio": "Test site", | ||
| "customFields": {}, | ||
| "followedByMe": false, | ||
| "followerCount": 0, | ||
| "followingCount": 0, | ||
| "followsMe": false, | ||
| "handle": "@index@example.com", | ||
| "id": 1, | ||
| "likedCount": 0, | ||
| "name": "Test site", | ||
| "postCount": 0, | ||
| "url": "https://example.com", | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,17 +1,12 @@ | ||
| import type { Federation } from '@fedify/fedify'; | ||
| import type { Account, PersistedAccount } from 'account/account.entity'; | ||
| import type { PersistedAccount } from 'account/account.entity'; | ||
| import type { KnexAccountRepository } from 'account/account.repository.knex'; | ||
| import type { AccountService } from 'account/account.service'; | ||
| import type { FedifyContextFactory } from 'activitypub/fedify-context.factory'; | ||
| import type { AppContext, ContextData } from 'app'; | ||
| import type { AppContext } from 'app'; | ||
| import { exhaustiveCheck, getError, getValue, isError } from 'core/result'; | ||
| import { isHandle } from 'helpers/activitypub/actor'; | ||
| import { lookupAPIdByHandle } from 'lookup-helpers'; | ||
| import { z } from 'zod'; | ||
| import { | ||
| getAccountDTOByHandle, | ||
| getAccountDTOFromAccount, | ||
| } from './helpers/account'; | ||
| import type { AccountDTO } from './types'; | ||
| import type { | ||
| AccountFollows, | ||
|
|
@@ -21,6 +16,8 @@ import type { | |
| AccountPosts, | ||
| AccountPostsView, | ||
| } from './views/account.posts.view'; | ||
| import type { AccountView } from './views/account.view'; | ||
|
|
||
| /** | ||
| * Default number of posts to return in a profile | ||
| */ | ||
|
|
@@ -31,78 +28,53 @@ const DEFAULT_POSTS_LIMIT = 20; | |
| */ | ||
| const MAX_POSTS_LIMIT = 100; | ||
|
|
||
| /** | ||
| * Keyword to indicate a request is for the current user | ||
| */ | ||
| const CURRENT_USER_KEYWORD = 'me'; | ||
|
|
||
| /** | ||
| * Create a handler to handle a request for an account | ||
| * | ||
| * @param accountService Account service instance | ||
| */ | ||
| export function createGetAccountHandler( | ||
| accountService: AccountService, | ||
| accountView: AccountView, | ||
| accountRepository: KnexAccountRepository, | ||
| fedify: Federation<ContextData>, | ||
| ) { | ||
| /** | ||
| * Handle a request for an account | ||
| * | ||
| * @param ctx App context | ||
| */ | ||
| return async function handleGetAccount(ctx: AppContext) { | ||
| const logger = ctx.get('logger'); | ||
| const site = ctx.get('site'); | ||
| let account: Account | null = null; | ||
| const db = ctx.get('db'); | ||
| const handle = ctx.req.param('handle'); | ||
|
|
||
| const apCtx = fedify.createContext(ctx.req.raw as Request, { | ||
| db, | ||
| globaldb: ctx.get('globaldb'), | ||
| logger, | ||
| }); | ||
| if (handle !== CURRENT_USER_KEYWORD && !isHandle(handle)) { | ||
| return new Response(null, { status: 404 }); | ||
| } | ||
|
|
||
| const defaultAccount = await accountRepository.getBySite( | ||
| const siteDefaultAccount = await accountRepository.getBySite( | ||
| ctx.get('site'), | ||
| ); | ||
|
|
||
| const handle = ctx.req.param('handle'); | ||
| // We are using the keyword 'me', if we want to get the account of teh current user | ||
| if (handle === 'me') { | ||
| account = defaultAccount; | ||
| } else { | ||
| if (!isHandle(handle)) { | ||
| return new Response(null, { status: 404 }); | ||
| } | ||
| let accountDto: AccountDTO | null = null; | ||
|
|
||
| const apId = await lookupAPIdByHandle(apCtx, handle); | ||
| if (apId) { | ||
| account = await accountRepository.getByApId(new URL(apId)); | ||
| } | ||
| } | ||
|
|
||
| let accountDto: AccountDTO; | ||
| const viewContext = { | ||
| requestUserAccount: siteDefaultAccount, | ||
| }; | ||
|
|
||
| try { | ||
| //If we found the account in our db and it's an internal account, do an internal lookup | ||
| if (account?.isInternal) { | ||
| accountDto = await getAccountDTOFromAccount( | ||
| account, | ||
| defaultAccount, | ||
| accountService, | ||
| ); | ||
| } else { | ||
| //Otherwise, do a remote lookup to fetch the updated data | ||
| accountDto = await getAccountDTOByHandle( | ||
| handle, | ||
| apCtx, | ||
| site, | ||
| accountService, | ||
| ); | ||
| } | ||
| } catch (error) { | ||
| logger.error('Error getting account: {error}', { error }); | ||
| if (handle === CURRENT_USER_KEYWORD) { | ||
| accountDto = await accountView.viewById( | ||
| siteDefaultAccount.id!, | ||
| viewContext, | ||
| ); | ||
| } else { | ||
| accountDto = await accountView.viewByHandle(handle, viewContext); | ||
| } | ||
|
|
||
| return new Response(null, { status: 500 }); | ||
| if (accountDto === null) { | ||
| return new Response(null, { status: 404 }); | ||
| } | ||
|
|
||
| // Return response | ||
| return new Response(JSON.stringify(accountDto), { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't check for the
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. updated in e67df95 |
||
| headers: { | ||
| 'Content-Type': 'application/json', | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😬 Yeah I think we should never have an explicit 500 check here.
I think we should update that stepdef to throw an error if a 5xx error is passed as the expected status code - what do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeh I don't think it makes sense to expect an internal server error 😅 I'll add a check into the stepdef 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated in 0a05e1b