Skip to content

Commit

Permalink
WIP: chat-history: Notify visible messages to tdlib
Browse files Browse the repository at this point in the history
Fixes #73.
Supersedes #286.
  • Loading branch information
melix99 committed Feb 23, 2023
1 parent cf48b4b commit 16a3d52
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 1 deletion.
76 changes: 76 additions & 0 deletions src/session/content/chat_history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ mod imp {
use once_cell::sync::Lazy;
use once_cell::unsync::OnceCell;
use std::cell::{Cell, RefCell};
use std::collections::HashSet;

#[derive(Debug, Default, CompositeTemplate)]
#[template(resource = "/com/github/melix99/telegrand/ui/content-chat-history.ui")]
Expand All @@ -31,6 +32,7 @@ mod imp {
pub(super) message_menu: OnceCell<gtk::PopoverMenu>,
pub(super) is_auto_scrolling: Cell<bool>,
pub(super) sticky: Cell<bool>,
pub(super) visible_messages: RefCell<HashSet<i64>>,
#[template_child]
pub(super) window_title: TemplateChild<adw::WindowTitle>,
#[template_child]
Expand Down Expand Up @@ -76,6 +78,38 @@ mod imp {
widget.show_leave_chat_dialog().await;
},
);
klass.install_action_async(
"chat-history.add-visible-message",
Some("x"),
|widget, _, variant| async move {
let message_id = variant.and_then(|v| v.get()).unwrap();
if widget
.imp()
.visible_messages
.borrow_mut()
.insert(message_id)
{
println!("ADD {}", message_id);
widget.update_visible_messages().await;
}
},
);
klass.install_action_async(
"chat-history.remove-visible-message",
Some("x"),
|widget, _, variant| async move {
let message_id = variant.and_then(|v| v.get()).unwrap();
if widget
.imp()
.visible_messages
.borrow_mut()
.remove(&message_id)
{
println!("REMOVE {}", message_id);
widget.update_visible_messages().await;
}
},
);
}

fn instance_init(obj: &glib::subclass::InitializingObject<Self>) {
Expand Down Expand Up @@ -217,6 +251,48 @@ impl ChatHistory {
}
}

async fn update_visible_messages(&self) {
if let Some(chat) = self.chat() {
let client_id = chat.session().client_id();
let message_ids = self
.imp()
.visible_messages
.borrow()
.clone()
.into_iter()
.collect();
// FIXME: The following bool should be set to false after we notify tdlib about opened chats.
// See doc here: https://docs.rs/tdlib/latest/tdlib/functions/fn.view_messages.html
let force_mark_as_read = true;
let result = tdlib::functions::view_messages(
chat.id(),
0,
message_ids,
force_mark_as_read,
client_id,
)
.await;

if let Err(e) = result {
log::warn!("Error setting visible messages: {e:?}");
}

let msgs: Vec<String> = self
.imp()
.visible_messages
.borrow()
.iter()
.map(|id| {
let message = self.chat().unwrap().message(*id).unwrap();
format!("{} ||| {}", crate::strings::message_content(&message), id)
})
.collect();
dbg!(msgs);
println!();
println!();
}
}

fn open_info_dialog(&self) {
if let Some(chat) = self.chat() {
ChatInfoWindow::new(&self.parent_window(), &chat).present();
Expand Down
40 changes: 39 additions & 1 deletion src/session/content/message_row/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,13 @@ use crate::utils::spawn;

const AVATAR_SIZE: i32 = 32;
const SPACING: i32 = 6;
const VISIBLE_MESSAGE_DELAY_MILLIS: u64 = 100;

mod imp {
use super::*;
use once_cell::sync::Lazy;
use std::cell::RefCell;
use std::time::Duration;

#[derive(Debug, Default, CompositeTemplate)]
#[template(string = r#"
Expand Down Expand Up @@ -124,7 +126,43 @@ mod imp {
}
}

impl WidgetImpl for MessageRow {}
impl WidgetImpl for MessageRow {
fn map(&self) {
self.parent_map();

let obj = self.obj();
glib::timeout_add_local_once(
Duration::from_millis(VISIBLE_MESSAGE_DELAY_MILLIS),
clone!(@weak obj => move || if obj.is_mapped() {
if let Ok(message) = obj.message().downcast::<Message>() {
obj.activate_action(
"chat-history.add-visible-message",
Some(&message.id().to_variant()),
)
.unwrap();
}
}),
);
}

fn unmap(&self) {
self.parent_unmap();

let obj = self.obj();
glib::timeout_add_local_once(
Duration::from_millis(VISIBLE_MESSAGE_DELAY_MILLIS),
clone!(@weak obj => move || if !obj.is_mapped() {
if let Ok(message) = obj.message().downcast::<Message>() {
obj.activate_action(
"chat-history.remove-visible-message",
Some(&message.id().to_variant()),
)
.unwrap();
}
}),
);
}
}
}

glib::wrapper! {
Expand Down

0 comments on commit 16a3d52

Please sign in to comment.