2021-01-22 19:41:11 +02:00
|
|
|
import BaseModel from '../BaseModel';
|
2021-08-17 13:03:19 +02:00
|
|
|
import { MasterKeyEntity } from '../services/e2ee/types';
|
2021-08-12 17:54:10 +02:00
|
|
|
import { localSyncInfo, saveLocalSyncInfo } from '../services/synchronizer/syncInfoUtils';
|
2021-01-22 19:41:11 +02:00
|
|
|
import BaseItem from './BaseItem';
|
2021-08-12 17:54:10 +02:00
|
|
|
import uuid from '../uuid';
|
2017-12-12 20:41:02 +02:00
|
|
|
|
2021-01-22 19:41:11 +02:00
|
|
|
export default class MasterKey extends BaseItem {
|
2017-12-12 20:41:02 +02:00
|
|
|
static tableName() {
|
|
|
|
return 'master_keys';
|
|
|
|
}
|
|
|
|
|
|
|
|
static modelType() {
|
|
|
|
return BaseModel.TYPE_MASTER_KEY;
|
|
|
|
}
|
|
|
|
|
2017-12-13 20:57:40 +02:00
|
|
|
static encryptionSupported() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-08-12 17:54:10 +02:00
|
|
|
public static latest() {
|
|
|
|
let output: MasterKeyEntity = null;
|
|
|
|
const syncInfo = localSyncInfo();
|
|
|
|
for (const mk of syncInfo.masterKeys) {
|
|
|
|
if (!output || output.updated_time < mk.updated_time) {
|
|
|
|
output = mk;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return output;
|
|
|
|
// return this.modelSelectOne('SELECT * FROM master_keys WHERE created_time >= (SELECT max(created_time) FROM master_keys)');
|
2017-12-14 21:39:13 +02:00
|
|
|
}
|
|
|
|
|
2021-01-22 19:41:11 +02:00
|
|
|
static allWithoutEncryptionMethod(masterKeys: MasterKeyEntity[], method: number) {
|
2020-05-21 10:14:33 +02:00
|
|
|
return masterKeys.filter(m => m.encryption_method !== method);
|
2020-03-13 19:42:50 +02:00
|
|
|
}
|
|
|
|
|
2021-08-12 17:54:10 +02:00
|
|
|
public static async all(): Promise<MasterKeyEntity[]> {
|
|
|
|
return localSyncInfo().masterKeys;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static async allIds(): Promise<string[]> {
|
|
|
|
return localSyncInfo().masterKeys.map(k => k.id);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static async count(): Promise<number> {
|
|
|
|
return localSyncInfo().masterKeys.length;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static async load(id: string): Promise<MasterKeyEntity> {
|
|
|
|
return localSyncInfo().masterKeys.find(mk => mk.id === id);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static async save(o: MasterKeyEntity): Promise<MasterKeyEntity> {
|
|
|
|
const syncInfo = localSyncInfo();
|
|
|
|
|
|
|
|
const masterKey = { ...o };
|
|
|
|
if (!masterKey.id) {
|
|
|
|
masterKey.id = uuid.create();
|
|
|
|
masterKey.created_time = Date.now();
|
|
|
|
}
|
|
|
|
|
|
|
|
masterKey.updated_time = Date.now();
|
|
|
|
|
|
|
|
const idx = syncInfo.masterKeys.findIndex(mk => mk.id === masterKey.id);
|
|
|
|
|
|
|
|
if (idx >= 0) {
|
|
|
|
syncInfo.masterKeys[idx] = masterKey;
|
|
|
|
} else {
|
|
|
|
syncInfo.masterKeys.push(masterKey);
|
|
|
|
}
|
|
|
|
|
|
|
|
saveLocalSyncInfo(syncInfo);
|
|
|
|
|
|
|
|
this.dispatch({
|
|
|
|
type: 'MASTERKEY_UPDATE_ONE',
|
|
|
|
item: masterKey,
|
2017-12-14 21:39:13 +02:00
|
|
|
});
|
2021-08-12 17:54:10 +02:00
|
|
|
|
|
|
|
return masterKey;
|
|
|
|
|
|
|
|
// return super.save(o, options).then(item => {
|
|
|
|
// this.dispatch({
|
|
|
|
// type: 'MASTERKEY_UPDATE_ONE',
|
|
|
|
// item: item,
|
|
|
|
// });
|
|
|
|
// return item;
|
|
|
|
// });
|
2017-12-14 21:39:13 +02:00
|
|
|
}
|
2017-12-12 20:41:02 +02:00
|
|
|
}
|