From edf78f454df6bc66f0aee182b43bf1f3c19d1428 Mon Sep 17 00:00:00 2001 From: Arvin Xu Date: Sun, 15 Dec 2024 00:01:15 +0800 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20refactor:=20refactor=20=20?= =?UTF-8?q?file=20Url=20query=20in=20message=20model=20(#5019)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * ♻️ refactor: refactor the file Url query method * ♻️ refactor: refactor the file Url query method * Delete src/database/queries/fileUrl.ts --- .../server/models/__tests__/message.test.ts | 10 +++++++--- src/database/server/models/message.ts | 13 +++++-------- src/server/routers/lambda/message.ts | 5 +++-- 3 files changed, 15 insertions(+), 13 deletions(-) diff --git a/src/database/server/models/__tests__/message.test.ts b/src/database/server/models/__tests__/message.test.ts index 653a41d9b950..cdbe22dabb00 100644 --- a/src/database/server/models/__tests__/message.test.ts +++ b/src/database/server/models/__tests__/message.test.ts @@ -209,15 +209,19 @@ describe('MessageModel', () => { ]); }); + const domain = 'http://abc.com'; // 调用 query 方法 - const result = await messageModel.query(); + const result = await messageModel.query( + {}, + { postProcessUrl: async (path) => `${domain}/${path}` }, + ); // 断言结果 expect(result).toHaveLength(2); expect(result[0].id).toBe('1'); expect(result[0].imageList).toEqual([ - { alt: 'file-1', id: 'f-0', url: expect.stringContaining('/abc') }, - { alt: 'file-3', id: 'f-3', url: expect.stringContaining('/abc') }, + { alt: 'file-1', id: 'f-0', url: `${domain}/abc` }, + { alt: 'file-3', id: 'f-3', url: `${domain}/abc` }, ]); expect(result[1].id).toBe('2'); diff --git a/src/database/server/models/message.ts b/src/database/server/models/message.ts index f674788beb1e..276761bacdac 100644 --- a/src/database/server/models/message.ts +++ b/src/database/server/models/message.ts @@ -3,7 +3,6 @@ import { and, asc, desc, eq, gte, inArray, isNull, like, lt } from 'drizzle-orm/ import { LobeChatDatabase } from '@/database/type'; import { idGenerator } from '@/database/utils/idGenerator'; -import { getFullFileUrl } from '@/server/utils/files'; import { ChatFileItem, ChatImageItem, @@ -47,12 +46,10 @@ export class MessageModel { } // **************** Query *************** // - async query({ - current = 0, - pageSize = 1000, - sessionId, - topicId, - }: QueryMessageParams = {}): Promise { + async query( + { current = 0, pageSize = 1000, sessionId, topicId }: QueryMessageParams = {}, + options: { postProcessUrl?: (path: string | null) => Promise } = {}, + ): Promise { const offset = current * pageSize; // 1. get basic messages @@ -133,7 +130,7 @@ export class MessageModel { const relatedFileList = await Promise.all( rawRelatedFileList.map(async (file) => ({ ...file, - url: await getFullFileUrl(file.url), + url: options.postProcessUrl ? await options.postProcessUrl(file.url) : (file.url as string), })), ); diff --git a/src/server/routers/lambda/message.ts b/src/server/routers/lambda/message.ts index dcc665c4f786..e8bc58e4cad8 100644 --- a/src/server/routers/lambda/message.ts +++ b/src/server/routers/lambda/message.ts @@ -1,9 +1,10 @@ import { z } from 'zod'; +import { updateMessagePluginSchema } from '@/database/schemas'; import { serverDB } from '@/database/server'; import { MessageModel } from '@/database/server/models/message'; -import { updateMessagePluginSchema } from '@/database/schemas'; import { authedProcedure, publicProcedure, router } from '@/libs/trpc'; +import { getFullFileUrl } from '@/server/utils/files'; import { ChatMessage } from '@/types/message'; import { BatchTaskResult } from '@/types/service'; @@ -70,7 +71,7 @@ export const messageRouter = router({ const messageModel = new MessageModel(serverDB, ctx.userId); - return messageModel.query(input); + return messageModel.query(input, { postProcessUrl: (path) => getFullFileUrl(path) }); }), removeAllMessages: messageProcedure.mutation(async ({ ctx }) => {