mirror of
https://github.com/outline/outline.git
synced 2026-11-20 21:23:06 +03:00
feat: Show export completion in the notifications center (#14673)
* feat: Show export completion in the notifications center Exports now create an in-app notification with the export name, status, format and a download button. Export emails are sent from the notification with a one-minute delay, so they are skipped once the notification is read. * fix: Avoid nested button in export notification and keep it after delete * fix: Revoke export notifications when access to the source is lost * fix: Remove icon from export notification download cue
This commit is contained in:
1 parent
41abdbec3f
commit
1465b7ea80
11 files changed
+426
-67
No files matched your search
@@ -1,14 +1,16 @@
|
||||
import { toJS } from "mobx";
|
||||
import { observer } from "mobx-react";
|
||||
import { ArchiveIcon, DownloadIcon, WarningIcon } from "outline-icons";
|
||||
import * as React from "react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import { Link } from "react-router-dom";
|
||||
import styled from "styled-components";
|
||||
import styled, { useTheme } from "styled-components";
|
||||
import { s, hover, truncateMultiline } from "@shared/styles";
|
||||
import { NotificationEventType } from "@shared/types";
|
||||
import { FileOperationState, NotificationEventType } from "@shared/types";
|
||||
import type Notification from "~/models/Notification";
|
||||
import useStores from "~/hooks/useStores";
|
||||
import { Avatar, AvatarSize, AvatarVariant } from "../Avatar";
|
||||
import Button from "../Button";
|
||||
import Flex from "../Flex";
|
||||
import Text from "../Text";
|
||||
import Time from "../Time";
|
||||
@@ -42,6 +44,9 @@ function NotificationListItem({ notification, onNavigate }: Props) {
|
||||
notification.event === NotificationEventType.RequestDocumentAccess &&
|
||||
notification.accessRequestStatus === "pending";
|
||||
|
||||
const isExport = notification.event === NotificationEventType.ExportCompleted;
|
||||
const { fileOperation, downloadUrl } = notification;
|
||||
|
||||
const handleClick: React.MouseEventHandler<HTMLAnchorElement> = (event) => {
|
||||
if (event.altKey) {
|
||||
event.preventDefault();
|
||||
@@ -50,6 +55,11 @@ function NotificationListItem({ notification, onNavigate }: Props) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (downloadUrl) {
|
||||
event.preventDefault();
|
||||
window.open(downloadUrl, "_blank");
|
||||
}
|
||||
|
||||
void notification.markAsRead();
|
||||
|
||||
onNavigate();
|
||||
@@ -73,19 +83,36 @@ function NotificationListItem({ notification, onNavigate }: Props) {
|
||||
<ContextMenu action={menuAction} ariaLabel={t("Notification options")}>
|
||||
<StyledLink to={notification.path ?? ""} onClick={handleClick}>
|
||||
<Container gap={8} $unread={!notification.viewedAt}>
|
||||
<StyledAvatar model={notification.actor} />
|
||||
{isExport ? (
|
||||
<ExportIcon state={fileOperation?.state} />
|
||||
) : (
|
||||
<StyledAvatar model={notification.actor} />
|
||||
)}
|
||||
<Flex column>
|
||||
<Text as="div" size="small">
|
||||
<Text weight="bold">
|
||||
{notification.actor?.name ?? t("Unknown")}
|
||||
</Text>{" "}
|
||||
{notification.eventText(t)}{" "}
|
||||
<Text weight="bold">{notification.subject(t)}</Text>
|
||||
</Text>
|
||||
{isExport ? (
|
||||
<Text as="div" size="small">
|
||||
<Text weight="bold">{notification.subject(t)}</Text>{" "}
|
||||
{notification.eventText(t)}
|
||||
</Text>
|
||||
) : (
|
||||
<Text as="div" size="small">
|
||||
<Text weight="bold">
|
||||
{notification.actor?.name ?? t("Unknown")}
|
||||
</Text>{" "}
|
||||
{notification.eventText(t)}{" "}
|
||||
<Text weight="bold">{notification.subject(t)}</Text>
|
||||
</Text>
|
||||
)}
|
||||
<Text type="tertiary" size="xsmall">
|
||||
<Time dateTime={notification.createdAt} addSuffix />{" "}
|
||||
{collection && <>· {collection.displayName}</>}
|
||||
{collection && <>· {collection.displayName} </>}
|
||||
{fileOperation && <>· {fileOperation.formatLabel}</>}
|
||||
</Text>
|
||||
{downloadUrl && (
|
||||
<DownloadButton forwardedAs="span" aria-hidden neutral>
|
||||
{t("Download")}
|
||||
</DownloadButton>
|
||||
)}
|
||||
{commentData && (
|
||||
<StyledCommentEditor value={commentData} readOnly />
|
||||
)}
|
||||
@@ -100,6 +127,48 @@ function NotificationListItem({ notification, onNavigate }: Props) {
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* An icon that shows the state of the export in an export notification.
|
||||
*/
|
||||
function ExportIcon({ state }: { state?: FileOperationState }) {
|
||||
const theme = useTheme();
|
||||
|
||||
if (state === FileOperationState.Complete) {
|
||||
return (
|
||||
<IconWrapper>
|
||||
<DownloadIcon color={theme.textSecondary} />
|
||||
</IconWrapper>
|
||||
);
|
||||
}
|
||||
|
||||
if (state === FileOperationState.Error) {
|
||||
return (
|
||||
<IconWrapper>
|
||||
<WarningIcon color={theme.danger} />
|
||||
</IconWrapper>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<IconWrapper>
|
||||
<ArchiveIcon color={theme.textTertiary} />
|
||||
</IconWrapper>
|
||||
);
|
||||
}
|
||||
|
||||
const IconWrapper = styled.div`
|
||||
flex-shrink: 0;
|
||||
width: ${AvatarSize.Medium}px;
|
||||
height: ${AvatarSize.Medium}px;
|
||||
margin-top: 4px;
|
||||
`;
|
||||
|
||||
// A visual cue only, as a click anywhere on the row downloads the export.
|
||||
const DownloadButton = styled(Button)`
|
||||
align-self: flex-start;
|
||||
margin-top: 8px;
|
||||
`;
|
||||
|
||||
const StyledLink = styled(Link)`
|
||||
display: block;
|
||||
margin: 0 8px;
|
||||
|
||||
@@ -2,9 +2,9 @@ import { computed, observable } from "mobx";
|
||||
import { t } from "i18next";
|
||||
import { toast } from "sonner";
|
||||
import {
|
||||
FileOperationFormat,
|
||||
FileOperationState,
|
||||
FileOperationType,
|
||||
type FileOperationFormat,
|
||||
} from "@shared/types";
|
||||
import { bytesToHumanReadable } from "@shared/utils/files";
|
||||
import type FileOperationsStore from "~/stores/FileOperationsStore";
|
||||
@@ -16,6 +16,17 @@ import Relation from "./decorators/Relation";
|
||||
class FileOperation extends Model {
|
||||
static modelName = "FileOperation";
|
||||
|
||||
/** Readable labels for each file operation format. */
|
||||
static formatLabels: Record<FileOperationFormat, string> = {
|
||||
[FileOperationFormat.JSON]: "JSON",
|
||||
[FileOperationFormat.Notion]: "Notion",
|
||||
[FileOperationFormat.MarkdownZip]: "Markdown",
|
||||
[FileOperationFormat.OKFZip]: "OKF",
|
||||
[FileOperationFormat.HTMLZip]: "HTML",
|
||||
[FileOperationFormat.TextBundleZip]: "TextBundle",
|
||||
[FileOperationFormat.PDF]: "PDF",
|
||||
};
|
||||
|
||||
constructor(fields: Record<string, unknown>, store: FileOperationsStore) {
|
||||
super(fields, store);
|
||||
this.initialize(fields);
|
||||
@@ -49,6 +60,29 @@ class FileOperation extends Model {
|
||||
return bytesToHumanReadable(this.size);
|
||||
}
|
||||
|
||||
/**
|
||||
* The name to show for the file operation. Exports without a collection or
|
||||
* document cover all collections.
|
||||
*/
|
||||
get displayName(): string {
|
||||
if (
|
||||
this.type === FileOperationType.Export &&
|
||||
!this.collectionId &&
|
||||
!this.documentId
|
||||
) {
|
||||
return t("All collections");
|
||||
}
|
||||
return this.name;
|
||||
}
|
||||
|
||||
/**
|
||||
* A readable label for the format of the file operation.
|
||||
*/
|
||||
@computed
|
||||
get formatLabel(): string {
|
||||
return FileOperation.formatLabels[this.format];
|
||||
}
|
||||
|
||||
@computed
|
||||
get downloadUrl(): string {
|
||||
return `/api/fileOperations.redirect?id=${this.id}`;
|
||||
@@ -65,7 +99,7 @@ class FileOperation extends Model {
|
||||
return;
|
||||
}
|
||||
|
||||
const { ui, auth } = model.store.rootStore;
|
||||
const { ui, auth, notifications } = model.store.rootStore;
|
||||
if (model.user?.id !== auth.user?.id) {
|
||||
return;
|
||||
}
|
||||
@@ -85,7 +119,12 @@ class FileOperation extends Model {
|
||||
duration: Infinity,
|
||||
action: {
|
||||
label: t("Download"),
|
||||
onClick: () => window.open(model.downloadUrl, "_blank"),
|
||||
onClick: () => {
|
||||
window.open(model.downloadUrl, "_blank");
|
||||
void notifications
|
||||
.find({ fileOperationId: model.id })
|
||||
?.markAsRead();
|
||||
},
|
||||
},
|
||||
});
|
||||
ui.removeExportToast(model.id);
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import type { TFunction } from "i18next";
|
||||
import { action, computed, observable } from "mobx";
|
||||
import type { NotificationData } from "@shared/types";
|
||||
import { NotificationEventType } from "@shared/types";
|
||||
import { FileOperationState, NotificationEventType } from "@shared/types";
|
||||
import {
|
||||
collectionPath,
|
||||
commentPath,
|
||||
@@ -11,6 +11,7 @@ import {
|
||||
import Collection from "./Collection";
|
||||
import Comment from "./Comment";
|
||||
import Document from "./Document";
|
||||
import FileOperation from "./FileOperation";
|
||||
import Group from "./Group";
|
||||
import User from "./User";
|
||||
import Model from "./base/Model";
|
||||
@@ -135,6 +136,17 @@ class Notification extends Model {
|
||||
@Relation(() => Group, { onDelete: "cascade" })
|
||||
group?: Group;
|
||||
|
||||
/**
|
||||
* The file operation ID that the notification is associated with.
|
||||
*/
|
||||
fileOperationId?: string;
|
||||
|
||||
/**
|
||||
* The file operation that the notification is associated with.
|
||||
*/
|
||||
@Relation(() => FileOperation, { onDelete: "ignore" })
|
||||
fileOperation?: FileOperation;
|
||||
|
||||
commentId?: string;
|
||||
|
||||
/**
|
||||
@@ -228,6 +240,15 @@ class Notification extends Model {
|
||||
return t("added you to the group");
|
||||
case NotificationEventType.RemoveUserFromGroup:
|
||||
return t("removed you from the group");
|
||||
case NotificationEventType.ExportCompleted:
|
||||
switch (this.fileOperation?.state) {
|
||||
case FileOperationState.Complete:
|
||||
return t("export completed");
|
||||
case FileOperationState.Error:
|
||||
return t("export failed");
|
||||
default:
|
||||
return t("export is no longer available");
|
||||
}
|
||||
case NotificationEventType.RequestDocumentAccess:
|
||||
if (this.accessRequestStatus === "approved") {
|
||||
return t("was granted access to");
|
||||
@@ -243,12 +264,15 @@ class Notification extends Model {
|
||||
|
||||
/**
|
||||
* Returns translated text for the subject of the notification. This is the
|
||||
* name of the associated document, collection, or group.
|
||||
* name of the associated document, collection, group, or export.
|
||||
*
|
||||
* @param t - The translation function.
|
||||
* @returns The subject.
|
||||
*/
|
||||
subject(t: TFunction): string {
|
||||
if (this.fileOperationId) {
|
||||
return this.fileOperation?.displayName ?? t("Your");
|
||||
}
|
||||
if (this.documentId) {
|
||||
return this.document?.title ?? t("a document");
|
||||
}
|
||||
@@ -261,6 +285,18 @@ class Notification extends Model {
|
||||
return t("Unknown");
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the URL to download the file of the notification, if available.
|
||||
*
|
||||
* @returns The download URL.
|
||||
*/
|
||||
@computed
|
||||
get downloadUrl(): string | undefined {
|
||||
return this.fileOperation?.state === FileOperationState.Complete
|
||||
? this.fileOperation.downloadUrl
|
||||
: undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the path to the model associated with the notification that can be
|
||||
* used with the router.
|
||||
|
||||
@@ -6,11 +6,7 @@ import { toast } from "sonner";
|
||||
import { useTheme } from "styled-components";
|
||||
import { errToString } from "@shared/utils/error";
|
||||
import Spinner from "@shared/components/Spinner";
|
||||
import {
|
||||
FileOperationFormat,
|
||||
FileOperationState,
|
||||
FileOperationType,
|
||||
} from "@shared/types";
|
||||
import { FileOperationState, FileOperationType } from "@shared/types";
|
||||
import type FileOperation from "~/models/FileOperation";
|
||||
import { Action } from "~/components/Actions";
|
||||
import ConfirmationDialog from "~/components/ConfirmationDialog";
|
||||
@@ -53,23 +49,8 @@ const FileOperationListItem = ({ fileOperation }: Props) => {
|
||||
[FileOperationState.Error]: <WarningIcon color={theme.danger} />,
|
||||
};
|
||||
|
||||
const formatMapping: Record<FileOperationFormat, string> = {
|
||||
[FileOperationFormat.JSON]: "JSON",
|
||||
[FileOperationFormat.Notion]: "Notion",
|
||||
[FileOperationFormat.MarkdownZip]: "Markdown",
|
||||
[FileOperationFormat.OKFZip]: "OKF",
|
||||
[FileOperationFormat.HTMLZip]: "HTML",
|
||||
[FileOperationFormat.TextBundleZip]: "TextBundle",
|
||||
[FileOperationFormat.PDF]: "PDF",
|
||||
};
|
||||
|
||||
const format = formatMapping[fileOperation.format];
|
||||
const title =
|
||||
fileOperation.type === FileOperationType.Import ||
|
||||
fileOperation.collectionId ||
|
||||
fileOperation.documentId
|
||||
? fileOperation.name
|
||||
: t("All collections");
|
||||
const format = fileOperation.formatLabel;
|
||||
const title = fileOperation.displayName;
|
||||
|
||||
const handleDelete = React.useCallback(async () => {
|
||||
try {
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
"use strict";
|
||||
|
||||
/** @type {import('sequelize-cli').Migration} */
|
||||
module.exports = {
|
||||
async up(queryInterface, Sequelize) {
|
||||
await queryInterface.addColumn("notifications", "fileOperationId", {
|
||||
type: Sequelize.UUID,
|
||||
allowNull: true,
|
||||
references: {
|
||||
model: "file_operations",
|
||||
key: "id",
|
||||
},
|
||||
onDelete: "CASCADE",
|
||||
onUpdate: "CASCADE",
|
||||
});
|
||||
|
||||
await queryInterface.addIndex("notifications", ["fileOperationId"], {
|
||||
concurrently: true,
|
||||
where: { fileOperationId: { [Sequelize.Op.ne]: null } },
|
||||
});
|
||||
},
|
||||
|
||||
async down(queryInterface) {
|
||||
await queryInterface.removeIndex("notifications", ["fileOperationId"]);
|
||||
await queryInterface.removeColumn("notifications", "fileOperationId");
|
||||
},
|
||||
};
|
||||
@@ -34,6 +34,7 @@ import Team from "./Team";
|
||||
import User from "./User";
|
||||
import Group from "./Group";
|
||||
import AccessRequest from "./AccessRequest";
|
||||
import FileOperation from "./FileOperation";
|
||||
|
||||
let baseDomain;
|
||||
|
||||
@@ -100,6 +101,11 @@ let baseDomain;
|
||||
association: "group",
|
||||
required: false,
|
||||
},
|
||||
{
|
||||
model: FileOperation,
|
||||
as: "fileOperation",
|
||||
required: false,
|
||||
},
|
||||
],
|
||||
}))
|
||||
@Table({
|
||||
@@ -213,6 +219,14 @@ class Notification extends Model<
|
||||
@Column(DataType.UUID)
|
||||
accessRequestId: string;
|
||||
|
||||
@BelongsTo(() => FileOperation, "fileOperationId")
|
||||
fileOperation: FileOperation | null;
|
||||
|
||||
@AllowNull
|
||||
@ForeignKey(() => FileOperation)
|
||||
@Column(DataType.UUID)
|
||||
fileOperationId: string | null;
|
||||
|
||||
@AfterCreate
|
||||
static async createEvent(
|
||||
model: Notification,
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import type { Notification } from "@server/models";
|
||||
import type { APIContext } from "@server/types";
|
||||
import presentFileOperation from "./fileOperation";
|
||||
import presentGroup from "./group";
|
||||
import presentUser from "./user";
|
||||
import { presentComment, presentDocument } from ".";
|
||||
@@ -33,6 +34,10 @@ export default async function presentNotification(
|
||||
group: notification.group
|
||||
? await presentGroup(notification.group)
|
||||
: undefined,
|
||||
fileOperationId: notification.fileOperationId,
|
||||
fileOperation: notification.fileOperation
|
||||
? presentFileOperation(notification.fileOperation)
|
||||
: undefined,
|
||||
data: notification.data,
|
||||
};
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
import { NotificationEventType } from "@shared/types";
|
||||
import { FileOperationState, NotificationEventType } from "@shared/types";
|
||||
import { Minute } from "@shared/utils/time";
|
||||
import CollectionCreatedEmail from "@server/emails/templates/CollectionCreatedEmail";
|
||||
import CollectionSharedEmail from "@server/emails/templates/CollectionSharedEmail";
|
||||
@@ -9,7 +9,9 @@ import DocumentAccessRequestEmail from "@server/emails/templates/DocumentAccessR
|
||||
import DocumentMentionedEmail from "@server/emails/templates/DocumentMentionedEmail";
|
||||
import DocumentPublishedOrUpdatedEmail from "@server/emails/templates/DocumentPublishedOrUpdatedEmail";
|
||||
import DocumentSharedEmail from "@server/emails/templates/DocumentSharedEmail";
|
||||
import { Notification } from "@server/models";
|
||||
import ExportFailureEmail from "@server/emails/templates/ExportFailureEmail";
|
||||
import ExportSuccessEmail from "@server/emails/templates/ExportSuccessEmail";
|
||||
import { FileOperation, Notification } from "@server/models";
|
||||
import type { Event, NotificationEvent } from "@server/types";
|
||||
import BaseProcessor from "./BaseProcessor";
|
||||
import GroupDocumentMentionedEmail from "@server/emails/templates/GroupDocumentMentionedEmail";
|
||||
@@ -213,6 +215,44 @@ export default class EmailsProcessor extends BaseProcessor {
|
||||
return;
|
||||
}
|
||||
|
||||
case NotificationEventType.ExportCompleted: {
|
||||
const fileOperation = notification.fileOperationId
|
||||
? await FileOperation.unscoped().findByPk(
|
||||
notification.fileOperationId
|
||||
)
|
||||
: null;
|
||||
if (fileOperation?.state === FileOperationState.Complete) {
|
||||
await new ExportSuccessEmail(
|
||||
{
|
||||
to: notification.user.email,
|
||||
language: notification.user.language,
|
||||
userId: notification.userId,
|
||||
id: fileOperation.id,
|
||||
teamUrl: notification.team.url,
|
||||
teamId: notification.teamId,
|
||||
},
|
||||
{ notificationId }
|
||||
).schedule({
|
||||
delay: Minute.ms,
|
||||
});
|
||||
}
|
||||
if (fileOperation?.state === FileOperationState.Error) {
|
||||
await new ExportFailureEmail(
|
||||
{
|
||||
to: notification.user.email,
|
||||
language: notification.user.language,
|
||||
userId: notification.userId,
|
||||
teamUrl: notification.team.url,
|
||||
teamId: notification.teamId,
|
||||
},
|
||||
{ notificationId }
|
||||
).schedule({
|
||||
delay: Minute.ms,
|
||||
});
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
case NotificationEventType.RequestDocumentAccess: {
|
||||
await new DocumentAccessRequestEmail(
|
||||
{
|
||||
|
||||
@@ -0,0 +1,134 @@
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import fs from "fs-extra";
|
||||
import { vi } from "vitest";
|
||||
import { FileOperationState, NotificationEventType } from "@shared/types";
|
||||
import { Notification } from "@server/models";
|
||||
import presentNotification from "@server/presenters/notification";
|
||||
import FileStorage from "@server/storage/files";
|
||||
import {
|
||||
buildAdmin,
|
||||
buildCollection,
|
||||
buildFileOperation,
|
||||
buildUser,
|
||||
} from "@server/test/factories";
|
||||
import ExportMarkdownZipTask from "./ExportMarkdownZipTask";
|
||||
import RevokeUserNotificationsTask from "./RevokeUserNotificationsTask";
|
||||
|
||||
describe("ExportTask", () => {
|
||||
beforeEach(() => {
|
||||
vi.spyOn(FileStorage, "store").mockResolvedValue("https://example.com");
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
vi.restoreAllMocks();
|
||||
});
|
||||
|
||||
const exportToTempFile = () =>
|
||||
vi
|
||||
.spyOn(ExportMarkdownZipTask.prototype, "loadDataAndExport")
|
||||
.mockImplementation(async () => {
|
||||
const filePath = path.join(os.tmpdir(), `export-${Date.now()}.zip`);
|
||||
await fs.writeFile(filePath, "data");
|
||||
return filePath;
|
||||
});
|
||||
|
||||
it("should notify the user in the app when the export completes", async () => {
|
||||
const user = await buildAdmin();
|
||||
const fileOperation = await buildFileOperation({
|
||||
teamId: user.teamId,
|
||||
userId: user.id,
|
||||
});
|
||||
exportToTempFile();
|
||||
|
||||
await new ExportMarkdownZipTask().perform({
|
||||
fileOperationId: fileOperation.id,
|
||||
});
|
||||
|
||||
const notifications = await Notification.findAll({
|
||||
where: { userId: user.id },
|
||||
});
|
||||
expect(notifications).toHaveLength(1);
|
||||
expect(notifications[0].event).toEqual(
|
||||
NotificationEventType.ExportCompleted
|
||||
);
|
||||
expect(notifications[0].fileOperationId).toEqual(fileOperation.id);
|
||||
|
||||
const presented = await presentNotification(undefined, notifications[0]);
|
||||
expect(presented.fileOperation?.id).toEqual(fileOperation.id);
|
||||
expect(presented.fileOperation?.state).toEqual(FileOperationState.Complete);
|
||||
});
|
||||
|
||||
it("should notify the user in the app when the export fails", async () => {
|
||||
const user = await buildAdmin();
|
||||
const fileOperation = await buildFileOperation({
|
||||
teamId: user.teamId,
|
||||
userId: user.id,
|
||||
});
|
||||
vi.spyOn(
|
||||
ExportMarkdownZipTask.prototype,
|
||||
"loadDataAndExport"
|
||||
).mockRejectedValue(new Error("Export failed"));
|
||||
|
||||
await expect(
|
||||
new ExportMarkdownZipTask().perform({
|
||||
fileOperationId: fileOperation.id,
|
||||
})
|
||||
).rejects.toThrow("Export failed");
|
||||
|
||||
const notification = await Notification.findOne({
|
||||
where: { userId: user.id },
|
||||
rejectOnEmpty: true,
|
||||
});
|
||||
expect(notification.fileOperationId).toEqual(fileOperation.id);
|
||||
expect(notification.fileOperation?.state).toEqual(FileOperationState.Error);
|
||||
});
|
||||
|
||||
it("should revoke the notification when the user can no longer read the source", async () => {
|
||||
const user = await buildUser();
|
||||
const collection = await buildCollection({
|
||||
teamId: user.teamId,
|
||||
permission: null,
|
||||
});
|
||||
const fileOperation = await buildFileOperation({
|
||||
teamId: user.teamId,
|
||||
userId: user.id,
|
||||
collectionId: collection.id,
|
||||
});
|
||||
exportToTempFile();
|
||||
|
||||
await new ExportMarkdownZipTask().perform({
|
||||
fileOperationId: fileOperation.id,
|
||||
});
|
||||
const notification = await Notification.findOne({
|
||||
where: { userId: user.id },
|
||||
rejectOnEmpty: true,
|
||||
});
|
||||
expect(notification.collectionId).toEqual(collection.id);
|
||||
|
||||
await new RevokeUserNotificationsTask().perform({
|
||||
userId: user.id,
|
||||
collectionId: collection.id,
|
||||
});
|
||||
|
||||
expect(await Notification.findByPk(notification.id)).toBeNull();
|
||||
});
|
||||
|
||||
it("should not notify a user that is not subscribed", async () => {
|
||||
const user = await buildAdmin();
|
||||
user.setNotificationEventType(NotificationEventType.ExportCompleted, false);
|
||||
await user.save();
|
||||
const fileOperation = await buildFileOperation({
|
||||
teamId: user.teamId,
|
||||
userId: user.id,
|
||||
});
|
||||
exportToTempFile();
|
||||
|
||||
await new ExportMarkdownZipTask().perform({
|
||||
fileOperationId: fileOperation.id,
|
||||
});
|
||||
|
||||
const count = await Notification.count({ where: { userId: user.id } });
|
||||
expect(count).toEqual(0);
|
||||
});
|
||||
});
|
||||
@@ -8,8 +8,6 @@ import { toError } from "@shared/utils/error";
|
||||
import type { NavigationNode } from "@shared/types";
|
||||
import { FileOperationState, NotificationEventType } from "@shared/types";
|
||||
import { bytesToHumanReadable } from "@shared/utils/files";
|
||||
import ExportFailureEmail from "@server/emails/templates/ExportFailureEmail";
|
||||
import ExportSuccessEmail from "@server/emails/templates/ExportSuccessEmail";
|
||||
import env from "@server/env";
|
||||
import { ValidationError } from "@server/errors";
|
||||
import Logger from "@server/logging/Logger";
|
||||
@@ -19,7 +17,7 @@ import {
|
||||
Document,
|
||||
Event,
|
||||
FileOperation,
|
||||
Team,
|
||||
Notification,
|
||||
User,
|
||||
} from "@server/models";
|
||||
import fileOperationPresenter from "@server/presenters/fileOperation";
|
||||
@@ -46,10 +44,9 @@ export default abstract class ExportTask extends BaseTask<Props> {
|
||||
const fileOperation = await FileOperation.findByPk(fileOperationId, {
|
||||
rejectOnEmpty: true,
|
||||
});
|
||||
const [team, user] = await Promise.all([
|
||||
Team.findByPk(fileOperation.teamId, { rejectOnEmpty: true }),
|
||||
User.findByPk(fileOperation.userId, { rejectOnEmpty: true }),
|
||||
]);
|
||||
const user = await User.findByPk(fileOperation.userId, {
|
||||
rejectOnEmpty: true,
|
||||
});
|
||||
|
||||
let filePath: string | undefined;
|
||||
let readStream: fs.ReadStream | undefined;
|
||||
@@ -87,30 +84,13 @@ export default abstract class ExportTask extends BaseTask<Props> {
|
||||
url,
|
||||
});
|
||||
|
||||
if (user.subscribedToEventType(NotificationEventType.ExportCompleted)) {
|
||||
await new ExportSuccessEmail({
|
||||
to: user.email,
|
||||
language: user.language,
|
||||
userId: user.id,
|
||||
id: fileOperation.id,
|
||||
teamUrl: team.url,
|
||||
teamId: team.id,
|
||||
}).schedule();
|
||||
}
|
||||
await this.createNotification(fileOperation, user);
|
||||
} catch (error) {
|
||||
await this.updateFileOperation(fileOperation, {
|
||||
state: FileOperationState.Error,
|
||||
error: toError(error),
|
||||
});
|
||||
if (user.subscribedToEventType(NotificationEventType.ExportCompleted)) {
|
||||
await new ExportFailureEmail({
|
||||
to: user.email,
|
||||
language: user.language,
|
||||
userId: user.id,
|
||||
teamUrl: team.url,
|
||||
teamId: team.id,
|
||||
}).schedule();
|
||||
}
|
||||
await this.createNotification(fileOperation, user);
|
||||
throw error;
|
||||
} finally {
|
||||
// Destroy the read stream first to release the file handle before deletion
|
||||
@@ -278,6 +258,36 @@ export default abstract class ExportTask extends BaseTask<Props> {
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Notify the user that requested the export, in the app and by email. A
|
||||
* failure is logged and does not change the result of the export.
|
||||
*
|
||||
* @param fileOperation The FileOperation that finished.
|
||||
* @param user The user that requested the export.
|
||||
*/
|
||||
private async createNotification(fileOperation: FileOperation, user: User) {
|
||||
if (!user.subscribedToEventType(NotificationEventType.ExportCompleted)) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
await Notification.create({
|
||||
event: NotificationEventType.ExportCompleted,
|
||||
userId: user.id,
|
||||
actorId: user.id,
|
||||
teamId: user.teamId,
|
||||
fileOperationId: fileOperation.id,
|
||||
// The source lets the notification be revoked if access is lost.
|
||||
documentId: fileOperation.documentId ?? undefined,
|
||||
collectionId: fileOperation.collectionId ?? undefined,
|
||||
});
|
||||
} catch (error) {
|
||||
Logger.error("Failed to create export notification", toError(error), {
|
||||
fileOperationId: fileOperation.id,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the state of the underlying FileOperation in the database and send
|
||||
* an event to the client.
|
||||
|
||||
@@ -940,6 +940,7 @@
|
||||
"Trash options": "Trash options",
|
||||
"User options": "User options",
|
||||
"document": "document",
|
||||
"All collections": "All collections",
|
||||
"Export complete": "Export complete",
|
||||
"Export failed": "Export failed",
|
||||
"An unexpected error occurred": "An unexpected error occurred",
|
||||
@@ -955,9 +956,13 @@
|
||||
"invited you to": "invited you to",
|
||||
"added you to the group": "added you to the group",
|
||||
"removed you from the group": "removed you from the group",
|
||||
"export completed": "export completed",
|
||||
"export failed": "export failed",
|
||||
"export is no longer available": "export is no longer available",
|
||||
"was granted access to": "was granted access to",
|
||||
"requested access to": "requested access to",
|
||||
"is requesting access to": "is requesting access to",
|
||||
"Your": "Your",
|
||||
"a document": "a document",
|
||||
"a collection": "a collection",
|
||||
"a group": "a group",
|
||||
@@ -1388,7 +1393,6 @@
|
||||
"Processing": "Processing",
|
||||
"Completed": "Completed",
|
||||
"Failed": "Failed",
|
||||
"All collections": "All collections",
|
||||
"Import deleted": "Import deleted",
|
||||
"Export deleted": "Export deleted",
|
||||
"Are you sure you want to delete this import?": "Are you sure you want to delete this import?",
|
||||
|
||||
Reference in new issue
Block a user