Skip to content

Commit

Permalink
json deserialization for legacy reactions
Browse files Browse the repository at this point in the history
  • Loading branch information
cameronvoell committed Jan 11, 2025
1 parent 121a788 commit 76983ce
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 3 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

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

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

# XMTP/Local
xmtp_proto = { workspace = true, features = ["convert"] }
Expand Down
16 changes: 16 additions & 0 deletions xmtp_content_types/src/reaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::collections::HashMap;
use crate::{CodecError, ContentCodec};
use prost::Message;

use serde::{Deserialize, Serialize};
use xmtp_proto::xmtp::mls::message_contents::{
content_types::ReactionV2, ContentTypeId, EncodedContent,
};
Expand Down Expand Up @@ -47,6 +48,21 @@ impl ContentCodec<ReactionV2> for ReactionCodec {
}
}

#[derive(Debug, Serialize, Deserialize)]
pub struct LegacyReaction {
/// The message ID for the message that is being reacted to
pub reference: String,
/// The inbox ID of the user who sent the message that is being reacted to
#[serde(rename = "referenceInboxId", skip_serializing_if = "Option::is_none")]
pub reference_inbox_id: Option<String>,
/// The action of the reaction ("added" or "removed")
pub action: String,
/// The content of the reaction
pub content: String,
/// The schema of the content ("unicode", "shortcode", or "custom")
pub schema: String,
}

#[cfg(test)]
pub(crate) mod tests {
#[cfg(target_arch = "wasm32")]
Expand Down
20 changes: 17 additions & 3 deletions xmtp_mls/src/groups/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@ use openmls::{
};
use openmls_traits::OpenMlsProvider;
use prost::Message;
use serde::{Deserialize, Serialize};
use thiserror::Error;
use tokio::sync::Mutex;
use xmtp_content_types::reaction::ReactionCodec;
use xmtp_content_types::reaction::{LegacyReaction, ReactionCodec};

use self::device_sync::DeviceSyncError;
pub use self::group_permissions::PreconfiguredPolicies;
Expand Down Expand Up @@ -357,8 +358,21 @@ impl TryFrom<EncodedContent> for QueryableContentFields {
hex::decode(reaction.reference).ok()
}
(ReactionCodec::TYPE_ID, _) => {
// TODO: Implement JSON deserialization for legacy reaction format
None
// Try to decode the content as UTF-8 string first
if let Ok(decoded_content) = String::from_utf8(content.content) {
tracing::info!("attempting legacy json deserialization: {}", decoded_content);
// Try parsing as canonical JSON format first
if let Ok(reaction) = serde_json::from_str::<LegacyReaction>(&decoded_content) {
hex::decode(reaction.reference).ok()
} else {
tracing::error!("legacy json deserialization failed");
// If canonical format fails, try legacy format using parameters
None
}
} else {
tracing::error!("utf-8 deserialization failed");
None
}
}
_ => None,
};
Expand Down

0 comments on commit 76983ce

Please sign in to comment.