Skip to content

Commit 4a25860

Browse files
Hocurilink2xt
andauthored
feat: Deduplicate blob files in the JsonRPC API (#6470)
This makes it so that files will be deduplicated when using the JsonRPC API. @nicodh and @WofWca you know the Desktop code and how it is using the API, so, you can probably tell me whether this is a good way of changing the JsonRPC code - feel free to push changes directly to this PR here! This PR here changes the existing functions instead of creating new ones; we can alternatively create new ones if it allows for a smoother transition. This brings a few changes: - If you pass a file that is already in the blobdir, it will be renamed to `<hash>.<extension>` immediately (previously, the filename on the disk stayed the same) - If you pass a file that's not in the blobdir yet, it will be copied to the blobdir immediately (previously, it was copied to the blobdir later, when sending) - If you create a file and then pass it to `create_message()`, it's better to directly create it in the blobdir, since it doesn't need to be copied. - You must not write to the files after they were passed to core, because otherwise, the hash will be wrong. So, if Desktop recodes videos or so, then the video file mustn't just be overwritten. What you can do instead is write the recoded video to a file with a random name in the blobdir and then create a new message with the new attachment. If needed, we can also create a JsonRPC for `set_file_and_deduplicate()` that replaces the file on an existing message. In order to test whether everything still works, the desktop issue has a list of things to test: deltachat/deltachat-desktop#4498 Core issue: #6265 --------- Co-authored-by: l <[email protected]>
1 parent 67f768f commit 4a25860

File tree

3 files changed

+20
-5
lines changed

3 files changed

+20
-5
lines changed

deltachat-jsonrpc/src/api.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -1950,7 +1950,7 @@ impl CommandApi {
19501950
let ctx = self.get_context(account_id).await?;
19511951

19521952
let mut msg = Message::new(Viewtype::Sticker);
1953-
msg.set_file(&sticker_path, None);
1953+
msg.set_file_and_deduplicate(&ctx, Path::new(&sticker_path), None, None)?;
19541954

19551955
// JSON-rpc does not need heuristics to turn [Viewtype::Sticker] into [Viewtype::Image]
19561956
msg.force_sticker();
@@ -2170,12 +2170,14 @@ impl CommandApi {
21702170

21712171
// mimics the old desktop call, will get replaced with something better in the composer rewrite,
21722172
// the better version will just be sending the current draft, though there will be probably something similar with more options to this for the corner cases like setting a marker on the map
2173+
#[expect(clippy::too_many_arguments)]
21732174
async fn misc_send_msg(
21742175
&self,
21752176
account_id: u32,
21762177
chat_id: u32,
21772178
text: Option<String>,
21782179
file: Option<String>,
2180+
filename: Option<String>,
21792181
location: Option<(f64, f64)>,
21802182
quoted_message_id: Option<u32>,
21812183
) -> Result<(u32, MessageObject)> {
@@ -2187,7 +2189,7 @@ impl CommandApi {
21872189
});
21882190
message.set_text(text.unwrap_or_default());
21892191
if let Some(file) = file {
2190-
message.set_file(file, None);
2192+
message.set_file_and_deduplicate(&ctx, Path::new(&file), filename.as_deref(), None)?;
21912193
}
21922194
if let Some((latitude, longitude)) = location {
21932195
message.set_location(latitude, longitude);
@@ -2215,12 +2217,14 @@ impl CommandApi {
22152217
// the better version should support:
22162218
// - changing viewtype to enable/disable compression
22172219
// - keeping same message id as long as attachment does not change for webxdc messages
2220+
#[expect(clippy::too_many_arguments)]
22182221
async fn misc_set_draft(
22192222
&self,
22202223
account_id: u32,
22212224
chat_id: u32,
22222225
text: Option<String>,
22232226
file: Option<String>,
2227+
filename: Option<String>,
22242228
quoted_message_id: Option<u32>,
22252229
view_type: Option<MessageViewtype>,
22262230
) -> Result<()> {
@@ -2237,7 +2241,7 @@ impl CommandApi {
22372241
));
22382242
draft.set_text(text.unwrap_or_default());
22392243
if let Some(file) = file {
2240-
draft.set_file(file, None);
2244+
draft.set_file_and_deduplicate(&ctx, Path::new(&file), filename.as_deref(), None)?;
22412245
}
22422246
if let Some(id) = quoted_message_id {
22432247
draft

deltachat-jsonrpc/src/api/types/message.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::path::Path;
2+
13
use crate::api::VcardContact;
24
use anyhow::{Context as _, Result};
35
use deltachat::chat::Chat;
@@ -607,6 +609,7 @@ pub struct MessageData {
607609
pub html: Option<String>,
608610
pub viewtype: Option<MessageViewtype>,
609611
pub file: Option<String>,
612+
pub filename: Option<String>,
610613
pub location: Option<(f64, f64)>,
611614
pub override_sender_name: Option<String>,
612615
/// Quoted message id. Takes preference over `quoted_text` (see below).
@@ -631,7 +634,12 @@ impl MessageData {
631634
message.set_override_sender_name(self.override_sender_name);
632635
}
633636
if let Some(file) = self.file {
634-
message.set_file(file, None);
637+
message.set_file_and_deduplicate(
638+
context,
639+
Path::new(&file),
640+
self.filename.as_deref(),
641+
None,
642+
)?;
635643
}
636644
if let Some((latitude, longitude)) = self.location {
637645
message.set_location(latitude, longitude);

deltachat-rpc-client/src/deltachat_rpc_client/chat.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ def send_message(
124124
html: Optional[str] = None,
125125
viewtype: Optional[ViewType] = None,
126126
file: Optional[str] = None,
127+
filename: Optional[str] = None,
127128
location: Optional[tuple[float, float]] = None,
128129
override_sender_name: Optional[str] = None,
129130
quoted_msg: Optional[Union[int, Message]] = None,
@@ -137,6 +138,7 @@ def send_message(
137138
"html": html,
138139
"viewtype": viewtype,
139140
"file": file,
141+
"filename": filename,
140142
"location": location,
141143
"overrideSenderName": override_sender_name,
142144
"quotedMessageId": quoted_msg,
@@ -172,13 +174,14 @@ def set_draft(
172174
self,
173175
text: Optional[str] = None,
174176
file: Optional[str] = None,
177+
filename: Optional[str] = None,
175178
quoted_msg: Optional[int] = None,
176179
viewtype: Optional[str] = None,
177180
) -> None:
178181
"""Set draft message."""
179182
if isinstance(quoted_msg, Message):
180183
quoted_msg = quoted_msg.id
181-
self._rpc.misc_set_draft(self.account.id, self.id, text, file, quoted_msg, viewtype)
184+
self._rpc.misc_set_draft(self.account.id, self.id, text, file, filename, quoted_msg, viewtype)
182185

183186
def remove_draft(self) -> None:
184187
"""Remove draft message."""

0 commit comments

Comments
 (0)