Skip to content

Commit d59debd

Browse files
committed
fix: Never change Viewtype::Sticker to Image if file has non-image extension (#6352)
Even if UIs don't call `Message::force_sticker()`, they don't want conversions of `Sticker` to `Image` if it's obviously not an image, particularly, has non-image extension. Also UIs don't want conversions of `Sticker` to anything other than `Image`, so let's keep the `Sticker` viewtype in this case.
1 parent eaeef30 commit d59debd

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

src/blob.rs

+19
Original file line numberDiff line numberDiff line change
@@ -1455,4 +1455,23 @@ mod tests {
14551455
check_image_size(file_saved, width, height);
14561456
Ok(())
14571457
}
1458+
1459+
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
1460+
async fn test_send_gif_as_sticker() -> Result<()> {
1461+
let bytes = include_bytes!("../test-data/image/image100x50.gif");
1462+
let alice = &TestContext::new_alice().await;
1463+
let file = alice.get_blobdir().join("file").with_extension("gif");
1464+
fs::write(&file, &bytes)
1465+
.await
1466+
.context("failed to write file")?;
1467+
let mut msg = Message::new(Viewtype::Sticker);
1468+
msg.set_file(file.to_str().unwrap(), None);
1469+
let chat = alice.get_self_chat().await;
1470+
let sent = alice.send_msg(chat.id, &mut msg).await;
1471+
let msg = Message::load_from_db(alice, sent.sender_msg_id).await?;
1472+
// Message::force_sticker() wasn't used, still Viewtype::Sticker is preserved because of the
1473+
// extension.
1474+
assert_eq!(msg.get_viewtype(), Viewtype::Sticker);
1475+
Ok(())
1476+
}
14581477
}

src/chat.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -2688,7 +2688,10 @@ async fn prepare_msg_blob(context: &Context, msg: &mut Message) -> Result<()> {
26882688
.with_context(|| format!("attachment missing for message of type #{}", msg.viewtype))?;
26892689
let send_as_is = msg.viewtype == Viewtype::File;
26902690

2691-
if msg.viewtype == Viewtype::File || msg.viewtype == Viewtype::Image {
2691+
if msg.viewtype == Viewtype::File
2692+
|| msg.viewtype == Viewtype::Image
2693+
|| msg.viewtype == Viewtype::Sticker && !msg.param.exists(Param::ForceSticker)
2694+
{
26922695
// Correct the type, take care not to correct already very special
26932696
// formats as GIF or VOICE.
26942697
//
@@ -2697,7 +2700,12 @@ async fn prepare_msg_blob(context: &Context, msg: &mut Message) -> Result<()> {
26972700
// - from FILE/IMAGE to GIF */
26982701
if let Some((better_type, _)) = message::guess_msgtype_from_suffix(&blob.to_abs_path())
26992702
{
2700-
if better_type != Viewtype::Webxdc
2703+
if msg.viewtype == Viewtype::Sticker {
2704+
if better_type != Viewtype::Image {
2705+
// UIs don't want conversions of `Sticker` to anything other than `Image`.
2706+
msg.param.set_int(Param::ForceSticker, 1);
2707+
}
2708+
} else if better_type != Viewtype::Webxdc
27012709
|| context
27022710
.ensure_sendable_webxdc_file(&blob.to_abs_path())
27032711
.await

0 commit comments

Comments
 (0)