chore: Move to Typescript (#2783)

This PR moves the entire project to Typescript. Due to the ~1000 ignores this will lead to a messy codebase for a while, but the churn is worth it – all of those ignore comments are places that were never type-safe previously.

closes #1282
This commit is contained in:
Tom Moor
2021-11-29 06:40:55 -08:00
committed by GitHub
parent 25ccfb5d04
commit 15b1069bcc
1017 changed files with 17410 additions and 54942 deletions

View File

@@ -1,11 +1,9 @@
/* eslint-disable flowtype/require-valid-file-annotation */
import { Backlink } from "../../models";
import { buildDocument } from "../../test/factories";
import { flushdb } from "../../test/support";
import { Backlink } from "@server/models";
import { buildDocument } from "@server/test/factories";
import { flushdb } from "@server/test/support";
import BacklinksService from "./backlinks";
const Backlinks = new BacklinksService();
beforeEach(() => flushdb());
beforeEach(jest.resetAllMocks);
@@ -15,7 +13,7 @@ describe("documents.publish", () => {
const document = await buildDocument({
text: `[this is a link](${otherDocument.url})`,
});
// @ts-expect-error ts-migrate(2345) FIXME: Argument of type '{ name: "documents.publish"; doc... Remove this comment to see the full error message
await Backlinks.on({
name: "documents.publish",
documentId: document.id,
@@ -23,26 +21,24 @@ describe("documents.publish", () => {
teamId: document.teamId,
actorId: document.createdById,
});
const backlinks = await Backlink.findAll({
where: { reverseDocumentId: document.id },
where: {
reverseDocumentId: document.id,
},
});
expect(backlinks.length).toBe(1);
});
test("should not fail when linked document is destroyed", async () => {
const otherDocument = await buildDocument();
await otherDocument.destroy();
const document = await buildDocument({
version: null,
text: `[ ] checklist item`,
});
document.text = `[this is a link](${otherDocument.url})`;
await document.save();
// @ts-expect-error ts-migrate(2345) FIXME: Argument of type '{ name: "documents.publish"; doc... Remove this comment to see the full error message
await Backlinks.on({
name: "documents.publish",
documentId: document.id,
@@ -50,22 +46,21 @@ describe("documents.publish", () => {
teamId: document.teamId,
actorId: document.createdById,
});
const backlinks = await Backlink.findAll({
where: { reverseDocumentId: document.id },
where: {
reverseDocumentId: document.id,
},
});
expect(backlinks.length).toBe(0);
});
});
describe("documents.update", () => {
test("should not fail on a document with no previous revisions", async () => {
const otherDocument = await buildDocument();
const document = await buildDocument({
text: `[this is a link](${otherDocument.url})`,
});
// @ts-expect-error ts-migrate(2345) FIXME: Argument of type '{ name: "documents.update"; docu... Remove this comment to see the full error message
await Backlinks.on({
name: "documents.update",
documentId: document.id,
@@ -73,11 +68,11 @@ describe("documents.update", () => {
teamId: document.teamId,
actorId: document.createdById,
});
const backlinks = await Backlink.findAll({
where: { reverseDocumentId: document.id },
where: {
reverseDocumentId: document.id,
},
});
expect(backlinks.length).toBe(1);
});
@@ -87,10 +82,9 @@ describe("documents.update", () => {
version: null,
text: `[ ] checklist item`,
});
document.text = `[this is a link](${otherDocument.url})`;
await document.save();
// @ts-expect-error ts-migrate(2345) FIXME: Argument of type '{ name: "documents.update"; docu... Remove this comment to see the full error message
await Backlinks.on({
name: "documents.update",
documentId: document.id,
@@ -98,21 +92,20 @@ describe("documents.update", () => {
teamId: document.teamId,
actorId: document.createdById,
});
const backlinks = await Backlink.findAll({
where: { reverseDocumentId: document.id },
where: {
reverseDocumentId: document.id,
},
});
expect(backlinks.length).toBe(1);
});
test("should create new backlink records", async () => {
const otherDocument = await buildDocument();
const document = await buildDocument();
document.text = `[this is a link](${otherDocument.url})`;
await document.save();
// @ts-expect-error ts-migrate(2345) FIXME: Argument of type '{ name: "documents.update"; docu... Remove this comment to see the full error message
await Backlinks.on({
name: "documents.update",
documentId: document.id,
@@ -120,11 +113,11 @@ describe("documents.update", () => {
teamId: document.teamId,
actorId: document.createdById,
});
const backlinks = await Backlink.findAll({
where: { reverseDocumentId: document.id },
where: {
reverseDocumentId: document.id,
},
});
expect(backlinks.length).toBe(1);
});
@@ -136,7 +129,7 @@ describe("documents.update", () => {
[this is a another link](${yetAnotherDocument.url})`,
});
// @ts-expect-error ts-migrate(2345) FIXME: Argument of type '{ name: "documents.publish"; doc... Remove this comment to see the full error message
await Backlinks.on({
name: "documents.publish",
documentId: document.id,
@@ -144,12 +137,11 @@ describe("documents.update", () => {
teamId: document.teamId,
actorId: document.createdById,
});
document.text = `First link is gone
[this is a another link](${yetAnotherDocument.url})`;
await document.save();
// @ts-expect-error ts-migrate(2345) FIXME: Argument of type '{ name: "documents.update"; docu... Remove this comment to see the full error message
await Backlinks.on({
name: "documents.update",
documentId: document.id,
@@ -157,24 +149,22 @@ describe("documents.update", () => {
teamId: document.teamId,
actorId: document.createdById,
});
const backlinks = await Backlink.findAll({
where: { reverseDocumentId: document.id },
where: {
reverseDocumentId: document.id,
},
});
expect(backlinks.length).toBe(1);
expect(backlinks[0].documentId).toBe(yetAnotherDocument.id);
});
});
describe("documents.delete", () => {
test("should destroy related backlinks", async () => {
const otherDocument = await buildDocument();
const document = await buildDocument();
document.text = `[this is a link](${otherDocument.url})`;
await document.save();
// @ts-expect-error ts-migrate(2345) FIXME: Argument of type '{ name: "documents.update"; docu... Remove this comment to see the full error message
await Backlinks.on({
name: "documents.update",
documentId: document.id,
@@ -182,7 +172,7 @@ describe("documents.delete", () => {
teamId: document.teamId,
actorId: document.createdById,
});
// @ts-expect-error ts-migrate(2345) FIXME: Argument of type '{ name: "documents.delete"; docu... Remove this comment to see the full error message
await Backlinks.on({
name: "documents.delete",
documentId: document.id,
@@ -190,27 +180,25 @@ describe("documents.delete", () => {
teamId: document.teamId,
actorId: document.createdById,
});
const backlinks = await Backlink.findAll({
where: { reverseDocumentId: document.id },
where: {
reverseDocumentId: document.id,
},
});
expect(backlinks.length).toBe(0);
});
});
describe("documents.title_change", () => {
test("should update titles in backlinked documents", async () => {
const newTitle = "test";
const document = await buildDocument();
const otherDocument = await buildDocument();
const previousTitle = otherDocument.title;
// create a doc with a link back
document.text = `[${otherDocument.title}](${otherDocument.url})`;
await document.save();
// ensure the backlinks are created
// @ts-expect-error ts-migrate(2345) FIXME: Argument of type '{ name: "documents.update"; docu... Remove this comment to see the full error message
await Backlinks.on({
name: "documents.update",
documentId: document.id,
@@ -218,12 +206,11 @@ describe("documents.title_change", () => {
teamId: document.teamId,
actorId: document.createdById,
});
// change the title of the linked doc
otherDocument.title = newTitle;
await otherDocument.save();
// does the text get updated with the new title
// @ts-expect-error ts-migrate(2345) FIXME: Argument of type '{ name: "documents.title_change"... Remove this comment to see the full error message
await Backlinks.on({
name: "documents.title_change",
documentId: otherDocument.id,
@@ -236,7 +223,6 @@ describe("documents.title_change", () => {
},
});
await document.reload();
expect(document.text).toBe(`[${newTitle}](${otherDocument.url})`);
});
});

View File

@@ -1,9 +1,8 @@
// @flow
import { Document, Backlink, Team } from "../../models";
import { Op } from "../../sequelize";
import type { DocumentEvent, RevisionEvent } from "../../types";
import parseDocumentIds from "../../utils/parseDocumentIds";
import slugify from "../../utils/slugify";
import { Document, Backlink, Team } from "@server/models";
import { Op } from "@server/sequelize";
import parseDocumentIds from "@server/utils/parseDocumentIds";
import slugify from "@server/utils/slugify";
import { DocumentEvent, RevisionEvent } from "../../types";
export default class BacklinksProcessor {
async on(event: DocumentEvent | RevisionEvent) {
@@ -11,12 +10,11 @@ export default class BacklinksProcessor {
case "documents.publish": {
const document = await Document.findByPk(event.documentId);
if (!document) return;
const linkIds = parseDocumentIds(document.text);
await Promise.all(
linkIds.map(async (linkId) => {
const linkedDocument = await Document.findByPk(linkId);
if (!linkedDocument || linkedDocument.id === event.documentId) {
return;
}
@@ -32,9 +30,9 @@ export default class BacklinksProcessor {
});
})
);
break;
}
case "documents.update": {
const document = await Document.findByPk(event.documentId);
if (!document) return;
@@ -43,12 +41,13 @@ export default class BacklinksProcessor {
if (!document.publishedAt) return;
const linkIds = parseDocumentIds(document.text);
const linkedDocumentIds = [];
const linkedDocumentIds: string[] = [];
// create or find existing backlink records for referenced docs
await Promise.all(
linkIds.map(async (linkId) => {
const linkedDocument = await Document.findByPk(linkId);
if (!linkedDocument || linkedDocument.id === event.documentId) {
return;
}
@@ -77,6 +76,7 @@ export default class BacklinksProcessor {
});
break;
}
case "documents.title_change": {
// might as well check
const { title, previousTitle } = event.data;
@@ -87,6 +87,7 @@ export default class BacklinksProcessor {
// TODO: Handle re-writing of titles into CRDT
const team = await Team.findByPk(document.teamId);
if (team?.collaborativeEditing) {
break;
}
@@ -96,10 +97,15 @@ export default class BacklinksProcessor {
where: {
documentId: event.documentId,
},
include: [{ model: Document, as: "reverseDocument" }],
include: [
{
model: Document,
as: "reverseDocument",
},
],
});
await Promise.all(
// @ts-expect-error ts-migrate(7006) FIXME: Parameter 'backlink' implicitly has an 'any' type.
backlinks.map(async (backlink) => {
const previousUrl = `/doc/${slugify(previousTitle)}-${
document.urlId
@@ -118,20 +124,25 @@ export default class BacklinksProcessor {
});
})
);
break;
}
case "documents.delete": {
await Backlink.destroy({
where: {
[Op.or]: [
{ reverseDocumentId: event.documentId },
{ documentId: event.documentId },
{
reverseDocumentId: event.documentId,
},
{
documentId: event.documentId,
},
],
},
});
break;
}
default:
}
}

View File

@@ -1,23 +1,20 @@
// @flow
import { Document } from "../../models";
import { Document } from "@server/models";
import { globalEventQueue } from "../../queues";
import type { Event } from "../../types";
import { Event } from "../../types";
export default class DebounceProcessor {
async on(event: Event) {
switch (event.name) {
case "documents.update": {
globalEventQueue.add(
{
...event,
name: "documents.update.delayed",
},
{ ...event, name: "documents.update.delayed" },
{
delay: 5 * 60 * 1000,
}
);
break;
}
case "documents.update.delayed": {
const document = await Document.findByPk(event.documentId, {
fields: ["updatedAt"],
@@ -31,12 +28,10 @@ export default class DebounceProcessor {
// this functions as a simple distributed debounce.
if (document.updatedAt > new Date(event.createdAt)) return;
globalEventQueue.add({
...event,
name: "documents.update.debounced",
});
globalEventQueue.add({ ...event, name: "documents.update.debounced" });
break;
}
default:
}
}

View File

@@ -1,14 +0,0 @@
// @flow
import mailer, { type EmailSendOptions, type EmailTypes } from "../../mailer";
type EmailEvent = {
type: EmailTypes,
opts: EmailSendOptions,
};
export default class EmailsProcessor {
async on(event: EmailEvent) {
// $FlowIssue flow rightly doesn't like dynaic values
await mailer[event.type](event.opts);
}
}

View File

@@ -0,0 +1,12 @@
import mailer, { EmailSendOptions, EmailTypes } from "../../mailer";
type EmailEvent = {
type: EmailTypes;
opts: EmailSendOptions;
};
export default class EmailsProcessor {
async on(event: EmailEvent) {
await mailer[event.type](event.opts);
}
}

View File

@@ -1,49 +1,48 @@
// @flow
import fs from "fs";
import { FileOperation, Collection, Event, Team, User } from "@server/models";
import { uploadToS3FromBuffer } from "@server/utils/s3";
import { archiveCollections } from "@server/utils/zip";
import Logger from "../../logging/logger";
import mailer from "../../mailer";
import { FileOperation, Collection, Event, Team, User } from "../../models";
import type { Event as TEvent } from "../../types";
import { uploadToS3FromBuffer } from "../../utils/s3";
import { archiveCollections } from "../../utils/zip";
import { Event as TEvent } from "../../types";
export default class ExportsProcessor {
async on(event: TEvent) {
switch (event.name) {
case "collections.export":
case "collections.export_all":
case "collections.export_all": {
const { actorId, teamId } = event;
const team = await Team.findByPk(teamId);
const user = await User.findByPk(actorId);
let exportData = await FileOperation.findByPk(event.modelId);
const exportData = await FileOperation.findByPk(event.modelId);
const collectionIds =
// @ts-expect-error ts-migrate(2339) FIXME: Property 'collectionId' does not exist on type 'Co... Remove this comment to see the full error message
event.collectionId || (await user.collectionIds());
const collections = await Collection.findAll({
where: { id: collectionIds },
where: {
id: collectionIds,
},
});
this.updateFileOperation(exportData, actorId, teamId, {
state: "creating",
});
// heavy lifting of creating the zip file
Logger.info(
"processor",
`Archiving collections for file operation ${exportData.id}`
);
const filePath = await archiveCollections(collections);
let url, state;
try {
const readBuffer = await fs.promises.readFile(filePath);
const stat = await fs.promises.stat(filePath);
try {
// @ts-expect-error ts-migrate(2769) FIXME: No overload matches this call.
const readBuffer = await fs.promises.readFile(filePath);
// @ts-expect-error ts-migrate(2769) FIXME: No overload matches this call.
const stat = await fs.promises.stat(filePath);
this.updateFileOperation(exportData, actorId, teamId, {
state: "uploading",
size: stat.size,
});
Logger.info(
"processor",
`Uploading archive for file operation ${exportData.id}`
@@ -54,7 +53,6 @@ export default class ExportsProcessor {
exportData.key,
"private"
);
Logger.info(
"processor",
`Upload complete for file operation ${exportData.id}`
@@ -85,19 +83,21 @@ export default class ExportsProcessor {
});
}
}
break;
}
default:
}
}
async updateFileOperation(
// @ts-expect-error ts-migrate(2749) FIXME: 'FileOperation' refers to a value, but is being us... Remove this comment to see the full error message
fileOperation: FileOperation,
actorId: string,
teamId: string,
data: Object
data: Record<string, any>
) {
await fileOperation.update(data);
await Event.add({
name: "fileOperations.update",
teamId,

View File

@@ -1,10 +1,10 @@
// @flow
import fs from "fs";
import os from "os";
// @ts-expect-error ts-migrate(7016) FIXME: Could not find a declaration file for module 'form... Remove this comment to see the full error message
import File from "formidable/lib/file";
import collectionImporter from "../../commands/collectionImporter";
import { Attachment, User } from "../../models";
import type { Event } from "../../types";
import collectionImporter from "@server/commands/collectionImporter";
import { Attachment, User } from "@server/models";
import { Event } from "../../types";
export default class ImportsProcessor {
async on(event: Event) {
@@ -13,29 +13,25 @@ export default class ImportsProcessor {
const { type } = event.data;
const attachment = await Attachment.findByPk(event.modelId);
const user = await User.findByPk(event.actorId);
const buffer = await attachment.buffer;
const tmpDir = os.tmpdir();
const tmpFilePath = `${tmpDir}/upload-${event.modelId}`;
await fs.promises.writeFile(tmpFilePath, buffer);
const file = new File({
name: attachment.name,
type: attachment.type,
path: tmpFilePath,
});
await collectionImporter({
file,
user,
type,
ip: event.ip,
});
await attachment.destroy();
return;
}
default:
}
}

View File

@@ -1,21 +1,17 @@
/* eslint-disable flowtype/require-valid-file-annotation */
import mailer from "../../mailer";
import { View, NotificationSetting } from "../../models";
import { View, NotificationSetting } from "@server/models";
import {
buildDocument,
buildCollection,
buildUser,
} from "../../test/factories";
import { flushdb } from "../../test/support";
} from "@server/test/factories";
import { flushdb } from "@server/test/support";
import mailer from "../../mailer";
import NotificationsService from "./notifications";
jest.mock("../../mailer");
const Notifications = new NotificationsService();
beforeEach(() => flushdb());
beforeEach(jest.resetAllMocks);
describe("documents.publish", () => {
test("should not send a notification to author", async () => {
const user = await buildUser();
@@ -23,13 +19,12 @@ describe("documents.publish", () => {
teamId: user.teamId,
lastModifiedById: user.id,
});
await NotificationSetting.create({
userId: user.id,
teamId: user.teamId,
event: "documents.publish",
});
// @ts-expect-error ts-migrate(2345) FIXME: Argument of type '{ name: "documents.publish"; doc... Remove this comment to see the full error message
await Notifications.on({
name: "documents.publish",
documentId: document.id,
@@ -37,7 +32,6 @@ describe("documents.publish", () => {
teamId: document.teamId,
actorId: document.createdById,
});
expect(mailer.documentNotification).not.toHaveBeenCalled();
});
@@ -46,13 +40,12 @@ describe("documents.publish", () => {
const document = await buildDocument({
teamId: user.teamId,
});
await NotificationSetting.create({
userId: user.id,
teamId: user.teamId,
event: "documents.publish",
});
// @ts-expect-error ts-migrate(2345) FIXME: Argument of type '{ name: "documents.publish"; doc... Remove this comment to see the full error message
await Notifications.on({
name: "documents.publish",
documentId: document.id,
@@ -60,7 +53,6 @@ describe("documents.publish", () => {
teamId: document.teamId,
actorId: document.createdById,
});
expect(mailer.documentNotification).toHaveBeenCalled();
});
@@ -74,13 +66,12 @@ describe("documents.publish", () => {
teamId: user.teamId,
collectionId: collection.id,
});
await NotificationSetting.create({
userId: user.id,
teamId: user.teamId,
event: "documents.publish",
});
// @ts-expect-error ts-migrate(2345) FIXME: Argument of type '{ name: "documents.publish"; doc... Remove this comment to see the full error message
await Notifications.on({
name: "documents.publish",
documentId: document.id,
@@ -88,57 +79,54 @@ describe("documents.publish", () => {
teamId: document.teamId,
actorId: document.createdById,
});
expect(mailer.documentNotification).not.toHaveBeenCalled();
});
});
describe("revisions.create", () => {
test("should send a notification to other collaborators", async () => {
const document = await buildDocument();
const collaborator = await buildUser({ teamId: document.teamId });
const collaborator = await buildUser({
teamId: document.teamId,
});
document.collaboratorIds = [collaborator.id];
await document.save();
await NotificationSetting.create({
userId: collaborator.id,
teamId: collaborator.teamId,
event: "documents.update",
});
await Notifications.on({
name: "revisions.create",
documentId: document.id,
collectionId: document.collectionId,
teamId: document.teamId,
// @ts-expect-error ts-migrate(2345) FIXME: Argument of type '{ name: "revisions.create"; docu... Remove this comment to see the full error message
actorId: document.createdById,
});
expect(mailer.documentNotification).toHaveBeenCalled();
});
test("should not send a notification if viewed since update", async () => {
const document = await buildDocument();
const collaborator = await buildUser({ teamId: document.teamId });
const collaborator = await buildUser({
teamId: document.teamId,
});
document.collaboratorIds = [collaborator.id];
await document.save();
await NotificationSetting.create({
userId: collaborator.id,
teamId: collaborator.teamId,
event: "documents.update",
});
await View.touch(document.id, collaborator.id, true);
await Notifications.on({
name: "revisions.create",
documentId: document.id,
collectionId: document.collectionId,
teamId: document.teamId,
// @ts-expect-error ts-migrate(2345) FIXME: Argument of type '{ name: "revisions.create"; docu... Remove this comment to see the full error message
actorId: document.createdById,
});
expect(mailer.documentNotification).not.toHaveBeenCalled();
});
@@ -148,21 +136,19 @@ describe("revisions.create", () => {
teamId: user.teamId,
lastModifiedById: user.id,
});
await NotificationSetting.create({
userId: user.id,
teamId: user.teamId,
event: "documents.update",
});
await Notifications.on({
name: "revisions.create",
documentId: document.id,
collectionId: document.collectionId,
teamId: document.teamId,
// @ts-expect-error ts-migrate(2345) FIXME: Argument of type '{ name: "revisions.create"; docu... Remove this comment to see the full error message
actorId: document.createdById,
});
expect(mailer.documentNotification).not.toHaveBeenCalled();
});
});

View File

@@ -1,6 +1,3 @@
// @flow
import Logger from "../../logging/logger";
import mailer from "../../mailer";
import {
View,
Document,
@@ -8,9 +5,11 @@ import {
Collection,
User,
NotificationSetting,
} from "../../models";
import { Op } from "../../sequelize";
import type {
} from "@server/models";
import { Op } from "@server/sequelize";
import Logger from "../../logging/logger";
import mailer from "../../mailer";
import {
DocumentEvent,
CollectionEvent,
RevisionEvent,
@@ -23,24 +22,24 @@ export default class NotificationsProcessor {
case "documents.publish":
case "revisions.create":
return this.documentUpdated(event);
case "collections.create":
return this.collectionCreated(event);
default:
}
}
async documentUpdated(event: DocumentEvent | RevisionEvent) {
// never send notifications when batch importing documents
// @ts-expect-error ts-migrate(2339) FIXME: Property 'data' does not exist on type 'DocumentEv... Remove this comment to see the full error message
if (event.data && event.data.source === "import") return;
const [document, team] = await Promise.all([
Document.findByPk(event.documentId),
Team.findByPk(event.teamId),
]);
if (!document || !team || !document.collection) return;
const { collection } = document;
const notificationSettings = await NotificationSetting.findAll({
where: {
userId: {
@@ -60,7 +59,6 @@ export default class NotificationsProcessor {
},
],
});
const eventName =
event.name === "documents.publish" ? "published" : "updated";
@@ -83,6 +81,7 @@ export default class NotificationsProcessor {
// Check the user has access to the collection this document is in. Just
// because they were a collaborator once doesn't mean they still are.
const collectionIds = await setting.user.collectionIds();
if (!collectionIds.includes(document.collectionId)) {
continue;
}
@@ -131,7 +130,6 @@ export default class NotificationsProcessor {
});
if (!collection) return;
if (!collection.permission) return;
const notificationSettings = await NotificationSetting.findAll({
where: {
userId: {

View File

@@ -1,18 +1,15 @@
/* eslint-disable flowtype/require-valid-file-annotation */
import { Revision } from "../../models";
import { buildDocument } from "../../test/factories";
import { flushdb } from "../../test/support";
import { Revision } from "@server/models";
import { buildDocument } from "@server/test/factories";
import { flushdb } from "@server/test/support";
import RevisionsService from "./revisions";
const Revisions = new RevisionsService();
beforeEach(() => flushdb());
beforeEach(jest.resetAllMocks);
describe("documents.publish", () => {
test("should create a revision", async () => {
const document = await buildDocument();
// @ts-expect-error ts-migrate(2345) FIXME: Argument of type '{ name: "documents.publish"; doc... Remove this comment to see the full error message
await Revisions.on({
name: "documents.publish",
documentId: document.id,
@@ -20,16 +17,18 @@ describe("documents.publish", () => {
teamId: document.teamId,
actorId: document.createdById,
});
const amount = await Revision.count({ where: { documentId: document.id } });
const amount = await Revision.count({
where: {
documentId: document.id,
},
});
expect(amount).toBe(1);
});
});
describe("documents.update.debounced", () => {
test("should create a revision", async () => {
const document = await buildDocument();
// @ts-expect-error ts-migrate(2345) FIXME: Argument of type '{ name: "documents.update.deboun... Remove this comment to see the full error message
await Revisions.on({
name: "documents.update.debounced",
documentId: document.id,
@@ -37,16 +36,18 @@ describe("documents.update.debounced", () => {
teamId: document.teamId,
actorId: document.createdById,
});
const amount = await Revision.count({ where: { documentId: document.id } });
const amount = await Revision.count({
where: {
documentId: document.id,
},
});
expect(amount).toBe(1);
});
test("should not create a revision if identical to previous", async () => {
const document = await buildDocument();
await Revision.createFromDocument(document);
// @ts-expect-error ts-migrate(2345) FIXME: Argument of type '{ name: "documents.update.deboun... Remove this comment to see the full error message
await Revisions.on({
name: "documents.update.debounced",
documentId: document.id,
@@ -54,8 +55,11 @@ describe("documents.update.debounced", () => {
teamId: document.teamId,
actorId: document.createdById,
});
const amount = await Revision.count({ where: { documentId: document.id } });
const amount = await Revision.count({
where: {
documentId: document.id,
},
});
expect(amount).toBe(1);
});
});

View File

@@ -1,8 +1,7 @@
// @flow
import invariant from "invariant";
import revisionCreator from "../../commands/revisionCreator";
import { Revision, Document, User } from "../../models";
import type { DocumentEvent, RevisionEvent } from "../../types";
import revisionCreator from "@server/commands/revisionCreator";
import { Revision, Document, User } from "@server/models";
import { DocumentEvent, RevisionEvent } from "../../types";
export default class RevisionsProcessor {
async on(event: DocumentEvent | RevisionEvent) {
@@ -11,7 +10,6 @@ export default class RevisionsProcessor {
case "documents.update.debounced": {
const document = await Document.findByPk(event.documentId);
invariant(document, "Document should exist");
const previous = await Revision.findLatest(document.id);
// we don't create revisions if identical to previous revision, this can
@@ -26,11 +24,13 @@ export default class RevisionsProcessor {
const user = await User.findByPk(event.actorId);
invariant(user, "User should exist");
await revisionCreator({ user, document });
await revisionCreator({
user,
document,
});
break;
}
default:
}
}

View File

@@ -1,8 +1,7 @@
// @flow
import fetch from "fetch-with-proxy";
import { Document, Integration, Collection, Team } from "../../models";
import { presentSlackAttachment } from "../../presenters";
import type {
import { Document, Integration, Collection, Team } from "@server/models";
import { presentSlackAttachment } from "@server/presenters";
import {
DocumentEvent,
IntegrationEvent,
RevisionEvent,
@@ -15,8 +14,10 @@ export default class SlackProcessor {
case "documents.publish":
case "revisions.create":
return this.documentUpdated(event);
case "integrations.create":
return this.integrationCreated(event);
default:
}
}
@@ -37,10 +38,8 @@ export default class SlackProcessor {
],
});
if (!integration) return;
const collection = integration.collection;
if (!collection) return;
await fetch(integration.settings.url, {
method: "POST",
headers: {
@@ -62,18 +61,15 @@ export default class SlackProcessor {
async documentUpdated(event: DocumentEvent | RevisionEvent) {
// never send notifications when batch importing documents
// @ts-expect-error ts-migrate(2339) FIXME: Property 'data' does not exist on type 'DocumentEv... Remove this comment to see the full error message
if (event.data && event.data.source === "import") return;
const [document, team] = await Promise.all([
Document.findByPk(event.documentId),
Team.findByPk(event.teamId),
]);
if (!document) return;
// never send notifications for draft documents
if (!document.publishedAt) return;
const integration = await Integration.findOne({
where: {
teamId: document.teamId,
@@ -83,7 +79,6 @@ export default class SlackProcessor {
},
});
if (!integration) return;
let text = `${document.updatedBy.name} updated a document`;
if (event.name === "documents.publish") {

View File

@@ -1,4 +1,3 @@
// @flow
import { subHours } from "date-fns";
import {
Document,
@@ -6,9 +5,9 @@ import {
Group,
CollectionGroup,
GroupUser,
} from "../../models";
import { Op } from "../../sequelize";
import type { Event } from "../../types";
} from "@server/models";
import { Op } from "@server/sequelize";
import { Event } from "../../types";
export default class WebsocketsProcessor {
async on(event: Event, socketio: any) {
@@ -20,11 +19,9 @@ export default class WebsocketsProcessor {
const document = await Document.findByPk(event.documentId, {
paranoid: false,
});
const channel = document.publishedAt
? `collection-${document.collectionId}`
: `user-${event.actorId}`;
return socketio.to(channel).emit("entities", {
event: event.name,
documentIds: [
@@ -40,6 +37,7 @@ export default class WebsocketsProcessor {
],
});
}
case "documents.delete": {
const document = await Document.findByPk(event.documentId, {
paranoid: false,
@@ -74,6 +72,7 @@ export default class WebsocketsProcessor {
],
});
}
case "documents.permanent_delete": {
return socketio
.to(`collection-${event.collectionId}`)
@@ -81,17 +80,16 @@ export default class WebsocketsProcessor {
documentId: event.documentId,
});
}
case "documents.pin":
case "documents.unpin":
case "documents.update": {
const document = await Document.findByPk(event.documentId, {
paranoid: false,
});
const channel = document.publishedAt
? `collection-${document.collectionId}`
: `user-${event.actorId}`;
return socketio.to(channel).emit("entities", {
event: event.name,
documentIds: [
@@ -102,9 +100,9 @@ export default class WebsocketsProcessor {
],
});
}
case "documents.create": {
const document = await Document.findByPk(event.documentId);
return socketio.to(`user-${event.actorId}`).emit("entities", {
event: event.name,
documentIds: [
@@ -120,12 +118,14 @@ export default class WebsocketsProcessor {
],
});
}
case "documents.star":
case "documents.unstar": {
return socketio.to(`user-${event.actorId}`).emit(event.name, {
documentId: event.documentId,
});
}
case "documents.move": {
const documents = await Document.findAll({
where: {
@@ -133,6 +133,7 @@ export default class WebsocketsProcessor {
},
paranoid: false,
});
// @ts-expect-error ts-migrate(7006) FIXME: Parameter 'document' implicitly has an 'any' type.
documents.forEach((document) => {
socketio.to(`collection-${document.collectionId}`).emit("entities", {
event: event.name,
@@ -147,16 +148,20 @@ export default class WebsocketsProcessor {
event.data.collectionIds.forEach((collectionId) => {
socketio.to(`collection-${collectionId}`).emit("entities", {
event: event.name,
collectionIds: [{ id: collectionId }],
collectionIds: [
{
id: collectionId,
},
],
});
});
return;
}
case "collections.create": {
const collection = await Collection.findByPk(event.collectionId, {
paranoid: false,
});
socketio
.to(
collection.permission
@@ -172,7 +177,6 @@ export default class WebsocketsProcessor {
},
],
});
return socketio
.to(
collection.permission
@@ -184,12 +188,12 @@ export default class WebsocketsProcessor {
collectionId: collection.id,
});
}
case "collections.update":
case "collections.delete": {
const collection = await Collection.findByPk(event.collectionId, {
paranoid: false,
});
return socketio.to(`team-${collection.teamId}`).emit("entities", {
event: event.name,
collectionIds: [
@@ -218,20 +222,19 @@ export default class WebsocketsProcessor {
userId: event.userId,
collectionId: event.collectionId,
});
// let everyone with access to the collection know a user was added
socketio.to(`collection-${event.collectionId}`).emit(event.name, {
event: event.name,
userId: event.userId,
collectionId: event.collectionId,
});
// tell any user clients to connect to the websocket channel for the collection
return socketio.to(`user-${event.userId}`).emit("join", {
event: event.name,
collectionId: event.collectionId,
});
}
case "collections.remove_user": {
const membershipUserIds = await Collection.membershipUserIds(
event.collectionId
@@ -255,15 +258,16 @@ export default class WebsocketsProcessor {
userId: event.userId,
collectionId: event.collectionId,
});
// tell any user clients to disconnect from the websocket channel for the collection
socketio.to(`user-${event.userId}`).emit("leave", {
event: event.name,
collectionId: event.collectionId,
});
}
return;
}
case "collections.add_group": {
const group = await Group.findByPk(event.data.groupId);
@@ -277,15 +281,16 @@ export default class WebsocketsProcessor {
userId: groupMembership.userId,
collectionId: event.collectionId,
});
// tell any user clients to connect to the websocket channel for the collection
socketio.to(`user-${groupMembership.userId}`).emit("join", {
event: event.name,
collectionId: event.collectionId,
});
}
return;
}
case "collections.remove_group": {
const group = await Group.findByPk(event.data.groupId);
const membershipUserIds = await Collection.membershipUserIds(
@@ -312,7 +317,6 @@ export default class WebsocketsProcessor {
userId: groupMembership.userId,
collectionId: event.collectionId,
});
// tell any user clients to disconnect to the websocket channel for the collection
socketio.to(`user-${groupMembership.userId}`).emit("leave", {
event: event.name,
@@ -320,6 +324,7 @@ export default class WebsocketsProcessor {
});
}
}
return;
}
@@ -334,7 +339,6 @@ export default class WebsocketsProcessor {
const group = await Group.findByPk(event.modelId, {
paranoid: false,
});
return socketio.to(`team-${group.teamId}`).emit("entities", {
event: event.name,
groupIds: [
@@ -345,10 +349,13 @@ export default class WebsocketsProcessor {
],
});
}
case "groups.add_user": {
// do an add user for every collection that the group is a part of
const collectionGroupMemberships = await CollectionGroup.findAll({
where: { groupId: event.modelId },
where: {
groupId: event.modelId,
},
});
for (const collectionGroup of collectionGroupMemberships) {
@@ -359,7 +366,6 @@ export default class WebsocketsProcessor {
userId: event.userId,
collectionId: collectionGroup.collectionId,
});
// let everyone with access to the collection know a user was added
socketio
.to(`collection-${collectionGroup.collectionId}`)
@@ -368,18 +374,21 @@ export default class WebsocketsProcessor {
userId: event.userId,
collectionId: collectionGroup.collectionId,
});
// tell any user clients to connect to the websocket channel for the collection
return socketio.to(`user-${event.userId}`).emit("join", {
event: event.name,
collectionId: collectionGroup.collectionId,
});
}
return;
}
case "groups.remove_user": {
const collectionGroupMemberships = await CollectionGroup.findAll({
where: { groupId: event.modelId },
where: {
groupId: event.modelId,
},
});
for (const collectionGroup of collectionGroupMemberships) {
@@ -414,7 +423,6 @@ export default class WebsocketsProcessor {
userId: event.userId,
collectionId: collectionGroup.collectionId,
});
// tell any user clients to disconnect from the websocket channel for the collection
socketio.to(`user-${event.userId}`).emit("leave", {
event: event.name,
@@ -422,13 +430,14 @@ export default class WebsocketsProcessor {
});
}
}
return;
}
case "groups.delete": {
const group = await Group.findByPk(event.modelId, {
paranoid: false,
});
socketio.to(`team-${group.teamId}`).emit("entities", {
event: event.name,
groupIds: [
@@ -438,7 +447,6 @@ export default class WebsocketsProcessor {
},
],
});
// we the users and collection relations that were just severed as a result of the group deletion
// since there are cascading deletes, we approximate this by looking for the recently deleted
// items in the GroupUser and CollectionGroup tables
@@ -451,7 +459,6 @@ export default class WebsocketsProcessor {
},
},
});
const collectionGroupMemberships = await CollectionGroup.findAll({
paranoid: false,
where: {
@@ -487,7 +494,6 @@ export default class WebsocketsProcessor {
userId: groupUser.userId,
collectionId: collectionGroup.collectionId,
});
// tell any user clients to disconnect from the websocket channel for the collection
socketio.to(`user-${groupUser.userId}`).emit("leave", {
event: event.name,
@@ -496,6 +502,7 @@ export default class WebsocketsProcessor {
}
}
}
return;
}