Link entity migrated to Attribute, WIP

This commit is contained in:
zadam
2019-08-19 20:12:00 +02:00
parent fd9b79e115
commit 3cb421143f
22 changed files with 172 additions and 174 deletions

View File

@@ -9,7 +9,7 @@ const messagingService = require('../../services/messaging');
const log = require('../../services/log');
const utils = require('../../services/utils');
const path = require('path');
const Link = require('../../entities/link');
const Attribute = require('../../entities/attribute');
async function findClippingNote(todayNote, pageUrl) {
const notes = await todayNote.getDescendantNotesWithLabel('pageUrl', pageUrl);
@@ -84,10 +84,11 @@ async function addImagesToNote(images, note, content) {
const {note: imageNote, url} = await imageService.saveImage(buffer, filename, note.noteId, true);
await new Link({
await new Attribute({
noteId: note.noteId,
targetNoteId: imageNote.noteId,
type: 'image'
type: 'relation',
value: imageNote.noteId,
name: 'image-link'
}).save();
console.log(`Replacing ${imageId} with ${url}`);

View File

@@ -2,38 +2,34 @@
const sql = require('../../services/sql');
async function getLinks(noteIds, linkTypes) {
async function getRelations(noteIds, relationNames) {
return (await sql.getManyRows(`
SELECT noteId, targetNoteId, type
FROM links
WHERE (noteId IN (???) OR targetNoteId IN (???))
AND isDeleted = 0
UNION
SELECT noteId, value, 'relation'
SELECT noteId, name, value AS targetNoteId
FROM attributes
WHERE (noteId IN (???) OR value IN (???))
AND type = 'relation'
AND isDeleted = 0
`, Array.from(noteIds))).filter(l => linkTypes.includes(l.type));
`, Array.from(noteIds))).filter(l => relationNames.includes(l.name));
}
async function getLinkMap(req) {
const {noteId} = req.params;
const {linkTypes, maxNotes, maxDepth} = req.body;
const {relationNames, maxNotes, maxDepth} = req.body;
let noteIds = new Set([noteId]);
let links = [];
let relations;
let depth = 0;
while (true) {
links = await getLinks(noteIds, linkTypes);
relations = await getRelations(noteIds, relationNames);
if (depth === maxDepth) {
break;
}
const newNoteIds = new Set(links.map(l => l.noteId).concat(links.map(l => l.targetNoteId)));
const newNoteIds = new Set(relations.map(rel => rel.noteId)
.concat(relations.map(rel => rel.targetNoteId)));
if (newNoteIds.size === noteIds.size) {
// no new note discovered, no need to search any further
@@ -51,9 +47,9 @@ async function getLinkMap(req) {
}
// keep only links coming from and targetting some note in the noteIds set
links = links.filter(l => noteIds.has(l.noteId) && noteIds.has(l.targetNoteId));
relations = relations.filter(rel => noteIds.has(rel.noteId) && noteIds.has(rel.targetNoteId));
return links;
return relations;
}
module.exports = {

28
src/routes/api/links.js Normal file
View File

@@ -0,0 +1,28 @@
"use strict";
const repository = require('../../services/repository');
async function getLinks(req) {
const note = await repository.getNote(req.params.noteId);
if (!note) {
return [404, `Note ${req.params.noteId} not found`];
}
return await note.getLinks();
}
async function getIncomingLinks(req) {
const note = await repository.getNote(req.params.noteId);
if (!note) {
return [404, `Note ${req.params.noteId} not found`];
}
note.getTargetRelations()
}
module.exports = {
getLinks,
getIncomingLinks
};

View File

@@ -114,8 +114,7 @@ async function getRelationMap(req) {
noteTitles: {},
relations: [],
// relation name => inverse relation name
inverseRelations: {},
links: []
inverseRelations: {}
};
if (noteIds.length === 0) {
@@ -145,16 +144,6 @@ async function getRelationMap(req) {
}
}
resp.links = (await repository.getEntities(`SELECT * FROM links WHERE isDeleted = 0 AND noteId IN (${questionMarks})`, noteIds))
.filter(link => noteIds.includes(link.targetNoteId))
.map(link => {
return {
linkId: link.linkId,
sourceNoteId: link.noteId,
targetNoteId: link.targetNoteId
}
});
return resp;
}