Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.
feat: improve annotations
Marc Seitz9c21b6e28803
diff --git a/components/documents/annotations/annotation-form.tsx b/components/documents/annotations/annotation-form.tsxindex c61a0b6a..3498a324 100644--- a/components/documents/annotations/annotation-form.tsx+++ b/components/documents/annotations/annotation-form.tsx@@ -54,7 +54,7 @@ export function AnnotationForm({}: AnnotationFormProps) {const [isLoading, setIsLoading] = useState(false);const [editorContent, setEditorContent] = useState(- annotation?.content || null,+ annotation?.content || { type: "doc", content: [] },);const form = useForm<FormValues>({diff --git a/pages/api/links/[id]/annotations.ts b/pages/api/links/[id]/annotations.tsindex 4215a05d..b2ba99fd 100644--- a/pages/api/links/[id]/annotations.ts+++ b/pages/api/links/[id]/annotations.ts@@ -45,7 +45,7 @@ export default async function handle(return res.status(404).json({ error: "Annotation not found" });}- if (view.viewedAt > new Date(Date.now() - 1000 * 60 * 60 * 23)) {+ if (view.viewedAt < new Date(Date.now() - 1000 * 60 * 60 * 23)) {// if view is older than 23 hours, we should not allow the annotations to be accessedreturn res.status(404).json({ error: "Annotation not found" });}diff --git a/pages/api/links/[id]/documents/[documentId]/annotations.ts b/pages/api/links/[id]/documents/[documentId]/annotations.tsindex 06b88c1a..549c1848 100644--- a/pages/api/links/[id]/documents/[documentId]/annotations.ts+++ b/pages/api/links/[id]/documents/[documentId]/annotations.ts@@ -27,13 +27,30 @@ export default async function handle(try {const view = await prisma.view.findUnique({where: { id: viewId, linkId: linkId },- select: { id: true, link: true },+ select: {+ id: true,+ viewedAt: true,+ link: {+ select: {+ id: true,+ linkType: true,+ teamId: true,+ documentId: true,+ dataroomId: true,+ },+ },+ },});if (!view) {return res.status(404).json({ error: "View not found" });}+ // Check TTL - deny access for views older than 23 hours+ if (view.viewedAt < new Date(Date.now() - 23 * 60 * 60 * 1000)) {+ return res.status(403).json({ error: "Access denied" });+ }+// Check if annotations feature is enabled for this teamconst featureFlags = await getFeatureFlags({teamId: view.link.teamId || undefined,diff --git a/pages/api/teams/[teamId]/documents/[id]/annotations/[annotationId].ts b/pages/api/teams/[teamId]/documents/[id]/annotations/[annotationId].tsindex 31106de2..0973edd5 100644--- a/pages/api/teams/[teamId]/documents/[id]/annotations/[annotationId].ts+++ b/pages/api/teams/[teamId]/documents/[id]/annotations/[annotationId].ts@@ -69,7 +69,7 @@ export default async function handle(if (req.method === "GET") {// GET /api/teams/:teamId/documents/:id/annotations/:annotationIdconst fullAnnotation = await prisma.documentAnnotation.findUnique({- where: { id: annotationId },+ where: { id: annotationId, documentId: docId, teamId },include: {images: true,createdBy: {@@ -88,7 +88,7 @@ export default async function handle(const validatedData = updateAnnotationSchema.parse(req.body);const updatedAnnotation = await prisma.documentAnnotation.update({- where: { id: annotationId },+ where: { id: annotationId, documentId: docId, teamId },data: validatedData,include: {images: true,@@ -106,7 +106,7 @@ export default async function handle(} else if (req.method === "DELETE") {// DELETE /api/teams/:teamId/documents/:id/annotations/:annotationIdawait prisma.documentAnnotation.delete({- where: { id: annotationId },+ where: { id: annotationId, documentId: docId, teamId },});return res.status(204).end();diff --git a/pages/api/teams/[teamId]/documents/[id]/annotations/index.ts b/pages/api/teams/[teamId]/documents/[id]/annotations/index.tsindex 6d7f34d5..ca4794b9 100644--- a/pages/api/teams/[teamId]/documents/[id]/annotations/index.ts+++ b/pages/api/teams/[teamId]/documents/[id]/annotations/index.ts@@ -15,9 +15,9 @@ const createAnnotationSchema = z.object({.string().min(1, "Title is required").max(100, "Title must be less than 100 characters"),- content: z.record(z.any()), // Rich text content as JSON+ content: z.record(z.any()).nullable().optional(), // Rich text content as JSON - allow null/omittedpages: z- .array(z.number().min(1))+ .array(z.number().int().min(1)).min(1, "At least one page must be selected"),isVisible: z.boolean().default(true),});@@ -39,33 +39,18 @@ export default async function handle(const userId = (session.user as CustomUser).id;try {- const { document } = await getTeamWithUsersAndDocument({- teamId,- userId,- docId,- options: {- include: {- annotations: {- include: {- images: true,- createdBy: {- select: {- id: true,- name: true,- email: true,- },- },- },- orderBy: {- createdAt: "desc",- },- },- },+ // Validate access; avoid heavy includes+ await getTeamWithUsersAndDocument({ teamId, userId, docId });+ const annotations = await prisma.documentAnnotation.findMany({+ where: { documentId: docId, teamId },+ include: {+ images: true,+ createdBy: { select: { id: true, name: true, email: true } },},+ orderBy: { createdAt: "desc" },});- // @ts-ignore- return res.status(200).json(document?.annotations || []);+ return res.status(200).json(annotations);} catch (error) {log({message: `Failed to get annotations for document: _${docId}_. \n\n ${error} \n\n*Metadata*: \`{teamId: ${teamId}, userId: ${userId}}\``,