mtcute/packages/client/src/methods/misc/normalize-privacy-rules.ts

51 lines
1.5 KiB
TypeScript
Raw Normal View History

import { BaseTelegramClient, tl } from '@mtcute/core'
2023-10-04 19:26:21 +03:00
import { InputPrivacyRule } from '../../types'
import { normalizeToInputUser } from '../../utils'
import { resolvePeerMany } from '../users/resolve-peer-many'
2023-10-04 19:26:21 +03:00
/**
* Normalize {@link InputPrivacyRule}[] to `tl.TypeInputPrivacyRule`,
* resolving the peers if needed.
*/
export async function _normalizePrivacyRules(
client: BaseTelegramClient,
2023-10-04 19:26:21 +03:00
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)
2023-10-04 19:26:21 +03:00
res.push({
_: rule.allow ? 'inputPrivacyValueAllowUsers' : 'inputPrivacyValueDisallowUsers',
users,
})
continue
}
if ('chats' in rule) {
const chats = await resolvePeerMany(client, rule.chats)
2023-10-04 19:26:21 +03:00
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
}