2017-11-16 21:50:00 -05:00
|
|
|
const sql = require('./sql');
|
2020-12-16 22:17:42 +01:00
|
|
|
const dateUtils = require('./date_utils');
|
2017-12-23 09:35:00 -05:00
|
|
|
const log = require('./log');
|
2018-03-30 19:41:54 -04:00
|
|
|
const cls = require('./cls');
|
2021-11-18 22:33:08 +01:00
|
|
|
const utils = require('./utils');
|
2023-07-29 21:59:20 +02:00
|
|
|
const instanceId = require('./instance_id');
|
2021-06-29 22:15:57 +02:00
|
|
|
const becca = require("../becca/becca");
|
2023-07-27 23:22:08 +02:00
|
|
|
const blobService = require("../services/blob");
|
2018-04-18 23:11:30 -04:00
|
|
|
|
2020-08-02 23:27:48 +02:00
|
|
|
let maxEntityChangeId = 0;
|
2020-03-09 22:32:26 +01:00
|
|
|
|
2023-07-29 23:25:02 +02:00
|
|
|
function putEntityChangeWithInstanceId(origEntityChange, instanceId) {
|
2022-01-09 21:25:15 +01:00
|
|
|
const ec = {...origEntityChange, instanceId};
|
2022-01-09 20:16:39 +01:00
|
|
|
|
2023-07-29 23:25:02 +02:00
|
|
|
return putEntityChange(ec);
|
2022-01-09 20:16:39 +01:00
|
|
|
}
|
|
|
|
|
2023-07-29 23:25:02 +02:00
|
|
|
function putEntityChange(origEntityChange) {
|
2021-09-07 22:37:03 +02:00
|
|
|
const ec = {...origEntityChange};
|
2019-12-02 22:27:06 +01:00
|
|
|
|
2021-11-17 21:47:41 +01:00
|
|
|
delete ec.id;
|
2021-09-07 22:56:54 +02:00
|
|
|
|
2021-11-18 22:33:08 +01:00
|
|
|
if (!ec.changeId) {
|
|
|
|
ec.changeId = utils.randomString(12);
|
|
|
|
}
|
|
|
|
|
2022-01-12 19:32:23 +01:00
|
|
|
ec.componentId = ec.componentId || cls.getComponentId() || "NA"; // NA = not available
|
2022-01-09 21:25:15 +01:00
|
|
|
ec.instanceId = ec.instanceId || instanceId;
|
2021-09-07 22:37:03 +02:00
|
|
|
ec.isSynced = ec.isSynced ? 1 : 0;
|
|
|
|
ec.isErased = ec.isErased ? 1 : 0;
|
|
|
|
ec.id = sql.replace("entity_changes", ec);
|
2020-03-09 22:32:26 +01:00
|
|
|
|
2021-09-07 22:37:03 +02:00
|
|
|
maxEntityChangeId = Math.max(maxEntityChangeId, ec.id);
|
|
|
|
|
2023-07-29 23:25:02 +02:00
|
|
|
cls.putEntityChange(ec);
|
2019-12-02 22:27:06 +01:00
|
|
|
}
|
|
|
|
|
2023-07-29 23:25:02 +02:00
|
|
|
function putNoteReorderingEntityChange(parentNoteId, componentId) {
|
|
|
|
putEntityChange({
|
2021-03-06 23:53:10 +01:00
|
|
|
entityName: "note_reordering",
|
|
|
|
entityId: parentNoteId,
|
|
|
|
hash: 'N/A',
|
|
|
|
isErased: false,
|
2021-06-29 23:45:45 +02:00
|
|
|
utcDateChanged: dateUtils.utcNowDateTime(),
|
2021-06-30 20:54:15 +02:00
|
|
|
isSynced: true,
|
2022-01-09 20:16:39 +01:00
|
|
|
componentId,
|
2022-01-12 19:32:23 +01:00
|
|
|
instanceId
|
2021-06-30 20:54:15 +02:00
|
|
|
});
|
2021-03-06 23:53:10 +01:00
|
|
|
|
|
|
|
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])
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-07-29 23:25:02 +02:00
|
|
|
function putEntityChangeForOtherInstances(ec) {
|
|
|
|
putEntityChangeWithInstanceId(ec, null);
|
2020-12-07 23:04:17 +01:00
|
|
|
}
|
|
|
|
|
2021-04-04 22:02:40 +02:00
|
|
|
function addEntityChangesForSector(entityName, sector) {
|
|
|
|
const entityChanges = sql.getRows(`SELECT * FROM entity_changes WHERE entityName = ? AND SUBSTR(entityId, 1, 1) = ?`, [entityName, sector]);
|
2020-04-04 21:49:57 +02:00
|
|
|
|
2021-04-04 22:02:40 +02:00
|
|
|
sql.transactional(() => {
|
|
|
|
for (const ec of entityChanges) {
|
2023-07-29 23:25:02 +02:00
|
|
|
putEntityChange(ec);
|
2020-03-09 21:28:41 +01:00
|
|
|
}
|
2020-04-04 21:49:57 +02:00
|
|
|
});
|
2020-03-09 21:28:41 +01:00
|
|
|
|
2023-07-29 21:59:20 +02:00
|
|
|
log.info(`Added sector ${sector} of '${entityName}' (${entityChanges.length} entities) to the sync queue.`);
|
2019-12-18 22:58:30 +01:00
|
|
|
}
|
|
|
|
|
2020-12-14 14:17:51 +01:00
|
|
|
function cleanupEntityChangesForMissingEntities(entityName, entityPrimaryKey) {
|
2020-06-20 12:31:38 +02:00
|
|
|
sql.execute(`
|
2017-12-23 09:35:00 -05:00
|
|
|
DELETE
|
2020-08-02 23:27:48 +02:00
|
|
|
FROM entity_changes
|
2020-12-14 13:15:32 +01:00
|
|
|
WHERE
|
|
|
|
isErased = 0
|
2020-12-16 22:17:42 +01:00
|
|
|
AND entityName = '${entityName}'
|
|
|
|
AND entityId NOT IN (SELECT ${entityPrimaryKey} FROM ${entityName})`);
|
2017-12-23 09:35:00 -05:00
|
|
|
}
|
|
|
|
|
2020-08-02 23:27:48 +02:00
|
|
|
function fillEntityChanges(entityName, entityPrimaryKey, condition = '') {
|
2022-12-27 09:53:25 +01:00
|
|
|
cleanupEntityChangesForMissingEntities(entityName, entityPrimaryKey);
|
2019-06-05 22:07:12 +02:00
|
|
|
|
2022-12-27 09:53:25 +01:00
|
|
|
sql.transactional(() => {
|
2023-07-27 23:22:08 +02:00
|
|
|
const entityIds = sql.getColumn(`SELECT ${entityPrimaryKey} FROM ${entityName} ${condition}`);
|
2019-06-05 22:07:12 +02:00
|
|
|
|
2022-12-27 09:53:25 +01:00
|
|
|
let createdCount = 0;
|
2019-10-02 23:22:58 +02:00
|
|
|
|
2022-12-27 09:53:25 +01:00
|
|
|
for (const entityId of entityIds) {
|
|
|
|
const existingRows = sql.getValue("SELECT COUNT(1) FROM entity_changes WHERE entityName = ? AND entityId = ?", [entityName, entityId]);
|
2019-06-05 22:07:12 +02:00
|
|
|
|
2023-07-27 23:22:08 +02:00
|
|
|
if (existingRows !== 0) {
|
|
|
|
// we don't want to replace existing entities (which would effectively cause full resync)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
createdCount++;
|
2019-06-05 22:07:12 +02:00
|
|
|
|
2023-07-29 21:59:20 +02:00
|
|
|
const ec = {
|
|
|
|
entityName,
|
|
|
|
entityId,
|
|
|
|
isErased: false
|
|
|
|
};
|
2023-02-10 09:16:32 +01:00
|
|
|
|
2023-07-27 23:22:08 +02:00
|
|
|
if (entityName === 'blobs') {
|
|
|
|
const blob = sql.getRow("SELECT blobId, content, utcDateModified FROM blobs WHERE blobId = ?", [entityId]);
|
2023-07-29 21:59:20 +02:00
|
|
|
ec.hash = blobService.calculateContentHash(blob);
|
|
|
|
ec.utcDateChanged = blob.utcDateModified;
|
|
|
|
ec.isSynced = true; // blobs are always synced
|
2023-07-27 23:22:08 +02:00
|
|
|
} else {
|
|
|
|
const entity = becca.getEntity(entityName, entityId);
|
|
|
|
|
|
|
|
if (entity) {
|
2023-07-29 21:59:20 +02:00
|
|
|
ec.hash = entity.generateHash() || "|deleted";
|
|
|
|
ec.utcDateChanged = entity.getUtcDateChanged() || dateUtils.utcNowDateTime();
|
|
|
|
ec.isSynced = entityName !== 'options' || !!entity.isSynced;
|
2023-07-27 23:22:08 +02:00
|
|
|
} else {
|
|
|
|
// entity might be null (not present in becca) when it's deleted
|
2023-02-10 09:16:32 +01:00
|
|
|
// FIXME: hacky, not sure if it might cause some problems
|
2023-07-29 21:59:20 +02:00
|
|
|
ec.hash = "deleted";
|
|
|
|
ec.utcDateChanged = dateUtils.utcNowDateTime();
|
|
|
|
ec.isSynced = true; // deletable (the ones with isDeleted) entities are synced
|
2023-02-10 09:16:32 +01:00
|
|
|
}
|
2019-06-05 22:07:12 +02:00
|
|
|
}
|
2023-07-27 23:22:08 +02:00
|
|
|
|
2023-07-29 23:25:02 +02:00
|
|
|
putEntityChange(ec);
|
2022-12-27 09:53:25 +01:00
|
|
|
}
|
2019-10-02 23:22:58 +02:00
|
|
|
|
2022-12-27 09:53:25 +01:00
|
|
|
if (createdCount > 0) {
|
2023-05-04 22:16:18 +02:00
|
|
|
log.info(`Created ${createdCount} missing entity changes for entity '${entityName}'.`);
|
2022-12-27 09:53:25 +01:00
|
|
|
}
|
|
|
|
});
|
2017-12-23 09:35:00 -05:00
|
|
|
}
|
|
|
|
|
2020-08-02 23:27:48 +02:00
|
|
|
function fillAllEntityChanges() {
|
2020-07-02 21:08:18 +02:00
|
|
|
sql.transactional(() => {
|
2020-12-14 13:15:32 +01:00
|
|
|
sql.execute("DELETE FROM entity_changes WHERE isErased = 0");
|
2020-08-02 23:27:48 +02:00
|
|
|
|
|
|
|
fillEntityChanges("notes", "noteId");
|
|
|
|
fillEntityChanges("branches", "branchId");
|
2023-06-04 23:01:40 +02:00
|
|
|
fillEntityChanges("revisions", "revisionId");
|
2023-03-16 12:17:55 +01:00
|
|
|
fillEntityChanges("attachments", "attachmentId");
|
2023-03-16 11:02:07 +01:00
|
|
|
fillEntityChanges("blobs", "blobId");
|
2020-08-02 23:27:48 +02:00
|
|
|
fillEntityChanges("attributes", "attributeId");
|
2022-01-10 17:09:20 +01:00
|
|
|
fillEntityChanges("etapi_tokens", "etapiTokenId");
|
2023-07-27 23:22:08 +02:00
|
|
|
fillEntityChanges("options", "name", 'WHERE isSynced = 1');
|
2020-07-02 21:08:18 +02:00
|
|
|
});
|
2017-12-23 13:55:13 -05:00
|
|
|
}
|
|
|
|
|
2023-01-24 09:35:00 +01:00
|
|
|
function recalculateMaxEntityChangeId() {
|
|
|
|
maxEntityChangeId = sql.getValue("SELECT COALESCE(MAX(id), 0) FROM entity_changes");
|
|
|
|
}
|
|
|
|
|
2017-11-16 21:50:00 -05:00
|
|
|
module.exports = {
|
2023-07-29 23:25:02 +02:00
|
|
|
putNoteReorderingEntityChange,
|
|
|
|
putEntityChangeForOtherInstances,
|
|
|
|
putEntityChange,
|
|
|
|
putEntityChangeWithInstanceId,
|
2020-08-02 23:27:48 +02:00
|
|
|
fillAllEntityChanges,
|
|
|
|
addEntityChangesForSector,
|
2023-01-24 09:35:00 +01:00
|
|
|
getMaxEntityChangeId: () => maxEntityChangeId,
|
|
|
|
recalculateMaxEntityChangeId
|
2020-06-20 12:31:38 +02:00
|
|
|
};
|