grammyjs / nestjs

grammY integration for NestJS.
42 stars 11 forks source link

Feature Request: @Filter decorator #30

Closed ByMsx closed 1 year ago

ByMsx commented 1 year ago

Hello!

I'm using this library in my project and I need to handle a 'message' event from private and group chats with different behaviour. In original Grammy I can use bot.filter(chat => chat.type === 'private').on(...) and bot.filter(chat => chat.type !== 'private').on(...), but I can't do it here without if in handler code. For example:

  @On('message')
  // @UseGuards(AuthorizedUserGuard) -- this guard will be removed, because it's only for private chats
  messageHandler(@GetContext() ctx: MyContext) {
    if (ctx.chat.type === 'private') {
      // here is behaviour for private chats and access checking without guard (bad pattern)
    } else {
      // here is an other behaviour
    }
  }

And I want to do something like:

  @On('message')
  @Filter((ctx) => ctx.chat.type === 'private')
  @UseGuards(AuthorizedUserGuard)
  createPublicationDraft(@GetContext() ctx: MyContext) {
    // behaviour for private chats
  }

  @On('message')
  @Filter((ctx) => ctx.chat.type !== 'private')
  handleMessage(@GetContext() ctx: MyContext) {
    // other behaviour
  }

What do you think about this idea? I'm ready to submit PR if you approve my idea.

P.S. Sorry for my bad English.

KnorpelSenf commented 1 year ago

I think this is a really cool idea and it's pretty awesome to see that you already implemented it! I'm curious what the maintainers @drmikecrowe and @solidprinciples will think :)