vaegram 0.2.0

Telegram MTProto client library for users and bots
Documentation
mod add_contact;
mod block_user;
mod delete_contacts;
mod export_no_paid_messages_token;
mod get_blocked_message_senders;
mod get_contacts;
mod get_contacts_count;
mod import_contacts;
mod import_no_paid_messages_token;
mod search_contacts;
mod set_contact_note;
mod unblock_user;

pub use add_contact::AddContact;
pub use block_user::BlockUser;
pub use export_no_paid_messages_token::ExportNoPaidMessagesToken;
pub use get_blocked_message_senders::GetBlockedMessageSenders;
pub use set_contact_note::SetContactNote;
pub use unblock_user::UnblockUser;

#[cfg(test)]
mod tests {
    use crate::Client;
    use crate::errors::Error;
    use crate::raw::functions::contacts;
    use crate::raw::{Identifiable, base, types};

    #[tokio::test]
    async fn contact_calls_reach_the_right_methods() {
        let (client, mut rx) = Client::recording();
        let user = types::InputPeerUser { user_id: 9, access_hash: 1 };
        client.0.peers.lock().unwrap().insert(9, base::InputPeer::InputPeerUser(Box::new(user)));
        let mut sent = || u32::from_le_bytes(rx.try_recv().expect("no request sent")[..4].try_into().unwrap());

        let _ = client.block_user(9_i64).await;
        assert_eq!(sent(), contacts::Block::CONSTRUCTOR_ID);
        let _ = client.unblock_user(9_i64).await;
        assert_eq!(sent(), contacts::Unblock::CONSTRUCTOR_ID);
        let _ = client.add_contact(9_i64, "Ann").share_phone_number().await;
        assert_eq!(sent(), contacts::AddContact::CONSTRUCTOR_ID);
        let _ = client.delete_contacts([9_i64]).await;
        assert_eq!(sent(), contacts::DeleteContacts::CONSTRUCTOR_ID);
        let _ = client.get_common_chats(9_i64).await;
        assert_eq!(sent(), crate::raw::functions::messages::GetCommonChats::CONSTRUCTOR_ID);
    }

    #[tokio::test]
    async fn block_list_sets_my_stories_from() {
        use crate::enums::BlockList;
        use crate::raw::Serializable;
        let (client, mut rx) = Client::recording();
        let user = types::InputPeerUser { user_id: 9, access_hash: 1 };
        let id = base::InputPeer::InputPeerUser(Box::new(user.clone()));
        client.0.peers.lock().unwrap().insert(9, id.clone());
        for (list, flag) in [(BlockList::Main, false), (BlockList::Stories, true)] {
            let _ = client.block_user(9_i64).block_list(list).await;
            assert_eq!(rx.try_recv().unwrap(), contacts::Block { my_stories_from: flag, id: id.clone() }.to_bytes());
            let _ = client.unblock_user(9_i64).block_list(list).await;
            assert_eq!(rx.try_recv().unwrap(), contacts::Unblock { my_stories_from: flag, id: id.clone() }.to_bytes());
            let _ = client.get_blocked_message_senders().offset(5).limit(10).block_list(list).await;
            let request = contacts::GetBlocked { my_stories_from: flag, offset: 5, limit: 10 };
            assert_eq!(rx.try_recv().unwrap(), request.to_bytes());
        }
    }

    #[tokio::test]
    async fn deleting_no_contacts_is_refused_without_a_request() {
        let (client, mut rx) = Client::recording();
        let result = client.delete_contacts(Vec::<i64>::new()).await;
        assert!(matches!(result, Err(Error::InvalidArgument(_))), "{result:?}");
        assert!(rx.try_recv().is_err());
    }
}