fix backlink count

This commit is contained in:
zadam 2021-12-24 21:01:36 +01:00
parent 9d38e9342d
commit a232035d47
3 changed files with 36 additions and 13 deletions

View file

@ -103,18 +103,20 @@ export default class BacklinksWidget extends NoteContextAwareWidget {
async refreshWithNote(note) { async refreshWithNote(note) {
this.clearItems(); this.clearItems();
const targetRelationCount = note.getTargetRelations().length; // can't use froca since that would count only relations from loaded notes
if (targetRelationCount === 0) { const resp = await server.get(`notes/${this.noteId}/backlink-count`);
if (!resp || !resp.count) {
this.$ticker.hide(); this.$ticker.hide();
return;
} }
else {
this.$ticker.show(); this.$ticker.show();
this.$count.text( this.$count.text(
`${targetRelationCount} backlink` `${resp.count} backlink`
+ (targetRelationCount === 1 ? '' : 's') + (resp.count === 1 ? '' : 's')
); );
} }
}
clearItems() { clearItems() {
this.$items.empty().hide(); this.$items.empty().hide();
@ -136,18 +138,22 @@ export default class BacklinksWidget extends NoteContextAwareWidget {
await froca.getNotes(backlinks.map(bl => bl.noteId)); // prefetch all await froca.getNotes(backlinks.map(bl => bl.noteId)); // prefetch all
for (const backlink of backlinks) { for (const backlink of backlinks) {
this.$items.append(await linkService.createNoteLink(backlink.noteId, { const $item = $("<div>");
$item.append(await linkService.createNoteLink(backlink.noteId, {
showNoteIcon: true, showNoteIcon: true,
showNotePath: true, showNotePath: true,
showTooltip: false showTooltip: false
})); }));
if (backlink.relationName) { if (backlink.relationName) {
this.$items.append($("<p>").text("relation: " + backlink.relationName)); $item.append($("<p>").text("relation: " + backlink.relationName));
} }
else { else {
this.$items.append(...backlink.excerpts); $item.append(...backlink.excerpts);
} }
this.$items.append($item);
} }
} }
} }

View file

@ -302,6 +302,21 @@ function uploadModifiedFile(req) {
note.setContent(fileContent); 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().length
};
}
}
module.exports = { module.exports = {
getNote, getNote,
updateNote, updateNote,
@ -316,5 +331,6 @@ module.exports = {
duplicateSubtree, duplicateSubtree,
eraseDeletedNotesNow, eraseDeletedNotesNow,
getDeleteNotesPreview, getDeleteNotesPreview,
uploadModifiedFile uploadModifiedFile,
getBacklinkCount
}; };

View file

@ -220,6 +220,7 @@ function register(app) {
apiRoute(DELETE, '/api/notes/:noteId/revisions/:noteRevisionId', noteRevisionsApiRoute.eraseNoteRevision); apiRoute(DELETE, '/api/notes/:noteId/revisions/:noteRevisionId', noteRevisionsApiRoute.eraseNoteRevision);
route(GET, '/api/notes/:noteId/revisions/:noteRevisionId/download', [auth.checkApiAuthOrElectron], noteRevisionsApiRoute.downloadNoteRevision); route(GET, '/api/notes/:noteId/revisions/:noteRevisionId/download', [auth.checkApiAuthOrElectron], noteRevisionsApiRoute.downloadNoteRevision);
apiRoute(PUT, '/api/notes/:noteId/restore-revision/:noteRevisionId', noteRevisionsApiRoute.restoreNoteRevision); 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/relation-map', notesApiRoute.getRelationMap);
apiRoute(POST, '/api/notes/erase-deleted-notes-now', notesApiRoute.eraseDeletedNotesNow); apiRoute(POST, '/api/notes/erase-deleted-notes-now', notesApiRoute.eraseDeletedNotesNow);
apiRoute(PUT, '/api/notes/:noteId/change-title', notesApiRoute.changeTitle); apiRoute(PUT, '/api/notes/:noteId/change-title', notesApiRoute.changeTitle);