Review follow-ups for the combined QA-UI fixes (#216/#206/#204/#218/#192): - export/utils: correct the misleading getInternalLinkPageName comment — a bare `v1.2` loses its last dot-segment (`v1`); dots survive only in multi-segment names like `v1.2.md` -> `v1.2`. - share: extract toPublicSharePayload(page, share): PublicSharePayload, an explicit allowlist type+mapper replacing the inline literal in the /shares/page-info anonymous path (#218). Add share.controller.spec.ts that stubs getSharedPage returning internal fields and asserts the response key set EXACTLY equals the whitelist (page + share), so any `...shareData` regression or new leaking field fails. Also key-tests the extracted mapper. - breadcrumb: extract pure resolveBreadcrumbNodes(treeData, ancestors, pageId) (tree-hit -> tree; tree-miss -> map ancestors via canonical pageToTreeNode, dropping the as-any casts; else null) and unit-test all three branches. - share-modal: RTL test asserting enabling a share calls mutateAsync with includeSubPages: false (#216 security default). - share.service: one-line note at getSharedPage on the deferred consolidation of the ancestor-aware match into resolveReadableSharePage. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
304 lines
8.7 KiB
TypeScript
304 lines
8.7 KiB
TypeScript
import {
|
|
BadRequestException,
|
|
Body,
|
|
Controller,
|
|
ForbiddenException,
|
|
HttpCode,
|
|
HttpStatus,
|
|
Inject,
|
|
NotFoundException,
|
|
Post,
|
|
UseGuards,
|
|
} from '@nestjs/common';
|
|
import { AuthUser } from '../../common/decorators/auth-user.decorator';
|
|
import { User, Workspace } from '@docmost/db/types/entity.types';
|
|
import { AuthWorkspace } from '../../common/decorators/auth-workspace.decorator';
|
|
import { ShareService } from './share.service';
|
|
import {
|
|
CreateShareDto,
|
|
ShareIdDto,
|
|
ShareInfoDto,
|
|
SharePageIdDto,
|
|
UpdateShareDto,
|
|
} from './dto/share.dto';
|
|
import { ShareTransclusionLookupDto } from './dto/share-transclusion-lookup.dto';
|
|
import { PageRepo } from '@docmost/db/repos/page/page.repo';
|
|
import { PagePermissionRepo } from '@docmost/db/repos/page/page-permission.repo';
|
|
import { PageAccessService } from '../page/page-access/page-access.service';
|
|
import { JwtAuthGuard } from '../../common/guards/jwt-auth.guard';
|
|
import { Public } from '../../common/decorators/public.decorator';
|
|
import { ShareRepo } from '@docmost/db/repos/share/share.repo';
|
|
import { PaginationOptions } from '@docmost/db/pagination/pagination-options';
|
|
import { LicenseCheckService } from '../../integrations/environment/license-check.service';
|
|
import { AuditEvent, AuditResource } from '../../common/events/audit-events';
|
|
import {
|
|
AUDIT_SERVICE,
|
|
IAuditService,
|
|
} from '../../integrations/audit/audit.service';
|
|
import { AiSettingsService } from '../../integrations/ai/ai-settings.service';
|
|
import { toPublicSharePayload } from './share-public-payload';
|
|
|
|
@UseGuards(JwtAuthGuard)
|
|
@Controller('shares')
|
|
export class ShareController {
|
|
constructor(
|
|
private readonly shareService: ShareService,
|
|
private readonly shareRepo: ShareRepo,
|
|
private readonly pageRepo: PageRepo,
|
|
private readonly pagePermissionRepo: PagePermissionRepo,
|
|
private readonly pageAccessService: PageAccessService,
|
|
private readonly licenseCheckService: LicenseCheckService,
|
|
private readonly aiSettings: AiSettingsService,
|
|
@Inject(AUDIT_SERVICE) private readonly auditService: IAuditService,
|
|
) {}
|
|
|
|
@HttpCode(HttpStatus.OK)
|
|
@Post('/')
|
|
async getShares(
|
|
@AuthUser() user: User,
|
|
@Body() pagination: PaginationOptions,
|
|
) {
|
|
return this.shareRepo.getShares(user.id, pagination);
|
|
}
|
|
|
|
@Public()
|
|
@HttpCode(HttpStatus.OK)
|
|
@Post('/page-info')
|
|
async getSharedPageInfo(
|
|
@Body() dto: ShareInfoDto,
|
|
@AuthWorkspace() workspace: Workspace,
|
|
) {
|
|
if (!dto.pageId && !dto.shareId) {
|
|
throw new BadRequestException();
|
|
}
|
|
|
|
const shareData = await this.shareService.getSharedPage(dto, workspace.id);
|
|
|
|
const sharingAllowed = await this.shareService.isSharingAllowed(
|
|
workspace.id,
|
|
shareData.share.spaceId,
|
|
);
|
|
if (!sharingAllowed) {
|
|
throw new NotFoundException('Shared page not found');
|
|
}
|
|
|
|
// Surface whether the anonymous public-share AI assistant is enabled, so the
|
|
// client only renders the "Ask AI" widget when the workspace allows it.
|
|
const aiAssistant = await this.aiSettings.isPublicShareAssistantEnabled(
|
|
workspace.id,
|
|
);
|
|
|
|
// Resolve the identity name only when the assistant is enabled, so the
|
|
// anonymous widget can label messages with the configured persona name.
|
|
const aiAssistantName = aiAssistant
|
|
? await this.aiSettings.resolvePublicShareAssistantName(workspace.id)
|
|
: null;
|
|
|
|
// Trim the public payload to the explicit allowlist the anonymous renderer
|
|
// needs (#218); the PublicSharePayload type + mapper guarantee internal
|
|
// metadata can never leak to anonymous viewers (see share-public-payload.ts).
|
|
const { page, share } = shareData;
|
|
|
|
return {
|
|
...toPublicSharePayload(page, share),
|
|
aiAssistant,
|
|
aiAssistantName,
|
|
features: this.licenseCheckService.resolveFeatures(
|
|
workspace.licenseKey,
|
|
workspace.plan,
|
|
),
|
|
};
|
|
}
|
|
|
|
@Public()
|
|
@HttpCode(HttpStatus.OK)
|
|
@Post('/info')
|
|
async getShare(@Body() dto: ShareIdDto) {
|
|
const share = await this.shareRepo.findById(dto.shareId, {
|
|
includeSharedPage: true,
|
|
});
|
|
|
|
if (!share) {
|
|
throw new NotFoundException('Share not found');
|
|
}
|
|
|
|
const sharingAllowed = await this.shareService.isSharingAllowed(
|
|
share.workspaceId,
|
|
share.spaceId,
|
|
);
|
|
if (!sharingAllowed) {
|
|
throw new NotFoundException('Share not found');
|
|
}
|
|
|
|
return share;
|
|
}
|
|
|
|
@Public()
|
|
@HttpCode(HttpStatus.OK)
|
|
@Post('/transclusion/lookup')
|
|
async transclusionLookup(
|
|
@Body() dto: ShareTransclusionLookupDto,
|
|
@AuthWorkspace() workspace: Workspace,
|
|
) {
|
|
return this.shareService.lookupTransclusionForShare(
|
|
dto.shareId,
|
|
dto.references,
|
|
workspace.id,
|
|
);
|
|
}
|
|
|
|
@HttpCode(HttpStatus.OK)
|
|
@Post('/for-page')
|
|
async getShareForPage(
|
|
@Body() dto: SharePageIdDto,
|
|
@AuthUser() user: User,
|
|
@AuthWorkspace() workspace: Workspace,
|
|
) {
|
|
const page = await this.pageRepo.findById(dto.pageId);
|
|
if (!page) {
|
|
throw new NotFoundException('Shared page not found');
|
|
}
|
|
|
|
await this.pageAccessService.validateCanView(page, user);
|
|
|
|
return this.shareService.getShareForPage(page.id, workspace.id);
|
|
}
|
|
|
|
@HttpCode(HttpStatus.OK)
|
|
@Post('create')
|
|
async create(
|
|
@Body() createShareDto: CreateShareDto,
|
|
@AuthUser() user: User,
|
|
@AuthWorkspace() workspace: Workspace,
|
|
) {
|
|
const page = await this.pageRepo.findById(createShareDto.pageId);
|
|
|
|
if (!page || workspace.id !== page.workspaceId) {
|
|
throw new NotFoundException('Page not found');
|
|
}
|
|
|
|
// User must be able to edit the page to create a share
|
|
//TODO: i dont think this is neccessary if we prevent restricted pages from getting shared
|
|
// rather, use space level permission and workspace/space level sharing restriction
|
|
await this.pageAccessService.validateCanEdit(page, user);
|
|
|
|
// Prevent sharing restricted pages
|
|
const isRestricted = await this.pagePermissionRepo.hasRestrictedAncestor(
|
|
page.id,
|
|
);
|
|
if (isRestricted) {
|
|
throw new BadRequestException('Cannot share a restricted page');
|
|
}
|
|
|
|
const sharingAllowed = await this.shareService.isSharingAllowed(
|
|
workspace.id,
|
|
page.spaceId,
|
|
);
|
|
if (!sharingAllowed) {
|
|
throw new ForbiddenException('Public sharing is disabled');
|
|
}
|
|
|
|
const share = await this.shareService.createShare({
|
|
page,
|
|
authUserId: user.id,
|
|
workspaceId: workspace.id,
|
|
createShareDto,
|
|
});
|
|
|
|
this.auditService.log({
|
|
event: AuditEvent.SHARE_CREATED,
|
|
resourceType: AuditResource.SHARE,
|
|
resourceId: share.id,
|
|
spaceId: page.spaceId,
|
|
metadata: {
|
|
pageId: page.id,
|
|
spaceId: page.spaceId,
|
|
},
|
|
});
|
|
|
|
return share;
|
|
}
|
|
|
|
@HttpCode(HttpStatus.OK)
|
|
@Post('update')
|
|
async update(@Body() updateShareDto: UpdateShareDto, @AuthUser() user: User) {
|
|
const share = await this.shareRepo.findById(updateShareDto.shareId);
|
|
|
|
if (!share) {
|
|
throw new NotFoundException('Share not found');
|
|
}
|
|
|
|
const page = await this.pageRepo.findById(share.pageId);
|
|
if (!page) {
|
|
throw new NotFoundException('Page not found');
|
|
}
|
|
|
|
// User must be able to edit the page to update its share
|
|
await this.pageAccessService.validateCanEdit(page, user);
|
|
|
|
return this.shareService.updateShare(share.id, updateShareDto);
|
|
}
|
|
|
|
@HttpCode(HttpStatus.OK)
|
|
@Post('delete')
|
|
async delete(@Body() shareIdDto: ShareIdDto, @AuthUser() user: User) {
|
|
const share = await this.shareRepo.findById(shareIdDto.shareId);
|
|
|
|
if (!share) {
|
|
throw new NotFoundException('Share not found');
|
|
}
|
|
|
|
const page = await this.pageRepo.findById(share.pageId);
|
|
if (!page) {
|
|
throw new NotFoundException('Page not found');
|
|
}
|
|
|
|
// User must be able to edit the page to delete its share
|
|
await this.pageAccessService.validateCanEdit(page, user);
|
|
|
|
await this.shareRepo.deleteShare(share.id);
|
|
|
|
this.auditService.log({
|
|
event: AuditEvent.SHARE_DELETED,
|
|
resourceType: AuditResource.SHARE,
|
|
resourceId: share.id,
|
|
spaceId: share.spaceId,
|
|
changes: {
|
|
before: {
|
|
pageId: share.pageId,
|
|
spaceId: share.spaceId,
|
|
},
|
|
},
|
|
});
|
|
}
|
|
|
|
@Public()
|
|
@HttpCode(HttpStatus.OK)
|
|
@Post('/tree')
|
|
async getSharePageTree(
|
|
@Body() dto: ShareIdDto,
|
|
@AuthWorkspace() workspace: Workspace,
|
|
) {
|
|
const treeData = await this.shareService.getShareTree(
|
|
dto.shareId,
|
|
workspace.id,
|
|
);
|
|
|
|
const sharingAllowed = await this.shareService.isSharingAllowed(
|
|
workspace.id,
|
|
treeData.share.spaceId,
|
|
);
|
|
if (!sharingAllowed) {
|
|
throw new NotFoundException('Share not found');
|
|
}
|
|
|
|
return {
|
|
...treeData,
|
|
features: this.licenseCheckService.resolveFeatures(
|
|
workspace.licenseKey,
|
|
workspace.plan,
|
|
),
|
|
};
|
|
}
|
|
}
|