Skip to content

Conversation

@mfts
Copy link
Owner

@mfts mfts commented Oct 7, 2025

Summary by CodeRabbit

  • Bug Fixes
    • Watermark toggle now reliably preserves your on/off choice and existing settings, avoiding unintended resets.
    • Creating or updating links with watermarking now validates settings and provides clear error messages if configuration is missing or invalid, reducing failed saves.
    • Watermarks are applied to downloads only when valid settings are present, preventing unexpected watermarking and reducing processing errors for PDFs.
    • Overall improves reliability of watermark-enabled links and ensures more predictable download behavior.

@vercel
Copy link

vercel bot commented Oct 7, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Preview Comments Updated (UTC)
papermark Ready Ready Preview Comment Oct 7, 2025 7:31pm

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Oct 7, 2025

Walkthrough

Adds runtime validation for watermark configuration on link creation and update APIs, adjusts download logic to require watermarkConfig for PDF watermarking, and updates the UI toggle to preserve and set watermarkConfig based on current state and initial config.

Changes

Cohort / File(s) Summary
UI watermark toggle logic
components/links/link-sheet/watermark-section.tsx
Repositions and revises handleWatermarkToggle: when enabling, preserves existing watermarkConfig or falls back to initial config; when disabling, clears watermarkConfig; synchronizes enableWatermark and watermarkConfig in component state/data.
API validation for create/update
pages/api/links/index.ts, pages/api/links/[id]/index.ts
Adds server-side checks: if enableWatermark is true, watermarkConfig must be present and pass WatermarkConfigSchema.safeParse; otherwise respond 400 with detailed messages. Imports WatermarkConfigSchema. Validation occurs before DB update/creation.
Download watermark gating
pages/api/links/download/dataroom-document.ts
Tightens condition to apply watermarking to PDFs: requires watermarkConfig to be truthy before processing and including watermark payload.

Possibly related PRs

Pre-merge checks

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title Check ✅ Passed The title succinctly describes the main purpose of the changeset by indicating a fix for the scenario where watermarking could be enabled without providing a configuration, aligning directly with the added validation logic and toggle behavior updates in the code.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

♻️ Duplicate comments (1)
pages/api/links/[id]/index.ts (1)

277-297: Validation logic is correct but duplicated.

This validation block is identical to the one in pages/api/links/index.ts (lines 126-146). See the refactoring suggestion in that file's review to extract this into a shared helper function.

🧹 Nitpick comments (1)
pages/api/links/index.ts (1)

126-146: Consider extracting duplicated validation logic.

The watermark validation logic is sound and provides clear error messages. However, this exact validation block is duplicated in pages/api/links/[id]/index.ts (lines 277-297).

Consider extracting this into a reusable helper function:

Create a new file lib/validations/watermark.ts:

import { WatermarkConfigSchema } from "@/lib/types";

export function validateWatermarkConfig(
  enableWatermark: boolean,
  watermarkConfig: any
): { valid: boolean; error?: string; details?: string } {
  if (!enableWatermark) {
    return { valid: true };
  }

  if (!watermarkConfig) {
    return {
      valid: false,
      error: "Watermark configuration is required when watermark is enabled.",
    };
  }

  const validation = WatermarkConfigSchema.safeParse(watermarkConfig);
  if (!validation.success) {
    return {
      valid: false,
      error: "Invalid watermark configuration.",
      details: validation.error.issues
        .map((issue) => issue.message)
        .join(", "),
    };
  }

  return { valid: true };
}

Then use it in both endpoints:

+import { validateWatermarkConfig } from "@/lib/validations/watermark";

-      if (linkData.enableWatermark) {
-        if (!linkData.watermarkConfig) {
-          return res.status(400).json({
-            error:
-              "Watermark configuration is required when watermark is enabled.",
-          });
-        }
-
-        // Validate the watermark config structure
-        const validation = WatermarkConfigSchema.safeParse(
-          linkData.watermarkConfig,
-        );
-        if (!validation.success) {
-          return res.status(400).json({
-            error: "Invalid watermark configuration.",
-            details: validation.error.issues
-              .map((issue) => issue.message)
-              .join(", "),
-          });
-        }
-      }
+      const watermarkValidation = validateWatermarkConfig(
+        linkData.enableWatermark,
+        linkData.watermarkConfig
+      );
+      if (!watermarkValidation.valid) {
+        return res.status(400).json({
+          error: watermarkValidation.error,
+          ...(watermarkValidation.details && {
+            details: watermarkValidation.details,
+          }),
+        });
+      }
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 1beaf98 and fbb6d84.

📒 Files selected for processing (4)
  • components/links/link-sheet/watermark-section.tsx (1 hunks)
  • pages/api/links/[id]/index.ts (2 hunks)
  • pages/api/links/download/dataroom-document.ts (1 hunks)
  • pages/api/links/index.ts (2 hunks)
🧰 Additional context used
🧬 Code graph analysis (2)
pages/api/links/index.ts (1)
lib/types.ts (1)
  • WatermarkConfigSchema (334-360)
pages/api/links/[id]/index.ts (1)
lib/types.ts (1)
  • WatermarkConfigSchema (334-360)
🔇 Additional comments (4)
pages/api/links/download/dataroom-document.ts (1)

200-204: LGTM! Watermark gating now requires valid configuration.

The added view.link.watermarkConfig check ensures watermarking only occurs when a valid configuration is present, aligning with the validation logic added to the API endpoints. This prevents attempts to watermark PDFs without proper configuration.

pages/api/links/index.ts (1)

9-9: LGTM! Appropriate import for watermark validation.

The WatermarkConfigSchema import enables runtime validation of watermark configuration.

pages/api/links/[id]/index.ts (1)

11-11: LGTM! Appropriate import for watermark validation.

The WatermarkConfigSchema import enables runtime validation of watermark configuration in the PUT endpoint.

components/links/link-sheet/watermark-section.tsx (1)

68-79: LGTM! Toggle logic correctly preserves watermark configuration.

The updated toggle logic properly handles watermark state:

  • When enabling: preserves existing watermarkConfig if present, otherwise falls back to initialconfig
  • When disabling: sets watermarkConfig to null

This ensures the frontend state aligns with the backend validation requirements, where watermarkConfig must be present when enableWatermark is true.

@mfts mfts merged commit 390e165 into main Oct 7, 2025
9 checks passed
@github-actions github-actions bot locked and limited conversation to collaborators Oct 7, 2025
@mfts mfts deleted the fix/watermark-config branch November 19, 2025 11:47
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants