tgbot/types/message/origin/mod.rs
1use serde::{Deserialize, Serialize};
2
3use crate::types::{Chat, Integer, User};
4
5#[cfg(test)]
6mod tests;
7
8/// Describes the origin of a message.
9#[derive(Clone, Debug, Deserialize, derive_more::From, PartialEq, Serialize)]
10#[serde(rename_all = "snake_case", tag = "type")]
11pub enum MessageOrigin {
12 /// The message was originally sent to a channel chat.
13 Channel(MessageOriginChannel),
14 /// The message was originally sent on behalf of a chat to a group chat.
15 Chat(MessageOriginChat),
16 /// The message was originally sent by an unknown user.
17 HiddenUser(MessageOriginHiddenUser),
18 /// The message was originally sent by a known user.
19 User(MessageOriginUser),
20}
21
22/// The message was originally sent to a channel chat.
23#[serde_with::skip_serializing_none]
24#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
25pub struct MessageOriginChannel {
26 /// Chat that sent the message originally.
27 pub chat: Chat,
28 /// Date the message was sent originally in Unix time.
29 pub date: Integer,
30 /// Unique message identifier inside the chat.
31 pub message_id: Integer,
32 /// Signature of the original post author.
33 pub author_signature: Option<String>,
34}
35
36impl MessageOriginChannel {
37 /// Creates a new `MessageOriginChannel`.
38 ///
39 /// # Arguments
40 ///
41 /// * `chat` - Chat that sent the message originally.
42 /// * `date` - Date the message was sent originally in Unix time.
43 /// * `message_id` - Unique message identifier inside the chat.
44 pub fn new<T>(chat: T, date: Integer, message_id: Integer) -> Self
45 where
46 T: Into<Chat>,
47 {
48 Self {
49 chat: chat.into(),
50 date,
51 message_id,
52 author_signature: None,
53 }
54 }
55
56 /// Sets a new author signature
57 ///
58 /// # Arguments
59 ///
60 /// * `value` - Signature of the original post author.
61 pub fn with_author_signature<T>(mut self, value: T) -> Self
62 where
63 T: Into<String>,
64 {
65 self.author_signature = Some(value.into());
66 self
67 }
68}
69
70/// The message was originally sent on behalf of a chat to a group chat.
71#[serde_with::skip_serializing_none]
72#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
73pub struct MessageOriginChat {
74 /// Date the message was sent originally in Unix time.
75 pub date: Integer,
76 /// Chat that sent the message originally.
77 pub sender_chat: Chat,
78 /// For messages originally sent by an anonymous chat administrator, original message author signature.
79 pub author_signature: Option<String>,
80}
81
82impl MessageOriginChat {
83 /// Creates a new `MessageOriginChat`.
84 ///
85 /// # Arguments
86 ///
87 /// * `date` - Date the message was sent originally in Unix time.
88 /// * `sender_chat` - Chat that sent the message originally.
89 pub fn new<T>(date: Integer, sender_chat: T) -> Self
90 where
91 T: Into<Chat>,
92 {
93 Self {
94 date,
95 sender_chat: sender_chat.into(),
96 author_signature: None,
97 }
98 }
99
100 /// Sets a new author signature
101 ///
102 /// # Arguments
103 ///
104 /// * `value` - Signature of the original post author.
105 pub fn with_author_signature<T>(mut self, value: T) -> Self
106 where
107 T: Into<String>,
108 {
109 self.author_signature = Some(value.into());
110 self
111 }
112}
113
114/// The message was originally sent by an unknown user.
115#[derive(Clone, Debug, Deserialize, PartialEq, PartialOrd, Serialize)]
116pub struct MessageOriginHiddenUser {
117 /// Date the message was sent originally in Unix time.
118 pub date: Integer,
119 /// Name of the user that sent the message originally.
120 pub sender_user_name: String,
121}
122
123impl MessageOriginHiddenUser {
124 /// Creates a new `MessageOriginHiddenUser`.
125 ///
126 /// # Arguments
127 ///
128 /// * `date` - Date the message was sent originally in Unix time.
129 /// * `sender_user_name` - Name of the user that sent the message originally.
130 pub fn new<T>(date: Integer, sender_user_name: T) -> Self
131 where
132 T: Into<String>,
133 {
134 Self {
135 date,
136 sender_user_name: sender_user_name.into(),
137 }
138 }
139}
140
141/// The message was originally sent by a known user.
142#[derive(Clone, Debug, Deserialize, PartialEq, PartialOrd, Serialize)]
143pub struct MessageOriginUser {
144 /// Date the message was sent originally in Unix time.
145 pub date: Integer,
146 /// User that sent the message originally.
147 pub sender_user: User,
148}
149
150impl MessageOriginUser {
151 /// Creates a new `MessageOriginUser`.
152 ///
153 /// # Arguments
154 ///
155 /// * `date` - Date the message was sent originally in Unix time.
156 /// * `sender_user` - User that sent the message originally.
157 pub fn new(date: Integer, sender_user: User) -> Self {
158 Self { date, sender_user }
159 }
160}