Commit 505ef13448
Unsigned
Layout: unified · split
src/room/mod.rs +125 −25
| @@ -227,6 +227,26 @@ impl MessageQueue { | ||
| 227 | 227 | } |
| 228 | 228 | } |
| 229 | 229 | |
| 230 | enum TransactionEventHandling { | |
| 231 | RenderNormally, | |
| 232 | LocalEcho { | |
| 233 | echo: bool, | |
| 234 | content: RoomMessageEventContent, | |
| 235 | }, | |
| 236 | } | |
| 237 | ||
| 238 | fn take_transaction_event( | |
| 239 | queue: &MessageQueue, | |
| 240 | transaction_id: Option<&TransactionId>, | |
| 241 | ) -> TransactionEventHandling { | |
| 242 | match transaction_id.and_then(|id| queue.remove(id)) { | |
| 243 | Some((echo, content)) => { | |
| 244 | TransactionEventHandling::LocalEcho { echo, content } | |
| 245 | } | |
| 246 | None => TransactionEventHandling::RenderNormally, | |
| 247 | } | |
| 248 | } | |
| 249 | ||
| 230 | 250 | impl RoomHandle { |
| 231 | 251 | pub fn new( |
| 232 | 252 | server_name: &str, |
| @@ -775,8 +795,21 @@ impl MatrixRoom { | ||
| 775 | 795 | .await |
| 776 | 796 | { |
| 777 | 797 | Ok(r) => { |
| 778 | self.handle_outgoing_message(&transaction_id, &r.event_id) | |
| 798 | if let TransactionEventHandling::LocalEcho { | |
| 799 | echo, | |
| 800 | content, | |
| 801 | } = take_transaction_event( | |
| 802 | &self.outgoing_messages, | |
| 803 | Some(&transaction_id), | |
| 804 | ) { | |
| 805 | self.handle_outgoing_message( | |
| 806 | &transaction_id, | |
| 807 | &r.event_id, | |
| 808 | echo, | |
| 809 | content, | |
| 810 | ) | |
| 779 | 811 | .await; |
| 812 | } | |
| 780 | 813 | } |
| 781 | 814 | Err(_e) => { |
| 782 | 815 | // TODO: print out an error, remember to modify the local |
| @@ -1086,32 +1119,30 @@ impl MatrixRoom { | ||
| 1086 | 1119 | &self, |
| 1087 | 1120 | transaction_id: &TransactionId, |
| 1088 | 1121 | event_id: &EventId, |
| 1122 | echo: bool, | |
| 1123 | content: RoomMessageEventContent, | |
| 1089 | 1124 | ) { |
| 1090 | if let Some((echo, content)) = | |
| 1091 | self.outgoing_messages.remove(transaction_id) | |
| 1092 | { | |
| 1093 | let event = OriginalSyncMessageLikeEvent { | |
| 1094 | sender: (*self.own_user_id).to_owned(), | |
| 1095 | origin_server_ts: MilliSecondsSinceUnixEpoch::now(), | |
| 1096 | event_id: event_id.to_owned(), | |
| 1097 | content, | |
| 1098 | unsigned: Default::default(), | |
| 1099 | }; | |
| 1125 | let event = OriginalSyncMessageLikeEvent { | |
| 1126 | sender: (*self.own_user_id).to_owned(), | |
| 1127 | origin_server_ts: MilliSecondsSinceUnixEpoch::now(), | |
| 1128 | event_id: event_id.to_owned(), | |
| 1129 | content, | |
| 1130 | unsigned: Default::default(), | |
| 1131 | }; | |
| 1100 | 1132 | |
| 1101 | let event = AnySyncMessageLikeEvent::RoomMessage( | |
| 1102 | SyncMessageLikeEvent::Original(event), | |
| 1103 | ); | |
| 1133 | let event = AnySyncMessageLikeEvent::RoomMessage( | |
| 1134 | SyncMessageLikeEvent::Original(event), | |
| 1135 | ); | |
| 1104 | 1136 | |
| 1105 | let rendered = self | |
| 1106 | .render_sync_message(&event) | |
| 1107 | .await | |
| 1108 | .expect("Sent out an event that we don't know how to render"); | |
| 1137 | let rendered = self | |
| 1138 | .render_sync_message(&event) | |
| 1139 | .await | |
| 1140 | .expect("Sent out an event that we don't know how to render"); | |
| 1109 | 1141 | |
| 1110 | if echo { | |
| 1111 | self.buffer.replace_local_echo(transaction_id, rendered); | |
| 1112 | } else { | |
| 1113 | self.buffer.print_rendered_event(rendered); | |
| 1114 | } | |
| 1142 | if echo { | |
| 1143 | self.buffer.replace_local_echo(transaction_id, rendered); | |
| 1144 | } else { | |
| 1145 | self.buffer.print_rendered_event(rendered); | |
| 1115 | 1146 | } |
| 1116 | 1147 | |
| 1117 | 1148 | self.mark_event_as_read(event_id.to_owned(), false); |
| @@ -1159,8 +1190,18 @@ impl MatrixRoom { | ||
| 1159 | 1190 | .mark_active(event.sender(), event.origin_server_ts()); |
| 1160 | 1191 | |
| 1161 | 1192 | if let Some(id) = event.transaction_id() { |
| 1162 | self.handle_outgoing_message(id, event.event_id()).await; | |
| 1163 | return; | |
| 1193 | if let TransactionEventHandling::LocalEcho { echo, content } = | |
| 1194 | take_transaction_event(&self.outgoing_messages, Some(id)) | |
| 1195 | { | |
| 1196 | self.handle_outgoing_message( | |
| 1197 | id, | |
| 1198 | event.event_id(), | |
| 1199 | echo, | |
| 1200 | content, | |
| 1201 | ) | |
| 1202 | .await; | |
| 1203 | return; | |
| 1204 | } | |
| 1164 | 1205 | } |
| 1165 | 1206 | |
| 1166 | 1207 | if let AnySyncMessageLikeEvent::RoomRedaction(r) = event { |
| @@ -1334,3 +1375,62 @@ impl MatrixRoom { | ||
| 1334 | 1375 | } |
| 1335 | 1376 | } |
| 1336 | 1377 | } |
| 1378 | ||
| 1379 | #[cfg(test)] | |
| 1380 | mod tests { | |
| 1381 | use super::*; | |
| 1382 | ||
| 1383 | fn text_content(body: &str) -> RoomMessageEventContent { | |
| 1384 | RoomMessageEventContent::text_plain(body) | |
| 1385 | } | |
| 1386 | ||
| 1387 | #[test] | |
| 1388 | fn transaction_event_without_id_renders_normally() { | |
| 1389 | let queue = MessageQueue::new(); | |
| 1390 | ||
| 1391 | assert!(matches!( | |
| 1392 | take_transaction_event(&queue, None), | |
| 1393 | TransactionEventHandling::RenderNormally | |
| 1394 | )); | |
| 1395 | } | |
| 1396 | ||
| 1397 | #[test] | |
| 1398 | fn unmatched_transaction_event_renders_normally() { | |
| 1399 | let queue = MessageQueue::new(); | |
| 1400 | let queued_id = TransactionId::new(); | |
| 1401 | let other_id = TransactionId::new(); | |
| 1402 | ||
| 1403 | queue.add(queued_id.clone(), text_content("queued message")); | |
| 1404 | ||
| 1405 | assert!(matches!( | |
| 1406 | take_transaction_event(&queue, Some(&other_id)), | |
| 1407 | TransactionEventHandling::RenderNormally | |
| 1408 | )); | |
| 1409 | assert!(matches!( | |
| 1410 | take_transaction_event(&queue, Some(&queued_id)), | |
| 1411 | TransactionEventHandling::LocalEcho { .. } | |
| 1412 | )); | |
| 1413 | } | |
| 1414 | ||
| 1415 | #[test] | |
| 1416 | fn matched_transaction_event_is_consumed_once() { | |
| 1417 | let queue = MessageQueue::new(); | |
| 1418 | let transaction_id = TransactionId::new(); | |
| 1419 | ||
| 1420 | queue.add_with_echo(transaction_id.clone(), text_content("local echo")); | |
| 1421 | ||
| 1422 | match take_transaction_event(&queue, Some(&transaction_id)) { | |
| 1423 | TransactionEventHandling::LocalEcho { echo, .. } => { | |
| 1424 | assert!(echo); | |
| 1425 | } | |
| 1426 | TransactionEventHandling::RenderNormally => { | |
| 1427 | panic!("expected the queued local echo to be consumed"); | |
| 1428 | } | |
| 1429 | } | |
| 1430 | ||
| 1431 | assert!(matches!( | |
| 1432 | take_transaction_event(&queue, Some(&transaction_id)), | |
| 1433 | TransactionEventHandling::RenderNormally | |
| 1434 | )); | |
| 1435 | } | |
| 1436 | } | |