1
0
mirror of https://github.com/laurent22/joplin.git synced 2025-06-15 23:00:36 +02:00

All: Decryption worker and handling of missing master key passwords

This commit is contained in:
Laurent Cozic
2017-12-14 18:53:08 +00:00
parent df05d04dad
commit 5bc72e2b44
12 changed files with 230 additions and 22 deletions

View File

@ -1,13 +1,57 @@
const BaseItem = require('lib/models/BaseItem');
class DecryptionWorker {
constructor() {
this.state_ = 'idle';
this.dispatch = (action) => {
console.warn('DecryptionWorker.dispatch is not defined');
};
}
start() {
static instance() {
if (this.instance_) return this.instance_;
this.instance_ = new DecryptionWorker();
return this.instance_;
}
static encryptionService() {
if (!this.encryptionService_) throw new Error('DecryptionWorker.encryptionService_ is not set!!');
return this.encryptionService_;
}
async start() {
if (this.state_ !== 'idle') return;
this.state_ = 'started';
let excludedIds = [];
while (true) {
const result = await BaseItem.itemsThatNeedDecryption(excludedIds);
const items = result.items;
for (let i = 0; i < items.length; i++) {
const item = items[i];
const ItemClass = BaseItem.itemClass(item);
try {
await ItemClass.decrypt(item);
} catch (error) {
if (error.code === 'missingMasterKey') {
excludedIds.push(item.id);
this.dispatch({
type: 'MASTERKEY_ADD_MISSING',
id: error.masterKeyId,
});
continue;
}
throw error;
}
}
if (!result.hasMore) break;
}
}
}