Skip to content

Commit

Permalink
add inline limits and offsets
Browse files Browse the repository at this point in the history
  • Loading branch information
koenigskraut committed Aug 8, 2023
1 parent 699fc40 commit 067155a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 14 deletions.
31 changes: 18 additions & 13 deletions cmd/bot/handle_inline.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ func handleInline(client *tg.Client) func(context.Context, tg.Entities, *tg.Upda
return func(ctx context.Context, entities tg.Entities, update *tg.UpdateBotInlineQuery) (err error) {
var q []*db.StickerTag

const limit = 50
offset, _ := strconv.ParseInt(update.Offset, 10, 32)
nextOffset := ""

u := db.User{UserID: update.UserID}
if e := u.Get(); e != nil {
return e
Expand All @@ -30,6 +34,11 @@ func handleInline(client *tg.Client) func(context.Context, tg.Entities, *tg.Upda
if err != nil {
return err
}
q = q[offset:]
if len(q) > limit {
q = q[:limit]
nextOffset = strconv.FormatInt(offset+limit, 10)
}

as := make([]inline.ResultOption, len(q))
for i, st := range q {
Expand All @@ -41,7 +50,7 @@ func handleInline(client *tg.Client) func(context.Context, tg.Entities, *tg.Upda
)
}

w := sender.Inline(update).CacheTimeSeconds(0).NextOffset("").Gallery(true)
w := sender.Inline(update).CacheTimeSeconds(0).NextOffset(nextOffset).Gallery(true)
webAppUser := &webapp.User{}
for _, e := range entities.Users {
if e.ID == update.UserID {
Expand All @@ -61,18 +70,14 @@ func handleInline(client *tg.Client) func(context.Context, tg.Entities, *tg.Upda
return err
}
URL := fmt.Sprintf("https://%s:%s?%s", appDomain, appPort, string(signed))

if len(as) > 0 {
if len(as) > 50 {
as = as[:50]
}
if _, err := w.SwitchWebview("Изменить порядок стикеров", URL).Set(ctx, as...); err != nil {
return err
}
} else {
_, err := w.SwitchPM("Начать создавать теги!", "a").Set(ctx)
return err
switch len(as) {
case 0:
_, err = w.SwitchPM("Начать создавать теги!", "").Set(ctx)
case 1:
_, err = w.SwitchPM("Добавить теги!", "").Set(ctx)
default:
_, err = w.SwitchWebview("Изменить порядок стикеров", URL).Set(ctx, as...)
}
return nil
return err
}
}
4 changes: 3 additions & 1 deletion database/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ func (u *User) RecentStickers() ([]*StickerTag, error) {
Where(&StickerTag{User: u.UserID}).
Order("added desc").
Group("sticker_id").
Limit(500).
Find(&pre).Error
if err != nil {
return nil, err
Expand All @@ -85,7 +86,8 @@ func (u *User) SearchStickers(prefix string) ([]*StickerTag, error) {
Order("added desc").
Where("tag LIKE ?", prefix+"%").
Order("added desc").
Group("sticker_id")
Group("sticker_id").
Limit(500)
if u.GlobalTag {
query = query.Where("user IN (?, 0)", u.UserID)
} else {
Expand Down

0 comments on commit 067155a

Please sign in to comment.