Skip to main content

im_core/attachments/
dto.rs

1use std::path::PathBuf;
2
3use serde::{Deserialize, Serialize};
4use serde_json::Value;
5
6#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
7pub struct AttachmentSendRequest {
8    pub input: AttachmentInput,
9    pub caption: Option<String>,
10    #[serde(default, skip_serializing_if = "Option::is_none")]
11    pub mention_payload: Option<Value>,
12    pub mime_type: Option<String>,
13    pub filename: Option<String>,
14    pub delivery: crate::messages::MessageDeliveryOptions,
15    pub security: crate::messages::MessageSecurityMode,
16}
17
18#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
19pub struct SendConversationAttachmentRequest {
20    pub conversation: crate::messages::ConversationReadRef,
21    pub input: AttachmentInput,
22    pub caption: Option<String>,
23    #[serde(default, skip_serializing_if = "Option::is_none")]
24    pub mention_payload: Option<Value>,
25    pub mime_type: Option<String>,
26    pub filename: Option<String>,
27    pub security: crate::messages::MessageSecurityMode,
28    pub client_message_id: Option<crate::ids::MessageId>,
29    pub idempotency_key: Option<String>,
30    pub wait_for_final_acceptance: bool,
31}
32
33impl SendConversationAttachmentRequest {
34    pub(crate) fn into_attachment_send_request(
35        self,
36    ) -> (
37        crate::messages::ConversationReadRef,
38        Option<crate::ids::MessageId>,
39        AttachmentSendRequest,
40    ) {
41        (
42            self.conversation,
43            self.client_message_id,
44            AttachmentSendRequest {
45                input: self.input,
46                caption: self.caption,
47                mention_payload: self.mention_payload,
48                mime_type: self.mime_type,
49                filename: self.filename,
50                delivery: crate::messages::MessageDeliveryOptions {
51                    idempotency_key: self.idempotency_key,
52                    wait_for_final_acceptance: self.wait_for_final_acceptance,
53                },
54                security: self.security,
55            },
56        )
57    }
58}
59
60#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
61pub struct AttachmentSendResult {
62    pub message: crate::messages::SendMessageResult,
63    pub target_kind: String,
64    pub target_did: String,
65    pub attachment: UploadedAttachment,
66    pub manifest: Value,
67}
68
69#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
70pub struct UploadedAttachment {
71    pub attachment_id: String,
72    pub filename: String,
73    pub mime_type: String,
74    pub size_bytes: u64,
75    pub size: String,
76    pub digest_b64u: String,
77    pub object_uri: String,
78    pub object_encryption_mode: String,
79    pub plaintext_size_bytes: Option<u64>,
80}
81
82#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
83pub enum AttachmentInput {
84    LocalFile(PathBuf),
85    Bytes {
86        filename: Option<String>,
87        mime_type: Option<String>,
88        bytes: Vec<u8>,
89    },
90}
91
92#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
93pub struct DownloadAttachmentRequest {
94    pub thread: crate::messages::ThreadRef,
95    pub message_id: crate::ids::MessageId,
96    pub attachment_id: Option<String>,
97    pub destination: AttachmentDestination,
98    pub overwrite: bool,
99}
100
101#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
102pub enum AttachmentDestination {
103    LocalFile(PathBuf),
104    Memory,
105}
106
107#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
108pub struct DownloadedAttachment {
109    pub attachment_id: String,
110    pub filename: Option<String>,
111    pub mime_type: Option<String>,
112    pub size_bytes: Option<u64>,
113    pub destination: DownloadedAttachmentDestination,
114    pub selection: Option<crate::attachments::AttachmentSelection>,
115    pub warnings: Vec<String>,
116}
117
118#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
119pub enum DownloadedAttachmentDestination {
120    LocalFile(PathBuf),
121    Memory(Vec<u8>),
122}
123
124impl AttachmentSendResult {
125    pub(crate) fn from_upload_result(
126        result: crate::internal::attachment_runtime::upload::AttachmentUploadResult,
127    ) -> Self {
128        let object_encryption_mode = result.prepared.object_encryption_mode();
129        let plaintext_size_bytes = result.prepared.plaintext_size_bytes;
130        Self {
131            message: result.sdk_result,
132            target_kind: result.target_kind.to_string(),
133            target_did: result.target_did,
134            attachment: UploadedAttachment {
135                attachment_id: result.slot.attachment_id,
136                filename: result.prepared.filename,
137                mime_type: result.prepared.mime_type,
138                size_bytes: result.prepared.size_bytes,
139                size: result.prepared.size_string,
140                digest_b64u: result.prepared.digest_b64u,
141                object_uri: result.slot.object_uri,
142                object_encryption_mode,
143                plaintext_size_bytes,
144            },
145            manifest: result.manifest,
146        }
147    }
148}