chore: Refactor backlinks and revisions (#1611)

* Update backlinks service to not rely on revisions

* fix: Add missing index for finding backlinks

* Debounce revision creation (#1616)

* refactor debounce logic to service

* Debounce slack notification

* Revisions created by service

* fix: Revision sidebar latest

* test: Add tests for notifications
This commit is contained in:
Tom Moor
2020-11-01 10:26:39 -08:00
committed by GitHub
parent 7735aa12d7
commit 3d09c8f655
20 changed files with 487 additions and 246 deletions

View File

@@ -870,6 +870,8 @@ router.post("documents.update", auth(), async (ctx) => {
throw new InvalidRequestError("Document has changed since last revision");
}
const previousTitle = document.title;
// Update document
if (title) document.title = title;
if (editorVersion) document.editorVersion = editorVersion;
@@ -926,6 +928,21 @@ router.post("documents.update", auth(), async (ctx) => {
});
}
if (document.title !== previousTitle) {
Event.add({
name: "documents.title_change",
documentId: document.id,
collectionId: document.collectionId,
teamId: document.teamId,
actorId: user.id,
data: {
previousTitle,
title: document.title,
},
ip: ctx.request.ip,
});
}
document.updatedBy = user;
document.collection = collection;

View File

@@ -1369,9 +1369,7 @@ describe("#documents.restore", () => {
it("should restore the document to a previous version", async () => {
const { user, document } = await seed();
const revision = await Revision.findOne({
where: { documentId: document.id },
});
const revision = await Revision.createFromDocument(document);
const previousText = revision.text;
const revisionId = revision.id;
@@ -1391,9 +1389,7 @@ describe("#documents.restore", () => {
it("should not allow restoring a revision in another document", async () => {
const { user, document } = await seed();
const anotherDoc = await buildDocument();
const revision = await Revision.findOne({
where: { documentId: anotherDoc.id },
});
const revision = await Revision.createFromDocument(anotherDoc);
const revisionId = revision.id;
const res = await server.post("/api/documents.restore", {
@@ -1421,9 +1417,7 @@ describe("#documents.restore", () => {
it("should require authorization", async () => {
const { document } = await seed();
const revision = await Revision.findOne({
where: { documentId: document.id },
});
const revision = await Revision.createFromDocument(document);
const revisionId = revision.id;
const user = await buildUser();
@@ -1684,31 +1678,6 @@ describe("#documents.update", () => {
expect(res.status).toEqual(403);
});
it("should not create new version when autosave=true", async () => {
const { user, document } = await seed();
const res = await server.post("/api/documents.update", {
body: {
token: user.getJwtToken(),
id: document.id,
title: "Updated title",
text: "Updated text",
lastRevision: document.revision,
autosave: true,
},
});
const prevRevisionRecords = await Revision.count();
const body = await res.json();
expect(res.status).toEqual(200);
expect(body.data.title).toBe("Updated title");
expect(body.data.text).toBe("Updated text");
const revisionRecords = await Revision.count();
expect(revisionRecords).toBe(prevRevisionRecords);
});
it("should fail if document lastRevision does not match", async () => {
const { user, document } = await seed();

View File

@@ -1,7 +1,7 @@
/* eslint-disable flowtype/require-valid-file-annotation */
import TestServer from "fetch-test-server";
import app from "../app";
import Revision from "../models/Revision";
import { Revision } from "../models";
import { buildDocument, buildUser } from "../test/factories";
import { flushdb, seed } from "../test/support";
@@ -13,11 +13,8 @@ afterAll(() => server.close());
describe("#revisions.info", () => {
it("should return a document revision", async () => {
const { user, document } = await seed();
const revision = await Revision.findOne({
where: {
documentId: document.id,
},
});
const revision = await Revision.createFromDocument(document);
const res = await server.post("/api/revisions.info", {
body: {
token: user.getJwtToken(),
@@ -33,11 +30,8 @@ describe("#revisions.info", () => {
it("should require authorization", async () => {
const document = await buildDocument();
const revision = await Revision.findOne({
where: {
documentId: document.id,
},
});
const revision = await Revision.createFromDocument(document);
const user = await buildUser();
const res = await server.post("/api/revisions.info", {
body: {
@@ -52,6 +46,8 @@ describe("#revisions.info", () => {
describe("#revisions.list", () => {
it("should return a document's revisions", async () => {
const { user, document } = await seed();
await Revision.createFromDocument(document);
const res = await server.post("/api/revisions.list", {
body: {
token: user.getJwtToken(),
@@ -68,6 +64,8 @@ describe("#revisions.list", () => {
it("should not return revisions for document in collection not a member of", async () => {
const { user, document, collection } = await seed();
await Revision.createFromDocument(document);
collection.private = true;
await collection.save();