Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
183 changes: 174 additions & 9 deletions src/session/content/chat_history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ use glib::clone;
use gtk::prelude::*;
use gtk::subclass::prelude::*;
use gtk::{gio, glib, CompositeTemplate};
use tdlib::functions;

use crate::session::content::{ChatActionBar, ChatInfoDialog, ItemRow};
use crate::tdlib::{Chat, ChatType, SponsoredMessage};
use crate::tdlib::{Chat, ChatHistoryItem, ChatType, SponsoredMessage};
use crate::utils::spawn;
use crate::{expressions, Session};

Expand All @@ -21,6 +22,13 @@ mod imp {
pub(super) compact: Cell<bool>,
pub(super) chat: RefCell<Option<Chat>>,
pub(super) message_menu: OnceCell<gtk::PopoverMenu>,
/// This stores the previous vadjustment value of the scrolled window. This is needed to
/// mark messages as viewed since the scrolled window lags behind the current vadjustment
/// value due to the scroll animation. In this way, we can calculate the difference of both
/// vadjustment values and take that into account when we decide which messages are
/// considered as being viewed.
pub(super) prev_vadjument_value: Cell<f64>,
pub(super) sponsored_message_needs_view: Cell<bool>,
#[template_child]
pub(super) window_title: TemplateChild<adw::WindowTitle>,
#[template_child]
Expand Down Expand Up @@ -107,6 +115,10 @@ mod imp {
let adj = self.list_view.vadjustment().unwrap();
adj.connect_value_changed(clone!(@weak obj => move |adj| {
obj.load_older_messages(adj);
obj.update_viewed_messages(
&obj.chat().unwrap(),
adj.value() - obj.imp().prev_vadjument_value.replace(adj.value())
);
}));
}
}
Expand Down Expand Up @@ -163,14 +175,19 @@ impl ChatHistory {
}

fn request_sponsored_message(&self, session: &Session, chat_id: i64, list: &gio::ListStore) {
spawn(clone!(@weak session, @weak list => async move {
match SponsoredMessage::request(chat_id, &session).await {
Ok(sponsored_message) => list.append(&sponsored_message),
Err(e) => if e.code != 404 {
log::warn!("Failed to request a SponsoredMessage: {:?}", e);
},
}
}));
spawn(
clone!(@weak self as obj, @weak session, @weak list => async move {
match SponsoredMessage::request(chat_id, &session).await {
Ok(sponsored_message) => {
list.append(&sponsored_message);
obj.imp().sponsored_message_needs_view.set(true);
}
Err(e) => if e.code != 404 {
log::warn!("Failed to request a SponsoredMessage: {:?}", e);
},
}
}),
);
}

pub(crate) fn message_menu(&self) -> &gtk::PopoverMenu {
Expand All @@ -196,6 +213,12 @@ impl ChatHistory {

let imp = self.imp();

// This will mark all messages being read as soon as a new chat is opened (assuming we
// always open the chat at the end). In the future we should figure out how to scroll the
// history to the last read message.
imp.prev_vadjument_value.set(f32::MIN as f64);
imp.sponsored_message_needs_view.set(false);

if let Some(ref chat) = chat {
// Request sponsored message, if needed
let chat_history: gio::ListModel = if matches!(chat.type_(), ChatType::Supergroup(supergroup) if supergroup.is_channel())
Expand Down Expand Up @@ -225,4 +248,146 @@ impl ChatHistory {
let adj = imp.list_view.vadjustment().unwrap();
self.load_older_messages(&adj);
}

fn update_viewed_messages(&self, chat: &Chat, vadjustment_delta: f64) {
if vadjustment_delta <= 0.0 {
// We don't really need to check for messages being viewed if the user has scrolled up.
return;
}

let imp = self.imp();

let chat_id = chat.id();
let client_id = chat.session().client_id();

if chat.unread_count() > 0 {
// Set the bottommost regular message as viewed if necessary.
view_message(
self.find_bottommost_message_to_view(chat, vadjustment_delta),
chat_id,
client_id,
);
}

if imp.sponsored_message_needs_view.get() {
let sponsored_message_id =
visible_sponsored_message_id(&*imp.list_view, vadjustment_delta);
imp.sponsored_message_needs_view
.set(sponsored_message_id.is_none());

// Set the sponsored message as viewed if necessary.
view_message(sponsored_message_id, chat_id, client_id);
}
}

/// Finds the id of the bottommost regular message that needs to be marked as being viewed.
/// Returns `None` if currently no new message needs to be marked as being viewed.
fn find_bottommost_message_to_view(&self, chat: &Chat, vadjustment_delta: f64) -> Option<i64> {
struct Iter(Option<gtk::Widget>);
impl Iterator for Iter {
type Item = gtk::Widget;

fn next(&mut self) -> Option<Self::Item> {
let r = self.0.take();
self.0 = r.as_ref().and_then(|widget| widget.next_sibling());
r
}
}

let list_view = &*self.imp().list_view;
Iter(list_view.first_child())
.find_map(|widget| {
let maybe_message_viewed_info = check_message_viewed(
&widget.first_child().unwrap().downcast::<ItemRow>().unwrap(),
list_view,
vadjustment_delta,
);

maybe_message_viewed_info.and_then(|message_viewed_info| {
if message_viewed_info.message_id <= chat.last_read_inbox_message_id() {
Some(None)
} else if message_viewed_info.considered_as_viewed {
Some(Some(message_viewed_info.message_id))
} else {
None
}
})
})
.flatten()
}
}

/// Struct to indicate whether a message is considered as being viewed.
struct MessageViewedInfo {
/// The id of the message that may be visible.
message_id: i64,
/// Whether the message can be marked as viewed.
considered_as_viewed: bool,
}

/// Returns a `MessageViewedInfo` for the specified `ItemRow`. Returns `None` if the item row
/// doesn't contain a message.
/// A message is considered as being viewed if parts of it are either visible inside the specified
/// list view or if the user needs to scroll up in order to see it.
fn check_message_viewed(
item_row: &ItemRow,
list_view: &gtk::ListView,
vadjustment_delta: f64,
) -> Option<MessageViewedInfo> {
item_row
.item()
.unwrap()
.downcast_ref::<ChatHistoryItem>()
.and_then(ChatHistoryItem::message)
.map(|message| {
let (_, dest_y_top) = item_row
.translate_coordinates(list_view, 0.0, -vadjustment_delta)
.unwrap();

MessageViewedInfo {
message_id: message.id(),
considered_as_viewed: dest_y_top < list_view.height() as f64,
}
})
}

/// Returns the message id of the sponsored message if it visible inside the specified list view.
/// Returns `None` if there is either no sponsored message or it is not visible.
/// A sponsored message (S) is considered visible inside the list view (L) if `S ∩ L = S` applies.
fn visible_sponsored_message_id(list_view: &gtk::ListView, vadjustment_delta: f64) -> Option<i64> {
list_view
.first_child()
.unwrap()
.first_child()
.and_then(|child| {
let item_row = child.downcast::<ItemRow>().unwrap();

item_row
.item()
.unwrap()
.downcast_ref::<SponsoredMessage>()
.and_then(|sponsored_message| {
let (_, dest_y_top) = item_row
.translate_coordinates(list_view, 0.0, -vadjustment_delta)
.unwrap();

if dest_y_top + (item_row.height() as f64) < list_view.height() as f64
&& dest_y_top > 0.0
{
Some(sponsored_message.message_id())
} else {
None
}
})
})
}

fn view_message(maybe_message_id: Option<i64>, chat_id: i64, client_id: i32) {
if let Some(message_id) = maybe_message_id {
spawn(async move {
functions::view_messages(chat_id, 0, vec![message_id], true, client_id)
.await
.unwrap();
});
}
}
35 changes: 35 additions & 0 deletions src/tdlib/chat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ mod imp {
pub(super) is_pinned: Cell<bool>,
pub(super) unread_mention_count: Cell<i32>,
pub(super) unread_count: Cell<i32>,
pub(super) last_read_inbox_message_id: Cell<i64>,
pub(super) draft_message: RefCell<Option<BoxedDraftMessage>>,
pub(super) notification_settings: RefCell<Option<BoxedChatNotificationSettings>>,
pub(super) history: OnceCell<ChatHistory>,
Expand Down Expand Up @@ -173,6 +174,17 @@ mod imp {
| glib::ParamFlags::CONSTRUCT
| glib::ParamFlags::EXPLICIT_NOTIFY,
),
glib::ParamSpecInt64::new(
"last-read-inbox-message-id",
"Last Read Inbox Message Id",
"The identifier of the last read incoming message",
std::i64::MIN,
std::i64::MAX,
0,
glib::ParamFlags::READWRITE
| glib::ParamFlags::CONSTRUCT
| glib::ParamFlags::EXPLICIT_NOTIFY,
),
glib::ParamSpecBoxed::new(
"draft-message",
"Draft Message",
Expand Down Expand Up @@ -246,6 +258,9 @@ mod imp {
"is-pinned" => obj.set_is_pinned(value.get().unwrap()),
"unread-mention-count" => obj.set_unread_mention_count(value.get().unwrap()),
"unread-count" => obj.set_unread_count(value.get().unwrap()),
"last-read-inbox-message-id" => {
obj.set_last_read_inbox_message_id(value.get().unwrap())
}
"draft-message" => obj.set_draft_message(value.get().unwrap()),
"notification-settings" => obj.set_notification_settings(value.get().unwrap()),
"permissions" => obj.set_permissions(value.get().unwrap()),
Expand All @@ -266,6 +281,7 @@ mod imp {
"is-pinned" => obj.is_pinned().to_value(),
"unread-mention-count" => obj.unread_mention_count().to_value(),
"unread-count" => obj.unread_count().to_value(),
"last-read-inbox-message-id" => obj.last_read_inbox_message_id().to_value(),
"draft-message" => obj.draft_message().to_value(),
"notification-settings" => obj.notification_settings().to_value(),
"history" => obj.history().to_value(),
Expand Down Expand Up @@ -301,6 +317,10 @@ impl Chat {
"notification-settings",
&BoxedChatNotificationSettings(chat.notification_settings),
),
(
"last-read-inbox-message-id",
&chat.last_read_inbox_message_id,
),
("permissions", &BoxedChatPermissions(chat.permissions)),
("session", &session),
])
Expand Down Expand Up @@ -365,6 +385,7 @@ impl Chat {
}
Update::ChatReadInbox(update) => {
self.set_unread_count(update.unread_count);
self.set_last_read_inbox_message_id(update.last_read_inbox_message_id);
}
Update::ChatDraftMessage(update) => {
self.set_draft_message(update.draft_message.map(BoxedDraftMessage));
Expand Down Expand Up @@ -501,6 +522,20 @@ impl Chat {
self.notify("unread-count");
}

pub(crate) fn last_read_inbox_message_id(&self) -> i64 {
self.imp().last_read_inbox_message_id.get()
}

pub(crate) fn set_last_read_inbox_message_id(&self, last_read_inbox_message_id: i64) {
if self.last_read_inbox_message_id() == last_read_inbox_message_id {
return;
}
self.imp()
.last_read_inbox_message_id
.set(last_read_inbox_message_id);
self.notify("last-read-inbox-message-id");
}

pub(crate) fn draft_message(&self) -> Option<BoxedDraftMessage> {
self.imp().draft_message.borrow().to_owned()
}
Expand Down