Skip to content

messages list --after-id returns an empty list #60

Description

@kfastov

Problem

tgcli messages list --after-id <N> returns an empty list even when the chat has messages newer than N.

Found on tgcli 2.2.3 (Homebrew), --source live, chat me (Saved Messages):

# newest messages in the chat: 501195, 501004, 500803, 500802, 500673, ...
tgcli --json messages list --chat me --source live --limit 5
# -> [501195, 501004, 500803, 500802, 500673]

tgcli --json messages list --chat me --source live --after-id 500673
tgcli --json messages list --chat me --source live --after-id 500673 --limit 2
tgcli --json messages list --chat me --source live --after-id 500673 --limit 10
# -> "messages": [], "hasMore": false

tgcli --json messages list --chat me --source live --after-id 501000 --limit 5
# -> "messages": [], "hasMore": false   (expected 501004, 501195)

--before-id works: --before-id 501004 --limit 3 returns [500803, 500802, 500673], hasMore: true.

Where to look

getMessagesByChannelId in telegram-client.js (same code on main after v2.2.3). With afterId set, it calls iterHistory with reverse: true and minId, but no offset:

const iterOptions = {
  limit: effectiveLimit,
  chunkSize: Math.min(effectiveLimit, 100),
  reverse: useAfterIdPagination ? true : reverse,
};
if (effectiveMinId) {
  iterOptions.minId = effectiveMinId;
}

Hypothesis, not verified: when mtcute iterates in reverse without an offset, it starts from the wrong end, so every message gets filtered out by minId. Passing offset: { id: afterId + 1, date: 0 } (plus a matching addOffset) may fix it.

Workaround

Page backwards with --before-id until reaching the known id.

Acceptance

  • messages list --after-id N returns messages with id > N, oldest first or documented order, respecting --limit
  • hasMore / next cursor lets a caller page forward through everything newer than N
  • a test covers --after-id against a chat with more than --limit newer messages

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions