Skip to content

Commit

Permalink
feat: Deduplicate blob files in the JsonRPC API
Browse files Browse the repository at this point in the history
  • Loading branch information
Hocuri committed Jan 24, 2025
1 parent 8435f40 commit 7663144
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
10 changes: 7 additions & 3 deletions deltachat-jsonrpc/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1937,7 +1937,7 @@ impl CommandApi {
let ctx = self.get_context(account_id).await?;

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

// JSON-rpc does not need heuristics to turn [Viewtype::Sticker] into [Viewtype::Image]
msg.force_sticker();
Expand Down Expand Up @@ -2157,12 +2157,14 @@ impl CommandApi {

// mimics the old desktop call, will get replaced with something better in the composer rewrite,
// 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
#[allow(clippy::too_many_arguments)]
async fn misc_send_msg(
&self,
account_id: u32,
chat_id: u32,
text: Option<String>,
file: Option<String>,
filename: Option<String>,
location: Option<(f64, f64)>,
quoted_message_id: Option<u32>,
) -> Result<(u32, MessageObject)> {
Expand All @@ -2174,7 +2176,7 @@ impl CommandApi {
});
message.set_text(text.unwrap_or_default());
if let Some(file) = file {
message.set_file(file, None);
message.set_file_and_deduplicate(&ctx, Path::new(&file), filename.as_deref(), None)?;
}
if let Some((latitude, longitude)) = location {
message.set_location(latitude, longitude);
Expand Down Expand Up @@ -2202,12 +2204,14 @@ impl CommandApi {
// the better version should support:
// - changing viewtype to enable/disable compression
// - keeping same message id as long as attachment does not change for webxdc messages
#[allow(clippy::too_many_arguments)]
async fn misc_set_draft(
&self,
account_id: u32,
chat_id: u32,
text: Option<String>,
file: Option<String>,
filename: Option<String>,
quoted_message_id: Option<u32>,
view_type: Option<MessageViewtype>,
) -> Result<()> {
Expand All @@ -2224,7 +2228,7 @@ impl CommandApi {
));
draft.set_text(text.unwrap_or_default());
if let Some(file) = file {
draft.set_file(file, None);
draft.set_file_and_deduplicate(&ctx, Path::new(&file), filename.as_deref(), None)?;
}
if let Some(id) = quoted_message_id {
draft
Expand Down
10 changes: 9 additions & 1 deletion deltachat-jsonrpc/src/api/types/message.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::path::Path;

use crate::api::VcardContact;
use anyhow::{Context as _, Result};
use deltachat::chat::Chat;
Expand Down Expand Up @@ -589,6 +591,7 @@ pub struct MessageData {
pub html: Option<String>,
pub viewtype: Option<MessageViewtype>,
pub file: Option<String>,
pub filename: Option<String>,
pub location: Option<(f64, f64)>,
pub override_sender_name: Option<String>,
/// Quoted message id. Takes preference over `quoted_text` (see below).
Expand All @@ -613,7 +616,12 @@ impl MessageData {
message.set_override_sender_name(self.override_sender_name);
}
if let Some(file) = self.file {
message.set_file(file, None);
message.set_file_and_deduplicate(
context,
Path::new(&file),
self.filename.as_deref(),
None,
)?;
}
if let Some((latitude, longitude)) = self.location {
message.set_location(latitude, longitude);
Expand Down

0 comments on commit 7663144

Please sign in to comment.