fixes #1093 – account for no previous revision in backlinks service

This commit is contained in:
Tom Moor
2019-12-07 11:40:15 -08:00
parent 504aa9f7bb
commit cbd9ff2dd9
2 changed files with 5 additions and 3 deletions

View File

@ -38,12 +38,14 @@ export default class Backlinks {
const document = await Document.findByPk(event.documentId); const document = await Document.findByPk(event.documentId);
if (!document.publishedAt) return; if (!document.publishedAt) return;
const [currentRevision, previsionRevision] = await Revision.findAll({ const [currentRevision, previousRevision] = await Revision.findAll({
where: { documentId: event.documentId }, where: { documentId: event.documentId },
order: [['createdAt', 'desc']], order: [['createdAt', 'desc']],
limit: 2, limit: 2,
}); });
const previousLinkIds = parseDocumentIds(previsionRevision.text); const previousLinkIds = previousRevision
? parseDocumentIds(previousRevision.text)
: [];
const currentLinkIds = parseDocumentIds(currentRevision.text); const currentLinkIds = parseDocumentIds(currentRevision.text);
const addedLinkIds = difference(currentLinkIds, previousLinkIds); const addedLinkIds = difference(currentLinkIds, previousLinkIds);
const removedLinkIds = difference(previousLinkIds, currentLinkIds); const removedLinkIds = difference(previousLinkIds, currentLinkIds);

View File

@ -2,7 +2,7 @@
import MarkdownSerializer from 'slate-md-serializer'; import MarkdownSerializer from 'slate-md-serializer';
const Markdown = new MarkdownSerializer(); const Markdown = new MarkdownSerializer();
export default function parseDocumentIds(text: string) { export default function parseDocumentIds(text: string): string[] {
const value = Markdown.deserialize(text); const value = Markdown.deserialize(text);
let links = []; let links = [];