2020-10-01 17:32:48 +00:00
|
|
|
package middleware
|
|
|
|
|
2022-01-19 13:31:36 +00:00
|
|
|
import tele "gopkg.in/telebot.v3"
|
2020-10-01 17:32:48 +00:00
|
|
|
|
2022-10-01 09:21:55 +00:00
|
|
|
// RestrictConfig defines config for Restrict middleware.
|
2020-10-01 17:32:48 +00:00
|
|
|
type RestrictConfig struct {
|
2022-10-01 09:21:55 +00:00
|
|
|
// Chats is a list of chats that are going to be affected
|
|
|
|
// by either In or Out function.
|
|
|
|
Chats []int64
|
|
|
|
|
|
|
|
// In defines a function that will be called if the chat
|
|
|
|
// of an update will be found in the Chats list.
|
|
|
|
In tele.HandlerFunc
|
|
|
|
|
|
|
|
// Out defines a function that will be called if the chat
|
|
|
|
// of an update will NOT be found in the Chats list.
|
|
|
|
Out tele.HandlerFunc
|
2020-10-01 17:32:48 +00:00
|
|
|
}
|
|
|
|
|
2022-10-01 09:21:55 +00:00
|
|
|
// Restrict returns a middleware that handles a list of provided
|
|
|
|
// chats with the logic defined by In and Out functions.
|
|
|
|
// If the chat is found in the Chats field, In function will be called,
|
|
|
|
// otherwise Out function will be called.
|
2020-10-01 17:32:48 +00:00
|
|
|
func Restrict(v RestrictConfig) tele.MiddlewareFunc {
|
|
|
|
return func(next tele.HandlerFunc) tele.HandlerFunc {
|
|
|
|
if v.In == nil {
|
|
|
|
v.In = next
|
|
|
|
}
|
|
|
|
if v.Out == nil {
|
|
|
|
v.Out = next
|
|
|
|
}
|
|
|
|
return func(c tele.Context) error {
|
|
|
|
for _, chat := range v.Chats {
|
2021-07-27 12:18:01 +00:00
|
|
|
if chat == c.Sender().ID {
|
2020-10-01 17:32:48 +00:00
|
|
|
return v.In(c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return v.Out(c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-01 09:21:55 +00:00
|
|
|
// Blacklist returns a middleware that skips the update for users
|
|
|
|
// specified in the chats field.
|
|
|
|
func Blacklist(chats ...int64) tele.MiddlewareFunc {
|
2020-10-01 17:32:48 +00:00
|
|
|
return func(next tele.HandlerFunc) tele.HandlerFunc {
|
|
|
|
return Restrict(RestrictConfig{
|
|
|
|
Chats: chats,
|
2022-10-01 09:21:55 +00:00
|
|
|
Out: next,
|
|
|
|
In: func(c tele.Context) error { return nil },
|
2020-10-01 17:32:48 +00:00
|
|
|
})(next)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-01 09:21:55 +00:00
|
|
|
// Whitelist returns a middleware that skips the update for users
|
|
|
|
// NOT specified in the chats field.
|
|
|
|
func Whitelist(chats ...int64) tele.MiddlewareFunc {
|
2020-10-01 17:32:48 +00:00
|
|
|
return func(next tele.HandlerFunc) tele.HandlerFunc {
|
|
|
|
return Restrict(RestrictConfig{
|
|
|
|
Chats: chats,
|
2022-10-01 09:21:55 +00:00
|
|
|
In: next,
|
|
|
|
Out: func(c tele.Context) error { return nil },
|
2020-10-01 17:32:48 +00:00
|
|
|
})(next)
|
|
|
|
}
|
|
|
|
}
|