Skip to content

Build your personal knowledge base with Trilium Notes

Merge branch 'master' into next53

zadam0f7fa7a7b7b6
diff --cc src/routes/api/search.js
index 0c020e7468,9e4e9ce154..f164a290fa
--- a/src/routes/api/search.js
+++ b/src/routes/api/search.js
@@@ -5,10 -5,13 +5,11 @@@ const SearchContext = require('../../se
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");
+ const utils = require("../../services/utils.js");
- async function searchFromNoteInt(note) {
+ function searchFromNoteInt(note) {
let searchResultNoteIds;
const searchScript = note.getRelationValue('searchScript');
@@@ -57,7 -60,99 +58,7 @@@ async function searchFromNote(req)
return await searchFromNoteInt(note);
}
- async function searchAndExecute(req) {
-const ACTION_HANDLERS = {
- deleteNote: (action, note) => {
- const deleteId = 'searchbulkaction-' + utils.randomString(10);
-
- note.deleteNote(deleteId);
- },
- 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)) {
- label.name = action.newLabelName;
- label.save();
- }
- },
- renameRelation: (action, note) => {
- for (const relation of note.getOwnedRelations(action.oldRelationName)) {
- relation.name = action.newRelationName;
- relation.save();
- }
- },
- setLabelValue: (action, note) => {
- note.setLabel(action.labelName, action.labelValue);
- },
- setRelationTarget: (action, note) => {
- note.setRelation(action.relationName, action.targetNoteId);
- },
- 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);
-}
-
+ function searchAndExecute(req) {
const note = becca.getNote(req.params.noteId);
if (!note) {
@@@ -73,9 -168,28 +74,9 @@@
return [400, `Note ${req.params.noteId} is not a search note.`]
}
- const searchResultNoteIds = await searchFromNoteInt(note);
+ const searchResultNoteIds = 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) {