mirror of
https://github.com/zadam/trilium.git
synced 2025-11-03 03:46:37 +01:00
introduced bulk action groups
This commit is contained in:
@@ -5,9 +5,7 @@ const SearchContext = require('../../services/search/search_context');
|
||||
const log = require('../../services/log');
|
||||
const scriptService = require('../../services/script');
|
||||
const searchService = require('../../services/search/services/search');
|
||||
const noteRevisionService = require("../../services/note_revisions");
|
||||
const branchService = require("../../services/branches");
|
||||
const cloningService = require("../../services/cloning");
|
||||
const bulkActionService = require("../../services/bulk_actions");
|
||||
const {formatAttrForSearch} = require("../../services/attribute_formatter");
|
||||
|
||||
async function searchFromNoteInt(note) {
|
||||
@@ -59,108 +57,6 @@ async function searchFromNote(req) {
|
||||
return await searchFromNoteInt(note);
|
||||
}
|
||||
|
||||
const ACTION_HANDLERS = {
|
||||
deleteNote: (action, note) => {
|
||||
note.markAsDeleted();
|
||||
},
|
||||
deleteNoteRevisions: (action, note) => {
|
||||
noteRevisionService.eraseNoteRevisions(note.getNoteRevisions().map(rev => rev.noteRevisionId));
|
||||
},
|
||||
deleteLabel: (action, note) => {
|
||||
for (const label of note.getOwnedLabels(action.labelName)) {
|
||||
label.markAsDeleted();
|
||||
}
|
||||
},
|
||||
deleteRelation: (action, note) => {
|
||||
for (const relation of note.getOwnedRelations(action.relationName)) {
|
||||
relation.markAsDeleted();
|
||||
}
|
||||
},
|
||||
renameLabel: (action, note) => {
|
||||
for (const label of note.getOwnedLabels(action.oldLabelName)) {
|
||||
// attribute name is immutable, renaming means delete old + create new
|
||||
const newLabel = label.createClone('label', action.newLabelName, label.value);
|
||||
|
||||
newLabel.save();
|
||||
label.markAsDeleted();
|
||||
}
|
||||
},
|
||||
renameRelation: (action, note) => {
|
||||
for (const relation of note.getOwnedRelations(action.oldRelationName)) {
|
||||
// attribute name is immutable, renaming means delete old + create new
|
||||
const newRelation = relation.createClone('relation', action.newRelationName, relation.value);
|
||||
|
||||
newRelation.save();
|
||||
relation.markAsDeleted();
|
||||
}
|
||||
},
|
||||
updateLabelValue: (action, note) => {
|
||||
for (const label of note.getOwnedLabels(action.labelName)) {
|
||||
label.value = action.labelValue;
|
||||
label.save();
|
||||
}
|
||||
},
|
||||
updateRelationTarget: (action, note) => {
|
||||
for (const relation of note.getOwnedLabels(action.relationName)) {
|
||||
relation.value = action.targetNoteId;
|
||||
relation.save();
|
||||
}
|
||||
},
|
||||
moveNote: (action, note) => {
|
||||
const targetParentNote = becca.getNote(action.targetParentNoteId);
|
||||
|
||||
if (!targetParentNote) {
|
||||
return;
|
||||
}
|
||||
|
||||
let res;
|
||||
|
||||
if (note.getParentBranches().length > 1) {
|
||||
res = cloningService.cloneNoteToNote(note.noteId, action.targetParentNoteId);
|
||||
}
|
||||
else {
|
||||
res = branchService.moveBranchToNote(note.getParentBranches()[0], action.targetParentNoteId);
|
||||
}
|
||||
|
||||
if (!res.success) {
|
||||
log.info(`Moving/cloning note ${note.noteId} to ${action.targetParentNoteId} failed with error ${JSON.stringify(res)}`);
|
||||
}
|
||||
},
|
||||
executeScript: (action, note) => {
|
||||
if (!action.script || !action.script.trim()) {
|
||||
log.info("Ignoring executeScript since the script is empty.")
|
||||
return;
|
||||
}
|
||||
|
||||
const scriptFunc = new Function("note", action.script);
|
||||
scriptFunc(note);
|
||||
|
||||
note.save();
|
||||
}
|
||||
};
|
||||
|
||||
function getActions(note) {
|
||||
return note.getLabels('action')
|
||||
.map(actionLabel => {
|
||||
let action;
|
||||
|
||||
try {
|
||||
action = JSON.parse(actionLabel.value);
|
||||
} catch (e) {
|
||||
log.error(`Cannot parse '${actionLabel.value}' into search action, skipping.`);
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!(action.name in ACTION_HANDLERS)) {
|
||||
log.error(`Cannot find '${action.name}' search action handler, skipping.`);
|
||||
return null;
|
||||
}
|
||||
|
||||
return action;
|
||||
})
|
||||
.filter(a => !!a);
|
||||
}
|
||||
|
||||
async function searchAndExecute(req) {
|
||||
const note = becca.getNote(req.params.noteId);
|
||||
|
||||
@@ -179,26 +75,7 @@ async function searchAndExecute(req) {
|
||||
|
||||
const searchResultNoteIds = await searchFromNoteInt(note);
|
||||
|
||||
const actions = getActions(note);
|
||||
|
||||
for (const resultNoteId of searchResultNoteIds) {
|
||||
const resultNote = becca.getNote(resultNoteId);
|
||||
|
||||
if (!resultNote || resultNote.isDeleted) {
|
||||
continue;
|
||||
}
|
||||
|
||||
for (const action of actions) {
|
||||
try {
|
||||
log.info(`Applying action handler to note ${resultNote.noteId}: ${JSON.stringify(action)}`);
|
||||
|
||||
ACTION_HANDLERS[action.name](action, resultNote);
|
||||
}
|
||||
catch (e) {
|
||||
log.error(`ExecuteScript search action failed with ${e.message}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
bulkActionService.executeActions(note, searchResultNoteIds);
|
||||
}
|
||||
|
||||
function searchFromRelation(note, relationName) {
|
||||
|
||||
Reference in New Issue
Block a user