Skip to content

Commit

Permalink
move content type enum to encrypted_store
Browse files Browse the repository at this point in the history
  • Loading branch information
cameronvoell committed Dec 19, 2024
1 parent 0c3232f commit 2bd831a
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 80 deletions.
2 changes: 0 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions xmtp_content_types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ license.workspace = true
thiserror = { workspace = true }
prost = { workspace = true, features = ["prost-derive"] }
rand = { workspace = true }
serde = { workspace = true }

# XMTP/Local
xmtp_proto = { workspace = true, features = ["convert"] }
Expand All @@ -19,4 +18,3 @@ xmtp_common = { workspace = true, features = ['test-utils'] }

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
tonic = { version = "0.12", features = ["transport"] }
diesel = { workspace = true }
67 changes: 0 additions & 67 deletions xmtp_content_types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Integer, Sqlite> for ContentType
where
i32: ToSql<Integer, Sqlite>,
{
fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Sqlite>) -> serialize::Result {
out.set_value(*self as i32);
Ok(IsNull::No)
}
}

impl FromSql<Integer, Sqlite> for ContentType
where
i32: FromSql<Integer, Sqlite>,
{
fn from_sql(bytes: <Sqlite as Backend>::RawValue<'_>) -> deserialize::Result<Self> {
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}")]
Expand Down
11 changes: 5 additions & 6 deletions xmtp_mls/src/groups/mls_sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand All @@ -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,
Expand Down Expand Up @@ -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::{
Expand Down
3 changes: 1 addition & 2 deletions xmtp_mls/src/groups/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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::{
Expand Down
57 changes: 56 additions & 1 deletion xmtp_mls/src/storage/encrypted_store/group_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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<Integer, Sqlite> for ContentType
where
i32: ToSql<Integer, Sqlite>,
{
fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Sqlite>) -> serialize::Result {
out.set_value(*self as i32);
Ok(IsNull::No)
}
}

impl FromSql<Integer, Sqlite> for ContentType
where
i32: FromSql<Integer, Sqlite>,
{
fn from_sql(bytes: <Sqlite as Backend>::RawValue<'_>) -> deserialize::Result<Self> {
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)]
Expand Down

0 comments on commit 2bd831a

Please sign in to comment.