|
| 1 | +use napi::bindgen_prelude::{Result, Uint8Array}; |
| 2 | +use napi_derive::napi; |
| 3 | +use prost::Message; |
| 4 | +use xmtp_content_types::multi_remote_attachment::MultiRemoteAttachmentCodec; |
| 5 | +use xmtp_content_types::ContentCodec; |
| 6 | +use xmtp_proto::xmtp::mls::message_contents::content_types::{ |
| 7 | + MultiRemoteAttachment as XmtpMultiRemoteAttachment, |
| 8 | + RemoteAttachmentInfo as XmtpRemoteAttachmentInfo, |
| 9 | +}; |
| 10 | +use xmtp_proto::xmtp::mls::message_contents::EncodedContent; |
| 11 | + |
| 12 | +use crate::ErrorWrapper; |
| 13 | + |
| 14 | +#[napi(object)] |
| 15 | +pub struct RemoteAttachmentInfo { |
| 16 | + pub secret: Uint8Array, |
| 17 | + pub content_digest: String, |
| 18 | + pub nonce: Uint8Array, |
| 19 | + pub scheme: String, |
| 20 | + pub url: String, |
| 21 | + pub salt: Uint8Array, |
| 22 | + pub content_length: Option<u32>, |
| 23 | + pub filename: Option<String>, |
| 24 | +} |
| 25 | + |
| 26 | +impl From<RemoteAttachmentInfo> for XmtpRemoteAttachmentInfo { |
| 27 | + fn from(remote_attachment_info: RemoteAttachmentInfo) -> Self { |
| 28 | + XmtpRemoteAttachmentInfo { |
| 29 | + content_digest: remote_attachment_info.content_digest, |
| 30 | + secret: remote_attachment_info.secret.to_vec(), |
| 31 | + nonce: remote_attachment_info.nonce.to_vec(), |
| 32 | + salt: remote_attachment_info.salt.to_vec(), |
| 33 | + scheme: remote_attachment_info.scheme, |
| 34 | + url: remote_attachment_info.url, |
| 35 | + content_length: remote_attachment_info.content_length, |
| 36 | + filename: remote_attachment_info.filename, |
| 37 | + } |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +impl From<XmtpRemoteAttachmentInfo> for RemoteAttachmentInfo { |
| 42 | + fn from(remote_attachment_info: XmtpRemoteAttachmentInfo) -> Self { |
| 43 | + RemoteAttachmentInfo { |
| 44 | + secret: Uint8Array::from(remote_attachment_info.secret), |
| 45 | + content_digest: remote_attachment_info.content_digest, |
| 46 | + nonce: Uint8Array::from(remote_attachment_info.nonce), |
| 47 | + scheme: remote_attachment_info.scheme, |
| 48 | + url: remote_attachment_info.url, |
| 49 | + salt: Uint8Array::from(remote_attachment_info.salt), |
| 50 | + content_length: remote_attachment_info.content_length, |
| 51 | + filename: remote_attachment_info.filename, |
| 52 | + } |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +#[napi(object)] |
| 57 | +pub struct MultiRemoteAttachment { |
| 58 | + pub attachments: Vec<RemoteAttachmentInfo>, |
| 59 | +} |
| 60 | + |
| 61 | +impl From<MultiRemoteAttachment> for XmtpMultiRemoteAttachment { |
| 62 | + fn from(multi_remote_attachment: MultiRemoteAttachment) -> Self { |
| 63 | + XmtpMultiRemoteAttachment { |
| 64 | + attachments: multi_remote_attachment |
| 65 | + .attachments |
| 66 | + .into_iter() |
| 67 | + .map(Into::into) |
| 68 | + .collect(), |
| 69 | + } |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +impl From<XmtpMultiRemoteAttachment> for MultiRemoteAttachment { |
| 74 | + fn from(multi_remote_attachment: XmtpMultiRemoteAttachment) -> Self { |
| 75 | + MultiRemoteAttachment { |
| 76 | + attachments: multi_remote_attachment |
| 77 | + .attachments |
| 78 | + .into_iter() |
| 79 | + .map(Into::into) |
| 80 | + .collect(), |
| 81 | + } |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +#[napi] |
| 86 | +pub fn encode_multi_remote_attachment( |
| 87 | + multi_remote_attachment: MultiRemoteAttachment, |
| 88 | +) -> Result<Uint8Array> { |
| 89 | + // Convert MultiRemoteAttachment to MultiRemoteAttachment |
| 90 | + let multi_remote_attachment: XmtpMultiRemoteAttachment = multi_remote_attachment.into(); |
| 91 | + |
| 92 | + // Use MultiRemoteAttachmentCodec to encode the attachments |
| 93 | + let encoded = |
| 94 | + MultiRemoteAttachmentCodec::encode(multi_remote_attachment).map_err(ErrorWrapper::from)?; |
| 95 | + |
| 96 | + // Encode the EncodedContent to bytes |
| 97 | + let mut buf = Vec::new(); |
| 98 | + encoded.encode(&mut buf).map_err(ErrorWrapper::from)?; |
| 99 | + |
| 100 | + Ok(Uint8Array::from(buf)) |
| 101 | +} |
| 102 | + |
| 103 | +#[napi] |
| 104 | +pub fn decode_multi_remote_attachment(bytes: Uint8Array) -> Result<MultiRemoteAttachment> { |
| 105 | + // Decode bytes into EncodedContent |
| 106 | + let encoded_content = |
| 107 | + EncodedContent::decode(bytes.to_vec().as_slice()).map_err(ErrorWrapper::from)?; |
| 108 | + |
| 109 | + // Use MultiRemoteAttachmentCodec to decode into MultiRemoteAttachment and convert to MultiRemoteAttachment |
| 110 | + MultiRemoteAttachmentCodec::decode(encoded_content) |
| 111 | + .map(Into::into) |
| 112 | + .map_err(|e| napi::Error::from_reason(e.to_string())) |
| 113 | +} |
0 commit comments