From 6c6c16923793316a2f4c3dbf20b234dcd26c5c09 Mon Sep 17 00:00:00 2001 From: indes Date: Sat, 4 Jan 2020 15:20:06 +0800 Subject: [PATCH] feat: set command for channel --- bot/controller.go | 115 ++++++++++++++++++++++++++++++++++------------ 1 file changed, 86 insertions(+), 29 deletions(-) diff --git a/bot/controller.go b/bot/controller.go index d2cd112e..cdfa64d4 100644 --- a/bot/controller.go +++ b/bot/controller.go @@ -34,6 +34,21 @@ func toggleCtrlButtons(c *tb.Callback, action string) { return } + data := strings.Split(c.Data, ":") + subscriberID, _ := strconv.Atoi(data[0]) + // 如果订阅者与按钮点击者id不一致,需要验证管理员权限 + if subscriberID != c.Sender.ID { + channelChat, err := B.ChatByID(fmt.Sprintf("%d", subscriberID)) + + if err != nil { + return + } + + if !UserIsAdminChannel(c.Sender.ID, channelChat) { + return + } + } + msg := strings.Split(c.Message.Text, "\n") subID, err := strconv.Atoi(strings.Split(msg[1], " ")[1]) if err != nil { @@ -75,6 +90,7 @@ func toggleCtrlButtons(c *tb.Callback, action string) { toggleNoticeKey := tb.InlineButton{ Unique: "set_toggle_notice_btn", Text: "开启通知", + Data: c.Data, } if sub.EnableNotification == 1 { toggleNoticeKey.Text = "关闭通知" @@ -83,6 +99,7 @@ func toggleCtrlButtons(c *tb.Callback, action string) { toggleTelegraphKey := tb.InlineButton{ Unique: "set_toggle_telegraph_btn", Text: "开启 Telegraph 转码", + Data: c.Data, } if sub.EnableTelegraph == 1 { toggleTelegraphKey.Text = "关闭 Telegraph 转码" @@ -91,6 +108,7 @@ func toggleCtrlButtons(c *tb.Callback, action string) { toggleEnabledKey := tb.InlineButton{ Unique: "set_toggle_update_btn", Text: "暂停更新", + Data: c.Data, } if source.ErrorCount >= config.ErrorThreshold { @@ -276,47 +294,68 @@ func listCmdCtr(m *tb.Message) { func setCmdCtr(m *tb.Message) { - metion := GetMentionFromMessage(m) - - if metion == "" { - sources, _ := model.GetSourcesByUserID(m.Chat.ID) - + mention := GetMentionFromMessage(m) + var sources []model.Source + var ownerID int64 + // 获取订阅列表 + if mention == "" { + sources, _ = model.GetSourcesByUserID(m.Chat.ID) + ownerID = int64(m.Sender.ID) if len(sources) <= 0 { _, _ = B.Send(m.Chat, "当前没有订阅源") return } - var replyButton []tb.ReplyButton - replyKeys := [][]tb.ReplyButton{} + } else { + + channelChat, err := B.ChatByID(mention) + + if err != nil { + _, _ = B.Send(m.Chat, "获取Channel信息错误。") + return + } + + if UserIsAdminChannel(m.Sender.ID, channelChat) { + sources, _ = model.GetSourcesByUserID(channelChat.ID) - setFeedItemBtns := [][]tb.InlineButton{} - for _, source := range sources { - // 添加按钮 - text := fmt.Sprintf("%s %s", source.Title, source.Link) - replyButton = []tb.ReplyButton{ - tb.ReplyButton{Text: text}, + if len(sources) <= 0 { + _, _ = B.Send(m.Chat, "Channel没有订阅源。") + return } - replyKeys = append(replyKeys, replyButton) + ownerID = channelChat.ID - setFeedItemBtns = append(setFeedItemBtns, []tb.InlineButton{ - tb.InlineButton{ - Unique: "set_feed_item_btn", - Text: fmt.Sprintf("[%d] %s", source.ID, source.Title), - Data: fmt.Sprintf("%d:%d", m.Chat.ID, source.ID), - }, - }) + } else { + _, _ = B.Send(m.Chat, "非Channel管理员无法执行此操作。") + return } - _, _ = B.Send(m.Chat, "请选择你要设置的源", &tb.ReplyMarkup{ - //ForceReply: true, - //ReplyKeyboard: replyKeys, - InlineKeyboard: setFeedItemBtns, - }) + } - } else { + var replyButton []tb.ReplyButton + replyKeys := [][]tb.ReplyButton{} + setFeedItemBtns := [][]tb.InlineButton{} + + // 配置按钮 + for _, source := range sources { + // 添加按钮 + text := fmt.Sprintf("%s %s", source.Title, source.Link) + replyButton = []tb.ReplyButton{ + tb.ReplyButton{Text: text}, + } + replyKeys = append(replyKeys, replyButton) + setFeedItemBtns = append(setFeedItemBtns, []tb.InlineButton{ + tb.InlineButton{ + Unique: "set_feed_item_btn", + Text: fmt.Sprintf("[%d] %s", source.ID, source.Title), + Data: fmt.Sprintf("%d:%d", ownerID, source.ID), + }, + }) } + _, _ = B.Send(m.Chat, "请选择你要设置的源", &tb.ReplyMarkup{ + InlineKeyboard: setFeedItemBtns, + }) } func setFeedItemBtnCtr(c *tb.Callback) { @@ -327,7 +366,22 @@ func setFeedItemBtnCtr(c *tb.Callback) { } data := strings.Split(c.Data, ":") - chatID, _ := strconv.Atoi(data[0]) + subscriberID, _ := strconv.Atoi(data[0]) + + // 如果订阅者与按钮点击者id不一致,需要验证管理员权限 + + if subscriberID != c.Sender.ID { + channelChat, err := B.ChatByID(fmt.Sprintf("%d", subscriberID)) + + if err != nil { + return + } + + if !UserIsAdminChannel(c.Sender.ID, channelChat) { + return + } + } + sourceID, _ := strconv.Atoi(data[1]) source, err := model.GetSourceById(uint(sourceID)) @@ -337,7 +391,7 @@ func setFeedItemBtnCtr(c *tb.Callback) { return } - sub, err := model.GetSubscribeByUserIDAndSourceID(int64(chatID), source.ID) + sub, err := model.GetSubscribeByUserIDAndSourceID(int64(subscriberID), source.ID) if err != nil { _, _ = B.Edit(c.Message, "用户未订阅该rss,错误代码02。") return @@ -349,6 +403,7 @@ func setFeedItemBtnCtr(c *tb.Callback) { toggleNoticeKey := tb.InlineButton{ Unique: "set_toggle_notice_btn", Text: "开启通知", + Data: c.Data, } if sub.EnableNotification == 1 { toggleNoticeKey.Text = "关闭通知" @@ -357,6 +412,7 @@ func setFeedItemBtnCtr(c *tb.Callback) { toggleTelegraphKey := tb.InlineButton{ Unique: "set_toggle_telegraph_btn", Text: "开启 Telegraph 转码", + Data: c.Data, } if sub.EnableTelegraph == 1 { toggleTelegraphKey.Text = "关闭 Telegraph 转码" @@ -365,6 +421,7 @@ func setFeedItemBtnCtr(c *tb.Callback) { toggleEnabledKey := tb.InlineButton{ Unique: "set_toggle_update_btn", Text: "暂停更新", + Data: c.Data, } if source.ErrorCount >= config.ErrorThreshold {