-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathchat_history_model.rs
More file actions
364 lines (301 loc) · 11.7 KB
/
chat_history_model.rs
File metadata and controls
364 lines (301 loc) · 11.7 KB
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
use glib::clone;
use gtk::prelude::*;
use gtk::subclass::prelude::*;
use gtk::{gio, glib};
use itertools::Itertools;
use std::cmp::Ordering;
use thiserror::Error;
use crate::session::content::{ChatHistoryItem, ChatHistoryItemType, MessageStyle};
use crate::tdlib::{Chat, Message};
#[derive(Error, Debug)]
pub(crate) enum ChatHistoryError {
#[error("The chat history is already loading messages")]
AlreadyLoading,
#[error("TDLib error: {0:?}")]
Tdlib(tdlib::types::Error),
}
mod imp {
use super::*;
use glib::WeakRef;
use once_cell::sync::Lazy;
use std::cell::{Cell, RefCell};
use std::collections::VecDeque;
#[derive(Debug, Default)]
pub(crate) struct ChatHistoryModel {
pub(super) chat: WeakRef<Chat>,
pub(super) is_loading: Cell<bool>,
pub(super) list: RefCell<VecDeque<ChatHistoryItem>>,
}
#[glib::object_subclass]
impl ObjectSubclass for ChatHistoryModel {
const NAME: &'static str = "ContentChatHistoryModel";
type Type = super::ChatHistoryModel;
type Interfaces = (gio::ListModel,);
}
impl ObjectImpl for ChatHistoryModel {
fn properties() -> &'static [glib::ParamSpec] {
static PROPERTIES: Lazy<Vec<glib::ParamSpec>> = Lazy::new(|| {
vec![glib::ParamSpecObject::builder::<Chat>("chat")
.read_only()
.build()]
});
PROPERTIES.as_ref()
}
fn property(&self, _id: usize, pspec: &glib::ParamSpec) -> glib::Value {
let obj = self.obj();
match pspec.name() {
"chat" => obj.chat().to_value(),
_ => unimplemented!(),
}
}
}
impl ListModelImpl for ChatHistoryModel {
fn item_type(&self) -> glib::Type {
ChatHistoryItem::static_type()
}
fn n_items(&self) -> u32 {
self.list.borrow().len() as u32
}
fn item(&self, position: u32) -> Option<glib::Object> {
self.list
.borrow()
.get(position as usize)
.map(glib::object::Cast::upcast_ref::<glib::Object>)
.cloned()
}
}
}
glib::wrapper! {
pub(crate) struct ChatHistoryModel(ObjectSubclass<imp::ChatHistoryModel>)
@implements gio::ListModel;
}
impl ChatHistoryModel {
pub(crate) fn new(chat: &Chat) -> Self {
let obj: ChatHistoryModel = glib::Object::new();
obj.imp().chat.set(Some(chat));
chat.connect_new_message(clone!(@weak obj => move |_, message| {
obj.push_front(message);
}));
chat.connect_deleted_message(clone!(@weak obj => move |_, message| {
obj.remove(message);
}));
obj
}
/// Loads older messages from this chat history.
///
/// Returns `true` when more messages can be loaded.
pub(crate) async fn load_older_messages(&self, limit: i32) -> Result<bool, ChatHistoryError> {
let imp = self.imp();
if imp.is_loading.get() {
return Err(ChatHistoryError::AlreadyLoading);
}
let oldest_message_id = imp
.list
.borrow()
.iter()
.rev()
.find_map(|item| item.message())
.map(|m| m.id())
.unwrap_or_default();
imp.is_loading.set(true);
let result = self.chat().get_chat_history(oldest_message_id, limit).await;
imp.is_loading.set(false);
let messages = result.map_err(ChatHistoryError::Tdlib)?;
if messages.is_empty() {
return Ok(false);
}
self.append(messages);
Ok(true)
}
fn items_changed(&self, position: u32, removed: u32, added: u32) {
let imp = self.imp();
// Insert day dividers where needed
let added = {
let position = position as usize;
let added = added as usize;
let mut list = imp.list.borrow_mut();
let mut previous_timestamp = if position + 1 < list.len() {
list.get(position + 1)
.and_then(|item| item.message_timestamp())
} else {
None
};
let mut dividers: Vec<(usize, ChatHistoryItem)> = vec![];
for (index, current) in list.range(position..position + added).enumerate().rev() {
if let Some(current_timestamp) = current.message_timestamp() {
if Some(current_timestamp.ymd()) != previous_timestamp.as_ref().map(|t| t.ymd())
{
let divider_pos = position + index + 1;
dividers.push((
divider_pos,
ChatHistoryItem::for_day_divider(current_timestamp.clone()),
));
previous_timestamp = Some(current_timestamp);
}
}
}
let dividers_len = dividers.len();
for (position, item) in dividers {
list.insert(position, item);
}
(added + dividers_len) as u32
};
// Check and remove no more needed day divider after removing messages
let removed = {
let mut removed = removed as usize;
if removed > 0 {
let mut list = imp.list.borrow_mut();
let position = position as usize;
let item_before_removed = list.get(position);
if let Some(ChatHistoryItemType::DayDivider(_)) =
item_before_removed.map(|i| i.type_())
{
let item_after_removed = if position > 0 {
list.get(position - 1)
} else {
None
};
match item_after_removed.map(|item| item.type_()) {
None | Some(ChatHistoryItemType::DayDivider(_)) => {
list.remove(position + removed);
removed += 1;
}
_ => {}
}
}
}
removed as u32
};
// Check and remove no more needed day divider after adding messages
let (position, removed) = {
let mut removed = removed;
let mut position = position as usize;
if added > 0 && position > 0 {
let mut list = imp.list.borrow_mut();
let last_added_timestamp = list.get(position).unwrap().message_timestamp().unwrap();
let next_item = list.get(position - 1);
if let Some(ChatHistoryItemType::DayDivider(date)) =
next_item.map(|item| item.type_())
{
if date.ymd() == last_added_timestamp.ymd() {
list.remove(position - 1);
removed += 1;
position -= 1;
}
}
}
(position as u32, removed)
};
// Set message styles
{
let list = imp.list.borrow_mut();
let range = list.range((position as usize)..(position as usize + added as usize));
for (_, mut group) in &range.group_by(|item| item.group_key()) {
use MessageStyle::*;
if let Some(last) = group.next() {
if let Some(first) = group
.map(|m| {
m.set_style(Center);
m
})
.last()
{
last.set_style(Last);
first.set_style(First);
} else {
last.set_style(Single);
}
}
}
// Check corner cases
fn refresh_grouping_at_connection(first: &ChatHistoryItem, second: &ChatHistoryItem) {
if first.group_key() == second.group_key() {
use MessageStyle::*;
let new_style = match first.style() {
Single => First,
Last => Center,
other => other,
};
first.set_style(new_style);
let new_style = match second.style() {
Single => Last,
First => Center,
other => other,
};
second.set_style(new_style);
}
}
if position > 0 {
if let Some(after_last) = list.get(position as usize - 1) {
if let Some(last) = list.get(position as usize) {
refresh_grouping_at_connection(last, after_last);
}
}
}
if position + added + 1 < list.len() as u32 {
if let Some(before_first) = list.get((position + added) as usize + 1) {
if let Some(first) = list.get((position + added) as usize) {
refresh_grouping_at_connection(before_first, first);
}
}
}
}
self.upcast_ref::<gio::ListModel>()
.items_changed(position, removed, added);
}
fn push_front(&self, message: Message) {
self.imp()
.list
.borrow_mut()
.push_front(ChatHistoryItem::for_message(message));
self.items_changed(0, 0, 1);
}
fn append(&self, messages: Vec<Message>) {
let imp = self.imp();
let added = messages.len();
imp.list.borrow_mut().reserve(added);
for message in messages {
imp.list
.borrow_mut()
.push_back(ChatHistoryItem::for_message(message));
}
let index = imp.list.borrow().len() - added;
self.items_changed(index as u32, 0, added as u32);
}
fn remove(&self, message: Message) {
let imp = self.imp();
// Put this in a block, so that we only need to borrow the list once and the runtime
// borrow checker does not panic in Self::items_changed when it borrows the list again.
let index = {
let mut list = imp.list.borrow_mut();
// The elements in this list are ordered. While the day dividers are ordered
// only by their date time, the messages are additionally sorted by their id. We
// can exploit this by applying a binary search.
let index = list
.binary_search_by(|m| match m.type_() {
ChatHistoryItemType::Message(other_message) => {
message.id().cmp(&other_message.id())
}
ChatHistoryItemType::DayDivider(date_time) => {
let ordering = glib::DateTime::from_unix_utc(message.date() as i64)
.unwrap()
.cmp(date_time);
if let Ordering::Equal = ordering {
// We found the day divider of the message. Therefore, the message
// must be among the following elements.
Ordering::Greater
} else {
ordering
}
}
})
.unwrap();
list.remove(index);
index as u32
};
self.items_changed(index, 1, 0);
}
pub(crate) fn chat(&self) -> Chat {
self.imp().chat.upgrade().unwrap()
}
}