tweb/src/lib/rootScope.ts

220 lines
7.9 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-09-20 19:34:08 +02:00
import type {Message, StickerSet, Update, NotifyPeer, PeerNotifySettings, PollResults, Poll, WebPage, GroupCall, GroupCallParticipant, ReactionCount, MessagePeerReaction, PhoneCall, Config, Reaction} from '../layer';
2023-01-06 20:27:29 +01:00
import type {Dialog, ForumTopic, MessagesStorageKey, MyMessage} from './appManagers/appMessagesManager';
2022-08-04 08:49:54 +02:00
import type {MyDialogFilter} from './storages/filters';
import type {Folder} from './storages/dialogs';
import type {UserTyping} from './appManagers/appProfileManager';
import type {MyDraftMessage} from './appManagers/appDraftsManager';
import type {ConnectionStatusChange} from './mtproto/connectionStatus';
import type {GroupCallId} from './appManagers/appGroupCallsManager';
import type {AppManagers} from './appManagers/managers';
import type {State} from '../config/state';
import type {Progress} from './appManagers/appDownloadManager';
import type {CallId} from './appManagers/appCallsManager';
2022-08-09 17:35:11 +02:00
import type {MyDocument} from './appManagers/appDocsManager';
2023-01-06 20:27:29 +01:00
import type {MTAppConfig} from './mtproto/appConfig';
2022-08-04 08:49:54 +02:00
import {NULL_PEER_ID, UserAuth} from './mtproto/mtproto_config';
import EventListenerBase from '../helpers/eventListenerBase';
import {MOUNT_CLASS_TO} from '../config/debug';
import MTProtoMessagePort from './mtproto/mtprotoMessagePort';
import {IS_WORKER} from '../helpers/context';
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,
'chat_toggle_forum': {chatId: ChatId, enabled: boolean},
'chat_participant': Update.updateChannelParticipant,
'channel_update': ChatId,
2022-08-04 08:49:54 +02: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,
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},
2023-01-06 20:27:29 +01:00
'peer_typings': {peerId: PeerId, threadId?: number, typings: UserTyping[]},
2021-10-21 15:16:43 +02:00
'peer_block': {peerId: PeerId, blocked: boolean},
2023-01-06 20:27:29 +01:00
'peer_title_edit': {peerId: PeerId, threadId?: number},
2021-10-21 15:16:43 +02:00
'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': Omit<Folder, 'dialogs' | 'dispatchUnreadTimeout'>,
2022-08-04 08:49:54 +02:00
2023-01-06 20:27:29 +01:00
'dialog_draft': {peerId: PeerId, dialog: Dialog | ForumTopic, drop: boolean, draft: MyDraftMessage | undefined},
'dialog_unread': {peerId: PeerId, dialog: Dialog | ForumTopic},
2022-04-25 16:54:30 +02:00
'dialog_flush': {peerId: PeerId, dialog: Dialog},
2023-01-06 20:27:29 +01:00
'dialog_drop': Dialog | ForumTopic,
2021-10-21 15:16:43 +02:00
'dialog_migrate': {migrateFrom: PeerId, migrateTo: PeerId},
2022-08-04 08:49:54 +02:00
// 'dialog_top': Dialog,
2023-01-06 20:27:29 +01:00
'dialog_notify_settings': Dialog | ForumTopic,
2021-09-17 18:50:29 +02:00
// 'dialog_order': {dialog: Dialog, pos: number},
2023-01-06 20:27:29 +01:00
'dialogs_multiupdate': Map<PeerId, {dialog?: Dialog, topics?: Map<number, ForumTopic>}>,
2022-08-04 08:49:54 +02:00
'history_append': {storageKey: MessagesStorageKey, message: Message.message},
'history_update': {storageKey: MessagesStorageKey, message: MyMessage, sequential?: boolean},
2021-10-21 15:16:43 +02:00
'history_reply_markup': {peerId: PeerId},
'history_multiappend': MyMessage,
2021-10-21 15:16:43 +02:00
'history_delete': {peerId: PeerId, msgs: Set<number>},
'history_forbidden': PeerId,
'history_reload': PeerId,
2022-08-04 08:49:54 +02:00
// 'history_request': void,
'message_edit': {storageKey: MessagesStorageKey, peerId: PeerId, mid: number, message: MyMessage},
'message_sent': {storageKey: MessagesStorageKey, tempId: number, tempMessage: any, mid: number, message: MyMessage},
2022-09-02 19:43:54 +02:00
'message_error': {storageKey: MessagesStorageKey, tempId: number, error: ApiError},
2023-02-20 20:39:43 +01:00
'message_transcribed': {peerId: PeerId, mid: number, text: string, pending?: boolean},
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,
'scheduled_new': Message.message,
2021-10-21 15:16:43 +02:00
'scheduled_delete': {peerId: PeerId, mids: number[]},
'album_edit': {peerId: PeerId, groupId: string, deletedMids: number[], messages: Message.message[]},
'stickers_installed': StickerSet.stickerSet,
'stickers_deleted': StickerSet.stickerSet,
'stickers_updated': {type: 'recent' | 'faved', stickers: MyDocument[]},
2022-11-01 18:39:23 +01:00
'stickers_top': Long,
'stickers_order': {type: 'masks' | 'emojis' | 'stickers', order: Long[]},
'sticker_updated': {type: 'recent' | 'faved', document: MyDocument, faved: boolean},
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,
2022-08-04 08:49:54 +02:00
2021-10-21 15:16:43 +02:00
'contacts_update': UserId,
2023-01-06 20:27:29 +01:00
'avatar_update': {peerId: PeerId, threadId?: number},
'poll_update': {poll: Poll, results: PollResults},
2021-10-21 15:16:43 +02:00
'invalidate_participants': ChatId,
2022-08-04 08:49:54 +02:00
// '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, settings: State['settings']},
2021-10-21 15:16:43 +02:00
'draft_updated': {peerId: PeerId, threadId: number, draft: MyDraftMessage | undefined, force?: boolean},
2022-08-04 08:49:54 +02:00
'background_change': void,
2022-08-04 08:49:54 +02:00
'privacy_update': Update.updatePrivacy,
2022-08-04 08:49:54 +02:00
2021-03-11 04:06:44 +01:00
'notify_settings': Update.updateNotifySettings,
'notify_peer_type_settings': {key: Exclude<NotifyPeer['_'], 'notifyPeer'>, settings: PeerNotifySettings},
2022-04-25 16:54:30 +02:00
'notification_reset': string,
'notification_cancel': string,
2022-08-04 08:49:54 +02:00
2021-06-14 20:13:31 +02:00
'language_change': string,
2022-08-04 08:49:54 +02:00
'theme_change': void,
'media_play': void,
2022-08-04 08:49:54 +02:00
2022-11-01 18:39:23 +01:00
'emoji_recent': AppEmoji,
2022-08-04 08:49:54 +02:00
'download_progress': Progress,
'document_downloading': DocId,
'document_downloaded': DocId,
2021-08-25 00:19:05 +02:00
2021-11-29 16:24:29 +01:00
'choosing_sticker': boolean
2021-12-11 17:37:08 +01:00
'group_call_update': GroupCall,
'group_call_participant': {groupCallId: GroupCallId, participant: GroupCallParticipant},
// 'group_call_video_track_added': {instance: GroupCallInstance}
'call_update': PhoneCall,
'call_signaling': {callId: CallId, data: Uint8Array},
2022-09-20 19:34:08 +02:00
'quick_reaction': Reaction,
2022-02-08 20:18:01 +01:00
'service_notification': Update.updateServiceNotification,
2022-07-18 15:00:41 +02:00
'logging_out': void,
'payment_sent': {peerId: PeerId, mid: number, receiptMessage: Message.messageService}
2022-07-26 23:50:05 +02:00
2022-08-01 11:22:44 +02:00
'premium_toggle': boolean,
2023-01-06 20:27:29 +01:00
'premium_toggle_private': {isNew: boolean, isPremium: boolean},
2022-08-01 11:22:44 +02:00
2022-08-09 16:41:06 +02:00
'config': Config,
2022-08-01 11:22:44 +02:00
'app_config': MTAppConfig
};
export type BroadcastEventsListeners = {
2021-04-24 19:06:24 +02:00
[name in keyof BroadcastEvents]: (e: BroadcastEvents[name]) => void
};
export class RootScope extends EventListenerBase<BroadcastEventsListeners> {
2022-08-01 11:22:44 +02:00
public myId: PeerId;
private connectionStatus: {[name: string]: ConnectionStatusChange};
2021-01-03 13:02:35 +01:00
public settings: State['settings'];
2022-04-25 16:54:30 +02:00
public managers: AppManagers;
public premium: boolean;
2020-11-15 04:33:47 +01:00
constructor() {
super();
2022-08-01 11:22:44 +02:00
this.myId = NULL_PEER_ID;
this.connectionStatus = {};
this.premium = false;
2021-10-21 15:16:43 +02:00
this.addEventListener('user_auth', ({id}) => {
this.myId = id.toPeerId();
2020-11-15 04:33:47 +01:00
});
2023-01-06 20:27:29 +01:00
this.addEventListener('premium_toggle_private', ({isNew, isPremium}) => {
2022-08-01 11:22:44 +02:00
this.premium = isPremium;
this.dispatchEventSingle('premium_toggle', isPremium);
2022-08-01 11:22:44 +02: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
});
this.dispatchEvent = (e, ...args) => {
super.dispatchEvent(e, ...args);
MTProtoMessagePort.getInstance().invokeVoid('event', {name: e as string, args});
};
if(!IS_WORKER) {
this.addEventListener('settings_updated', ({settings}) => {
this.settings = settings;
});
}
}
public getConnectionStatus() {
return this.connectionStatus;
}
2022-08-04 08:49:54 +02:00
public getPremium() {
return this.premium;
}
public dispatchEventSingle(...args: any[]) {
// @ts-ignore
super.dispatchEvent(...args);
}
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;