Commit 72ef50f3ac
Unsigned
Layout: unified · split
src/room/buffer.rs +14
| @@ -62,6 +62,20 @@ impl RoomBuffer { | |||
| 62 | reply_sender_id_from_tags(&line.tags()) | 62 | reply_sender_id_from_tags(&line.tags()) |
| 63 | } | 63 | } |
| 64 | 64 | ||
| 65 | /// Return whether an event is already rendered in the buffer. | ||
| 66 | pub fn contains_event(&self, event_id: &EventId) -> bool { | ||
| 67 | let buffer_handle = self.buffer_handle(); | ||
| 68 | let Ok(buffer) = buffer_handle.upgrade() else { | ||
| 69 | return false; | ||
| 70 | }; | ||
| 71 | let event_id_tag = Cow::from(event_id.to_tag()); | ||
| 72 | |||
| 73 | buffer | ||
| 74 | .lines() | ||
| 75 | .rfind(|line| line.tags().contains(&event_id_tag)) | ||
| 76 | .is_some() | ||
| 77 | } | ||
| 78 | |||
| 65 | /// Replace the local echo of an event with a fully rendered one. | 79 | /// Replace the local echo of an event with a fully rendered one. |
| 66 | pub fn replace_local_echo( | 80 | pub fn replace_local_echo( |
| 67 | &self, | 81 | &self, |
src/room/mod.rs +55 −8
| @@ -36,7 +36,7 @@ use verification::Verification; | |||
| 36 | use std::{ | 36 | use std::{ |
| 37 | borrow::Cow, | 37 | borrow::Cow, |
| 38 | cell::RefCell, | 38 | cell::RefCell, |
| 39 | collections::HashMap, | 39 | collections::{HashMap, HashSet}, |
| 40 | ops::Deref, | 40 | ops::Deref, |
| 41 | path::PathBuf, | 41 | path::PathBuf, |
| 42 | rc::Rc, | 42 | rc::Rc, |
| @@ -180,12 +180,14 @@ pub struct MessageQueue { | |||
| 180 | queue: Rc< | 180 | queue: Rc< |
| 181 | RefCell<HashMap<OwnedTransactionId, (bool, RoomMessageEventContent)>>, | 181 | RefCell<HashMap<OwnedTransactionId, (bool, RoomMessageEventContent)>>, |
| 182 | >, | 182 | >, |
| 183 | rendering: Rc<RefCell<HashSet<OwnedEventId>>>, | ||
| 183 | } | 184 | } |
| 184 | 185 | ||
| 185 | impl MessageQueue { | 186 | impl MessageQueue { |
| 186 | fn new() -> Self { | 187 | fn new() -> Self { |
| 187 | Self { | 188 | Self { |
| 188 | queue: Rc::new(RefCell::new(HashMap::new())), | 189 | queue: Rc::new(RefCell::new(HashMap::new())), |
| 190 | rendering: Rc::new(RefCell::new(HashSet::new())), | ||
| 189 | } | 191 | } |
| 190 | } | 192 | } |
| 191 | 193 | ||
| @@ -207,6 +209,24 @@ impl MessageQueue { | |||
| 207 | ) -> Option<(bool, RoomMessageEventContent)> { | 209 | ) -> Option<(bool, RoomMessageEventContent)> { |
| 208 | self.queue.borrow_mut().remove(uuid) | 210 | self.queue.borrow_mut().remove(uuid) |
| 209 | } | 211 | } |
| 212 | |||
| 213 | fn start_response( | ||
| 214 | &self, | ||
| 215 | uuid: &TransactionId, | ||
| 216 | event_id: &EventId, | ||
| 217 | ) -> Option<(bool, RoomMessageEventContent)> { | ||
| 218 | let message = self.remove(uuid)?; | ||
| 219 | self.rendering.borrow_mut().insert(event_id.to_owned()); | ||
| 220 | Some(message) | ||
| 221 | } | ||
| 222 | |||
| 223 | fn finish_response(&self, event_id: &EventId) { | ||
| 224 | self.rendering.borrow_mut().remove(event_id); | ||
| 225 | } | ||
| 226 | |||
| 227 | fn response_in_progress(&self, event_id: &EventId) -> bool { | ||
| 228 | self.rendering.borrow().contains(event_id) | ||
| 229 | } | ||
| 210 | } | 230 | } |
| 211 | 231 | ||
| 212 | enum TransactionEventHandling { | 232 | enum TransactionEventHandling { |
| @@ -782,13 +802,10 @@ impl MatrixRoom { | |||
| 782 | .await | 802 | .await |
| 783 | { | 803 | { |
| 784 | Ok(r) => { | 804 | Ok(r) => { |
| 785 | if let TransactionEventHandling::LocalEcho { | 805 | if let Some((echo, content)) = self |
| 786 | echo, | 806 | .outgoing_messages |
| 787 | content, | 807 | .start_response(&transaction_id, &r.event_id) |
| 788 | } = take_transaction_event( | 808 | { |
| 789 | &self.outgoing_messages, | ||
| 790 | Some(&transaction_id), | ||
| 791 | ) { | ||
| 792 | self.handle_outgoing_message( | 809 | self.handle_outgoing_message( |
| 793 | &transaction_id, | 810 | &transaction_id, |
| 794 | &r.event_id, | 811 | &r.event_id, |
| @@ -796,6 +813,7 @@ impl MatrixRoom { | |||
| 796 | content, | 813 | content, |
| 797 | ) | 814 | ) |
| 798 | .await; | 815 | .await; |
| 816 | self.outgoing_messages.finish_response(&r.event_id); | ||
| 799 | } | 817 | } |
| 800 | } | 818 | } |
| 801 | Err(_e) => { | 819 | Err(_e) => { |
| @@ -1177,6 +1195,17 @@ impl MatrixRoom { | |||
| 1177 | .mark_active(event.sender(), event.origin_server_ts()); | 1195 | .mark_active(event.sender(), event.origin_server_ts()); |
| 1178 | 1196 | ||
| 1179 | if let Some(id) = event.transaction_id() { | 1197 | if let Some(id) = event.transaction_id() { |
| 1198 | // The send response may have rendered this event before /sync | ||
| 1199 | // arrived. | ||
| 1200 | if !event.is_edit() | ||
| 1201 | && (self | ||
| 1202 | .outgoing_messages | ||
| 1203 | .response_in_progress(event.event_id()) | ||
| 1204 | || self.buffer.contains_event(event.event_id())) | ||
| 1205 | { | ||
| 1206 | return; | ||
| 1207 | } | ||
| 1208 | |||
| 1180 | if let TransactionEventHandling::LocalEcho { echo, content } = | 1209 | if let TransactionEventHandling::LocalEcho { echo, content } = |
| 1181 | take_transaction_event(&self.outgoing_messages, Some(id)) | 1210 | take_transaction_event(&self.outgoing_messages, Some(id)) |
| 1182 | { | 1211 | { |
| @@ -1433,4 +1462,22 @@ mod tests { | |||
| 1433 | TransactionEventHandling::RenderNormally | 1462 | TransactionEventHandling::RenderNormally |
| 1434 | )); | 1463 | )); |
| 1435 | } | 1464 | } |
| 1465 | |||
| 1466 | #[test] | ||
| 1467 | fn send_response_reserves_event_until_render_finishes() { | ||
| 1468 | let queue = MessageQueue::new(); | ||
| 1469 | let transaction_id = TransactionId::new(); | ||
| 1470 | let event_id = EventId::parse("$event:example.org").unwrap(); | ||
| 1471 | |||
| 1472 | queue.add(transaction_id.clone(), text_content("sent message")); | ||
| 1473 | assert!(queue.start_response(&transaction_id, &event_id).is_some()); | ||
| 1474 | assert!(queue.response_in_progress(&event_id)); | ||
| 1475 | assert!(matches!( | ||
| 1476 | take_transaction_event(&queue, Some(&transaction_id)), | ||
| 1477 | TransactionEventHandling::RenderNormally | ||
| 1478 | )); | ||
| 1479 | |||
| 1480 | queue.finish_response(&event_id); | ||
| 1481 | assert!(!queue.response_in_progress(&event_id)); | ||
| 1482 | } | ||
| 1436 | } | 1483 | } |