From 0a445160599ac0c69f2ffece091dcdcb2d1d6e5e Mon Sep 17 00:00:00 2001 From: cameronvoell Date: Thu, 19 Dec 2024 11:04:08 -0800 Subject: [PATCH] move content type enum to encrypted_store --- xmtp_content_types/Cargo.toml | 8 ++- xmtp_content_types/src/lib.rs | 67 ------------------- xmtp_mls/src/groups/mls_sync.rs | 11 ++- xmtp_mls/src/groups/mod.rs | 3 +- .../storage/encrypted_store/group_message.rs | 57 +++++++++++++++- 5 files changed, 69 insertions(+), 77 deletions(-) diff --git a/xmtp_content_types/Cargo.toml b/xmtp_content_types/Cargo.toml index 52fa200b5..09d7e907a 100644 --- a/xmtp_content_types/Cargo.toml +++ b/xmtp_content_types/Cargo.toml @@ -4,11 +4,18 @@ name = "xmtp_content_types" version.workspace = true license.workspace = true +targets = [ + "x86_64-unknown-linux-gnu", + "wasm32-unknown-unknown", + "aarch64-apple-darwin", +] + [dependencies] thiserror = { workspace = true } prost = { workspace = true, features = ["prost-derive"] } rand = { workspace = true } serde = { workspace = true } +diesel = { workspace = true } # XMTP/Local xmtp_proto = { workspace = true, features = ["convert"] } @@ -19,4 +26,3 @@ xmtp_common = { workspace = true, features = ['test-utils'] } [target.'cfg(not(target_arch = "wasm32"))'.dependencies] tonic = { version = "0.12", features = ["transport"] } -diesel = { workspace = true } diff --git a/xmtp_content_types/src/lib.rs b/xmtp_content_types/src/lib.rs index c3453908a..3bafb2a03 100644 --- a/xmtp_content_types/src/lib.rs +++ b/xmtp_content_types/src/lib.rs @@ -2,76 +2,9 @@ pub mod group_updated; pub mod membership_change; pub mod text; -use diesel::{ - backend::Backend, - deserialize::{self, FromSql, FromSqlRow}, - expression::AsExpression, - serialize::{self, IsNull, Output, ToSql}, - sql_types::Integer, - sqlite::Sqlite, -}; -use serde::{Deserialize, Serialize}; use thiserror::Error; use xmtp_proto::xmtp::mls::message_contents::{ContentTypeId, EncodedContent}; -/// ContentType and their corresponding string representation -/// are derived from the `ContentTypeId` enum in the xmtp-proto crate -/// that each content type in this crate establishes for itself -#[repr(i32)] -#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, FromSqlRow, AsExpression)] -#[diesel(sql_type = diesel::sql_types::Integer)] -pub enum ContentType { - Unknown = 0, - Text = 1, - GroupMembershipChange = 2, - 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 { - Self::Unknown => "unknown", - Self::Text => text::TextCodec::TYPE_ID, - Self::GroupMembershipChange => membership_change::GroupMembershipChangeCodec::TYPE_ID, - Self::GroupUpdated => group_updated::GroupUpdatedCodec::TYPE_ID, - } - } -} - -impl ToSql for ContentType -where - i32: ToSql, -{ - fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Sqlite>) -> serialize::Result { - out.set_value(*self as i32); - Ok(IsNull::No) - } -} - -impl FromSql for ContentType -where - i32: FromSql, -{ - fn from_sql(bytes: ::RawValue<'_>) -> deserialize::Result { - match i32::from_sql(bytes)? { - 0 => Ok(ContentType::Unknown), - 1 => Ok(ContentType::Text), - 2 => Ok(ContentType::GroupMembershipChange), - 3 => Ok(ContentType::GroupUpdated), - x => Err(format!("Unrecognized variant {}", x).into()), - } - } -} - #[derive(Debug, Error)] pub enum CodecError { #[error("encode error {0}")] diff --git a/xmtp_mls/src/groups/mls_sync.rs b/xmtp_mls/src/groups/mls_sync.rs index 044e164ac..b1a60ec87 100644 --- a/xmtp_mls/src/groups/mls_sync.rs +++ b/xmtp_mls/src/groups/mls_sync.rs @@ -13,9 +13,9 @@ use crate::{ GRPC_DATA_LIMIT, HMAC_SALT, MAX_GROUP_SIZE, MAX_INTENT_PUBLISH_ATTEMPTS, MAX_PAST_EPOCHS, SYNC_UPDATE_INSTALLATIONS_INTERVAL_NS, }, - groups::device_sync::DeviceSyncContent, groups::{ - device_sync::preference_sync::UserPreferenceUpdate, intents::UpdateMetadataIntentData, + device_sync::{preference_sync::UserPreferenceUpdate, DeviceSyncContent}, + intents::UpdateMetadataIntentData, validated_commit::ValidatedCommit, }, hpke::{encrypt_welcome, HpkeError}, @@ -25,15 +25,14 @@ use crate::{ storage::{ db_connection::DbConnection, group_intent::{IntentKind, IntentState, StoredGroupIntent, ID}, - group_message::{DeliveryStatus, GroupMessageKind, StoredGroupMessage}, + group_message::{ContentType, DeliveryStatus, GroupMessageKind, StoredGroupMessage}, refresh_state::EntityKind, serialization::{db_deserialize, db_serialize}, sql_key_store, user_preferences::StoredUserPreferences, StorageError, }, - subscriptions::LocalEvents, - subscriptions::SyncMessage, + subscriptions::{LocalEvents, SyncMessage}, utils::{hash::sha256, id::calculate_message_id, time::hmac_epoch}, xmtp_openmls_provider::XmtpOpenMlsProvider, Delete, Fetch, StoreOrIgnore, @@ -67,7 +66,7 @@ use std::{ use thiserror::Error; use tracing::debug; use xmtp_common::{retry_async, Retry, RetryableError}; -use xmtp_content_types::{group_updated::GroupUpdatedCodec, CodecError, ContentCodec, ContentType}; +use xmtp_content_types::{group_updated::GroupUpdatedCodec, CodecError, ContentCodec}; use xmtp_id::{InboxId, InboxIdRef}; use xmtp_proto::xmtp::mls::{ api::v1::{ diff --git a/xmtp_mls/src/groups/mod.rs b/xmtp_mls/src/groups/mod.rs index e23362c08..a72c0d02c 100644 --- a/xmtp_mls/src/groups/mod.rs +++ b/xmtp_mls/src/groups/mod.rs @@ -35,7 +35,6 @@ use openmls_traits::OpenMlsProvider; use prost::Message; use thiserror::Error; use tokio::sync::Mutex; -use xmtp_content_types::ContentType; use self::device_sync::DeviceSyncError; pub use self::group_permissions::PreconfiguredPolicies; @@ -59,7 +58,7 @@ use self::{ intents::IntentError, validated_commit::CommitValidationError, }; -use crate::storage::StorageError; +use crate::storage::{group_message::ContentType, StorageError}; use xmtp_common::time::now_ns; use xmtp_proto::xmtp::mls::{ api::v1::{ diff --git a/xmtp_mls/src/storage/encrypted_store/group_message.rs b/xmtp_mls/src/storage/encrypted_store/group_message.rs index 75ee41213..9d4178ea2 100644 --- a/xmtp_mls/src/storage/encrypted_store/group_message.rs +++ b/xmtp_mls/src/storage/encrypted_store/group_message.rs @@ -7,7 +7,7 @@ use diesel::{ sql_types::Integer, }; use serde::{Deserialize, Serialize}; -use xmtp_content_types::ContentType; +use xmtp_content_types::{group_updated, membership_change, text}; use super::{ db_connection::DbConnection, @@ -86,6 +86,61 @@ where } } +#[repr(i32)] +#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, FromSqlRow, AsExpression)] +#[diesel(sql_type = diesel::sql_types::Integer)] +pub enum ContentType { + Unknown = 0, + Text = 1, + GroupMembershipChange = 2, + 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 { + Self::Unknown => "unknown", + Self::Text => text::TextCodec::TYPE_ID, + Self::GroupMembershipChange => membership_change::GroupMembershipChangeCodec::TYPE_ID, + Self::GroupUpdated => group_updated::GroupUpdatedCodec::TYPE_ID, + } + } +} + +impl ToSql for ContentType +where + i32: ToSql, +{ + fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Sqlite>) -> serialize::Result { + out.set_value(*self as i32); + Ok(IsNull::No) + } +} + +impl FromSql for ContentType +where + i32: FromSql, +{ + fn from_sql(bytes: ::RawValue<'_>) -> deserialize::Result { + match i32::from_sql(bytes)? { + 0 => Ok(ContentType::Unknown), + 1 => Ok(ContentType::Text), + 2 => Ok(ContentType::GroupMembershipChange), + 3 => Ok(ContentType::GroupUpdated), + x => Err(format!("Unrecognized variant {}", x).into()), + } + } +} + #[repr(i32)] #[derive(Debug, Copy, Clone, Serialize, Deserialize, Eq, PartialEq, FromSqlRow, AsExpression)] #[diesel(sql_type = Integer)]