From fbed7ce02c68dbcb639e5d3cee55e35871299fd1 Mon Sep 17 00:00:00 2001 From: cameronvoell Date: Thu, 19 Dec 2024 14:51:52 -0800 Subject: [PATCH] impl from encoded content for queryable content fields --- xmtp_mls/src/groups/mls_sync.rs | 2 +- xmtp_mls/src/groups/mod.rs | 42 ++++++++----------- .../storage/encrypted_store/group_message.rs | 28 +++++++------ 3 files changed, 34 insertions(+), 38 deletions(-) diff --git a/xmtp_mls/src/groups/mls_sync.rs b/xmtp_mls/src/groups/mls_sync.rs index b1a60ec87..4550abfc3 100644 --- a/xmtp_mls/src/groups/mls_sync.rs +++ b/xmtp_mls/src/groups/mls_sync.rs @@ -967,7 +967,7 @@ where sender_installation_id, sender_inbox_id, delivery_status: DeliveryStatus::Published, - content_type: ContentType::from_string(&content_type.type_id), + content_type: content_type.type_id.into(), version_major: content_type.version_major as i32, version_minor: content_type.version_minor as i32, authority_id: content_type.authority_id.to_string(), diff --git a/xmtp_mls/src/groups/mod.rs b/xmtp_mls/src/groups/mod.rs index 1431925b5..3c1320f6c 100644 --- a/xmtp_mls/src/groups/mod.rs +++ b/xmtp_mls/src/groups/mod.rs @@ -328,6 +328,19 @@ impl Default for QueryableContentFields { } } +impl From for QueryableContentFields { + fn from(content: EncodedContent) -> Self { + let content_type_id = content.r#type.unwrap_or_default(); + + QueryableContentFields { + content_type: content_type_id.type_id.into(), + version_major: content_type_id.version_major as i32, + version_minor: content_type_id.version_minor as i32, + authority_id: content_type_id.authority_id.to_string(), + } + } +} + /// Represents a group, which can contain anywhere from 1 to MAX_GROUP_SIZE inboxes. /// /// This is a wrapper around OpenMLS's `MlsGroup` that handles our application-level configuration @@ -727,32 +740,11 @@ impl MlsGroup { /// Helper function to extract queryable content fields from a message fn extract_queryable_content_fields(message: &[u8]) -> QueryableContentFields { - let default = QueryableContentFields { - content_type: ContentType::Unknown, - version_major: 0, - version_minor: 0, - authority_id: "unknown".to_string(), - }; - // Return early with default if decoding fails or type is missing - let content_type_id = match EncodedContent::decode(message) - .map_err(|e| tracing::debug!("Failed to decode message as EncodedContent: {}", e)) - .ok() - .and_then(|content| content.r#type) - { - Some(type_id) => type_id, - None => { - tracing::debug!("Message content type is missing"); - return default; - } - }; - - QueryableContentFields { - content_type: ContentType::from_string(&content_type_id.type_id), - version_major: content_type_id.version_major as i32, - version_minor: content_type_id.version_minor as i32, - authority_id: content_type_id.authority_id.to_string(), - } + EncodedContent::decode(message) + .inspect_err(|e| tracing::debug!("Failed to decode message as EncodedContent: {}", e)) + .map(QueryableContentFields::from) + .unwrap_or_default() } /// Prepare a [`IntentKind::SendMessage`] intent, and [`StoredGroupMessage`] on this users XMTP [`Client`]. diff --git a/xmtp_mls/src/storage/encrypted_store/group_message.rs b/xmtp_mls/src/storage/encrypted_store/group_message.rs index a452ecc3c..5c75c3a79 100644 --- a/xmtp_mls/src/storage/encrypted_store/group_message.rs +++ b/xmtp_mls/src/storage/encrypted_store/group_message.rs @@ -96,22 +96,26 @@ pub enum ContentType { GroupUpdated = 3, } -impl ContentType { - pub fn from_string(type_id: &str) -> Self { - match type_id { - text::TextCodec::TYPE_ID => Self::Text, - membership_change::GroupMembershipChangeCodec::TYPE_ID => Self::GroupMembershipChange, - group_updated::GroupUpdatedCodec::TYPE_ID => Self::GroupUpdated, - _ => Self::Unknown, - } - } - - pub fn to_string(&self) -> &'static str { - match self { +impl std::fmt::Display for ContentType { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + let as_string = match self { Self::Unknown => "unknown", Self::Text => text::TextCodec::TYPE_ID, Self::GroupMembershipChange => membership_change::GroupMembershipChangeCodec::TYPE_ID, Self::GroupUpdated => group_updated::GroupUpdatedCodec::TYPE_ID, + }; + + write!(f, "{}", as_string) + } +} + +impl From for ContentType { + fn from(type_id: String) -> Self { + match type_id.as_str() { + text::TextCodec::TYPE_ID => Self::Text, + membership_change::GroupMembershipChangeCodec::TYPE_ID => Self::GroupMembershipChange, + group_updated::GroupUpdatedCodec::TYPE_ID => Self::GroupUpdated, + _ => Self::Unknown, } } }