|
| 1 | +import { Controller, Get, Param, Res } from '@nestjs/common'; |
| 2 | +import type { Response } from 'express'; |
| 3 | +import xss from 'xss'; |
| 4 | + |
| 5 | +import { DocNotFound } from '../../fundamentals'; |
| 6 | +import { PermissionService } from '../permission'; |
| 7 | +import { PageDocContent } from '../utils/blocksuite'; |
| 8 | +import { DocContentService } from './service'; |
| 9 | + |
| 10 | +interface RenderOptions { |
| 11 | + og: boolean; |
| 12 | + content: boolean; |
| 13 | +} |
| 14 | + |
| 15 | +@Controller('/workspace/:workspaceId/:docId') |
| 16 | +export class DocRendererController { |
| 17 | + constructor( |
| 18 | + private readonly doc: DocContentService, |
| 19 | + private readonly permission: PermissionService |
| 20 | + ) {} |
| 21 | + |
| 22 | + @Get() |
| 23 | + async render( |
| 24 | + @Res() res: Response, |
| 25 | + @Param('workspaceId') workspaceId: string, |
| 26 | + @Param('docId') docId: string |
| 27 | + ) { |
| 28 | + if (workspaceId === docId) { |
| 29 | + throw new DocNotFound({ spaceId: workspaceId, docId }); |
| 30 | + } |
| 31 | + |
| 32 | + // if page is public, show all |
| 33 | + // if page is private, but workspace public og is on, show og but not content |
| 34 | + const opts: RenderOptions = { |
| 35 | + og: false, |
| 36 | + content: false, |
| 37 | + }; |
| 38 | + const isPagePublic = await this.permission.isPublicPage(workspaceId, docId); |
| 39 | + |
| 40 | + if (isPagePublic) { |
| 41 | + opts.og = true; |
| 42 | + opts.content = true; |
| 43 | + } else { |
| 44 | + const allowPreview = await this.permission.allowUrlPreview(workspaceId); |
| 45 | + |
| 46 | + if (allowPreview) { |
| 47 | + opts.og = true; |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + let docContent = opts.og |
| 52 | + ? await this.doc.getPageContent(workspaceId, docId) |
| 53 | + : null; |
| 54 | + if (!docContent) { |
| 55 | + docContent = { title: 'untitled', summary: '' }; |
| 56 | + } |
| 57 | + |
| 58 | + res.setHeader('Content-Type', 'text/html'); |
| 59 | + if (!opts.og) { |
| 60 | + res.setHeader('X-Robots-Tag', 'noindex'); |
| 61 | + } |
| 62 | + res.send(this._render(docContent, opts)); |
| 63 | + } |
| 64 | + |
| 65 | + _render(doc: PageDocContent, { og }: RenderOptions): string { |
| 66 | + const title = xss(doc.title); |
| 67 | + const summary = xss(doc.summary); |
| 68 | + |
| 69 | + return ` |
| 70 | + <!DOCTYPE html> |
| 71 | + <html> |
| 72 | + <head> |
| 73 | + <title>${title} | AFFiNE</title> |
| 74 | + <meta name="theme-color" content="#fafafa" /> |
| 75 | + <link rel="manifest" href="/manifest.json" /> |
| 76 | + <link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png" /> |
| 77 | + <link rel="icon" sizes="192x192" href="/favicon-192.png" /> |
| 78 | + ${!og ? '<meta name="robots" content="noindex, nofollow" />' : ''} |
| 79 | + <meta |
| 80 | + name="twitter:title" |
| 81 | + content="AFFiNE: There can be more than Notion and Miro." |
| 82 | + /> |
| 83 | + <meta name="twitter:description" content="${title}" /> |
| 84 | + <meta name="twitter:site" content="@AffineOfficial" /> |
| 85 | + <meta name="twitter:image" content="https://affine.pro/og.jpeg" /> |
| 86 | + <meta property="og:title" content="${title}" /> |
| 87 | + <meta property="og:description" content="${summary}" /> |
| 88 | + <meta property="og:image" content="https://affine.pro/og.jpeg" /> |
| 89 | + </head> |
| 90 | + <body> |
| 91 | + </body> |
| 92 | + </html> |
| 93 | + `; |
| 94 | + } |
| 95 | +} |
0 commit comments