mirror of
https://github.com/outline/outline.git
synced 2026-11-22 22:02:35 +03:00
fix: Translate access request emails and set email language and direction (#14147)
* fix: Allow every mention notification to be turned off and unsubscribed - The Mentioned and Group mentions settings now control both the document and comment event types. - Mention, share, collection invite, and access request emails now have an unsubscribe link and List-Unsubscribe header. * fix: Translate access request emails and set email language and direction - The access request email now uses the language of the recipient. - Emails now set lang and dir on the HTML document, and RTL emails align content to the right.
This commit is contained in:
1 parent
71c3f79018
commit
ba7b6e88e7
10 files changed
+217
-72
No files matched your search
@@ -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
|
||||
|
||||
@@ -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: (
|
||||
<>
|
||||
<DirectionContext.Provider value={dir}>
|
||||
{this.render(data)}
|
||||
{notification ? this.pixel(notification) : null}
|
||||
</>
|
||||
</DirectionContext.Provider>
|
||||
),
|
||||
text: this.renderAsText(data),
|
||||
headCSS: this.headCSS?.(data),
|
||||
|
||||
@@ -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 (
|
||||
<EmailTemplate
|
||||
previewText={this.preview(props)}
|
||||
goToAction={{ url: documentUrl, name: "View Document" }}
|
||||
goToAction={{ url: documentUrl, name: this.t("View Document") }}
|
||||
>
|
||||
<Header />
|
||||
|
||||
<Body>
|
||||
<Heading>{document.displayTitle}</Heading>
|
||||
<p>
|
||||
{actor.name} is requesting access to the{" "}
|
||||
<a href={documentUrl}>{document.displayTitle}</a> document.
|
||||
{this.t("{{ actorName }} is requesting access to the", {
|
||||
actorName: actor.name,
|
||||
})}{" "}
|
||||
<a href={documentUrl}>{document.displayTitle}</a>{" "}
|
||||
{this.t("document")}.
|
||||
</p>
|
||||
<p>Open the document to share it with them.</p>
|
||||
<p>{this.t("Open the document to share it with them")}.</p>
|
||||
<p>
|
||||
<Button href={documentUrl}>View Document</Button>
|
||||
<Button href={documentUrl}>{this.t("View Document")}</Button>
|
||||
</p>
|
||||
</Body>
|
||||
|
||||
|
||||
@@ -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"');
|
||||
});
|
||||
});
|
||||
@@ -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");
|
||||
@@ -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<
|
||||
<Table width="550">
|
||||
<TBody>
|
||||
<TR>
|
||||
<TD align="left">{children}</TD>
|
||||
<TD align={dir === "rtl" ? "right" : "left"}>{children}</TD>
|
||||
</TR>
|
||||
</TBody>
|
||||
</Table>
|
||||
|
||||
@@ -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 (
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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 }}",
|
||||
|
||||
@@ -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:
|
||||
|
||||
Reference in new issue
Block a user