From 02e845ef711a8938dbd2c7a850e750a5dba9441c Mon Sep 17 00:00:00 2001 From: yiling Date: Sat, 6 Jun 2026 12:25:07 +0800 Subject: [PATCH] feat(backend): generate presigned avatar URLs for collection and share link creators --- packages/api/src/api/collection.ts | 18 +++++++++--------- packages/core/src/share/share.ts | 16 ++++++++-------- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/packages/api/src/api/collection.ts b/packages/api/src/api/collection.ts index dcb002f4..c0d97ef5 100644 --- a/packages/api/src/api/collection.ts +++ b/packages/api/src/api/collection.ts @@ -2,6 +2,7 @@ import { zValidator } from '@hono/zod-validator' import { Hono } from 'hono' import { collectionService } from '@shumai/core/src/collection/collection' import { authzService, Permission, ResourceType } from '@shumai/core/src/authz/authz' +import { getAvatarUrl } from '@shumai/core/src/user/avatar' import { createCollectionRequestSchema, updateCollectionRequestSchema, @@ -13,9 +14,10 @@ import type { Prisma } from '@shumai/db' type User = Prisma.UserGetPayload> -function toCollectionInfo( +async function toCollectionInfo( c: Prisma.CollectionGetPayload<{ include: { creator: true } }>, -): CollectionInfo { +): Promise { + const avatarUrl = c.creator ? await getAvatarUrl(c.creator.image) : undefined return { id: c.id, name: c.name, @@ -23,9 +25,7 @@ function toCollectionInfo( projectId: c.projectId, createdAt: c.createdAt.toISOString(), updatedAt: c.updatedAt.toISOString(), - creator: c.creator - ? { id: c.creator.id, name: c.creator.name, image: c.creator.image ?? undefined } - : null, + creator: c.creator ? { id: c.creator.id, name: c.creator.name, image: avatarUrl } : null, } } @@ -46,7 +46,7 @@ const route = new Hono<{ Variables: { user: User } }>() }) const collection = await collectionService.createCollection(projectId, req, user.id) - return c.json(toCollectionInfo(collection)) + return c.json(await toCollectionInfo(collection)) }, ) .get( @@ -67,7 +67,7 @@ const route = new Hono<{ Variables: { user: User } }>() const collections = await collectionService.listCollections(projectId, req) return c.json({ // eslint-disable-next-line @typescript-eslint/no-explicit-any - data: collections.data.map((c: any) => toCollectionInfo(c)), + data: await Promise.all(collections.data.map((c: any) => toCollectionInfo(c))), pageInfo: collections.pageInfo, }) }, @@ -88,7 +88,7 @@ const route = new Hono<{ Variables: { user: User } }>() return c.json({ error: 'Collection not found' }, 404) } - return c.json(toCollectionInfo(collection)) + return c.json(await toCollectionInfo(collection)) }) .patch( '/collections/:collectionId', @@ -106,7 +106,7 @@ const route = new Hono<{ Variables: { user: User } }>() }) const collection = await collectionService.updateCollection(collectionId, req) - return c.json(toCollectionInfo(collection)) + return c.json(await toCollectionInfo(collection)) }, ) .delete('/collections/:collectionId', async (c) => { diff --git a/packages/core/src/share/share.ts b/packages/core/src/share/share.ts index 182a104d..57c08815 100644 --- a/packages/core/src/share/share.ts +++ b/packages/core/src/share/share.ts @@ -8,6 +8,7 @@ import { AddAssetToShareRequest, } from '@shumai/dtos' import { PaginatedData, paginateQuery } from '@shumai/core/src/pagination' +import { getAvatarUrl } from '@shumai/core/src/user/avatar' export class ShareService { async createShareLink( @@ -64,7 +65,7 @@ export class ShareService { include: { creator: true }, }) - return this.toShareLinkInfo(shareLink) + return await this.toShareLinkInfo(shareLink) }) } @@ -95,7 +96,7 @@ export class ShareService { }) } - return this.toShareLinkInfo(updated) + return await this.toShareLinkInfo(updated) } async deleteShareLink(shareLinkId: string): Promise { @@ -128,7 +129,7 @@ export class ShareService { take, orderBy: { createdAt: 'desc' }, }) - return links.map((l) => this.toShareLinkInfo(l)) + return await Promise.all(links.map((l) => this.toShareLinkInfo(l))) }, () => prisma.shareLink.count({ where }), req, @@ -141,7 +142,7 @@ export class ShareService { include: { creator: true }, }) if (!shareLink) throw new Error('Share link not found') - return this.toShareLinkInfo(shareLink) + return await this.toShareLinkInfo(shareLink) } async addAssetToShare(shareLinkId: string, req: AddAssetToShareRequest): Promise { @@ -289,8 +290,9 @@ export class ShareService { } // eslint-disable-next-line @typescript-eslint/no-explicit-any - private toShareLinkInfo(l: any): ShareLinkInfo { + private async toShareLinkInfo(l: any): Promise { const isExpired = l.expireAt ? new Date(l.expireAt) < new Date() : false + const avatarUrl = l.creator ? await getAvatarUrl(l.creator.image) : undefined return { id: l.id, name: l.name, @@ -304,9 +306,7 @@ export class ShareService { isExpired, createdAt: l.createdAt.toISOString(), updatedAt: l.updatedAt.toISOString(), - creator: l.creator - ? { id: l.creator.id, name: l.creator.name, image: l.creator.image ?? undefined } - : null, + creator: l.creator ? { id: l.creator.id, name: l.creator.name, image: avatarUrl } : null, } } }