Files
outline/server/emails/templates/CommentCreatedEmail.tsx
T
Tom Moor 8b120caccf feat: Add document unsubscribe link to comment emails (#14149)
* feat: Add document unsubscribe link to comment emails

- Comment and resolved emails now link to unsubscribe from the document, above the global unsubscribe link.
- Thread replies and resolve notifications skip users who unsubscribed from the document after their last involvement in the thread.

* refactor: Check only deleted document subscriptions for thread unsubscribe

* fix: Make the document unsubscribe link a document-only opt-out

- The email link no longer deletes the collection subscription, and records or refreshes a document opt-out on every click.
- A document opt-out takes precedence over an older collection subscription.
- Users mentioned in the thread after they opt out get thread notifications again.
2026-11-01 08:05:43 -05:00

226 lines
6.3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import * as React from "react";
import { NotificationEventType } from "@shared/types";
import type { Collection } from "@server/models";
import { Comment, Document } from "@server/models";
import { DocumentHelper } from "@server/models/helpers/DocumentHelper";
import NotificationSettingsHelper from "@server/models/helpers/NotificationSettingsHelper";
import SubscriptionHelper from "@server/models/helpers/SubscriptionHelper";
import { ProsemirrorHelper } from "@server/models/helpers/ProsemirrorHelper";
import { can } from "@server/policies";
import type { EmailProps } from "./BaseEmail";
import BaseEmail, { EmailMessageCategory } from "./BaseEmail";
import Body from "./components/Body";
import Button from "./components/Button";
import Diff from "./components/Diff";
import EmailTemplate from "./components/EmailLayout";
import EmptySpace from "./components/EmptySpace";
import Footer, { Link } from "./components/Footer";
import Header from "./components/Header";
import Heading from "./components/Heading";
const MAX_SUBJECT_CONTENT = 50;
type InputProps = EmailProps & {
userId: string;
documentId: string;
actorName: string;
commentId: string;
teamUrl: string;
};
type BeforeSend = {
comment: Comment;
parentComment?: Comment;
document: Document;
collection: Collection | null;
body: string | undefined;
isReply: boolean;
unsubscribeUrl: string;
};
type Props = InputProps & BeforeSend;
/**
* Email sent to a user when a new comment is created in a document they are
* subscribed to.
*/
export default class CommentCreatedEmail extends BaseEmail<
InputProps,
BeforeSend
> {
protected get category() {
return EmailMessageCategory.Notification;
}
protected async beforeSend(props: InputProps) {
const { documentId, commentId } = props;
const document = await Document.unscoped().findByPk(documentId);
if (!document) {
return false;
}
const [comment, team, collection] = await Promise.all([
Comment.findByPk(commentId),
document.$get("team"),
document.$get("collection"),
]);
if (!comment || !team) {
return false;
}
const parentComment = comment.parentCommentId
? ((await comment.$get("parentComment")) ?? undefined)
: undefined;
const body = await this.htmlForData(
team,
ProsemirrorHelper.toProsemirror(comment.data)
);
const isReply = !!comment.parentCommentId;
return {
comment,
parentComment,
document,
collection,
isReply,
body,
unsubscribeUrl: this.unsubscribeUrl(props),
};
}
protected unsubscribeUrl({ userId }: InputProps) {
return NotificationSettingsHelper.unsubscribeUrl(
userId,
NotificationEventType.CreateComment
);
}
protected subject({ comment, parentComment, document }: Props) {
const commentText = DocumentHelper.toPlainText(
parentComment?.data ?? comment.data
);
const trimmedText =
commentText.length <= MAX_SUBJECT_CONTENT
? commentText
: `${commentText.slice(0, MAX_SUBJECT_CONTENT)}…`;
return `${parentComment ? this.t("Re") + ": " : ""}${this.t(
"New comment on “{{ documentTitle }}” – {{ trimmedText }}",
{ documentTitle: document.displayTitle, trimmedText }
)}`;
}
protected preview({ isReply, actorName }: Props): string {
return isReply
? this.t("{{ actorName }} replied in a thread", { actorName })
: this.t("{{ actorName }} commented on the document", { actorName });
}
protected fromName({ actorName }: Props): string {
return actorName;
}
protected replyTo({ notification }: Props) {
if (notification?.user && notification.actor?.email) {
if (can(notification.user, "readEmail", notification.actor)) {
return notification.actor.email;
}
}
return;
}
protected renderAsText({
actorName,
teamUrl,
isReply,
document,
commentId,
collection,
}: Props): string {
const action = isReply
? this.t("{{ actorName }} replied to a thread in “{{ documentTitle }}”", {
actorName,
documentTitle: document.displayTitle,
})
: this.t("{{ actorName }} commented on “{{ documentTitle }}”", {
actorName,
documentTitle: document.displayTitle,
});
const inCollection = collection?.name
? ` ${this.t("in the {{ collectionName }} collection", { collectionName: collection.name })}`
: "";
return `
${action}${inCollection}.
${this.t("Open Thread")}: ${teamUrl}${document.url}?commentId=${commentId}
`;
}
protected render(props: Props) {
const {
document,
actorName,
isReply,
collection,
teamUrl,
commentId,
unsubscribeUrl,
body,
} = props;
const threadLink = `${teamUrl}${document.url}?commentId=${commentId}&ref=notification-email`;
return (
<EmailTemplate
previewText={this.preview(props)}
goToAction={{ url: threadLink, name: this.t("View Thread") }}
>
<Header />
<Body>
<Heading>{document.displayTitle}</Heading>
<p>
{isReply
? this.t("{{ actorName }} replied to a thread in", { actorName })
: this.t("{{ actorName }} commented on", { actorName })}{" "}
<a href={threadLink}>{document.displayTitle}</a>
{collection?.name
? ` ${this.t("in the {{ collectionName }} collection", {
collectionName: collection.name,
})}`
: ""}
.
</p>
{body && (
<>
<EmptySpace height={20} />
<Diff>
<div dangerouslySetInnerHTML={{ __html: body }} />
</Diff>
<EmptySpace height={20} />
</>
)}
<p>
<Button href={threadLink}>{this.t("Open Thread")}</Button>
</p>
</Body>
<Footer
unsubscribeUrl={unsubscribeUrl}
unsubscribeText={this.t("Unsubscribe from these emails")}
>
<Link
href={SubscriptionHelper.unsubscribeUrl(
props.userId,
props.documentId
)}
>
{this.t("Unsubscribe from this doc")}
</Link>
</Footer>
</EmailTemplate>
);
}
}