ec736f8590
i've been wanting to name a commit like this for my entire life, lol. seriously though, a lot has changed: - extracted TL-related stuff to `@mtcute/tl-utils` and `@mtcute/tl-runtime`, rewrote codegen in TS - updated to layer 134, moved to int64 identifiers - rewritten networking (mtproto), rewritten updates handling - *lots* of refactoring still a very early version though, there are a lot of improvements to be made, but at least it runs, lol also tl-reference will not be updated anytime soon because i want to rewrite it
130 lines
3.9 KiB
TypeScript
130 lines
3.9 KiB
TypeScript
import { TelegramClient } from '../client'
|
|
import { tl } from '@mtcute/tl'
|
|
import {
|
|
BotStoppedUpdate,
|
|
CallbackQuery,
|
|
ChatMemberUpdate,
|
|
ChosenInlineResult,
|
|
DeleteMessageUpdate,
|
|
HistoryReadUpdate,
|
|
InlineQuery,
|
|
Message,
|
|
ParsedUpdate, PeersIndex,
|
|
PollUpdate,
|
|
PollVoteUpdate,
|
|
UserStatusUpdate,
|
|
UserTypingUpdate,
|
|
} from '../types'
|
|
|
|
type ParserFunction = (
|
|
client: TelegramClient,
|
|
upd: tl.TypeUpdate | tl.TypeMessage,
|
|
peers: PeersIndex,
|
|
) => any
|
|
type UpdateParser = [ParsedUpdate['name'], ParserFunction]
|
|
|
|
const baseMessageParser: ParserFunction = (
|
|
client: TelegramClient,
|
|
upd,
|
|
peers
|
|
) =>
|
|
new Message(
|
|
client,
|
|
tl.isAnyMessage(upd) ? upd : (upd as any).message,
|
|
peers,
|
|
upd._ === 'updateNewScheduledMessage'
|
|
)
|
|
|
|
const newMessageParser: UpdateParser = ['new_message', baseMessageParser]
|
|
const editMessageParser: UpdateParser = ['edit_message', baseMessageParser]
|
|
const chatMemberParser: UpdateParser = [
|
|
'chat_member',
|
|
(client, upd, peers) =>
|
|
new ChatMemberUpdate(client, upd as any, peers),
|
|
]
|
|
const callbackQueryParser: UpdateParser = [
|
|
'callback_query',
|
|
(client, upd, peers) => new CallbackQuery(client, upd as any, peers),
|
|
]
|
|
const userTypingParser: UpdateParser = [
|
|
'user_typing',
|
|
(client, upd) => new UserTypingUpdate(client, upd as any),
|
|
]
|
|
const deleteMessageParser: UpdateParser = [
|
|
'delete_message',
|
|
(client, upd) => new DeleteMessageUpdate(client, upd as any),
|
|
]
|
|
const historyReadParser: UpdateParser = [
|
|
'history_read',
|
|
(client, upd) => new HistoryReadUpdate(client, upd as any),
|
|
]
|
|
|
|
const PARSERS: Partial<
|
|
Record<(tl.TypeUpdate | tl.TypeMessage)['_'], UpdateParser>
|
|
> = {
|
|
message: newMessageParser,
|
|
messageEmpty: newMessageParser,
|
|
messageService: newMessageParser,
|
|
updateNewMessage: newMessageParser,
|
|
updateNewChannelMessage: newMessageParser,
|
|
updateNewScheduledMessage: newMessageParser,
|
|
updateEditMessage: editMessageParser,
|
|
updateEditChannelMessage: editMessageParser,
|
|
updateChatParticipant: chatMemberParser,
|
|
updateChannelParticipant: chatMemberParser,
|
|
updateBotInlineQuery: [
|
|
'inline_query',
|
|
(client, upd, peers) => new InlineQuery(client, upd as any, peers),
|
|
],
|
|
updateBotInlineSend: [
|
|
'chosen_inline_result',
|
|
(client, upd, peers) =>
|
|
new ChosenInlineResult(client, upd as any, peers),
|
|
],
|
|
updateBotCallbackQuery: callbackQueryParser,
|
|
updateInlineBotCallbackQuery: callbackQueryParser,
|
|
updateMessagePoll: [
|
|
'poll',
|
|
(client, upd, peers) => new PollUpdate(client, upd as any, peers),
|
|
],
|
|
updateMessagePollVote: [
|
|
'poll_vote',
|
|
(client, upd, peers) => new PollVoteUpdate(client, upd as any, peers),
|
|
],
|
|
updateUserStatus: [
|
|
'user_status',
|
|
(client, upd) => new UserStatusUpdate(client, upd as any),
|
|
],
|
|
updateChannelUserTyping: userTypingParser,
|
|
updateChatUserTyping: userTypingParser,
|
|
updateUserTyping: userTypingParser,
|
|
updateDeleteChannelMessages: deleteMessageParser,
|
|
updateDeleteMessages: deleteMessageParser,
|
|
updateReadHistoryInbox: historyReadParser,
|
|
updateReadHistoryOutbox: historyReadParser,
|
|
updateReadChannelInbox: historyReadParser,
|
|
updateReadChannelOutbox: historyReadParser,
|
|
updateReadChannelDiscussionInbox: historyReadParser,
|
|
updateReadChannelDiscussionOutbox: historyReadParser,
|
|
updateBotStopped: [
|
|
'bot_stopped',
|
|
(client, upd, users) => new BotStoppedUpdate(client, upd as any, users),
|
|
],
|
|
}
|
|
|
|
/** @internal */
|
|
export function _parseUpdate(
|
|
client: TelegramClient,
|
|
update: tl.TypeUpdate | tl.TypeMessage,
|
|
peers: PeersIndex
|
|
): ParsedUpdate | null {
|
|
const pair = PARSERS[update._]
|
|
if (pair) {
|
|
return {
|
|
name: pair[0],
|
|
data: pair[1](client, update, peers),
|
|
}
|
|
} else {
|
|
return null
|
|
}
|
|
}
|