Skip to content

Commit

Permalink
feat(unsub): inline button
Browse files Browse the repository at this point in the history
  • Loading branch information
indes committed Dec 4, 2019
1 parent f3d2fdf commit 47dfe73
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 25 deletions.
2 changes: 2 additions & 0 deletions bot/bot.go
Expand Up @@ -85,6 +85,8 @@ func makeHandle() {

B.Handle(&tb.InlineButton{Unique: "unsub_all_cancel_btn"}, unsubAllCancelBtnCtr)

B.Handle(&tb.InlineButton{Unique:"unsub_feed_item_btn"},unsubFeedItemBtnCtr)

B.Handle("/start", startCmdCtr)

B.Handle("/export", exportCmdCtr)
Expand Down
72 changes: 52 additions & 20 deletions bot/controller.go
Expand Up @@ -42,7 +42,7 @@ func toggleCtrlButtons(c *tb.Callback, action string) {
return
}

source, _ := model.GetSourceById(int(sub.SourceID))
source, _ := model.GetSourceById(sub.SourceID)
t := template.New("setting template")
_, _ = t.Parse(feedSettingTmpl)

Expand Down Expand Up @@ -335,31 +335,42 @@ func unsubCmdCtr(m *tb.Message) {
}
} else {
//Unsub by button
sources, _ := model.GetSourcesByUserID(m.Chat.ID)
if len(sources) > 0 {
var replyButton []tb.ReplyButton
replyKeys := [][]tb.ReplyButton{}
for _, source := range sources {
// 添加按钮
text := fmt.Sprintf("[%d] %s", source.ID, source.Title)
replyButton = []tb.ReplyButton{
tb.ReplyButton{Text: text},
//sources, _ := model.GetSourcesByUserID(m.Chat.ID)

subs, err := model.GetSubsByUserID(m.Chat.ID)

if err != nil {
errorCtr(m, "Bot错误,请联系管理员!")
return
}

if len(subs) > 0 {
unsubFeedItemBtns := [][]tb.InlineButton{}

for _, sub := range subs {
log.Print(sub.ID)
source, err := model.GetSourceById(sub.SourceID)
if err != nil {
errorCtr(m, "Bot错误,请联系管理员!")
return
}

replyKeys = append(replyKeys, replyButton)
unsubFeedItemBtns = append(unsubFeedItemBtns, []tb.InlineButton{
tb.InlineButton{
Unique: "unsub_feed_item_btn",
Text: fmt.Sprintf("[%d] %s", sub.SourceID, source.Title),
Data: fmt.Sprintf("%d:%d", sub.UserID, sub.ID),
Action: nil,
},
})
}
_, err := B.Send(m.Chat, "请选择你要退订的源", &tb.ReplyMarkup{
ForceReply: true,
ReplyKeyboard: replyKeys,
})

if err == nil {
UserState[m.Chat.ID] = fsm.UnSub
}
_, _ = B.Send(m.Chat, "请选择你要退订的源", &tb.ReplyMarkup{
InlineKeyboard: unsubFeedItemBtns,
})
} else {
_, _ = B.Send(m.Chat, "当前没有订阅源")
}

}
} else {
if url != "" {
Expand Down Expand Up @@ -431,6 +442,23 @@ func unsubCmdCtr(m *tb.Message) {

}

func unsubFeedItemBtnCtr(c *tb.Callback) {
//model.UnsubAllByUserID()
log.Print(c.Data)
data := strings.Split(c.Data, ":")
if len(data) == 2 {
userID, _ := strconv.Atoi(data[0])
subID, _ := strconv.Atoi(data[1])
err := model.UnsubByUserIDAndSubID(int64(userID), uint(subID))

if err == nil {
_, _ = B.Edit(c.Message, "退订成功")
return
}
}
_, _ = B.Edit(c.Message, "退订错误!")
}

func unsubAllCmdCtr(m *tb.Message) {
mention := GetMentionFromMessage(m)
confirmKeys := [][]tb.InlineButton{}
Expand Down Expand Up @@ -539,7 +567,7 @@ func textCtr(m *tb.Message) {
_, _ = B.Send(m.Chat, "请选择正确的指令!")
} else {

var sourceId int
var sourceId uint
if _, err := fmt.Sscanf(str[0], "[%d]", &sourceId); err != nil {
_, _ = B.Send(m.Chat, "请选择正确的指令!")
return
Expand Down Expand Up @@ -739,3 +767,7 @@ func docCtr(m *tb.Message) {
}

}

func errorCtr(m *tb.Message, errMsg string) {
_, _ = B.Send(m.Chat, errMsg)
}
9 changes: 7 additions & 2 deletions model/source.go
Expand Up @@ -125,7 +125,12 @@ func GetSourcesByUserID(userID int64) ([]Source, error) {
db := getConnect()
defer db.Close()
var sources []Source
subs := GetSubsByUserID(userID)
subs, err := GetSubsByUserID(userID)

if err != nil {
return nil, err
}

for _, sub := range subs {
var source Source
db.Where("id=?", sub.SourceID).First(&source)
Expand Down Expand Up @@ -154,7 +159,7 @@ func (s *Source) Save() {
return
}

func GetSourceById(id int) (*Source, error) {
func GetSourceById(id uint) (*Source, error) {
db := getConnect()
defer db.Close()
var source Source
Expand Down
34 changes: 31 additions & 3 deletions model/subscribe.go
Expand Up @@ -58,6 +58,10 @@ func GetSubscribeByUserIDAndURL(userID int, url string) (*Subscribe, error) {
return &sub, nil
}
func GetSubscriberBySource(s *Source) []Subscribe {
if s == nil {
return []Subscribe{}
}

db := getConnect()
defer db.Close()
var subs []Subscribe
Expand All @@ -67,11 +71,16 @@ func GetSubscriberBySource(s *Source) []Subscribe {
}

func UnsubByUserIDAndSource(userID int64, source *Source) error {

if source == nil {
return errors.New("nil pointer")
}

db := getConnect()
defer db.Close()
var sub Subscribe
db.Where("user_id=? and source_id=?", userID, source.ID).First(&sub)
if sub.UserID != int64(userID) {
if sub.UserID != userID {
return errors.New("未订阅该RSS源")
}
db.Delete(&sub)
Expand All @@ -81,6 +90,25 @@ func UnsubByUserIDAndSource(userID int64, source *Source) error {
return nil
}

func UnsubByUserIDAndSubID(userID int64, subID uint) error {
db := getConnect()
defer db.Close()

var sub Subscribe
db.Where("id=?", subID).First(&sub)

if sub.UserID != userID {
return errors.New("未找到该条订阅")
}
db.Delete(&sub)

source, _ := GetSourceById(sub.SourceID)
if source.GetSubscribeNum() < 1 {
source.DeleteDueNoSubscriber()
}
return nil
}

func UnsubAllByUserID(userID int64) (success int, fail int, err error) {
db := getConnect()
defer db.Close()
Expand Down Expand Up @@ -115,15 +143,15 @@ func GetSubByUserIDAndURL(userID int64, url string) (*Subscribe, error) {
return &sub, err
}

func GetSubsByUserID(userID int64) []Subscribe {
func GetSubsByUserID(userID int64) ([]Subscribe, error) {
db := getConnect()
defer db.Close()

var subs []Subscribe

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

return subs
return subs, nil
}

func UnsubByUserIDAndSourceURL(userID int64, url string) error {
Expand Down

0 comments on commit 47dfe73

Please sign in to comment.