2024-04-05 20:36:10 +03:00
|
|
|
import { Request } from 'express';
|
2024-07-18 21:35:17 +03:00
|
|
|
import becca from "../../becca/becca.js";
|
|
|
|
import bulkActionService from "../../services/bulk_actions.js";
|
2022-06-12 00:05:46 +02:00
|
|
|
|
2024-04-05 20:36:10 +03:00
|
|
|
function execute(req: Request) {
|
2022-06-12 10:35:30 +02:00
|
|
|
const {noteIds, includeDescendants} = req.body;
|
|
|
|
|
|
|
|
const affectedNoteIds = getAffectedNoteIds(noteIds, includeDescendants);
|
2022-06-12 00:05:46 +02:00
|
|
|
|
2024-04-05 20:36:10 +03:00
|
|
|
const bulkActionNote = becca.getNoteOrThrow('_bulkAction');
|
2022-06-12 00:05:46 +02:00
|
|
|
|
2022-06-12 10:35:30 +02:00
|
|
|
bulkActionService.executeActions(bulkActionNote, affectedNoteIds);
|
|
|
|
}
|
|
|
|
|
2024-04-05 20:36:10 +03:00
|
|
|
function getAffectedNoteCount(req: Request) {
|
2022-06-12 10:35:30 +02:00
|
|
|
const {noteIds, includeDescendants} = req.body;
|
|
|
|
|
|
|
|
const affectedNoteIds = getAffectedNoteIds(noteIds, includeDescendants);
|
|
|
|
|
|
|
|
return {
|
|
|
|
affectedNoteCount: affectedNoteIds.size
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2024-04-05 20:36:10 +03:00
|
|
|
function getAffectedNoteIds(noteIds: string[], includeDescendants: boolean) {
|
|
|
|
const affectedNoteIds = new Set<string>();
|
2022-06-12 10:35:30 +02:00
|
|
|
|
|
|
|
for (const noteId of noteIds) {
|
|
|
|
const note = becca.getNote(noteId);
|
|
|
|
|
|
|
|
if (!note) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
affectedNoteIds.add(noteId);
|
|
|
|
|
|
|
|
if (includeDescendants) {
|
|
|
|
for (const descendantNoteId of note.getDescendantNoteIds()) {
|
|
|
|
affectedNoteIds.add(descendantNoteId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return affectedNoteIds;
|
2022-06-12 00:05:46 +02:00
|
|
|
}
|
|
|
|
|
2024-07-18 21:47:30 +03:00
|
|
|
export default {
|
2022-06-12 10:35:30 +02:00
|
|
|
execute,
|
|
|
|
getAffectedNoteCount
|
2022-06-12 00:05:46 +02:00
|
|
|
};
|