Commit 7b53c933a5

7b53c933a5ada6ab9bce46e7f8b48cb8d8b2d0ec

parent: 70c6dd5e61

Unsigned

Darafei Praliaskouski <me@komzpa.net> · 2026-06-23 18:28 UTC
committer: <strk@kbt.io>

feat: mark rooms as read

Layout: unified · split

src/commands/matrix.rs +13 −2
@@ -42,13 +42,15 @@ impl MatrixCommand {
42 .add_argument("media download <mxc-uri> [file]") 42 .add_argument("media download <mxc-uri> [file]")
43 .add_argument("disconnect <server-name>") 43 .add_argument("disconnect <server-name>")
44 .add_argument("reconnect <server-name>") 44 .add_argument("reconnect <server-name>")
45 .add_argument("read")
45 .add_argument("help <matrix-command> [<matrix-subcommand>]") 46 .add_argument("help <matrix-command> [<matrix-subcommand>]")
46 .arguments_description(format!( 47 .arguments_description(format!(
47 " server: List, add, or remove Matrix servers. 48 " server: List, add, or remove Matrix servers.
48 connect: Connect to Matrix servers. 49 connect: Connect to Matrix servers.
49 disconnect: Disconnect from one or all Matrix servers. 50 disconnect: Disconnect from one or all Matrix servers.
50 reconnect: Reconnect to server(s). 51 reconnect: Reconnect to server(s).
51 join: Join a Matrix room by ID or alias. 52 join: Join a Matrix room by ID or alias.
53 read: Mark the current room as read.
52 devices: {} 54 devices: {}
53 keys: {} 55 keys: {}
54 media: {} 56 media: {}
@@ -72,7 +74,7 @@ Use /matrix [command] help to find out more.\n",
72 .add_completion("disconnect %(matrix_servers)") 74 .add_completion("disconnect %(matrix_servers)")
73 .add_completion("reconnect %(matrix_servers)") 75 .add_completion("reconnect %(matrix_servers)")
74 .add_completion( 76 .add_completion(
75 "help server|connect|disconnect|reconnect|join|keys|devices|media", 77 "help server|connect|disconnect|reconnect|join|read|keys|devices|media",
76 ); 78 );
77 79
78 Command::new( 80 Command::new(
@@ -263,6 +265,11 @@ Use /matrix [command] help to find out more.\n",
263 ("verification", Some(subargs)) => { 265 ("verification", Some(subargs)) => {
264 VerificationCommand::run(buffer, &self.servers, subargs) 266 VerificationCommand::run(buffer, &self.servers, subargs)
265 } 267 }
268 ("read", _) => {
269 if let Some(room) = self.servers.find_room(buffer) {
270 room.mark_as_read();
271 }
272 }
266 _ => unreachable!(), 273 _ => unreachable!(),
267 } 274 }
268 } 275 }
@@ -369,6 +376,10 @@ impl CommandCallback for MatrixCommand {
369 .value_name("room-id-or-alias") 376 .value_name("room-id-or-alias")
370 .required(true), 377 .required(true),
371 ), 378 ),
379 )
380 .subcommand(
381 SubCommand::with_name("read")
382 .about("Mark the current room as read."),
372 ); 383 );
373 384
374 parse_and_run(argparse, arguments, |args| self.run(buffer, args)); 385 parse_and_run(argparse, arguments, |args| self.run(buffer, args));
src/config.rs +7
@@ -145,6 +145,13 @@ config!(
145 // Default value. 145 // Default value.
146 false, 146 false,
147 }, 147 },
148
149 send_read_receipts: bool {
150 // Description
151 "Send public read receipts when marking a room as read",
152 // Default value.
153 true,
154 },
148 }, 155 },
149 156
150 Section input { 157 Section input {
src/connection.rs +21 −2
@@ -16,7 +16,7 @@ use matrix_sdk::{
16 self, 16 self,
17 config::SyncSettings, 17 config::SyncSettings,
18 deserialized_responses::AmbiguityChange, 18 deserialized_responses::AmbiguityChange,
19 room::{Messages, MessagesOptions, Room}, 19 room::{Messages, MessagesOptions, Receipts, Room},
20 ruma::{ 20 ruma::{
21 api::client::{ 21 api::client::{
22 device::{ 22 device::{
@@ -36,7 +36,7 @@ use matrix_sdk::{
36 AnySyncStateEvent, AnySyncTimelineEvent, AnyToDeviceEvent, 36 AnySyncStateEvent, AnySyncTimelineEvent, AnyToDeviceEvent,
37 SyncStateEvent, 37 SyncStateEvent,
38 }, 38 },
39 OwnedDeviceId, OwnedRoomId, OwnedTransactionId, 39 OwnedDeviceId, OwnedEventId, OwnedRoomId, OwnedTransactionId,
40 }, 40 },
41 sync::State, 41 sync::State,
42 Client, LoopCtrl, Result as MatrixResult, RoomMemberships, 42 Client, LoopCtrl, Result as MatrixResult, RoomMemberships,
@@ -231,6 +231,25 @@ impl Connection {
231 .await 231 .await
232 } 232 }
233 233
234 pub async fn mark_room_as_read(
235 &self,
236 room: Room,
237 event_id: OwnedEventId,
238 public_receipt: bool,
239 ) -> MatrixResult<()> {
240 self.spawn(async move {
241 let receipts = Receipts::new().fully_read_marker(event_id.clone());
242 let receipts = if public_receipt {
243 receipts.public_read_receipt(event_id)
244 } else {
245 receipts.private_read_receipt(event_id)
246 };
247
248 room.send_multiple_receipts(receipts).await
249 })
250 .await
251 }
252
234 fn save_device_id( 253 fn save_device_id(
235 user_name: &str, 254 user_name: &str,
236 mut server_path: PathBuf, 255 mut server_path: PathBuf,
src/lib.rs +6
@@ -174,6 +174,7 @@ impl SignalCallback for Servers {
174 if let Some(room) = self.find_room(&buffer) { 174 if let Some(room) = self.find_room(&buffer) {
175 match signal_name { 175 match signal_name {
176 "buffer_switch" => { 176 "buffer_switch" => {
177 room.mark_as_read_silent();
177 Weechat::spawn(async move { 178 Weechat::spawn(async move {
178 room.get_messages_if_empty().await 179 room.get_messages_if_empty().await
179 }) 180 })
@@ -202,6 +203,8 @@ struct Matrix {
202 #[allow(dead_code)] 203 #[allow(dead_code)]
203 history_fetch_signal: SignalHook, 204 history_fetch_signal: SignalHook,
204 #[allow(dead_code)] 205 #[allow(dead_code)]
206 read_marker_signal: SignalHook,
207 #[allow(dead_code)]
205 completions: Completions, 208 completions: Completions,
206 debug_buffer: RefCell<Option<BufferHandle>>, 209 debug_buffer: RefCell<Option<BufferHandle>>,
207} 210}
@@ -279,6 +282,8 @@ impl Plugin for Matrix {
279 .expect("Can't create signal hook for the typing notice cb"); 282 .expect("Can't create signal hook for the typing notice cb");
280 let history_fetch = SignalHook::new("buffer_switch", servers.clone()) 283 let history_fetch = SignalHook::new("buffer_switch", servers.clone())
281 .expect("Can't create signal hook for the history fetch cb"); 284 .expect("Can't create signal hook for the history fetch cb");
285 let read_marker = SignalHook::new("buffer_switch", servers.clone())
286 .expect("Can't create signal hook for the read marker cb");
282 287
283 let plugin = Matrix { 288 let plugin = Matrix {
284 global_runtime, 289 global_runtime,
@@ -290,6 +295,7 @@ impl Plugin for Matrix {
290 debug_buffer: RefCell::new(None), 295 debug_buffer: RefCell::new(None),
291 typing_notice_signal: typing, 296 typing_notice_signal: typing,
292 history_fetch_signal: history_fetch, 297 history_fetch_signal: history_fetch,
298 read_marker_signal: read_marker,
293 }; 299 };
294 300
295 Weechat::spawn(async move { 301 Weechat::spawn(async move {
src/room/mod.rs +77 −1
@@ -68,7 +68,7 @@ use matrix_sdk::{
68 AnySyncStateEvent, AnySyncTimelineEvent, AnyTimelineEvent, 68 AnySyncStateEvent, AnySyncTimelineEvent, AnyTimelineEvent,
69 OriginalSyncMessageLikeEvent, SyncMessageLikeEvent, SyncStateEvent, 69 OriginalSyncMessageLikeEvent, SyncMessageLikeEvent, SyncStateEvent,
70 }, 70 },
71 EventId, MilliSecondsSinceUnixEpoch, OwnedRoomAliasId, 71 EventId, MilliSecondsSinceUnixEpoch, OwnedEventId, OwnedRoomAliasId,
72 OwnedTransactionId, OwnedUserId, RoomId, TransactionId, UInt, UserId, 72 OwnedTransactionId, OwnedUserId, RoomId, TransactionId, UInt, UserId,
73 }, 73 },
74 StoreError, 74 StoreError,
@@ -184,6 +184,8 @@ pub struct MatrixRoom {
184 184
185 messages_in_flight: IntMutex, 185 messages_in_flight: IntMutex,
186 prev_batch: Rc<RefCell<Option<PrevBatch>>>, 186 prev_batch: Rc<RefCell<Option<PrevBatch>>>,
187 latest_event_id: Rc<RefCell<Option<OwnedEventId>>>,
188 latest_read_event_id: Rc<RefCell<Option<OwnedEventId>>>,
187 189
188 outgoing_messages: MessageQueue, 190 outgoing_messages: MessageQueue,
189 191
@@ -262,6 +264,8 @@ impl RoomHandle {
262 prev_batch: Rc::new(RefCell::new( 264 prev_batch: Rc::new(RefCell::new(
263 room.last_prev_batch().map(PrevBatch::Backwards), 265 room.last_prev_batch().map(PrevBatch::Backwards),
264 )), 266 )),
267 latest_event_id: Rc::new(RefCell::new(None)),
268 latest_read_event_id: Rc::new(RefCell::new(None)),
265 own_user_id: own_user_id.into(), 269 own_user_id: own_user_id.into(),
266 members, 270 members,
267 buffer, 271 buffer,
@@ -902,6 +906,69 @@ impl MatrixRoom {
902 *self.prev_batch.borrow_mut() = None; 906 *self.prev_batch.borrow_mut() = None;
903 } 907 }
904 908
909 fn mark_event_as_read(&self, event_id: OwnedEventId, verbose: bool) {
910 if self.latest_read_event_id.borrow().as_ref() == Some(&event_id) {
911 return;
912 }
913
914 let connection = self.connection.borrow().clone();
915 let room = self.room().clone();
916 let public_receipt =
917 self.config.borrow().network().send_read_receipts();
918 let latest_read_event_id = self.latest_read_event_id.clone();
919
920 let mark_read = async move {
921 if let Some(connection) = connection {
922 match connection
923 .mark_room_as_read(room, event_id.clone(), public_receipt)
924 .await
925 {
926 Ok(()) => {
927 *latest_read_event_id.borrow_mut() = Some(event_id);
928 }
929 Err(e) => {
930 Weechat::print(&format!(
931 "{}: Failed to mark room as read: {}",
932 PLUGIN_NAME, e
933 ));
934 }
935 }
936 } else if verbose {
937 Weechat::print(&format!(
938 "{}: Room is not connected.",
939 PLUGIN_NAME
940 ));
941 }
942 };
943
944 Weechat::spawn(mark_read).detach();
945 }
946
947 fn mark_latest_event_as_read(&self, verbose: bool) {
948 let event_id =
949 if let Some(event_id) = self.latest_event_id.borrow().clone() {
950 event_id
951 } else if verbose {
952 Weechat::print(&format!(
953 "{}: No event has been received for this room yet.",
954 PLUGIN_NAME
955 ));
956 return;
957 } else {
958 return;
959 };
960
961 self.mark_event_as_read(event_id, verbose);
962 }
963
964 pub fn mark_as_read(&self) {
965 self.mark_latest_event_as_read(true);
966 }
967
968 pub fn mark_as_read_silent(&self) {
969 self.mark_latest_event_as_read(false);
970 }
971
905 pub async fn get_messages(&self) { 972 pub async fn get_messages(&self) {
906 let messages_lock = self.messages_in_flight.clone(); 973 let messages_lock = self.messages_in_flight.clone();
907 974
@@ -999,6 +1066,8 @@ impl MatrixRoom {
999 self.buffer.print_rendered_event(rendered); 1066 self.buffer.print_rendered_event(rendered);
1000 } 1067 }
1001 } 1068 }
1069
1070 self.mark_event_as_read(event_id.to_owned(), false);
1002 } 1071 }
1003 1072
1004 async fn handle_edits(&self, event: &AnySyncMessageLikeEvent) { 1073 async fn handle_edits(&self, event: &AnySyncMessageLikeEvent) {
@@ -1119,6 +1188,13 @@ impl MatrixRoom {
1119 pub async fn handle_sync_room_event(&self, event: AnySyncTimelineEvent) { 1188 pub async fn handle_sync_room_event(&self, event: AnySyncTimelineEvent) {
1120 self.set_prev_batch(); 1189 self.set_prev_batch();
1121 1190
1191 *self.latest_event_id.borrow_mut() = Some(match &event {
1192 AnySyncTimelineEvent::MessageLike(event) => {
1193 event.event_id().to_owned()
1194 }
1195 AnySyncTimelineEvent::State(event) => event.event_id().to_owned(),
1196 });
1197
1122 match &event { 1198 match &event {
1123 AnySyncTimelineEvent::MessageLike(message) => { 1199 AnySyncTimelineEvent::MessageLike(message) => {
1124 self.handle_room_message(message).await 1200 self.handle_room_message(message).await