Skip to main content

rustigram_api/methods/
getters.rs

1use crate::client::BotClient;
2use crate::error::Result;
3use rustigram_types::chat::ChatFullInfo;
4use rustigram_types::chat_member::ChatMember;
5use rustigram_types::file::File;
6use rustigram_types::user::{ChatId, User, UserProfilePhotos};
7use serde::Serialize;
8use std::future::{Future, IntoFuture};
9use std::pin::Pin;
10
11macro_rules! simple_getter {
12    ($(#[$doc:meta])* $name:ident, $params_ty:ty, $return_ty:ty, $method:literal) => {
13        $(#[$doc])*
14        pub struct $name {
15            client: BotClient,
16            params: $params_ty,
17        }
18        impl IntoFuture for $name {
19            type Output = Result<$return_ty>;
20            type IntoFuture = Pin<Box<dyn Future<Output = Self::Output> + Send>>;
21            fn into_future(self) -> Self::IntoFuture {
22                Box::pin(async move { self.client.post_json($method, &self.params).await })
23            }
24        }
25    };
26}
27
28/// Builder for the [`getMe`](https://core.telegram.org/bots/api#getme) method.
29pub struct GetMe {
30    client: BotClient,
31}
32impl GetMe {
33    pub(crate) fn new(client: BotClient) -> Self {
34        Self { client }
35    }
36}
37impl IntoFuture for GetMe {
38    type Output = Result<User>;
39    type IntoFuture = Pin<Box<dyn Future<Output = Self::Output> + Send>>;
40    fn into_future(self) -> Self::IntoFuture {
41        Box::pin(async move { self.client.post_json("getMe", &serde_json::json!({})).await })
42    }
43}
44
45#[derive(Serialize)]
46struct GetChatParams {
47    chat_id: ChatId,
48}
49simple_getter!(
50    /// Builder for the [`getChat`](https://core.telegram.org/bots/api#getchat) method.
51    GetChat,
52    GetChatParams,
53    ChatFullInfo,
54    "getChat"
55);
56impl GetChat {
57    pub(crate) fn new(client: BotClient, chat_id: impl Into<ChatId>) -> Self {
58        Self {
59            client,
60            params: GetChatParams {
61                chat_id: chat_id.into(),
62            },
63        }
64    }
65}
66
67#[derive(Serialize)]
68struct GetChatAdministratorsParams {
69    chat_id: ChatId,
70}
71simple_getter!(
72    /// Builder for the [`getChatAdministrators`](https://core.telegram.org/bots/api#getchatadministrators) method.
73    GetChatAdministrators,
74    GetChatAdministratorsParams,
75    Vec<ChatMember>,
76    "getChatAdministrators"
77);
78impl GetChatAdministrators {
79    pub(crate) fn new(client: BotClient, chat_id: impl Into<ChatId>) -> Self {
80        Self {
81            client,
82            params: GetChatAdministratorsParams {
83                chat_id: chat_id.into(),
84            },
85        }
86    }
87}
88
89#[derive(Serialize)]
90struct GetChatMemberCountParams {
91    chat_id: ChatId,
92}
93simple_getter!(
94    /// Builder for the [`getChatMemberCount`](https://core.telegram.org/bots/api#getchatmembercount) method.
95    GetChatMemberCount,
96    GetChatMemberCountParams,
97    u32,
98    "getChatMemberCount"
99);
100impl GetChatMemberCount {
101    pub(crate) fn new(client: BotClient, chat_id: impl Into<ChatId>) -> Self {
102        Self {
103            client,
104            params: GetChatMemberCountParams {
105                chat_id: chat_id.into(),
106            },
107        }
108    }
109}
110
111#[derive(Serialize)]
112struct GetChatMemberParams {
113    chat_id: ChatId,
114    user_id: i64,
115}
116simple_getter!(
117    /// Builder for the [`getChatMember`](https://core.telegram.org/bots/api#getchatmember) method.
118    GetChatMember,
119    GetChatMemberParams,
120    ChatMember,
121    "getChatMember"
122);
123impl GetChatMember {
124    pub(crate) fn new(client: BotClient, chat_id: impl Into<ChatId>, user_id: i64) -> Self {
125        Self {
126            client,
127            params: GetChatMemberParams {
128                chat_id: chat_id.into(),
129                user_id,
130            },
131        }
132    }
133}
134
135#[derive(Serialize)]
136struct GetFileParams {
137    file_id: String,
138}
139simple_getter!(
140    /// Builder for the [`getFile`](https://core.telegram.org/bots/api#getfile) method.
141    GetFile, GetFileParams, File, "getFile");
142impl GetFile {
143    pub(crate) fn new(client: BotClient, file_id: impl Into<String>) -> Self {
144        Self {
145            client,
146            params: GetFileParams {
147                file_id: file_id.into(),
148            },
149        }
150    }
151}
152
153#[derive(Serialize)]
154struct GetUserProfilePhotosParams {
155    user_id: i64,
156    #[serde(skip_serializing_if = "Option::is_none")]
157    offset: Option<u32>,
158    #[serde(skip_serializing_if = "Option::is_none")]
159    limit: Option<u8>,
160}
161
162/// Builder for the [`getUserProfilePhotos`](https://core.telegram.org/bots/api#getuserprofilephotos) method.
163pub struct GetUserProfilePhotos {
164    client: BotClient,
165    params: GetUserProfilePhotosParams,
166}
167impl GetUserProfilePhotos {
168    pub(crate) fn new(client: BotClient, user_id: i64) -> Self {
169        Self {
170            client,
171            params: GetUserProfilePhotosParams {
172                user_id,
173                offset: None,
174                limit: None,
175            },
176        }
177    }
178    /// Skips the first N profile photos.
179    pub fn offset(mut self, v: u32) -> Self {
180        self.params.offset = Some(v);
181        self
182    }
183    /// Limits the number of photos returned (1–100).
184    pub fn limit(mut self, v: u8) -> Self {
185        self.params.limit = Some(v);
186        self
187    }
188}
189impl IntoFuture for GetUserProfilePhotos {
190    type Output = Result<UserProfilePhotos>;
191    type IntoFuture = Pin<Box<dyn Future<Output = Self::Output> + Send>>;
192    fn into_future(self) -> Self::IntoFuture {
193        Box::pin(async move {
194            self.client
195                .post_json("getUserProfilePhotos", &self.params)
196                .await
197        })
198    }
199}