tweb/src/lib/rootScope.ts

315 lines
10 KiB
TypeScript
Raw Normal View History

2021-04-08 15:52:31 +02:00
/*
* https://github.com/morethanwords/tweb
* Copyright (C) 2019-2021 Eduard Kuzmenko
* https://github.com/morethanwords/tweb/blob/master/LICENSE
*/
2022-02-08 20:18:01 +01:00
import type { Message, StickerSet, Update, NotifyPeer, PeerNotifySettings, ConstructorDeclMap, Config, PollResults, Poll, WebPage, GroupCall, GroupCallParticipant, PhoneCall, MethodDeclMap, MessageReactions, ReactionCount } from "../layer";
import type { MyDocument } from "./appManagers/appDocsManager";
2021-10-21 15:16:43 +02:00
import type { AppMessagesManager, Dialog, MessagesStorage, MyMessage } from "./appManagers/appMessagesManager";
import type { MyDialogFilter } from "./storages/filters";
2021-10-21 15:16:43 +02:00
import type { Folder } from "./storages/dialogs";
2021-06-18 14:00:30 +02:00
import type { UserTyping } from "./appManagers/appProfileManager";
import type { State, Theme } from "./appManagers/appStateManager";
import type { MyDraftMessage } from "./appManagers/appDraftsManager";
2021-06-18 04:30:36 +02:00
import type { PushSubscriptionNotify } from "./mtproto/webPushApiManager";
import type { PushNotificationObject } from "./serviceWorker/push";
2021-06-22 03:34:04 +02:00
import type { ConnectionStatusChange } from "./mtproto/connectionStatus";
import type { GroupCallId } from "./appManagers/appGroupCallsManager";
import type GroupCallInstance from "./calls/groupCallInstance";
2022-03-24 15:16:41 +01:00
import type CallInstance from "./calls/callInstance";
2021-12-11 17:37:08 +01:00
import type { StreamAmplitude } from "./calls/streamManager";
import type Chat from "../components/chat/chat";
import { NULL_PEER_ID, UserAuth } from "./mtproto/mtproto_config";
import EventListenerBase from "../helpers/eventListenerBase";
2021-02-13 16:32:10 +01:00
import { MOUNT_CLASS_TO } from "../config/debug";
2022-01-24 09:25:21 +01:00
import { MTAppConfig } from "./mtproto/appConfig";
2021-03-10 21:57:28 +01:00
export type BroadcastEvents = {
2021-12-11 17:37:08 +01:00
'chat_full_update': ChatId,
'chat_update': ChatId,
'channel_update': ChatId,
2021-12-11 17:37:08 +01:00
2021-10-21 15:16:43 +02:00
'user_update': UserId,
'user_auth': UserAuth,
2021-12-11 17:37:08 +01:00
'user_full_update': UserId,
'chat_changing': {from: Chat, to: Chat},
2021-10-21 15:16:43 +02:00
'peer_changed': PeerId,
2021-01-09 15:08:26 +01:00
'peer_changing': Chat,
2021-10-21 15:16:43 +02:00
'peer_pinned_messages': {peerId: PeerId, mids?: number[], pinned?: boolean, unpinAll?: true},
'peer_pinned_hidden': {peerId: PeerId, maxId: number},
'peer_typings': {peerId: PeerId, typings: UserTyping[]},
'peer_block': {peerId: PeerId, blocked: boolean},
'peer_title_edit': PeerId,
'peer_bio_edit': PeerId,
2021-11-12 18:40:13 +01:00
'peer_deleted': PeerId, // left chat, deleted user dialog, left channel
2021-12-11 17:37:08 +01:00
'peer_full_update': PeerId,
'filter_delete': MyDialogFilter,
'filter_update': MyDialogFilter,
'filter_new': MyDialogFilter,
'filter_order': number[],
2021-10-21 15:16:43 +02:00
'folder_unread': Folder,
2021-10-21 15:16:43 +02:00
'dialog_draft': {peerId: PeerId, dialog: Dialog, drop: boolean, draft: MyDraftMessage | undefined, index: number},
'dialog_unread': {peerId: PeerId},
'dialog_flush': {peerId: PeerId},
'dialog_drop': {peerId: PeerId, dialog?: Dialog},
'dialog_migrate': {migrateFrom: PeerId, migrateTo: PeerId},
//'dialog_top': Dialog,
2021-03-11 04:06:44 +01:00
'dialog_notify_settings': Dialog,
2021-09-17 18:50:29 +02:00
// 'dialog_order': {dialog: Dialog, pos: number},
2021-10-21 15:16:43 +02:00
'dialogs_multiupdate': {[peerId: PeerId]: Dialog},
2021-10-21 15:16:43 +02:00
'history_append': {storage: MessagesStorage, peerId: PeerId, mid: number},
'history_update': {storage: MessagesStorage, peerId: PeerId, mid: number},
'history_reply_markup': {peerId: PeerId},
'history_multiappend': AppMessagesManager['newMessagesToHandle'],
2021-10-21 15:16:43 +02:00
'history_delete': {peerId: PeerId, msgs: Set<number>},
'history_forbidden': PeerId,
'history_reload': PeerId,
2022-01-15 00:20:59 +01:00
'history_focus': {peerId: PeerId, threadId?: number, mid?: number, startParam?: string},
//'history_request': void,
2021-10-21 15:16:43 +02:00
'message_edit': {storage: MessagesStorage, peerId: PeerId, mid: number},
'message_sent': {storage: MessagesStorage, tempId: number, tempMessage: any, mid: number, message: MyMessage},
2022-03-28 17:16:12 +02:00
'messages_views': {peerId: PeerId, mid: number, views: number}[],
'messages_reactions': {message: Message.message, changedResults: ReactionCount[]}[],
'messages_pending': void,
'messages_read': void,
2021-10-21 15:16:43 +02:00
'messages_downloaded': {peerId: PeerId, mids: number[]},
'messages_media_read': {peerId: PeerId, mids: number[]},
2020-12-22 04:02:30 +01:00
'replies_updated': Message.message,
2021-10-21 15:16:43 +02:00
'scheduled_new': {peerId: PeerId, mid: number},
'scheduled_delete': {peerId: PeerId, mids: number[]},
2021-10-21 15:16:43 +02:00
'album_edit': {peerId: PeerId, groupId: string, deletedMids: number[]},
'stickers_installed': StickerSet.stickerSet,
'stickers_deleted': StickerSet.stickerSet,
2021-10-05 22:40:07 +02:00
'media_play': {doc: MyDocument, message: Message.message, media: HTMLMediaElement},
'media_pause': void,
'media_playback_params': {volume: number, muted: boolean, playbackRate: number},
'media_stop': void,
2020-11-15 22:14:05 +01:00
2021-06-11 14:52:53 +02:00
'state_cleared': void,
2021-10-21 15:16:43 +02:00
'state_synchronized': ChatId | void,
'state_synchronizing': ChatId | void,
2020-11-15 22:14:05 +01:00
2021-10-21 15:16:43 +02:00
'contacts_update': UserId,
'avatar_update': PeerId,
'poll_update': {poll: Poll, results: PollResults},
2021-10-21 15:16:43 +02:00
'invalidate_participants': ChatId,
//'channel_settings': {channelId: number},
2021-10-21 15:16:43 +02:00
'webpage_updated': {id: WebPage.webPage['id'], msgs: {peerId: PeerId, mid: number, isScheduled: boolean}[]},
2021-01-03 13:02:35 +01:00
'connection_status_change': ConnectionStatusChange,
'settings_updated': {key: string, value: any},
2021-10-21 15:16:43 +02:00
'draft_updated': {peerId: PeerId, threadId: number, draft: MyDraftMessage | undefined, force?: boolean},
2021-08-18 16:48:13 +02:00
'event-heavy-animation-start': void,
'event-heavy-animation-end': void,
2021-08-18 16:48:13 +02:00
'im_mount': void,
'im_tab_change': number,
2021-08-18 16:48:13 +02:00
'idle': boolean,
2021-08-18 16:48:13 +02:00
'overlay_toggle': boolean,
2021-08-18 16:48:13 +02:00
'background_change': void,
2021-08-18 16:48:13 +02:00
'privacy_update': Update.updatePrivacy,
2021-08-18 16:48:13 +02:00
2021-03-11 04:06:44 +01:00
'notify_settings': Update.updateNotifySettings,
'notify_peer_type_settings': {key: Exclude<NotifyPeer['_'], 'notifyPeer'>, settings: PeerNotifySettings},
2021-08-18 16:48:13 +02:00
2021-06-14 20:13:31 +02:00
'language_change': string,
'theme_change': void,
2021-08-18 16:48:13 +02:00
2021-06-18 04:30:36 +02:00
'instance_activated': void,
'instance_deactivated': void,
2021-08-18 16:48:13 +02:00
2021-06-18 04:30:36 +02:00
'push_notification_click': PushNotificationObject,
'push_init': PushSubscriptionNotify,
'push_subscribe': PushSubscriptionNotify,
'push_unsubscribe': PushSubscriptionNotify,
2021-08-18 16:48:13 +02:00
'emoji_recent': string,
2021-10-21 15:16:43 +02:00
'download_start': DocId,
2021-08-18 16:48:13 +02:00
'download_progress': any,
'document_downloaded': MyDocument,
2021-08-25 00:19:05 +02:00
2021-11-29 16:24:29 +01:00
'context_menu_toggle': boolean,
'choosing_sticker': boolean
2021-12-11 17:37:08 +01:00
'group_call_instance': GroupCallInstance,
2021-12-11 17:37:08 +01:00
'group_call_update': GroupCall,
'group_call_amplitude': {amplitudes: StreamAmplitude[], type: 'all' | 'input'},
'group_call_participant': {groupCallId: GroupCallId, participant: GroupCallParticipant},
// 'group_call_video_track_added': {instance: GroupCallInstance}
2022-03-24 15:16:41 +01:00
'call_instance': {hasCurrent: boolean, instance: CallInstance},
'call_accepting': CallInstance, // это костыль. используется при параллельном вызове, чтобы заменить звонок в topbarCall
2022-03-25 17:42:35 +01:00
'call_incompatible': UserId,
2022-02-08 20:18:01 +01:00
'quick_reaction': string,
'missed_reactions_element': {message: Message.message, changedResults: ReactionCount[]}
};
2021-04-24 19:06:24 +02:00
export class RootScope extends EventListenerBase<{
[name in Update['_']]: (update: ConstructorDeclMap[name]) => void
} & {
[name in keyof BroadcastEvents]: (e: BroadcastEvents[name]) => void
}> {
public overlaysActive = 0;
2021-10-21 15:16:43 +02:00
public myId: PeerId;
2020-11-15 04:33:47 +01:00
public idle = {
isIDLE: true,
deactivated: false,
focusPromise: Promise.resolve(),
focusResolve: () => {}
2020-11-15 04:33:47 +01:00
};
public connectionStatus: {[name: string]: ConnectionStatusChange} = {};
2021-01-03 13:02:35 +01:00
public settings: State['settings'];
2021-10-21 15:16:43 +02:00
public peerId: PeerId;
public filterId = 0;
public systemTheme: Theme['name'];
public config: Partial<Config.config> = {
forwarded_count_max: 100,
edit_time_limit: 86400 * 2,
pinned_dialogs_count_max: 5,
pinned_infolder_count_max: 100,
message_length_max: 4096,
caption_length_max: 1024,
};
2022-01-24 09:25:21 +01:00
public appConfig: MTAppConfig;
2020-11-15 04:33:47 +01:00
public themeColor: string;
private _themeColorElem: Element;
2020-11-15 04:33:47 +01:00
constructor() {
super();
2021-06-11 14:52:53 +02:00
this.addEventListener('peer_changed', (peerId) => {
this.peerId = peerId;
2021-12-11 17:37:08 +01:00
document.body.classList.toggle('has-chat', !!peerId);
});
2021-10-21 15:16:43 +02:00
this.addEventListener('user_auth', ({id}) => {
// @ts-ignore
this.myId = typeof(NULL_PEER_ID) === 'number' ? +id : '' + id;
2020-11-15 04:33:47 +01:00
});
2021-10-21 15:16:43 +02:00
this.addEventListener('connection_status_change', (status) => {
this.connectionStatus[status.name] = status;
2020-11-15 04:33:47 +01:00
});
2021-06-11 14:52:53 +02:00
this.addEventListener('idle', (isIDLE) => {
if(isIDLE) {
this.idle.focusPromise = new Promise((resolve) => {
this.idle.focusResolve = resolve;
});
} else {
this.idle.focusResolve();
}
});
2020-11-15 04:33:47 +01:00
}
get themeColorElem() {
if(this._themeColorElem !== undefined) {
return this._themeColorElem;
}
return this._themeColorElem = document.head.querySelector('[name="theme-color"]') as Element || null;
}
public setThemeColor(color = this.themeColor) {
if(!color) {
color = this.isNight() ? '#212121' : '#ffffff';
}
const themeColorElem = this.themeColorElem;
if(themeColorElem) {
themeColorElem.setAttribute('content', color);
}
}
public setThemeListener() {
try {
const darkModeMediaQuery = window.matchMedia('(prefers-color-scheme: dark)');
const checkDarkMode = () => {
//const theme = this.getTheme();
this.systemTheme = darkModeMediaQuery.matches ? 'night' : 'day';
//const newTheme = this.getTheme();
if(this.myId) {
2021-06-11 14:52:53 +02:00
this.dispatchEvent('theme_change');
} else {
this.setTheme();
}
};
if('addEventListener' in darkModeMediaQuery) {
darkModeMediaQuery.addEventListener('change', checkDarkMode);
} else if('addListener' in darkModeMediaQuery) {
(darkModeMediaQuery as any).addListener(checkDarkMode);
}
checkDarkMode();
} catch(err) {
}
}
public setTheme() {
const isNight = this.isNight();
2021-05-03 02:29:46 +02:00
const colorScheme = document.head.querySelector('[name="color-scheme"]');
if(colorScheme) {
colorScheme.setAttribute('content', isNight ? 'dark' : 'light');
}
document.documentElement.classList.toggle('night', isNight);
this.setThemeColor();
}
get isOverlayActive() {
return this.overlaysActive > 0;
}
set isOverlayActive(value: boolean) {
this.overlaysActive += value ? 1 : -1;
this.dispatchEvent('overlay_toggle', this.isOverlayActive);
}
public isNight() {
return this.getTheme().name === 'night';
}
public getTheme(name: Theme['name'] = this.settings.theme === 'system' ? this.systemTheme : this.settings.theme) {
return this.settings.themes.find(t => t.name === name);
}
2020-11-15 04:33:47 +01:00
}
2020-11-15 04:33:47 +01:00
const rootScope = new RootScope();
MOUNT_CLASS_TO.rootScope = rootScope;
2021-02-13 16:32:10 +01:00
export default rootScope;
2021-04-19 12:36:14 +02:00
/* rootScope.addEventListener('album_edit', (e) => {
});
rootScope.addEventListener<'album_edit'>('album_edit', (e) => {
2021-04-19 12:36:14 +02:00
}); */