mtcute/packages/client/src/methods/misc/normalize-privacy-rules.ts
Alina Sireneva a03d73503a refactor: initial support for esm
also fixed all circular imports and added checking for them via dpdm
2023-10-11 08:42:37 +03:00

50 lines
1.5 KiB
TypeScript

import { BaseTelegramClient, tl } from '@mtcute/core'
import { InputPrivacyRule } from '../../types'
import { normalizeToInputUser } from '../../utils'
import { resolvePeerMany } from '../users/resolve-peer-many'
/**
* Normalize {@link InputPrivacyRule}[] to `tl.TypeInputPrivacyRule`,
* resolving the peers if needed.
*/
export async function _normalizePrivacyRules(
client: BaseTelegramClient,
rules: InputPrivacyRule[],
): Promise<tl.TypeInputPrivacyRule[]> {
const res: tl.TypeInputPrivacyRule[] = []
for (const rule of rules) {
if ('_' in rule) {
res.push(rule)
continue
}
if ('users' in rule) {
const users = await resolvePeerMany(client, rule.users, normalizeToInputUser)
res.push({
_: rule.allow ? 'inputPrivacyValueAllowUsers' : 'inputPrivacyValueDisallowUsers',
users,
})
continue
}
if ('chats' in rule) {
const chats = await resolvePeerMany(client, rule.chats)
res.push({
_: rule.allow ? 'inputPrivacyValueAllowChatParticipants' : 'inputPrivacyValueDisallowChatParticipants',
chats: chats.map((peer) => {
if ('channelId' in peer) return peer.channelId
if ('chatId' in peer) return peer.chatId
throw new Error('UNREACHABLE')
}),
})
continue
}
}
return res
}