From ced6bd8e696211b18a11fa05dcac20ed6fe1cc45 Mon Sep 17 00:00:00 2001 From: nextel Date: Thu, 19 Sep 2019 17:57:50 +0300 Subject: [PATCH 1/7] add get user profile photos template struct --- src/core/requests/get_user_profile_photos.rs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/core/requests/get_user_profile_photos.rs b/src/core/requests/get_user_profile_photos.rs index 2dfed773..fb69133f 100644 --- a/src/core/requests/get_user_profile_photos.rs +++ b/src/core/requests/get_user_profile_photos.rs @@ -1,8 +1,16 @@ use crate::core::requests::RequestContext; -//TODO:: need implementation + +//TODO: complete implementation after user_profile_fotos will be added to types/mod.rs +///Use this method to get a list of profile pictures for a user. Returns a UserProfilePhotos object. #[derive(Debug, Clone, Serialize)] struct GetUserProfilePhotos<'a> { #[serde(skip_serializing)] ctx: RequestContext<'a>, + /// Unique identifier of the target user + user_id: i32, + /// Sequential number of the first photo to be returned. By default, all photos are returned. + offset: Option, + ///Limits the number of photos to be retrieved. Values between 1—100 are accepted. Defaults to 100. + limit: Option, } From ccbfee24b83eba5fd8062a08755c3e0bd34ef6f7 Mon Sep 17 00:00:00 2001 From: Mr-Andersen Date: Thu, 19 Sep 2019 18:24:11 +0300 Subject: [PATCH 2/7] +Message::text(&self); message_kind->kind; IncomingMessage->CommonMessage --- src/core/types/message.rs | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/core/types/message.rs b/src/core/types/message.rs index ebc76355..2252f54f 100644 --- a/src/core/types/message.rs +++ b/src/core/types/message.rs @@ -11,13 +11,26 @@ pub struct Message { pub date: i32, pub chat: Chat, #[serde(flatten)] - pub message_kind: MessageKind, + pub kind: MessageKind, +} + +impl Message { + fn text(&self) -> Option<&str> { + if let MessageKind::CommonMessage { + media_kind: MediaKind::Text { + ref text, .. + }, .. } = self.kind { + Some(text) + } else { + None + } + } } #[derive(Debug, Deserialize, PartialEq, Clone)] #[serde(untagged)] pub enum MessageKind { - IncomingMessage { + CommonMessage { #[serde(flatten)] from: Sender, #[serde(flatten)] From c395e24792d113dd8f67323cb08295e203a7bc4c Mon Sep 17 00:00:00 2001 From: P0lunin Date: Thu, 19 Sep 2019 19:37:48 +0300 Subject: [PATCH 3/7] add enum ChatMemberStatus --- src/core/types/chat_member.rs | 73 ++++++++++++++++++++++++- src/core/types/mod.rs | 2 +- src/core/types/not_implemented_types.rs | 3 - 3 files changed, 73 insertions(+), 5 deletions(-) diff --git a/src/core/types/chat_member.rs b/src/core/types/chat_member.rs index 600b7e2e..cee60bc6 100644 --- a/src/core/types/chat_member.rs +++ b/src/core/types/chat_member.rs @@ -1,4 +1,4 @@ -use crate::core::types::{ChatMemberStatus, User}; +use crate::core::types::User; /// This object contains information about one member of the chat. #[derive(Debug, Deserialize, Hash, PartialEq, Eq, Clone)] @@ -54,3 +54,74 @@ pub struct ChatMember { /// his messages, implies can_send_media_messages pub can_add_web_page_previews: Option, } + +#[derive(Deserialize, Debug, Hash, PartialEq, Eq, Clone)] +#[serde(rename_all = "snake_case")] +pub enum ChatMemberStatus { + Creator, + Administrator, + Member, + Restricted, + Left, + Kicked +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn deserialize() { + let json = r#"{ + "user":{ + "id":12345, + "is_bot":false, + "first_name":"firstName" + }, + "status":"creator", + "until_date":123456, + "can_be_edited":true, + "can_post_messages":true, + "can_edit_messages":true, + "can_delete_messages":true, + "can_restrict_members":true, + "can_promote_members":true, + "can_change_info":true, + "can_invite_users":true, + "can_pin_messages":true, + "is_member":true, + "can_send_messages":true, + "can_send_media_messages":true, + "can_send_polls":true, + "can_send_other_messages":true, + "can_add_web_page_previews":true + }"#; + let expected = ChatMember { + user: User { + id: 12345, + is_bot: false, + first_name: "firstName".to_string(), + last_name: None, + username: None, + language_code: None + }, + status: ChatMemberStatus::Creator, + until_date: Some(123456), + can_be_edited: Some(true), + can_change_info: Some(true), + can_post_messages: Some(true), + can_edit_messages: Some(true), + can_delete_messages: Some(true), + can_invite_users: Some(true), + can_restrict_members: Some(true), + can_pin_messages: Some(true), + can_promote_members: Some(true), + can_send_messages: Some(true), + can_send_media_messages: Some(true), + can_send_other_messages: Some(true), + can_add_web_page_previews: Some(true) + }; + let actual = serde_json::from_str::(&json).unwrap(); + assert_eq!(actual, expected) + } +} \ No newline at end of file diff --git a/src/core/types/mod.rs b/src/core/types/mod.rs index 42e9869a..27ba457b 100644 --- a/src/core/types/mod.rs +++ b/src/core/types/mod.rs @@ -4,7 +4,7 @@ pub use self::{ audio::Audio, callback_query::CallbackQuery, chat::{Chat, ChatKind, NonPrivateChatKind}, - chat_member::ChatMember, + chat_member::{ChatMember, ChatMemberStatus}, chat_permissions::ChatPermissions, chat_photo::ChatPhoto, chosen_inline_result::ChosenInlineResult, diff --git a/src/core/types/not_implemented_types.rs b/src/core/types/not_implemented_types.rs index c2700b2d..c1e9d2ba 100644 --- a/src/core/types/not_implemented_types.rs +++ b/src/core/types/not_implemented_types.rs @@ -1,5 +1,2 @@ #[derive(Debug, Deserialize, Eq, Hash, PartialEq, Serialize, Clone)] pub struct PassportData; - -#[derive(Debug, Deserialize, Eq, Hash, PartialEq, Serialize, Clone)] -pub struct ChatMemberStatus; From 7b638eb2c0a73b02b99d4ed04cb05a0e2cfe90a9 Mon Sep 17 00:00:00 2001 From: P0lunin Date: Thu, 19 Sep 2019 19:38:07 +0300 Subject: [PATCH 4/7] add tests --- src/core/types/audio.rs | 46 ++++++++++++++++++++++---- src/core/types/callback_query.rs | 57 ++++++++++++++++++++++++-------- src/core/types/user.rs | 27 +++++++++++++++ 3 files changed, 110 insertions(+), 20 deletions(-) diff --git a/src/core/types/audio.rs b/src/core/types/audio.rs index 304d4946..66cc0878 100644 --- a/src/core/types/audio.rs +++ b/src/core/types/audio.rs @@ -1,17 +1,51 @@ use crate::core::types::PhotoSize; -#[derive(Debug, Deserialize, Eq, Hash, PartialEq, Serialize, Clone)] +#[derive(Debug, Deserialize, Eq, Hash, PartialEq, Clone)] pub struct Audio { pub file_id: String, pub duration: u32, - #[serde(skip_serializing_if = "Option::is_none")] pub performer: Option, - #[serde(skip_serializing_if = "Option::is_none")] pub title: Option, - #[serde(skip_serializing_if = "Option::is_none")] pub mime_type: Option, - #[serde(skip_serializing_if = "Option::is_none")] pub file_size: Option, - #[serde(skip_serializing_if = "Option::is_none")] pub thumb: Option, } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn deserialize() { + let json = r#"{ + "file_id":"id", + "duration":60, + "performer":"Performer", + "title":"Title", + "mime_type":"MimeType", + "file_size":123456, + "thumb":{ + "file_id":"id", + "width":320, + "height":320, + "file_size":3452 + } + }"#; + let expected = Audio { + file_id: "id".to_string(), + duration: 60, + performer: Some("Performer".to_string()), + title: Some("Title".to_string()), + mime_type: Some("MimeType".to_string()), + file_size: Some(123456), + thumb: Some(PhotoSize { + file_id: "id".to_string(), + width: 320, + height: 320, + file_size: Some(3452) + }) + }; + let actual = serde_json::from_str::