fix(client): improved searchMessages method
use offsetId for pagination, expose minId/maxId/minDate/maxDate, threadId, offsetId
This commit is contained in:
parent
0330efd4dc
commit
b6f2f39906
2 changed files with 112 additions and 11 deletions
|
@ -757,7 +757,10 @@ export interface TelegramClient extends BaseTelegramClient {
|
||||||
* Whether the results should be displayed as a gallery instead
|
* Whether the results should be displayed as a gallery instead
|
||||||
* of a vertical list. Only applicable to some media types.
|
* of a vertical list. Only applicable to some media types.
|
||||||
*
|
*
|
||||||
* Defaults to `true`
|
* In some cases changing this may lead to the results not being
|
||||||
|
* displayed by the client.
|
||||||
|
*
|
||||||
|
* Default is derived automatically based on result types
|
||||||
*/
|
*/
|
||||||
gallery?: boolean
|
gallery?: boolean
|
||||||
|
|
||||||
|
@ -2818,12 +2821,56 @@ export interface TelegramClient extends BaseTelegramClient {
|
||||||
query?: string
|
query?: string
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sequential number of the first message to be returned.
|
* Offset ID for the search. Only messages earlier than this
|
||||||
|
* ID will be returned.
|
||||||
*
|
*
|
||||||
* Defaults to `0`.
|
* Defaults to `0` (for the latest message).
|
||||||
|
*/
|
||||||
|
offsetId?: number
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Offset from the {@link offsetId}. Only used for the
|
||||||
|
* first chunk
|
||||||
|
*
|
||||||
|
* Defaults to `0` (for the same message as {@link offsetId}).
|
||||||
*/
|
*/
|
||||||
offset?: number
|
offset?: number
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Minimum message ID to return
|
||||||
|
*
|
||||||
|
* Defaults to `0` (disabled).
|
||||||
|
*/
|
||||||
|
minId?: number
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Maximum message ID to return.
|
||||||
|
*
|
||||||
|
* > *Seems* to work the same as {@link offsetId}
|
||||||
|
*
|
||||||
|
* Defaults to `0` (disabled).
|
||||||
|
*/
|
||||||
|
maxId?: number
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Minimum message date to return
|
||||||
|
*
|
||||||
|
* Defaults to `0` (disabled).
|
||||||
|
*/
|
||||||
|
minDate?: number | Date
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Maximum message date to return
|
||||||
|
*
|
||||||
|
* Defaults to `0` (disabled).
|
||||||
|
*/
|
||||||
|
maxDate?: number | Date
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Thread ID to return only messages from this thread.
|
||||||
|
*/
|
||||||
|
threadId?: number
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Limits the number of messages to be retrieved.
|
* Limits the number of messages to be retrieved.
|
||||||
*
|
*
|
||||||
|
|
|
@ -9,6 +9,7 @@ import {
|
||||||
PeersIndex,
|
PeersIndex,
|
||||||
} from '../../types'
|
} from '../../types'
|
||||||
import { SearchFilters } from '../../types'
|
import { SearchFilters } from '../../types'
|
||||||
|
import { normalizeDate } from '../../utils/misc-utils'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Search for messages inside a specific chat
|
* Search for messages inside a specific chat
|
||||||
|
@ -30,12 +31,56 @@ export async function* searchMessages(
|
||||||
query?: string
|
query?: string
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sequential number of the first message to be returned.
|
* Offset ID for the search. Only messages earlier than this
|
||||||
|
* ID will be returned.
|
||||||
*
|
*
|
||||||
* Defaults to `0`.
|
* Defaults to `0` (for the latest message).
|
||||||
|
*/
|
||||||
|
offsetId?: number
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Offset from the {@link offsetId}. Only used for the
|
||||||
|
* first chunk
|
||||||
|
*
|
||||||
|
* Defaults to `0` (for the same message as {@link offsetId}).
|
||||||
*/
|
*/
|
||||||
offset?: number
|
offset?: number
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Minimum message ID to return
|
||||||
|
*
|
||||||
|
* Defaults to `0` (disabled).
|
||||||
|
*/
|
||||||
|
minId?: number
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Maximum message ID to return.
|
||||||
|
*
|
||||||
|
* > *Seems* to work the same as {@link offsetId}
|
||||||
|
*
|
||||||
|
* Defaults to `0` (disabled).
|
||||||
|
*/
|
||||||
|
maxId?: number
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Minimum message date to return
|
||||||
|
*
|
||||||
|
* Defaults to `0` (disabled).
|
||||||
|
*/
|
||||||
|
minDate?: number | Date
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Maximum message date to return
|
||||||
|
*
|
||||||
|
* Defaults to `0` (disabled).
|
||||||
|
*/
|
||||||
|
maxDate?: number | Date
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Thread ID to return only messages from this thread.
|
||||||
|
*/
|
||||||
|
threadId?: number
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Limits the number of messages to be retrieved.
|
* Limits the number of messages to be retrieved.
|
||||||
*
|
*
|
||||||
|
@ -70,8 +115,14 @@ export async function* searchMessages(
|
||||||
if (!params) params = {}
|
if (!params) params = {}
|
||||||
|
|
||||||
let current = 0
|
let current = 0
|
||||||
|
let offsetId = params.offsetId || 0
|
||||||
let offset = params.offset || 0
|
let offset = params.offset || 0
|
||||||
|
|
||||||
|
const minDate = normalizeDate(params.minDate) ?? 0
|
||||||
|
const maxDate = normalizeDate(params.maxDate) ?? 0
|
||||||
|
const minId = params.minId ?? 0
|
||||||
|
const maxId = params.maxId ?? 0
|
||||||
|
|
||||||
const total = params.limit || Infinity
|
const total = params.limit || Infinity
|
||||||
const limit = Math.min(params.chunkSize || 100, total)
|
const limit = Math.min(params.chunkSize || 100, total)
|
||||||
|
|
||||||
|
@ -86,13 +137,13 @@ export async function* searchMessages(
|
||||||
peer,
|
peer,
|
||||||
q: params.query || '',
|
q: params.query || '',
|
||||||
filter: params.filter || SearchFilters.Empty,
|
filter: params.filter || SearchFilters.Empty,
|
||||||
minDate: 0,
|
minDate,
|
||||||
maxDate: 0,
|
maxDate,
|
||||||
offsetId: 0,
|
offsetId,
|
||||||
addOffset: offset,
|
addOffset: offset,
|
||||||
limit: Math.min(limit, total - current),
|
limit: Math.min(limit, total - current),
|
||||||
minId: 0,
|
minId,
|
||||||
maxId: 0,
|
maxId,
|
||||||
fromId: fromUser,
|
fromId: fromUser,
|
||||||
hash: Long.ZERO,
|
hash: Long.ZERO,
|
||||||
})
|
})
|
||||||
|
@ -104,6 +155,9 @@ export async function* searchMessages(
|
||||||
res._
|
res._
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// for successive chunks, we need to reset the offset
|
||||||
|
offset = 0
|
||||||
|
|
||||||
const peers = PeersIndex.from(res)
|
const peers = PeersIndex.from(res)
|
||||||
|
|
||||||
const msgs = res.messages
|
const msgs = res.messages
|
||||||
|
@ -112,7 +166,7 @@ export async function* searchMessages(
|
||||||
|
|
||||||
if (!msgs.length) break
|
if (!msgs.length) break
|
||||||
|
|
||||||
offset += msgs.length
|
offsetId = res.messages[res.messages.length - 1].id
|
||||||
yield* msgs
|
yield* msgs
|
||||||
|
|
||||||
current += msgs.length
|
current += msgs.length
|
||||||
|
|
Loading…
Reference in a new issue