chore(core)!: resolvePeerMany now returns null
on fail
This commit is contained in:
parent
ef15b846a9
commit
cc71f41a9a
4 changed files with 41 additions and 14 deletions
|
@ -1,4 +1,5 @@
|
||||||
import { MaybeArray } from '../../../types/utils.js'
|
import { MaybeArray } from '../../../types/utils.js'
|
||||||
|
import { isPresent } from '../../../utils/type-assertions.js'
|
||||||
import { ITelegramClient } from '../../client.types.js'
|
import { ITelegramClient } from '../../client.types.js'
|
||||||
import { InputPeerLike } from '../../types/index.js'
|
import { InputPeerLike } from '../../types/index.js'
|
||||||
import { resolvePeerMany } from '../users/resolve-peer-many.js'
|
import { resolvePeerMany } from '../users/resolve-peer-many.js'
|
||||||
|
@ -15,7 +16,7 @@ export async function archiveChats(client: ITelegramClient, chats: MaybeArray<In
|
||||||
|
|
||||||
const updates = await client.call({
|
const updates = await client.call({
|
||||||
_: 'folders.editPeerFolders',
|
_: 'folders.editPeerFolders',
|
||||||
folderPeers: resolvedPeers.map((peer) => ({
|
folderPeers: resolvedPeers.filter(isPresent).map((peer) => ({
|
||||||
_: 'inputFolderPeer',
|
_: 'inputFolderPeer',
|
||||||
peer,
|
peer,
|
||||||
folderId: 1,
|
folderId: 1,
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
import { MaybeArray } from '../../../types/utils.js'
|
import { MaybeArray } from '../../../types/utils.js'
|
||||||
|
import { isPresent } from '../../../utils/type-assertions.js'
|
||||||
import { ITelegramClient } from '../../client.types.js'
|
import { ITelegramClient } from '../../client.types.js'
|
||||||
import { Dialog } from '../../types/messages/dialog.js'
|
import { Dialog } from '../../types/messages/dialog.js'
|
||||||
import { InputPeerLike } from '../../types/peers/index.js'
|
import { InputPeerLike } from '../../types/peers/index.js'
|
||||||
|
@ -15,7 +16,7 @@ export async function getPeerDialogs(client: ITelegramClient, peers: MaybeArray<
|
||||||
const res = await client.call({
|
const res = await client.call({
|
||||||
_: 'messages.getPeerDialogs',
|
_: 'messages.getPeerDialogs',
|
||||||
peers: await resolvePeerMany(client, peers).then((peers) =>
|
peers: await resolvePeerMany(client, peers).then((peers) =>
|
||||||
peers.map((it) => ({
|
peers.filter(isPresent).map((it) => ({
|
||||||
_: 'inputDialogPeer',
|
_: 'inputDialogPeer',
|
||||||
peer: it,
|
peer: it,
|
||||||
})),
|
})),
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
import { tl } from '@mtcute/tl'
|
import { tl } from '@mtcute/tl'
|
||||||
|
|
||||||
|
import { isPresent } from '../../../utils/type-assertions.js'
|
||||||
import { ITelegramClient } from '../../client.types.js'
|
import { ITelegramClient } from '../../client.types.js'
|
||||||
import { InputPrivacyRule } from '../../types/index.js'
|
import { InputPrivacyRule } from '../../types/index.js'
|
||||||
import { toInputUser } from '../../utils/index.js'
|
import { toInputUser } from '../../utils/index.js'
|
||||||
|
@ -36,7 +37,7 @@ export async function _normalizePrivacyRules(
|
||||||
|
|
||||||
res.push({
|
res.push({
|
||||||
_: rule.allow ? 'inputPrivacyValueAllowChatParticipants' : 'inputPrivacyValueDisallowChatParticipants',
|
_: rule.allow ? 'inputPrivacyValueAllowChatParticipants' : 'inputPrivacyValueDisallowChatParticipants',
|
||||||
chats: chats.map((peer) => {
|
chats: chats.filter(isPresent).map((peer) => {
|
||||||
if ('channelId' in peer) return peer.channelId
|
if ('channelId' in peer) return peer.channelId
|
||||||
if ('chatId' in peer) return peer.chatId
|
if ('chatId' in peer) return peer.chatId
|
||||||
|
|
||||||
|
|
|
@ -2,7 +2,8 @@ import { tl } from '@mtcute/tl'
|
||||||
|
|
||||||
import { ConditionVariable } from '../../../utils/condition-variable.js'
|
import { ConditionVariable } from '../../../utils/condition-variable.js'
|
||||||
import { ITelegramClient } from '../../client.types.js'
|
import { ITelegramClient } from '../../client.types.js'
|
||||||
import { InputPeerLike } from '../../types/index.js'
|
import { MtPeerNotFoundError } from '../../types/errors.js'
|
||||||
|
import { InputPeerLike } from '../../types/peers/index.js'
|
||||||
import { resolvePeer } from './resolve-peer.js'
|
import { resolvePeer } from './resolve-peer.js'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -10,6 +11,8 @@ import { resolvePeer } from './resolve-peer.js'
|
||||||
* while also normalizing and removing
|
* while also normalizing and removing
|
||||||
* peers that can't be normalized to that type.
|
* peers that can't be normalized to that type.
|
||||||
*
|
*
|
||||||
|
* If a peer was not found, it will be skipped.
|
||||||
|
*
|
||||||
* Uses async pool internally, with a concurrent limit of 8
|
* Uses async pool internally, with a concurrent limit of 8
|
||||||
*
|
*
|
||||||
* @param peerIds Peer Ids
|
* @param peerIds Peer Ids
|
||||||
|
@ -24,11 +27,16 @@ export async function resolvePeerMany<T extends tl.TypeInputPeer | tl.TypeInputU
|
||||||
/**
|
/**
|
||||||
* Get multiple `InputPeer`s at once.
|
* Get multiple `InputPeer`s at once.
|
||||||
*
|
*
|
||||||
|
* If a peer was not found, `null` will be returned instead
|
||||||
|
*
|
||||||
* Uses async pool internally, with a concurrent limit of 8
|
* Uses async pool internally, with a concurrent limit of 8
|
||||||
*
|
*
|
||||||
* @param peerIds Peer Ids
|
* @param peerIds Peer Ids
|
||||||
*/
|
*/
|
||||||
export async function resolvePeerMany(client: ITelegramClient, peerIds: InputPeerLike[]): Promise<tl.TypeInputPeer[]>
|
export async function resolvePeerMany(
|
||||||
|
client: ITelegramClient,
|
||||||
|
peerIds: InputPeerLike[],
|
||||||
|
): Promise<(tl.TypeInputPeer | null)[]>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @internal
|
* @internal
|
||||||
|
@ -37,18 +45,28 @@ export async function resolvePeerMany(
|
||||||
client: ITelegramClient,
|
client: ITelegramClient,
|
||||||
peerIds: InputPeerLike[],
|
peerIds: InputPeerLike[],
|
||||||
normalizer?: (obj: tl.TypeInputPeer) => tl.TypeInputPeer | tl.TypeInputUser | tl.TypeInputChannel | null,
|
normalizer?: (obj: tl.TypeInputPeer) => tl.TypeInputPeer | tl.TypeInputUser | tl.TypeInputChannel | null,
|
||||||
): Promise<(tl.TypeInputPeer | tl.TypeInputUser | tl.TypeInputChannel)[]> {
|
): Promise<(tl.TypeInputPeer | tl.TypeInputUser | tl.TypeInputChannel | null)[]> {
|
||||||
const ret: (tl.TypeInputPeer | tl.TypeInputUser | tl.TypeInputChannel)[] = []
|
const ret: (tl.TypeInputPeer | tl.TypeInputUser | tl.TypeInputChannel | null)[] = []
|
||||||
|
|
||||||
const limit = 8
|
const limit = 8
|
||||||
|
|
||||||
if (peerIds.length < limit) {
|
if (peerIds.length < limit) {
|
||||||
// no point in using async pool for <limit peers
|
// no point in using async pool for <limit peers
|
||||||
const res = await Promise.all(peerIds.map((it) => resolvePeer(client, it)))
|
const res = await Promise.all(
|
||||||
|
peerIds.map((it) =>
|
||||||
|
resolvePeer(client, it).catch((e) => {
|
||||||
|
if (e instanceof MtPeerNotFoundError) {
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
throw e
|
||||||
|
}),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
if (!normalizer) return res
|
if (!normalizer) return res
|
||||||
|
|
||||||
for (const value of res) {
|
for (const value of res) {
|
||||||
|
if (!value) continue
|
||||||
const norm = normalizer(value)
|
const norm = normalizer(value)
|
||||||
|
|
||||||
if (norm) {
|
if (norm) {
|
||||||
|
@ -66,9 +84,14 @@ export async function resolvePeerMany(
|
||||||
let nextWorkerIdx = 0
|
let nextWorkerIdx = 0
|
||||||
|
|
||||||
const fetchNext = async (idx = nextWorkerIdx++): Promise<void> => {
|
const fetchNext = async (idx = nextWorkerIdx++): Promise<void> => {
|
||||||
|
try {
|
||||||
const result = await resolvePeer(client, peerIds[idx])
|
const result = await resolvePeer(client, peerIds[idx])
|
||||||
|
|
||||||
buffer[idx] = result
|
buffer[idx] = result
|
||||||
|
} catch (e) {
|
||||||
|
if (e instanceof MtPeerNotFoundError) {
|
||||||
|
buffer[idx] = null
|
||||||
|
} else throw e
|
||||||
|
}
|
||||||
|
|
||||||
if (nextIdx === idx) {
|
if (nextIdx === idx) {
|
||||||
cv.notify()
|
cv.notify()
|
||||||
|
@ -98,11 +121,12 @@ export async function resolvePeerMany(
|
||||||
|
|
||||||
nextIdx++
|
nextIdx++
|
||||||
|
|
||||||
if (!buf) continue
|
|
||||||
|
|
||||||
if (!normalizer) {
|
if (!normalizer) {
|
||||||
ret.push(buf)
|
ret.push(buf)
|
||||||
} else {
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if (buf !== null) {
|
||||||
const norm = normalizer(buf)
|
const norm = normalizer(buf)
|
||||||
|
|
||||||
if (norm) {
|
if (norm) {
|
||||||
|
|
Loading…
Reference in a new issue