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';
|
2020-10-02 22:33:32 +02:00
|
|
|
|
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,
|
2023-01-13 12:10:41 +01:00
|
|
|
'chat_toggle_forum': {chatId: ChatId, enabled: boolean},
|
2023-02-27 14:11:37 +01:00
|
|
|
'chat_participant': Update.updateChannelParticipant,
|
2022-02-20 16:37:37 +01:00
|
|
|
|
|
|
|
'channel_update': ChatId,
|
2022-08-04 08:49:54 +02:00
|
|
|
|
2021-10-21 15:16:43 +02:00
|
|
|
'user_update': UserId,
|
2020-11-15 23:34:48 +01:00
|
|
|
'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,
|
2020-10-02 22:33:32 +02:00
|
|
|
|
|
|
|
'filter_delete': MyDialogFilter,
|
|
|
|
'filter_update': MyDialogFilter,
|
2021-10-21 18:40:45 +02:00
|
|
|
'filter_new': MyDialogFilter,
|
2020-11-10 01:47:00 +01:00
|
|
|
'filter_order': number[],
|
2021-10-21 15:16:43 +02:00
|
|
|
|
2022-06-17 18:01: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
|
|
|
|
2022-06-30 16:14:33 +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},
|
2022-06-30 16:14:33 +02:00
|
|
|
'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,
|
|
|
|
|
2022-06-17 18:01:43 +02:00
|
|
|
'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[]}[],
|
2020-10-02 22:33:32 +02:00
|
|
|
'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-16 04:22:58 +01:00
|
|
|
|
2020-12-22 04:02:30 +01:00
|
|
|
'replies_updated': Message.message,
|
|
|
|
|
2022-06-30 16:14:33 +02:00
|
|
|
'scheduled_new': Message.message,
|
2021-10-21 15:16:43 +02:00
|
|
|
'scheduled_delete': {peerId: PeerId, mids: number[]},
|
2020-10-02 22:33:32 +02:00
|
|
|
|
2022-06-17 18:01:43 +02:00
|
|
|
'album_edit': {peerId: PeerId, groupId: string, deletedMids: number[], messages: Message.message[]},
|
2020-11-07 18:47:54 +01:00
|
|
|
|
2020-10-02 22:33:32 +02:00
|
|
|
'stickers_installed': StickerSet.stickerSet,
|
|
|
|
'stickers_deleted': StickerSet.stickerSet,
|
2022-08-18 13:21:19 +02:00
|
|
|
'stickers_updated': {type: 'recent' | 'faved', stickers: MyDocument[]},
|
2022-11-01 18:39:23 +01:00
|
|
|
'stickers_top': Long,
|
2023-03-06 14:05:15 +01:00
|
|
|
'stickers_order': {type: 'masks' | 'emojis' | 'stickers', order: Long[]},
|
2022-08-18 13:21:19 +02:00
|
|
|
'sticker_updated': {type: 'recent' | 'faved', document: MyDocument, faved: boolean},
|
2020-10-02 22:33:32 +02: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,
|
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},
|
2020-10-02 22:33:32 +02:00
|
|
|
'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}[]},
|
2020-10-02 22:33:32 +02:00
|
|
|
|
2021-01-03 13:02:35 +01:00
|
|
|
'connection_status_change': ConnectionStatusChange,
|
2022-06-17 18:01:43 +02:00
|
|
|
'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
|
|
|
|
2021-01-31 02:52:14 +01:00
|
|
|
'background_change': void,
|
2022-08-04 08:49:54 +02:00
|
|
|
|
2021-03-08 23:15:44 +01: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
|
|
|
|
2021-04-30 16:58:00 +02:00
|
|
|
'theme_change': void,
|
2022-06-24 19:23:12 +02:00
|
|
|
|
|
|
|
'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
|
|
|
|
2022-06-17 18:01:43 +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}
|
2022-01-08 13:52:14 +01:00
|
|
|
|
2022-06-17 18:01:43 +02:00
|
|
|
'call_update': PhoneCall,
|
|
|
|
'call_signaling': {callId: CallId, data: Uint8Array},
|
2022-01-29 16:46:24 +01:00
|
|
|
|
2022-09-20 19:34:08 +02:00
|
|
|
'quick_reaction': Reaction,
|
2022-02-08 20:18:01 +01:00
|
|
|
|
2022-06-17 18:01:43 +02:00
|
|
|
'service_notification': Update.updateServiceNotification,
|
|
|
|
|
2022-07-18 15:00:41 +02:00
|
|
|
'logging_out': void,
|
|
|
|
|
2022-07-19 16:17:01 +02:00
|
|
|
'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
|
2020-10-02 22:33:32 +02:00
|
|
|
};
|
|
|
|
|
2022-06-24 19:23:12 +02:00
|
|
|
export type BroadcastEventsListeners = {
|
2021-04-24 19:06:24 +02:00
|
|
|
[name in keyof BroadcastEvents]: (e: BroadcastEvents[name]) => void
|
2022-06-24 19:23:12 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
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;
|
2022-06-17 18:01:43 +02:00
|
|
|
public premium: boolean;
|
2021-10-06 23:18:56 +02:00
|
|
|
|
2020-11-15 04:33:47 +01:00
|
|
|
constructor() {
|
2021-01-08 10:55:37 +01:00
|
|
|
super();
|
|
|
|
|
2022-08-01 11:22:44 +02:00
|
|
|
this.myId = NULL_PEER_ID;
|
|
|
|
this.connectionStatus = {};
|
2022-06-17 18:01:43 +02:00
|
|
|
this.premium = false;
|
2021-04-23 15:25:08 +02:00
|
|
|
|
2021-10-21 15:16:43 +02:00
|
|
|
this.addEventListener('user_auth', ({id}) => {
|
2022-06-17 18:01:43 +02:00
|
|
|
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;
|
2023-02-27 14:11:37 +01:00
|
|
|
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
|
|
|
});
|
2021-05-08 19:46:50 +02:00
|
|
|
|
2022-06-17 18:01:43 +02:00
|
|
|
this.dispatchEvent = (e, ...args) => {
|
|
|
|
super.dispatchEvent(e, ...args);
|
|
|
|
MTProtoMessagePort.getInstance().invokeVoid('event', {name: e as string, args});
|
|
|
|
};
|
2021-05-18 16:17:54 +02:00
|
|
|
|
2022-06-17 18:01:43 +02:00
|
|
|
if(!IS_WORKER) {
|
|
|
|
this.addEventListener('settings_updated', ({settings}) => {
|
|
|
|
this.settings = settings;
|
|
|
|
});
|
2021-05-18 16:17:54 +02:00
|
|
|
}
|
2021-04-30 16:58:00 +02:00
|
|
|
}
|
|
|
|
|
2022-06-17 18:01:43 +02:00
|
|
|
public getConnectionStatus() {
|
|
|
|
return this.connectionStatus;
|
2021-04-30 16:58:00 +02:00
|
|
|
}
|
2022-08-04 08:49:54 +02:00
|
|
|
|
2022-08-21 13:52:00 +02:00
|
|
|
public getPremium() {
|
|
|
|
return this.premium;
|
|
|
|
}
|
|
|
|
|
2022-06-17 18:01:43 +02:00
|
|
|
public dispatchEventSingle(...args: any[]) {
|
|
|
|
// @ts-ignore
|
|
|
|
super.dispatchEvent(...args);
|
2021-04-30 16:58:00 +02:00
|
|
|
}
|
2020-11-15 04:33:47 +01:00
|
|
|
}
|
2020-10-02 22:33:32 +02:00
|
|
|
|
2020-11-15 04:33:47 +01:00
|
|
|
const rootScope = new RootScope();
|
2021-03-27 16:34:08 +01:00
|
|
|
MOUNT_CLASS_TO.rootScope = rootScope;
|
2021-02-13 16:32:10 +01:00
|
|
|
export default rootScope;
|