mirror of
https://github.com/zadam/trilium.git
synced 2025-11-13 08:45:50 +01:00
link map WIP
This commit is contained in:
@@ -1,78 +1,81 @@
|
||||
"use strict";
|
||||
|
||||
const sql = require('../../services/sql');
|
||||
const becca = require("../../becca/becca");
|
||||
|
||||
function getRelations(noteIds) {
|
||||
noteIds = Array.from(noteIds);
|
||||
function getRelations(noteId) {
|
||||
const note = becca.getNote(noteId);
|
||||
|
||||
return [
|
||||
// first read all relations
|
||||
// some "system" relations are not included since they are rarely useful to see (#1820)
|
||||
...sql.getManyRows(`
|
||||
SELECT noteId, name, value AS targetNoteId
|
||||
FROM attributes
|
||||
WHERE (noteId IN (???) OR value IN (???))
|
||||
AND type = 'relation'
|
||||
AND name NOT IN ('imageLink', 'relationMapLink', 'template')
|
||||
AND isDeleted = 0
|
||||
AND noteId != ''
|
||||
AND value != ''`, noteIds),
|
||||
// ... then read only imageLink relations which are not connecting parent and child
|
||||
// this is done to not show image links in the trivial case where they are direct children of the note to which they are included. Same heuristic as in note tree
|
||||
...sql.getManyRows(`
|
||||
SELECT rel.noteId, rel.name, rel.value AS targetNoteId
|
||||
FROM attributes AS rel
|
||||
LEFT JOIN branches ON branches.parentNoteId = rel.noteId AND branches.noteId = rel.value AND branches.isDeleted = 0
|
||||
WHERE (rel.noteId IN (???) OR rel.value IN (???))
|
||||
AND rel.type = 'relation'
|
||||
AND rel.name = 'imageLink'
|
||||
AND rel.isDeleted = 0
|
||||
AND rel.noteId != ''
|
||||
AND rel.value != ''
|
||||
AND branches.branchId IS NULL`, noteIds)
|
||||
];
|
||||
if (!note) {
|
||||
throw new Error(noteId);
|
||||
}
|
||||
|
||||
const allRelations = note.getOwnedRelations().concat(note.getTargetRelations());
|
||||
|
||||
return allRelations.filter(rel => {
|
||||
if (rel.name === 'relationMapLink' || rel.name === 'template') {
|
||||
return false;
|
||||
}
|
||||
else if (rel.name === 'imageLink') {
|
||||
const parentNote = becca.getNote(rel.noteId);
|
||||
|
||||
return !parentNote.getChildNotes().find(childNote => childNote.noteId === rel.value);
|
||||
}
|
||||
else {
|
||||
return true;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function collectRelations(noteId, relations, depth) {
|
||||
if (depth === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (const relation of getRelations(noteId)) {
|
||||
if (!relations.has(relation)) {
|
||||
if (!relation.value) {
|
||||
continue;
|
||||
}
|
||||
|
||||
relations.add(relation);
|
||||
|
||||
if (relation.noteId !== noteId) {
|
||||
collectRelations(relation.noteId, relations, depth--);
|
||||
} else if (relation.value !== noteId) {
|
||||
collectRelations(relation.value, relations, depth--);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function getLinkMap(req) {
|
||||
const {noteId} = req.params;
|
||||
const {maxNotes, maxDepth} = req.body;
|
||||
const {maxDepth} = req.body;
|
||||
|
||||
let noteIds = new Set([noteId]);
|
||||
let relations;
|
||||
let relations = new Set();
|
||||
|
||||
let depth = 0;
|
||||
collectRelations(noteId, relations, maxDepth);
|
||||
|
||||
while (noteIds.size < maxNotes) {
|
||||
relations = getRelations(noteIds);
|
||||
relations = Array.from(relations);
|
||||
|
||||
if (depth === maxDepth) {
|
||||
break;
|
||||
}
|
||||
const noteIds = new Set(relations.map(rel => rel.noteId)
|
||||
.concat(relations.map(rel => rel.targetNoteId))
|
||||
.concat([noteId]));
|
||||
|
||||
let newNoteIds = relations.map(rel => rel.noteId)
|
||||
.concat(relations.map(rel => rel.targetNoteId))
|
||||
.filter(noteId => !noteIds.has(noteId));
|
||||
const noteIdToLinkCountMap = {};
|
||||
|
||||
if (newNoteIds.length === 0) {
|
||||
// no new note discovered, no need to search any further
|
||||
break;
|
||||
}
|
||||
|
||||
for (const newNoteId of newNoteIds) {
|
||||
noteIds.add(newNoteId);
|
||||
|
||||
if (noteIds.size >= maxNotes) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
depth++;
|
||||
for (const noteId of noteIds) {
|
||||
noteIdToLinkCountMap[noteId] = getRelations(noteId).length;
|
||||
}
|
||||
|
||||
// keep only links coming from and targetting some note in the noteIds set
|
||||
relations = relations.filter(rel => noteIds.has(rel.noteId) && noteIds.has(rel.targetNoteId));
|
||||
|
||||
return relations;
|
||||
return {
|
||||
noteIdToLinkCountMap,
|
||||
links: Array.from(relations).map(rel => ({
|
||||
sourceNoteId: rel.noteId,
|
||||
targetNoteId: rel.value,
|
||||
name: rel.name
|
||||
}))
|
||||
};
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
|
||||
Reference in New Issue
Block a user