rustigram_api/methods/
getters.rs1use 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
28pub 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 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 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 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 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 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
162pub 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 pub fn offset(mut self, v: u32) -> Self {
180 self.params.offset = Some(v);
181 self
182 }
183 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}
200
201#[derive(Serialize)]
204struct GetUserProfileAudiosParams {
205 user_id: i64,
206 #[serde(skip_serializing_if = "Option::is_none")]
207 offset: Option<u32>,
208 #[serde(skip_serializing_if = "Option::is_none")]
209 limit: Option<u8>,
210}
211
212pub struct GetUserProfileAudios {
216 client: BotClient,
217 params: GetUserProfileAudiosParams,
218}
219
220impl GetUserProfileAudios {
221 pub(crate) fn new(client: BotClient, user_id: i64) -> Self {
222 Self {
223 client,
224 params: GetUserProfileAudiosParams {
225 user_id,
226 offset: None,
227 limit: None,
228 },
229 }
230 }
231 pub fn offset(mut self, v: u32) -> Self {
233 self.params.offset = Some(v);
234 self
235 }
236 pub fn limit(mut self, v: u8) -> Self {
238 self.params.limit = Some(v);
239 self
240 }
241}
242
243impl IntoFuture for GetUserProfileAudios {
244 type Output = Result<rustigram_types::user::UserProfileAudios>;
245 type IntoFuture = Pin<Box<dyn Future<Output = Self::Output> + Send>>;
246 fn into_future(self) -> Self::IntoFuture {
247 Box::pin(async move {
248 self.client
249 .post_json("getUserProfileAudios", &self.params)
250 .await
251 })
252 }
253}