mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-19 15:26:59 +08:00
feat(server): add full content support for doc (#11149)
This commit is contained in:
@@ -183,6 +183,27 @@ test('should get doc content in json format', async t => {
|
|||||||
t.pass();
|
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 => {
|
test('should 404 when workspace content not found', async t => {
|
||||||
const { app } = t.context;
|
const { app } = t.context;
|
||||||
|
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import {
|
|||||||
RawBody,
|
RawBody,
|
||||||
Res,
|
Res,
|
||||||
} from '@nestjs/common';
|
} from '@nestjs/common';
|
||||||
|
import { Args } from '@nestjs/graphql';
|
||||||
import type { Response } from 'express';
|
import type { Response } from 'express';
|
||||||
|
|
||||||
import { NotFound, SkipThrottle } from '../../base';
|
import { NotFound, SkipThrottle } from '../../base';
|
||||||
@@ -76,9 +77,12 @@ export class DocRpcController {
|
|||||||
@Get('/workspaces/:workspaceId/docs/:docId/content')
|
@Get('/workspaces/:workspaceId/docs/:docId/content')
|
||||||
async getDocContent(
|
async getDocContent(
|
||||||
@Param('workspaceId') workspaceId: string,
|
@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) {
|
if (!content) {
|
||||||
throw new NotFound('Doc not found');
|
throw new NotFound('Doc not found');
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -347,9 +347,10 @@ export class RpcDocReader extends DatabaseDocReader {
|
|||||||
|
|
||||||
protected override async getDocContentWithoutCache(
|
protected override async getDocContentWithoutCache(
|
||||||
workspaceId: string,
|
workspaceId: string,
|
||||||
docId: string
|
docId: string,
|
||||||
|
fullContent = false
|
||||||
): Promise<PageDocContent | null> {
|
): 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);
|
const accessToken = this.crypto.sign(docId);
|
||||||
try {
|
try {
|
||||||
const res = await this.fetch(accessToken, url, 'GET');
|
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`,
|
`Failed to fetch doc content ${url}, fallback to database doc reader`,
|
||||||
err
|
err
|
||||||
);
|
);
|
||||||
return await super.getDocContentWithoutCache(workspaceId, docId);
|
return await super.getDocContentWithoutCache(
|
||||||
|
workspaceId,
|
||||||
|
docId,
|
||||||
|
fullContent
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user