feat: Auto update titles in linked documents (#1233)

* feat: Auto update titles in linked documents

* Add spec
This commit is contained in:
Tom Moor
2020-04-19 21:58:42 -07:00
committed by GitHub
parent ee5ae140c3
commit c526adf292
4 changed files with 159 additions and 8 deletions

View File

@@ -3,6 +3,7 @@ import { difference } from 'lodash';
import type { DocumentEvent } from '../events';
import { Document, Revision, Backlink } from '../models';
import parseDocumentIds from '../../shared/utils/parseDocumentIds';
import slugify from '../utils/slugify';
export default class Backlinks {
async on(event: DocumentEvent) {
@@ -50,6 +51,7 @@ export default class Backlinks {
const addedLinkIds = difference(currentLinkIds, previousLinkIds);
const removedLinkIds = difference(previousLinkIds, currentLinkIds);
// add any new backlinks that were created
await Promise.all(
addedLinkIds.map(async linkId => {
const linkedDocument = await Document.findByPk(linkId);
@@ -67,6 +69,7 @@ export default class Backlinks {
})
);
// delete any backlinks that were removed
await Promise.all(
removedLinkIds.map(async linkId => {
const document = await Document.findByPk(linkId);
@@ -78,6 +81,39 @@ export default class Backlinks {
});
})
);
if (currentRevision.title === previousRevision.title) {
break;
}
// update any link titles in documents that lead to this one
const backlinks = await Backlink.findAll({
where: {
documentId: event.documentId,
},
include: [{ model: Document, as: 'reverseDocument' }],
});
await Promise.all(
backlinks.map(async backlink => {
const previousUrl = `/doc/${slugify(previousRevision.title)}-${
document.urlId
}`;
// find links in the other document that lead to this one and have
// the old title as anchor text. Go ahead and update those to the
// new title automatically
backlink.reverseDocument.text = backlink.reverseDocument.text.replace(
`[${previousRevision.title}](${previousUrl})`,
`[${document.title}](${document.url})`
);
await backlink.reverseDocument.save({
silent: true,
hooks: false,
});
})
);
break;
}
case 'documents.delete': {

View File

@@ -0,0 +1,106 @@
/* eslint-disable flowtype/require-valid-file-annotation */
import { flushdb } from '../test/support';
import BacklinksService from './backlinks';
import { buildDocument } from '../test/factories';
import Backlink from '../models/Backlink';
const Backlinks = new BacklinksService();
beforeEach(flushdb);
beforeEach(jest.resetAllMocks);
describe('documents.update', () => {
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();
await Backlinks.on({
name: 'documents.update',
documentId: document.id,
collectionId: document.collectionId,
teamId: document.teamId,
actorId: document.createdById,
data: { autosave: false },
});
const backlinks = await Backlink.findAll({
where: { reverseDocumentId: document.id },
});
expect(backlinks.length).toBe(1);
});
test('should destroy removed backlink records', async () => {
const otherDocument = await buildDocument();
const document = await buildDocument({
text: `[this is a link](${otherDocument.url})`,
});
await Backlinks.on({
name: 'documents.publish',
documentId: document.id,
collectionId: document.collectionId,
teamId: document.teamId,
actorId: document.createdById,
data: { autosave: false },
});
document.text = 'Link is gone';
await document.save();
await Backlinks.on({
name: 'documents.update',
documentId: document.id,
collectionId: document.collectionId,
teamId: document.teamId,
actorId: document.createdById,
data: { autosave: false },
});
const backlinks = await Backlink.findAll({
where: { reverseDocumentId: document.id },
});
expect(backlinks.length).toBe(0);
});
test('should update titles in backlinked documents', async () => {
const newTitle = 'test';
const document = await buildDocument();
const otherDocument = await buildDocument();
// create a doc with a link back
document.text = `[${otherDocument.title}](${otherDocument.url})`;
await document.save();
// ensure the backlinks are created
await Backlinks.on({
name: 'documents.update',
documentId: document.id,
collectionId: document.collectionId,
teamId: document.teamId,
actorId: document.createdById,
data: { autosave: false },
});
// change the title of the linked doc
otherDocument.title = newTitle;
await otherDocument.save();
// does the text get updated with the new title
await Backlinks.on({
name: 'documents.update',
documentId: otherDocument.id,
collectionId: otherDocument.collectionId,
teamId: otherDocument.teamId,
actorId: otherDocument.createdById,
data: { autosave: false },
});
await document.reload();
expect(document.text).toBe(`[${newTitle}](${otherDocument.url})`);
});
});