2021-01-22 19:41:11 +02:00
|
|
|
import BaseModel, { ModelType } from '../BaseModel';
|
|
|
|
import shim from '../shim';
|
2023-12-13 21:24:58 +02:00
|
|
|
import eventManager, { EventName } from '../eventManager';
|
2021-08-30 19:53:24 +02:00
|
|
|
import { ItemChangeEntity } from '../services/database/types';
|
2018-03-13 01:40:43 +02:00
|
|
|
const Mutex = require('async-mutex').Mutex;
|
|
|
|
|
2021-08-30 19:53:24 +02:00
|
|
|
export interface ChangeSinceIdOptions {
|
|
|
|
limit?: number;
|
|
|
|
fields?: string[];
|
|
|
|
}
|
|
|
|
|
2021-01-22 19:41:11 +02:00
|
|
|
export default class ItemChange extends BaseModel {
|
|
|
|
|
|
|
|
private static addChangeMutex_: any = new Mutex();
|
|
|
|
private static saveCalls_: any[] = [];
|
|
|
|
|
|
|
|
public static TYPE_CREATE = 1;
|
|
|
|
public static TYPE_UPDATE = 2;
|
|
|
|
public static TYPE_DELETE = 3;
|
|
|
|
|
|
|
|
public static SOURCE_UNSPECIFIED = 1;
|
|
|
|
public static SOURCE_SYNC = 2;
|
|
|
|
public static SOURCE_DECRYPTION = 2; // CAREFUL - SAME ID AS SOURCE_SYNC!
|
2023-07-23 16:57:55 +02:00
|
|
|
public static SOURCE_SHARE_SERVICE = 4;
|
2021-01-22 19:41:11 +02:00
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public static tableName() {
|
2018-03-13 01:40:43 +02:00
|
|
|
return 'item_changes';
|
|
|
|
}
|
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public static modelType() {
|
2018-03-13 01:40:43 +02:00
|
|
|
return BaseModel.TYPE_ITEM_CHANGE;
|
|
|
|
}
|
|
|
|
|
2021-08-30 19:53:24 +02:00
|
|
|
public static async add(itemType: ModelType, itemId: string, type: number, changeSource: any = null, beforeChangeItemJson: string = null) {
|
2019-05-06 22:35:29 +02:00
|
|
|
if (changeSource === null) changeSource = ItemChange.SOURCE_UNSPECIFIED;
|
|
|
|
if (!beforeChangeItemJson) beforeChangeItemJson = '';
|
|
|
|
|
2018-03-15 20:08:46 +02:00
|
|
|
ItemChange.saveCalls_.push(true);
|
|
|
|
|
|
|
|
// Using a mutex so that records can be added to the database in the
|
|
|
|
// background, without making the UI wait.
|
2018-03-13 01:40:43 +02:00
|
|
|
const release = await ItemChange.addChangeMutex_.acquire();
|
|
|
|
|
|
|
|
try {
|
2020-12-01 16:08:41 +02:00
|
|
|
await this.db().transactionExecBatch([
|
|
|
|
{
|
|
|
|
sql: 'DELETE FROM item_changes WHERE item_id = ?',
|
|
|
|
params: [itemId],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
sql: 'INSERT INTO item_changes (item_type, item_id, type, source, created_time, before_change_item) VALUES (?, ?, ?, ?, ?, ?)',
|
|
|
|
params: [itemType, itemId, type, changeSource, Date.now(), beforeChangeItemJson],
|
|
|
|
},
|
|
|
|
]);
|
2018-03-13 01:40:43 +02:00
|
|
|
} finally {
|
|
|
|
release();
|
2018-03-15 20:08:46 +02:00
|
|
|
ItemChange.saveCalls_.pop();
|
2020-12-01 16:08:41 +02:00
|
|
|
|
2023-12-13 21:24:58 +02:00
|
|
|
eventManager.emit(EventName.ItemChange, {
|
2020-12-01 16:08:41 +02:00
|
|
|
itemType: itemType,
|
|
|
|
itemId: itemId,
|
|
|
|
eventType: type,
|
|
|
|
});
|
2018-03-13 01:40:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-30 19:53:24 +02:00
|
|
|
public static async lastChangeId() {
|
2019-01-13 18:05:07 +02:00
|
|
|
const row = await this.db().selectOne('SELECT max(id) as max_id FROM item_changes');
|
|
|
|
return row && row.max_id ? row.max_id : 0;
|
|
|
|
}
|
|
|
|
|
2018-03-15 20:08:46 +02:00
|
|
|
// Because item changes are recorded in the background, this function
|
|
|
|
// can be used for synchronous code, in particular when unit testing.
|
2021-08-30 19:53:24 +02:00
|
|
|
public static async waitForAllSaved() {
|
2019-09-13 00:16:42 +02:00
|
|
|
return new Promise((resolve) => {
|
2020-10-09 19:35:46 +02:00
|
|
|
const iid = shim.setInterval(() => {
|
2018-03-15 20:08:46 +02:00
|
|
|
if (!ItemChange.saveCalls_.length) {
|
2020-10-09 19:35:46 +02:00
|
|
|
shim.clearInterval(iid);
|
2021-01-22 19:41:11 +02:00
|
|
|
resolve(null);
|
2018-03-15 20:08:46 +02:00
|
|
|
}
|
|
|
|
}, 100);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-08-30 19:53:24 +02:00
|
|
|
public static async deleteOldChanges(lowestChangeId: number, itemMinTtl: number) {
|
2019-01-14 21:11:54 +02:00
|
|
|
if (!lowestChangeId) return;
|
2021-08-30 19:53:24 +02:00
|
|
|
|
|
|
|
const cutOffDate = Date.now() - itemMinTtl;
|
|
|
|
|
|
|
|
return this.db().exec(`
|
|
|
|
DELETE FROM item_changes
|
|
|
|
WHERE id <= ?
|
|
|
|
AND created_time <= ?
|
|
|
|
`, [lowestChangeId, cutOffDate]);
|
2019-01-14 21:11:54 +02:00
|
|
|
}
|
2021-08-30 19:53:24 +02:00
|
|
|
|
|
|
|
public static async changesSinceId(changeId: number, options: ChangeSinceIdOptions = null): Promise<ItemChangeEntity[]> {
|
|
|
|
options = {
|
|
|
|
limit: 100,
|
|
|
|
fields: ['id', 'item_type', 'item_id', 'type', 'created_time'],
|
|
|
|
...options,
|
|
|
|
};
|
|
|
|
|
|
|
|
return this.db().selectAll(`
|
|
|
|
SELECT ${this.db().escapeFieldsToString(options.fields)}
|
|
|
|
FROM item_changes
|
|
|
|
WHERE id > ?
|
|
|
|
ORDER BY id
|
|
|
|
LIMIT ?
|
|
|
|
`, [changeId, options.limit]);
|
|
|
|
}
|
|
|
|
|
2018-03-13 01:40:43 +02:00
|
|
|
}
|