use crate::*;
#[allow(unused_imports)]
use crate::{
InputMessage, InvocationError, PeerRef,
dialog::{Dialog, DialogIter, MessageIter},
inline_iter, media, participants, search, update,
};
use ferogram_tl_types::{Cursor, Deserializable};
impl Client {
pub async fn delete_channel(&self, peer: impl Into<PeerRef>) -> Result<(), InvocationError> {
let peer = peer.into().resolve(self).await?;
let input_peer = self.inner.peer_cache.read().await.peer_to_input(&peer)?;
let channel = match &input_peer {
tl::enums::InputPeer::Channel(c) => {
tl::enums::InputChannel::InputChannel(tl::types::InputChannel {
channel_id: c.channel_id,
access_hash: c.access_hash,
})
}
_ => {
return Err(InvocationError::Deserialize(
"delete_channel: peer must be a channel or supergroup".into(),
));
}
};
let req = tl::functions::channels::DeleteChannel { channel };
self.rpc_write(&req).await
}
pub async fn delete_chat(&self, chat_id: i64) -> Result<(), InvocationError> {
let req = tl::functions::messages::DeleteChat { chat_id };
self.rpc_write(&req).await
}
pub async fn leave_chat(&self, peer: impl Into<PeerRef>) -> Result<(), InvocationError> {
let peer = peer.into().resolve(self).await?;
let input_peer = self.inner.peer_cache.read().await.peer_to_input(&peer)?;
let channel = match &input_peer {
tl::enums::InputPeer::Channel(c) => {
tl::enums::InputChannel::InputChannel(tl::types::InputChannel {
channel_id: c.channel_id,
access_hash: c.access_hash,
})
}
_ => {
return Err(InvocationError::Deserialize(
"leave_chat: peer must be a channel or supergroup".into(),
));
}
};
let req = tl::functions::channels::LeaveChannel { channel };
self.rpc_write(&req).await
}
pub async fn migrate_chat(&self, chat_id: i64) -> Result<tl::enums::Chat, InvocationError> {
let req = tl::functions::messages::MigrateChat { chat_id };
let body: Vec<u8> = self.rpc_call_raw(&req).await?;
let mut cur = Cursor::from_slice(&body);
let updates = tl::enums::Updates::deserialize(&mut cur)?;
let chats = match updates {
tl::enums::Updates::Updates(u) => u.chats,
tl::enums::Updates::Combined(u) => u.chats,
_ => vec![],
};
chats
.into_iter()
.find(|c| matches!(c, tl::enums::Chat::Channel(_)))
.ok_or_else(|| {
InvocationError::Deserialize("migrate_chat: no channel in response".into())
})
}
}