Skip to content

There can be more than Notion and Miro. AFFiNE(pronounced [ə‘fain]) is a next-gen knowledge base that brings planning, sorting and creating all together. Privacy first, open-source, customizable and ready to use.

feat(server): add full content support for doc (#11149)

darkskygit314e5795ebbf
diff --git a/packages/backend/server/src/core/doc-service/__tests__/controller.spec.ts b/packages/backend/server/src/core/doc-service/__tests__/controller.spec.ts
index b56a006c8a..b9923d4a52 100644
--- a/packages/backend/server/src/core/doc-service/__tests__/controller.spec.ts
+++ b/packages/backend/server/src/core/doc-service/__tests__/controller.spec.ts
@@ -183,6 +183,27 @@ test('should get doc content in json format', async t => {
t.pass();
});
+test('should get full doc content in json format', async t => {
+ const { app } = t.context;
+ mock.method(t.context.databaseDocReader, 'getFullDocContent', async () => {
+ return {
+ title: 'test title',
+ summary: 'test summary',
+ };
+ });
+
+ const docId = randomUUID();
+ await app
+ .GET(`/rpc/workspaces/${workspace.id}/docs/${docId}/content?full=true`)
+ .set('x-access-token', t.context.crypto.sign(docId))
+ .expect({
+ title: 'test title',
+ summary: 'test summary',
+ })
+ .expect(200);
+ t.pass();
+});
+
test('should 404 when workspace content not found', async t => {
const { app } = t.context;
diff --git a/packages/backend/server/src/core/doc-service/controller.ts b/packages/backend/server/src/core/doc-service/controller.ts
index 9bf3d9aa75..09a2db7322 100644
--- a/packages/backend/server/src/core/doc-service/controller.ts
+++ b/packages/backend/server/src/core/doc-service/controller.ts
@@ -7,6 +7,7 @@ import {
RawBody,
Res,
} from '@nestjs/common';
+import { Args } from '@nestjs/graphql';
import type { Response } from 'express';
import { NotFound, SkipThrottle } from '../../base';
@@ -76,9 +77,12 @@ export class DocRpcController {
@Get('/workspaces/:workspaceId/docs/:docId/content')
async getDocContent(
@Param('workspaceId') workspaceId: string,
- @Param('docId') docId: string
+ @Param('docId') docId: string,
+ @Args('full', { nullable: true }) fullContent?: boolean
) {
- const content = await this.docReader.getDocContent(workspaceId, docId);
+ const content = fullContent
+ ? await this.docReader.getFullDocContent(workspaceId, docId)
+ : await this.docReader.getDocContent(workspaceId, docId);
if (!content) {
throw new NotFound('Doc not found');
}
diff --git a/packages/backend/server/src/core/doc/reader.ts b/packages/backend/server/src/core/doc/reader.ts
index 50326fcab7..d35fe7cd8b 100644
--- a/packages/backend/server/src/core/doc/reader.ts
+++ b/packages/backend/server/src/core/doc/reader.ts
@@ -347,9 +347,10 @@ export class RpcDocReader extends DatabaseDocReader {
protected override async getDocContentWithoutCache(
workspaceId: string,
- docId: string
+ docId: string,
+ fullContent = false
): Promise<PageDocContent | null> {
- const url = `${this.config.docService.endpoint}/rpc/workspaces/${workspaceId}/docs/${docId}/content`;
+ const url = `${this.config.docService.endpoint}/rpc/workspaces/${workspaceId}/docs/${docId}/content?full=${fullContent}`;
const accessToken = this.crypto.sign(docId);
try {
const res = await this.fetch(accessToken, url, 'GET');
@@ -366,7 +367,11 @@ export class RpcDocReader extends DatabaseDocReader {
`Failed to fetch doc content ${url}, fallback to database doc reader`,
err
);
- return await super.getDocContentWithoutCache(workspaceId, docId);
+ return await super.getDocContentWithoutCache(
+ workspaceId,
+ docId,
+ fullContent
+ );
}
}