2021-04-09 13:08:32 +03:00
|
|
|
import { TelegramClient } from '../../client'
|
|
|
|
import { Message, MtCuteTypeAssertionError } from '../../types'
|
|
|
|
import { tl } from '@mtcute/tl'
|
|
|
|
import { createUsersChatsIndex } from '../../utils/peer-utils'
|
|
|
|
import { SearchFilters } from '../../types'
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Search for messages globally from all of your chats
|
|
|
|
*
|
|
|
|
* **Note**: Due to Telegram limitations, you can only get up to ~10000 messages
|
|
|
|
*
|
|
|
|
* @param params Search parameters
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
export async function* searchGlobal(
|
|
|
|
this: TelegramClient,
|
|
|
|
params?: {
|
|
|
|
/**
|
|
|
|
* Text query string. Use `"@"` to search for mentions.
|
|
|
|
*
|
|
|
|
* Defaults to `""` (empty string)
|
|
|
|
*/
|
|
|
|
query?: string
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Limits the number of messages to be retrieved.
|
|
|
|
*
|
|
|
|
* By default, no limit is applied and all messages are returned
|
|
|
|
*/
|
|
|
|
limit?: number
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Filter the results using some filter.
|
|
|
|
* Defaults to {@link SearchFilters.Empty} (i.e. will return all messages)
|
|
|
|
*
|
|
|
|
* @link SearchFilters
|
|
|
|
*/
|
|
|
|
filter?: tl.TypeMessagesFilter
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Chunk size, which will be passed as `limit` parameter
|
|
|
|
* for `messages.search`. Usually you shouldn't care about this.
|
|
|
|
*
|
|
|
|
* Defaults to `100`
|
|
|
|
*/
|
|
|
|
chunkSize?: number
|
|
|
|
}
|
|
|
|
): AsyncIterableIterator<Message> {
|
|
|
|
if (!params) params = {}
|
|
|
|
|
|
|
|
let current = 0
|
|
|
|
|
|
|
|
const total = params.limit || Infinity
|
|
|
|
const limit = Math.min(params.chunkSize || 100, total)
|
|
|
|
|
|
|
|
let offsetDate = 0
|
|
|
|
let offsetPeer = { _: 'inputPeerEmpty' } as tl.TypeInputPeer
|
|
|
|
let offsetId = 0
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
const res = await this.call({
|
|
|
|
_: 'messages.searchGlobal',
|
|
|
|
q: params.query || '',
|
|
|
|
filter: params.filter || SearchFilters.Empty,
|
|
|
|
minDate: 0,
|
|
|
|
maxDate: 0,
|
|
|
|
offsetId,
|
|
|
|
offsetRate: offsetDate,
|
|
|
|
offsetPeer: offsetPeer,
|
|
|
|
limit: Math.min(limit, total - current),
|
|
|
|
})
|
|
|
|
|
|
|
|
if (res._ === 'messages.messagesNotModified')
|
|
|
|
throw new MtCuteTypeAssertionError(
|
2021-05-12 18:16:50 +03:00
|
|
|
'messages.searchGlobal',
|
2021-04-09 13:08:32 +03:00
|
|
|
'!messages.messagesNotModified',
|
|
|
|
res._
|
|
|
|
)
|
|
|
|
|
|
|
|
const { users, chats } = createUsersChatsIndex(res)
|
|
|
|
|
|
|
|
const msgs = res.messages.map(
|
|
|
|
(msg) => new Message(this, msg, users, chats)
|
|
|
|
)
|
|
|
|
|
|
|
|
if (!msgs.length) break
|
|
|
|
|
|
|
|
const last = msgs[msgs.length - 1]
|
|
|
|
offsetDate = last.raw.date
|
|
|
|
offsetPeer = last.chat.inputPeer
|
|
|
|
offsetId = last.id
|
|
|
|
|
|
|
|
yield* msgs
|
|
|
|
|
|
|
|
current += msgs.length
|
|
|
|
if (current >= total) break
|
|
|
|
}
|
|
|
|
}
|