Skip to content
This repository has been archived by the owner on Sep 17, 2024. It is now read-only.

Commit

Permalink
feat: set command for channel
Browse files Browse the repository at this point in the history
  • Loading branch information
indes committed Jan 4, 2020
1 parent 7c095d7 commit 6c6c169
Showing 1 changed file with 86 additions and 29 deletions.
115 changes: 86 additions & 29 deletions bot/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 = "关闭通知"
Expand All @@ -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 转码"
Expand All @@ -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 {
Expand Down Expand Up @@ -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) {
Expand All @@ -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))
Expand All @@ -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
Expand All @@ -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 = "关闭通知"
Expand All @@ -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 转码"
Expand All @@ -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 {
Expand Down

0 comments on commit 6c6c169

Please sign in to comment.