Skip to content

Commit ed9c01f

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 7d7a245 commit ed9c01f

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

src/blob.rs

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

src/chat.rs

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

2695-
if msg.viewtype == Viewtype::File || msg.viewtype == Viewtype::Image {
2695+
if msg.viewtype == Viewtype::File
2696+
|| msg.viewtype == Viewtype::Image
2697+
|| msg.viewtype == Viewtype::Sticker && !msg.param.exists(Param::ForceSticker)
2698+
{
26962699
// Correct the type, take care not to correct already very special
26972700
// formats as GIF or VOICE.
26982701
//
@@ -2701,7 +2704,12 @@ async fn prepare_msg_blob(context: &Context, msg: &mut Message) -> Result<()> {
27012704
// - from FILE/IMAGE to GIF */
27022705
if let Some((better_type, _)) = message::guess_msgtype_from_suffix(&blob.to_abs_path())
27032706
{
2704-
if better_type != Viewtype::Webxdc
2707+
if msg.viewtype == Viewtype::Sticker {
2708+
if better_type != Viewtype::Image {
2709+
// UIs don't want conversions of `Sticker` to anything other than `Image`.
2710+
msg.param.set_int(Param::ForceSticker, 1);
2711+
}
2712+
} else if better_type != Viewtype::Webxdc
27052713
|| context
27062714
.ensure_sendable_webxdc_file(&blob.to_abs_path())
27072715
.await

0 commit comments

Comments
 (0)