feat(dispatcher): history read update

also fixed imports/exports
This commit is contained in:
teidesu 2021-07-09 19:41:02 +03:00
parent 817eb3aa8d
commit 77d597e4db
6 changed files with 246 additions and 21 deletions

View file

@ -12,3 +12,4 @@ poll: PollUpdate = PollUpdate
poll_vote = PollVoteUpdate poll_vote = PollVoteUpdate
user_status: UserStatusUpdate = UserStatusUpdate user_status: UserStatusUpdate = UserStatusUpdate
user_typing = UserTypingUpdate user_typing = UserTypingUpdate
history_read = HistoryReadUpdate

View file

@ -14,17 +14,21 @@ import {
PollVoteHandler, PollVoteHandler,
UserStatusUpdateHandler, UserStatusUpdateHandler,
UserTypingHandler, UserTypingHandler,
HistoryReadHandler,
} from './handler' } from './handler'
// end-codegen-imports // end-codegen-imports
import { filters, UpdateFilter } from './filters' import { filters, UpdateFilter } from './filters'
import { CallbackQuery, InlineQuery, Message } from '@mtcute/client' import { CallbackQuery, InlineQuery, Message } from '@mtcute/client'
import { ChatMemberUpdate } from './updates' import {
import { ChosenInlineResult } from './updates/chosen-inline-result' ChatMemberUpdate,
import { PollUpdate } from './updates/poll-update' ChosenInlineResult,
import { PollVoteUpdate } from './updates/poll-vote' PollUpdate,
import { UserStatusUpdate } from './updates/user-status-update' PollVoteUpdate,
import { UserTypingUpdate } from './updates/user-typing-update' UserStatusUpdate,
import { DeleteMessageUpdate } from './updates/delete-message-update' UserTypingUpdate,
DeleteMessageUpdate,
HistoryReadUpdate,
} from './updates'
function _create<T extends UpdateHandler>( function _create<T extends UpdateHandler>(
type: T['type'], type: T['type'],
@ -385,5 +389,35 @@ export namespace handlers {
return _create('user_typing', filter, handler) return _create('user_typing', filter, handler)
} }
/**
* Create a history read handler
*
* @param handler History read handler
*/
export function historyRead(
handler: HistoryReadHandler['callback']
): HistoryReadHandler
/**
* Create a history read handler with a filter
*
* @param filter Update filter
* @param handler History read handler
*/
export function historyRead<Mod>(
filter: UpdateFilter<HistoryReadUpdate, Mod>,
handler: HistoryReadHandler<
filters.Modify<HistoryReadUpdate, Mod>
>['callback']
): HistoryReadHandler
/** @internal */
export function historyRead(
filter: any,
handler?: any
): HistoryReadHandler {
return _create('history_read', filter, handler)
}
// end-codegen // end-codegen
} }

View file

@ -25,18 +25,22 @@ import {
PollVoteHandler, PollVoteHandler,
UserStatusUpdateHandler, UserStatusUpdateHandler,
UserTypingHandler, UserTypingHandler,
HistoryReadHandler,
} from './handler' } from './handler'
// end-codegen-imports // end-codegen-imports
import { UpdateInfo } from './handler' import { UpdateInfo } from './handler'
import { filters, UpdateFilter } from './filters' import { filters, UpdateFilter } from './filters'
import { handlers } from './builders' import { handlers } from './builders'
import { ChatMemberUpdate } from './updates' import {
import { ChosenInlineResult } from './updates/chosen-inline-result' ChatMemberUpdate,
import { PollUpdate } from './updates/poll-update' ChosenInlineResult,
import { PollVoteUpdate } from './updates/poll-vote' PollUpdate,
import { UserStatusUpdate } from './updates/user-status-update' PollVoteUpdate,
import { UserTypingUpdate } from './updates/user-typing-update' UserStatusUpdate,
import { DeleteMessageUpdate } from './updates/delete-message-update' UserTypingUpdate,
DeleteMessageUpdate,
HistoryReadUpdate,
} from './updates'
import { IStateStorage, UpdateState, StateKeyDelegate } from './state' import { IStateStorage, UpdateState, StateKeyDelegate } from './state'
import { defaultStateKeyDelegate } from './state' import { defaultStateKeyDelegate } from './state'
import { PropagationAction } from './propagation' import { PropagationAction } from './propagation'
@ -85,6 +89,10 @@ const deleteMessageParser: UpdateParser = [
'delete_message', 'delete_message',
(client, upd) => new DeleteMessageUpdate(client, upd as any), (client, upd) => new DeleteMessageUpdate(client, upd as any),
] ]
const historyReadParser: UpdateParser = [
'history_read',
(client, upd) => new HistoryReadUpdate(client, upd as any),
]
const PARSERS: Partial< const PARSERS: Partial<
Record<(tl.TypeUpdate | tl.TypeMessage)['_'], UpdateParser> Record<(tl.TypeUpdate | tl.TypeMessage)['_'], UpdateParser>
@ -127,6 +135,12 @@ const PARSERS: Partial<
updateUserTyping: userTypingParser, updateUserTyping: userTypingParser,
updateDeleteChannelMessages: deleteMessageParser, updateDeleteChannelMessages: deleteMessageParser,
updateDeleteMessages: deleteMessageParser, updateDeleteMessages: deleteMessageParser,
updateReadHistoryInbox: historyReadParser,
updateReadHistoryOutbox: historyReadParser,
updateReadChannelInbox: historyReadParser,
updateReadChannelOutbox: historyReadParser,
updateReadChannelDiscussionInbox: historyReadParser,
updateReadChannelDiscussionOutbox: historyReadParser,
} }
const HANDLER_TYPE_TO_UPDATE: Record<string, string[]> = {} const HANDLER_TYPE_TO_UPDATE: Record<string, string[]> = {}
@ -138,7 +152,9 @@ Object.keys(PARSERS).forEach((upd: keyof typeof PARSERS) => {
}) })
export declare interface Dispatcher< export declare interface Dispatcher<
// eslint-disable-next-line @typescript-eslint/no-unused-vars
State = never, State = never,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
SceneName extends string = string SceneName extends string = string
> { > {
on<T = {}>( on<T = {}>(
@ -247,6 +263,14 @@ export declare interface Dispatcher<
*/ */
on(name: 'user_typing', handler: UserTypingHandler['callback']): this on(name: 'user_typing', handler: UserTypingHandler['callback']): this
/**
* Register a plain old history read handler
*
* @param name Event name
* @param handler History read handler
*/
on(name: 'history_read', handler: HistoryReadHandler['callback']): this
// end-codegen-declare // end-codegen-declare
} }
@ -1674,5 +1698,33 @@ export class Dispatcher<
this._addKnownHandler('userTyping', filter, handler, group) this._addKnownHandler('userTyping', filter, handler, group)
} }
/**
* Register a history read handler without any filters
*
* @param handler History read handler
* @param group Handler group index
*/
onHistoryRead(handler: HistoryReadHandler['callback'], group?: number): void
/**
* Register a history read handler with a filter
*
* @param filter Update filter
* @param handler History read handler
* @param group Handler group index
*/
onHistoryRead<Mod>(
filter: UpdateFilter<HistoryReadUpdate, Mod>,
handler: HistoryReadHandler<
filters.Modify<HistoryReadUpdate, Mod>
>['callback'],
group?: number
): void
/** @internal */
onHistoryRead(filter: any, handler?: any, group?: number): void {
this._addKnownHandler('historyRead', filter, handler, group)
}
// end-codegen // end-codegen
} }

View file

@ -9,13 +9,16 @@ import {
} from '@mtcute/client' } from '@mtcute/client'
import { tl } from '@mtcute/tl' import { tl } from '@mtcute/tl'
import { PropagationAction } from './propagation' import { PropagationAction } from './propagation'
import { ChatMemberUpdate } from './updates' import {
import { ChosenInlineResult } from './updates/chosen-inline-result' ChatMemberUpdate,
import { PollUpdate } from './updates/poll-update' ChosenInlineResult,
import { PollVoteUpdate } from './updates/poll-vote' PollUpdate,
import { UserStatusUpdate } from './updates/user-status-update' PollVoteUpdate,
import { UserTypingUpdate } from './updates/user-typing-update' UserStatusUpdate,
import { DeleteMessageUpdate } from './updates/delete-message-update' UserTypingUpdate,
DeleteMessageUpdate,
HistoryReadUpdate,
} from './updates'
interface BaseUpdateHandler<Type, Handler, Checker> { interface BaseUpdateHandler<Type, Handler, Checker> {
type: Type type: Type
@ -96,6 +99,10 @@ export type UserTypingHandler<T = UserTypingUpdate> = ParsedUpdateHandler<
'user_typing', 'user_typing',
T T
> >
export type HistoryReadHandler<T = HistoryReadUpdate> = ParsedUpdateHandler<
'history_read',
T
>
export type UpdateHandler = export type UpdateHandler =
| RawUpdateHandler | RawUpdateHandler
@ -110,5 +117,6 @@ export type UpdateHandler =
| PollVoteHandler | PollVoteHandler
| UserStatusUpdateHandler | UserStatusUpdateHandler
| UserTypingHandler | UserTypingHandler
| HistoryReadHandler
// end-codegen // end-codegen

View file

@ -0,0 +1,123 @@
import { tl } from '@mtcute/tl'
import { TelegramClient } from '@mtcute/client'
import { getMarkedPeerId, MAX_CHANNEL_ID } from '@mtcute/core'
import { makeInspectable } from '@mtcute/client/src/types/utils'
export class HistoryReadUpdate {
constructor (
readonly client: TelegramClient,
readonly raw:
| tl.RawUpdateReadHistoryInbox
| tl.RawUpdateReadHistoryOutbox
| tl.RawUpdateReadChannelInbox
| tl.RawUpdateReadChannelOutbox
| tl.RawUpdateReadChannelDiscussionInbox
| tl.RawUpdateReadChannelDiscussionOutbox
) {}
/**
* Whether this update is about an "outbox" read
* (i.e. a message you have sent earlier was read)
*/
get isOutbox(): boolean {
switch (this.raw._) {
case 'updateReadChannelDiscussionOutbox':
case 'updateReadHistoryOutbox':
case 'updateReadChannelOutbox':
return true
default:
return false
}
}
/**
* Whether this update is about messages in a discussion group
* (e.g. from a comments thread)
*/
get isDiscussion(): boolean {
switch (this.raw._) {
case 'updateReadChannelDiscussionOutbox':
case 'updateReadChannelDiscussionInbox':
return true
default:
return false
}
}
/**
* Marked peer ID of the chat where the messages were read.
*
* If `isDiscussion == true`, contains the discussion group.
*/
get chatId(): number {
switch (this.raw._) {
case 'updateReadHistoryOutbox':
case 'updateReadHistoryInbox':
return getMarkedPeerId(this.raw.peer)
case 'updateReadChannelOutbox':
case 'updateReadChannelInbox':
case 'updateReadChannelDiscussionOutbox':
case 'updateReadChannelDiscussionInbox':
return MAX_CHANNEL_ID - this.raw.channelId
}
}
/**
* For inbox updates (i.e. `isOutbox = false`),
* number of messages that are still unread in the chat.
*
* For other updates, `0`
*/
get unreadCount(): number {
switch (this.raw._) {
case 'updateReadHistoryInbox':
case 'updateReadChannelInbox':
return this.raw.stillUnreadCount
// `updateReadChannelDiscussionInbox` does not contain that data for some reason
case 'updateReadChannelDiscussionInbox':
case 'updateReadHistoryOutbox':
case 'updateReadChannelOutbox':
case 'updateReadChannelDiscussionOutbox':
return 0
}
}
/**
* ID of the last read message.
*
* Note that if `isDiscussion == true`, this contains the ID of the
* last read message inside that thread, and not in the group itself.
*/
get maxReadId(): number {
switch (this.raw._) {
case 'updateReadHistoryOutbox':
case 'updateReadHistoryInbox':
case 'updateReadChannelOutbox':
case 'updateReadChannelInbox':
return this.raw.maxId
case 'updateReadChannelDiscussionOutbox':
case 'updateReadChannelDiscussionInbox':
return this.raw.readMaxId
}
}
/**
* ID of the thread (i.e. ID of the top message).
*
* For non-discussion updates, 0.
*/
get threadId(): number {
switch (this.raw._) {
case 'updateReadHistoryOutbox':
case 'updateReadHistoryInbox':
case 'updateReadChannelOutbox':
case 'updateReadChannelInbox':
return 0
case 'updateReadChannelDiscussionOutbox':
case 'updateReadChannelDiscussionInbox':
return this.raw.topMsgId
}
}
}
makeInspectable(HistoryReadUpdate)

View file

@ -1 +1,8 @@
export * from './chat-member-update' export * from './chat-member-update'
export * from './chosen-inline-result'
export * from './delete-message-update'
export * from './poll-update'
export * from './poll-vote'
export * from './user-status-update'
export * from './user-typing-update'
export * from './history-read-update'