Skip to content

Commit

Permalink
Clear other storages than session on logout
Browse files Browse the repository at this point in the history
Fix blinking fonts
  • Loading branch information
morethanwords committed May 1, 2021
1 parent 73d0813 commit 56aa8f9
Show file tree
Hide file tree
Showing 13 changed files with 129 additions and 61 deletions.
2 changes: 1 addition & 1 deletion server.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const server = useHttp ? http : https;
let options = {};
if(!useHttp) {
options.key = fs.readFileSync(__dirname + '/certs/server-key.pem');
options.cert = s.readFileSync(__dirname + '/certs/server-cert.pem');
options.cert = fs.readFileSync(__dirname + '/certs/server-cert.pem');
}

server.createServer(options, app).listen(port, () => {
Expand Down
7 changes: 5 additions & 2 deletions src/components/popups/deleteDialog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ export default class PopupDeleteDialog {
}; */

const callbackLeave = (checked: PopupPeerButtonCallbackCheckboxes) => {
const promise = appChatsManager.leave(-peerId);
const promise = appChatsManager.leave(-peerId).then(() => {
return appMessagesManager.flushHistory(-peerId);
});

onSelect && onSelect(promise);
};

Expand All @@ -37,7 +40,7 @@ export default class PopupDeleteDialog {
if(checked[checkboxes[0].text]) {
promise = appChatsManager.delete(-peerId);
} else {
promise = appChatsManager.leave(-peerId);
return callbackLeave(checked);
}
}

Expand Down
21 changes: 20 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,11 @@ console.timeEnd('get storage1'); */
//import('./vendor/dateFormat');

const langPromise = I18n.default.getCacheLangPack();

function loadFonts(): Promise<void> {
// @ts-ignore
return 'fonts' in document ? Promise.all(['400 1rem Roboto', '500 1rem Roboto'].map(font => document.fonts.load(font))) : Promise.resolve();
}

const [state, langPack] = await Promise.all([
appStateManager.default.getState(),
Expand All @@ -263,6 +268,16 @@ console.timeEnd('get storage1'); */
I18n.default.getLangPack(langPack.lang_code);
}

function fadeInWhenFontsReady(elem: HTMLElement, promise: Promise<void>) {
elem.style.opacity = '0';

promise.then(() => {
window.requestAnimationFrame(() => {
elem.style.opacity = '';
});
});
}

console.log('got state, time:', performance.now() - perf);

const authState = state.authState;
Expand All @@ -271,11 +286,14 @@ console.timeEnd('get storage1'); */

const el = document.getElementById('auth-pages');
if(el) {
const scrollable = el.querySelector('.scrollable');
const scrollable = el.querySelector('.scrollable') as HTMLElement;
if((!touchSupport.isTouchSupported || isMobileSafari)) {
scrollable.classList.add('no-scrollbar');
}

// @ts-ignore
fadeInWhenFontsReady(scrollable, 'fonts' in document ? document.fonts.ready : Promise.resolve());

const placeholder = document.createElement('div');
placeholder.classList.add('auth-placeholder');

Expand Down Expand Up @@ -348,6 +366,7 @@ console.timeEnd('get storage1'); */
}, 500); */
} else {
console.log('Will mount IM page:', Date.now() / 1000);
fadeInWhenFontsReady(document.getElementById('main-columns'), loadFonts());
(await import('./pages/pageIm')).default.mount();
//getNearestDc();
}
Expand Down
52 changes: 28 additions & 24 deletions src/lib/appManagers/apiUpdatesManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,15 @@

//import apiManager from '../mtproto/apiManager';
import DEBUG, { MOUNT_CLASS_TO } from '../../config/debug';
import { copy } from '../../helpers/object';
import { Update } from '../../layer';
import { logger, LogTypes } from '../logger';
import apiManager from '../mtproto/mtprotoworker';
import rootScope from '../rootScope';
//import networkerFactory from '../mtproto/networkerFactory';
import appUsersManager from "./appUsersManager";
import appChatsManager from "./appChatsManager";
import appPeersManager from "./appPeersManager";
import appStateManager from './appStateManager';
import appUsersManager from "./appUsersManager";

type UpdatesState = {
pendingPtsUpdates: {pts: number, pts_count: number}[],
Expand Down Expand Up @@ -51,7 +50,7 @@ export class ApiUpdatesManager {
public channelStates: {[channelId: number]: UpdatesState} = {};
private attached = false;

private log = logger('UPDATES', LogTypes.Error | LogTypes.Warn/* | LogTypes.Log | LogTypes.Debug */);
private log = logger('UPDATES', LogTypes.Error | LogTypes.Warn | LogTypes.Log/* | LogTypes.Debug */);
private debug = DEBUG;

private setProxy() {
Expand All @@ -60,17 +59,21 @@ export class ApiUpdatesManager {
set: function(target: ApiUpdatesManager['updatesState'], key: keyof ApiUpdatesManager['updatesState'], value: ApiUpdatesManager['updatesState'][typeof key]) {
// @ts-ignore
target[key] = value;
const us = self.updatesState;
appStateManager.pushToState('updates', {
seq: us.seq,
pts: us.pts,
date: us.date
});
self.saveUpdatesState();
return true;
}
});
}

public saveUpdatesState() {
const us = this.updatesState;
appStateManager.pushToState('updates', {
seq: us.seq,
pts: us.pts,
date: us.date
});
}

private popPendingSeqUpdate() {
const state = this.updatesState;
const nextSeq = state.seq + 1;
Expand Down Expand Up @@ -132,7 +135,7 @@ export class ApiUpdatesManager {
return false;
}

this.debug && this.log('pop pending pts updates', goodPts, curState.pendingPtsUpdates.slice(0, goodIndex + 1));
this.debug && this.log.debug('pop pending pts updates', goodPts, curState.pendingPtsUpdates.slice(0, goodIndex + 1));

curState.pts = goodPts;
for(let i = 0; i <= goodIndex; ++i) {
Expand Down Expand Up @@ -172,7 +175,7 @@ export class ApiUpdatesManager {
//ignoreSyncLoading: options.ignoreSyncLoading
};

this.debug && this.log('processUpdateMessage', updateMessage);
this.debug && this.log.debug('processUpdateMessage', updateMessage);

switch(updateMessage._) {
case 'updatesTooLong':
Expand All @@ -186,7 +189,7 @@ export class ApiUpdatesManager {

case 'updateShortMessage':
case 'updateShortChatMessage': {
this.debug && this.log('updateShortMessage | updateShortChatMessage', {...updateMessage});
this.debug && this.log.debug('updateShortMessage | updateShortChatMessage', {...updateMessage});
const isOut = updateMessage.pFlags.out;
const fromId = updateMessage.from_id || (isOut ? rootScope.myId : updateMessage.user_id);
const toId = updateMessage.chat_id
Expand Down Expand Up @@ -249,10 +252,10 @@ export class ApiUpdatesManager {
}, {
timeout: 0x7fffffff
}).then((differenceResult) => {
this.debug && this.log('Get diff result', differenceResult);
this.debug && this.log.debug('Get diff result', differenceResult);

if(differenceResult._ === 'updates.differenceEmpty') {
this.debug && this.log('apply empty diff', differenceResult.seq);
this.debug && this.log.debug('apply empty diff', differenceResult.seq);
updatesState.date = differenceResult.date;
updatesState.seq = differenceResult.seq;
return;
Expand Down Expand Up @@ -307,7 +310,7 @@ export class ApiUpdatesManager {
if(differenceResult._ === 'updates.differenceSlice') {
return this.getDifference();
} else {
this.debug && this.log('finished get diff');
this.debug && this.log.debug('finished get diff');
}
});

Expand Down Expand Up @@ -337,16 +340,16 @@ export class ApiUpdatesManager {
pts: channelState.pts,
limit: 30
}, {timeout: 0x7fffffff}).then((differenceResult) => {
this.debug && this.log('Get channel diff result', differenceResult)
this.debug && this.log.debug('Get channel diff result', differenceResult)
channelState.pts = 'pts' in differenceResult ? differenceResult.pts : undefined;

if(differenceResult._ === 'updates.channelDifferenceEmpty') {
this.debug && this.log('apply channel empty diff', differenceResult);
this.debug && this.log.debug('apply channel empty diff', differenceResult);
return;
}

if(differenceResult._ === 'updates.channelDifferenceTooLong') {
this.debug && this.log('channel diff too long', differenceResult);
this.debug && this.log.debug('channel diff too long', differenceResult);
delete this.channelStates[channelId];

// @ts-ignore
Expand All @@ -358,12 +361,12 @@ export class ApiUpdatesManager {
appChatsManager.saveApiChats(differenceResult.chats);

// Should be first because of updateMessageID
this.debug && this.log('applying', differenceResult.other_updates.length, 'channel other updates');
this.debug && this.log.debug('applying', differenceResult.other_updates.length, 'channel other updates');
differenceResult.other_updates.forEach((update) => {
this.saveUpdate(update);
});

this.debug && this.log('applying', differenceResult.new_messages.length, 'channel new messages');
this.debug && this.log.debug('applying', differenceResult.new_messages.length, 'channel new messages');
differenceResult.new_messages.forEach((apiMessage) => {
this.saveUpdate({
_: 'updateNewChannelMessage',
Expand All @@ -373,13 +376,13 @@ export class ApiUpdatesManager {
});
});

this.debug && this.log('apply channel diff', channelState.pts);
this.debug && this.log.debug('apply channel diff', channelState.pts);

if(differenceResult._ === 'updates.channelDifference' &&
!differenceResult.pFlags['final']) {
return this.getChannelDifference(channelId);
} else {
this.debug && this.log('finished channel get diff');
this.debug && this.log.debug('finished channel get diff');
}
});

Expand Down Expand Up @@ -593,7 +596,7 @@ export class ApiUpdatesManager {
}

public saveUpdate(update: Update) {
this.log('saveUpdate', update);
this.debug && this.log('saveUpdate', update);
rootScope.dispatchEvent(update._, update as any);
}

Expand All @@ -618,6 +621,7 @@ export class ApiUpdatesManager {
this.updatesState.seq = stateResult.seq;
this.updatesState.pts = stateResult.pts;
this.updatesState.date = stateResult.date;
this.saveUpdatesState();
//setTimeout(() => {
this.updatesState.syncLoading = null;
resolve();
Expand All @@ -639,7 +643,7 @@ export class ApiUpdatesManager {

Object.assign(this.updatesState, state);

this.log('will get difference', copy(state));
this.log('will get difference', Object.assign({}, state));

this.getDifference(true)/* .finally(() => {
if(this.updatesState.syncLoading) {
Expand Down
9 changes: 2 additions & 7 deletions src/lib/appManagers/appChatsManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import apiManager from '../mtproto/mtprotoworker';
import { RichTextProcessor } from "../richtextprocessor";
import rootScope from "../rootScope";
import apiUpdatesManager from "./apiUpdatesManager";
import appMessagesManager from "./appMessagesManager";
import appPeersManager from "./appPeersManager";
import appProfileManager from "./appProfileManager";
import appStateManager from "./appStateManager";
Expand Down Expand Up @@ -637,12 +636,8 @@ export class AppChatsManager {
}).then(this.onChatUpdated.bind(this, id));
}

public leaveChat(id: number, flushHistory = true) {
let promise: Promise<any> = this.deleteChatUser(id, appUsersManager.getSelf().id)
if(flushHistory) promise = promise.then(() => {
return appMessagesManager.flushHistory(-id);
});
return promise;;
public leaveChat(id: number) {
return this.deleteChatUser(id, appUsersManager.getSelf().id);
}

public leave(id: number) {
Expand Down
29 changes: 19 additions & 10 deletions src/lib/appManagers/appDialogsManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ export class AppDialogsManager {
title: HTMLElement
}
} = {};
private showFiltersTimeout: number;
private showFiltersPromise: Promise<void>;
private allUnreadCount: HTMLElement;

private accumulateArchivedTimeout: number;
Expand Down Expand Up @@ -491,16 +491,22 @@ export class AppDialogsManager {
//selectTab(0);
(this.folders.menu.firstElementChild as HTMLElement).click();
appMessagesManager.construct();
appStateManager.getState().then((state) => {
appStateManager.getState().then(async(state) => {
appNotificationsManager.getNotifyPeerTypeSettings();

const getFiltersPromise = appMessagesManager.filtersStorage.getDialogFilters();
getFiltersPromise.then((filters) => {
const renderFiltersPromise = appMessagesManager.filtersStorage.getDialogFilters().then((filters) => {
for(const filter of filters) {
this.addFilter(filter);
}
});

if(state.filters && Object.keys(state.filters).length) {
await renderFiltersPromise;
if(this.showFiltersPromise) {
await this.showFiltersPromise;
}
}

if(appStateManager.storagesResults.dialogs.length) {
appDraftsManager.getAllDrafts();
appDraftsManager.addMissedDialogs();
Expand Down Expand Up @@ -706,12 +712,15 @@ export class AppDialogsManager {
title: titleSpan
};

if(!this.showFiltersTimeout && Object.keys(this.filtersRendered).length > 1) {
this.showFiltersTimeout = window.setTimeout(() => {
this.showFiltersTimeout = 0;
this.folders.menuScrollContainer.classList.remove('hide');
this.setFiltersUnreadCount();
}, 0);
if(!this.showFiltersPromise && Object.keys(this.filtersRendered).length > 1) {
this.showFiltersPromise = new Promise<void>((resolve) => {
window.setTimeout(() => {
this.showFiltersPromise = undefined;
this.folders.menuScrollContainer.classList.remove('hide');
this.setFiltersUnreadCount();
resolve();
}, 0);
});
}
}

Expand Down
3 changes: 1 addition & 2 deletions src/lib/appManagers/appMessagesManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,8 +305,8 @@ export class AppMessagesManager {
}

public construct() {
this.dialogsStorage = new DialogsStorage(this, appChatsManager, appPeersManager, appUsersManager, appDraftsManager, appNotificationsManager, appStateManager, apiUpdatesManager, serverTimeManager);
this.filtersStorage = new FiltersStorage(this, appPeersManager, appUsersManager, appNotificationsManager, appStateManager, apiUpdatesManager, /* apiManager, */ rootScope);
this.dialogsStorage = new DialogsStorage(this, appChatsManager, appPeersManager, appUsersManager, appDraftsManager, appNotificationsManager, appStateManager, apiUpdatesManager, serverTimeManager);
}

public getInputEntities(entities: MessageEntity[]) {
Expand Down Expand Up @@ -2362,7 +2362,6 @@ export class AppMessagesManager {
break;

case 'messageActionPhoneCall':
delete message.fromId;
message.action.type =
(message.pFlags.out ? 'out_' : 'in_') +
(
Expand Down
Loading

0 comments on commit 56aa8f9

Please sign in to comment.