From 8211838fe92f0f5d9f6a4f97b387a93ccf1acfc9 Mon Sep 17 00:00:00 2001 From: Mr-Andersen Date: Sat, 21 Sep 2019 10:48:34 +0300 Subject: [PATCH 1/8] +PinChatMessage --- src/core/requests/mod.rs | 1 + src/core/requests/pin_chat_message.rs | 32 +++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 src/core/requests/pin_chat_message.rs diff --git a/src/core/requests/mod.rs b/src/core/requests/mod.rs index a1dfa82f..d04e113a 100644 --- a/src/core/requests/mod.rs +++ b/src/core/requests/mod.rs @@ -97,6 +97,7 @@ pub mod get_file; pub mod get_me; pub mod get_user_profile_photos; pub mod kick_chat_member; +pub mod pin_chat_message; pub mod restrict_chat_member; pub mod send_audio; pub mod send_chat_action; diff --git a/src/core/requests/pin_chat_message.rs b/src/core/requests/pin_chat_message.rs new file mode 100644 index 00000000..c1481d38 --- /dev/null +++ b/src/core/requests/pin_chat_message.rs @@ -0,0 +1,32 @@ +use crate::core::requests::{ChatId, RequestContext, RequestFuture, ResponseResult, Request}; +use crate::core::network; + +/// Use this method to get up to date information about the chat +/// (current name of the user for one-on-one conversations, +/// current username of a user, group or channel, etc.). +/// Returns a Chat object on success. +#[derive(Debug, Clone, Serialize)] +pub struct PinChatMessage<'a> { + #[serde(skip_serializing)] + ctx: RequestContext<'a>, + /// Unique identifier for the target chat or username + /// of the target supergroup or channel (in the format @channelusername) + chat_id: ChatId, + message_id: i32, + disable_notification: bool +} + +impl<'a> Request<'a> for PinChatMessage<'a> { + type ReturnValue = bool; + + fn send(self) -> RequestFuture<'a, ResponseResult> { + Box::pin(async move { + network::request_json( + &self.ctx.client, + &self.ctx.token, + "pinChatMessage", + &self, + ).await + }) + } +} From fc68705d71b67cc6b96f1737626f2c252f36cdfd Mon Sep 17 00:00:00 2001 From: Mr-Andersen Date: Sat, 21 Sep 2019 11:38:58 +0300 Subject: [PATCH 2/8] +EncryptedPassportElement --- src/requests/pin_chat_message.rs | 17 +++- src/types/encrypted_passport_element.rs | 128 +++++++++++------------- src/types/message.rs | 2 +- src/types/mod.rs | 2 +- 4 files changed, 77 insertions(+), 72 deletions(-) diff --git a/src/requests/pin_chat_message.rs b/src/requests/pin_chat_message.rs index c1481d38..6f5db2a6 100644 --- a/src/requests/pin_chat_message.rs +++ b/src/requests/pin_chat_message.rs @@ -1,5 +1,5 @@ -use crate::core::requests::{ChatId, RequestContext, RequestFuture, ResponseResult, Request}; -use crate::core::network; +use crate::requests::{ChatId, RequestContext, RequestFuture, ResponseResult, Request}; +use crate::network; /// Use this method to get up to date information about the chat /// (current name of the user for one-on-one conversations, @@ -16,6 +16,19 @@ pub struct PinChatMessage<'a> { disable_notification: bool } +impl<'a> PinChatMessage<'a> { + pub(crate) fn new( + ctx: RequestContext<'a>, chat_id: ChatId, message_id: i32 + ) -> Self { + Self { ctx, chat_id, message_id, disable_notification: false } + } + + pub fn disable_notification(mut self) -> Self { + self.disable_notification = true; + self + } +} + impl<'a> Request<'a> for PinChatMessage<'a> { type ReturnValue = bool; diff --git a/src/types/encrypted_passport_element.rs b/src/types/encrypted_passport_element.rs index 0a013bf1..dbb3a6de 100644 --- a/src/types/encrypted_passport_element.rs +++ b/src/types/encrypted_passport_element.rs @@ -1,75 +1,67 @@ use super::passport_file::PassportFile; -#[derive(Debug, Deserialize, Eq, Hash, PartialEq, Clone, Serialize)] +#[derive(Clone, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)] pub struct EncryptedPassportElement { - #[serde(rename = "type")] - pub element_type: ElementType, - pub data: String, - pub phone_number: String, - pub email: String, - #[serde(skip_serializing_if = "Option::is_none")] - pub files: Option>, - #[serde(skip_serializing_if = "Option::is_none")] - pub front_size: Option, - #[serde(skip_serializing_if = "Option::is_none")] - pub reverse_side: Option, - #[serde(skip_serializing_if = "Option::is_none")] - pub selfie: Option, - #[serde(skip_serializing_if = "Option::is_none")] - pub translation: Option>, + pub hash: String, + #[serde(flatten)] + pub kind: EncryptedPassportElementKind } -#[derive(Debug, Deserialize, Eq, Hash, PartialEq, Clone, Serialize)] +#[derive(Clone, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)] #[serde(rename_all = "snake_case")] -pub enum ElementType { - PersonalData, - Passport, - DriverLicense, - IdentityCard, - Address, - UtilityBill, - BankStatement, - RentalAgreement, - PassportRegistration, - TemporaryRegistration, - PhoneNumber, - Email, -} - -#[cfg(test)] -mod tests { - use super::super::{ElementType, EncryptedPassportElement, PassportFile}; - #[test] - fn must_serialize_encrypted_passport_element_to_json() { - // given - let expected_json = r#" - { - "type":"passport_registration", - "data":"somedata", - "phone_number":"1313", - "email":"someemail", - "front_size":{"file_id":"someId","file_size":13,"file_date":13} - }"# - .replace("\n", "") - .replace(" ", ""); - - let passport_element = EncryptedPassportElement { - element_type: ElementType::PassportRegistration, - data: "somedata".to_string(), - phone_number: "1313".to_string(), - email: "someemail".to_string(), - files: None, - front_size: Some(PassportFile { - file_id: "someId".to_string(), - file_size: 13, - file_date: 13, - }), - reverse_side: None, - selfie: None, - translation: None, - }; - - let actual_json = serde_json::to_string(&passport_element).unwrap(); - assert_eq!(actual_json, expected_json) - } +pub enum EncryptedPassportElementKind { + PersonalDetails { + data: String + }, + Passport { + data: String, + front_side: PassportFile, + selfie: PassportFile, + translation: Vec + }, + DriverLicense { + data: String, + front_side: PassportFile, + reverse_side: PassportFile, + selfie: PassportFile, + translation: Vec + }, + IdentityCard { + data: String, + front_side: PassportFile, + reverse_side: PassportFile, + selfie: PassportFile, + translation: Vec + }, + InternalPassport { + data: String, + front_side: PassportFile, + selfie: PassportFile, + translation: Vec + }, + Address { + data: String + }, + UtilityBill { + files: Vec, + translation: Vec + }, + BankStatement { + files: Vec, + translation: Vec + }, + RentalAgreement { + files: Vec, + translation: Vec + }, + PassportRegistration { + files: Vec, + translation: Vec + }, + TemporaryRegistration { + files: Vec, + translation: Vec + }, + PhoneNumber { phone_number: String }, + Email { email: String } } diff --git a/src/types/message.rs b/src/types/message.rs index 905d79d4..2bc8a102 100644 --- a/src/types/message.rs +++ b/src/types/message.rs @@ -15,7 +15,7 @@ pub struct Message { } impl Message { - fn text(&self) -> Option<&str> { + pub fn text(&self) -> Option<&str> { if let MessageKind::Common { media_kind: MediaKind::Text { ref text, .. }, .. diff --git a/src/types/mod.rs b/src/types/mod.rs index 893d492f..2c1c221b 100644 --- a/src/types/mod.rs +++ b/src/types/mod.rs @@ -12,7 +12,7 @@ pub use self::{ contact::Contact, document::Document, encrypted_credintials::EncryptedCredentials, - encrypted_passport_element::{ElementType, EncryptedPassportElement}, + encrypted_passport_element::{EncryptedPassportElement, EncryptedPassportElementKind}, file::File, force_reply::ForceReply, game::Game, From b7e0a1c7d1849b6f30e3793b6527248fcfafe721 Mon Sep 17 00:00:00 2001 From: Mr-Andersen Date: Sat, 21 Sep 2019 11:52:59 +0300 Subject: [PATCH 3/8] In EncryptedMessageElementKind::translation: Vec<_> -> Option> --- src/types/encrypted_passport_element.rs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/types/encrypted_passport_element.rs b/src/types/encrypted_passport_element.rs index dbb3a6de..61b1fa47 100644 --- a/src/types/encrypted_passport_element.rs +++ b/src/types/encrypted_passport_element.rs @@ -17,50 +17,50 @@ pub enum EncryptedPassportElementKind { data: String, front_side: PassportFile, selfie: PassportFile, - translation: Vec + translation: Option> }, DriverLicense { data: String, front_side: PassportFile, reverse_side: PassportFile, selfie: PassportFile, - translation: Vec + translation: Option> }, IdentityCard { data: String, front_side: PassportFile, reverse_side: PassportFile, selfie: PassportFile, - translation: Vec + translation: Option> }, InternalPassport { data: String, front_side: PassportFile, selfie: PassportFile, - translation: Vec + translation: Option> }, Address { data: String }, UtilityBill { files: Vec, - translation: Vec + translation: Option> }, BankStatement { files: Vec, - translation: Vec + translation: Option> }, RentalAgreement { files: Vec, - translation: Vec + translation: Option> }, PassportRegistration { files: Vec, - translation: Vec + translation: Option> }, TemporaryRegistration { files: Vec, - translation: Vec + translation: Option> }, PhoneNumber { phone_number: String }, Email { email: String } From 02d97966676bc3770285609202e1d3f905e4d4a9 Mon Sep 17 00:00:00 2001 From: Mr-Andersen Date: Sat, 21 Sep 2019 12:05:13 +0300 Subject: [PATCH 4/8] request/PinChatMessage pub fixes --- src/requests/pin_chat_message.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/requests/pin_chat_message.rs b/src/requests/pin_chat_message.rs index 6f5db2a6..8f8a955c 100644 --- a/src/requests/pin_chat_message.rs +++ b/src/requests/pin_chat_message.rs @@ -11,9 +11,9 @@ pub struct PinChatMessage<'a> { ctx: RequestContext<'a>, /// Unique identifier for the target chat or username /// of the target supergroup or channel (in the format @channelusername) - chat_id: ChatId, - message_id: i32, - disable_notification: bool + pub chat_id: ChatId, + pub message_id: i32, + pub disable_notification: bool } impl<'a> PinChatMessage<'a> { From b0aff65f71b2517a11ca5115270316c765181248 Mon Sep 17 00:00:00 2001 From: Mr-Andersen Date: Sat, 21 Sep 2019 12:08:19 +0300 Subject: [PATCH 5/8] request/PinChatMessage added TODO --- src/requests/pin_chat_message.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/requests/pin_chat_message.rs b/src/requests/pin_chat_message.rs index 8f8a955c..5b9ddfde 100644 --- a/src/requests/pin_chat_message.rs +++ b/src/requests/pin_chat_message.rs @@ -30,7 +30,7 @@ impl<'a> PinChatMessage<'a> { } impl<'a> Request<'a> for PinChatMessage<'a> { - type ReturnValue = bool; + type ReturnValue = bool; // TODO: change to unit type True fn send(self) -> RequestFuture<'a, ResponseResult> { Box::pin(async move { From b500296ed60501297af82288cfaaf1e792db03b7 Mon Sep 17 00:00:00 2001 From: Mr-Andersen Date: Sat, 21 Sep 2019 12:18:42 +0300 Subject: [PATCH 6/8] `pub use`d requests/PinChatMessage in requests/mod --- src/requests/mod.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/requests/mod.rs b/src/requests/mod.rs index ede9b45a..04c2251b 100644 --- a/src/requests/mod.rs +++ b/src/requests/mod.rs @@ -14,7 +14,8 @@ pub use self::{ forward_message::ForwardMessage, get_chat::GetChat, get_file::GetFile, get_me::GetMe, get_updates::GetUpdates, get_user_profile_photos::GetUserProfilePhotos, - kick_chat_member::KickChatMember, restrict_chat_member::RestrictChatMember, + kick_chat_member::KickChatMember, pin_chat_message::PinChatMessage, + restrict_chat_member::RestrictChatMember, send_audio::SendAudio, send_chat_action::SendChatAction, send_contact::SendContact, send_location::SendLocation, send_media_group::SendMediaGroup, send_message::SendMessage, From 695ea93e067a7d47a7f753fc20aec98a214784e0 Mon Sep 17 00:00:00 2001 From: Mr-Andersen Date: Tue, 24 Sep 2019 19:32:57 +0300 Subject: [PATCH 7/8] bool -> Option --- src/requests/pin_chat_message.rs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/requests/pin_chat_message.rs b/src/requests/pin_chat_message.rs index 5b9ddfde..f50110b2 100644 --- a/src/requests/pin_chat_message.rs +++ b/src/requests/pin_chat_message.rs @@ -13,18 +13,20 @@ pub struct PinChatMessage<'a> { /// of the target supergroup or channel (in the format @channelusername) pub chat_id: ChatId, pub message_id: i32, - pub disable_notification: bool + pub disable_notification: Option } impl<'a> PinChatMessage<'a> { pub(crate) fn new( ctx: RequestContext<'a>, chat_id: ChatId, message_id: i32 ) -> Self { - Self { ctx, chat_id, message_id, disable_notification: false } + Self { ctx, chat_id, message_id, disable_notification: None } } - pub fn disable_notification(mut self) -> Self { - self.disable_notification = true; + pub fn disable_notification(mut self, val: T) -> Self + where T: Into + { + self.disable_notification = Some(val.into()); self } } From e813953b00c802c6d11ec31136cbc76f8fd6fa4e Mon Sep 17 00:00:00 2001 From: Mr-Andersen Date: Tue, 24 Sep 2019 19:56:57 +0300 Subject: [PATCH 8/8] Deleted test --- src/types/passport_data.rs | 60 -------------------------------------- 1 file changed, 60 deletions(-) diff --git a/src/types/passport_data.rs b/src/types/passport_data.rs index 9c7ffc89..8ca8e20c 100644 --- a/src/types/passport_data.rs +++ b/src/types/passport_data.rs @@ -6,63 +6,3 @@ pub struct PassportData { pub data: Vec, pub credentials: EncryptedCredentials, } - -#[cfg(test)] -mod tests { - use super::super::{ElementType, PassportFile}; - use super::*; - - #[test] - fn must_serialize_passport_data_to_json() { - let expected_json = r#"{ - "data": - [ - { - "type":"passport_registration", - "data":"somedata", - "phone_number":"1313", - "email":"someemail", - "front_size": - { - "file_id":"someId", - "file_size":13, - "file_date":13 - } - } - ], - "credentials": - { - "data":"someData", - "hash":"1122", - "secret":"secret" - } - }"# - .replace("\n", "") - .replace(" ", ""); - let passport_data = PassportData { - data: vec![EncryptedPassportElement { - element_type: ElementType::PassportRegistration, - data: "somedata".to_string(), - phone_number: "1313".to_string(), - email: "someemail".to_string(), - files: None, - front_size: Some(PassportFile { - file_id: "someId".to_string(), - file_size: 13, - file_date: 13, - }), - reverse_side: None, - selfie: None, - translation: None, - }], - credentials: EncryptedCredentials { - data: "someData".to_string(), - hash: "1122".to_string(), - secret: "secret".to_string(), - }, - }; - - let actual_json = serde_json::to_string(&passport_data).unwrap(); - assert_eq!(actual_json, expected_json) - } -}