Skip to content

Commit

Permalink
added pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
chofnar committed Jan 23, 2024
1 parent 67580e2 commit ec123f0
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 14 deletions.
10 changes: 5 additions & 5 deletions internal/server/behaviors/behaviors.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,8 @@ func (bh BehaviorHandler) validateAndRetrieveRepo(owner, name string) (repo.Repo
}, errors.ErrNoReleases
}

func (bh BehaviorHandler) SeeAll(chatID int64, messageID int) error {
markup, err := messages.SeeAllReposMarkup(chatID, messageID, &bh.DB)
func (bh BehaviorHandler) SeeRepos(chatID int64, messageID, limit, page int) error {
markup, err := messages.SeeReposMarkup(chatID, messageID, limit, page, &bh.DB)
if err != errors.ErrNoRepos && err != nil {
return err
}
Expand Down Expand Up @@ -202,11 +202,11 @@ func (bh BehaviorHandler) DeleteRepo(chatID int64, messageID int, data string) e
return err
}

return bh.SeeAll(chatID, messageID)
return bh.Menu(chatID, messageID)
}

func (bh BehaviorHandler) FlipPreRelease(chatID int64, messageID int, repoIDwithOP string) error {
repoIDwithNewVal := strings.TrimPrefix(repoIDwithOP, consts.OperationPrefix)
repoIDwithNewVal := strings.TrimPrefix(repoIDwithOP, consts.FlipOperationPrefix)

newValStr := repoIDwithNewVal[0]
newVal := false
Expand All @@ -221,7 +221,7 @@ func (bh BehaviorHandler) FlipPreRelease(chatID int64, messageID int, repoIDwith
return err
}

return bh.SeeAll(chatID, messageID)
return bh.Menu(chatID, messageID)
}

func (bh BehaviorHandler) newUpdate(repository repo.RepoWithChatID, isPre bool) error {
Expand Down
8 changes: 8 additions & 0 deletions internal/server/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,31 @@ package config

import (
"os"
"strconv"
)

type BotConfig struct {
TelegramToken, WebhookSite, Port, GithubGQLToken, ResetWebhookUrl string
Limit int
}

func LoadBotConfig() *BotConfig {
if os.Getenv("FROM_FILE") == "1" {
// TODO: implement
return nil
} else {
limit, err := strconv.Atoi(os.Getenv("LIMIT"))
if err != nil || limit == 0 {
panic("Limit cannot be read or is 0!")
}

return &BotConfig{
TelegramToken: os.Getenv("TELEGRAM_BOT_TOKEN"),
WebhookSite: os.Getenv("TELEGRAM_BOT_SITE_URL"),
Port: os.Getenv("PORT"),
GithubGQLToken: os.Getenv("GITHUB_GQL_TOKEN"),
ResetWebhookUrl: os.Getenv("RESET_WEBHOOK_URL"),
Limit: limit,
}
}
}
4 changes: 3 additions & 1 deletion internal/server/consts/strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,7 @@ const (

CheckRepo = "Check it out"

OperationPrefix = "OP_"
FlipOperationPrefix = "FLOP_"
PreviousOperationPrefix = "PRV_"
ForwardOperationPrefix = "FWD_"
)
47 changes: 43 additions & 4 deletions internal/server/messages/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func SeeAllReposButNoneFoundMessage(chatID int64, messageID int, markup telego.I
}
}

func SeeAllReposMarkup(chatID int64, messageID int, database *database.Database) (*telego.EditMessageReplyMarkupParams, error) {
func SeeReposMarkup(chatID int64, messageID, limit, page int, database *database.Database) (*telego.EditMessageReplyMarkupParams, error) {
repoList, err := (*database).GetRepos(fmt.Sprint(chatID))
if err != nil {
return nil, err
Expand All @@ -93,9 +93,17 @@ func SeeAllReposMarkup(chatID int64, messageID int, database *database.Database)
}, errors.ErrNoRepos
}

rows := make([][]telego.InlineKeyboardButton, len(repoList))
start := limit * page
var end int
if limit*page+limit < len(repoList) {
end = limit
} else {
end = len(repoList) - limit*page
}

rows := make([][]telego.InlineKeyboardButton, end)

for index, repo := range repoList {
for index, repo := range repoList[start : start+end] {
currentRow := make([]telego.InlineKeyboardButton, 4)

repoNameButton := telego.InlineKeyboardButton{
Expand All @@ -118,7 +126,7 @@ func SeeAllReposMarkup(chatID int64, messageID int, database *database.Database)
}
preReleaseNotifyButton := telego.InlineKeyboardButton{
Text: notifyPre,
CallbackData: consts.OperationPrefix + newVal + "_" + repo.RepoID,
CallbackData: consts.FlipOperationPrefix + newVal + "_" + repo.RepoID,
}
currentRow[2] = preReleaseNotifyButton

Expand All @@ -131,6 +139,37 @@ func SeeAllReposMarkup(chatID int64, messageID int, database *database.Database)
rows[index] = currentRow
}

// prev/forward

paginationRow := []telego.InlineKeyboardButton{}

if page > 0 {
paginationRow = append(paginationRow, telego.InlineKeyboardButton{
Text: "Previous",
CallbackData: consts.PreviousOperationPrefix + strconv.Itoa(page-1),
})
}

// more pages left
if page*limit+limit < len(repoList) {
paginationRow = append(paginationRow, telego.InlineKeyboardButton{
Text: "Next",
CallbackData: consts.ForwardOperationPrefix + strconv.Itoa(page+1),
})
}

rows = append(rows, paginationRow)

// back

backToMenuRow := []telego.InlineKeyboardButton{}
backToMenuRow = append(backToMenuRow, telego.InlineKeyboardButton{
Text: "Back to Menu",
CallbackData: consts.MenuCallback,
})

rows = append(rows, backToMenuRow)

replyMarkup := tu.InlineKeyboard(rows...)

return &telego.EditMessageReplyMarkupParams{
Expand Down
3 changes: 2 additions & 1 deletion internal/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ func Start() {
BehaviorHandler: behaviorHandler,
Logger: *logger,
AwaitingAddRepo: awaitingAddRepo,
Limit: botConf.Limit,
}

if botConf.ResetWebhookUrl != "" {
Expand Down Expand Up @@ -108,7 +109,7 @@ func Start() {
botHandler.Handle(handler.UnknownOrSent(), th.AnyMessageWithText())

// Callback queries
botHandler.HandleCallbackQuery(handler.SeeAll(), th.CallbackDataEqual(consts.SeeAllCallback))
botHandler.HandleCallbackQuery(handler.SeeRepos(botConf.Limit, 0), th.CallbackDataEqual(consts.SeeAllCallback))
botHandler.HandleCallbackQuery(handler.Add(), th.CallbackDataEqual(consts.AddCallback))
botHandler.HandleCallbackQuery(handler.Menu(), th.CallbackDataEqual(consts.MenuCallback))
botHandler.HandleCallbackQuery(handler.AnyCallbackRouter(), th.AnyCallbackQuery())
Expand Down
30 changes: 27 additions & 3 deletions internal/server/telegohandlers/telehandlers.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package telegohandlers

import (
"strconv"
"strings"

"github.com/chofnar/release-bot/internal/server/behaviors"
Expand All @@ -14,6 +15,7 @@ type Handler struct {
BehaviorHandler behaviors.BehaviorHandler
Logger zap.SugaredLogger
AwaitingAddRepo map[int64]struct{}
Limit int
}

type void struct{}
Expand Down Expand Up @@ -54,9 +56,9 @@ func (hc *Handler) UnknownOrSent() telegohandler.Handler {
}
}

func (hc *Handler) SeeAll() telegohandler.CallbackQueryHandler {
func (hc *Handler) SeeRepos(limit, page int) telegohandler.CallbackQueryHandler {
return func(bot *telego.Bot, query telego.CallbackQuery) {
err := hc.BehaviorHandler.SeeAll(query.Message.Chat.ID, query.Message.MessageID)
err := hc.BehaviorHandler.SeeRepos(query.Message.Chat.ID, query.Message.MessageID, limit, page)
if err != nil {
hc.Logger.Error(err)
}
Expand Down Expand Up @@ -85,11 +87,33 @@ func (hc *Handler) Add() telegohandler.CallbackQueryHandler {

func (hc *Handler) AnyCallbackRouter() telegohandler.CallbackQueryHandler {
return func(bot *telego.Bot, query telego.CallbackQuery) {
if strings.HasPrefix(query.Data, consts.OperationPrefix) {
if strings.HasPrefix(query.Data, consts.FlipOperationPrefix) {
err := hc.BehaviorHandler.FlipPreRelease(query.Message.Chat.ID, query.Message.MessageID, query.Data)
if err != nil {
hc.Logger.Error(err)
}
} else if strings.HasPrefix(query.Data, consts.PreviousOperationPrefix) {
page, err := strconv.Atoi(strings.TrimPrefix(query.Data, consts.PreviousOperationPrefix))
if err != nil {
hc.Logger.Error(err)
return
}

err = hc.BehaviorHandler.SeeRepos(query.Message.Chat.ID, query.Message.MessageID, hc.Limit, page)
if err != nil {
hc.Logger.Error(err)
}
} else if strings.HasPrefix(query.Data, consts.ForwardOperationPrefix) {
page, err := strconv.Atoi(strings.TrimPrefix(query.Data, consts.ForwardOperationPrefix))
if err != nil {
hc.Logger.Error(err)
return
}

err = hc.BehaviorHandler.SeeRepos(query.Message.Chat.ID, query.Message.MessageID, hc.Limit, page)
if err != nil {
hc.Logger.Error(err)
}
} else {
err := hc.BehaviorHandler.DeleteRepo(query.Message.Chat.ID, query.Message.MessageID, query.Data)
if err != nil {
Expand Down

0 comments on commit ec123f0

Please sign in to comment.