2021-04-11 00:37:12 +03:00
|
|
|
import { InputPeerLike, MtCuteInvalidPeerTypeError } from '../../types'
|
|
|
|
import { TelegramClient } from '../../client'
|
|
|
|
import {
|
2021-05-11 23:21:35 +03:00
|
|
|
isInputPeerChannel, isInputPeerChat,
|
2021-04-11 00:37:12 +03:00
|
|
|
normalizeToInputChannel,
|
|
|
|
normalizeToInputPeer,
|
|
|
|
} from '../../utils/peer-utils'
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Leave a group chat, supergroup or channel
|
|
|
|
*
|
|
|
|
* @param chatId Chat ID or username
|
|
|
|
* @param clear Whether to clear history after leaving (only for legacy group chats)
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
export async function leaveChat(
|
|
|
|
this: TelegramClient,
|
|
|
|
chatId: InputPeerLike,
|
|
|
|
clear = false
|
|
|
|
): Promise<void> {
|
2021-05-11 23:21:35 +03:00
|
|
|
const chat = normalizeToInputPeer(await this.resolvePeer(chatId))
|
2021-04-11 00:37:12 +03:00
|
|
|
|
2021-05-11 23:21:35 +03:00
|
|
|
if (isInputPeerChannel(chat)) {
|
2021-04-24 16:23:30 +03:00
|
|
|
const res = await this.call({
|
2021-04-11 00:37:12 +03:00
|
|
|
_: 'channels.leaveChannel',
|
2021-05-11 23:21:35 +03:00
|
|
|
channel: normalizeToInputChannel(chat),
|
2021-04-11 00:37:12 +03:00
|
|
|
})
|
2021-04-24 16:23:30 +03:00
|
|
|
this._handleUpdate(res)
|
2021-05-11 23:21:35 +03:00
|
|
|
} else if (isInputPeerChat(chat)) {
|
2021-04-24 16:23:30 +03:00
|
|
|
const res = await this.call({
|
2021-04-11 00:37:12 +03:00
|
|
|
_: 'messages.deleteChatUser',
|
2021-05-11 23:21:35 +03:00
|
|
|
chatId: chat.chatId,
|
2021-04-11 00:37:12 +03:00
|
|
|
userId: { _: 'inputUserSelf' },
|
|
|
|
})
|
2021-04-24 16:23:30 +03:00
|
|
|
this._handleUpdate(res)
|
2021-04-11 00:37:12 +03:00
|
|
|
|
|
|
|
if (clear) {
|
2021-05-11 23:21:35 +03:00
|
|
|
await this.deleteHistory(chat)
|
2021-04-11 00:37:12 +03:00
|
|
|
}
|
|
|
|
} else throw new MtCuteInvalidPeerTypeError(chatId, 'chat or channel')
|
|
|
|
}
|