From 14fb9c76b06e35f0f0cb9f20ce44f39f92e9dcf4 Mon Sep 17 00:00:00 2001 From: zadam Date: Sat, 22 Oct 2022 15:01:10 +0200 Subject: [PATCH] fix backlinks in day note subtree, fixes #3158 --- .../widgets/floating_buttons/zpetne_odkazy.js | 2 +- src/routes/api/note_map.js | 26 ++++++++++++++++--- src/routes/api/notes.js | 18 +------------ src/routes/routes.js | 2 +- 4 files changed, 26 insertions(+), 22 deletions(-) diff --git a/src/public/app/widgets/floating_buttons/zpetne_odkazy.js b/src/public/app/widgets/floating_buttons/zpetne_odkazy.js index 8761f20fb..d3f115f97 100644 --- a/src/public/app/widgets/floating_buttons/zpetne_odkazy.js +++ b/src/public/app/widgets/floating_buttons/zpetne_odkazy.js @@ -86,7 +86,7 @@ export default class BacklinksWidget extends NoteContextAwareWidget { this.clearItems(); // can't use froca since that would count only relations from loaded notes - const resp = await server.get(`notes/${this.noteId}/backlink-count`); + const resp = await server.get(`note-map/${this.noteId}/backlink-count`); if (!resp || !resp.count) { this.toggle(false); diff --git a/src/routes/api/note_map.js b/src/routes/api/note_map.js index 4cbfb8a82..812d4db0e 100644 --- a/src/routes/api/note_map.js +++ b/src/routes/api/note_map.js @@ -287,6 +287,27 @@ function findExcerpts(sourceNote, referencedNoteId) { return excerpts; } +function getFilteredBacklinks(note) { + return note.getTargetRelations() + // search notes have "ancestor" relations which are not interesting + .filter(note => note.getNote().type !== 'search'); +} + +function getBacklinkCount(req) { + const {noteId} = req.params; + + const note = becca.getNote(noteId); + + if (!note) { + return [404, "Not found"]; + } + else { + return { + count: getFilteredBacklinks(note).length + }; + } +} + function getBacklinks(req) { const {noteId} = req.params; const note = becca.getNote(noteId); @@ -295,11 +316,9 @@ function getBacklinks(req) { return [404, `Note ${noteId} was not found`]; } - let backlinks = note.getTargetRelations(); - let backlinksWithExcerptCount = 0; - return backlinks.filter(note => !note.getNote().hasLabel('excludeFromNoteMap')).map(backlink => { + return getFilteredBacklinks(note).map(backlink => { const sourceNote = backlink.note; if (sourceNote.type !== 'text' || backlinksWithExcerptCount > 50) { @@ -323,5 +342,6 @@ function getBacklinks(req) { module.exports = { getLinkMap, getTreeMap, + getBacklinkCount, getBacklinks }; diff --git a/src/routes/api/notes.js b/src/routes/api/notes.js index d21addaad..ea69e7de5 100644 --- a/src/routes/api/notes.js +++ b/src/routes/api/notes.js @@ -305,21 +305,6 @@ function uploadModifiedFile(req) { note.setContent(fileContent); } -function getBacklinkCount(req) { - const {noteId} = req.params; - - const note = becca.getNote(noteId); - - if (!note) { - return [404, "Not found"]; - } - else { - return { - count: note.getTargetRelations().filter(note => !note.getNote().hasLabel('excludeFromNoteMap')).length - }; - } -} - module.exports = { getNote, updateNoteContent, @@ -334,6 +319,5 @@ module.exports = { duplicateSubtree, eraseDeletedNotesNow, getDeleteNotesPreview, - uploadModifiedFile, - getBacklinkCount + uploadModifiedFile }; diff --git a/src/routes/routes.js b/src/routes/routes.js index 928204ba6..d843252f7 100644 --- a/src/routes/routes.js +++ b/src/routes/routes.js @@ -264,7 +264,6 @@ function register(app) { apiRoute(DELETE, '/api/notes/:noteId/revisions/:noteRevisionId', noteRevisionsApiRoute.eraseNoteRevision); route(GET, '/api/notes/:noteId/revisions/:noteRevisionId/download', [auth.checkApiAuthOrElectron], noteRevisionsApiRoute.downloadNoteRevision); apiRoute(PUT, '/api/notes/:noteId/restore-revision/:noteRevisionId', noteRevisionsApiRoute.restoreNoteRevision); - apiRoute(GET, '/api/notes/:noteId/backlink-count', notesApiRoute.getBacklinkCount); apiRoute(POST, '/api/notes/relation-map', notesApiRoute.getRelationMap); apiRoute(POST, '/api/notes/erase-deleted-notes-now', notesApiRoute.eraseDeletedNotesNow); apiRoute(PUT, '/api/notes/:noteId/title', notesApiRoute.changeTitle); @@ -306,6 +305,7 @@ function register(app) { apiRoute(POST, '/api/note-map/:noteId/tree', noteMapRoute.getTreeMap); apiRoute(POST, '/api/note-map/:noteId/link', noteMapRoute.getLinkMap); + apiRoute(GET, '/api/note-map/:noteId/backlink-count', noteMapRoute.getBacklinkCount); apiRoute(GET, '/api/note-map/:noteId/backlinks', noteMapRoute.getBacklinks); apiRoute(GET, '/api/special-notes/inbox/:date', specialNotesRoute.getInboxNote);