Comment notification emails (#4978)

* Comment notification emails

* fix links
fix threading in email inboxes
from is now commenter name

* fix

* refactor

* fix async filter
This commit is contained in:
Tom Moor
2023-03-05 11:01:56 -05:00
committed by GitHub
parent 4ff0fdfb4f
commit 760355302c
10 changed files with 599 additions and 236 deletions

View File

@@ -4,25 +4,17 @@ import {
} from "@getoutline/y-prosemirror";
import { JSDOM } from "jsdom";
import { escapeRegExp, startCase } from "lodash";
import { Node, DOMSerializer } from "prosemirror-model";
import * as React from "react";
import { renderToString } from "react-dom/server";
import styled, { ServerStyleSheet, ThemeProvider } from "styled-components";
import { Node } from "prosemirror-model";
import * as Y from "yjs";
import EditorContainer from "@shared/editor/components/Styles";
import textBetween from "@shared/editor/lib/textBetween";
import GlobalStyles from "@shared/styles/globals";
import light from "@shared/styles/theme";
import {
getCurrentDateAsString,
getCurrentDateTimeAsString,
getCurrentTimeAsString,
unicodeCLDRtoBCP47,
} from "@shared/utils/date";
import { isRTL } from "@shared/utils/rtl";
import unescape from "@shared/utils/unescape";
import { parser, schema } from "@server/editor";
import Logger from "@server/logging/Logger";
import { trace } from "@server/logging/tracing";
import type Document from "@server/models/Document";
import type Revision from "@server/models/Revision";
@@ -31,6 +23,7 @@ import diff from "@server/utils/diff";
import parseAttachmentIds from "@server/utils/parseAttachmentIds";
import { getSignedUrl } from "@server/utils/s3";
import Attachment from "../Attachment";
import ProsemirrorHelper from "./ProsemirrorHelper";
type HTMLOptions = {
/** Whether to include the document title in the generated HTML (defaults to true) */
@@ -39,8 +32,11 @@ type HTMLOptions = {
includeStyles?: boolean;
/** Whether to include styles to center diff (defaults to true) */
centered?: boolean;
/** Whether to replace attachment urls with pre-signed versions (defaults to false) */
signedUrls?: boolean;
/**
* Whether to replace attachment urls with pre-signed versions. If set to a
* number then the urls will be signed for that many seconds. (defaults to false)
*/
signedUrls?: boolean | number;
};
@trace()
@@ -106,87 +102,17 @@ export default class DocumentHelper {
*/
static async toHTML(document: Document | Revision, options?: HTMLOptions) {
const node = DocumentHelper.toProsemirror(document);
const sheet = new ServerStyleSheet();
let html, styleTags;
const Centered = options?.centered
? styled.article`
max-width: 46em;
margin: 0 auto;
padding: 0 1em;
`
: "article";
const rtl = isRTL(document.title);
const content = <div id="content" className="ProseMirror"></div>;
const children = (
<>
{options?.includeTitle !== false && (
<h1 dir={rtl ? "rtl" : "ltr"}>{document.title}</h1>
)}
{options?.includeStyles !== false ? (
<EditorContainer dir={rtl ? "rtl" : "ltr"} rtl={rtl}>
{content}
</EditorContainer>
) : (
content
)}
</>
);
// First render the containing document which has all the editor styles,
// global styles, layout and title.
try {
html = renderToString(
sheet.collectStyles(
<ThemeProvider theme={light}>
<>
{options?.includeStyles === false ? (
<article>{children}</article>
) : (
<>
<GlobalStyles />
<Centered>{children}</Centered>
</>
)}
</>
</ThemeProvider>
)
);
styleTags = sheet.getStyleTags();
} catch (error) {
Logger.error("Failed to render styles on document export", error, {
id: document.id,
});
} finally {
sheet.seal();
}
// Render the Prosemirror document using virtual DOM and serialize the
// result to a string
const dom = new JSDOM(
`<!DOCTYPE html>${
options?.includeStyles === false ? "" : styleTags
}${html}`
);
const doc = dom.window.document;
const target = doc.getElementById("content");
DOMSerializer.fromSchema(schema).serializeFragment(
node.content,
{
document: doc,
},
// @ts-expect-error incorrect library type, third argument is target node
target
);
let output = dom.serialize();
let output = ProsemirrorHelper.toHTML(node, {
title: options?.includeTitle !== false ? document.title : undefined,
includeStyles: options?.includeStyles,
centered: options?.centered,
});
if (options?.signedUrls && "teamId" in document) {
output = await DocumentHelper.attachmentsToSignedUrls(
output,
document.teamId
document.teamId,
typeof options.signedUrls === "number" ? options.signedUrls : undefined
);
}

View File

@@ -0,0 +1,142 @@
import { uniqBy } from "lodash";
import { Op } from "sequelize";
import {
Document,
Collection,
NotificationSetting,
Subscription,
Comment,
} from "@server/models";
export default class NotificationHelper {
/**
* Get the recipients of a notification for a collection event.
*
* @param collection The collection to get recipients for
* @param eventName The event name
* @returns A list of recipients
*/
public static getCollectionNotificationRecipients = async (
collection: Collection,
eventName: string
): Promise<NotificationSetting[]> => {
// First find all the users that have notifications enabled for this event
// type at all and aren't the one that performed the action.
const recipients = await NotificationSetting.scope("withUser").findAll({
where: {
userId: {
[Op.ne]: collection.createdById,
},
teamId: collection.teamId,
event: eventName,
},
});
// Ensure we only have one recipient per user as a safety measure
return uniqBy(recipients, "userId");
};
/**
* Get the recipients of a notification for a comment event.
*
* @param document The document associated with the comment
* @param comment The comment to get recipients for
* @param eventName The event name
* @returns A list of recipients
*/
public static getCommentNotificationRecipients = async (
document: Document,
comment: Comment,
actorId: string
): Promise<NotificationSetting[]> => {
const recipients = await this.getDocumentNotificationRecipients(
document,
"documents.update",
actorId
);
if (recipients.length > 0 && comment.parentCommentId) {
const contextComments = await Comment.findAll({
attributes: ["createdById"],
where: {
[Op.or]: [
{ id: comment.parentCommentId },
{ parentCommentId: comment.parentCommentId },
],
},
});
const userIdsInThread = contextComments.map((c) => c.createdById);
return recipients.filter((r) => userIdsInThread.includes(r.userId));
}
return recipients;
};
/**
* Get the recipients of a notification for a document event.
*
* @param document The document to get recipients for
* @param eventName The event name
* @param actorId The id of the user that performed the action
* @returns A list of recipients
*/
public static getDocumentNotificationRecipients = async (
document: Document,
eventName: string,
actorId: string
): Promise<NotificationSetting[]> => {
// First find all the users that have notifications enabled for this event
// type at all and aren't the one that performed the action.
let recipients = await NotificationSetting.scope("withUser").findAll({
where: {
userId: {
[Op.ne]: actorId,
},
teamId: document.teamId,
event: eventName,
},
});
// If the event is a revision creation we can filter further to only those
// that have a subscription to the document…
if (eventName === "documents.update") {
const subscriptions = await Subscription.findAll({
attributes: ["userId"],
where: {
userId: recipients.map((recipient) => recipient.user.id),
documentId: document.id,
event: eventName,
},
});
const subscribedUserIds = subscriptions.map(
(subscription) => subscription.userId
);
recipients = recipients.filter((recipient) =>
subscribedUserIds.includes(recipient.user.id)
);
}
const filtered = [];
for (const recipient of recipients) {
const collectionIds = await recipient.user.collectionIds();
// Check the recipient has access to the collection this document is in. Just
// because they are subscribed doesn't meant they still have access to read
// the document.
if (
recipient.user.email &&
!recipient.user.isSuspended &&
collectionIds.includes(document.collectionId)
) {
filtered.push(recipient);
}
}
// Ensure we only have one recipient per user as a safety measure
return uniqBy(filtered, "userId");
};
}

View File

@@ -0,0 +1,107 @@
import { JSDOM } from "jsdom";
import { Node, DOMSerializer } from "prosemirror-model";
import * as React from "react";
import { renderToString } from "react-dom/server";
import styled, { ServerStyleSheet, ThemeProvider } from "styled-components";
import EditorContainer from "@shared/editor/components/Styles";
import GlobalStyles from "@shared/styles/globals";
import light from "@shared/styles/theme";
import { isRTL } from "@shared/utils/rtl";
import { schema } from "@server/editor";
import Logger from "@server/logging/Logger";
import { trace } from "@server/logging/tracing";
export type HTMLOptions = {
/** A title, if it should be included */
title?: string;
/** Whether to include style tags in the generated HTML (defaults to true) */
includeStyles?: boolean;
/** Whether to include styles to center diff (defaults to true) */
centered?: boolean;
};
@trace()
export default class ProsemirrorHelper {
/**
* Returns the node as HTML. This is a lossy conversion and should only be used
* for export.
*
* @param node The node to convert to HTML
* @param options Options for the HTML output
* @returns The content as a HTML string
*/
static toHTML(node: Node, options?: HTMLOptions) {
const sheet = new ServerStyleSheet();
let html, styleTags;
const Centered = options?.centered
? styled.article`
max-width: 46em;
margin: 0 auto;
padding: 0 1em;
`
: "article";
const rtl = isRTL(node.textContent);
const content = <div id="content" className="ProseMirror"></div>;
const children = (
<>
{options?.title && <h1 dir={rtl ? "rtl" : "ltr"}>{options.title}</h1>}
{options?.includeStyles !== false ? (
<EditorContainer dir={rtl ? "rtl" : "ltr"} rtl={rtl}>
{content}
</EditorContainer>
) : (
content
)}
</>
);
// First render the containing document which has all the editor styles,
// global styles, layout and title.
try {
html = renderToString(
sheet.collectStyles(
<ThemeProvider theme={light}>
<>
{options?.includeStyles === false ? (
<article>{children}</article>
) : (
<>
<GlobalStyles />
<Centered>{children}</Centered>
</>
)}
</>
</ThemeProvider>
)
);
styleTags = sheet.getStyleTags();
} catch (error) {
Logger.error("Failed to render styles on node HTML conversion", error);
} finally {
sheet.seal();
}
// Render the Prosemirror document using virtual DOM and serialize the
// result to a string
const dom = new JSDOM(
`<!DOCTYPE html>${
options?.includeStyles === false ? "" : styleTags
}${html}`
);
const doc = dom.window.document;
const target = doc.getElementById("content");
DOMSerializer.fromSchema(schema).serializeFragment(
node.content,
{
document: doc,
},
// @ts-expect-error incorrect library type, third argument is target node
target
);
return dom.serialize();
}
}