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 {
|
2023-03-06 16:22:01 +02:00
|
|
|
public static tableName() {
|
2017-12-12 20:41:02 +02:00
|
|
|
return 'master_keys';
|
|
|
|
}
|
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public static modelType() {
|
2017-12-12 20:41:02 +02:00
|
|
|
return BaseModel.TYPE_MASTER_KEY;
|
|
|
|
}
|
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public static encryptionSupported() {
|
2017-12-13 20:57:40 +02:00
|
|
|
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;
|
2017-12-14 21:39:13 +02:00
|
|
|
}
|
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public static allWithoutEncryptionMethod(masterKeys: MasterKeyEntity[], methods: number[]) {
|
2021-10-03 17:00:49 +02:00
|
|
|
return masterKeys.filter(m => !methods.includes(m.encryption_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
|
|
|
}
|