-
Notifications
You must be signed in to change notification settings - Fork 264
feat(web): add /api/avatar resolver #1159
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
4 commits
Select commit
Hold shift + click to select a range
95fcd1a
feat(web): add /api/avatar resolver and use it in UserAvatar
brendan-kellam 614800a
chore(web): add CHANGELOG entry for /api/avatar resolver
brendan-kellam 9cd3080
chore(web): validate /api/avatar query params with Zod
brendan-kellam 4c2e9ed
feedback
brendan-kellam 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| 'use server'; | ||
|
|
||
| import { minidenticon } from 'minidenticons'; | ||
| import { NextRequest } from 'next/server'; | ||
| import { z } from 'zod'; | ||
| import { apiHandler } from '@/lib/apiHandler'; | ||
| import { queryParamsSchemaValidationError, serviceErrorResponse } from '@/lib/serviceError'; | ||
| import { isServiceError } from '@/lib/utils'; | ||
| import { withOptionalAuth } from '@/middleware/withAuth'; | ||
|
|
||
| const queryParamsSchema = z.object({ | ||
| email: z.string().min(1), | ||
| }); | ||
|
|
||
| // Resolves an email to an avatar image. If the email belongs to a Sourcebot | ||
| // user in the requester's org and that user has a profile image set, the | ||
| // request is redirected to that URL. Otherwise a minidenticon SVG is returned. | ||
| // | ||
| // We never 4xx on this endpoint — even if the requester is unauthenticated or | ||
| // the user isn't found, we serve the identicon so the avatar visually renders. | ||
| export const GET = apiHandler(async (request: NextRequest) => { | ||
| const rawParams = Object.fromEntries( | ||
| Object.keys(queryParamsSchema.shape).map(key => [ | ||
| key, | ||
| request.nextUrl.searchParams.get(key) ?? undefined, | ||
| ]) | ||
| ); | ||
| const parsed = queryParamsSchema.safeParse(rawParams); | ||
|
|
||
| if (!parsed.success) { | ||
| return serviceErrorResponse( | ||
| queryParamsSchemaValidationError(parsed.error) | ||
| ); | ||
| } | ||
|
|
||
| const { email } = parsed.data; | ||
|
|
||
| const lookup = await withOptionalAuth(async ({ org, prisma }) => { | ||
| return prisma.user.findFirst({ | ||
| where: { | ||
| email, | ||
| orgs: { some: { orgId: org.id } }, | ||
| }, | ||
| select: { image: true }, | ||
| }); | ||
| }); | ||
|
|
||
| if (!isServiceError(lookup) && lookup?.image) { | ||
| return new Response(null, { | ||
| status: 302, | ||
| headers: { | ||
| 'Location': lookup.image, | ||
| 'Cache-Control': 'public, max-age=300', | ||
| }, | ||
| }); | ||
| } | ||
|
|
||
| // Fallback: identicon. Cache lifetime matches the redirect path so the | ||
| // response naturally revalidates as users sign up, set profile pictures, | ||
| // or transient lookup errors recover. | ||
| const svg = minidenticon(email, 50, 50); | ||
| return new Response(svg, { | ||
| status: 200, | ||
| headers: { | ||
| 'Content-Type': 'image/svg+xml', | ||
| 'Cache-Control': 'public, max-age=300', | ||
| }, | ||
| }); | ||
| }, { track: false }); | ||
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
39 changes: 0 additions & 39 deletions
39
packages/web/src/features/chat/components/chatThread/messageAvatar.tsx
This file was deleted.
Oops, something went wrong.
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.