1use serde::{Deserialize, Serialize};
2
3pub use self::{
4 action::*,
5 boost::*,
6 full_info::*,
7 id::*,
8 invite_link::*,
9 join_request::*,
10 location::*,
11 member::*,
12 message::*,
13 permissions::*,
14 photo::*,
15 sender_chat::*,
16 sticker_set::*,
17};
18use crate::{
19 api::{Method, Payload},
20 types::BackgroundType,
21};
22
23#[cfg(test)]
24mod tests;
25
26mod action;
27mod boost;
28mod full_info;
29mod id;
30mod invite_link;
31mod join_request;
32mod location;
33mod member;
34mod message;
35mod permissions;
36mod photo;
37mod sender_chat;
38mod sticker_set;
39
40#[derive(Clone, Debug, derive_more::From, Deserialize, PartialEq, Serialize)]
42#[serde(tag = "type")]
43#[serde(rename_all = "snake_case")]
44pub enum Chat {
45 Channel(ChannelChat),
47 Group(GroupChat),
49 Private(PrivateChat),
51 Supergroup(SupergroupChat),
53}
54
55impl Chat {
56 pub fn get_id(&self) -> ChatPeerId {
58 match self {
59 Chat::Channel(chat) => chat.id,
60 Chat::Group(chat) => chat.id,
61 Chat::Private(chat) => chat.id,
62 Chat::Supergroup(chat) => chat.id,
63 }
64 }
65
66 pub fn get_username(&self) -> Option<&ChatUsername> {
68 match &self {
69 Chat::Channel(chat) => chat.username.as_ref(),
70 Chat::Group(_) => None,
71 Chat::Private(chat) => chat.username.as_ref(),
72 Chat::Supergroup(chat) => chat.username.as_ref(),
73 }
74 }
75}
76
77#[serde_with::skip_serializing_none]
79#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
80pub struct ChannelChat {
81 pub id: ChatPeerId,
83 pub title: String,
85 pub username: Option<ChatUsername>,
87}
88
89impl ChannelChat {
90 pub fn new<A, B>(id: A, title: B) -> Self
97 where
98 A: Into<ChatPeerId>,
99 B: Into<String>,
100 {
101 Self {
102 id: id.into(),
103 title: title.into(),
104 username: None,
105 }
106 }
107
108 pub fn with_username<T>(mut self, value: T) -> Self
114 where
115 T: Into<ChatUsername>,
116 {
117 self.username = Some(value.into());
118 self
119 }
120}
121
122#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
124pub struct GroupChat {
125 pub id: ChatPeerId,
127 pub title: String,
129}
130
131impl GroupChat {
132 pub fn new<A, B>(id: A, title: B) -> Self
139 where
140 A: Into<ChatPeerId>,
141 B: Into<String>,
142 {
143 Self {
144 id: id.into(),
145 title: title.into(),
146 }
147 }
148}
149
150#[serde_with::skip_serializing_none]
152#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
153pub struct PrivateChat {
154 pub id: ChatPeerId,
156 pub first_name: String,
158 pub last_name: Option<String>,
160 pub username: Option<ChatUsername>,
162}
163
164impl PrivateChat {
165 pub fn new<A, B>(id: A, first_name: B) -> Self
172 where
173 A: Into<ChatPeerId>,
174 B: Into<String>,
175 {
176 Self {
177 id: id.into(),
178 first_name: first_name.into(),
179 last_name: None,
180 username: None,
181 }
182 }
183
184 pub fn with_last_name<T>(mut self, value: T) -> Self
190 where
191 T: Into<String>,
192 {
193 self.last_name = Some(value.into());
194 self
195 }
196
197 pub fn with_username<T>(mut self, value: T) -> Self
203 where
204 T: Into<ChatUsername>,
205 {
206 self.username = Some(value.into());
207 self
208 }
209}
210
211#[serde_with::skip_serializing_none]
213#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
214pub struct SupergroupChat {
215 pub id: ChatPeerId,
217 pub title: String,
219 pub is_forum: Option<bool>,
221 pub username: Option<ChatUsername>,
223}
224
225impl SupergroupChat {
226 pub fn new<A, B>(id: A, title: B) -> Self
233 where
234 A: Into<ChatPeerId>,
235 B: Into<String>,
236 {
237 Self {
238 id: id.into(),
239 title: title.into(),
240 is_forum: None,
241 username: None,
242 }
243 }
244
245 pub fn with_is_forum(mut self, value: bool) -> Self {
251 self.is_forum = Some(value);
252 self
253 }
254
255 pub fn with_username<T>(mut self, value: T) -> Self
261 where
262 T: Into<ChatUsername>,
263 {
264 self.username = Some(value.into());
265 self
266 }
267}
268
269#[derive(Clone, Debug, Deserialize, PartialEq, PartialOrd, Serialize)]
271pub struct ChatBackground {
272 #[serde(rename = "type")]
274 pub background_type: BackgroundType,
275}
276
277impl From<BackgroundType> for ChatBackground {
278 fn from(value: BackgroundType) -> Self {
279 Self { background_type: value }
280 }
281}
282
283#[derive(Clone, Debug, Serialize)]
285pub struct GetChat {
286 chat_id: ChatId,
287}
288
289impl GetChat {
290 pub fn new<T>(chat_id: T) -> Self
296 where
297 T: Into<ChatId>,
298 {
299 GetChat {
300 chat_id: chat_id.into(),
301 }
302 }
303}
304
305impl Method for GetChat {
306 type Response = ChatFullInfo;
307
308 fn into_payload(self) -> Payload {
309 Payload::json("getChat", self)
310 }
311}
312
313#[derive(Clone, Debug, Serialize)]
315pub struct LeaveChat {
316 chat_id: ChatId,
317}
318
319impl LeaveChat {
320 pub fn new<T>(chat_id: T) -> Self
326 where
327 T: Into<ChatId>,
328 {
329 LeaveChat {
330 chat_id: chat_id.into(),
331 }
332 }
333}
334
335impl Method for LeaveChat {
336 type Response = bool;
337
338 fn into_payload(self) -> Payload {
339 Payload::json("leaveChat", self)
340 }
341}
342
343#[serde_with::skip_serializing_none]
348#[derive(Clone, Debug, Serialize)]
349pub struct SetChatDescription {
350 chat_id: ChatId,
351 description: Option<String>,
352}
353
354impl SetChatDescription {
355 pub fn new<T>(chat_id: T) -> Self
361 where
362 T: Into<ChatId>,
363 {
364 SetChatDescription {
365 chat_id: chat_id.into(),
366 description: None,
367 }
368 }
369
370 pub fn with_description<T>(mut self, value: T) -> Self
376 where
377 T: Into<String>,
378 {
379 self.description = Some(value.into());
380 self
381 }
382}
383
384impl Method for SetChatDescription {
385 type Response = bool;
386
387 fn into_payload(self) -> Payload {
388 Payload::json("setChatDescription", self)
389 }
390}
391
392#[derive(Clone, Debug, Serialize)]
403pub struct SetChatTitle {
404 chat_id: ChatId,
405 title: String,
406}
407
408impl SetChatTitle {
409 pub fn new<A, B>(chat_id: A, title: B) -> Self
416 where
417 A: Into<ChatId>,
418 B: Into<String>,
419 {
420 SetChatTitle {
421 chat_id: chat_id.into(),
422 title: title.into(),
423 }
424 }
425}
426
427impl Method for SetChatTitle {
428 type Response = bool;
429
430 fn into_payload(self) -> Payload {
431 Payload::json("setChatTitle", self)
432 }
433}