1use super::{Chat, ChatMap, Permissions, Restrictions};
9use crate::utils;
10use chrono::{DateTime, Utc};
11use grammers_tl_types as tl;
12
13#[derive(Clone, Debug, PartialEq)]
14pub struct Normal {
15 date: i32,
16 inviter_id: Option<i64>,
17}
18
19#[derive(Clone, Debug, PartialEq)]
20pub struct Creator {
21 permissions: Permissions,
22 rank: Option<String>,
23}
24
25#[derive(Clone, Debug, PartialEq)]
26pub struct Admin {
27 can_edit: bool,
28 inviter_id: Option<i64>,
29 promoted_by: Option<i64>,
30 date: i32,
31 permissions: Permissions,
32 rank: Option<String>,
33}
34
35#[derive(Clone, Debug, PartialEq)]
36pub struct Banned {
37 left: bool,
38 kicked_by: i64,
39 date: i32,
40 restrictions: Restrictions,
41}
42
43#[derive(Clone, Debug, PartialEq)]
44pub struct Left {}
45
46#[derive(Clone, Debug, PartialEq)]
47#[non_exhaustive]
48pub enum Role {
49 User(Normal),
50 Creator(Creator),
51 Admin(Admin),
52 Banned(Banned),
53 Left(Left),
54}
55
56#[derive(Clone, Debug)]
57#[non_exhaustive]
58pub struct Participant {
59 pub user: crate::types::User,
60 pub role: Role,
61}
62
63impl Normal {
64 pub fn date(&self) -> DateTime<Utc> {
65 utils::date(self.date)
66 }
67
68 pub fn inviter_id(&self) -> Option<i64> {
69 self.inviter_id
70 }
71}
72
73impl Creator {
74 pub fn permissions(&self) -> &Permissions {
75 &self.permissions
76 }
77
78 pub fn rank(&self) -> Option<&str> {
79 self.rank.as_deref()
80 }
81}
82
83impl Admin {
84 pub fn can_edit(&self) -> bool {
85 self.can_edit
86 }
87
88 pub fn inviter_id(&self) -> Option<i64> {
89 self.inviter_id
90 }
91
92 pub fn promoted_by(&self) -> Option<i64> {
93 self.promoted_by
94 }
95
96 pub fn date(&self) -> DateTime<Utc> {
97 utils::date(self.date)
98 }
99
100 pub fn permissions(&self) -> &Permissions {
101 &self.permissions
102 }
103
104 pub fn rank(&self) -> Option<&str> {
105 self.rank.as_deref()
106 }
107}
108
109impl Banned {
110 pub fn left(&self) -> bool {
111 self.left
112 }
113
114 pub fn kicked_by(&self) -> i64 {
115 self.kicked_by
116 }
117
118 pub fn date(&self) -> DateTime<Utc> {
119 utils::date(self.date)
120 }
121
122 pub fn restrictions(&self) -> &Restrictions {
123 &self.restrictions
124 }
125}
126
127impl Participant {
128 pub(crate) fn from_raw_channel(
129 chats: &mut ChatMap,
130 participant: tl::enums::ChannelParticipant,
131 ) -> Self {
132 use tl::enums::ChannelParticipant as P;
133
134 match participant {
135 P::Participant(p) => Self {
136 user: chats.remove_user(p.user_id).unwrap(),
137 role: Role::User(Normal {
138 date: p.date,
139 inviter_id: None,
140 }),
141 },
142 P::ParticipantSelf(p) => Self {
143 user: chats.remove_user(p.user_id).unwrap(),
144 role: Role::User(Normal {
145 date: p.date,
146 inviter_id: Some(p.inviter_id),
147 }),
148 },
149 P::Creator(p) => Self {
150 user: chats.remove_user(p.user_id).unwrap(),
151 role: Role::Creator(Creator {
152 permissions: Permissions::from_raw(p.admin_rights.into()),
153 rank: p.rank,
154 }),
155 },
156 P::Admin(p) => Self {
157 user: chats.remove_user(p.user_id).unwrap(),
158 role: Role::Admin(Admin {
159 can_edit: p.can_edit,
160 inviter_id: p.inviter_id,
161 promoted_by: Some(p.promoted_by),
162 date: p.date,
163 permissions: Permissions::from_raw(p.admin_rights.into()),
164 rank: p.rank,
165 }),
166 },
167 P::Banned(p) => Self {
168 user: match chats.remove(&p.peer).unwrap() {
169 Chat::User(user) => user,
170 _ => todo!("figure out how to deal with non-user being banned"),
171 },
172 role: Role::Banned(Banned {
173 left: p.left,
174 kicked_by: p.kicked_by,
175 date: p.date,
176 restrictions: Restrictions::from_raw(p.banned_rights.into()),
177 }),
178 },
179 P::Left(p) => Self {
180 user: match chats.remove(&p.peer).unwrap() {
181 Chat::User(user) => user,
182 _ => todo!("figure out how to deal with non-user leaving"),
183 },
184 role: Role::Left(Left {}),
185 },
186 }
187 }
188
189 pub(crate) fn from_raw_chat(
190 chats: &mut ChatMap,
191 participant: tl::enums::ChatParticipant,
192 ) -> Self {
193 use tl::enums::ChatParticipant as P;
194
195 match participant {
196 P::Participant(p) => Self {
197 user: chats.remove_user(p.user_id).unwrap(),
198 role: Role::User(Normal {
199 date: p.date,
200 inviter_id: Some(p.inviter_id),
201 }),
202 },
203 P::Creator(p) => Self {
204 user: chats.remove_user(p.user_id).unwrap(),
205 role: Role::Creator(Creator {
206 permissions: Permissions::new_full(),
207 rank: None,
208 }),
209 },
210 P::Admin(p) => Self {
211 user: chats.remove_user(p.user_id).unwrap(),
212 role: Role::Admin(Admin {
213 can_edit: true,
214 inviter_id: Some(p.inviter_id),
215 promoted_by: None,
216 date: p.date,
217 permissions: Permissions::new_full(),
218 rank: None,
219 }),
220 },
221 }
222 }
223}