Notes/src/services/bulk_actions.ts

178 lines
5.4 KiB
TypeScript
Raw Normal View History

2024-02-18 13:23:37 +02:00
import log = require('./log');
import becca = require('../becca/becca');
import cloningService = require('./cloning');
import branchService = require('./branches');
import utils = require('./utils');
import eraseService = require("./erase");
import BNote = require('../becca/entities/bnote');
interface Action {
labelName: string;
labelValue: string;
oldLabelName: string;
newLabelName: string;
relationName: string;
oldRelationName: string;
newRelationName: string;
targetNoteId: string;
targetParentNoteId: string;
newTitle: string;
script: string;
}
type ActionHandler = (action: Action, note: BNote) => void;
const ACTION_HANDLERS: Record<string, ActionHandler> = {
2022-06-05 23:36:46 +02:00
addLabel: (action, note) => {
note.addLabel(action.labelName, action.labelValue);
},
addRelation: (action, note) => {
note.addRelation(action.relationName, action.targetNoteId);
},
deleteNote: (action, note) => {
const deleteId = `searchbulkaction-${utils.randomString(10)}`;
2022-06-08 22:52:17 +02:00
note.deleteNote(deleteId);
2022-06-05 23:36:46 +02:00
},
deleteRevisions: (action, note) => {
2024-02-18 13:23:37 +02:00
const revisionIds = note.getRevisions()
.map(rev => rev.revisionId)
.filter((rev) => !!rev) as string[];
eraseService.eraseRevisions(revisionIds);
2022-06-05 23:36:46 +02:00
},
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();
}
},
2022-06-12 23:29:11 +02:00
renameNote: (action, note) => {
// "officially" injected value:
// - note
const newTitle = eval(`\`${action.newTitle}\``);
2022-06-12 23:29:11 +02:00
if (note.title !== newTitle) {
note.title = newTitle;
note.save();
}
},
2022-06-05 23:36:46 +02:00
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) => {
2022-06-17 23:12:52 +02:00
for (const relation of note.getOwnedRelations(action.relationName)) {
2022-06-05 23:36:46 +02:00
relation.value = action.targetNoteId;
relation.save();
}
},
moveNote: (action, note) => {
const targetParentNote = becca.getNote(action.targetParentNoteId);
if (!targetParentNote) {
log.info(`Cannot execute moveNote because note ${action.targetParentNoteId} doesn't exist.`);
2022-06-05 23:36:46 +02:00
return;
}
let res;
if (note.getParentBranches().length > 1) {
2023-04-15 00:06:13 +02:00
res = cloningService.cloneNoteToParentNote(note.noteId, action.targetParentNoteId);
2022-06-05 23:36:46 +02:00
}
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();
}
};
2024-02-18 13:23:37 +02:00
function getActions(note: BNote) {
2022-06-05 23:36:46 +02:00
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);
}
2024-02-18 13:23:37 +02:00
function executeActions(note: BNote, searchResultNoteIds: string[]) {
2022-06-05 23:36:46 +02:00
const actions = getActions(note);
for (const resultNoteId of searchResultNoteIds) {
const resultNote = becca.getNote(resultNoteId);
2023-06-05 09:23:42 +02:00
if (!resultNote) {
2022-06-05 23:36:46 +02:00
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);
2024-02-18 13:23:37 +02:00
} catch (e: any) {
2022-06-05 23:36:46 +02:00
log.error(`ExecuteScript search action failed with ${e.message}`);
}
}
}
}
2024-02-18 13:23:37 +02:00
export = {
2022-06-05 23:36:46 +02:00
executeActions
};