refactor(bulk_action): remake types & change method signature

This commit is contained in:
Elian Doran 2025-07-19 12:32:47 +03:00
parent daa4743967
commit e2c8443778
No known key found for this signature in database
3 changed files with 76 additions and 76 deletions

View File

@ -12,7 +12,7 @@ function execute(req: Request) {
const bulkActionNote = becca.getNoteOrThrow("_bulkAction"); const bulkActionNote = becca.getNoteOrThrow("_bulkAction");
bulkActionService.executeActions(bulkActionNote, affectedNoteIds); bulkActionService.executeActionsFromNote(bulkActionNote, affectedNoteIds);
} }
function getAffectedNoteCount(req: Request) { function getAffectedNoteCount(req: Request) {

View File

@ -40,7 +40,7 @@ function searchAndExecute(req: Request) {
const { searchResultNoteIds } = searchService.searchFromNote(note); const { searchResultNoteIds } = searchService.searchFromNote(note);
bulkActionService.executeActions(note, searchResultNoteIds); bulkActionService.executeActionsFromNote(note, searchResultNoteIds);
} }
function quickSearch(req: Request) { function quickSearch(req: Request) {

View File

@ -6,97 +6,88 @@ import { randomString } from "./utils.js";
import eraseService from "./erase.js"; import eraseService from "./erase.js";
import type BNote from "../becca/entities/bnote.js"; import type BNote from "../becca/entities/bnote.js";
interface AddLabelAction { type ActionHandlers = {
addLabel: {
labelName: string; labelName: string;
labelValue?: string; labelValue?: string;
} },
addRelation: {
interface AddRelationAction {
relationName: string; relationName: string;
targetNoteId: string; targetNoteId: string;
} },
deleteNote: {},
interface DeleteRevisionsAction {} deleteRevisions: {},
interface DeleteLabelAction { deleteLabel: {
labelName: string; labelName: string;
} },
deleteRelation: {
interface DeleteRelationAction {
relationName: string; relationName: string;
} },
renameNote: {
interface RenameNoteAction {
newTitle: string; newTitle: string;
} },
renameLabel: {
interface RenameLabelAction {
oldLabelName: string; oldLabelName: string;
newLabelName: string; newLabelName: string;
} },
renameRelation: {
interface RenameRelationAction {
oldRelationName: string; oldRelationName: string;
newRelationName: string; newRelationName: string;
} },
updateLabelValue: {
interface UpdateLabelValueAction {
labelName: string; labelName: string;
labelValue: string; labelValue: string;
} },
updateRelationTarget: {
interface UpdateRelationTargetAction {
relationName: string; relationName: string;
targetNoteId: string; targetNoteId: string;
} },
moveNote: {
interface MoveNoteAction {
targetParentNoteId: string; targetParentNoteId: string;
} },
executeScript: {
interface ExecuteScriptAction {
script: string; script: string;
} }
};
interface DeleteNoteAction { }
type BulkAction = AddLabelAction | AddRelationAction | DeleteNoteAction | DeleteRevisionsAction | DeleteLabelAction | DeleteRelationAction | RenameNoteAction | RenameLabelAction | RenameRelationAction | UpdateLabelValueAction | UpdateRelationTargetAction | MoveNoteAction | ExecuteScriptAction;
type ActionHandler<T> = (action: T, note: BNote) => void; type ActionHandler<T> = (action: T, note: BNote) => void;
type ActionHandlerMap = { type ActionHandlerMap = {
[K in keyof BulkAction]: ActionHandler<K>; [K in keyof ActionHandlers]: ActionHandler<ActionHandlers[K] & { name: K }>;
} };
export type BulkAction = { name: keyof ActionHandlers } & ActionHandlers[keyof ActionHandlers];
const ACTION_HANDLERS: ActionHandlerMap = { const ACTION_HANDLERS: ActionHandlerMap = {
addLabel: (action: AddLabelAction, note: BNote) => { addLabel: (action, note) => {
note.addLabel(action.labelName, action.labelValue); note.addLabel(action.labelName, action.labelValue);
}, },
addRelation: (action: AddRelationAction, note: BNote) => { addRelation: (action, note) => {
note.addRelation(action.relationName, action.targetNoteId); note.addRelation(action.relationName, action.targetNoteId);
}, },
deleteNote: (action: DeleteNoteAction, note: BNote) => { deleteNote: (action, note) => {
const deleteId = `searchbulkaction-${randomString(10)}`; const deleteId = `searchbulkaction-${randomString(10)}`;
note.deleteNote(deleteId); note.deleteNote(deleteId);
}, },
deleteRevisions: (action: DeleteRevisionsAction, note: BNote) => { deleteRevisions: (action, note) => {
const revisionIds = note const revisionIds = note
.getRevisions() .getRevisions()
.map((rev) => rev.revisionId) .map((rev) => rev.revisionId)
.filter((rev) => !!rev) as string[]; .filter((rev) => !!rev) as string[];
eraseService.eraseRevisions(revisionIds); eraseService.eraseRevisions(revisionIds);
}, },
deleteLabel: (action: DeleteLabelAction, note: BNote) => { deleteLabel: (action, note) => {
for (const label of note.getOwnedLabels(action.labelName)) { for (const label of note.getOwnedLabels(action.labelName)) {
label.markAsDeleted(); label.markAsDeleted();
} }
}, },
deleteRelation: (action: DeleteRelationAction, note: BNote) => { deleteRelation: (action, note) => {
for (const relation of note.getOwnedRelations(action.relationName)) { for (const relation of note.getOwnedRelations(action.relationName)) {
relation.markAsDeleted(); relation.markAsDeleted();
} }
}, },
renameNote: (action: RenameNoteAction, note: BNote) => { renameNote: (action, note) => {
// "officially" injected value: // "officially" injected value:
// - note // - note
@ -107,7 +98,7 @@ const ACTION_HANDLERS: ActionHandlerMap = {
note.save(); note.save();
} }
}, },
renameLabel: (action: RenameLabelAction, note: BNote) => { renameLabel: (action, note) => {
for (const label of note.getOwnedLabels(action.oldLabelName)) { for (const label of note.getOwnedLabels(action.oldLabelName)) {
// attribute name is immutable, renaming means delete old + create new // attribute name is immutable, renaming means delete old + create new
const newLabel = label.createClone("label", action.newLabelName, label.value); const newLabel = label.createClone("label", action.newLabelName, label.value);
@ -116,7 +107,7 @@ const ACTION_HANDLERS: ActionHandlerMap = {
label.markAsDeleted(); label.markAsDeleted();
} }
}, },
renameRelation: (action: RenameRelationAction, note: BNote) => { renameRelation: (action, note) => {
for (const relation of note.getOwnedRelations(action.oldRelationName)) { for (const relation of note.getOwnedRelations(action.oldRelationName)) {
// attribute name is immutable, renaming means delete old + create new // attribute name is immutable, renaming means delete old + create new
const newRelation = relation.createClone("relation", action.newRelationName, relation.value); const newRelation = relation.createClone("relation", action.newRelationName, relation.value);
@ -125,19 +116,19 @@ const ACTION_HANDLERS: ActionHandlerMap = {
relation.markAsDeleted(); relation.markAsDeleted();
} }
}, },
updateLabelValue: (action: UpdateLabelValueAction, note: BNote) => { updateLabelValue: (action, note) => {
for (const label of note.getOwnedLabels(action.labelName)) { for (const label of note.getOwnedLabels(action.labelName)) {
label.value = action.labelValue; label.value = action.labelValue;
label.save(); label.save();
} }
}, },
updateRelationTarget: (action: UpdateRelationTargetAction, note: BNote) => { updateRelationTarget: (action, note) => {
for (const relation of note.getOwnedRelations(action.relationName)) { for (const relation of note.getOwnedRelations(action.relationName)) {
relation.value = action.targetNoteId; relation.value = action.targetNoteId;
relation.save(); relation.save();
} }
}, },
moveNote: (action: MoveNoteAction, note: BNote) => { moveNote: (action, note) => {
const targetParentNote = becca.getNote(action.targetParentNoteId); const targetParentNote = becca.getNote(action.targetParentNoteId);
if (!targetParentNote) { if (!targetParentNote) {
@ -158,7 +149,7 @@ const ACTION_HANDLERS: ActionHandlerMap = {
log.info(`Moving/cloning note ${note.noteId} to ${action.targetParentNoteId} failed with error ${JSON.stringify(res)}`); log.info(`Moving/cloning note ${note.noteId} to ${action.targetParentNoteId} failed with error ${JSON.stringify(res)}`);
} }
}, },
executeScript: (action: ExecuteScriptAction, note: BNote) => { executeScript: (action, note) => {
if (!action.script || !action.script.trim()) { if (!action.script || !action.script.trim()) {
log.info("Ignoring executeScript since the script is empty."); log.info("Ignoring executeScript since the script is empty.");
return; return;
@ -169,7 +160,7 @@ const ACTION_HANDLERS: ActionHandlerMap = {
note.save(); note.save();
} }
}; } as const;
function getActions(note: BNote) { function getActions(note: BNote) {
return note return note
@ -189,15 +180,23 @@ function getActions(note: BNote) {
return null; return null;
} }
return action; return action as BulkAction;
}) })
.filter((a) => !!a); .filter((a) => !!a);
} }
function executeActions(note: BNote, searchResultNoteIds: string[] | Set<string>) { /**
* Executes the bulk actions defined in the note against the provided search result note IDs.
* @param note the note containing the bulk actions, read from the `action` label.
* @param noteIds the IDs of the notes to apply the actions to.
*/
function executeActionsFromNote(note: BNote, noteIds: string[] | Set<string>) {
const actions = getActions(note); const actions = getActions(note);
return executeActions(actions, noteIds);
}
for (const resultNoteId of searchResultNoteIds) { function executeActions(actions: BulkAction[], noteIds: string[] | Set<string>) {
for (const resultNoteId of noteIds) {
const resultNote = becca.getNote(resultNoteId); const resultNote = becca.getNote(resultNoteId);
if (!resultNote) { if (!resultNote) {
@ -217,5 +216,6 @@ function executeActions(note: BNote, searchResultNoteIds: string[] | Set<string>
} }
export default { export default {
executeActions executeActions,
executeActionsFromNote
}; };