Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update callbackquery.go #141

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 16 additions & 11 deletions ext/handlers/callbackquery.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package handlers

import (
"fmt"
"log"

"github.com/PaulSonOfLars/gotgbot/v2"
"github.com/PaulSonOfLars/gotgbot/v2/ext"
Expand All @@ -11,27 +12,31 @@ import (
type CallbackQuery struct {
AllowChannel bool
Filter filters.CallbackQuery
Response Response
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the reasoning behind this change?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the signature (b *gotgbot.Bot, ctx *ext.Context) error, allowing for greater versatility in defining response behavior.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is the same signature as the existing Response type... This is a noop and completely unnecessary change.
The definition is already: type Response func(b *gotgbot.Bot, ctx *ext.Context) error

Response func(b *gotgbot.Bot, ctx *ext.Context) error
}

func NewCallback(filter filters.CallbackQuery, r Response) CallbackQuery {
return CallbackQuery{
func NewCallback(filter filters.CallbackQuery, response func(b *gotgbot.Bot, ctx *ext.Context) error) *CallbackQuery {
return &CallbackQuery{
Filter: filter,
Response: r,
Comment on lines -17 to -20
Copy link
Owner

@PaulSonOfLars PaulSonOfLars Feb 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please avoid unnecessary changes like this one (especially given there is no explanation provided)

Response: response,
}
}

// SetAllowChannel Enables channel messages for this handler.
func (cb CallbackQuery) SetAllowChannel(allow bool) CallbackQuery {
// SetAllowChannel enables channel messages for this handler.
func (cb *CallbackQuery) SetAllowChannel(allow bool) *CallbackQuery {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we changing to a pointer reference here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changing the receiver from CallbackQuery to *CallbackQuery in the SetAllowChannel method allows the method to modify the CallbackQuery struct directly, rather than working with a copy of the struct.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand the function of the change, but this doesnt explain the why.

Currently, SetAllowChannel copies the receiver and returns it, allowing for immutable chaining of methods. Changing it to be a pointer would break the immutability and introduce side effects to the method, which would then be inconsistent with all other handlers.

cb.AllowChannel = allow
return cb
}

func (cb CallbackQuery) HandleUpdate(b *gotgbot.Bot, ctx *ext.Context) error {
return cb.Response(b, ctx)
func (cb *CallbackQuery) HandleUpdate(b *gotgbot.Bot, ctx *ext.Context) error {
err := cb.Response(b, ctx)
if err != nil {
log.Printf("Error handling callback query: %v", err)
}
Comment on lines +32 to +35
Copy link
Owner

@PaulSonOfLars PaulSonOfLars Feb 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why should the callbackquery handler log errors, when none of the others do?

I'm not a fan of this pattern - I think people should add an error handler to the dispatcher, to ensure all errors are logged properly. Alternatively, they can also wrap the callbackquery handler to log just errors if necessary.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's indeed a matter of design preference and code organization. Logging errors within the callback query handler can clutter the handler's logic and may not be the best approach for all situations.

Copy link
Owner

@PaulSonOfLars PaulSonOfLars Feb 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not a matter of "preference", it is a matter of consistency and configurability.

This change introduces a new logging paradigm which is inconsistent with all other handlers. Additionally, it cannot be configured (eg, to use JSON logging over text logging, to silence certain errors but allow others, or even to assign warn/error levels). All of these things are already possible when using the dispatcher-level error handling logic.

Ultimately, these are large changes which should be discussed over an issue, or in the support group, before being opened in a PR without any context. I don't like having to close PRs that people have worked on without discussion, because I hate to see work go to waste - so please make sure to discuss these things first.

return err
}

func (cb CallbackQuery) CheckUpdate(b *gotgbot.Bot, ctx *ext.Context) bool {
func (cb *CallbackQuery) CheckUpdate(b *gotgbot.Bot, ctx *ext.Context) bool {
if ctx.CallbackQuery == nil {
return false
}
Expand All @@ -43,6 +48,6 @@ func (cb CallbackQuery) CheckUpdate(b *gotgbot.Bot, ctx *ext.Context) bool {
return cb.Filter == nil || cb.Filter(ctx.CallbackQuery)
}

func (cb CallbackQuery) Name() string {
return fmt.Sprintf("inlinequery_%p", cb.Response)
func (cb *CallbackQuery) Name() string {
return fmt.Sprintf("callback_query_handler_%p", cb.Response)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch here, thanks for finding it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok i will pr this seperate by in the evening due to iam in college now

}