From c3f543786d35495e141751189ef69a0b0bd2ba31 Mon Sep 17 00:00:00 2001 From: nextel Date: Sat, 21 Sep 2019 20:03:57 +0300 Subject: [PATCH 1/9] add get user profile photos action --- src/requests/get_user_profile_photos.rs | 49 +++++++++++++++++++++++-- 1 file changed, 46 insertions(+), 3 deletions(-) diff --git a/src/requests/get_user_profile_photos.rs b/src/requests/get_user_profile_photos.rs index 5feca84e..619dde88 100644 --- a/src/requests/get_user_profile_photos.rs +++ b/src/requests/get_user_profile_photos.rs @@ -1,7 +1,7 @@ -use crate::requests::RequestContext; +use crate::network; +use crate::requests::{Request, RequestContext, RequestFuture, ResponseResult}; +use crate::types::UserProfilePhotos; -//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)] @@ -17,3 +17,46 @@ pub struct GetUserProfilePhotos<'a> { /// accepted. Defaults to 100. limit: Option, } + +impl<'a> Request<'a> for GetUserProfilePhotos<'a> { + type ReturnValue = UserProfilePhotos; + + fn send(self) -> RequestFuture<'a, ResponseResult> { + Box::pin(async move { + network::request_json( + &self.ctx.client, + &self.ctx.token, + "getUserProfilePhotos", + &self, + ) + .await + }) + } +} + +impl<'a> GetUserProfilePhotos<'a> { + pub fn new(ctx: RequestContext<'a>, user_id: i32) -> Self { + Self { + ctx, + user_id, + offset: None, + limit: None, + } + } + + pub fn user_id(mut self, user_id: T) -> Self + where + T: Into, + { + self.user_id = user_id.into(); + self + } + + pub fn offset(mut self, offset: T) -> Self + where + T: Into, + { + self.offset = Some(offset.into()); + self + } +} From 23f2b9494d58ad963b3335427bbfe4e086ff6cfd Mon Sep 17 00:00:00 2001 From: nextel Date: Sat, 21 Sep 2019 20:08:06 +0300 Subject: [PATCH 2/9] add action template --- src/requests/kick_chat_member.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/requests/kick_chat_member.rs b/src/requests/kick_chat_member.rs index baabe494..a07f2252 100644 --- a/src/requests/kick_chat_member.rs +++ b/src/requests/kick_chat_member.rs @@ -9,4 +9,8 @@ use crate::requests::RequestContext; pub struct KickChatMember<'a> { #[serde(skip_serializing)] ctx: RequestContext<'a>, + + chat_id Integer or String Yes Unique identifier for the target group or username of the target supergroup or channel (in the format @channelusername) + user_id Integer Yes Unique identifier of the target user + until_date Integer Optional Date when the user will be unbanned, unix time. If user is banned for more than 366 days or less than 30 seconds from the current time they are considered to be banned forever } From 3443942cf9b5e9d8999fc7749b67a06886e9f447 Mon Sep 17 00:00:00 2001 From: nextel Date: Sat, 21 Sep 2019 20:09:35 +0300 Subject: [PATCH 3/9] fix optional value --- src/requests/get_user_profile_photos.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/requests/get_user_profile_photos.rs b/src/requests/get_user_profile_photos.rs index 619dde88..9e84d935 100644 --- a/src/requests/get_user_profile_photos.rs +++ b/src/requests/get_user_profile_photos.rs @@ -12,9 +12,11 @@ pub struct GetUserProfilePhotos<'a> { user_id: i32, /// Sequential number of the first photo to be returned. By default, all /// photos are returned. + #[serde(skip_serializing_if = "Option::is_none")] offset: Option, ///Limits the number of photos to be retrieved. Values between 1—100 are /// accepted. Defaults to 100. + #[serde(skip_serializing_if = "Option::is_none")] limit: Option, } @@ -59,4 +61,13 @@ impl<'a> GetUserProfilePhotos<'a> { self.offset = Some(offset.into()); self } + + pub fn limit(mut self, limit: T) -> Self + where + T: Into, + { + self.limit = Some(limit.into()); + self + } + } From 57e3b217786a8513419dfe2e861d9555a305de35 Mon Sep 17 00:00:00 2001 From: nextel Date: Sat, 21 Sep 2019 20:10:25 +0300 Subject: [PATCH 4/9] add pub modifcators for filds --- src/requests/get_user_profile_photos.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/requests/get_user_profile_photos.rs b/src/requests/get_user_profile_photos.rs index 9e84d935..722b919a 100644 --- a/src/requests/get_user_profile_photos.rs +++ b/src/requests/get_user_profile_photos.rs @@ -9,15 +9,15 @@ pub struct GetUserProfilePhotos<'a> { #[serde(skip_serializing)] ctx: RequestContext<'a>, /// Unique identifier of the target user - user_id: i32, + pub user_id: i32, /// Sequential number of the first photo to be returned. By default, all /// photos are returned. #[serde(skip_serializing_if = "Option::is_none")] - offset: Option, + pub offset: Option, ///Limits the number of photos to be retrieved. Values between 1—100 are /// accepted. Defaults to 100. #[serde(skip_serializing_if = "Option::is_none")] - limit: Option, + pub limit: Option, } impl<'a> Request<'a> for GetUserProfilePhotos<'a> { From 62b26fe14f96719b0a34477b3426d37821fb6d2d Mon Sep 17 00:00:00 2001 From: nextel Date: Sat, 21 Sep 2019 20:20:14 +0300 Subject: [PATCH 5/9] add kickChatMember action --- src/requests/kick_chat_member.rs | 66 +++++++++++++++++++++++++++++--- 1 file changed, 60 insertions(+), 6 deletions(-) diff --git a/src/requests/kick_chat_member.rs b/src/requests/kick_chat_member.rs index a07f2252..fe762ab4 100644 --- a/src/requests/kick_chat_member.rs +++ b/src/requests/kick_chat_member.rs @@ -1,5 +1,8 @@ -use crate::requests::RequestContext; -//TODO:: need implementation +use crate::network; +use crate::requests::{ + ChatId, Request, RequestContext, RequestFuture, ResponseResult, +}; + /// Use this method to kick a user from a group, a supergroup or a channel. In /// the case of supergroups and channels, the user will not be able to return to /// the group on their own using invite links, etc., unless unbanned first. The @@ -9,8 +12,59 @@ use crate::requests::RequestContext; pub struct KickChatMember<'a> { #[serde(skip_serializing)] ctx: RequestContext<'a>, - - chat_id Integer or String Yes Unique identifier for the target group or username of the target supergroup or channel (in the format @channelusername) - user_id Integer Yes Unique identifier of the target user - until_date Integer Optional Date when the user will be unbanned, unix time. If user is banned for more than 366 days or less than 30 seconds from the current time they are considered to be banned forever + ///Unique identifier for the target group or username of the target + /// supergroup or channel (in the format @channelusername) + pub chat_id: ChatId, + /// Unique identifier of the target user + pub user_id: i32, + ///Date when the user will be unbanned, unix time. If user is banned for + /// more than 366 days or less than 30 seconds from the current time they + /// are considered to be banned forever + pub until_date: Option, +} + +impl<'a> Request<'a> for KickChatMember<'a> { + type ReturnValue = bool; + + fn send(self) -> RequestFuture<'a, ResponseResult> { + Box::pin(async move { + network::request_json( + self.ctx.client, + self.ctx.token, + "kickChatMember", + &self, + ) + .await + }) + } +} + +impl<'a> KickChatMember<'a> { + pub(crate) fn new( + ctx: RequestContext<'a>, + chat_id: ChatId, + user_id: i32, + ) -> Self { + Self { + ctx, + chat_id, + user_id, + until_date: None, + } + } + + pub fn chat_id>(mut self, chat_id: T) -> Self { + self.chat_id = chat_id.into(); + self + } + + pub fn user_id>(mut self, user_id: T) -> Self { + self.user_id = user_id.into(); + self + } + + pub fn until_date>(mut self, until_date: T) -> Self { + self.until_date = Some(user_id.into()); + self + } } From a777b3a8055385a67f28e5ce31c6ea46c2743b67 Mon Sep 17 00:00:00 2001 From: nextel Date: Sat, 21 Sep 2019 20:39:58 +0300 Subject: [PATCH 6/9] add action template --- src/requests/restrict_chat_member.rs | 87 +++++++++++++++++++++++++++- 1 file changed, 85 insertions(+), 2 deletions(-) diff --git a/src/requests/restrict_chat_member.rs b/src/requests/restrict_chat_member.rs index 781dead6..62bf8f29 100644 --- a/src/requests/restrict_chat_member.rs +++ b/src/requests/restrict_chat_member.rs @@ -1,8 +1,91 @@ -use crate::requests::RequestContext; -//TODO:: need implementation +use crate::network; +use crate::requests::{ + ChatId, Request, RequestContext, RequestFuture, ResponseResult, +}; +use crate::types::ChatPermissions; +/// Use this method to restrict a user in a supergroup. The bot must be an +/// administrator in the supergroup for this to work and must have the +/// appropriate admin rights. Pass True for all permissions to lift restrictions +/// from a user. Returns True on success. #[derive(Debug, Clone, Serialize)] pub struct RestrictChatMember<'a> { #[serde(skip_serializing)] ctx: RequestContext<'a>, + ///Unique identifier for the target chat or username of the target + /// supergroup (in the format @supergroupusername) + pub chat_id: ChatId, + ///Unique identifier of the target user + pub user_id: i32, + ///New user permissions + pub permissions: ChatPermissions, + ///Date when restrictions will be lifted for the user, unix time. If user + /// is restricted for more than 366 days or less than 30 seconds from the + /// current time, they are considered to be restricted forever + pub until_date: Option, +} + +impl<'a> Request<'a> for RestrictChatMember<'a> { + type ReturnValue = bool; + + fn send(self) -> RequestFuture<'a, ResponseResult> { + Box::pin(async move { + network::request_json( + &self.ctx.client, + &self.ctx.token, + "restrictChatMember", + &self, + ) + .await + }) + } +} + +impl<'a> RestrictChatMember<'a> { + pub(crate) fn new( + ctx: RequestContext<'a>, + chat_id: ChatId, + user_id: i32, + permissions: ChatPermissions, + ) -> Self { + Self { + ctx, + chat_id, + user_id, + permissions, + until_date: None, + } + } + + pub fn chat_id(mut self, chat_id: T) -> Self + where + T: Into, + { + self.chat_id = chat_id.into(); + self + } + + pub fn user_id(mut self, user_id: T) -> Self + where + T: Into, + { + self.user_id = user_id.into(); + self + } + + pub fn permissions(mut self, permissions: T) -> Self + where + T: Into, + { + self.permissions = permissions.into(); + self + } + + pub fn until_date(mut self, until_date: T) -> Self + where + T: Into, + { + self.until_date = Some(until_date.into()); + self + } } From 774c481faf4fdb400e3cfc36e83e7819273435be Mon Sep 17 00:00:00 2001 From: nextel Date: Sat, 21 Sep 2019 20:41:31 +0300 Subject: [PATCH 7/9] fix build error --- src/requests/kick_chat_member.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/requests/kick_chat_member.rs b/src/requests/kick_chat_member.rs index fe762ab4..ea590d59 100644 --- a/src/requests/kick_chat_member.rs +++ b/src/requests/kick_chat_member.rs @@ -64,7 +64,7 @@ impl<'a> KickChatMember<'a> { } pub fn until_date>(mut self, until_date: T) -> Self { - self.until_date = Some(user_id.into()); + self.until_date = Some(until_date.into()); self } } From 7736ba3ca8ad33ee4044867fbf45d6a6adc9f0b6 Mon Sep 17 00:00:00 2001 From: nextel Date: Sun, 22 Sep 2019 17:03:12 +0300 Subject: [PATCH 8/9] add serde derive for optional field --- src/requests/kick_chat_member.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/requests/kick_chat_member.rs b/src/requests/kick_chat_member.rs index ea590d59..2ed54f0a 100644 --- a/src/requests/kick_chat_member.rs +++ b/src/requests/kick_chat_member.rs @@ -20,6 +20,7 @@ pub struct KickChatMember<'a> { ///Date when the user will be unbanned, unix time. If user is banned for /// more than 366 days or less than 30 seconds from the current time they /// are considered to be banned forever + #[serde(skip_serializing_if = "Option::is_none")] pub until_date: Option, } From a63fe51171b490ada49c2649f468a220a9a832d6 Mon Sep 17 00:00:00 2001 From: nextel Date: Sun, 22 Sep 2019 17:05:30 +0300 Subject: [PATCH 9/9] add skip serialization for opt value --- src/requests/restrict_chat_member.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/requests/restrict_chat_member.rs b/src/requests/restrict_chat_member.rs index 62bf8f29..dabfb9c1 100644 --- a/src/requests/restrict_chat_member.rs +++ b/src/requests/restrict_chat_member.rs @@ -22,6 +22,7 @@ pub struct RestrictChatMember<'a> { ///Date when restrictions will be lifted for the user, unix time. If user /// is restricted for more than 366 days or less than 30 seconds from the /// current time, they are considered to be restricted forever + #[serde(skip_serializing_if = "Option::is_none")] pub until_date: Option, }