Browse Source

Fix 'Keep me signed in' clearing

master
Eduard Kuzmenko 3 years ago
parent
commit
2513070a79
  1. 18
      src/helpers/toggleStorages.ts
  2. 14
      src/lib/appManagers/appStateManager.ts
  3. 6
      src/lib/appManagers/uiNotificationsManager.ts
  4. 4
      src/lib/cacheStorage.ts
  5. 13
      src/lib/idb.ts
  6. 3
      src/lib/mtproto/apiManager.ts
  7. 25
      src/lib/mtproto/mtproto.worker.ts
  8. 3
      src/lib/mtproto/mtprotoMessagePort.ts
  9. 14
      src/lib/mtproto/mtprotoworker.ts
  10. 2
      src/lib/serviceWorker/index.service.ts
  11. 5
      src/lib/storage.ts
  12. 5
      src/pages/pageSignIn.ts

18
src/helpers/toggleStorages.ts

@ -0,0 +1,18 @@
/*
* https://github.com/morethanwords/tweb
* Copyright (C) 2019-2021 Eduard Kuzmenko
* https://github.com/morethanwords/tweb/blob/master/LICENSE
*/
import CacheStorageController from "../lib/cacheStorage";
import AppStorage from "../lib/storage";
import sessionStorage from "../lib/sessionStorage";
import noop from "./noop";
export default function toggleStorages(enabled: boolean) {
return Promise.all([
AppStorage.toggleStorage(enabled),
CacheStorageController.toggleStorage(enabled),
sessionStorage.toggleStorage(enabled)
]).then(noop, noop);
}

14
src/lib/appManagers/appStateManager.ts

@ -11,7 +11,7 @@ import setDeepProperty from '../../helpers/object/setDeepProperty';
import MTProtoMessagePort from '../mtproto/mtprotoMessagePort'; import MTProtoMessagePort from '../mtproto/mtprotoMessagePort';
export class AppStateManager { export class AppStateManager {
private state: State; private state: State = {} as any;
private storage = stateStorage; private storage = stateStorage;
// ! for mtproto worker use only // ! for mtproto worker use only
@ -23,10 +23,6 @@ export class AppStateManager {
return Promise.resolve(this.state); return Promise.resolve(this.state);
} }
public setState(state: State) {
return this.state = state;
}
public setByKey(key: string, value: any) { public setByKey(key: string, value: any) {
setDeepProperty(this.state, key, value); setDeepProperty(this.state, key, value);
@ -38,20 +34,20 @@ export class AppStateManager {
this.pushToState(first, this.state[first]); this.pushToState(first, this.state[first]);
} }
public pushToState<T extends keyof State>(key: T, value: State[T], direct = true) { public pushToState<T extends keyof State>(key: T, value: State[T], direct = true, onlyLocal?: boolean) {
if(direct) { if(direct) {
this.state[key] = value; this.state[key] = value;
} }
this.setKeyValueToStorage(key, value); this.setKeyValueToStorage(key, value, onlyLocal);
} }
public setKeyValueToStorage<T extends keyof State>(key: T, value: State[T] = this.state[key]) { public setKeyValueToStorage<T extends keyof State>(key: T, value: State[T] = this.state[key], onlyLocal?: boolean) {
MTProtoMessagePort.getInstance<false>().invoke('mirror', {name: 'state', key, value}); MTProtoMessagePort.getInstance<false>().invoke('mirror', {name: 'state', key, value});
this.storage.set({ this.storage.set({
[key]: value [key]: value
}); }, onlyLocal);
} }
/* public resetState() { /* public resetState() {

6
src/lib/appManagers/uiNotificationsManager.ts

@ -22,7 +22,6 @@ import webPushApiManager, { PushSubscriptionNotify } from "../mtproto/webPushApi
import fixEmoji from "../richTextProcessor/fixEmoji"; import fixEmoji from "../richTextProcessor/fixEmoji";
import wrapPlainText from "../richTextProcessor/wrapPlainText"; import wrapPlainText from "../richTextProcessor/wrapPlainText";
import rootScope from "../rootScope"; import rootScope from "../rootScope";
import stateStorage from "../stateStorage";
import appRuntimeManager from "./appRuntimeManager"; import appRuntimeManager from "./appRuntimeManager";
import { AppManagers } from "./managers"; import { AppManagers } from "./managers";
import getPeerId from "./utils/peers/getPeerId"; import getPeerId from "./utils/peers/getPeerId";
@ -493,7 +492,10 @@ export class UiNotificationsManager {
} }
public updateLocalSettings = () => { public updateLocalSettings = () => {
Promise.all(['notify_nodesktop', 'notify_volume', 'notify_novibrate', 'notify_nopreview', 'notify_nopush'].map((k) => stateStorage.get(k as any))) const keys = ['notify_nodesktop', 'notify_volume', 'notify_novibrate', 'notify_nopreview', 'notify_nopush'];
const promises = keys.map(() => undefined);
// const promises = keys.map((k) => stateStorage.get(k as any));
Promise.all(promises)
.then((updSettings) => { .then((updSettings) => {
this.settings.nodesktop = updSettings[0]; this.settings.nodesktop = updSettings[0];
this.settings.volume = updSettings[1] === undefined ? 0.5 : updSettings[1]; this.settings.volume = updSettings[1] === undefined ? 0.5 : updSettings[1];

4
src/lib/cacheStorage.ts

@ -141,7 +141,3 @@ export default class CacheStorageController {
})); }));
} }
} }
//const cacheStorage = new CacheStorageController();
//MOUNT_CLASS_TO.cacheStorage = cacheStorage;
//export default cacheStorage;

13
src/lib/idb.ts

@ -261,7 +261,7 @@ export default class IDBStorage<T extends Database<any>, StoreName extends strin
}, DEBUG ? 'delete: ' + entryName.join(', ') : '', storeName); }, DEBUG ? 'delete: ' + entryName.join(', ') : '', storeName);
} }
public clear(storeName?: StoreName) { public clear(storeName?: StoreName): Promise<void> {
return this.getObjectStore('readwrite', (objectStore) => objectStore.clear(), DEBUG ? 'clear' : '', storeName); return this.getObjectStore('readwrite', (objectStore) => objectStore.clear(), DEBUG ? 'clear' : '', storeName);
} }
@ -422,7 +422,12 @@ export default class IDBStorage<T extends Database<any>, StoreName extends strin
}; };
transaction.onerror = onError; transaction.onerror = onError;
// transaction.oncomplete = () => onComplete('transaction');
// * have to wait while clearing or setting something
const waitForTransactionComplete = mode === 'readwrite';
if(waitForTransactionComplete) {
transaction.oncomplete = () => onComplete(/* 'transaction' */);
}
const timeout = setTimeout(() => { const timeout = setTimeout(() => {
this.log.error('transaction not finished', transaction, log); this.log.error('transaction not finished', transaction, log);
@ -438,6 +443,10 @@ export default class IDBStorage<T extends Database<any>, StoreName extends strin
const isArray = Array.isArray(callbackResult); const isArray = Array.isArray(callbackResult);
const requests: IDBRequest[] = isArray ? callbackResult : [].concat(callbackResult) as any; const requests: IDBRequest[] = isArray ? callbackResult : [].concat(callbackResult) as any;
if(waitForTransactionComplete) {
return;
}
const length = requests.length; const length = requests.length;
let left = length; let left = length;

3
src/lib/mtproto/apiManager.ts

@ -42,6 +42,7 @@ import pause from '../../helpers/schedulers/pause';
import ApiManagerMethods from './api_methods'; import ApiManagerMethods from './api_methods';
import { getEnvironment } from '../../environment/utils'; import { getEnvironment } from '../../environment/utils';
import AppStorage from '../storage'; import AppStorage from '../storage';
import toggleStorages from '../../helpers/toggleStorages';
/* var networker = apiManager.cachedNetworkers.websocket.upload[2]; /* var networker = apiManager.cachedNetworkers.websocket.upload[2];
networker.wrapMtpMessage({ networker.wrapMtpMessage({
@ -296,7 +297,7 @@ export class ApiManager extends ApiManagerMethods {
const clear = async() => { const clear = async() => {
this.baseDcId = undefined; this.baseDcId = undefined;
//this.telegramMeNotify(false); //this.telegramMeNotify(false);
await AppStorage.toggleStorage(false); await toggleStorages(false);
IDB.closeDatabases(); IDB.closeDatabases();
this.rootScope.dispatchEvent('logging_out'); this.rootScope.dispatchEvent('logging_out');
}; };

25
src/lib/mtproto/mtproto.worker.ts

@ -17,12 +17,18 @@ import MTProtoMessagePort from './mtprotoMessagePort';
import RESET_STORAGES_PROMISE from '../appManagers/utils/storages/resetStoragesPromise'; import RESET_STORAGES_PROMISE from '../appManagers/utils/storages/resetStoragesPromise';
import appManagersManager from '../appManagers/appManagersManager'; import appManagersManager from '../appManagers/appManagersManager';
import listenMessagePort from '../../helpers/listenMessagePort'; import listenMessagePort from '../../helpers/listenMessagePort';
import { logger } from '../logger';
import { State } from '../../config/state';
import AppStorage from '../storage';
import toggleStorages from '../../helpers/toggleStorages';
let _isServiceWorkerOnline = true; let _isServiceWorkerOnline = true;
export function isServiceWorkerOnline() { export function isServiceWorkerOnline() {
return _isServiceWorkerOnline; return _isServiceWorkerOnline;
} }
const log = logger('MTPROTO');
const port = new MTProtoMessagePort<false>(); const port = new MTProtoMessagePort<false>();
port.addMultipleEventsListeners({ port.addMultipleEventsListeners({
environment: (environment) => { environment: (environment) => {
@ -36,24 +42,23 @@ port.addMultipleEventsListeners({
}, },
state: ({state, resetStorages, pushedKeys, newVersion, oldVersion, userId}) => { state: ({state, resetStorages, pushedKeys, newVersion, oldVersion, userId}) => {
log('got state', state, pushedKeys);
appStateManager.userId = userId; appStateManager.userId = userId;
appStateManager.newVersion = newVersion; appStateManager.newVersion = newVersion;
appStateManager.oldVersion = oldVersion; appStateManager.oldVersion = oldVersion;
appStateManager.setState(state);
for(const key of pushedKeys) { (Object.keys(state) as any as (keyof State)[]).forEach((key) => {
appStateManager.setKeyValueToStorage(key); appStateManager.pushToState(key, state[key], true, !pushedKeys.includes(key));
} });
RESET_STORAGES_PROMISE.resolve(resetStorages); RESET_STORAGES_PROMISE.resolve(resetStorages);
}, },
toggleStorage: (enabled) => { toggleStorages: (enabled) => toggleStorages(enabled),
// AppStorage.toggleStorage(enabled);
CacheStorageController.toggleStorage(enabled);
},
event: (payload, source) => { event: (payload, source) => {
console.log('will redirect event', payload, source); log('will redirect event', payload, source);
port.invokeExceptSource('event', payload, source); port.invokeExceptSource('event', payload, source);
}, },
@ -94,7 +99,7 @@ port.addMultipleEventsListeners({
// }, // },
}); });
console.log('MTProto start'); log('MTProto start');
appManagersManager.start(); appManagersManager.start();
appManagersManager.getManagers(); appManagersManager.getManagers();

3
src/lib/mtproto/mtprotoMessagePort.ts

@ -11,6 +11,7 @@ import type { StoragesResults } from "../appManagers/utils/storages/loadStorages
import type { LocalStorageProxyTask } from "../localStorage"; import type { LocalStorageProxyTask } from "../localStorage";
import type { Awaited } from "../../types"; import type { Awaited } from "../../types";
import type { Mirrors, MirrorTaskPayload } from "./mtprotoworker"; import type { Mirrors, MirrorTaskPayload } from "./mtprotoworker";
import type toggleStorages from "../../helpers/toggleStorages";
import SuperMessagePort from "./superMessagePort"; import SuperMessagePort from "./superMessagePort";
export type MTProtoManagerTaskPayload = {name: string, method: string, args: any[]}; export type MTProtoManagerTaskPayload = {name: string, method: string, args: any[]};
@ -24,7 +25,7 @@ export default class MTProtoMessagePort<Master extends boolean = true> extends S
crypto: (payload: {method: string, args: any[]}) => Promise<any>, crypto: (payload: {method: string, args: any[]}) => Promise<any>,
state: (payload: {userId: UserId} & Awaited<ReturnType<typeof loadState>> & {storagesResults?: StoragesResults}) => void, state: (payload: {userId: UserId} & Awaited<ReturnType<typeof loadState>> & {storagesResults?: StoragesResults}) => void,
manager: (payload: MTProtoManagerTaskPayload) => any, manager: (payload: MTProtoManagerTaskPayload) => any,
toggleStorage: (enable: boolean) => void, toggleStorages: typeof toggleStorages,
serviceWorkerOnline: (online: boolean) => void, serviceWorkerOnline: (online: boolean) => void,
cryptoPort: (payload: void, source: MessageEventSource, event: MessageEvent) => void, cryptoPort: (payload: void, source: MessageEventSource, event: MessageEvent) => void,
createObjectURL: (blob: Blob) => string createObjectURL: (blob: Blob) => string

14
src/lib/mtproto/mtprotoworker.ts

@ -15,7 +15,7 @@ import webPushApiManager from './webPushApiManager';
import AppStorage from '../storage'; import AppStorage from '../storage';
import appRuntimeManager from '../appManagers/appRuntimeManager'; import appRuntimeManager from '../appManagers/appRuntimeManager';
import telegramMeWebManager from './telegramMeWebManager'; import telegramMeWebManager from './telegramMeWebManager';
import { CacheStorageDbName } from '../cacheStorage'; import CacheStorageController, { CacheStorageDbName } from '../cacheStorage';
import pause from '../../helpers/schedulers/pause'; import pause from '../../helpers/schedulers/pause';
import isObject from '../../helpers/object/isObject'; import isObject from '../../helpers/object/isObject';
import ENVIRONMENT from '../../environment'; import ENVIRONMENT from '../../environment';
@ -26,9 +26,10 @@ import cryptoMessagePort from '../crypto/cryptoMessagePort';
import SuperMessagePort from './superMessagePort'; import SuperMessagePort from './superMessagePort';
import IS_SHARED_WORKER_SUPPORTED from '../../environment/sharedWorkerSupport'; import IS_SHARED_WORKER_SUPPORTED from '../../environment/sharedWorkerSupport';
import type { State } from '../../config/state'; import type { State } from '../../config/state';
import toggleStorages from '../../helpers/toggleStorages';
export interface ToggleStorageTask extends WorkerTaskVoidTemplate { export interface ToggleStorageTask extends WorkerTaskVoidTemplate {
type: 'toggleStorage', type: 'toggleStorages',
payload: boolean payload: boolean
}; };
@ -157,7 +158,7 @@ class ApiManagerProxy extends MTProtoMessagePort {
rootScope.addEventListener('logging_out', () => { rootScope.addEventListener('logging_out', () => {
const toClear: CacheStorageDbName[] = ['cachedFiles', 'cachedStreamChunks']; const toClear: CacheStorageDbName[] = ['cachedFiles', 'cachedStreamChunks'];
Promise.all([ Promise.all([
AppStorage.toggleStorage(false), toggleStorages(false),
sessionStorage.clear(), sessionStorage.clear(),
Promise.race([ Promise.race([
telegramMeWebManager.setAuthorized(false), telegramMeWebManager.setAuthorized(false),
@ -356,9 +357,10 @@ class ApiManagerProxy extends MTProtoMessagePort {
} }
/// #endif /// #endif
public toggleStorage(enabled: boolean) { public async toggleStorages(enabled: boolean) {
const task: ToggleStorageTask = {type: 'toggleStorage', payload: enabled}; await toggleStorages(enabled);
this.invoke('toggleStorage', enabled); this.invoke('toggleStorages', enabled);
const task: ToggleStorageTask = {type: 'toggleStorages', payload: enabled};
this.postSWMessage(task); this.postSWMessage(task);
} }

2
src/lib/serviceWorker/index.service.ts

@ -93,7 +93,7 @@ const taskListeners: {
delete promises[task.id]; delete promises[task.id];
} }
}, },
toggleStorage: (task: ToggleStorageTask) => { toggleStorages: (task: ToggleStorageTask) => {
CacheStorageController.toggleStorage(task.payload); CacheStorageController.toggleStorage(task.payload);
} }
}; };

5
src/lib/storage.ts

@ -13,6 +13,7 @@ import { Database } from "../config/databases";
import { MOUNT_CLASS_TO } from "../config/debug"; import { MOUNT_CLASS_TO } from "../config/debug";
//import DATABASE_SESSION from "../config/databases/session"; //import DATABASE_SESSION from "../config/databases/session";
import deferredPromise, { CancellablePromise } from "../helpers/cancellablePromise"; import deferredPromise, { CancellablePromise } from "../helpers/cancellablePromise";
import { IS_WORKER } from "../helpers/context";
import throttle from "../helpers/schedulers/throttle"; import throttle from "../helpers/schedulers/throttle";
//import { WorkerTaskTemplate } from "../types"; //import { WorkerTaskTemplate } from "../types";
import IDBStorage from "./idb"; import IDBStorage from "./idb";
@ -295,6 +296,10 @@ export default class AppStorage<
return Promise.all(this.STORAGES.map((storage) => { return Promise.all(this.STORAGES.map((storage) => {
storage.useStorage = enabled; storage.useStorage = enabled;
if(!IS_WORKER) {
return;
}
if(!enabled) { if(!enabled) {
storage.keysToSet.clear(); storage.keysToSet.clear();
storage.keysToDelete.clear(); storage.keysToDelete.clear();

5
src/pages/pageSignIn.ts

@ -341,10 +341,7 @@ let onFirstMount = () => {
const keepSigned = signedCheckboxField.checked; const keepSigned = signedCheckboxField.checked;
rootScope.managers.appStateManager.pushToState('keepSigned', keepSigned); rootScope.managers.appStateManager.pushToState('keepSigned', keepSigned);
AppStorage.toggleStorage(keepSigned); apiManagerProxy.toggleStorages(keepSigned);
CacheStorageController.toggleStorage(keepSigned);
apiManagerProxy.toggleStorage(keepSigned);
sessionStorage.toggleStorage(keepSigned);
}); });
apiManagerProxy.getState().then((state) => { apiManagerProxy.getState().then((state) => {

Loading…
Cancel
Save