@@ -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 ( ) ;
0 commit comments