|
14 | 14 |
|
15 | 15 | use std::{ |
16 | 16 | collections::{BTreeMap, BTreeSet}, |
| 17 | + fmt::Debug, |
17 | 18 | ops::Deref, |
18 | 19 | sync::Arc, |
19 | 20 | }; |
@@ -318,16 +319,10 @@ impl GroupSessionManager { |
318 | 319 | let txn_id = TransactionId::new(); |
319 | 320 | let request = ToDeviceRequest { |
320 | 321 | event_type: ToDeviceEventType::RoomEncrypted, |
321 | | - txn_id: txn_id.clone(), |
| 322 | + txn_id: txn_id.to_owned(), |
322 | 323 | messages, |
323 | 324 | }; |
324 | 325 |
|
325 | | - trace!( |
326 | | - recipient_count = request.message_count(), |
327 | | - transaction_id = ?txn_id, |
328 | | - "Created a to-device request carrying a room_key" |
329 | | - ); |
330 | | - |
331 | 326 | Ok((txn_id, request, share_infos, changed_sessions, withheld_devices)) |
332 | 327 | } |
333 | 328 |
|
@@ -449,6 +444,12 @@ impl GroupSessionManager { |
449 | 444 | Self::encrypt_session_for(outbound.clone(), chunk).await?; |
450 | 445 |
|
451 | 446 | if !request.messages.is_empty() { |
| 447 | + trace!( |
| 448 | + recipient_count = request.message_count(), |
| 449 | + transaction_id = ?id, |
| 450 | + "Created a to-device request carrying a room_key" |
| 451 | + ); |
| 452 | + |
452 | 453 | outbound.add_request(id.clone(), request.into(), share_infos); |
453 | 454 | being_shared.insert(id, outbound.clone()); |
454 | 455 | } |
@@ -545,7 +546,7 @@ impl GroupSessionManager { |
545 | 546 | let (used_sessions, failed_no_olm) = result?; |
546 | 547 |
|
547 | 548 | changes.sessions.extend(used_sessions); |
548 | | - withheld_devices.extend(failed_no_olm) |
| 549 | + withheld_devices.extend(failed_no_olm); |
549 | 550 | } |
550 | 551 |
|
551 | 552 | Ok(withheld_devices) |
@@ -650,6 +651,88 @@ impl GroupSessionManager { |
650 | 651 | Ok(()) |
651 | 652 | } |
652 | 653 |
|
| 654 | + fn log_room_key_sharing_result( |
| 655 | + session: &OutboundGroupSession, |
| 656 | + requests: &[Arc<ToDeviceRequest>], |
| 657 | + ) { |
| 658 | + #[cfg(feature = "message-ids")] |
| 659 | + use serde::Deserialize; |
| 660 | + |
| 661 | + let mut withheld_messages: BTreeMap<_, BTreeMap<_, BTreeSet<_>>> = BTreeMap::new(); |
| 662 | + |
| 663 | + #[cfg(feature = "message-ids")] |
| 664 | + let mut messages: BTreeMap<_, BTreeMap<_, BTreeMap<_, _>>> = BTreeMap::new(); |
| 665 | + #[cfg(not(feature = "message-ids"))] |
| 666 | + let mut messages: BTreeMap<_, BTreeMap<_, BTreeSet<_>>> = BTreeMap::new(); |
| 667 | + |
| 668 | + #[derive(PartialEq, Eq, PartialOrd, Ord)] |
| 669 | + struct RequestId<'a>(&'a TransactionId); |
| 670 | + |
| 671 | + impl Debug for RequestId<'_> { |
| 672 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 673 | + f.debug_tuple("RequestId").field(&self.0).finish() |
| 674 | + } |
| 675 | + } |
| 676 | + |
| 677 | + #[cfg(feature = "message-ids")] |
| 678 | + #[derive(Debug, Deserialize)] |
| 679 | + #[serde(transparent)] |
| 680 | + struct MessageId<'a>(&'a str); |
| 681 | + |
| 682 | + #[cfg(feature = "message-ids")] |
| 683 | + #[derive(Deserialize)] |
| 684 | + struct ContentStub<'a> { |
| 685 | + #[serde(borrow, rename = "org.matrix.msgid")] |
| 686 | + message_id: MessageId<'a>, |
| 687 | + } |
| 688 | + |
| 689 | + // We're just collecting the recipients for logging reasons. |
| 690 | + for request in requests { |
| 691 | + for (user_id, device_map) in &request.messages { |
| 692 | + if request.event_type == ToDeviceEventType::RoomEncrypted { |
| 693 | + #[cfg(feature = "message-ids")] |
| 694 | + for (device, content) in device_map { |
| 695 | + let content: ContentStub<'_> = content |
| 696 | + .deserialize_as() |
| 697 | + .expect("We should be able to deserialize the content we generated"); |
| 698 | + |
| 699 | + let message_id = content.message_id; |
| 700 | + |
| 701 | + messages |
| 702 | + .entry(RequestId(&request.txn_id)) |
| 703 | + .or_default() |
| 704 | + .entry(user_id) |
| 705 | + .or_default() |
| 706 | + .insert(device, message_id); |
| 707 | + } |
| 708 | + |
| 709 | + #[cfg(not(feature = "message-ids"))] |
| 710 | + messages |
| 711 | + .entry(RequestId(&request.txn_id)) |
| 712 | + .or_default() |
| 713 | + .entry(user_id) |
| 714 | + .or_default() |
| 715 | + .extend(device_map.keys()); |
| 716 | + } else { |
| 717 | + withheld_messages |
| 718 | + .entry(RequestId(&request.txn_id)) |
| 719 | + .or_default() |
| 720 | + .entry(user_id) |
| 721 | + .or_default() |
| 722 | + .extend(device_map.keys()); |
| 723 | + } |
| 724 | + } |
| 725 | + } |
| 726 | + |
| 727 | + info!( |
| 728 | + session_id = session.session_id(), |
| 729 | + request_count = requests.len(), |
| 730 | + ?messages, |
| 731 | + ?withheld_messages, |
| 732 | + "Encrypted a room key and created to-device messages" |
| 733 | + ); |
| 734 | + } |
| 735 | + |
653 | 736 | /// Get to-device requests to share a room key with users in a room. |
654 | 737 | /// |
655 | 738 | /// # Arguments |
@@ -741,32 +824,7 @@ impl GroupSessionManager { |
741 | 824 | changes.outbound_group_sessions.push(outbound.clone()); |
742 | 825 | } |
743 | 826 | } else { |
744 | | - let mut recipients: BTreeMap<_, BTreeSet<_>> = BTreeMap::new(); |
745 | | - let mut withheld_recipients: BTreeMap<_, BTreeSet<_>> = BTreeMap::new(); |
746 | | - |
747 | | - // We're just collecting the recipients for logging reasons. |
748 | | - for request in &requests { |
749 | | - for (user_id, device_map) in &request.messages { |
750 | | - let devices = device_map.keys(); |
751 | | - if request.event_type == ToDeviceEventType::RoomEncrypted { |
752 | | - recipients.entry(user_id).or_default().extend(devices) |
753 | | - } else { |
754 | | - withheld_recipients.entry(user_id).or_default().extend(devices) |
755 | | - } |
756 | | - } |
757 | | - } |
758 | | - |
759 | | - let transaction_ids: Vec<_> = requests.iter().map(|r| r.txn_id.clone()).collect(); |
760 | | - |
761 | | - info!( |
762 | | - room_id = room_id.as_str(), |
763 | | - session_id = outbound.session_id(), |
764 | | - request_count = requests.len(), |
765 | | - ?transaction_ids, |
766 | | - ?recipients, |
767 | | - ?withheld_recipients, |
768 | | - "Encrypted a room key and created to-device requests" |
769 | | - ); |
| 827 | + Self::log_room_key_sharing_result(&outbound, &requests) |
770 | 828 | } |
771 | 829 |
|
772 | 830 | // Persist any changes we might have collected. |
|
0 commit comments