Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.
feat spam protection: added option to enable/disable email notifications
Vinod Palec39c6b340b3
diff --git a/components/emails/viewed-document.tsx b/components/emails/viewed-document.tsxindex 850e5c66..5be62a51 100644--- a/components/emails/viewed-document.tsx+++ b/components/emails/viewed-document.tsx@@ -77,6 +77,10 @@ export default function ViewedDocument({If you have any feedback or questions about this email, simplyreply to it. I'd love to hear from you!</Text>+ <Text className="text-xs">+ To stop email notifications for this link views, edit the link+ and uncheck "Receive email notification".+ </Text></Section></Container></Body>diff --git a/components/links/link-sheet/allow-notification.tsx b/components/links/link-sheet/allow-notification.tsxnew file mode 100644index 00000000..833bdae6--- /dev/null+++ b/components/links/link-sheet/allow-notification.tsx@@ -0,0 +1,43 @@+import { Dispatch, SetStateAction, useState, useEffect } from "react";+import { Switch } from "@/components/ui/switch";+import { cn } from "@/lib/utils";+import { DEFAULT_LINK_TYPE } from ".";++export default function AllowNotificationSection({+ data,+ setData,+}: {+ data: DEFAULT_LINK_TYPE;+ setData: Dispatch<SetStateAction<DEFAULT_LINK_TYPE>>;+}) {+ const { enableNotification } = data;+ const [enabled, setEnabled] = useState<boolean>(true);++ useEffect(() => {+ setEnabled(enableNotification);+ }, [enableNotification]);++ const handleEnableNotification = () => {+ const updatedEnableNotification = !enabled;+ setData({ ...data, enableNotification: updatedEnableNotification });+ setEnabled(updatedEnableNotification);+ };++ return (+ <div className="pb-5">+ <div className="flex items-center justify-between">+ <div className="flex items-center justify-between space-x-2">+ <h2+ className={cn(+ "text-sm font-medium leading-6",+ enabled ? "text-foreground" : "text-muted-foreground"+ )}+ >+ Receive email notification+ </h2>+ </div>+ <Switch checked={enabled} onCheckedChange={handleEnableNotification} />+ </div>+ </div>+ );+}diff --git a/components/links/link-sheet/index.tsx b/components/links/link-sheet/index.tsxindex 1ce5a9bf..6aa0fc08 100644--- a/components/links/link-sheet/index.tsx+++ b/components/links/link-sheet/index.tsx@@ -23,6 +23,7 @@ import { UpgradePlanModal } from "@/components/billing/upgrade-plan-modal";import Link from "next/link";import DomainSection from "./domain-section";import AllowDownloadSection from "./allow-download-section";+import AllowNotificationSection from "./allow-notification";export const DEFAULT_LINK_PROPS = {id: null,@@ -33,6 +34,7 @@ export const DEFAULT_LINK_PROPS = {password: null,emailProtected: true,allowDownload: false,+ enableNotification: true,};export type DEFAULT_LINK_TYPE = {@@ -44,6 +46,7 @@ export type DEFAULT_LINK_TYPE = {password: string | null;emailProtected: boolean;allowDownload: boolean;+ enableNotification: boolean;};export default function LinkSheet({@@ -182,6 +185,7 @@ export default function LinkSheet({<AllowDownloadSection {...{ data, setData }} /><PasswordSection {...{ data, setData }} /><ExpirationSection {...{ data, setData }} />+ <AllowNotificationSection {...{ data, setData }} /></div></div></div>diff --git a/components/links/links-table.tsx b/components/links/links-table.tsxindex 8b482c2d..ce90b801 100644--- a/components/links/links-table.tsx+++ b/components/links/links-table.tsx@@ -42,7 +42,7 @@ import { usePlan } from "@/lib/swr/use-billing";export default function LinksTable() {const { links } = useDocumentLinks();const router = useRouter();- const { plan } = usePlan()+ const { plan } = usePlan();const [isLoading, setIsLoading] = useState<boolean>(false);const [isLinkSheetVisible, setIsLinkSheetVisible] = useState<boolean>(false);@@ -63,6 +63,9 @@ export default function LinksTable() {password: link.password,emailProtected: link.emailProtected,allowDownload: link.allowDownload ? link.allowDownload : false,+ enableNotification: link.enableNotification+ ? link.enableNotification+ : false,});//wait for dropdown to close before opening the link sheetsetTimeout(() => {@@ -112,7 +115,7 @@ export default function LinksTable() {? links.filter((link) => link.isArchived).length: 0;- const hasFreePlan = plan && plan.plan === "free"+ const hasFreePlan = plan && plan.plan === "free";return (<>diff --git a/pages/api/links/[id]/index.ts b/pages/api/links/[id]/index.tsindex c33e2907..f91ecbd4 100644--- a/pages/api/links/[id]/index.ts+++ b/pages/api/links/[id]/index.ts@@ -115,6 +115,7 @@ export default async function handle(domainId: domainObj?.id || null,domainSlug: domain || null,slug: slug || null,+ enableNotification: linkData.enableNotification,},include: {views: {diff --git a/pages/api/links/index.ts b/pages/api/links/index.tsindex 76b6a0bf..d80b2048 100644--- a/pages/api/links/index.ts+++ b/pages/api/links/index.ts@@ -73,6 +73,7 @@ export default async function handler(domainId: domainObj?.id || null,domainSlug: domain || null,slug: slug || null,+ enableNotification: linkData.enableNotification,},});diff --git a/pages/api/views.ts b/pages/api/views.tsindex 7f84b3a6..17f46b03 100644--- a/pages/api/views.ts+++ b/pages/api/views.ts@@ -26,6 +26,7 @@ export default async function handle(},select: {emailProtected: true,+ enableNotification: true,password: true,},});@@ -97,15 +98,16 @@ export default async function handle(viewerEmail: email,});-// TODO: this can be offloaded to a background job in the future to save some time- // send email to document owner that document has been viewed- await sendViewedDocumentEmail(- newView.document.owner.email as string,- documentId,- newView.document.name,- );+ // send email to document owner that document has been viewed if user has not disabled notifications+ if (link.enableNotification) {+ await sendViewedDocumentEmail(+ newView.document.owner.email as string,+ documentId,+ newView.document.name,+ );+ }// check if document version has multiple pages, if so, return the pagesif (newView.document.versions[0].hasPages) {@@ -122,19 +124,20 @@ export default async function handle(},});- return res- .status(200)- .json({ message: "View recorded", viewId: newView.id, file: null, pages: pages });- }-- return res- .status(200)- .json({+ return res.status(200).json({message: "View recorded",viewId: newView.id,- file: newView.document.versions[0].file,- pages: null,+ file: null,+ pages: pages,});+ }++ return res.status(200).json({+ message: "View recorded",+ viewId: newView.id,+ file: newView.document.versions[0].file,+ pages: null,+ });} catch (error) {log(`Failed to record view for ${linkId}. Error: \n\n ${error}`);return res.status(500).json({ message: (error as Error).message });diff --git a/prisma/migrations/202310311254_NewColumnEnableNotificationLinkTable/migration.sql b/prisma/migrations/202310311254_NewColumnEnableNotificationLinkTable/migration.sqlnew file mode 100644index 00000000..7b78530d--- /dev/null+++ b/prisma/migrations/202310311254_NewColumnEnableNotificationLinkTable/migration.sql@@ -0,0 +1,3 @@+-- AlterTable+ALTER TABLE "Link" ADD COLUMN "enableNotification" BOOLEAN DEFAULT true;+diff --git a/prisma/schema.prisma b/prisma/schema.prismaindex 22054389..5bd43fad 100644--- a/prisma/schema.prisma+++ b/prisma/schema.prisma@@ -108,24 +108,25 @@ model DocumentPage {}model Link {- id String @id @default(cuid())- document Document @relation(fields: [documentId], references: [id], onDelete: Cascade)- documentId String- url String? @unique- name String? // Link name- slug String? // Link slug for pretty URLs- expiresAt DateTime? // Optional expiration date- password String? // Optional password for link protection- allowedEmails String[] // Array of emails allowed to view the document- emailProtected Boolean @default(true) // Optional email protection- allowDownload Boolean? @default(false) // Optional give user a option to allow to download the document- isArchived Boolean @default(false) // Indicates if the link is archived- views View[]- domain Domain? @relation(fields: [domainId], references: [id], onDelete: SetNull)- domainId String? // This can be nullable, representing links without custom domains- domainSlug String? // This will store the domain's slug even if the domain is deleted- createdAt DateTime @default(now())- updatedAt DateTime @updatedAt+ id String @id @default(cuid())+ document Document @relation(fields: [documentId], references: [id], onDelete: Cascade)+ documentId String+ url String? @unique+ name String? // Link name+ slug String? // Link slug for pretty URLs+ expiresAt DateTime? // Optional expiration date+ password String? // Optional password for link protection+ allowedEmails String[] // Array of emails allowed to view the document+ emailProtected Boolean @default(true) // Optional email protection+ allowDownload Boolean? @default(false) // Optional give user a option to allow to download the document+ isArchived Boolean @default(false) // Indicates if the link is archived+ views View[]+ domain Domain? @relation(fields: [domainId], references: [id], onDelete: SetNull)+ domainId String? // This can be nullable, representing links without custom domains+ domainSlug String? // This will store the domain's slug even if the domain is deleted+ createdAt DateTime @default(now())+ updatedAt DateTime @updatedAt+ enableNotification Boolean? @default(true) // Optional give user a option to pause/resume the notifications@@unique([domainSlug, slug])}