mirror of
https://github.com/krau/SaveAny-Bot.git
synced 2026-08-25 10:09:38 +08:00
Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
21a519fdbe | ||
|
|
1d431dbc88 |
@@ -5,6 +5,7 @@ import (
|
||||
"github.com/celestix/gotgproto/ext"
|
||||
"github.com/duke-git/lancet/v2/slice"
|
||||
"github.com/krau/SaveAny-Bot/client/bot/handlers/utils/dirutil"
|
||||
"github.com/krau/SaveAny-Bot/client/bot/handlers/utils/msgelem"
|
||||
"github.com/krau/SaveAny-Bot/common/i18n"
|
||||
"github.com/krau/SaveAny-Bot/common/i18n/i18nk"
|
||||
"github.com/krau/SaveAny-Bot/config"
|
||||
@@ -12,10 +13,23 @@ import (
|
||||
"github.com/krau/SaveAny-Bot/storage"
|
||||
)
|
||||
|
||||
// responsibleUserID returns the sender's ID. Callback queries carry it
|
||||
// natively; message updates resolve it through the entity map.
|
||||
func responsibleUserID(u *ext.Update) int64 {
|
||||
if u.CallbackQuery != nil {
|
||||
return u.CallbackQuery.GetUserID()
|
||||
}
|
||||
return u.GetUserChat().GetID()
|
||||
}
|
||||
|
||||
func checkPermission(ctx *ext.Context, update *ext.Update) error {
|
||||
userID := update.GetUserChat().GetID()
|
||||
userID := responsibleUserID(update)
|
||||
if !slice.Contain(config.C().GetUsersID(), userID) {
|
||||
ctx.Reply(update, ext.ReplyTextString(i18n.T(i18nk.BotMsgCommonErrorNoPermission, nil)), nil)
|
||||
if cbq := update.CallbackQuery; cbq != nil {
|
||||
ctx.AnswerCallback(msgelem.AlertCallbackAnswer(cbq.GetQueryID(), i18n.T(i18nk.BotMsgCommonErrorNoPermission, nil)))
|
||||
} else {
|
||||
ctx.Reply(update, ext.ReplyTextString(i18n.T(i18nk.BotMsgCommonErrorNoPermission, nil)), nil)
|
||||
}
|
||||
return dispatcher.EndGroups
|
||||
}
|
||||
|
||||
|
||||
51
client/bot/handlers/middleware_test.go
Normal file
51
client/bot/handlers/middleware_test.go
Normal file
@@ -0,0 +1,51 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/celestix/gotgproto/ext"
|
||||
"github.com/celestix/gotgproto/types"
|
||||
"github.com/gotd/td/tg"
|
||||
)
|
||||
|
||||
func TestResponsibleUserID(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
update *ext.Update
|
||||
want int64
|
||||
}{
|
||||
{
|
||||
name: "callback query uses native user id",
|
||||
update: &ext.Update{CallbackQuery: &tg.UpdateBotCallbackQuery{UserID: 42}},
|
||||
want: 42,
|
||||
},
|
||||
{
|
||||
name: "message resolves through entity map",
|
||||
update: &ext.Update{
|
||||
EffectiveMessage: &types.Message{Message: &tg.Message{PeerID: &tg.PeerUser{UserID: 7}}},
|
||||
Entities: &tg.Entities{Users: map[int64]*tg.User{7: {ID: 7}}},
|
||||
},
|
||||
want: 7,
|
||||
},
|
||||
{
|
||||
name: "callback query ignores entity map",
|
||||
update: &ext.Update{
|
||||
CallbackQuery: &tg.UpdateBotCallbackQuery{UserID: 9},
|
||||
Entities: &tg.Entities{Users: map[int64]*tg.User{8: {ID: 8}}},
|
||||
},
|
||||
want: 9,
|
||||
},
|
||||
{
|
||||
name: "unresolvable update yields zero",
|
||||
update: &ext.Update{},
|
||||
want: 0,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := responsibleUserID(tt.update); got != tt.want {
|
||||
t.Fatalf("responsibleUserID() = %d, want %d", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user