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
use sha2::{Sha256, Digest};
use std::collections::HashMap;
use super::*;
/// That one struct which makes everything work fine
struct TopEncryptor {
key: KeyChain,
timestamp: u64
}
/// [`Message`]s storage, including all specific logic.
pub struct History {
/// [`Chat::OneToOne`] | [`Chat::Group`] | [`Chat::Channel`]
pub chat_t: Chat,
/// Chat id used for identifing chats (mostly needed for [`Chat::Group`] | [`Chat::Channel`])
pub chat_id: [u8; 32],
/// Is history encrypted
pub is_encrypted: bool,
/// Key to encrypt new message. Needed because `messages` can be empty.
top_encryptor: Mutex<HashMap<PeerId, TopEncryptor>>,
/// Timestamp of the last message. Needed because `messages` can be empty.
top_timestamp: Mutex<u64>,
/// Maximum number of messages stored at a time.
constraint: usize,
/// Time span, while all messages stay alive
soft_ttl: u64,
/// Time span, after which all messages are deleted
hard_ttl: u64,
/// Stored [`Message`]s
pub messages: Mutex<Vec<Message>>,
/// Members of [`Chat::OneToOne`] | [`Chat::Group`] | [`Chat::Channel`]
other_members: Mutex<Vec<Peer>>
}
impl History {
/// Initiate history
///
/// Arguments
///
/// * `chat_t` --- needed for determining [`PacketType`]
/// * `chat_id` --- chat identifier
/// * `init_key` --- initial encryption key
/// * `timestamp` --- needed for history synchronization
/// * `constraint` --- maximum number of messages in [`History`] at a time
/// * `soft_ttl` --- soft time to live
/// * `hard_ttl` --- hard time to live
/// * `is_encrypted` --- shall messages be encrypted for sending
/// * `initial_members` --- members of chat, current [`Peer`] is the first one
pub fn init(
chat_t: Chat,
chat_id: [u8; 32],
timestamp: u64,
constraint: u16,
soft_ttl: u64,
hard_ttl: u64,
is_encrypted: bool,
initial_members: Vec<Peer>
) -> History {
let mut init_encryptor = HashMap::with_capacity(initial_members.len());
for peer in initial_members.iter() {
let mut hasher = Sha256::new();
hasher.update(peer.id.inner);
let hash = hasher.finalize().into();
init_encryptor.insert(
peer.id,
TopEncryptor {
key: KeyChain::new(hash),
timestamp: timestamp
}
);
}
History {
chat_t: chat_t,
chat_id: chat_id,
is_encrypted: is_encrypted,
top_encryptor: Mutex::new(init_encryptor),
top_timestamp: Mutex::new(timestamp),
constraint: constraint as usize,
soft_ttl,
hard_ttl,
messages: Mutex::new(Vec::new()),
other_members: Mutex::new(initial_members[1..].to_owned())
}
}
/// Add new message to [`History`]
///
/// Arguments
///
/// * `sender` --- [`Peer`] who sent this message
/// * `data` --- message
/// * `key` --- message encryption key
///
/// We do not need `&mut self` because of interior mutability
pub fn add_message(&self,
sender: Peer,
data: &impl AsRef<[u8]>,
key: [u8; 32]
) {
// create new message
let message = match self.is_encrypted {
true => Message::new(sender.clone(), data, key.clone()),
false => Message::new_encrypted(sender.clone(), data, key.clone()),
};
// update history's top key
let mut top_encryptor_map = self.top_encryptor.lock().unwrap();
let mut top_encryptor = top_encryptor_map.get_mut(&sender.id).unwrap();
top_encryptor.key.update(key);
// update history's top timestamp
top_encryptor.timestamp = message.timestamp;
*(self.top_timestamp.lock().unwrap()) = message.timestamp;
// finally, add new message to the history
let mut messages = self.messages.lock().unwrap();
(*messages).push(message);
}
/// Add new received message to [`History`]
///
/// Arguments
///
/// * `sender` --- [`Peer`] who sent this message
/// * `data` --- message
/// * `key` --- message encryption key
/// * `timestamp` --- creation time of message received
///
/// We do not need `&mut self` because of interior mutability
pub fn add_message_received(&self,
sender: Peer,
data: &impl AsRef<[u8]>,
key: [u8; 32],
timestamp: u64
) {
// create new message
let message = match self.is_encrypted {
true => Message::new_received(sender.clone(), data, key.clone(), timestamp),
false => Message::new_received_encrypted(sender.clone(), data, key.clone(), timestamp),
};
// update history's top key
let mut top_encryptor_map = self.top_encryptor.lock().unwrap();
let mut top_encryptor = top_encryptor_map.get_mut(&sender.id).unwrap();
top_encryptor.key.update(key);
// update history's top timestamp
top_encryptor.timestamp = message.timestamp;
*(self.top_timestamp.lock().unwrap()) = message.timestamp;
// finally, add new message to the history
let mut messages = self.messages.lock().unwrap();
(*messages).push(message);
}
/// Clean up [`History`]
///
/// We do not need `&mut self` because of interior mutability
pub fn cleanup(&self) {
// compute soft and hard deadlines
let now = SystemTime::now().elapsed().unwrap().as_secs();
let soft_deadline = now - self.soft_ttl;
let hard_deadline = now - self.hard_ttl;
let mut messages = self.messages.lock().unwrap();
// drop all old messages
(*messages).retain(|m| m.timestamp > hard_deadline);
// leave in history only `constraint` number of messages older than `soft_ttl`
while (*messages).len() > self.constraint && (*messages)[0].timestamp < soft_deadline {
(*messages).remove(0);
}
}
/// Rearrange history in case recieved message is older than the last in history
///
/// Potentially heavy
pub fn rearrange(&self) {
let mut messages = self.messages.lock().unwrap();
messages.sort_by_key(|m| m.timestamp);
}
/// Equivalent for [`ChatSynchronizer::new`]
pub fn synchronizer(&self) -> ChatSynchronizer {
ChatSynchronizer {
chat_id: self.chat_id,
timestamp: self.top_timestamp.lock().unwrap().to_owned()
}
}
/// Get other chat members
pub fn members(&self) -> Vec<Peer> {
self.other_members.lock().unwrap().to_owned()
}
}