Skip to content

Commit

Permalink
feat: set tag button handler
Browse files Browse the repository at this point in the history
  • Loading branch information
indes committed Feb 25, 2022
1 parent 017aebd commit f9af26e
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 26 deletions.
2 changes: 1 addition & 1 deletion internal/bot/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ func setCommands() {
handler.NewSetFeedItemButton(B),
handler.NewRemoveSubscriptionItemButton(),
handler.NewNotificationSwitchButton(B),
handler.NewSetSubscriptionTagButton(B),
}

for _, h := range ButtonHandlers {
Expand All @@ -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)
}
25 changes: 0 additions & 25 deletions internal/bot/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package bot

import (
"bytes"
"fmt"
"strconv"
"strings"
"text/template"
Expand Down Expand Up @@ -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")
}
Expand Down
75 changes: 75 additions & 0 deletions internal/bot/handler/set_subscription_tag_button.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit f9af26e

Please sign in to comment.