Skip to content

Commit 16a3d52

Browse files
committed
WIP: chat-history: Notify visible messages to tdlib
Fixes #73. Supersedes #286.
1 parent cf48b4b commit 16a3d52

File tree

2 files changed

+115
-1
lines changed

2 files changed

+115
-1
lines changed

src/session/content/chat_history.rs

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ mod imp {
2121
use once_cell::sync::Lazy;
2222
use once_cell::unsync::OnceCell;
2323
use std::cell::{Cell, RefCell};
24+
use std::collections::HashSet;
2425

2526
#[derive(Debug, Default, CompositeTemplate)]
2627
#[template(resource = "/com/github/melix99/telegrand/ui/content-chat-history.ui")]
@@ -31,6 +32,7 @@ mod imp {
3132
pub(super) message_menu: OnceCell<gtk::PopoverMenu>,
3233
pub(super) is_auto_scrolling: Cell<bool>,
3334
pub(super) sticky: Cell<bool>,
35+
pub(super) visible_messages: RefCell<HashSet<i64>>,
3436
#[template_child]
3537
pub(super) window_title: TemplateChild<adw::WindowTitle>,
3638
#[template_child]
@@ -76,6 +78,38 @@ mod imp {
7678
widget.show_leave_chat_dialog().await;
7779
},
7880
);
81+
klass.install_action_async(
82+
"chat-history.add-visible-message",
83+
Some("x"),
84+
|widget, _, variant| async move {
85+
let message_id = variant.and_then(|v| v.get()).unwrap();
86+
if widget
87+
.imp()
88+
.visible_messages
89+
.borrow_mut()
90+
.insert(message_id)
91+
{
92+
println!("ADD {}", message_id);
93+
widget.update_visible_messages().await;
94+
}
95+
},
96+
);
97+
klass.install_action_async(
98+
"chat-history.remove-visible-message",
99+
Some("x"),
100+
|widget, _, variant| async move {
101+
let message_id = variant.and_then(|v| v.get()).unwrap();
102+
if widget
103+
.imp()
104+
.visible_messages
105+
.borrow_mut()
106+
.remove(&message_id)
107+
{
108+
println!("REMOVE {}", message_id);
109+
widget.update_visible_messages().await;
110+
}
111+
},
112+
);
79113
}
80114

81115
fn instance_init(obj: &glib::subclass::InitializingObject<Self>) {
@@ -217,6 +251,48 @@ impl ChatHistory {
217251
}
218252
}
219253

254+
async fn update_visible_messages(&self) {
255+
if let Some(chat) = self.chat() {
256+
let client_id = chat.session().client_id();
257+
let message_ids = self
258+
.imp()
259+
.visible_messages
260+
.borrow()
261+
.clone()
262+
.into_iter()
263+
.collect();
264+
// FIXME: The following bool should be set to false after we notify tdlib about opened chats.
265+
// See doc here: https://docs.rs/tdlib/latest/tdlib/functions/fn.view_messages.html
266+
let force_mark_as_read = true;
267+
let result = tdlib::functions::view_messages(
268+
chat.id(),
269+
0,
270+
message_ids,
271+
force_mark_as_read,
272+
client_id,
273+
)
274+
.await;
275+
276+
if let Err(e) = result {
277+
log::warn!("Error setting visible messages: {e:?}");
278+
}
279+
280+
let msgs: Vec<String> = self
281+
.imp()
282+
.visible_messages
283+
.borrow()
284+
.iter()
285+
.map(|id| {
286+
let message = self.chat().unwrap().message(*id).unwrap();
287+
format!("{} ||| {}", crate::strings::message_content(&message), id)
288+
})
289+
.collect();
290+
dbg!(msgs);
291+
println!();
292+
println!();
293+
}
294+
}
295+
220296
fn open_info_dialog(&self) {
221297
if let Some(chat) = self.chat() {
222298
ChatInfoWindow::new(&self.parent_window(), &chat).present();

src/session/content/message_row/mod.rs

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,13 @@ use crate::utils::spawn;
3636

3737
const AVATAR_SIZE: i32 = 32;
3838
const SPACING: i32 = 6;
39+
const VISIBLE_MESSAGE_DELAY_MILLIS: u64 = 100;
3940

4041
mod imp {
4142
use super::*;
4243
use once_cell::sync::Lazy;
4344
use std::cell::RefCell;
45+
use std::time::Duration;
4446

4547
#[derive(Debug, Default, CompositeTemplate)]
4648
#[template(string = r#"
@@ -124,7 +126,43 @@ mod imp {
124126
}
125127
}
126128

127-
impl WidgetImpl for MessageRow {}
129+
impl WidgetImpl for MessageRow {
130+
fn map(&self) {
131+
self.parent_map();
132+
133+
let obj = self.obj();
134+
glib::timeout_add_local_once(
135+
Duration::from_millis(VISIBLE_MESSAGE_DELAY_MILLIS),
136+
clone!(@weak obj => move || if obj.is_mapped() {
137+
if let Ok(message) = obj.message().downcast::<Message>() {
138+
obj.activate_action(
139+
"chat-history.add-visible-message",
140+
Some(&message.id().to_variant()),
141+
)
142+
.unwrap();
143+
}
144+
}),
145+
);
146+
}
147+
148+
fn unmap(&self) {
149+
self.parent_unmap();
150+
151+
let obj = self.obj();
152+
glib::timeout_add_local_once(
153+
Duration::from_millis(VISIBLE_MESSAGE_DELAY_MILLIS),
154+
clone!(@weak obj => move || if !obj.is_mapped() {
155+
if let Ok(message) = obj.message().downcast::<Message>() {
156+
obj.activate_action(
157+
"chat-history.remove-visible-message",
158+
Some(&message.id().to_variant()),
159+
)
160+
.unwrap();
161+
}
162+
}),
163+
);
164+
}
165+
}
128166
}
129167

130168
glib::wrapper! {

0 commit comments

Comments
 (0)