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 {
4242 .add_argument("media download <mxc-uri> [file]")
4343 .add_argument("disconnect <server-name>")
4444 .add_argument("reconnect <server-name>")
45 .add_argument("read")
4546 .add_argument("help <matrix-command> [<matrix-subcommand>]")
4647 .arguments_description(format!(
4748 " server: List, add, or remove Matrix servers.
4849 connect: Connect to Matrix servers.
4950 disconnect: Disconnect from one or all Matrix servers.
5051 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.
5254 devices: {}
5355 keys: {}
5456 media: {}
@@ -72,7 +74,7 @@ Use /matrix [command] help to find out more.\n",
7274 .add_completion("disconnect %(matrix_servers)")
7375 .add_completion("reconnect %(matrix_servers)")
7476 .add_completion(
75 "help server|connect|disconnect|reconnect|join|keys|devices|media",
77 "help server|connect|disconnect|reconnect|join|read|keys|devices|media",
7678 );
7779
7880 Command::new(
@@ -263,6 +265,11 @@ Use /matrix [command] help to find out more.\n",
263265 ("verification", Some(subargs)) => {
264266 VerificationCommand::run(buffer, &self.servers, subargs)
265267 }
268 ("read", _) => {
269 if let Some(room) = self.servers.find_room(buffer) {
270 room.mark_as_read();
271 }
272 }
266273 _ => unreachable!(),
267274 }
268275 }
@@ -369,6 +376,10 @@ impl CommandCallback for MatrixCommand {
369376 .value_name("room-id-or-alias")
370377 .required(true),
371378 ),
379 )
380 .subcommand(
381 SubCommand::with_name("read")
382 .about("Mark the current room as read."),
372383 );
373384
374385 parse_and_run(argparse, arguments, |args| self.run(buffer, args));
src/config.rs +7
@@ -145,6 +145,13 @@ config!(
145145 // Default value.
146146 false,
147147 },
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 },
148155 },
149156
150157 Section input {
src/connection.rs +21 −2
@@ -16,7 +16,7 @@ use matrix_sdk::{
1616 self,
1717 config::SyncSettings,
1818 deserialized_responses::AmbiguityChange,
19 room::{Messages, MessagesOptions, Room},
19 room::{Messages, MessagesOptions, Receipts, Room},
2020 ruma::{
2121 api::client::{
2222 device::{
@@ -36,7 +36,7 @@ use matrix_sdk::{
3636 AnySyncStateEvent, AnySyncTimelineEvent, AnyToDeviceEvent,
3737 SyncStateEvent,
3838 },
39 OwnedDeviceId, OwnedRoomId, OwnedTransactionId,
39 OwnedDeviceId, OwnedEventId, OwnedRoomId, OwnedTransactionId,
4040 },
4141 sync::State,
4242 Client, LoopCtrl, Result as MatrixResult, RoomMemberships,
@@ -231,6 +231,25 @@ impl Connection {
231231 .await
232232 }
233233
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
234253 fn save_device_id(
235254 user_name: &str,
236255 mut server_path: PathBuf,
src/lib.rs +6
@@ -174,6 +174,7 @@ impl SignalCallback for Servers {
174174 if let Some(room) = self.find_room(&buffer) {
175175 match signal_name {
176176 "buffer_switch" => {
177 room.mark_as_read_silent();
177178 Weechat::spawn(async move {
178179 room.get_messages_if_empty().await
179180 })
@@ -202,6 +203,8 @@ struct Matrix {
202203 #[allow(dead_code)]
203204 history_fetch_signal: SignalHook,
204205 #[allow(dead_code)]
206 read_marker_signal: SignalHook,
207 #[allow(dead_code)]
205208 completions: Completions,
206209 debug_buffer: RefCell<Option<BufferHandle>>,
207210}
@@ -279,6 +282,8 @@ impl Plugin for Matrix {
279282 .expect("Can't create signal hook for the typing notice cb");
280283 let history_fetch = SignalHook::new("buffer_switch", servers.clone())
281284 .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");
282287
283288 let plugin = Matrix {
284289 global_runtime,
@@ -290,6 +295,7 @@ impl Plugin for Matrix {
290295 debug_buffer: RefCell::new(None),
291296 typing_notice_signal: typing,
292297 history_fetch_signal: history_fetch,
298 read_marker_signal: read_marker,
293299 };
294300
295301 Weechat::spawn(async move {
src/room/mod.rs +77 −1
@@ -68,7 +68,7 @@ use matrix_sdk::{
6868 AnySyncStateEvent, AnySyncTimelineEvent, AnyTimelineEvent,
6969 OriginalSyncMessageLikeEvent, SyncMessageLikeEvent, SyncStateEvent,
7070 },
71 EventId, MilliSecondsSinceUnixEpoch, OwnedRoomAliasId,
71 EventId, MilliSecondsSinceUnixEpoch, OwnedEventId, OwnedRoomAliasId,
7272 OwnedTransactionId, OwnedUserId, RoomId, TransactionId, UInt, UserId,
7373 },
7474 StoreError,
@@ -184,6 +184,8 @@ pub struct MatrixRoom {
184184
185185 messages_in_flight: IntMutex,
186186 prev_batch: Rc<RefCell<Option<PrevBatch>>>,
187 latest_event_id: Rc<RefCell<Option<OwnedEventId>>>,
188 latest_read_event_id: Rc<RefCell<Option<OwnedEventId>>>,
187189
188190 outgoing_messages: MessageQueue,
189191
@@ -262,6 +264,8 @@ impl RoomHandle {
262264 prev_batch: Rc::new(RefCell::new(
263265 room.last_prev_batch().map(PrevBatch::Backwards),
264266 )),
267 latest_event_id: Rc::new(RefCell::new(None)),
268 latest_read_event_id: Rc::new(RefCell::new(None)),
265269 own_user_id: own_user_id.into(),
266270 members,
267271 buffer,
@@ -902,6 +906,69 @@ impl MatrixRoom {
902906 *self.prev_batch.borrow_mut() = None;
903907 }
904908
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
905972 pub async fn get_messages(&self) {
906973 let messages_lock = self.messages_in_flight.clone();
907974
@@ -999,6 +1066,8 @@ impl MatrixRoom {
9991066 self.buffer.print_rendered_event(rendered);
10001067 }
10011068 }
1069
1070 self.mark_event_as_read(event_id.to_owned(), false);
10021071 }
10031072
10041073 async fn handle_edits(&self, event: &AnySyncMessageLikeEvent) {
@@ -1119,6 +1188,13 @@ impl MatrixRoom {
11191188 pub async fn handle_sync_room_event(&self, event: AnySyncTimelineEvent) {
11201189 self.set_prev_batch();
11211190
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
11221198 match &event {
11231199 AnySyncTimelineEvent::MessageLike(message) => {
11241200 self.handle_room_message(message).await