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

Commit

Permalink
feat(bot): unsub all
Browse files Browse the repository at this point in the history
  • Loading branch information
indes committed Nov 19, 2019
1 parent 318c229 commit 51b8c61
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 3 deletions.
84 changes: 81 additions & 3 deletions bot/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,26 @@ var (
Unique: "toggle_notice",
Text: "切换通知",
}

toggleTelegraphKey = tb.InlineButton{
Unique: "toggle_telegraph",
Text: "切换 Telegraph",
}

toggleEnabledKey = tb.InlineButton{
Unique: "toggle_enabled",
Text: "抓取开关",
}

confirmButton = tb.InlineButton{
Unique: "confirm",
Text: "确认",
}

cancelButton = tb.InlineButton{
Unique: "cancel",
Text: "取消",
}
)

func init() {
Expand Down Expand Up @@ -167,7 +179,7 @@ func toggleCtrlButtons(c *tb.Callback, action string) {
})
}

//Start run bot
//Start bot
func Start() {
makeHandle()
B.Start()
Expand All @@ -187,6 +199,48 @@ func makeHandle() {
toggleCtrlButtons(c, "toggleEnabledKey")
})

B.Handle(&confirmButton, func(c *tb.Callback) {

mention := GetMentionFromMessage(c.Message)
var msg string
if mention == "" {
success, fail, err := model.UnsubAllByUserID(int64(c.Sender.ID))
if err != nil {
msg = "退订失败"
} else {
msg = fmt.Sprintf("退订成功:%d\n退订失败:%d", success, fail)
}

} else {
channelChat, err := B.ChatByID(mention)

if err != nil {
_, _ = B.Edit(c.Message, "error")
return
}

if UserIsAdminChannel(c.Sender.ID, channelChat) {
success, fail, err := model.UnsubAllByUserID(channelChat.ID)
if err != nil {
msg = "退订失败"

} else {
msg = fmt.Sprintf("退订成功:%d\n退订失败:%d", success, fail)
}

} else {
msg = "非频道管理员无法执行此操作"
}
}

_, _ = B.Edit(c.Message, msg)

})

B.Handle(&cancelButton, func(c *tb.Callback) {
_, _ = B.Edit(c.Message, "操作取消")
})

B.Handle("/start", func(m *tb.Message) {
user := model.FindOrInitUser(m.Chat.ID)
log.Printf("/start %d", user.ID)
Expand Down Expand Up @@ -285,7 +339,7 @@ func makeHandle() {
})

B.Handle("/list", func(m *tb.Message) {
_, mention := GetUrlAndMentionFromMessage(m)
mention := GetMentionFromMessage(m)
if mention != "" {
channelChat, err := B.ChatByID(mention)
if err != nil {
Expand Down Expand Up @@ -496,6 +550,29 @@ func makeHandle() {

})

B.Handle("/unsuball", func(m *tb.Message) {
mention := GetMentionFromMessage(m)
confirmKeys := [][]tb.InlineButton{}
confirmKeys = append(confirmKeys, []tb.InlineButton{confirmButton, cancelButton})
var msg string

if mention == "" {
msg = "是否退订当前用户的所有订阅?"
} else {
msg = fmt.Sprintf("%s 是否退订该 Channel 所有订阅?", mention)
}

_, _ = B.Send(
m.Chat,
msg,
&tb.SendOptions{
ParseMode: tb.ModeHTML,
}, &tb.ReplyMarkup{
InlineKeyboard: confirmKeys,
},
)
})

B.Handle("/ping", func(m *tb.Message) {

_, _ = B.Send(m.Chat, "pong")
Expand Down Expand Up @@ -603,16 +680,17 @@ func makeHandle() {

text := new(bytes.Buffer)
if sub.EnableNotification == 1 {

toggleNoticeKey.Text = "关闭通知"
} else {
toggleNoticeKey.Text = "开启通知"
}

if sub.EnableTelegraph == 1 {
toggleTelegraphKey.Text = "关闭 Telegraph 转码"
} else {
toggleTelegraphKey.Text = "开启 Telegraph 转码"
}

if source.ErrorCount >= 100 {
toggleEnabledKey.Text = "重启更新"
} else {
Expand Down
1 change: 1 addition & 0 deletions bot/fsm/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ const (
Sub
UnSub
Set
UnSubAll
)
16 changes: 16 additions & 0 deletions bot/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,22 @@ func CheckAdmin(upd *tb.Update) bool {
return false
}

func UserIsAdminChannel(userID int, channelChat *tb.Chat) (isAdmin bool) {
adminList, err := B.AdminsOf(channelChat)
isAdmin = false

if err != nil {
return
}

for _, admin := range adminList {
if userID == admin.User.ID {
isAdmin = true
}
}
return
}

func HasAdminType(t tb.ChatType) bool {
hasAdmin := []tb.ChatType{tb.ChatGroup, tb.ChatSuperGroup, tb.ChatChannel, tb.ChatChannelPrivate}
for _, n := range hasAdmin {
Expand Down
23 changes: 23 additions & 0 deletions model/subscribe.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,29 @@ func UnsubByUserIDAndSource(userID int64, source *Source) error {
}
return nil
}

func UnsubAllByUserID(userID int64) (success int, fail int, err error) {
db := getConnect()
defer db.Close()
success = 0
fail = 0
var subs []Subscribe

db.Where("user_id=?", userID).Find(&subs)

for _, sub := range subs {
err := sub.Unsub()
if err != nil {
fail += 1
} else {
success += 1
}
}
err = nil

return
}

func GetSubByUserIDAndURL(userID int64, url string) (*Subscribe, error) {
db := getConnect()
defer db.Close()
Expand Down

0 comments on commit 51b8c61

Please sign in to comment.