Skip to content

Commit

Permalink
fix: fix the fallback avatar to just serve the default static avatar.
Browse files Browse the repository at this point in the history
  • Loading branch information
zicklag committed Dec 20, 2024
1 parent 14821d1 commit e904f67
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 22 deletions.
12 changes: 5 additions & 7 deletions src/lib/image.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { base32Encode, type ExactLink } from 'leaf-proto';
import { leafClient } from './leaf';
import { RawImage } from 'leaf-proto/components';
import { env } from '$env/dynamic/public';

/**
* Formats an image response from the `RawImage` component of the provided entity link, optionally
Expand All @@ -15,15 +14,14 @@ import { env } from '$env/dynamic/public';
* */
export async function createImageResponse(
link: ExactLink,
fetch?: typeof globalThis.fetch,
fallbackAvatarSeed?: string
url?: URL,
fetch?: typeof globalThis.fetch
): Promise<Response> {
let fallbackAvatar = fallbackAvatarSeed
? `${env.PUBLIC_DICEBEAR_URL}/8.x/${env.PUBLIC_DICEBEAR_STYLE}/svg?seed=${fallbackAvatarSeed}`
: undefined;
const fallbackAvatar = url && new URL(url);
if (fallbackAvatar) fallbackAvatar.pathname = '/default-avatar.svg';
const ent = await leafClient.get_components(link, RawImage);
const image = ent?.get(RawImage)?.value;
if (!ent || !image) {
if (!image) {
if (fallbackAvatar && fetch) {
return fetch(fallbackAvatar);
} else {
Expand Down
9 changes: 2 additions & 7 deletions src/routes/(app)/[username]/avatar/+server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,12 @@ import { createImageResponse } from '$lib/image';
import { profileLinkByUsername } from '$lib/leaf/profile';
import { error, type RequestHandler } from '@sveltejs/kit';

export const GET: RequestHandler = async ({ fetch, params }) => {
export const GET: RequestHandler = async ({ fetch, params, url }) => {
const username = params.username!.includes('.')
? params.username!
: `${params.username}.${env.PUBLIC_USER_DOMAIN_PARENT}`;

const profileLink = await profileLinkByUsername(username);
if (!profileLink) return error(404, 'Avatar not found');
return await createImageResponse(profileLink, fetch, params.username);
};

export const fallback: RequestHandler = async ({ request }) => {
console.error('AVATAR REQ FALLBACK:', request);
return new Response(null, { status: 400, statusText: 'Method not supported' });
return await createImageResponse(profileLink, url, fetch);
};
10 changes: 2 additions & 8 deletions src/routes/(subsites)/subsite/[username]/avatar/+server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,8 @@ import { createImageResponse } from '$lib/image';
import { profileLinkByUsername } from '$lib/leaf/profile';
import { error, type RequestHandler } from '@sveltejs/kit';

export const GET: RequestHandler = async ({ fetch, params }) => {
export const GET: RequestHandler = async ({ fetch, params, url }) => {
const profileLink = await profileLinkByUsername(params.username!);
if (!profileLink) return error(404, 'Avatar not found');
return await createImageResponse(profileLink, fetch, params.username);
return await createImageResponse(profileLink, url, fetch);
};

export const fallback: RequestHandler = async ({ request }) => {
console.error('AVATAR REQ FALLBACK:', request);
return new Response(null, { status: 400, statusText: 'Method not supported' });
};

0 comments on commit e904f67

Please sign in to comment.