- {actor.name} is requesting access to the{" "} - {document.displayTitle} document. + {this.t("{{ actorName }} is requesting access to the", { + actorName: actor.name, + })}{" "} + {document.displayTitle}{" "} + {this.t("document")}.
-Open the document to share it with them.
+{this.t("Open the document to share it with them")}.
- +
diff --git a/server/emails/templates/NotificationEmails.test.ts b/server/emails/templates/NotificationEmails.test.ts new file mode 100644 index 0000000000..d29f7e0a72 --- /dev/null +++ b/server/emails/templates/NotificationEmails.test.ts @@ -0,0 +1,167 @@ +import { renderToStaticMarkup } from "react-dom/server"; +import { CollectionPermission, DocumentPermission } from "@shared/types"; +import mailer from "@server/emails/mailer"; +import { UserMembership } from "@server/models"; +import { + buildCollection, + buildDocument, + buildGroup, + buildUser, +} from "@server/test/factories"; +import CollectionSharedEmail from "./CollectionSharedEmail"; +import DocumentAccessRequestEmail from "./DocumentAccessRequestEmail"; +import DocumentMentionedEmail from "./DocumentMentionedEmail"; +import DocumentSharedEmail from "./DocumentSharedEmail"; +import GroupDocumentMentionedEmail from "./GroupDocumentMentionedEmail"; + +const teamUrl = "https://example.outline.dev"; + +describe("notification emails", () => { + beforeEach(() => { + vi.spyOn(mailer, "sendMail").mockResolvedValue(); + }); + + afterEach(() => { + vi.restoreAllMocks(); + }); + + const expectUnsubscribe = (eventType: string, userId: string) => { + expect(mailer.sendMail).toHaveBeenCalledTimes(1); + const options = vi.mocked(mailer.sendMail).mock.calls[0][0]; + expect(options.unsubscribeUrl).toContain(`userId=${userId}`); + expect(options.unsubscribeUrl).toContain(`eventType=${eventType}`); + }; + + it("should include an unsubscribe link in DocumentMentionedEmail", async () => { + const user = await buildUser(); + const document = await buildDocument({ teamId: user.teamId }); + + await new DocumentMentionedEmail({ + to: user.email, + userId: user.id, + documentId: document.id, + revisionId: undefined, + actorName: "Actor", + teamUrl, + }).send(); + + expectUnsubscribe("documents.mentioned", user.id); + }); + + it("should include an unsubscribe link in GroupDocumentMentionedEmail", async () => { + const user = await buildUser(); + const group = await buildGroup({ teamId: user.teamId }); + const document = await buildDocument({ teamId: user.teamId }); + + await new GroupDocumentMentionedEmail({ + to: user.email, + userId: user.id, + documentId: document.id, + revisionId: undefined, + groupId: group.id, + actorName: "Actor", + teamUrl, + }).send(); + + expectUnsubscribe("documents.group_mentioned", user.id); + }); + + it("should include an unsubscribe link in DocumentSharedEmail", async () => { + const user = await buildUser(); + const document = await buildDocument({ teamId: user.teamId }); + const membership = await UserMembership.create({ + userId: user.id, + documentId: document.id, + createdById: document.createdById, + permission: DocumentPermission.Read, + }); + + await new DocumentSharedEmail({ + to: user.email, + userId: user.id, + documentId: document.id, + membershipId: membership.id, + actorName: "Actor", + teamUrl, + }).send(); + + expectUnsubscribe("documents.add_user", user.id); + }); + + it("should include an unsubscribe link in CollectionSharedEmail", async () => { + const user = await buildUser(); + const collection = await buildCollection({ teamId: user.teamId }); + await UserMembership.create({ + userId: user.id, + collectionId: collection.id, + createdById: collection.createdById, + permission: CollectionPermission.Read, + }); + + await new CollectionSharedEmail({ + to: user.email, + userId: user.id, + collectionId: collection.id, + actorName: "Actor", + teamUrl, + }).send(); + + expectUnsubscribe("collections.add_user", user.id); + }); + + it("should include an unsubscribe link in DocumentAccessRequestEmail", async () => { + const user = await buildUser(); + const actor = await buildUser({ teamId: user.teamId }); + const document = await buildDocument({ teamId: user.teamId }); + + await new DocumentAccessRequestEmail({ + to: user.email, + userId: user.id, + documentId: document.id, + actorId: actor.id, + teamUrl, + }).send(); + + expectUnsubscribe("access_requests.create", user.id); + }); + + it("should set the language and direction for RTL languages", async () => { + const user = await buildUser({ language: "he_IL" }); + const actor = await buildUser({ teamId: user.teamId }); + const document = await buildDocument({ teamId: user.teamId }); + + await new DocumentAccessRequestEmail({ + to: user.email, + language: user.language, + userId: user.id, + documentId: document.id, + actorId: actor.id, + teamUrl, + }).send(); + + const options = vi.mocked(mailer.sendMail).mock.calls[0][0]; + expect(options.lang).toEqual("he-IL"); + expect(options.dir).toEqual("rtl"); + expect(renderToStaticMarkup(options.component)).toContain('align="right"'); + }); + + it("should set the language and direction for LTR languages", async () => { + const user = await buildUser({ language: "en_US" }); + const document = await buildDocument({ teamId: user.teamId }); + + await new DocumentMentionedEmail({ + to: user.email, + language: user.language, + userId: user.id, + documentId: document.id, + revisionId: undefined, + actorName: "Actor", + teamUrl, + }).send(); + + const options = vi.mocked(mailer.sendMail).mock.calls[0][0]; + expect(options.lang).toEqual("en-US"); + expect(options.dir).toEqual("ltr"); + expect(renderToStaticMarkup(options.component)).toContain('align="left"'); + }); +}); diff --git a/server/emails/templates/components/Direction.tsx b/server/emails/templates/components/Direction.tsx new file mode 100644 index 0000000000..daa1e5d2a7 --- /dev/null +++ b/server/emails/templates/components/Direction.tsx @@ -0,0 +1,4 @@ +import * as React from "react"; + +/** The text direction of the email that is being rendered. */ +export const DirectionContext = React.createContext<"ltr" | "rtl">("ltr"); diff --git a/server/emails/templates/components/EmailLayout.tsx b/server/emails/templates/components/EmailLayout.tsx index e8e57c6b1d..dbc3070570 100644 --- a/server/emails/templates/components/EmailLayout.tsx +++ b/server/emails/templates/components/EmailLayout.tsx @@ -1,6 +1,7 @@ import { Table, TBody, TR, TD } from "oy-vey"; import * as React from "react"; import theme from "@shared/styles/theme"; +import { DirectionContext } from "./Direction"; const EmailLayout: React.FC< React.PropsWithChildren<{ @@ -9,6 +10,7 @@ const EmailLayout: React.FC< goToAction?: { url: string; name: string }; }> > = ({ previewText, bgcolor = "#FFFFFF", goToAction, children }) => { + const dir = React.useContext(DirectionContext); let markup; if (goToAction) { markup = JSON.stringify({ @@ -60,7 +62,7 @@ const EmailLayout: React.FC<| {children} | +{children} |