server-ts: Port services/erase

This commit is contained in:
Elian Doran 2024-02-17 21:13:04 +02:00
parent f31d788e2e
commit 77fb7bc6e8
No known key found for this signature in database
7 changed files with 37 additions and 44 deletions

View File

@ -4,7 +4,7 @@ const sql = require('../../services/sql');
const utils = require('../../services/utils'); const utils = require('../../services/utils');
const entityChangesService = require('../../services/entity_changes'); const entityChangesService = require('../../services/entity_changes');
const treeService = require('../../services/tree.js'); const treeService = require('../../services/tree.js');
const eraseService = require('../../services/erase.js'); const eraseService = require('../../services/erase');
const becca = require('../../becca/becca'); const becca = require('../../becca/becca');
const TaskContext = require('../../services/task_context'); const TaskContext = require('../../services/task_context');
const branchService = require('../../services/branches.js'); const branchService = require('../../services/branches.js');

View File

@ -1,7 +1,7 @@
"use strict"; "use strict";
const noteService = require('../../services/notes.js'); const noteService = require('../../services/notes.js');
const eraseService = require('../../services/erase.js'); const eraseService = require('../../services/erase');
const treeService = require('../../services/tree.js'); const treeService = require('../../services/tree.js');
const sql = require('../../services/sql'); const sql = require('../../services/sql');
const utils = require('../../services/utils'); const utils = require('../../services/utils');

View File

@ -8,7 +8,7 @@ const cls = require('../../services/cls');
const path = require('path'); const path = require('path');
const becca = require('../../becca/becca'); const becca = require('../../becca/becca');
const blobService = require('../../services/blob'); const blobService = require('../../services/blob');
const eraseService = require("../../services/erase.js"); const eraseService = require("../../services/erase");
function getRevisionBlob(req) { function getRevisionBlob(req) {
const preview = req.query.preview === 'true'; const preview = req.query.preview === 'true';

View File

@ -4,7 +4,7 @@ const becca = require('../becca/becca');
const cloningService = require('./cloning.js'); const cloningService = require('./cloning.js');
const branchService = require('./branches.js'); const branchService = require('./branches.js');
const utils = require('./utils'); const utils = require('./utils');
const eraseService = require("./erase.js"); const eraseService = require("./erase");
const ACTION_HANDLERS = { const ACTION_HANDLERS = {
addLabel: (action, note) => { addLabel: (action, note) => {

View File

@ -12,7 +12,7 @@ const BBranch = require('../becca/entities/bbranch');
const revisionService = require('./revisions'); const revisionService = require('./revisions');
const becca = require('../becca/becca'); const becca = require('../becca/becca');
const utils = require('../services/utils'); const utils = require('../services/utils');
const eraseService = require('../services/erase.js'); const eraseService = require('../services/erase');
const {sanitizeAttributeName} = require('./sanitize_attribute_name'); const {sanitizeAttributeName} = require('./sanitize_attribute_name');
const noteTypes = require('../services/note_types.js').getNoteTypeNames(); const noteTypes = require('../services/note_types.js').getNoteTypeNames();

View File

@ -3,7 +3,7 @@
const sql = require('./sql'); const sql = require('./sql');
const utils = require('./utils'); const utils = require('./utils');
const log = require('./log'); const log = require('./log');
const eraseService = require('./erase.js'); const eraseService = require('./erase');
function getEntityHashes() { function getEntityHashes() {
// blob erasure is not synced, we should check before each sync if there's some blob to erase // blob erasure is not synced, we should check before each sync if there's some blob to erase

View File

@ -1,13 +1,14 @@
const sql = require("./sql"); import sql = require("./sql");
const revisionService = require("./revisions"); import revisionService = require("./revisions");
const log = require("./log.ts"); import log = require("./log");
const entityChangesService = require("./entity_changes"); import entityChangesService = require("./entity_changes");
const optionService = require("./options"); import optionService = require("./options");
const dateUtils = require("./date_utils"); import dateUtils = require("./date_utils");
const sqlInit = require("./sql_init"); import sqlInit = require("./sql_init");
const cls = require("./cls"); import cls = require("./cls");
import { EntityChange } from "./entity_changes_interface";
function eraseNotes(noteIdsToErase) { function eraseNotes(noteIdsToErase: string[]) {
if (noteIdsToErase.length === 0) { if (noteIdsToErase.length === 0) {
return; return;
} }
@ -16,17 +17,17 @@ function eraseNotes(noteIdsToErase) {
setEntityChangesAsErased(sql.getManyRows(`SELECT * FROM entity_changes WHERE entityName = 'notes' AND entityId IN (???)`, noteIdsToErase)); setEntityChangesAsErased(sql.getManyRows(`SELECT * FROM entity_changes WHERE entityName = 'notes' AND entityId IN (???)`, noteIdsToErase));
// we also need to erase all "dependent" entities of the erased notes // we also need to erase all "dependent" entities of the erased notes
const branchIdsToErase = sql.getManyRows(`SELECT branchId FROM branches WHERE noteId IN (???)`, noteIdsToErase) const branchIdsToErase = sql.getManyRows<{ branchId: string }>(`SELECT branchId FROM branches WHERE noteId IN (???)`, noteIdsToErase)
.map(row => row.branchId); .map(row => row.branchId);
eraseBranches(branchIdsToErase); eraseBranches(branchIdsToErase);
const attributeIdsToErase = sql.getManyRows(`SELECT attributeId FROM attributes WHERE noteId IN (???)`, noteIdsToErase) const attributeIdsToErase = sql.getManyRows<{ attributeId: string }>(`SELECT attributeId FROM attributes WHERE noteId IN (???)`, noteIdsToErase)
.map(row => row.attributeId); .map(row => row.attributeId);
eraseAttributes(attributeIdsToErase); eraseAttributes(attributeIdsToErase);
const revisionIdsToErase = sql.getManyRows(`SELECT revisionId FROM revisions WHERE noteId IN (???)`, noteIdsToErase) const revisionIdsToErase = sql.getManyRows<{ revisionId: string }>(`SELECT revisionId FROM revisions WHERE noteId IN (???)`, noteIdsToErase)
.map(row => row.revisionId); .map(row => row.revisionId);
eraseRevisions(revisionIdsToErase); eraseRevisions(revisionIdsToErase);
@ -34,7 +35,7 @@ function eraseNotes(noteIdsToErase) {
log.info(`Erased notes: ${JSON.stringify(noteIdsToErase)}`); log.info(`Erased notes: ${JSON.stringify(noteIdsToErase)}`);
} }
function setEntityChangesAsErased(entityChanges) { function setEntityChangesAsErased(entityChanges: EntityChange[]) {
for (const ec of entityChanges) { for (const ec of entityChanges) {
ec.isErased = true; ec.isErased = true;
// we're not changing hash here, not sure if good or not // we're not changing hash here, not sure if good or not
@ -45,7 +46,7 @@ function setEntityChangesAsErased(entityChanges) {
} }
} }
function eraseBranches(branchIdsToErase) { function eraseBranches(branchIdsToErase: string[]) {
if (branchIdsToErase.length === 0) { if (branchIdsToErase.length === 0) {
return; return;
} }
@ -57,7 +58,7 @@ function eraseBranches(branchIdsToErase) {
log.info(`Erased branches: ${JSON.stringify(branchIdsToErase)}`); log.info(`Erased branches: ${JSON.stringify(branchIdsToErase)}`);
} }
function eraseAttributes(attributeIdsToErase) { function eraseAttributes(attributeIdsToErase: string[]) {
if (attributeIdsToErase.length === 0) { if (attributeIdsToErase.length === 0) {
return; return;
} }
@ -69,7 +70,7 @@ function eraseAttributes(attributeIdsToErase) {
log.info(`Erased attributes: ${JSON.stringify(attributeIdsToErase)}`); log.info(`Erased attributes: ${JSON.stringify(attributeIdsToErase)}`);
} }
function eraseAttachments(attachmentIdsToErase) { function eraseAttachments(attachmentIdsToErase: string[]) {
if (attachmentIdsToErase.length === 0) { if (attachmentIdsToErase.length === 0) {
return; return;
} }
@ -81,7 +82,7 @@ function eraseAttachments(attachmentIdsToErase) {
log.info(`Erased attachments: ${JSON.stringify(attachmentIdsToErase)}`); log.info(`Erased attachments: ${JSON.stringify(attachmentIdsToErase)}`);
} }
function eraseRevisions(revisionIdsToErase) { function eraseRevisions(revisionIdsToErase: string[]) {
if (revisionIdsToErase.length === 0) { if (revisionIdsToErase.length === 0) {
return; return;
} }
@ -116,7 +117,7 @@ function eraseUnusedBlobs() {
log.info(`Erased unused blobs: ${JSON.stringify(unusedBlobIds)}`); log.info(`Erased unused blobs: ${JSON.stringify(unusedBlobIds)}`);
} }
function eraseDeletedEntities(eraseEntitiesAfterTimeInSeconds = null) { function eraseDeletedEntities(eraseEntitiesAfterTimeInSeconds: number | null = null) {
// this is important also so that the erased entity changes are sent to the connected clients // this is important also so that the erased entity changes are sent to the connected clients
sql.transactional(() => { sql.transactional(() => {
if (eraseEntitiesAfterTimeInSeconds === null) { if (eraseEntitiesAfterTimeInSeconds === null) {
@ -125,41 +126,33 @@ function eraseDeletedEntities(eraseEntitiesAfterTimeInSeconds = null) {
const cutoffDate = new Date(Date.now() - eraseEntitiesAfterTimeInSeconds * 1000); const cutoffDate = new Date(Date.now() - eraseEntitiesAfterTimeInSeconds * 1000);
const noteIdsToErase = sql.getColumn("SELECT noteId FROM notes WHERE isDeleted = 1 AND utcDateModified <= ?", [dateUtils.utcDateTimeStr(cutoffDate)]); const noteIdsToErase = sql.getColumn<string>("SELECT noteId FROM notes WHERE isDeleted = 1 AND utcDateModified <= ?", [dateUtils.utcDateTimeStr(cutoffDate)]);
eraseNotes(noteIdsToErase); eraseNotes(noteIdsToErase);
const branchIdsToErase = sql.getColumn("SELECT branchId FROM branches WHERE isDeleted = 1 AND utcDateModified <= ?", [dateUtils.utcDateTimeStr(cutoffDate)]); const branchIdsToErase = sql.getColumn<string>("SELECT branchId FROM branches WHERE isDeleted = 1 AND utcDateModified <= ?", [dateUtils.utcDateTimeStr(cutoffDate)]);
eraseBranches(branchIdsToErase); eraseBranches(branchIdsToErase);
const attributeIdsToErase = sql.getColumn("SELECT attributeId FROM attributes WHERE isDeleted = 1 AND utcDateModified <= ?", [dateUtils.utcDateTimeStr(cutoffDate)]); const attributeIdsToErase = sql.getColumn<string>("SELECT attributeId FROM attributes WHERE isDeleted = 1 AND utcDateModified <= ?", [dateUtils.utcDateTimeStr(cutoffDate)]);
eraseAttributes(attributeIdsToErase); eraseAttributes(attributeIdsToErase);
const attachmentIdsToErase = sql.getColumn("SELECT attachmentId FROM attachments WHERE isDeleted = 1 AND utcDateModified <= ?", [dateUtils.utcDateTimeStr(cutoffDate)]); const attachmentIdsToErase = sql.getColumn<string>("SELECT attachmentId FROM attachments WHERE isDeleted = 1 AND utcDateModified <= ?", [dateUtils.utcDateTimeStr(cutoffDate)]);
eraseAttachments(attachmentIdsToErase); eraseAttachments(attachmentIdsToErase);
eraseUnusedBlobs(); eraseUnusedBlobs();
}); });
} }
function eraseNotesWithDeleteId(deleteId) { function eraseNotesWithDeleteId(deleteId: string) {
const noteIdsToErase = sql.getColumn("SELECT noteId FROM notes WHERE isDeleted = 1 AND deleteId = ?", [deleteId]); const noteIdsToErase = sql.getColumn<string>("SELECT noteId FROM notes WHERE isDeleted = 1 AND deleteId = ?", [deleteId]);
eraseNotes(noteIdsToErase); eraseNotes(noteIdsToErase);
const branchIdsToErase = sql.getColumn("SELECT branchId FROM branches WHERE isDeleted = 1 AND deleteId = ?", [deleteId]); const branchIdsToErase = sql.getColumn<string>("SELECT branchId FROM branches WHERE isDeleted = 1 AND deleteId = ?", [deleteId]);
eraseBranches(branchIdsToErase); eraseBranches(branchIdsToErase);
const attributeIdsToErase = sql.getColumn("SELECT attributeId FROM attributes WHERE isDeleted = 1 AND deleteId = ?", [deleteId]); const attributeIdsToErase = sql.getColumn<string>("SELECT attributeId FROM attributes WHERE isDeleted = 1 AND deleteId = ?", [deleteId]);
eraseAttributes(attributeIdsToErase); eraseAttributes(attributeIdsToErase);
const attachmentIdsToErase = sql.getColumn("SELECT attachmentId FROM attachments WHERE isDeleted = 1 AND deleteId = ?", [deleteId]); const attachmentIdsToErase = sql.getColumn<string>("SELECT attachmentId FROM attachments WHERE isDeleted = 1 AND deleteId = ?", [deleteId]);
eraseAttachments(attachmentIdsToErase); eraseAttachments(attachmentIdsToErase);
eraseUnusedBlobs(); eraseUnusedBlobs();
@ -173,13 +166,13 @@ function eraseUnusedAttachmentsNow() {
eraseScheduledAttachments(0); eraseScheduledAttachments(0);
} }
function eraseScheduledAttachments(eraseUnusedAttachmentsAfterSeconds = null) { function eraseScheduledAttachments(eraseUnusedAttachmentsAfterSeconds: number | null = null) {
if (eraseUnusedAttachmentsAfterSeconds === null) { if (eraseUnusedAttachmentsAfterSeconds === null) {
eraseUnusedAttachmentsAfterSeconds = optionService.getOptionInt('eraseUnusedAttachmentsAfterSeconds'); eraseUnusedAttachmentsAfterSeconds = optionService.getOptionInt('eraseUnusedAttachmentsAfterSeconds');
} }
const cutOffDate = dateUtils.utcDateTimeStr(new Date(Date.now() - (eraseUnusedAttachmentsAfterSeconds * 1000))); const cutOffDate = dateUtils.utcDateTimeStr(new Date(Date.now() - (eraseUnusedAttachmentsAfterSeconds * 1000)));
const attachmentIdsToErase = sql.getColumn('SELECT attachmentId FROM attachments WHERE utcDateScheduledForErasureSince < ?', [cutOffDate]); const attachmentIdsToErase = sql.getColumn<string>('SELECT attachmentId FROM attachments WHERE utcDateScheduledForErasureSince < ?', [cutOffDate]);
eraseAttachments(attachmentIdsToErase); eraseAttachments(attachmentIdsToErase);
} }
@ -193,7 +186,7 @@ sqlInit.dbReady.then(() => {
setInterval(cls.wrap(() => eraseScheduledAttachments()), 3600 * 1000); setInterval(cls.wrap(() => eraseScheduledAttachments()), 3600 * 1000);
}); });
module.exports = { export = {
eraseDeletedNotesNow, eraseDeletedNotesNow,
eraseUnusedAttachmentsNow, eraseUnusedAttachmentsNow,
eraseNotesWithDeleteId, eraseNotesWithDeleteId,