Add notifications for document and collection access (#6460)
* Add notification for added to document * Add notifications for document and collection access * Add notification delay * fix: Collection notifications not appearing * Add notification settings
This commit is contained in:
102
server/emails/templates/CollectionSharedEmail.tsx
Normal file
102
server/emails/templates/CollectionSharedEmail.tsx
Normal file
@@ -0,0 +1,102 @@
|
||||
import * as React from "react";
|
||||
import { CollectionPermission } from "@shared/types";
|
||||
import { Collection, UserMembership } from "@server/models";
|
||||
import BaseEmail, { EmailProps } from "./BaseEmail";
|
||||
import Body from "./components/Body";
|
||||
import Button from "./components/Button";
|
||||
import EmailTemplate from "./components/EmailLayout";
|
||||
import Header from "./components/Header";
|
||||
import Heading from "./components/Heading";
|
||||
|
||||
type InputProps = EmailProps & {
|
||||
userId: string;
|
||||
collectionId: string;
|
||||
actorName: string;
|
||||
teamUrl: string;
|
||||
};
|
||||
|
||||
type BeforeSend = {
|
||||
collection: Collection;
|
||||
membership: UserMembership;
|
||||
};
|
||||
|
||||
type Props = InputProps & BeforeSend;
|
||||
|
||||
/**
|
||||
* Email sent to a user when someone adds them to a collection.
|
||||
*/
|
||||
export default class CollectionSharedEmail extends BaseEmail<
|
||||
InputProps,
|
||||
BeforeSend
|
||||
> {
|
||||
protected async beforeSend({ userId, collectionId }: InputProps) {
|
||||
const collection = await Collection.findByPk(collectionId);
|
||||
if (!collection) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const membership = await UserMembership.findOne({
|
||||
where: {
|
||||
collectionId,
|
||||
userId,
|
||||
},
|
||||
});
|
||||
if (!membership) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return { collection, membership };
|
||||
}
|
||||
|
||||
protected subject({ actorName, collection }: Props) {
|
||||
return `${actorName} invited you to the “${collection.name}” collection`;
|
||||
}
|
||||
|
||||
protected preview({ actorName }: Props): string {
|
||||
return `${actorName} invited you to a collection`;
|
||||
}
|
||||
|
||||
protected fromName({ actorName }: Props) {
|
||||
return actorName;
|
||||
}
|
||||
|
||||
protected renderAsText({ actorName, teamUrl, collection }: Props): string {
|
||||
return `
|
||||
${actorName} invited you to the “${collection.name}” collection.
|
||||
|
||||
View Document: ${teamUrl}${collection.path}
|
||||
`;
|
||||
}
|
||||
|
||||
protected render(props: Props) {
|
||||
const { collection, membership, actorName, teamUrl } = props;
|
||||
const collectionUrl = `${teamUrl}${collection.path}?ref=notification-email`;
|
||||
|
||||
const permission =
|
||||
membership.permission === CollectionPermission.ReadWrite
|
||||
? "view and edit"
|
||||
: CollectionPermission.Admin
|
||||
? "manage"
|
||||
: "view";
|
||||
|
||||
return (
|
||||
<EmailTemplate
|
||||
previewText={this.preview(props)}
|
||||
goToAction={{ url: collectionUrl, name: "View Collection" }}
|
||||
>
|
||||
<Header />
|
||||
|
||||
<Body>
|
||||
<Heading>{collection.name}</Heading>
|
||||
<p>
|
||||
{actorName} invited you to {permission} documents in the{" "}
|
||||
<a href={collectionUrl}>{collection.name}</a> collection.
|
||||
</p>
|
||||
<p>
|
||||
<Button href={collectionUrl}>View Collection</Button>
|
||||
</p>
|
||||
</Body>
|
||||
</EmailTemplate>
|
||||
);
|
||||
}
|
||||
}
|
||||
98
server/emails/templates/DocumentSharedEmail.tsx
Normal file
98
server/emails/templates/DocumentSharedEmail.tsx
Normal file
@@ -0,0 +1,98 @@
|
||||
import * as React from "react";
|
||||
import { DocumentPermission } from "@shared/types";
|
||||
import { Document, UserMembership } from "@server/models";
|
||||
import BaseEmail, { EmailProps } from "./BaseEmail";
|
||||
import Body from "./components/Body";
|
||||
import Button from "./components/Button";
|
||||
import EmailTemplate from "./components/EmailLayout";
|
||||
import Header from "./components/Header";
|
||||
import Heading from "./components/Heading";
|
||||
|
||||
type InputProps = EmailProps & {
|
||||
userId: string;
|
||||
documentId: string;
|
||||
actorName: string;
|
||||
teamUrl: string;
|
||||
};
|
||||
|
||||
type BeforeSend = {
|
||||
document: Document;
|
||||
membership: UserMembership;
|
||||
};
|
||||
|
||||
type Props = InputProps & BeforeSend;
|
||||
|
||||
/**
|
||||
* Email sent to a user when someone adds them to a document.
|
||||
*/
|
||||
export default class DocumentSharedEmail extends BaseEmail<
|
||||
InputProps,
|
||||
BeforeSend
|
||||
> {
|
||||
protected async beforeSend({ documentId, userId }: InputProps) {
|
||||
const document = await Document.unscoped().findByPk(documentId);
|
||||
if (!document) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const membership = await UserMembership.findOne({
|
||||
where: {
|
||||
documentId,
|
||||
userId,
|
||||
},
|
||||
});
|
||||
if (!membership) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return { document, membership };
|
||||
}
|
||||
|
||||
protected subject({ actorName, document }: Props) {
|
||||
return `${actorName} shared “${document.title}” with you`;
|
||||
}
|
||||
|
||||
protected preview({ actorName }: Props): string {
|
||||
return `${actorName} shared a document`;
|
||||
}
|
||||
|
||||
protected fromName({ actorName }: Props) {
|
||||
return actorName;
|
||||
}
|
||||
|
||||
protected renderAsText({ actorName, teamUrl, document }: Props): string {
|
||||
return `
|
||||
${actorName} shared “${document.title}” with you.
|
||||
|
||||
View Document: ${teamUrl}${document.path}
|
||||
`;
|
||||
}
|
||||
|
||||
protected render(props: Props) {
|
||||
const { document, membership, actorName, teamUrl } = props;
|
||||
const documentUrl = `${teamUrl}${document.path}?ref=notification-email`;
|
||||
|
||||
const permission =
|
||||
membership.permission === DocumentPermission.ReadWrite ? "edit" : "view";
|
||||
|
||||
return (
|
||||
<EmailTemplate
|
||||
previewText={this.preview(props)}
|
||||
goToAction={{ url: documentUrl, name: "View Document" }}
|
||||
>
|
||||
<Header />
|
||||
|
||||
<Body>
|
||||
<Heading>{document.title}</Heading>
|
||||
<p>
|
||||
{actorName} invited you to {permission} the{" "}
|
||||
<a href={documentUrl}>{document.title}</a> document.
|
||||
</p>
|
||||
<p>
|
||||
<Button href={documentUrl}>View Document</Button>
|
||||
</p>
|
||||
</Body>
|
||||
</EmailTemplate>
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user