Skip to content

Commit

Permalink
api(json-rpc): force stickers to be sent as stickers (#4819)
Browse files Browse the repository at this point in the history
This approach uses a param field to enable forcing the sticker
`viewtype`. The first commit has the memory-only flag implemented, but
this flag is not persistent through the database conversion needed for
draft/undraft. That's why `param` has to be used.

follow up to #4814 
fixes #4739

---------

Co-authored-by: Septias <[email protected]>
  • Loading branch information
Septias and Septias authored Oct 14, 2023
1 parent 2775fd1 commit 0751cc5
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 1 deletion.
3 changes: 3 additions & 0 deletions deltachat-jsonrpc/src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1756,6 +1756,9 @@ impl CommandApi {
let mut msg = Message::new(Viewtype::Sticker);
msg.set_file(&sticker_path, None);

// JSON-rpc does not need heuristics to turn [Viewtype::Sticker] into [Viewtype::Image]
msg.force_sticker();

let message_id = deltachat::chat::send_msg(&ctx, ChatId::new(chat_id), &mut msg).await?;
Ok(message_id.to_u32())
}
Expand Down
50 changes: 49 additions & 1 deletion src/chat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2208,9 +2208,12 @@ async fn prepare_msg_blob(context: &Context, msg: &mut Message) -> Result<()> {
.with_context(|| format!("attachment missing for message of type #{}", msg.viewtype))?;

let mut maybe_sticker = msg.viewtype == Viewtype::Sticker;
if msg.viewtype == Viewtype::Image || maybe_sticker {
if msg.viewtype == Viewtype::Image
|| maybe_sticker && !msg.param.exists(Param::ForceSticker)
{
blob.recode_to_image_size(context, &mut maybe_sticker)
.await?;

if !maybe_sticker {
msg.viewtype = Viewtype::Image;
}
Expand Down Expand Up @@ -5679,6 +5682,51 @@ mod tests {
.await
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_sticker_jpeg_force() {
let alice = TestContext::new_alice().await;
let bob = TestContext::new_bob().await;
let alice_chat = alice.create_chat(&bob).await;

let file = alice.get_blobdir().join("sticker.jpg");
tokio::fs::write(
&file,
include_bytes!("../test-data/image/avatar1000x1000.jpg"),
)
.await
.unwrap();

// Images without force_sticker should be turned into [Viewtype::Image]
let mut msg = Message::new(Viewtype::Sticker);
msg.set_file(file.to_str().unwrap(), None);
let sent_msg = alice.send_msg(alice_chat.id, &mut msg).await;
let msg = bob.recv_msg(&sent_msg).await;
assert_eq!(msg.get_viewtype(), Viewtype::Image);

// Images with `force_sticker = true` should keep [Viewtype::Sticker]
let mut msg = Message::new(Viewtype::Sticker);
msg.set_file(file.to_str().unwrap(), None);
msg.force_sticker();
let sent_msg = alice.send_msg(alice_chat.id, &mut msg).await;
let msg = bob.recv_msg(&sent_msg).await;
assert_eq!(msg.get_viewtype(), Viewtype::Sticker);

// Images with `force_sticker = true` should keep [Viewtype::Sticker]
// even on drafted messages
let mut msg = Message::new(Viewtype::Sticker);
msg.set_file(file.to_str().unwrap(), None);
msg.force_sticker();
alice_chat
.id
.set_draft(&alice, Some(&mut msg))
.await
.unwrap();
let mut msg = alice_chat.id.get_draft(&alice).await.unwrap().unwrap();
let sent_msg = alice.send_msg(alice_chat.id, &mut msg).await;
let msg = bob.recv_msg(&sent_msg).await;
assert_eq!(msg.get_viewtype(), Viewtype::Sticker);
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_sticker_gif() -> Result<()> {
test_sticker(
Expand Down
6 changes: 6 additions & 0 deletions src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,12 @@ impl Message {
self.viewtype
}

/// Forces the message to **keep** [Viewtype::Sticker]
/// e.g the message will not be converted to a [Viewtype::Image].
pub fn force_sticker(&mut self) {
self.param.set_int(Param::ForceSticker, 1);
}

/// Returns the state of the message.
pub fn get_state(&self) -> MessageState {
self.state
Expand Down
3 changes: 3 additions & 0 deletions src/param.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,9 @@ pub enum Param {

/// For Webxdc Message Instances: timestamp of summary update.
WebxdcSummaryTimestamp = b'Q',

/// For messages: Whether [crate::message::Viewtype::Sticker] should be forced.
ForceSticker = b'X',
}

/// An object for handling key=value parameter lists.
Expand Down

0 comments on commit 0751cc5

Please sign in to comment.