diff --git a/server/emails/mailer.tsx b/server/emails/mailer.tsx index 20adfa4089..ec88f1b901 100644 --- a/server/emails/mailer.tsx +++ b/server/emails/mailer.tsx @@ -29,6 +29,10 @@ type SendMailOptions = { previewText?: string; /** The plain-text version of the email body. */ text: string; + /** The language of the email in BCP47 format, e.g. "en-US". */ + lang?: string; + /** The text direction of the email. */ + dir?: "ltr" | "rtl"; /** The React element rendered to produce the HTML body. */ component: React.JSX.Element; /** Additional CSS to inject into the head of the email. */ @@ -172,6 +176,8 @@ export class Mailer { data.component, { title: data.subject, + lang: data.lang, + dir: data.dir ?? "ltr", headCSS: [baseStyles, data.headCSS].join(" "), } as Oy.RenderOptions, this.template diff --git a/server/emails/templates/BaseEmail.tsx b/server/emails/templates/BaseEmail.tsx index a8a9d660ff..2635eb1156 100644 --- a/server/emails/templates/BaseEmail.tsx +++ b/server/emails/templates/BaseEmail.tsx @@ -9,6 +9,7 @@ import { toError } from "@shared/utils/error"; import { randomString } from "@shared/random"; import { TeamPreference } from "@shared/types"; import { unicodeCLDRtoBCP47 } from "@shared/utils/date"; +import { isRTLLanguage } from "@shared/utils/rtl"; import { Day } from "@shared/utils/time"; import mailer from "@server/emails/mailer"; import env from "@server/env"; @@ -22,6 +23,7 @@ import { TextHelper } from "@server/models/helpers/TextHelper"; import { taskQueue } from "@server/queues"; import { TaskPriority } from "@server/queues/tasks/base/BaseTask"; import type { NotificationMetadata } from "@server/types"; +import { DirectionContext } from "./components/Direction"; export enum EmailMessageCategory { Authentication = "authentication", @@ -160,6 +162,9 @@ export default abstract class BaseEmail< } } + const lang = this.locale; + const dir = isRTLLanguage(lang) ? "rtl" : "ltr"; + try { await mailer.sendMail({ to: this.props.to, @@ -169,11 +174,13 @@ export default abstract class BaseEmail< messageId, references, previewText: this.preview(data), + lang, + dir, component: ( - <> + {this.render(data)} {notification ? this.pixel(notification) : null} - + ), text: this.renderAsText(data), headCSS: this.headCSS?.(data), diff --git a/server/emails/templates/DocumentAccessRequestEmail.tsx b/server/emails/templates/DocumentAccessRequestEmail.tsx index 2a226c8a92..3232a55fa3 100644 --- a/server/emails/templates/DocumentAccessRequestEmail.tsx +++ b/server/emails/templates/DocumentAccessRequestEmail.tsx @@ -58,11 +58,16 @@ export default class DocumentAccessRequestEmail extends BaseEmail< } protected subject({ actor, document }: Props) { - return `${actor.name} is requesting access to “${document.displayTitle}”`; + return this.t( + "{{ actorName }} is requesting access to “{{ documentTitle }}”", + { actorName: actor.name, documentTitle: document.displayTitle } + ); } protected preview({ actor }: Props): string { - return `${actor.name} is requesting access to a document`; + return this.t("{{ actorName }} is requesting access to a document", { + actorName: actor.name, + }); } protected fromName({ actor }: Props) { @@ -71,9 +76,9 @@ export default class DocumentAccessRequestEmail extends BaseEmail< protected renderAsText({ actor, teamUrl, document }: Props): string { return ` -${actor.name} is requesting access to “${document.displayTitle}”. +${this.t("{{ actorName }} is requesting access to “{{ documentTitle }}”.", { actorName: actor.name, documentTitle: document.displayTitle })} -Open the document to share it with them: ${teamUrl}${document.path} +${this.t("Open the document to share it with them")}: ${teamUrl}${document.path} `; } @@ -84,19 +89,22 @@ Open the document to share it with them: ${teamUrl}${document.path} return (
{document.displayTitle}

- {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}
diff --git a/server/emails/templates/components/Footer.tsx b/server/emails/templates/components/Footer.tsx index 882bce0701..2755c0a8db 100644 --- a/server/emails/templates/components/Footer.tsx +++ b/server/emails/templates/components/Footer.tsx @@ -3,6 +3,7 @@ import * as React from "react"; import theme from "@shared/styles/theme"; import { UrlHelper } from "@shared/utils/UrlHelper"; import env from "@server/env"; +import { DirectionContext } from "./Direction"; type Props = { unsubscribeUrl?: string; @@ -17,11 +18,12 @@ export const Link = ({ href: string; children: React.ReactNode; }) => { + const dir = React.useContext(DirectionContext); const linkStyle = { color: theme.slate, fontWeight: 500, textDecoration: "none", - marginRight: "10px", + [dir === "rtl" ? "marginLeft" : "marginRight"]: "10px", }; return ( diff --git a/server/queues/processors/EmailsProcessor.ts b/server/queues/processors/EmailsProcessor.ts index ea4bfb8ad4..6513b7a5ba 100644 --- a/server/queues/processors/EmailsProcessor.ts +++ b/server/queues/processors/EmailsProcessor.ts @@ -217,6 +217,7 @@ export default class EmailsProcessor extends BaseProcessor { await new DocumentAccessRequestEmail( { to: notification.user.email, + language: notification.user.language, userId: notification.userId, documentId: notification.documentId, actorId: notification.actorId, diff --git a/shared/i18n/locales/en_US/translation.json b/shared/i18n/locales/en_US/translation.json index 002b6d8e99..3261aac826 100644 --- a/shared/i18n/locales/en_US/translation.json +++ b/shared/i18n/locales/en_US/translation.json @@ -1832,11 +1832,16 @@ "You requested to permanently delete your {{ appName }} user account in the {{ teamName }} workspace. Please enter the code below to confirm your account deletion.": "You requested to permanently delete your {{ appName }} user account in the {{ teamName }} workspace. Please enter the code below to confirm your account deletion.", "You requested to permanently delete your {{ appName }} user account in the": "You requested to permanently delete your {{ appName }} user account in the", "workspace. Please enter the code below to confirm your account deletion.": "workspace. Please enter the code below to confirm your account deletion.", + "{{ actorName }} is requesting access to “{{ documentTitle }}”": "{{ actorName }} is requesting access to “{{ documentTitle }}”", + "{{ actorName }} is requesting access to a document": "{{ actorName }} is requesting access to a document", + "{{ actorName }} is requesting access to “{{ documentTitle }}”.": "{{ actorName }} is requesting access to “{{ documentTitle }}”.", + "Open the document to share it with them": "Open the document to share it with them", + "View Document": "View Document", + "{{ actorName }} is requesting access to the": "{{ actorName }} is requesting access to the", "{{ actorName }} mentioned you": "{{ actorName }} mentioned you", "You were mentioned": "You were mentioned", "{{ actorName }} mentioned you in the document “{{ documentTitle }}”.": "{{ actorName }} mentioned you in the document “{{ documentTitle }}”.", "Open Document": "Open Document", - "View Document": "View Document", "{{ actorName }} mentioned you in the document": "{{ actorName }} mentioned you in the document", "updated": "updated", "“{{ documentTitle }}” {{ eventName }}": "“{{ documentTitle }}” {{ eventName }}", diff --git a/yarn.lock b/yarn.lock index 4f76e61392..2b378a0dd4 100644 --- a/yarn.lock +++ b/yarn.lock @@ -19331,64 +19331,7 @@ __metadata: languageName: node linkType: hard -"vite@npm:^6.0.0 || ^7.0.0 || ^8.0.0": - version: 8.3.2 - resolution: "vite@npm:8.3.2" - dependencies: - fsevents: "npm:~2.3.3" - lightningcss: "npm:^1.33.0" - picomatch: "npm:^4.0.7" - postcss: "npm:^8.5.28" - rolldown: "npm:~1.2.11" - tinyglobby: "npm:^0.2.17" - peerDependencies: - "@types/node": ^20.19.0 || >=22.12.0 - "@vitejs/devtools": ^0.7.1 - esbuild: ^0.27.0 || ^0.28.0 - jiti: ">=1.21.0" - less: ^4.0.0 - sass: ^1.70.0 - sass-embedded: ^1.70.0 - stylus: ">=0.54.8" - sugarss: ^5.0.0 - terser: ^5.16.0 - tsx: ^4.8.1 - yaml: ^2.4.2 - dependenciesMeta: - fsevents: - optional: true - peerDependenciesMeta: - "@types/node": - optional: true - "@vitejs/devtools": - optional: true - esbuild: - optional: true - jiti: - optional: true - less: - optional: true - sass: - optional: true - sass-embedded: - optional: true - stylus: - optional: true - sugarss: - optional: true - terser: - optional: true - tsx: - optional: true - yaml: - optional: true - bin: - vite: bin/vite.js - checksum: 10c0/bc2dc5b9077624686a0e4ddfdbfca9d35bf97a32c53966bf1cdd7f355a8f2c488c0422d1b02328a959cfd06b490f98c7e24938f8a251a1039adf2219f2da552e - languageName: node - linkType: hard - -"vite@npm:^8.3.3": +"vite@npm:^6.0.0 || ^7.0.0 || ^8.0.0, vite@npm:^8.3.3": version: 8.3.3 resolution: "vite@npm:8.3.3" dependencies: