core: remove outbox

This commit is contained in:
Abdullah Atta
2023-09-18 15:48:58 +05:00
parent 66cd011e04
commit 3e7e410460
3 changed files with 32 additions and 94 deletions

View File

@@ -34,7 +34,6 @@ import Hosts from "../utils/constants";
import { EV, EVENTS } from "../common";
import { LegacySettings } from "../collections/legacy-settings";
import Migrations from "./migrations";
import Outbox from "./outbox";
import UserManager from "./user-manager";
import http from "../utils/http";
import { Monographs } from "./monographs";
@@ -132,7 +131,6 @@ class Database {
legacySettings = new LegacySettings(this);
settings = new Settings(this);
migrations = new Migrations(this);
outbox = new Outbox(this);
monographs = new Monographs(this);
trash = new Trash(this);
@@ -185,7 +183,6 @@ class Database {
await this.initCollections();
await this.outbox.init();
await this.migrations.init();
this.isInitialized = true;
if (this.migrations.required()) {

View File

@@ -1,57 +0,0 @@
/*
This file is part of the Notesnook project (https://notesnook.com/)
Copyright (C) 2023 Streetwriters (Private) Limited
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
class Outbox {
/**
*
* @param {import("./index").default} db
*/
constructor(db) {
this._db = db;
this.outbox = {};
}
async init() {
this.outbox = (await this._db.storage().read("outbox")) || {};
for (var id in this.outbox) {
const data = this.outbox[id];
switch (id) {
case "reset_password":
case "change_password":
if (await this._db.user._updatePassword(id, data))
await this.delete(id);
break;
}
}
}
async add(id, data, action) {
this.outbox[id] = data;
await this._db.storage().write("outbox", this.outbox);
await action();
await this.delete(id);
}
delete(id) {
delete this.outbox[id];
return this._db.storage().write("outbox", this.outbox);
}
}
export default Outbox;

View File

@@ -483,43 +483,41 @@ class UserManager {
const attachmentsKey = await this.getAttachmentsKey();
data.encryptionKey = data.encryptionKey || (await this.getEncryptionKey());
await this.db.outbox.add(type, data, async () => {
if (data.encryptionKey) await this.db.sync(true, true);
if (data.encryptionKey) await this.db.sync(true, true);
await this.db.storage().deriveCryptoKey(`_uk_@${email}`, {
password: new_password,
salt
});
if (!(await this.resetUser(false))) return;
if (attachmentsKey) {
const userEncryptionKey = await this.getEncryptionKey();
if (!userEncryptionKey) return;
user.attachmentsKey = await this.db
.storage()
.encrypt(userEncryptionKey, JSON.stringify(attachmentsKey));
await this.updateUser(user);
}
await this.db.sync(false, true);
if (old_password)
old_password = await this.db.storage().hash(old_password, email);
if (new_password)
new_password = await this.db.storage().hash(new_password, email);
await http.patch(
`${constants.AUTH_HOST}${ENDPOINTS.patchUser}`,
{
type,
old_password,
new_password
},
token
);
await this.db.storage().deriveCryptoKey(`_uk_@${email}`, {
password: new_password,
salt
});
if (!(await this.resetUser(false))) return;
if (attachmentsKey) {
const userEncryptionKey = await this.getEncryptionKey();
if (!userEncryptionKey) return;
user.attachmentsKey = await this.db
.storage()
.encrypt(userEncryptionKey, JSON.stringify(attachmentsKey));
await this.updateUser(user);
}
await this.db.sync(false, true);
if (old_password)
old_password = await this.db.storage().hash(old_password, email);
if (new_password)
new_password = await this.db.storage().hash(new_password, email);
await http.patch(
`${constants.AUTH_HOST}${ENDPOINTS.patchUser}`,
{
type,
old_password,
new_password
},
token
);
return true;
}
}