use crate::client::BotClient;
use crate::error::Result;
use rustigram_types::chat::ChatFullInfo;
use rustigram_types::chat_member::ChatMember;
use rustigram_types::file::File;
use rustigram_types::user::{ChatId, User, UserProfilePhotos};
use serde::Serialize;
use std::future::{Future, IntoFuture};
use std::pin::Pin;
macro_rules! simple_getter {
($(#[$doc:meta])* $name:ident, $params_ty:ty, $return_ty:ty, $method:literal) => {
$(#[$doc])*
pub struct $name {
client: BotClient,
params: $params_ty,
}
impl IntoFuture for $name {
type Output = Result<$return_ty>;
type IntoFuture = Pin<Box<dyn Future<Output = Self::Output> + Send>>;
fn into_future(self) -> Self::IntoFuture {
Box::pin(async move { self.client.post_json($method, &self.params).await })
}
}
};
}
pub struct GetMe {
client: BotClient,
}
impl GetMe {
pub(crate) fn new(client: BotClient) -> Self {
Self { client }
}
}
impl IntoFuture for GetMe {
type Output = Result<User>;
type IntoFuture = Pin<Box<dyn Future<Output = Self::Output> + Send>>;
fn into_future(self) -> Self::IntoFuture {
Box::pin(async move { self.client.post_json("getMe", &serde_json::json!({})).await })
}
}
#[derive(Serialize)]
struct GetChatParams {
chat_id: ChatId,
}
simple_getter!(
GetChat,
GetChatParams,
ChatFullInfo,
"getChat"
);
impl GetChat {
pub(crate) fn new(client: BotClient, chat_id: impl Into<ChatId>) -> Self {
Self {
client,
params: GetChatParams {
chat_id: chat_id.into(),
},
}
}
}
#[derive(Serialize)]
struct GetChatAdministratorsParams {
chat_id: ChatId,
}
simple_getter!(
GetChatAdministrators,
GetChatAdministratorsParams,
Vec<ChatMember>,
"getChatAdministrators"
);
impl GetChatAdministrators {
pub(crate) fn new(client: BotClient, chat_id: impl Into<ChatId>) -> Self {
Self {
client,
params: GetChatAdministratorsParams {
chat_id: chat_id.into(),
},
}
}
}
#[derive(Serialize)]
struct GetChatMemberCountParams {
chat_id: ChatId,
}
simple_getter!(
GetChatMemberCount,
GetChatMemberCountParams,
u32,
"getChatMemberCount"
);
impl GetChatMemberCount {
pub(crate) fn new(client: BotClient, chat_id: impl Into<ChatId>) -> Self {
Self {
client,
params: GetChatMemberCountParams {
chat_id: chat_id.into(),
},
}
}
}
#[derive(Serialize)]
struct GetChatMemberParams {
chat_id: ChatId,
user_id: i64,
}
simple_getter!(
GetChatMember,
GetChatMemberParams,
ChatMember,
"getChatMember"
);
impl GetChatMember {
pub(crate) fn new(client: BotClient, chat_id: impl Into<ChatId>, user_id: i64) -> Self {
Self {
client,
params: GetChatMemberParams {
chat_id: chat_id.into(),
user_id,
},
}
}
}
#[derive(Serialize)]
struct GetFileParams {
file_id: String,
}
simple_getter!(
GetFile, GetFileParams, File, "getFile");
impl GetFile {
pub(crate) fn new(client: BotClient, file_id: impl Into<String>) -> Self {
Self {
client,
params: GetFileParams {
file_id: file_id.into(),
},
}
}
}
#[derive(Serialize)]
struct GetUserProfilePhotosParams {
user_id: i64,
#[serde(skip_serializing_if = "Option::is_none")]
offset: Option<u32>,
#[serde(skip_serializing_if = "Option::is_none")]
limit: Option<u8>,
}
pub struct GetUserProfilePhotos {
client: BotClient,
params: GetUserProfilePhotosParams,
}
impl GetUserProfilePhotos {
pub(crate) fn new(client: BotClient, user_id: i64) -> Self {
Self {
client,
params: GetUserProfilePhotosParams {
user_id,
offset: None,
limit: None,
},
}
}
pub fn offset(mut self, v: u32) -> Self {
self.params.offset = Some(v);
self
}
pub fn limit(mut self, v: u8) -> Self {
self.params.limit = Some(v);
self
}
}
impl IntoFuture for GetUserProfilePhotos {
type Output = Result<UserProfilePhotos>;
type IntoFuture = Pin<Box<dyn Future<Output = Self::Output> + Send>>;
fn into_future(self) -> Self::IntoFuture {
Box::pin(async move {
self.client
.post_json("getUserProfilePhotos", &self.params)
.await
})
}
}
#[derive(Serialize)]
struct GetUserProfileAudiosParams {
user_id: i64,
#[serde(skip_serializing_if = "Option::is_none")]
offset: Option<u32>,
#[serde(skip_serializing_if = "Option::is_none")]
limit: Option<u8>,
}
pub struct GetUserProfileAudios {
client: BotClient,
params: GetUserProfileAudiosParams,
}
impl GetUserProfileAudios {
pub(crate) fn new(client: BotClient, user_id: i64) -> Self {
Self {
client,
params: GetUserProfileAudiosParams {
user_id,
offset: None,
limit: None,
},
}
}
pub fn offset(mut self, v: u32) -> Self {
self.params.offset = Some(v);
self
}
pub fn limit(mut self, v: u8) -> Self {
self.params.limit = Some(v);
self
}
}
impl IntoFuture for GetUserProfileAudios {
type Output = Result<rustigram_types::user::UserProfileAudios>;
type IntoFuture = Pin<Box<dyn Future<Output = Self::Output> + Send>>;
fn into_future(self) -> Self::IntoFuture {
Box::pin(async move {
self.client
.post_json("getUserProfileAudios", &self.params)
.await
})
}
}