Skip to content

Commit

Permalink
♻️ refactor: refactor file Url query in message model (#5019)
Browse files Browse the repository at this point in the history
* ♻️ refactor: refactor the file Url query method

* ♻️ refactor: refactor the file Url query method

* Delete src/database/queries/fileUrl.ts
  • Loading branch information
arvinxx authored Dec 14, 2024
1 parent fef58c5 commit edf78f4
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 13 deletions.
10 changes: 7 additions & 3 deletions src/database/server/models/__tests__/message.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
13 changes: 5 additions & 8 deletions src/database/server/models/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -47,12 +46,10 @@ export class MessageModel {
}

// **************** Query *************** //
async query({
current = 0,
pageSize = 1000,
sessionId,
topicId,
}: QueryMessageParams = {}): Promise<MessageItem[]> {
async query(
{ current = 0, pageSize = 1000, sessionId, topicId }: QueryMessageParams = {},
options: { postProcessUrl?: (path: string | null) => Promise<string> } = {},
): Promise<MessageItem[]> {
const offset = current * pageSize;

// 1. get basic messages
Expand Down Expand Up @@ -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),
})),
);

Expand Down
5 changes: 3 additions & 2 deletions src/server/routers/lambda/message.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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 }) => {
Expand Down

0 comments on commit edf78f4

Please sign in to comment.