From f9af26ee8f141fd2844baab31823a7700698e322 Mon Sep 17 00:00:00 2001 From: indes Date: Fri, 25 Feb 2022 21:46:00 +0800 Subject: [PATCH] feat: set tag button handler --- internal/bot/bot.go | 2 +- internal/bot/controller.go | 25 ------- .../handler/set_subscription_tag_button.go | 75 +++++++++++++++++++ 3 files changed, 76 insertions(+), 26 deletions(-) create mode 100644 internal/bot/handler/set_subscription_tag_button.go diff --git a/internal/bot/bot.go b/internal/bot/bot.go index 7dbd5b80..9a4f4c6f 100644 --- a/internal/bot/bot.go +++ b/internal/bot/bot.go @@ -104,6 +104,7 @@ func setCommands() { handler.NewSetFeedItemButton(B), handler.NewRemoveSubscriptionItemButton(), handler.NewNotificationSwitchButton(B), + handler.NewSetSubscriptionTagButton(B), } for _, h := range ButtonHandlers { @@ -124,5 +125,4 @@ func setCommands() { B.Handle(&tb.InlineButton{Unique: "set_toggle_telegraph_btn"}, setToggleTelegraphBtnCtr) B.Handle(&tb.InlineButton{Unique: "set_toggle_update_btn"}, setToggleUpdateBtnCtr) - B.Handle(&tb.InlineButton{Unique: "set_set_sub_tag_btn"}, setSubTagBtnCtr) } diff --git a/internal/bot/controller.go b/internal/bot/controller.go index 7927410b..6c113236 100644 --- a/internal/bot/controller.go +++ b/internal/bot/controller.go @@ -2,7 +2,6 @@ package bot import ( "bytes" - "fmt" "strconv" "strings" "text/template" @@ -133,30 +132,6 @@ func genFeedSetBtn( return feedSettingKeys } -func setSubTagBtnCtr(ctx tb.Context) error { - c := ctx.Callback() - // 权限验证 - if !feedSetAuth(c) { - return ctx.Send("无权限") - } - data := strings.Split(c.Data, ":") - ownID, _ := strconv.Atoi(data[0]) - sourceID, _ := strconv.Atoi(data[1]) - - sub, err := model.GetSubscribeByUserIDAndSourceID(int64(ownID), uint(sourceID)) - if err != nil { - return ctx.Send("系统错误,代码04") - } - msg := fmt.Sprintf( - "请使用`/setfeedtag %d tags`命令为该订阅设置标签,tags为需要设置的标签,以空格分隔。(最多设置三个标签) \n"+ - "例如:`/setfeedtag %d 科技 苹果`", - sub.ID, sub.ID, - ) - - _ = B.Delete(c.Message) - return ctx.Send(msg, &tb.SendOptions{ParseMode: tb.ModeMarkdown}) -} - func setToggleTelegraphBtnCtr(ctx tb.Context) error { return toggleCtrlButtons(ctx, "toggleTelegraph") } diff --git a/internal/bot/handler/set_subscription_tag_button.go b/internal/bot/handler/set_subscription_tag_button.go new file mode 100644 index 00000000..c0ff89c9 --- /dev/null +++ b/internal/bot/handler/set_subscription_tag_button.go @@ -0,0 +1,75 @@ +package handler + +import ( + "fmt" + "strconv" + "strings" + + tb "gopkg.in/telebot.v3" + + "github.com/indes/flowerss-bot/internal/bot/chat" + "github.com/indes/flowerss-bot/internal/model" +) + +const ( + SetSubscriptionTagButtonUnique = "set_set_sub_tag_btn" +) + +type SetSubscriptionTagButton struct { + bot *tb.Bot +} + +func NewSetSubscriptionTagButton(bot *tb.Bot) *SetSubscriptionTagButton { + return &SetSubscriptionTagButton{bot: bot} +} + +func (b *SetSubscriptionTagButton) CallbackUnique() string { + return "\f" + SetSubscriptionTagButtonUnique +} + +func (b *SetSubscriptionTagButton) Description() string { + return "" +} + +func (b *SetSubscriptionTagButton) feedSetAuth(c *tb.Callback) bool { + data := strings.Split(c.Data, ":") + subscriberID, _ := strconv.ParseInt(data[0], 10, 64) + // 如果订阅者与按钮点击者id不一致,需要验证管理员权限 + if subscriberID != c.Sender.ID { + channelChat, err := b.bot.ChatByID(subscriberID) + if err != nil { + return false + } + + if !chat.IsChatAdmin(b.bot, channelChat, c.Sender.ID) { + return false + } + } + return true +} + +func (b *SetSubscriptionTagButton) Handle(ctx tb.Context) error { + c := ctx.Callback() + // 权限验证 + if !b.feedSetAuth(c) { + return ctx.Send("无权限") + } + data := strings.Split(c.Data, ":") + ownID, _ := strconv.Atoi(data[0]) + sourceID, _ := strconv.Atoi(data[1]) + + sub, err := model.GetSubscribeByUserIDAndSourceID(int64(ownID), uint(sourceID)) + if err != nil { + return ctx.Send("系统错误,代码04") + } + msg := fmt.Sprintf( + "请使用`/setfeedtag %d tags`命令为该订阅设置标签,tags为需要设置的标签,以空格分隔。(最多设置三个标签) \n"+ + "例如:`/setfeedtag %d 科技 苹果`", + sub.ID, sub.ID, + ) + return ctx.Edit(msg, &tb.SendOptions{ParseMode: tb.ModeMarkdown}) +} + +func (b *SetSubscriptionTagButton) Middlewares() []tb.MiddlewareFunc { + return nil +}