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
|
|
|
|
|
|
|
type RestrictConfig struct {
|
2021-07-27 12:18:01 +00:00
|
|
|
Chats []int64
|
2020-10-01 17:32:48 +00:00
|
|
|
In, Out tele.HandlerFunc
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-27 12:18:01 +00:00
|
|
|
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,
|
|
|
|
In: next,
|
2020-10-20 10:30:47 +00:00
|
|
|
Out: func(c tele.Context) error { return nil },
|
2020-10-01 17:32:48 +00:00
|
|
|
})(next)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-27 12:18:01 +00:00
|
|
|
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,
|
|
|
|
Out: next,
|
2020-10-20 10:30:47 +00:00
|
|
|
In: func(c tele.Context) error { return nil },
|
2020-10-01 17:32:48 +00:00
|
|
|
})(next)
|
|
|
|
}
|
|
|
|
}
|