1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
use std::{convert::TryFrom, convert::TryInto};

use derivative::Derivative;
use serde::{Deserialize, Serialize};
use uuid::Uuid;
use zkgroup::profiles::ProfileKey;

use super::GroupDecodingError;

#[derive(Copy, Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub enum Role {
    Unknown,
    Default,
    Administrator,
}

#[derive(Derivative, Clone, Deserialize, Serialize)]
#[derivative(Debug)]
pub struct Member {
    pub uuid: Uuid,
    pub role: Role,
    #[derivative(Debug = "ignore")]
    pub profile_key: ProfileKey,
    pub joined_at_revision: u32,
}

impl PartialEq for Member {
    fn eq(&self, other: &Self) -> bool {
        self.uuid == other.uuid
    }
}

#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
pub struct PendingMember {
    pub uuid: Uuid,
    pub role: Role,
    pub added_by_uuid: Uuid,
    pub timestamp: u64,
}

#[derive(Derivative, Clone, Deserialize, Serialize)]
#[derivative(Debug)]
pub struct RequestingMember {
    pub uuid: Uuid,
    #[derivative(Debug = "ignore")]
    pub profile_key: ProfileKey,
    pub timestamp: u64,
}

impl PartialEq for RequestingMember {
    fn eq(&self, other: &Self) -> bool {
        self.uuid == other.uuid
    }
}

#[derive(Copy, Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum AccessRequired {
    Unknown,
    Any,
    Member,
    Administrator,
    Unsatisfiable,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct AccessControl {
    pub attributes: AccessRequired,
    pub members: AccessRequired,
    pub add_from_invite_link: AccessRequired,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Group {
    pub title: String,
    pub avatar: String,
    pub disappearing_messages_timer: Option<Timer>,
    pub access_control: Option<AccessControl>,
    pub revision: u32,
    pub members: Vec<Member>,
    pub pending_members: Vec<PendingMember>,
    pub requesting_members: Vec<RequestingMember>,
    pub invite_link_password: Vec<u8>,
    pub description: Option<String>,
}

#[derive(Debug, Clone)]
pub struct GroupChanges {
    pub editor: Uuid,
    pub revision: u32,
    pub changes: Vec<GroupChange>,
}

#[derive(Derivative, Clone)]
#[derivative(Debug)]
pub enum GroupChange {
    NewMember(Member),
    DeleteMember(Uuid),
    ModifyMemberRole {
        uuid: Uuid,
        role: Role,
    },
    ModifyMemberProfileKey {
        uuid: Uuid,
        #[derivative(Debug = "ignore")]
        profile_key: ProfileKey,
    },
    // for open groups
    NewPendingMember(PendingMember),
    DeletePendingMember(Uuid),
    PromotePendingMember {
        uuid: Uuid,
        #[derivative(Debug = "ignore")]
        profile_key: ProfileKey,
    },
    // when admin control is enabled
    NewRequestingMember(RequestingMember),
    DeleteRequestingMember(Uuid),
    PromoteRequestingMember {
        uuid: Uuid,
        role: Role,
    },
    // group metadata
    Title(String),
    Avatar(String),
    Timer(Option<Timer>),
    Description(Option<String>),
    AttributeAccess(AccessRequired),
    MemberAccess(AccessRequired),
    InviteLinkAccess(AccessRequired),
    InviteLinkPassword(String),
    AnnouncementOnly(bool),
}

#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
pub struct Timer {
    pub duration: u32,
}

// Conversion from and to protobuf definitions

impl TryFrom<i32> for Role {
    type Error = GroupDecodingError;

    fn try_from(value: i32) -> Result<Self, Self::Error> {
        use crate::proto::member::Role::*;
        match crate::proto::member::Role::try_from(value) {
            Ok(Unknown) => Ok(Role::Unknown),
            Ok(Default) => Ok(Role::Default),
            Ok(Administrator) => Ok(Role::Administrator),
            Err(_e) => Err(GroupDecodingError::WrongEnumValue),
        }
    }
}

impl From<Role> for i32 {
    fn from(val: Role) -> Self {
        use crate::proto::member::Role::*;
        match val {
            Role::Unknown => Unknown,
            Role::Default => Default,
            Role::Administrator => Administrator,
        }
        .into()
    }
}

impl TryFrom<i32> for AccessRequired {
    type Error = GroupDecodingError;

    fn try_from(value: i32) -> Result<Self, Self::Error> {
        use crate::proto::access_control::AccessRequired::*;
        match crate::proto::access_control::AccessRequired::try_from(value) {
            Ok(Unknown) => Ok(AccessRequired::Unknown),
            Ok(Any) => Ok(AccessRequired::Any),
            Ok(Member) => Ok(AccessRequired::Member),
            Ok(Administrator) => Ok(AccessRequired::Administrator),
            Ok(Unsatisfiable) => Ok(AccessRequired::Unsatisfiable),
            Err(_e) => Err(GroupDecodingError::WrongEnumValue),
        }
    }
}

impl From<AccessRequired> for i32 {
    fn from(val: AccessRequired) -> Self {
        use crate::proto::access_control::AccessRequired::*;
        match val {
            AccessRequired::Unknown => Unknown,
            AccessRequired::Any => Any,
            AccessRequired::Member => Member,
            AccessRequired::Administrator => Administrator,
            AccessRequired::Unsatisfiable => Unsatisfiable,
        }
        .into()
    }
}

impl TryFrom<crate::proto::AccessControl> for AccessControl {
    type Error = GroupDecodingError;

    fn try_from(
        value: crate::proto::AccessControl,
    ) -> Result<Self, Self::Error> {
        Ok(Self {
            attributes: value.attributes.try_into()?,
            members: value.members.try_into()?,
            add_from_invite_link: value.add_from_invite_link.try_into()?,
        })
    }
}