import { Controller, Get, Param, Req } from '@nestjs/common'; import type { OutlineEntry } from '@dorfteich/shared'; import type { PluginPageContent, PluginPageMeta, PluginPageSummary } from '@dorfteich/shared'; import { AuthedRequest } from '../auth/auth.guard'; import { RequiresPagePermission, RequiresPondRole } from '../permissions/permission.decorators'; import { PagesService } from './pages.service'; /** * Viewer-scoped plugin data API (ADR 0008, issue #74). These endpoints back * the plugin SDK's `readCurrentPage` and `readPond` capabilities: the host * bridge calls them with the viewing user's session, and each reuses the same * page/pond permission guards as the rest of the app — a plugin can never read * more than the person looking at it (no parallel permission logic). The * `readBlock`/`blockData` capabilities land with the `plugin_block` node in * issue #76, where block addressing exists. */ @Controller('plugin') export class PluginApiController { constructor(private readonly pages: PagesService) {} /** `readPond.listPages` — the pages of a pond the viewer may read, with * label names for pageTool filtering (issue #77). */ @Get('ponds/:pondId/pages') @RequiresPondRole('reader', { idParam: 'pondId' }) // the service filters per page listPages( @Param('pondId') pondId: string, @Req() request: AuthedRequest, ): Promise { return this.pages.pluginPageSummaries(request.user!, pondId); } /** `readCurrentPage.getOutline` / `readPond.getPageOutline`. */ @Get('pages/:pageId/outline') @RequiresPagePermission('read', { idParam: 'pageId' }) outline(@Param('pageId') pageId: string): Promise { return this.pages.outline(pageId); } /** `readCurrentPage.getContent` / `readPond.getPageContent` (Markdown). */ @Get('pages/:pageId/content') @RequiresPagePermission('read', { idParam: 'pageId' }) async content( @Param('pageId') pageId: string, @Req() request: AuthedRequest, ): Promise { const { markdown } = await this.pages.exportMarkdown(request.user!, pageId); return { markdown }; } /** `readCurrentPage.getMeta`. */ @Get('pages/:pageId/meta') @RequiresPagePermission('read', { idParam: 'pageId' }) meta(@Param('pageId') pageId: string): Promise { return this.pages.meta(pageId); } }