feat: added ArrayPaginated
This commit is contained in:
parent
82994408a2
commit
95032d3b9a
8 changed files with 62 additions and 25 deletions
|
@ -194,6 +194,7 @@ import { unblockUser } from './methods/users/unblock-user'
|
|||
import { updateProfile } from './methods/users/update-profile'
|
||||
import { updateUsername } from './methods/users/update-username'
|
||||
import {
|
||||
ArrayPaginated,
|
||||
ArrayWithTotal,
|
||||
BotChatJoinRequestUpdate,
|
||||
BotCommands,
|
||||
|
@ -1225,7 +1226,13 @@ export interface TelegramClient extends BaseTelegramClient {
|
|||
offset?: number
|
||||
|
||||
/**
|
||||
* Maximum number of members to be retrieved. Defaults to `200`
|
||||
* Maximum number of members to be retrieved.
|
||||
*
|
||||
* > **Note**: Telegram currently only allows you to ever retrieve at most
|
||||
* > 200 members, regardless of offset/limit. I.e. when passing
|
||||
* > `offset=201` nothing will ever be returned.
|
||||
*
|
||||
* @default 200
|
||||
*/
|
||||
limit?: number
|
||||
|
||||
|
@ -2005,7 +2012,7 @@ export interface TelegramClient extends BaseTelegramClient {
|
|||
*/
|
||||
requestedSearch?: string
|
||||
},
|
||||
): Promise<ArrayWithTotal<ChatInviteLinkMember>>
|
||||
): Promise<ArrayPaginated<ChatInviteLinkMember, { date: number; user: tl.TypeInputUser }>>
|
||||
/**
|
||||
* Get detailed information about an invite link
|
||||
*
|
||||
|
@ -2056,7 +2063,7 @@ export interface TelegramClient extends BaseTelegramClient {
|
|||
*/
|
||||
offsetLink?: string
|
||||
},
|
||||
): Promise<ArrayWithTotal<ChatInviteLink>>
|
||||
): Promise<ArrayPaginated<ChatInviteLink, { date: number; link: string }>>
|
||||
/**
|
||||
* Get primary invite link of a chat
|
||||
*
|
||||
|
|
|
@ -11,6 +11,7 @@ import { tdFileId } from '@mtcute/file-id'
|
|||
|
||||
// @copy
|
||||
import {
|
||||
ArrayPaginated,
|
||||
ArrayWithTotal,
|
||||
BotChatJoinRequestUpdate,
|
||||
BotCommands,
|
||||
|
|
|
@ -26,7 +26,7 @@ export async function getChatMembers(
|
|||
* Defaults to `''` (empty string)
|
||||
*
|
||||
* > **Note**: Only used for these values of `filter`:
|
||||
* > `all`, `banned`, `restricted`, `contacts`
|
||||
* > `all, banned, restricted, mention, contacts`
|
||||
*/
|
||||
query?: string
|
||||
|
||||
|
@ -36,7 +36,13 @@ export async function getChatMembers(
|
|||
offset?: number
|
||||
|
||||
/**
|
||||
* Maximum number of members to be retrieved. Defaults to `200`
|
||||
* Maximum number of members to be retrieved.
|
||||
*
|
||||
* > **Note**: Telegram currently only allows you to ever retrieve at most
|
||||
* > 200 members, regardless of offset/limit. I.e. when passing
|
||||
* > `offset=201` nothing will ever be returned.
|
||||
*
|
||||
* @default 200
|
||||
*/
|
||||
limit?: number
|
||||
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
import { tl } from '@mtcute/core'
|
||||
|
||||
import { TelegramClient } from '../../client'
|
||||
import { ArrayWithTotal, ChatInviteLinkMember, InputPeerLike, PeersIndex } from '../../types'
|
||||
import { makeArrayWithTotal, normalizeDate } from '../../utils'
|
||||
import { ArrayPaginated, ChatInviteLinkMember, InputPeerLike, PeersIndex } from '../../types'
|
||||
import { makeArrayPaginated, normalizeDate, normalizeToInputUser } from '../../utils'
|
||||
|
||||
/**
|
||||
* Iterate over users who have joined
|
||||
|
@ -52,7 +52,7 @@ export async function getInviteLinkMembers(
|
|||
*/
|
||||
requestedSearch?: string
|
||||
},
|
||||
): Promise<ArrayWithTotal<ChatInviteLinkMember>> {
|
||||
): Promise<ArrayPaginated<ChatInviteLinkMember, { date: number; user: tl.TypeInputUser }>> {
|
||||
const peer = await this.resolvePeer(chatId)
|
||||
|
||||
const { limit = 100, link, requestedSearch, requested = Boolean(requestedSearch) } = params
|
||||
|
@ -74,8 +74,15 @@ export async function getInviteLinkMembers(
|
|||
|
||||
const peers = PeersIndex.from(res)
|
||||
|
||||
return makeArrayWithTotal(
|
||||
res.importers.map((it) => new ChatInviteLinkMember(this, it, peers)),
|
||||
res.count,
|
||||
)
|
||||
const members = res.importers.map((it) => new ChatInviteLinkMember(this, it, peers))
|
||||
|
||||
const last = members[members.length - 1]
|
||||
const nextOffset = last ?
|
||||
{
|
||||
date: last.raw.date,
|
||||
user: normalizeToInputUser(last.user.inputPeer),
|
||||
} :
|
||||
undefined
|
||||
|
||||
return makeArrayPaginated(members, res.count, nextOffset)
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
import { TelegramClient } from '../../client'
|
||||
import { ArrayWithTotal, ChatInviteLink, InputPeerLike, PeersIndex } from '../../types'
|
||||
import { makeArrayWithTotal, normalizeDate } from '../../utils'
|
||||
import { ArrayPaginated, ChatInviteLink, InputPeerLike, PeersIndex } from '../../types'
|
||||
import { makeArrayPaginated, normalizeDate } from '../../utils'
|
||||
import { normalizeToInputUser } from '../../utils/peer-utils'
|
||||
|
||||
/**
|
||||
|
@ -48,7 +48,7 @@ export async function getInviteLinks(
|
|||
*/
|
||||
offsetLink?: string
|
||||
},
|
||||
): Promise<ArrayWithTotal<ChatInviteLink>> {
|
||||
): Promise<ArrayPaginated<ChatInviteLink, { date: number; link: string }>> {
|
||||
if (!params) params = {}
|
||||
|
||||
const { revoked = false, limit = Infinity, admin } = params
|
||||
|
@ -68,8 +68,15 @@ export async function getInviteLinks(
|
|||
|
||||
const peers = PeersIndex.from(res)
|
||||
|
||||
return makeArrayWithTotal(
|
||||
res.invites.map((it) => new ChatInviteLink(this, it, peers)),
|
||||
res.count,
|
||||
)
|
||||
const links = res.invites.map((it) => new ChatInviteLink(this, it, peers))
|
||||
|
||||
const last = links[links.length - 1]
|
||||
const nextOffset = last ?
|
||||
{
|
||||
date: last.raw.date,
|
||||
link: last.raw.link,
|
||||
} :
|
||||
undefined
|
||||
|
||||
return makeArrayPaginated(links, res.count, nextOffset)
|
||||
}
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
import { TelegramClient } from '../../client'
|
||||
import { ChatInviteLinkMember, InputPeerLike } from '../../types'
|
||||
import { normalizeToInputUser } from '../../utils'
|
||||
|
||||
/**
|
||||
* Iterate over users who have joined
|
||||
|
@ -50,14 +49,15 @@ export async function* iterInviteLinkMembers(
|
|||
|
||||
if (!items.length) break
|
||||
|
||||
const last = items[items.length - 1]
|
||||
offsetDate = last.date
|
||||
offsetUser = normalizeToInputUser(last.user.inputPeer)
|
||||
|
||||
for (const it of items) {
|
||||
yield it
|
||||
|
||||
if (++current >= limit) return
|
||||
}
|
||||
|
||||
if (!items.next) return
|
||||
|
||||
offsetDate = items.next.date
|
||||
offsetUser = items.next.user
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,3 +3,4 @@ import { MaybeAsync } from '@mtcute/core'
|
|||
export type MaybeDynamic<T> = MaybeAsync<T> | (() => MaybeAsync<T>)
|
||||
|
||||
export type ArrayWithTotal<T> = T[] & { total: number }
|
||||
export type ArrayPaginated<T, Offset> = T[] & { total: number; next?: Offset }
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
import { MtArgumentError, tl } from '@mtcute/core'
|
||||
|
||||
import { ArrayWithTotal, MaybeDynamic, Message } from '../types'
|
||||
import { ArrayPaginated, ArrayWithTotal, MaybeDynamic, Message } from '../types'
|
||||
|
||||
/**
|
||||
* Normalize phone number by stripping formatting
|
||||
|
@ -24,6 +24,14 @@ export function makeArrayWithTotal<T>(arr: T[], total: number): ArrayWithTotal<T
|
|||
return a
|
||||
}
|
||||
|
||||
export function makeArrayPaginated<T, Offset>(arr: T[], total: number, next?: Offset): ArrayPaginated<T, Offset> {
|
||||
const a = arr as ArrayPaginated<T, Offset>
|
||||
a.total = total
|
||||
a.next = next
|
||||
|
||||
return a
|
||||
}
|
||||
|
||||
export function extractChannelIdFromUpdate(upd: tl.TypeUpdate): number | undefined {
|
||||
// holy shit
|
||||
let res = 0
|
||||
|
|
Loading…
Reference in a new issue