mirror of
https://github.com/zadam/trilium.git
synced 2025-11-16 02:05:53 +01:00
cleanup of labels and relations from backend
This commit is contained in:
@@ -6,7 +6,9 @@ const repository = require('../../services/repository');
|
||||
const Attribute = require('../../entities/attribute');
|
||||
|
||||
async function getEffectiveNoteAttributes(req) {
|
||||
return await attributeService.getEffectiveAttributes(req.params.noteId);
|
||||
const note = await repository.getNote(req.params.noteId);
|
||||
|
||||
return await note.getAttributes();
|
||||
}
|
||||
|
||||
async function updateNoteAttribute(req) {
|
||||
@@ -87,7 +89,9 @@ async function updateNoteAttributes(req) {
|
||||
await attributeEntity.save();
|
||||
}
|
||||
|
||||
return await attributeService.getEffectiveAttributes(noteId);
|
||||
const note = await repository.getNote(noteId);
|
||||
|
||||
return await note.getAttributes();
|
||||
}
|
||||
|
||||
async function getAttributeNames(req) {
|
||||
|
||||
@@ -113,15 +113,18 @@ async function exportToTar(branchId, res) {
|
||||
prefix: branch.prefix,
|
||||
type: note.type,
|
||||
mime: note.mime,
|
||||
labels: (await note.getLabels()).map(label => {
|
||||
attributes: (await note.getOwnedAttributes()).map(attribute => {
|
||||
return {
|
||||
name: label.name,
|
||||
value: label.value
|
||||
type: attribute.type,
|
||||
name: attribute.name,
|
||||
value: attribute.value,
|
||||
isInheritable: attribute.isInheritable,
|
||||
position: attribute.position
|
||||
};
|
||||
})
|
||||
};
|
||||
|
||||
if (metadata.labels.find(label => label.name === 'excludeFromExport')) {
|
||||
if (metadata.attributes.find(attributes => attributes.type === 'label' && attributes.name === 'excludeFromExport')) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
"use strict";
|
||||
|
||||
const noteService = require('../../services/notes');
|
||||
const labelService = require('../../services/labels');
|
||||
const attributeService = require('../../services/attributes');
|
||||
const protectedSessionService = require('../../services/protected_session');
|
||||
const repository = require('../../services/repository');
|
||||
|
||||
@@ -26,8 +26,8 @@ async function uploadFile(req) {
|
||||
mime: file.mimetype
|
||||
});
|
||||
|
||||
await labelService.createLabel(note.noteId, "originalFileName", originalName);
|
||||
await labelService.createLabel(note.noteId, "fileSize", size);
|
||||
await attributeService.createLabel(note.noteId, "originalFileName", originalName);
|
||||
await attributeService.createLabel(note.noteId, "fileSize", size);
|
||||
|
||||
return {
|
||||
noteId: note.noteId
|
||||
@@ -47,8 +47,8 @@ async function downloadFile(req, res) {
|
||||
return;
|
||||
}
|
||||
|
||||
const labelMap = await note.getLabelMap();
|
||||
const fileName = labelMap.originalFileName || note.title;
|
||||
const originalFileName = await note.getLabel('originalFileName');
|
||||
const fileName = originalFileName.value || note.title;
|
||||
|
||||
res.setHeader('Content-Disposition', 'file; filename="' + fileName + '"');
|
||||
res.setHeader('Content-Type', note.mime);
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
"use strict";
|
||||
|
||||
const repository = require('../../services/repository');
|
||||
const labelService = require('../../services/labels');
|
||||
const attributeService = require('../../services/attributes');
|
||||
const noteService = require('../../services/notes');
|
||||
const Branch = require('../../entities/branch');
|
||||
const tar = require('tar-stream');
|
||||
@@ -187,8 +187,8 @@ async function importNotes(files, parentNoteId, noteIdMap) {
|
||||
|
||||
noteIdMap[file.meta.noteId] = note.noteId;
|
||||
|
||||
for (const label of file.meta.labels) {
|
||||
await labelService.createLabel(note.noteId, label.name, label.value);
|
||||
for (const attribute of file.meta.attributes) {
|
||||
await attributeService.createAttribute(attribute);
|
||||
}
|
||||
|
||||
if (file.children.length > 0) {
|
||||
|
||||
@@ -1,70 +0,0 @@
|
||||
"use strict";
|
||||
|
||||
const sql = require('../../services/sql');
|
||||
const labelService = require('../../services/labels');
|
||||
const repository = require('../../services/repository');
|
||||
const Label = require('../../entities/label');
|
||||
|
||||
async function getNoteLabels(req) {
|
||||
const noteId = req.params.noteId;
|
||||
|
||||
return await repository.getEntities("SELECT * FROM labels WHERE isDeleted = 0 AND noteId = ? ORDER BY position, dateCreated", [noteId]);
|
||||
}
|
||||
|
||||
async function updateNoteLabels(req) {
|
||||
const noteId = req.params.noteId;
|
||||
const labels = req.body;
|
||||
|
||||
for (const label of labels) {
|
||||
let labelEntity;
|
||||
|
||||
if (label.labelId) {
|
||||
labelEntity = await repository.getLabel(label.labelId);
|
||||
}
|
||||
else {
|
||||
// if it was "created" and then immediatelly deleted, we just don't create it at all
|
||||
if (label.isDeleted) {
|
||||
continue;
|
||||
}
|
||||
|
||||
labelEntity = new Label();
|
||||
labelEntity.noteId = noteId;
|
||||
}
|
||||
|
||||
labelEntity.name = label.name;
|
||||
labelEntity.value = label.value;
|
||||
labelEntity.position = label.position;
|
||||
labelEntity.isDeleted = label.isDeleted;
|
||||
|
||||
await labelEntity.save();
|
||||
}
|
||||
|
||||
return await repository.getEntities("SELECT * FROM labels WHERE isDeleted = 0 AND noteId = ? ORDER BY position, dateCreated", [noteId]);
|
||||
}
|
||||
|
||||
async function getAllLabelNames() {
|
||||
const names = await sql.getColumn("SELECT DISTINCT name FROM labels WHERE isDeleted = 0");
|
||||
|
||||
for (const label of labelService.BUILTIN_LABELS) {
|
||||
if (!names.includes(label)) {
|
||||
names.push(label);
|
||||
}
|
||||
}
|
||||
|
||||
names.sort();
|
||||
|
||||
return names;
|
||||
}
|
||||
|
||||
async function getValuesForLabel(req) {
|
||||
const labelName = req.params.labelName;
|
||||
|
||||
return await sql.getColumn("SELECT DISTINCT value FROM labels WHERE isDeleted = 0 AND name = ? AND value != '' ORDER BY value", [labelName]);
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
getNoteLabels,
|
||||
updateNoteLabels,
|
||||
getAllLabelNames,
|
||||
getValuesForLabel
|
||||
};
|
||||
@@ -1,64 +0,0 @@
|
||||
"use strict";
|
||||
|
||||
const sql = require('../../services/sql');
|
||||
const relationService = require('../../services/relations');
|
||||
const repository = require('../../services/repository');
|
||||
const Relation = require('../../entities/relation');
|
||||
|
||||
async function getNoteRelations(req) {
|
||||
const noteId = req.params.noteId;
|
||||
|
||||
return await repository.getEntities("SELECT * FROM relations WHERE isDeleted = 0 AND sourceNoteId = ? ORDER BY position, dateCreated", [noteId]);
|
||||
}
|
||||
|
||||
async function updateNoteRelations(req) {
|
||||
const noteId = req.params.noteId;
|
||||
const relations = req.body;
|
||||
|
||||
for (const relation of relations) {
|
||||
let relationEntity;
|
||||
|
||||
if (relation.relationId) {
|
||||
relationEntity = await repository.getRelation(relation.relationId);
|
||||
}
|
||||
else {
|
||||
// if it was "created" and then immediatelly deleted, we just don't create it at all
|
||||
if (relation.isDeleted) {
|
||||
continue;
|
||||
}
|
||||
|
||||
relationEntity = new Relation();
|
||||
relationEntity.sourceNoteId = noteId;
|
||||
}
|
||||
|
||||
relationEntity.name = relation.name;
|
||||
relationEntity.targetNoteId = relation.targetNoteId;
|
||||
relationEntity.isInheritable = relation.isInheritable;
|
||||
relationEntity.position = relation.position;
|
||||
relationEntity.isDeleted = relation.isDeleted;
|
||||
|
||||
await relationEntity.save();
|
||||
}
|
||||
|
||||
return await repository.getEntities("SELECT * FROM relations WHERE isDeleted = 0 AND sourceNoteId = ? ORDER BY position, dateCreated", [noteId]);
|
||||
}
|
||||
|
||||
async function getAllRelationNames() {
|
||||
const names = await sql.getColumn("SELECT DISTINCT name FROM relations WHERE isDeleted = 0");
|
||||
|
||||
for (const relationName of relationService.BUILTIN_RELATIONS) {
|
||||
if (!names.includes(relationName)) {
|
||||
names.push(relationName);
|
||||
}
|
||||
}
|
||||
|
||||
names.sort();
|
||||
|
||||
return names;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
getNoteRelations,
|
||||
updateNoteRelations,
|
||||
getAllRelationNames
|
||||
};
|
||||
@@ -1,6 +1,5 @@
|
||||
"use strict";
|
||||
|
||||
const labelService = require('../../services/labels');
|
||||
const scriptService = require('../../services/script');
|
||||
const attributeService = require('../../services/attributes');
|
||||
const repository = require('../../services/repository');
|
||||
@@ -21,7 +20,7 @@ async function run(req) {
|
||||
}
|
||||
|
||||
async function getStartupBundles() {
|
||||
const notes = await labelService.getNotesWithLabel("run", "frontendStartup");
|
||||
const notes = await attributeService.getNotesWithLabel("run", "frontendStartup");
|
||||
|
||||
const bundles = [];
|
||||
|
||||
@@ -38,9 +37,10 @@ async function getStartupBundles() {
|
||||
|
||||
async function getRelationBundles(req) {
|
||||
const noteId = req.params.noteId;
|
||||
const note = await repository.getNote(noteId);
|
||||
const relationName = req.params.relationName;
|
||||
|
||||
const attributes = await attributeService.getEffectiveAttributes(noteId);
|
||||
const attributes = await note.getAttributes();
|
||||
const filtered = attributes.filter(attr => attr.type === 'relation' && attr.name === relationName);
|
||||
const targetNoteIds = filtered.map(relation => relation.value);
|
||||
const uniqueNoteIds = Array.from(new Set(targetNoteIds));
|
||||
|
||||
Reference in New Issue
Block a user