From 252f94a5945edf5ff0da32264521df257495c541 Mon Sep 17 00:00:00 2001 From: teidesu <86301490+teidesu@users.noreply.github.com> Date: Fri, 6 May 2022 13:56:15 +0300 Subject: [PATCH] fix(dispatcher): implemented userId filter it got commented out when migrating to 64-bit ids, then i moved to using native numbers for them and forgot to update the filter --- packages/dispatcher/src/filters.ts | 238 +++++++++++++++-------------- 1 file changed, 120 insertions(+), 118 deletions(-) diff --git a/packages/dispatcher/src/filters.ts b/packages/dispatcher/src/filters.ts index 926bf8e2..22198c37 100644 --- a/packages/dispatcher/src/filters.ts +++ b/packages/dispatcher/src/filters.ts @@ -31,7 +31,6 @@ import { } from '@mtcute/client' import { MaybeArray } from '@mtcute/core' import { UpdateState } from './state' -import { tl } from '@mtcute/tl' function extractText( obj: Message | InlineQuery | ChosenInlineResult | CallbackQuery @@ -428,132 +427,135 @@ export namespace filters { * For chat member updates, uses `user.id` */ export const userId = ( - id: MaybeArray + id: MaybeArray ): UpdateFilter< | Message + | UserStatusUpdate + | UserTypingUpdate | InlineQuery | ChatMemberUpdate | ChosenInlineResult | CallbackQuery | PollVoteUpdate - | UserStatusUpdate - | UserTypingUpdate > => { - // TODO - return () => false + if (Array.isArray(id)) { + const index: Record = {} + let matchSelf = false + id.forEach((id) => { + if (id === 'me' || id === 'self') { + matchSelf = true + } else { + index[id] = true + } + }) - // if (Array.isArray(id)) { - // const index: Record = {} - // let matchSelf = false - // id.forEach((id) => { - // if (id === 'me' || id === 'self') { - // matchSelf = true - // } else { - // index[id] = true - // } - // }) - // - // return (upd) => { - // const ctor = upd.constructor - // - // if (ctor === Message) { - // const sender = (upd as Message).sender - // return ( - // (matchSelf && sender.isSelf) || - // sender.id in index || - // sender.username! in index - // ) - // } else { - // if ( - // ctor === UserStatusUpdate || - // ctor === UserTypingUpdate - // ) { - // const id = (upd as UserStatusUpdate | UserTypingUpdate) - // .userId - // return ( - // (matchSelf && id === upd.client['_userId']) || - // id in index - // ) - // } else { - // const user = (upd as Exclude< - // typeof upd, - // Message | UserStatusUpdate | UserTypingUpdate - // >).user - // - // return ( - // (matchSelf && user.isSelf) || - // user.id in index || - // user.username! in index - // ) - // } - // } - // } - // } - // - // if (id === 'me' || id === 'self') { - // return (upd) => { - // const ctor = upd.constructor - // - // if (ctor === Message) { - // return (upd as Message).sender.isSelf - // } else if ( - // ctor === UserStatusUpdate || - // ctor === UserTypingUpdate - // ) { - // return ( - // (upd as UserStatusUpdate | UserTypingUpdate).userId === - // upd.client['_userId'] - // ) - // } else { - // return (upd as Exclude< - // typeof upd, - // Message | UserStatusUpdate | UserTypingUpdate - // >).user.isSelf - // } - // } - // } - // - // if (typeof id === 'string') { - // return (upd) => { - // const ctor = upd.constructor - // - // if (ctor === Message) { - // return (upd as Message).sender.username === id - // } else if ( - // ctor === UserStatusUpdate || - // ctor === UserTypingUpdate - // ) { - // // username is not available - // return false - // } else { - // return ( - // (upd as Exclude< - // typeof upd, - // Message | UserStatusUpdate | UserTypingUpdate - // >).user.username === id - // ) - // } - // } - // } - // - // return (upd) => { - // const ctor = upd.constructor - // - // if (ctor === Message) { - // return (upd as Message).sender.id === id - // } else if (ctor === UserStatusUpdate || ctor === UserTypingUpdate) { - // return ( - // (upd as UserStatusUpdate | UserTypingUpdate).userId === id - // ) - // } else { - // return ( - // (upd as Exclude< - // typeof upd, - // Message | UserStatusUpdate | UserTypingUpdate - // >).user.id === id - // ) - // } - // } + return (upd) => { + const ctor = upd.constructor + + if (ctor === Message) { + const sender = (upd as Message).sender + return ( + (matchSelf && sender.isSelf) || + sender.id in index || + sender.username! in index + ) + } else if ( + ctor === UserStatusUpdate || + ctor === UserTypingUpdate + ) { + const id = (upd as UserStatusUpdate | UserTypingUpdate) + .userId + return ( + (matchSelf && id === upd.client['_userId']) || + id in index + ) + } else { + const user = ( + upd as Exclude< + typeof upd, + Message | UserStatusUpdate | UserTypingUpdate + > + ).user + + return ( + (matchSelf && user.isSelf) || + user.id in index || + user.username! in index + ) + } + } + } + + if (id === 'me' || id === 'self') { + return (upd) => { + const ctor = upd.constructor + + if (ctor === Message) { + return (upd as Message).sender.isSelf + } else if ( + ctor === UserStatusUpdate || + ctor === UserTypingUpdate + ) { + return ( + (upd as UserStatusUpdate | UserTypingUpdate).userId === + upd.client['_userId'] + ) + } else { + return ( + upd as Exclude< + typeof upd, + Message | UserStatusUpdate | UserTypingUpdate + > + ).user.isSelf + } + } + } + + if (typeof id === 'string') { + return (upd) => { + const ctor = upd.constructor + + if (ctor === Message) { + return (upd as Message).sender.username === id + } else if ( + ctor === UserStatusUpdate || + ctor === UserTypingUpdate + ) { + // username is not available + return false + } else { + return ( + ( + upd as Exclude< + typeof upd, + Message | UserStatusUpdate | UserTypingUpdate + > + ).user.username === id + ) + } + } + } + + return (upd) => { + const ctor = upd.constructor + + if (ctor === Message) { + return (upd as Message).sender.id === id + } else if (ctor === UserStatusUpdate || ctor === UserTypingUpdate) { + return ( + (upd as UserStatusUpdate | UserTypingUpdate).userId === id + ) + } else { + return ( + ( + upd as Exclude< + typeof upd, + Message | UserStatusUpdate | UserTypingUpdate + > + ).user.id === id + ) + } + } } /**