Make list_messages predictable and efficient for date ranges and search: avoid over-fetch, apply limit after filtering, and separate search from date offsets #27
- stream search results via `client.iter_messages()` and apply date bounds in-code so we collect up to `limit` matches - use server-side iteration for date-only queries with inclusive upper bounds to avoid over-fetching - keep the existing `get_messages()` fast path when no filters are provided
This commit is contained in:
parent
3cd0489769
commit
553125cd76
1 changed files with 38 additions and 11 deletions
49
main.py
49
main.py
|
|
@ -412,20 +412,47 @@ async def list_messages(
|
|||
# Prepare filter parameters
|
||||
params = {}
|
||||
if search_query:
|
||||
# IMPORTANT: Do not combine offset_date with search.
|
||||
# Use server-side search alone, then enforce date bounds client-side.
|
||||
params["search"] = search_query
|
||||
|
||||
messages = await client.get_messages(entity, limit=limit, **params)
|
||||
|
||||
# Apply date filters (Telethon doesn't support date filtering in get_messages directly)
|
||||
if from_date_obj or to_date_obj:
|
||||
filtered_messages = []
|
||||
for msg in messages:
|
||||
if from_date_obj and msg.date < from_date_obj:
|
||||
continue
|
||||
messages = []
|
||||
async for msg in client.iter_messages(entity, **params): # newest -> oldest
|
||||
if to_date_obj and msg.date > to_date_obj:
|
||||
continue
|
||||
filtered_messages.append(msg)
|
||||
messages = filtered_messages
|
||||
if from_date_obj and msg.date < from_date_obj:
|
||||
break
|
||||
messages.append(msg)
|
||||
if len(messages) >= limit:
|
||||
break
|
||||
|
||||
else:
|
||||
# Use server-side iteration when only date bounds are present
|
||||
# (no search) to avoid over-fetching.
|
||||
if from_date_obj or to_date_obj:
|
||||
messages = []
|
||||
if from_date_obj:
|
||||
# Walk forward from start date (oldest -> newest)
|
||||
async for msg in client.iter_messages(
|
||||
entity, offset_date=from_date_obj, reverse=True
|
||||
):
|
||||
if to_date_obj and msg.date > to_date_obj:
|
||||
break
|
||||
if msg.date < from_date_obj:
|
||||
continue
|
||||
messages.append(msg)
|
||||
if len(messages) >= limit:
|
||||
break
|
||||
else:
|
||||
# Only upper bound: walk backward from end bound
|
||||
async for msg in client.iter_messages(
|
||||
# offset_date is exclusive; +1µs makes to_date inclusive
|
||||
entity, offset_date=to_date_obj + timedelta(microseconds=1)
|
||||
):
|
||||
messages.append(msg)
|
||||
if len(messages) >= limit:
|
||||
break
|
||||
else:
|
||||
messages = await client.get_messages(entity, limit=limit, **params)
|
||||
|
||||
if not messages:
|
||||
return "No messages found matching the criteria."
|
||||
|
|
|
|||
Loading…
Reference in a new issue