grammers_client/types/
participant.rs

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