middleware: move m appending into separate func

pull/628/head
Demian 7 months ago
parent bb76a9a642
commit 48c941a8d9

@ -171,17 +171,13 @@ var (
// Middleware usage:
//
// b.Handle("/ban", onBan, middleware.Whitelist(ids...))
//
func (b *Bot) Handle(endpoint interface{}, h HandlerFunc, m ...MiddlewareFunc) {
mw := m
if len(b.group.middleware) > 0 {
mw = make([]MiddlewareFunc, 0, len(b.group.middleware)+len(m))
mw = append(mw, b.group.middleware...)
mw = append(mw, m...)
m = appendMiddleware(b.group.middleware, m)
}
handler := func(c Context) error {
return applyMiddleware(h, mw...)(c)
return applyMiddleware(h, m...)(c)
}
switch end := endpoint.(type) {
@ -259,6 +255,7 @@ func (b *Bot) NewContext(u Update) Context {
// some Sendable (or string!) and optional send options.
//
// NOTE:
//
// Since most arguments are of type interface{}, but have pointer
// method receivers, make sure to pass them by-pointer, NOT by-value.
//
@ -268,7 +265,6 @@ func (b *Bot) NewContext(u Update) Context {
// - *ReplyMarkup (a component of SendOptions)
// - Option (a shortcut flag for popular options)
// - ParseMode (HTML, Markdown, etc)
//
func (b *Bot) Send(to Recipient, what interface{}, opts ...interface{}) (*Message, error) {
if to == nil {
return nil, ErrBadRecipient
@ -447,7 +443,6 @@ func (b *Bot) Copy(to Recipient, msg Editable, options ...interface{}) (*Message
// b.Edit(m, tele.Location{42.1337, 69.4242})
// b.Edit(c, "edit inline message from the callback")
// b.Edit(r, "edit message from chosen inline result")
//
func (b *Bot) Edit(msg Editable, what interface{}, opts ...interface{}) (*Message, error) {
var (
method string
@ -506,7 +501,6 @@ func (b *Bot) Edit(msg Editable, what interface{}, opts ...interface{}) (*Messag
//
// If edited message is sent by the bot, returns it,
// otherwise returns nil and ErrTrueResult.
//
func (b *Bot) EditReplyMarkup(msg Editable, markup *ReplyMarkup) (*Message, error) {
msgID, chatID := msg.MessageSig()
params := make(map[string]string)
@ -540,7 +534,6 @@ func (b *Bot) EditReplyMarkup(msg Editable, markup *ReplyMarkup) (*Message, erro
//
// If edited message is sent by the bot, returns it,
// otherwise returns nil and ErrTrueResult.
//
func (b *Bot) EditCaption(msg Editable, caption string, opts ...interface{}) (*Message, error) {
msgID, chatID := msg.MessageSig()
@ -576,7 +569,6 @@ func (b *Bot) EditCaption(msg Editable, caption string, opts ...interface{}) (*M
//
// b.EditMedia(m, &tele.Photo{File: tele.FromDisk("chicken.jpg")})
// b.EditMedia(m, &tele.Video{File: tele.FromURL("http://video.mp4")})
//
func (b *Bot) EditMedia(msg Editable, media Inputtable, opts ...interface{}) (*Message, error) {
var (
repr string
@ -666,7 +658,6 @@ func (b *Bot) EditMedia(msg Editable, media Inputtable, opts ...interface{}) (*M
// - If the bot is an administrator of a group, it can delete any message there.
// - If the bot has can_delete_messages permission in a supergroup or a
// channel, it can delete any message there.
//
func (b *Bot) Delete(msg Editable) error {
msgID, chatID := msg.MessageSig()
@ -688,7 +679,6 @@ func (b *Bot) Delete(msg Editable) error {
//
// Currently, Telegram supports only a narrow range of possible
// actions, these are aligned as constants of this package.
//
func (b *Bot) Notify(to Recipient, action ChatAction) error {
if to == nil {
return ErrBadRecipient
@ -711,7 +701,6 @@ func (b *Bot) Notify(to Recipient, action ChatAction) error {
// b.Ship(query) // OK
// b.Ship(query, opts...) // OK with options
// b.Ship(query, "Oops!") // Error message
//
func (b *Bot) Ship(query *ShippingQuery, what ...interface{}) error {
params := map[string]string{
"shipping_query_id": query.ID,
@ -766,7 +755,6 @@ func (b *Bot) Accept(query *PreCheckoutQuery, errorMessage ...string) error {
//
// b.Respond(c)
// b.Respond(c, response)
//
func (b *Bot) Respond(c *Callback, resp ...*CallbackResponse) error {
var r *CallbackResponse
if resp == nil {
@ -824,7 +812,6 @@ func (b *Bot) AnswerWebApp(query *Query, r Result) (*WebAppMessage, error) {
//
// Usually, Telegram-provided File objects miss FilePath so you might need to
// perform an additional request to fetch them.
//
func (b *Bot) FileByID(fileID string) (File, error) {
params := map[string]string{
"file_id": fileID,
@ -904,7 +891,6 @@ func (b *Bot) File(file *File) (io.ReadCloser, error) {
//
// If the message is sent by the bot, returns it,
// otherwise returns nil and ErrTrueResult.
//
func (b *Bot) StopLiveLocation(msg Editable, opts ...interface{}) (*Message, error) {
msgID, chatID := msg.MessageSig()
@ -929,7 +915,6 @@ func (b *Bot) StopLiveLocation(msg Editable, opts ...interface{}) (*Message, err
//
// It supports ReplyMarkup.
// This function will panic upon nil Editable.
//
func (b *Bot) StopPoll(msg Editable, opts ...interface{}) (*Poll, error) {
msgID, chatID := msg.MessageSig()
@ -969,7 +954,6 @@ func (b *Bot) Leave(chat *Chat) error {
//
// It supports Silent option.
// This function will panic upon nil Editable.
//
func (b *Bot) Pin(msg Editable, opts ...interface{}) error {
msgID, chatID := msg.MessageSig()
@ -1014,7 +998,6 @@ func (b *Bot) UnpinAll(chat *Chat) error {
//
// Including current name of the user for one-on-one conversations,
// current username of a user, group or channel, etc.
//
func (b *Bot) ChatByID(id int64) (*Chat, error) {
return b.ChatByUsername(strconv.FormatInt(id, 10))
}
@ -1114,7 +1097,6 @@ func (b *Bot) MenuButton(chat *User) (*MenuButton, error) {
//
// - MenuButtonType for simple menu buttons (default, commands)
// - MenuButton complete structure for web_app menu button type
//
func (b *Bot) SetMenuButton(chat *User, mb interface{}) error {
params := map[string]interface{}{
"chat_id": chat.Recipient(),

@ -373,7 +373,7 @@ func TestBotOnError(t *testing.T) {
assert.True(t, ok)
}
func TestBot_Middleware(t *testing.T) {
func TestBotMiddleware(t *testing.T) {
t.Run("call order", func(t *testing.T) {
var trace []string

@ -4,6 +4,15 @@ package telebot
// which get called before the endpoint group or specific handler.
type MiddlewareFunc func(HandlerFunc) HandlerFunc
func appendMiddleware(a, b []MiddlewareFunc) []MiddlewareFunc {
if len(a) == 0 {
return b
}
m := make([]MiddlewareFunc, 0, len(a)+len(b))
return append(m, append(a, b...)...)
}
func applyMiddleware(h HandlerFunc, m ...MiddlewareFunc) HandlerFunc {
for i := len(m) - 1; i >= 0; i-- {
h = m[i](h)
@ -25,11 +34,5 @@ func (g *Group) Use(middleware ...MiddlewareFunc) {
// Handle adds endpoint handler to the bot, combining group's middleware
// with the optional given middleware.
func (g *Group) Handle(endpoint interface{}, h HandlerFunc, m ...MiddlewareFunc) {
mw := m
if len(g.middleware) > 0 {
mw = make([]MiddlewareFunc, 0, len(g.middleware)+len(m))
mw = append(mw, g.middleware...)
mw = append(mw, m...)
}
g.b.Handle(endpoint, h, mw...)
g.b.Handle(endpoint, h, appendMiddleware(g.middleware, m)...)
}

Loading…
Cancel
Save