Skip to content

Commit

Permalink
Return Chat from accept_invite_link
Browse files Browse the repository at this point in the history
Closes #281.
  • Loading branch information
Lonami committed Oct 24, 2024
1 parent 8997a2d commit 724188f
Showing 1 changed file with 29 additions and 33 deletions.
62 changes: 29 additions & 33 deletions lib/grammers-client/src/client/chats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,25 @@ impl ProfilePhotoIter {
}
}

fn updates_to_chat(id: Option<i64>, updates: tl::enums::Updates) -> Option<Chat> {
use tl::enums::Updates;

let chats = match updates {
Updates::Combined(updates) => Some(updates.chats),
Updates::Updates(updates) => Some(updates.chats),
_ => None,
};

match chats {
Some(chats) => match id {
Some(id) => chats.into_iter().find(|chat| chat.id() == id),
None => chats.into_iter().next(),
},
None => None,
}
.map(Chat::from_raw)
}

/// Method implementations related to dealing with chats or other users.
impl Client {
/// Resolves a username into the chat that owns it, if any.
Expand Down Expand Up @@ -788,12 +807,13 @@ impl Client {
pub async fn accept_invite_link(
&self,
invite_link: &str,
) -> Result<tl::enums::Updates, InvocationError> {
) -> Result<Option<Chat>, InvocationError> {
match Self::parse_invite_link(invite_link) {
Some(hash) => {
Some(hash) => Ok(updates_to_chat(
None,
self.invoke(&tl::functions::messages::ImportChatInvite { hash })
.await
}
.await?,
)),
None => Err(InvocationError::Rpc(RpcError {
code: 400,
name: "INVITE_HASH_INVALID".to_string(),
Expand All @@ -815,38 +835,14 @@ impl Client {
&self,
chat: C,
) -> Result<Option<Chat>, InvocationError> {
use tl::enums::Updates;

let chat = chat.into();
let update_chat = match self
.invoke(&tl::functions::channels::JoinChannel {
Ok(updates_to_chat(
Some(chat.id),
self.invoke(&tl::functions::channels::JoinChannel {
channel: chat.try_to_input_channel().unwrap(),
})
.await?
{
Updates::Combined(updates) => Some(
updates
.chats
.into_iter()
.filter(|x| x.id() == chat.id)
.collect::<Vec<tl::enums::Chat>>(),
),
Updates::Updates(updates) => Some(
updates
.chats
.into_iter()
.filter(|x| x.id() == chat.id)
.collect::<Vec<tl::enums::Chat>>(),
),
_ => None,
};

match update_chat {
Some(chats) if !chats.is_empty() => Ok(Some(Chat::from_raw(chats[0].clone()))),
Some(chats) if chats.is_empty() => Ok(None),
None => Ok(None),
Some(_) => Ok(None),
}
.await?,
))
}

/// Send a message action (such as typing, uploading photo, or viewing an emoji interaction)
Expand Down

0 comments on commit 724188f

Please sign in to comment.