Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add reaction content type from proto #1453

Merged
merged 1 commit into from
Jan 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions xmtp_content_types/src/reaction.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,83 @@
use std::collections::HashMap;

use crate::{CodecError, ContentCodec};
use prost::Message;

use xmtp_proto::xmtp::mls::message_contents::{
content_types::ReactionV2, ContentTypeId, EncodedContent,
};

pub struct ReactionCodec {}

/// Legacy content type id at https://github.com/xmtp/xmtp-js/blob/main/content-types/content-type-reaction/src/Reaction.ts
impl ReactionCodec {
const AUTHORITY_ID: &'static str = "xmtp.org";
pub const TYPE_ID: &'static str = "reaction";
}

impl ContentCodec<ReactionV2> for ReactionCodec {
fn content_type() -> ContentTypeId {
ContentTypeId {
authority_id: ReactionCodec::AUTHORITY_ID.to_string(),
type_id: ReactionCodec::TYPE_ID.to_string(),
version_major: 1,
version_minor: 0,
}
}

fn encode(data: ReactionV2) -> Result<EncodedContent, CodecError> {
let mut buf = Vec::new();
data.encode(&mut buf)
.map_err(|e| CodecError::Encode(e.to_string()))?;

Ok(EncodedContent {
r#type: Some(ReactionCodec::content_type()),
parameters: HashMap::new(),
fallback: None,
compression: None,
content: buf,
})
}

fn decode(content: EncodedContent) -> Result<ReactionV2, CodecError> {
let decoded = ReactionV2::decode(content.content.as_slice())
.map_err(|e| CodecError::Decode(e.to_string()))?;

Ok(decoded)
}
}

#[cfg(test)]
pub(crate) mod tests {
#[cfg(target_arch = "wasm32")]
wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_dedicated_worker);

use xmtp_proto::xmtp::mls::message_contents::content_types::{
ReactionAction, ReactionSchema, ReactionV2,
};

use xmtp_common::rand_string;

use super::*;

#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
#[cfg_attr(not(target_arch = "wasm32"), test)]
fn test_encode_decode() {
let new_reaction_data = ReactionV2 {
reference: rand_string::<24>(),
reference_inbox_id: rand_string::<24>(),
action: ReactionAction::Added as i32,
content: "👍".to_string(),
schema: ReactionSchema::Unicode as i32,
};

let encoded = ReactionCodec::encode(new_reaction_data).unwrap();
assert_eq!(encoded.clone().r#type.unwrap().type_id, "reaction");
assert!(!encoded.content.is_empty());

let decoded = ReactionCodec::decode(encoded).unwrap();
assert_eq!(decoded.action, ReactionAction::Added as i32);
assert_eq!(decoded.content, "👍".to_string());
assert_eq!(decoded.schema, ReactionSchema::Unicode as i32);
}
}
3 changes: 2 additions & 1 deletion xmtp_proto/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ test-utils = ["xmtp_common/test-utils"]
# @@protoc_deletion_point(features)
# This section is automatically generated by protoc-gen-prost-crate.
# Changes in this area may be lost on regeneration.
proto_full = ["xmtp-identity","xmtp-identity-api-v1","xmtp-identity-associations","xmtp-keystore_api-v1","xmtp-message_api-v1","xmtp-message_contents","xmtp-mls-api-v1","xmtp-mls-database","xmtp-mls-message_contents","xmtp-mls_validation-v1","xmtp-xmtpv4-envelopes","xmtp-xmtpv4-message_api","xmtp-xmtpv4-payer_api"]
proto_full = ["xmtp-identity","xmtp-identity-api-v1","xmtp-identity-associations","xmtp-keystore_api-v1","xmtp-message_api-v1","xmtp-message_contents","xmtp-mls-api-v1","xmtp-mls-database","xmtp-mls-message_contents","xmtp-mls-message_contents-content_types","xmtp-mls_validation-v1","xmtp-xmtpv4-envelopes","xmtp-xmtpv4-message_api","xmtp-xmtpv4-payer_api"]
"xmtp-identity" = []
"xmtp-identity-api-v1" = ["xmtp-identity","xmtp-identity-associations"]
"xmtp-identity-associations" = ["xmtp-identity","xmtp-message_contents"]
Expand All @@ -44,6 +44,7 @@ proto_full = ["xmtp-identity","xmtp-identity-api-v1","xmtp-identity-associations
"xmtp-mls-api-v1" = ["xmtp-message_contents"]
"xmtp-mls-database" = []
"xmtp-mls-message_contents" = []
"xmtp-mls-message_contents-content_types" = ["xmtp-mls-message_contents"]
"xmtp-mls_validation-v1" = ["xmtp-identity-api-v1","xmtp-identity-associations"]
"xmtp-xmtpv4-envelopes" = ["xmtp-identity-associations","xmtp-mls-api-v1"]
"xmtp-xmtpv4-message_api" = ["xmtp-xmtpv4-envelopes"]
Expand Down
6 changes: 6 additions & 0 deletions xmtp_proto/src/gen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ pub mod xmtp {
pub mod message_contents {
include!("xmtp.mls.message_contents.rs");
// @@protoc_insertion_point(xmtp.mls.message_contents)
#[cfg(feature = "xmtp-mls-message_contents-content_types")]
// @@protoc_insertion_point(attribute:xmtp.mls.message_contents.content_types)
pub mod content_types {
include!("xmtp.mls.message_contents.content_types.rs");
// @@protoc_insertion_point(xmtp.mls.message_contents.content_types)
}
}
}
pub mod mls_validation {
Expand Down
Loading
Loading