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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
// Copyright (c) 2022 Open Community Project Association https://ocpa.ch
// This software is published under the AGPLv3 license.

//! # Group Management

use libp2p::PeerId;
use std::collections::BTreeMap;

use super::group_id::GroupId;
use super::{Group, GroupStorage};
use crate::services::chat::{self, Chat, ChatStorage};
use crate::utilities::timestamp::Timestamp;

/// Group Manage Structure
pub struct GroupManage {}
impl GroupManage {
    /// Get a group from the data base
    ///
    /// If it is a direct chat group, and does not yet exist
    /// this function will create a new direct chat group and
    /// return it.
    pub fn get_group_create_direct(
        account_id: PeerId,
        group_id: GroupId,
        remote_id: &PeerId,
    ) -> Option<Group> {
        // try to get group from data base
        match GroupStorage::get_group(account_id.clone(), group_id.to_bytes()) {
            Some(group) => return Some(group),
            None => {
                // check if it is the direct chat group for the connection
                if group_id == GroupId::from_peers(&account_id, remote_id) {
                    // create a new direct chat group
                    let group = Self::create_new_direct_chat_group(&account_id, &remote_id);
                    return Some(group);
                }
            }
        }

        None
    }

    /// Create a new direct chat group
    ///
    /// The function expects two qaul user ID's:
    ///
    /// * `account_id` your user account ID
    /// * `user_id` the user ID of the other user
    pub fn create_new_direct_chat_group(account_id: &PeerId, user_id: &PeerId) -> Group {
        let group_id = GroupId::from_peers(account_id, user_id).to_bytes();

        // check if group already exists
        if let Some(group) = GroupStorage::get_group(account_id.to_owned(), group_id.clone()) {
            return group;
        }

        // create new group
        let mut group = Group::new();
        group.members.insert(
            account_id.to_bytes(),
            super::GroupMember {
                user_id: account_id.to_bytes(),
                role: super::proto_rpc::GroupMemberRole::Admin.try_into().unwrap(),
                joined_at: Timestamp::get_timestamp(),
                state: super::proto_rpc::GroupMemberState::Activated
                    .try_into()
                    .unwrap(),
                last_message_index: 0,
            },
        );
        group.members.insert(
            user_id.to_bytes(),
            super::GroupMember {
                user_id: user_id.to_bytes(),
                role: super::proto_rpc::GroupMemberRole::Admin.try_into().unwrap(),
                joined_at: Timestamp::get_timestamp(),
                state: super::proto_rpc::GroupMemberState::Activated
                    .try_into()
                    .unwrap(),
                last_message_index: 0,
            },
        );

        group.id = group_id.clone();
        group.is_direct_chat = true;

        // save group to data base
        GroupStorage::save_group(account_id.to_owned(), group.clone());

        group
    }

    /// create new group from rpc command
    pub fn create_new_group(account_id: &PeerId, name: String) -> Vec<u8> {
        let mut group = Group::new();

        group.id = uuid::Uuid::new_v4().as_bytes().to_vec();

        group.members.insert(
            account_id.to_bytes(),
            super::GroupMember {
                user_id: account_id.to_bytes(),
                role: super::proto_rpc::GroupMemberRole::Admin.try_into().unwrap(),
                joined_at: Timestamp::get_timestamp(),
                state: super::proto_rpc::GroupMemberState::Activated as i32,
                last_message_index: 0,
            },
        );

        group.name = name;

        // save group
        GroupStorage::save_group(account_id.to_owned(), group.clone());

        // save group created event
        let event = chat::rpc_proto::ChatContentMessage {
            message: Some(chat::rpc_proto::chat_content_message::Message::GroupEvent(
                chat::rpc_proto::GroupEvent {
                    event_type: chat::rpc_proto::GroupEventType::Created as i32,
                    user_id: account_id.to_bytes(),
                },
            )),
        };

        ChatStorage::save_message(
            account_id,
            &GroupId::from_bytes(&group.id).unwrap(),
            account_id,
            &Vec::new(),
            Timestamp::get_timestamp(),
            event,
            chat::rpc_proto::MessageStatus::Received,
        );

        return group.id;
    }

    /// rename group from RPC command
    ///
    /// `account_id` the user account ID
    pub fn rename_group(
        account_id: &PeerId,
        group_id: &Vec<u8>,
        name: String,
    ) -> Result<(), String> {
        if let Some(mut group) = GroupStorage::get_group(account_id.to_owned(), group_id.to_owned())
        {
            // check if administrator
            if let Some(member) = group.get_member(&account_id.to_bytes()) {
                // check permission
                if member.role != 255 {
                    return Err("you don't have the permissions to rename this group".to_string());
                }
            } else {
                return Err("you are not a member for this group".to_string());
            }

            // rename group
            group.name = name;

            // update revision
            group.revision = group.revision + 1;

            // save group
            GroupStorage::save_group(account_id.to_owned(), group);

            return Ok(());
        }

        Err("can not find group".to_string())
    }

    /// get a new message ID
    pub fn get_new_message_id(account_id: &PeerId, group_id: &Vec<u8>) -> Vec<u8> {
        if let Some(mut group) = GroupStorage::get_group(account_id.to_owned(), group_id.to_owned())
        {
            // get my member
            if let Some(member) = group.members.get(&account_id.to_bytes()) {
                let new_index = member.last_message_index + 1;

                // update & save last_index in group
                let mut member_updated = member.to_owned();
                member_updated.last_message_index = new_index;
                group.members.insert(account_id.to_bytes(), member_updated);
                GroupStorage::save_group(account_id.to_owned(), group);

                // create message id
                return Chat::generate_message_id(group_id, account_id, new_index);
            }
        }

        Vec::new()
    }

    /// get group information from rpc command
    ///
    /// `account_id` the user account ID
    pub fn group_info(
        account_id: &PeerId,
        group_id: &Vec<u8>,
    ) -> Result<super::proto_rpc::GroupInfo, String> {
        let group;
        match GroupStorage::get_group(account_id.to_owned(), group_id.to_owned()) {
            Some(group_result) => group = group_result,
            None => return Err("group not found".to_string()),
        }

        let mut members: Vec<super::proto_rpc::GroupMember> = vec![];
        for m in group.members.values() {
            let member = super::proto_rpc::GroupMember {
                user_id: m.user_id.clone(),
                role: m.role,
                joined_at: m.joined_at,
                state: m.state,
                last_message_index: m.last_message_index,
            };
            members.push(member);
        }

        let res = super::proto_rpc::GroupInfo {
            group_id: group.id,
            group_name: group.name,
            created_at: group.created_at,
            status: group.status,
            revision: group.revision,
            is_direct_chat: group.is_direct_chat,
            members,
            unread_messages: group.unread_messages,
            last_message_at: group.last_message_at,
            last_message: group.last_message_data,
            last_message_sender_id: group.last_message_sender_id,
        };
        Ok(res)
    }

    /// get group list from rpc command
    ///
    /// `account_id` the user account ID
    pub fn group_list(account_id: &PeerId) -> super::proto_rpc::GroupListResponse {
        let db_ref = GroupStorage::get_db_ref(account_id.to_owned());

        let mut res = super::proto_rpc::GroupListResponse { groups: vec![] };

        for entry in db_ref.groups.iter() {
            match entry {
                Ok((_, group)) => {
                    let mut members: Vec<super::proto_rpc::GroupMember> = vec![];
                    for m in group.members.values() {
                        let member = super::proto_rpc::GroupMember {
                            user_id: m.user_id.clone(),
                            role: m.role,
                            joined_at: m.joined_at,
                            state: m.state,
                            last_message_index: m.last_message_index,
                        };
                        members.push(member);
                    }

                    let grp = super::proto_rpc::GroupInfo {
                        group_id: group.id,
                        group_name: group.name,
                        created_at: group.created_at,
                        status: group.status,
                        revision: group.revision,
                        is_direct_chat: group.is_direct_chat,
                        members,
                        unread_messages: group.unread_messages,
                        last_message_at: group.last_message_at,
                        last_message: group.last_message_data,
                        last_message_sender_id: group.last_message_sender_id,
                    };
                    res.groups.push(grp);
                }
                _ => {}
            }
        }
        res
    }

    /// get invited list from rpc command
    pub fn invited_list(account_id: &PeerId) -> super::proto_rpc::GroupInvitedResponse {
        let db_ref = GroupStorage::get_db_ref(account_id.to_owned());

        let mut res = super::proto_rpc::GroupInvitedResponse { invited: vec![] };

        for entry in db_ref.invited.iter() {
            match entry {
                Ok((_, invite)) => {
                    let mut members: Vec<super::proto_rpc::GroupMember> = Vec::new();
                    for (_, member) in invite.group.members {
                        members.push(super::proto_rpc::GroupMember {
                            user_id: member.user_id,
                            role: member.role,
                            joined_at: member.joined_at,
                            state: member.state,
                            last_message_index: member.last_message_index,
                        });
                    }

                    let invited = super::proto_rpc::GroupInvited {
                        sender_id: invite.sender_id.clone(),
                        received_at: invite.received_at,
                        group: Some(super::proto_rpc::GroupInfo {
                            group_id: invite.group.id,
                            group_name: invite.group.name,
                            created_at: invite.group.created_at,
                            status: invite.group.status,
                            revision: invite.group.revision,
                            is_direct_chat: invite.group.is_direct_chat,
                            members,
                            unread_messages: 0,
                            last_message_at: 0,
                            last_message: Vec::new(),
                            last_message_sender_id: Vec::new(),
                        }),
                    };

                    res.invited.push(invited);
                }
                _ => {}
            }
        }
        res
    }

    /// process group notify message from network
    pub fn on_group_notify(
        sender_id: PeerId,
        account_id: PeerId,
        notify: &super::proto_net::GroupInfo,
    ) {
        // check for valid group ID
        let group_id;
        match GroupId::from_bytes(&notify.group_id) {
            Ok(id) => group_id = id,
            Err(e) => {
                log::error!("invalid group id: {}", e);
                return;
            }
        }

        let mut first_join = false;
        let mut orign_members: BTreeMap<Vec<u8>, bool> = BTreeMap::new();
        let mut new_members: Vec<Vec<u8>> = vec![];

        // get group
        let mut group: Group;
        match GroupStorage::get_group(account_id, notify.group_id.clone()) {
            Some(my_group) => {
                group = my_group;

                // check if the sent revision is higher then the one we already have
                // return otherwise
                if group.revision >= notify.revision {
                    log::warn!("group update: got a smaller revision");
                    return;
                }

                // check if sender is administrator, otherwise return
                let mut sender_is_admin = false;
                for (member_id, member) in &group.members {
                    orign_members.insert(member_id.clone(), true);

                    if member.user_id == sender_id.to_bytes() && member.role == 255 {
                        sender_is_admin = true;
                    }
                }

                if !sender_is_admin {
                    log::error!(
                        "illegitimate update from user {} for group {}",
                        sender_id.to_base58(),
                        group_id.to_string(),
                    );
                    return;
                }
            }
            None => {
                first_join = true;

                group = Group::new();
            }
        }

        // check for new members
        let mut members: BTreeMap<Vec<u8>, super::GroupMember> = BTreeMap::new();
        for m in &notify.members {
            if orign_members.contains_key(&m.user_id) {
                orign_members.remove(&m.user_id);
            } else {
                new_members.push(m.user_id.clone());
            }

            members.insert(
                m.user_id.clone(),
                super::GroupMember {
                    user_id: m.user_id.clone(),
                    role: m.role,
                    joined_at: m.joined_at,
                    state: m.state,
                    last_message_index: m.last_message_index,
                },
            );
        }

        // update group
        group.id = notify.group_id.clone();
        group.name = notify.group_name.clone();
        group.created_at = notify.created_at;
        group.revision = notify.revision;
        group.members = members;

        // activate group after invite accept
        if group.status == super::proto_rpc::GroupStatus::InviteAccepted as i32 {
            group.status = super::proto_rpc::GroupStatus::Active as i32;
        }

        // save group
        GroupStorage::save_group(account_id, group);

        // save events
        if first_join {
            let event = chat::rpc_proto::ChatContentMessage {
                message: Some(chat::rpc_proto::chat_content_message::Message::GroupEvent(
                    chat::rpc_proto::GroupEvent {
                        event_type: chat::rpc_proto::GroupEventType::Joined.try_into().unwrap(),
                        user_id: account_id.to_bytes(),
                    },
                )),
            };

            ChatStorage::save_message(
                &account_id,
                &group_id,
                &sender_id,
                &Vec::new(),
                Timestamp::get_timestamp(),
                event,
                chat::rpc_proto::MessageStatus::Received,
            );
        } else {
            for new_member in &new_members {
                let event = chat::rpc_proto::ChatContentMessage {
                    message: Some(chat::rpc_proto::chat_content_message::Message::GroupEvent(
                        chat::rpc_proto::GroupEvent {
                            event_type: chat::rpc_proto::GroupEventType::Joined.try_into().unwrap(),
                            user_id: new_member.clone(),
                        },
                    )),
                };

                ChatStorage::save_message(
                    &account_id,
                    &group_id,
                    &sender_id,
                    &Vec::new(),
                    Timestamp::get_timestamp(),
                    event,
                    chat::rpc_proto::MessageStatus::Received,
                );
            }

            for left_member in orign_members.keys() {
                let event = chat::rpc_proto::ChatContentMessage {
                    message: Some(chat::rpc_proto::chat_content_message::Message::GroupEvent(
                        chat::rpc_proto::GroupEvent {
                            event_type: chat::rpc_proto::GroupEventType::Left.try_into().unwrap(),
                            user_id: left_member.clone(),
                        },
                    )),
                };

                ChatStorage::save_message(
                    &account_id,
                    &group_id,
                    &sender_id,
                    &Vec::new(),
                    Timestamp::get_timestamp(),
                    event,
                    chat::rpc_proto::MessageStatus::Received,
                );
            }
        }
    }
}