Skip to content

Commit

Permalink
impl from encoded content for queryable content fields
Browse files Browse the repository at this point in the history
  • Loading branch information
cameronvoell committed Dec 19, 2024
1 parent 80c7673 commit fbed7ce
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 38 deletions.
2 changes: 1 addition & 1 deletion xmtp_mls/src/groups/mls_sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
42 changes: 17 additions & 25 deletions xmtp_mls/src/groups/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,19 @@ impl Default for QueryableContentFields {
}
}

impl From<EncodedContent> 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
Expand Down Expand Up @@ -727,32 +740,11 @@ impl<ScopedClient: ScopedGroupClient> MlsGroup<ScopedClient> {

/// 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`].
Expand Down
28 changes: 16 additions & 12 deletions xmtp_mls/src/storage/encrypted_store/group_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> 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,
}
}
}
Expand Down

0 comments on commit fbed7ce

Please sign in to comment.