mirror of
				https://github.com/TriliumNext/Notes.git
				synced 2025-11-04 15:11:31 +08:00 
			
		
		
		
	server-ts: Convert more classes, including entity_changes.js
This commit is contained in:
		
							parent
							
								
									cb14d4d8f9
								
							
						
					
					
						commit
						e04bd5aaf0
					
				@ -6,4 +6,4 @@ class NotFoundError {
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
module.exports = NotFoundError;
 | 
					export = NotFoundError;
 | 
				
			||||||
							
								
								
									
										5
									
								
								src/services/blob-interface.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										5
									
								
								src/services/blob-interface.ts
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,5 @@
 | 
				
			|||||||
 | 
					export interface Blob {
 | 
				
			||||||
 | 
					    blobId: string;
 | 
				
			||||||
 | 
					    content: Buffer;
 | 
				
			||||||
 | 
					    utcDateModified: string;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@ -2,6 +2,7 @@ import becca = require('../becca/becca.js');
 | 
				
			|||||||
import NotFoundError = require('../errors/not_found_error');
 | 
					import NotFoundError = require('../errors/not_found_error');
 | 
				
			||||||
import protectedSessionService = require('./protected_session');
 | 
					import protectedSessionService = require('./protected_session');
 | 
				
			||||||
import utils = require('./utils');
 | 
					import utils = require('./utils');
 | 
				
			||||||
 | 
					import type { Blob } from "./blob-interface";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function getBlobPojo(entityName: string, entityId: string) {
 | 
					function getBlobPojo(entityName: string, entityId: string) {
 | 
				
			||||||
    const entity = becca.getEntity(entityName, entityId);
 | 
					    const entity = becca.getEntity(entityName, entityId);
 | 
				
			||||||
@ -48,11 +49,11 @@ function processContent(content: Buffer | string | null, isProtected: boolean, i
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function calculateContentHash({blobId, content}: { blobId: string, content: Buffer }) {
 | 
					function calculateContentHash({blobId, content}: Blob) {
 | 
				
			||||||
    return utils.hash(`${blobId}|${content.toString()}`);
 | 
					    return utils.hash(`${blobId}|${content.toString()}`);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
module.exports = {
 | 
					export = {
 | 
				
			||||||
    getBlobPojo,
 | 
					    getBlobPojo,
 | 
				
			||||||
    processContent,
 | 
					    processContent,
 | 
				
			||||||
    calculateContentHash
 | 
					    calculateContentHash
 | 
				
			||||||
 | 
				
			|||||||
@ -95,7 +95,7 @@ function validateUtcDateTime(str: string) {
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
module.exports = {
 | 
					export = {
 | 
				
			||||||
    utcNowDateTime,
 | 
					    utcNowDateTime,
 | 
				
			||||||
    localNowDateTime,
 | 
					    localNowDateTime,
 | 
				
			||||||
    localNowDate,
 | 
					    localNowDate,
 | 
				
			||||||
 | 
				
			|||||||
@ -1,202 +0,0 @@
 | 
				
			|||||||
const sql = require('./sql');
 | 
					 | 
				
			||||||
const dateUtils = require('./date_utils');
 | 
					 | 
				
			||||||
const log = require('./log');
 | 
					 | 
				
			||||||
const cls = require('./cls');
 | 
					 | 
				
			||||||
const utils = require('./utils');
 | 
					 | 
				
			||||||
const instanceId = require('./instance_id');
 | 
					 | 
				
			||||||
const becca = require('../becca/becca.js');
 | 
					 | 
				
			||||||
const blobService = require('../services/blob.js');
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
let maxEntityChangeId = 0;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
function putEntityChangeWithInstanceId(origEntityChange, instanceId) {
 | 
					 | 
				
			||||||
    const ec = {...origEntityChange, instanceId};
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    putEntityChange(ec);
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
function putEntityChangeWithForcedChange(origEntityChange) {
 | 
					 | 
				
			||||||
    const ec = {...origEntityChange, changeId: null};
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    putEntityChange(ec);
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
function putEntityChange(origEntityChange) {
 | 
					 | 
				
			||||||
    const ec = {...origEntityChange};
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    delete ec.id;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    if (!ec.changeId) {
 | 
					 | 
				
			||||||
        ec.changeId = utils.randomString(12);
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    ec.componentId = ec.componentId || cls.getComponentId() || "NA"; // NA = not available
 | 
					 | 
				
			||||||
    ec.instanceId = ec.instanceId || instanceId;
 | 
					 | 
				
			||||||
    ec.isSynced = ec.isSynced ? 1 : 0;
 | 
					 | 
				
			||||||
    ec.isErased = ec.isErased ? 1 : 0;
 | 
					 | 
				
			||||||
    ec.id = sql.replace("entity_changes", ec);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    maxEntityChangeId = Math.max(maxEntityChangeId, ec.id);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    cls.putEntityChange(ec);
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
function putNoteReorderingEntityChange(parentNoteId, componentId) {
 | 
					 | 
				
			||||||
    putEntityChange({
 | 
					 | 
				
			||||||
        entityName: "note_reordering",
 | 
					 | 
				
			||||||
        entityId: parentNoteId,
 | 
					 | 
				
			||||||
        hash: 'N/A',
 | 
					 | 
				
			||||||
        isErased: false,
 | 
					 | 
				
			||||||
        utcDateChanged: dateUtils.utcNowDateTime(),
 | 
					 | 
				
			||||||
        isSynced: true,
 | 
					 | 
				
			||||||
        componentId,
 | 
					 | 
				
			||||||
        instanceId
 | 
					 | 
				
			||||||
    });
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    const eventService = require('./events');
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    eventService.emit(eventService.ENTITY_CHANGED, {
 | 
					 | 
				
			||||||
        entityName: 'note_reordering',
 | 
					 | 
				
			||||||
        entity: sql.getMap(`SELECT branchId, notePosition FROM branches WHERE isDeleted = 0 AND parentNoteId = ?`, [parentNoteId])
 | 
					 | 
				
			||||||
    });
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
function putEntityChangeForOtherInstances(ec) {
 | 
					 | 
				
			||||||
    putEntityChange({
 | 
					 | 
				
			||||||
        ...ec,
 | 
					 | 
				
			||||||
        changeId: null,
 | 
					 | 
				
			||||||
        instanceId: null
 | 
					 | 
				
			||||||
    });
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
function addEntityChangesForSector(entityName, sector) {
 | 
					 | 
				
			||||||
    const entityChanges = sql.getRows(`SELECT * FROM entity_changes WHERE entityName = ? AND SUBSTR(entityId, 1, 1) = ?`, [entityName, sector]);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    let entitiesInserted = entityChanges.length;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    sql.transactional(() => {
 | 
					 | 
				
			||||||
        if (entityName === 'blobs') {
 | 
					 | 
				
			||||||
            entitiesInserted += addEntityChangesForDependingEntity(sector, 'notes', 'noteId');
 | 
					 | 
				
			||||||
            entitiesInserted += addEntityChangesForDependingEntity(sector, 'attachments', 'attachmentId');
 | 
					 | 
				
			||||||
            entitiesInserted += addEntityChangesForDependingEntity(sector, 'revisions', 'revisionId');
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        for (const ec of entityChanges) {
 | 
					 | 
				
			||||||
            putEntityChangeWithForcedChange(ec);
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
    });
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    log.info(`Added sector ${sector} of '${entityName}' (${entitiesInserted} entities) to the sync queue.`);
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
function addEntityChangesForDependingEntity(sector, tableName, primaryKeyColumn) {
 | 
					 | 
				
			||||||
    // problem in blobs might be caused by problem in entity referencing the blob
 | 
					 | 
				
			||||||
    const dependingEntityChanges = sql.getRows(`
 | 
					 | 
				
			||||||
                SELECT dep_change.* 
 | 
					 | 
				
			||||||
                FROM entity_changes orig_sector
 | 
					 | 
				
			||||||
                JOIN ${tableName} ON ${tableName}.blobId = orig_sector.entityId
 | 
					 | 
				
			||||||
                JOIN entity_changes dep_change ON dep_change.entityName = '${tableName}' AND dep_change.entityId = ${tableName}.${primaryKeyColumn}
 | 
					 | 
				
			||||||
                WHERE orig_sector.entityName = 'blobs' AND SUBSTR(orig_sector.entityId, 1, 1) = ?`, [sector]);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    for (const ec of dependingEntityChanges) {
 | 
					 | 
				
			||||||
        putEntityChangeWithForcedChange(ec);
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    return dependingEntityChanges.length;
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
function cleanupEntityChangesForMissingEntities(entityName, entityPrimaryKey) {
 | 
					 | 
				
			||||||
    sql.execute(`
 | 
					 | 
				
			||||||
      DELETE 
 | 
					 | 
				
			||||||
      FROM entity_changes 
 | 
					 | 
				
			||||||
      WHERE
 | 
					 | 
				
			||||||
        isErased = 0
 | 
					 | 
				
			||||||
        AND entityName = '${entityName}' 
 | 
					 | 
				
			||||||
        AND entityId NOT IN (SELECT ${entityPrimaryKey} FROM ${entityName})`);
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
function fillEntityChanges(entityName, entityPrimaryKey, condition = '') {
 | 
					 | 
				
			||||||
    cleanupEntityChangesForMissingEntities(entityName, entityPrimaryKey);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    sql.transactional(() => {
 | 
					 | 
				
			||||||
        const entityIds = sql.getColumn(`SELECT ${entityPrimaryKey} FROM ${entityName} ${condition}`);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        let createdCount = 0;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        for (const entityId of entityIds) {
 | 
					 | 
				
			||||||
            const existingRows = sql.getValue("SELECT COUNT(1) FROM entity_changes WHERE entityName = ? AND entityId = ?", [entityName, entityId]);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            if (existingRows !== 0) {
 | 
					 | 
				
			||||||
                // we don't want to replace existing entities (which would effectively cause full resync)
 | 
					 | 
				
			||||||
                continue;
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            createdCount++;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            const ec = {
 | 
					 | 
				
			||||||
                entityName,
 | 
					 | 
				
			||||||
                entityId,
 | 
					 | 
				
			||||||
                isErased: false
 | 
					 | 
				
			||||||
            };
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            if (entityName === 'blobs') {
 | 
					 | 
				
			||||||
                const blob = sql.getRow("SELECT blobId, content, utcDateModified FROM blobs WHERE blobId = ?", [entityId]);
 | 
					 | 
				
			||||||
                ec.hash = blobService.calculateContentHash(blob);
 | 
					 | 
				
			||||||
                ec.utcDateChanged = blob.utcDateModified;
 | 
					 | 
				
			||||||
                ec.isSynced = true; // blobs are always synced
 | 
					 | 
				
			||||||
            } else {
 | 
					 | 
				
			||||||
                const entity = becca.getEntity(entityName, entityId);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
                if (entity) {
 | 
					 | 
				
			||||||
                    ec.hash = entity.generateHash();
 | 
					 | 
				
			||||||
                    ec.utcDateChanged = entity.getUtcDateChanged() || dateUtils.utcNowDateTime();
 | 
					 | 
				
			||||||
                    ec.isSynced = entityName !== 'options' || !!entity.isSynced;
 | 
					 | 
				
			||||||
                } else {
 | 
					 | 
				
			||||||
                    // entity might be null (not present in becca) when it's deleted
 | 
					 | 
				
			||||||
                    // this will produce different hash value than when entity is being deleted since then
 | 
					 | 
				
			||||||
                    // all normal hashed attributes are being used. Sync should recover from that, though.
 | 
					 | 
				
			||||||
                    ec.hash = "deleted";
 | 
					 | 
				
			||||||
                    ec.utcDateChanged = dateUtils.utcNowDateTime();
 | 
					 | 
				
			||||||
                    ec.isSynced = true; // deletable (the ones with isDeleted) entities are synced
 | 
					 | 
				
			||||||
                }
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            putEntityChange(ec);
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        if (createdCount > 0) {
 | 
					 | 
				
			||||||
            log.info(`Created ${createdCount} missing entity changes for entity '${entityName}'.`);
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
    });
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
function fillAllEntityChanges() {
 | 
					 | 
				
			||||||
    sql.transactional(() => {
 | 
					 | 
				
			||||||
        sql.execute("DELETE FROM entity_changes WHERE isErased = 0");
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        fillEntityChanges("notes", "noteId");
 | 
					 | 
				
			||||||
        fillEntityChanges("branches", "branchId");
 | 
					 | 
				
			||||||
        fillEntityChanges("revisions", "revisionId");
 | 
					 | 
				
			||||||
        fillEntityChanges("attachments", "attachmentId");
 | 
					 | 
				
			||||||
        fillEntityChanges("blobs", "blobId");
 | 
					 | 
				
			||||||
        fillEntityChanges("attributes", "attributeId");
 | 
					 | 
				
			||||||
        fillEntityChanges("etapi_tokens", "etapiTokenId");
 | 
					 | 
				
			||||||
        fillEntityChanges("options", "name", 'WHERE isSynced = 1');
 | 
					 | 
				
			||||||
    });
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
function recalculateMaxEntityChangeId() {
 | 
					 | 
				
			||||||
    maxEntityChangeId = sql.getValue("SELECT COALESCE(MAX(id), 0) FROM entity_changes");
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
module.exports = {
 | 
					 | 
				
			||||||
    putNoteReorderingEntityChange,
 | 
					 | 
				
			||||||
    putEntityChangeForOtherInstances,
 | 
					 | 
				
			||||||
    putEntityChangeWithForcedChange,
 | 
					 | 
				
			||||||
    putEntityChange,
 | 
					 | 
				
			||||||
    putEntityChangeWithInstanceId,
 | 
					 | 
				
			||||||
    fillAllEntityChanges,
 | 
					 | 
				
			||||||
    addEntityChangesForSector,
 | 
					 | 
				
			||||||
    getMaxEntityChangeId: () => maxEntityChangeId,
 | 
					 | 
				
			||||||
    recalculateMaxEntityChangeId
 | 
					 | 
				
			||||||
};
 | 
					 | 
				
			||||||
@ -1,12 +1,206 @@
 | 
				
			|||||||
export interface EntityChange {
 | 
					import sql = require('./sql');
 | 
				
			||||||
	id?: string;
 | 
					import dateUtils = require('./date_utils');
 | 
				
			||||||
	entityName: string;
 | 
					import log = require('./log');
 | 
				
			||||||
	entityId: string;
 | 
					import cls = require('./cls');
 | 
				
			||||||
	hash: string;
 | 
					import utils = require('./utils');
 | 
				
			||||||
	utcDateChanged: string;
 | 
					import instanceId = require('./instance_id');
 | 
				
			||||||
	isSynced: boolean | 1 | 0;
 | 
					import becca = require('../becca/becca.js');
 | 
				
			||||||
	isErased: boolean | 1 | 0;
 | 
					import blobService = require('../services/blob.js');
 | 
				
			||||||
	componentId?: string | null;
 | 
					import { EntityChange } from './entity_changes_interface';
 | 
				
			||||||
	changeId?: string | null;
 | 
					import type { Blob } from "./blob-interface";
 | 
				
			||||||
	instanceId?: string | null;
 | 
					
 | 
				
			||||||
 | 
					let maxEntityChangeId = 0;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					function putEntityChangeWithInstanceId(origEntityChange: EntityChange, instanceId: string) {
 | 
				
			||||||
 | 
					    const ec = {...origEntityChange, instanceId};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    putEntityChange(ec);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					function putEntityChangeWithForcedChange(origEntityChange: EntityChange) {
 | 
				
			||||||
 | 
					    const ec = {...origEntityChange, changeId: null};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    putEntityChange(ec);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					function putEntityChange(origEntityChange: EntityChange) {
 | 
				
			||||||
 | 
					    const ec = {...origEntityChange};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    delete ec.id;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if (!ec.changeId) {
 | 
				
			||||||
 | 
					        ec.changeId = utils.randomString(12);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    ec.componentId = ec.componentId || cls.getComponentId() || "NA"; // NA = not available
 | 
				
			||||||
 | 
					    ec.instanceId = ec.instanceId || instanceId;
 | 
				
			||||||
 | 
					    ec.isSynced = ec.isSynced ? 1 : 0;
 | 
				
			||||||
 | 
					    ec.isErased = ec.isErased ? 1 : 0;
 | 
				
			||||||
 | 
					    ec.id = sql.replace("entity_changes", ec);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (ec.id) {
 | 
				
			||||||
 | 
							maxEntityChangeId = Math.max(maxEntityChangeId, ec.id);
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    cls.putEntityChange(ec);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					function putNoteReorderingEntityChange(parentNoteId: string, componentId: string) {
 | 
				
			||||||
 | 
					    putEntityChange({
 | 
				
			||||||
 | 
					        entityName: "note_reordering",
 | 
				
			||||||
 | 
					        entityId: parentNoteId,
 | 
				
			||||||
 | 
					        hash: 'N/A',
 | 
				
			||||||
 | 
					        isErased: false,
 | 
				
			||||||
 | 
					        utcDateChanged: dateUtils.utcNowDateTime(),
 | 
				
			||||||
 | 
					        isSynced: true,
 | 
				
			||||||
 | 
					        componentId,
 | 
				
			||||||
 | 
					        instanceId
 | 
				
			||||||
 | 
					    });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    const eventService = require('./events');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    eventService.emit(eventService.ENTITY_CHANGED, {
 | 
				
			||||||
 | 
					        entityName: 'note_reordering',
 | 
				
			||||||
 | 
					        entity: sql.getMap(`SELECT branchId, notePosition FROM branches WHERE isDeleted = 0 AND parentNoteId = ?`, [parentNoteId])
 | 
				
			||||||
 | 
					    });
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					function putEntityChangeForOtherInstances(ec: EntityChange) {
 | 
				
			||||||
 | 
					    putEntityChange({
 | 
				
			||||||
 | 
					        ...ec,
 | 
				
			||||||
 | 
					        changeId: null,
 | 
				
			||||||
 | 
					        instanceId: null
 | 
				
			||||||
 | 
					    });
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					function addEntityChangesForSector(entityName: string, sector: string) {
 | 
				
			||||||
 | 
					    const entityChanges = sql.getRows<EntityChange>(`SELECT * FROM entity_changes WHERE entityName = ? AND SUBSTR(entityId, 1, 1) = ?`, [entityName, sector]);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    let entitiesInserted = entityChanges.length;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    sql.transactional(() => {
 | 
				
			||||||
 | 
					        if (entityName === 'blobs') {
 | 
				
			||||||
 | 
					            entitiesInserted += addEntityChangesForDependingEntity(sector, 'notes', 'noteId');
 | 
				
			||||||
 | 
					            entitiesInserted += addEntityChangesForDependingEntity(sector, 'attachments', 'attachmentId');
 | 
				
			||||||
 | 
					            entitiesInserted += addEntityChangesForDependingEntity(sector, 'revisions', 'revisionId');
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        for (const ec of entityChanges) {
 | 
				
			||||||
 | 
					            putEntityChangeWithForcedChange(ec);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    log.info(`Added sector ${sector} of '${entityName}' (${entitiesInserted} entities) to the sync queue.`);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					function addEntityChangesForDependingEntity(sector: string, tableName: string, primaryKeyColumn: string) {
 | 
				
			||||||
 | 
					    // problem in blobs might be caused by problem in entity referencing the blob
 | 
				
			||||||
 | 
					    const dependingEntityChanges = sql.getRows<EntityChange>(`
 | 
				
			||||||
 | 
					                SELECT dep_change.* 
 | 
				
			||||||
 | 
					                FROM entity_changes orig_sector
 | 
				
			||||||
 | 
					                JOIN ${tableName} ON ${tableName}.blobId = orig_sector.entityId
 | 
				
			||||||
 | 
					                JOIN entity_changes dep_change ON dep_change.entityName = '${tableName}' AND dep_change.entityId = ${tableName}.${primaryKeyColumn}
 | 
				
			||||||
 | 
					                WHERE orig_sector.entityName = 'blobs' AND SUBSTR(orig_sector.entityId, 1, 1) = ?`, [sector]);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    for (const ec of dependingEntityChanges) {
 | 
				
			||||||
 | 
					        putEntityChangeWithForcedChange(ec);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    return dependingEntityChanges.length;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					function cleanupEntityChangesForMissingEntities(entityName: string, entityPrimaryKey: string) {
 | 
				
			||||||
 | 
					    sql.execute(`
 | 
				
			||||||
 | 
					      DELETE 
 | 
				
			||||||
 | 
					      FROM entity_changes 
 | 
				
			||||||
 | 
					      WHERE
 | 
				
			||||||
 | 
					        isErased = 0
 | 
				
			||||||
 | 
					        AND entityName = '${entityName}' 
 | 
				
			||||||
 | 
					        AND entityId NOT IN (SELECT ${entityPrimaryKey} FROM ${entityName})`);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					function fillEntityChanges(entityName: string, entityPrimaryKey: string, condition = '') {
 | 
				
			||||||
 | 
					    cleanupEntityChangesForMissingEntities(entityName, entityPrimaryKey);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    sql.transactional(() => {
 | 
				
			||||||
 | 
					        const entityIds = sql.getColumn<string>(`SELECT ${entityPrimaryKey} FROM ${entityName} ${condition}`);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        let createdCount = 0;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        for (const entityId of entityIds) {
 | 
				
			||||||
 | 
					            const existingRows = sql.getValue("SELECT COUNT(1) FROM entity_changes WHERE entityName = ? AND entityId = ?", [entityName, entityId]);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            if (existingRows !== 0) {
 | 
				
			||||||
 | 
					                // we don't want to replace existing entities (which would effectively cause full resync)
 | 
				
			||||||
 | 
					                continue;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            createdCount++;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            const ec: Partial<EntityChange> = {
 | 
				
			||||||
 | 
					                entityName,
 | 
				
			||||||
 | 
					                entityId,
 | 
				
			||||||
 | 
					                isErased: false
 | 
				
			||||||
 | 
					            };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            if (entityName === 'blobs') {
 | 
				
			||||||
 | 
					                const blob = sql.getRow<Blob>("SELECT blobId, content, utcDateModified FROM blobs WHERE blobId = ?", [entityId]);
 | 
				
			||||||
 | 
					                ec.hash = blobService.calculateContentHash(blob);
 | 
				
			||||||
 | 
					                ec.utcDateChanged = blob.utcDateModified;
 | 
				
			||||||
 | 
					                ec.isSynced = true; // blobs are always synced
 | 
				
			||||||
 | 
					            } else {
 | 
				
			||||||
 | 
					                const entity = becca.getEntity(entityName, entityId);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                if (entity) {
 | 
				
			||||||
 | 
					                    ec.hash = entity.generateHash();
 | 
				
			||||||
 | 
					                    ec.utcDateChanged = entity.getUtcDateChanged() || dateUtils.utcNowDateTime();
 | 
				
			||||||
 | 
					                    ec.isSynced = entityName !== 'options' || !!entity.isSynced;
 | 
				
			||||||
 | 
					                } else {
 | 
				
			||||||
 | 
					                    // entity might be null (not present in becca) when it's deleted
 | 
				
			||||||
 | 
					                    // this will produce different hash value than when entity is being deleted since then
 | 
				
			||||||
 | 
					                    // all normal hashed attributes are being used. Sync should recover from that, though.
 | 
				
			||||||
 | 
					                    ec.hash = "deleted";
 | 
				
			||||||
 | 
					                    ec.utcDateChanged = dateUtils.utcNowDateTime();
 | 
				
			||||||
 | 
					                    ec.isSynced = true; // deletable (the ones with isDeleted) entities are synced
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            putEntityChange(ec as EntityChange);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if (createdCount > 0) {
 | 
				
			||||||
 | 
					            log.info(`Created ${createdCount} missing entity changes for entity '${entityName}'.`);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    });
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					function fillAllEntityChanges() {
 | 
				
			||||||
 | 
					    sql.transactional(() => {
 | 
				
			||||||
 | 
					        sql.execute("DELETE FROM entity_changes WHERE isErased = 0");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        fillEntityChanges("notes", "noteId");
 | 
				
			||||||
 | 
					        fillEntityChanges("branches", "branchId");
 | 
				
			||||||
 | 
					        fillEntityChanges("revisions", "revisionId");
 | 
				
			||||||
 | 
					        fillEntityChanges("attachments", "attachmentId");
 | 
				
			||||||
 | 
					        fillEntityChanges("blobs", "blobId");
 | 
				
			||||||
 | 
					        fillEntityChanges("attributes", "attributeId");
 | 
				
			||||||
 | 
					        fillEntityChanges("etapi_tokens", "etapiTokenId");
 | 
				
			||||||
 | 
					        fillEntityChanges("options", "name", 'WHERE isSynced = 1');
 | 
				
			||||||
 | 
					    });
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					function recalculateMaxEntityChangeId() {
 | 
				
			||||||
 | 
					    maxEntityChangeId = sql.getValue<number>("SELECT COALESCE(MAX(id), 0) FROM entity_changes");
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					export = {
 | 
				
			||||||
 | 
					    putNoteReorderingEntityChange,
 | 
				
			||||||
 | 
					    putEntityChangeForOtherInstances,
 | 
				
			||||||
 | 
					    putEntityChangeWithForcedChange,
 | 
				
			||||||
 | 
					    putEntityChange,
 | 
				
			||||||
 | 
					    putEntityChangeWithInstanceId,
 | 
				
			||||||
 | 
					    fillAllEntityChanges,
 | 
				
			||||||
 | 
					    addEntityChangesForSector,
 | 
				
			||||||
 | 
					    getMaxEntityChangeId: () => maxEntityChangeId,
 | 
				
			||||||
 | 
					    recalculateMaxEntityChangeId
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										12
									
								
								src/services/entity_changes_interface.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										12
									
								
								src/services/entity_changes_interface.ts
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,12 @@
 | 
				
			|||||||
 | 
					export interface EntityChange {
 | 
				
			||||||
 | 
						id?: number | null;
 | 
				
			||||||
 | 
						entityName: string;
 | 
				
			||||||
 | 
						entityId: string;
 | 
				
			||||||
 | 
						hash: string;
 | 
				
			||||||
 | 
						utcDateChanged: string;
 | 
				
			||||||
 | 
						isSynced: boolean | 1 | 0;
 | 
				
			||||||
 | 
						isErased: boolean | 1 | 0;
 | 
				
			||||||
 | 
						componentId?: string | null;
 | 
				
			||||||
 | 
						changeId?: string | null;
 | 
				
			||||||
 | 
						instanceId?: string | null;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@ -2,4 +2,4 @@ import utils = require('./utils');
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
const instanceId = utils.randomString(12);
 | 
					const instanceId = utils.randomString(12);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
module.exports = instanceId;
 | 
					export = instanceId;
 | 
				
			||||||
 | 
				
			|||||||
@ -30,16 +30,16 @@ function encrypt(plainText: string | Buffer) {
 | 
				
			|||||||
    return dataEncryptionService.encrypt(dataKey, plainText);
 | 
					    return dataEncryptionService.encrypt(dataKey, plainText);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function decrypt(cipherText: string | Buffer) {
 | 
					function decrypt(cipherText: string | Buffer): Buffer | null {
 | 
				
			||||||
    const dataKey = getDataKey();
 | 
					    const dataKey = getDataKey();
 | 
				
			||||||
    if (cipherText === null || dataKey === null) {
 | 
					    if (cipherText === null || dataKey === null) {
 | 
				
			||||||
        return null;
 | 
					        return null;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    return dataEncryptionService.decrypt(dataKey, cipherText);
 | 
					    return dataEncryptionService.decrypt(dataKey, cipherText) || null;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function decryptString(cipherText: string) {
 | 
					function decryptString(cipherText: string): string | null {
 | 
				
			||||||
    const dataKey = getDataKey();
 | 
					    const dataKey = getDataKey();
 | 
				
			||||||
    if (dataKey === null) {
 | 
					    if (dataKey === null) {
 | 
				
			||||||
        return null;
 | 
					        return null;
 | 
				
			||||||
@ -70,7 +70,7 @@ function checkProtectedSessionExpiration() {
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
module.exports = {
 | 
					export = {
 | 
				
			||||||
    setDataKey,
 | 
					    setDataKey,
 | 
				
			||||||
    resetDataKey,
 | 
					    resetDataKey,
 | 
				
			||||||
    isProtectedSessionAvailable,
 | 
					    isProtectedSessionAvailable,
 | 
				
			||||||
 | 
				
			|||||||
@ -103,8 +103,8 @@ function getRowOrNull<T>(query: string, params: Params = []): T | null {
 | 
				
			|||||||
    return (all.length > 0 ? all[0] : null) as (T | null);
 | 
					    return (all.length > 0 ? all[0] : null) as (T | null);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function getValue(query: string, params: Params = []) {
 | 
					function getValue<T>(query: string, params: Params = []): T {
 | 
				
			||||||
    return wrap(query, s => s.pluck().get(params));
 | 
					    return wrap(query, s => s.pluck().get(params)) as T;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// smaller values can result in better performance due to better usage of statement cache
 | 
					// smaller values can result in better performance due to better usage of statement cache
 | 
				
			||||||
@ -139,8 +139,8 @@ function getManyRows(query: string, params: Params) {
 | 
				
			|||||||
    return results;
 | 
					    return results;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function getRows(query: string, params: Params = []): unknown[] {
 | 
					function getRows<T>(query: string, params: Params = []): T[] {
 | 
				
			||||||
    return wrap(query, s => s.all(params)) as unknown[];
 | 
					    return wrap(query, s => s.all(params)) as T[];
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function getRawRows<T extends {} | unknown[]>(query: string, params: Params = []): T[] | null {
 | 
					function getRawRows<T extends {} | unknown[]>(query: string, params: Params = []): T[] | null {
 | 
				
			||||||
@ -166,8 +166,8 @@ function getMap<K extends string | number | symbol, V>(query: string, params: Pa
 | 
				
			|||||||
    return map;
 | 
					    return map;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function getColumn(query: string, params: Params = []) {
 | 
					function getColumn<T>(query: string, params: Params = []): T[] {
 | 
				
			||||||
    return wrap(query, s => s.pluck().all(params));
 | 
					    return wrap(query, s => s.pluck().all(params)) as T[];
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function execute(query: string, params: Params = []): RunResult {
 | 
					function execute(query: string, params: Params = []): RunResult {
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user