Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions crates/chatd/src/adapters/discord/content.rs
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,7 @@ fn attachment(rich: &mut Rich, attached: &Attached) -> Attachment {
name: attached.filename.clone(),
file,
thumb,
preview: None,
mime: mime.to_owned(),
duration: attached.duration_secs.round() as u32,
round: false,
Expand Down Expand Up @@ -545,6 +546,7 @@ fn sticker(rich: &mut Rich, sticker: &model::Sticker) -> Attachment {
name: sticker.name.clone(),
file,
thumb: None,
preview: None,
mime: mime.to_owned(),
duration: 0,
round: false,
Expand Down
2 changes: 2 additions & 0 deletions crates/chatd/src/adapters/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,7 @@ fn message(account: &Account, note: &bugle::Note) -> Message {
name: label,
file,
thumb: None,
preview: None,
mime: piece.mime.clone(),
duration: 0,
spoiler: false,
Expand Down Expand Up @@ -796,6 +797,7 @@ fn serve(account: &Arc<Account>, command: Command) {
name: name.clone(),
file: media,
thumb: None,
preview: None,
mime: mime.to_owned(),
duration: 0,
spoiler: false,
Expand Down
9 changes: 6 additions & 3 deletions crates/chatd/src/adapters/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -380,9 +380,12 @@ pub enum Command {
at: Instant,
reply: Reply<Vec<MessageHit>>,
},
/// The shell asked for these files and the media cache knows none of them. An adapter that
/// fetches files over its own session (Telegram) fetches those that are its own, such as the
/// original a picture's viewer opened, and records each as the cache would. Others ignore it.
/// The shell asked for these files and the media cache knows none of them, or knows them
/// only by Telegram's name for a file it kept, since gone from disk. An adapter that fetches
/// files over its own session (Telegram, Messages) fetches those that are its own, such as
/// the original a picture's viewer opened, and records each as the cache would. Telegram
/// also takes it as what is on screen: a picture a message shows goes ahead of those that
/// are not, even one no page has named yet. Others ignore it.
Wanted(Vec<String>),
/// The link that opens this conversation's call in the official app.
Handoff { conversation_id: String, reply: Reply<String> },
Expand Down
1 change: 1 addition & 0 deletions crates/chatd/src/adapters/slack/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,7 @@ impl Slack {
name: first(&[&file["name"], &file["title"]]).unwrap_or("untitled").to_owned(),
file: whole,
thumb,
preview: None,
mime: mime.to_owned(),
duration: (file["duration_ms"].as_u64().unwrap_or(0) / 1000) as u32,
round: false,
Expand Down
3 changes: 2 additions & 1 deletion crates/chatd/src/adapters/telegram/catalog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ fn item(account: &Account, document: &tl::enums::Document, pack: &str) -> Option
_ => None,
});
let animated = matches!(document.mime_type.as_str(), "application/x-tgsticker" | "video/webm" | "video/mp4" | "image/gif");
account.files.want(wants);
// A picker's tile, not a message's picture: it keeps its turn when the picker asks for it.
account.files.want_tiles(wants);
let id = format!("d{}", document.id);
remember(
account,
Expand Down
162 changes: 162 additions & 0 deletions crates/chatd/src/adapters/telegram/content.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,76 @@ fn pick(sizes: &[PhotoSize], least: u32) -> Option<Size<'_>> {
/// What a row shows of a photo, and no more than a row needs: 320 on its longer side.
const INLINE: u32 = 320;

/// Most bytes of a stripped thumbnail's picture. Telegram's are a few hundred; one larger than
/// this is not one.
const STRIPPED_MOST: usize = 16 * 1024;

/// The JPEG header Telegram leaves out of a stripped thumbnail
/// (<https://core.telegram.org/api/files#stripped-thumbnails>). It is the same for every one but
/// the picture's height and width, at 164 and 166.
#[rustfmt::skip]
const STRIPPED_HEADER: [u8; 623] = [
0xff, 0xd8, 0xff, 0xe0, 0x00, 0x10, 0x4a, 0x46, 0x49, 0x46, 0x00, 0x01, 0x01, 0x00, 0x00, 0x01,
0x00, 0x01, 0x00, 0x00, 0xff, 0xdb, 0x00, 0x43, 0x00, 0x28, 0x1c, 0x1e, 0x23, 0x1e, 0x19, 0x28,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 0x03, 0x03, 0x02, 0x04, 0x03, 0x05,
0x05, 0x04, 0x04, 0x00, 0x00, 0x01, 0x7d, 0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12, 0x21,
0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07, 0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08, 0x23,
0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0, 0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16, 0x17,
0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a,
0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a,
0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a,
0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99,
0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7,
0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4, 0xd5,
0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xf1,
0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xff, 0xc4, 0x00, 0x1f, 0x01, 0x00, 0x03,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0xff, 0xc4, 0x00, 0xb5, 0x11, 0x00,
0x02, 0x01, 0x02, 0x04, 0x04, 0x03, 0x04, 0x07, 0x05, 0x04, 0x04, 0x00, 0x01, 0x02, 0x77, 0x00,
0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21, 0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71, 0x13,
0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91, 0xa1, 0xb1, 0xc1, 0x09, 0x23, 0x33, 0x52, 0xf0, 0x15,
0x62, 0x72, 0xd1, 0x0a, 0x16, 0x24, 0x34, 0xe1, 0x25, 0xf1, 0x17, 0x18, 0x19, 0x1a, 0x26, 0x27,
0x28, 0x29, 0x2a, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49,
0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88,
0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6,
0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4,
0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe2,
0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9,
0xfa, 0xff, 0xda, 0x00, 0x0c, 0x03, 0x01, 0x00, 0x02, 0x11, 0x03, 0x11, 0x00, 0x3f, 0x00,
];

/// A stripped thumbnail put back together as the JPEG it was, with its width and height.
/// `None` for bytes that are not one: another version, no size, no picture, too many bytes.
/// chatd only joins the pieces and never decodes them; a shell that cannot read the result
/// draws the usual placeholder.
pub(super) fn stripped_jpeg(stripped: &[u8]) -> Option<(u32, u32, Vec<u8>)> {
let [1, height, width, body @ ..] = stripped else {
return None;
};
if *height == 0 || *width == 0 || body.is_empty() || body.len() > STRIPPED_MOST {
return None;
}
let mut jpeg = Vec::with_capacity(STRIPPED_HEADER.len() + body.len() + 2);
jpeg.extend_from_slice(&STRIPPED_HEADER);
jpeg[164] = *height;
jpeg[166] = *width;
jpeg.extend_from_slice(body);
jpeg.extend_from_slice(&[0xff, 0xd9]);
Some((u32::from(*width), u32::from(*height), jpeg))
}

fn photo(photo: &tl::types::Photo, spoiler: bool, wants: &mut Vec<Want>) -> Option<Attachment> {
let location = |kind: &str| -> tl::enums::InputFileLocation {
tl::types::InputPhotoFileLocation {
Expand All @@ -184,6 +254,20 @@ fn photo(photo: &tl::types::Photo, spoiler: bool, wants: &mut Vec<Want>) -> Opti
.into()
};
let (kind, width, height, bytes, inline) = pick(&photo.sizes, INLINE)?;
// The few pixels Telegram sends inside the message: on disk with the page, and drawn until
// the row's own size is. A row's size that came inside the message needs none.
let stripped = photo.sizes.iter().find_map(|size| match size {
PhotoSize::PhotoStrippedSize(stripped) if inline.is_none() => Some(stripped),
_ => None,
});
let preview = stripped.and_then(|stripped| {
let (width, height, jpeg) = stripped_jpeg(&stripped.bytes)?;
let mut preview = MediaFile::new(format!("p{}{}", photo.id, stripped.r#type), MediaKind::Image);
(preview.width, preview.height, preview.bytes) = (width, height, jpeg.len() as u64);
let location = location(&stripped.r#type);
wants.push(Want { file: preview.clone(), dc: photo.dc_id, location, inline: Some(jpeg), row: None, watched: false, original: false });
Some(preview)
});
let mut thumb = MediaFile::new(format!("p{}{kind}", photo.id), MediaKind::Image);
(thumb.width, thumb.height, thumb.bytes) = (width, height, bytes);
wants.push(Want { file: thumb.clone(), dc: photo.dc_id, location: location(kind), inline: inline.map(<[u8]>::to_vec), row: None, watched: false, original: false });
Expand All @@ -197,6 +281,7 @@ fn photo(photo: &tl::types::Photo, spoiler: bool, wants: &mut Vec<Want>) -> Opti
}
let mut attachment = Attachment::new("Photo", file);
attachment.thumb = Some(thumb);
attachment.preview = preview;
attachment.mime = "image/jpeg".to_owned();
attachment.spoiler = spoiler;
Some(attachment)
Expand Down Expand Up @@ -683,6 +768,83 @@ mod tests {
assert!(waveform(&[]).is_empty());
}

fn a_photo(sizes: Vec<PhotoSize>) -> tl::types::Photo {
tl::types::Photo { has_stickers: false, id: 6_016, access_hash: 1, file_reference: vec![1], date: 0, sizes, video_sizes: None, dc_id: 4 }
}

fn sized(kind: &str, w: i32, h: i32) -> PhotoSize {
tl::types::PhotoSize { r#type: kind.to_owned(), w, h, size: 4_096 }.into()
}

fn stripped(bytes: &[u8]) -> PhotoSize {
tl::types::PhotoStrippedSize { r#type: "i".to_owned(), bytes: bytes.to_vec() }.into()
}

/// A real one: the fake wire's 40 by 30 photo, as Telegram would send it.
const RIDGE: &[u8] = include_bytes!("fake_ridge_i.bin");

#[test]
fn a_stripped_thumbnail_is_put_back_together_as_the_jpeg_it_was() {
let (width, height, jpeg) = stripped_jpeg(RIDGE).unwrap();
assert_eq!((width, height), (40, 30));
assert_eq!(jpeg.len(), STRIPPED_HEADER.len() + RIDGE.len() - 3 + 2);
assert_eq!(&jpeg[..4], &[0xff, 0xd8, 0xff, 0xe0], "it starts a JFIF file");
// The frame header names the picture's size where a decoder reads it.
assert_eq!(&jpeg[158..167], &[0xff, 0xc0, 0x00, 0x11, 0x08, 0x00, 30, 0x00, 40]);
assert_eq!(&jpeg[STRIPPED_HEADER.len()..jpeg.len() - 2], &RIDGE[3..], "the picture follows the header untouched");
assert_eq!(&jpeg[jpeg.len() - 2..], &[0xff, 0xd9]);
}

#[test]
fn bytes_that_are_not_a_stripped_thumbnail_make_no_preview() {
assert!(stripped_jpeg(&[]).is_none());
// The fake wire's other photos carry only a size and no picture.
assert!(stripped_jpeg(&[1, 20, 30]).is_none());
assert!(stripped_jpeg(&[2, 30, 40, 0x12]).is_none(), "another version");
assert!(stripped_jpeg(&[1, 0, 40, 0x12]).is_none(), "no height");
assert!(stripped_jpeg(&[1, 30, 0, 0x12]).is_none(), "no width");
let mut huge = vec![1, 30, 40];
huge.resize(3 + STRIPPED_MOST + 1, 0x12);
assert!(stripped_jpeg(&huge).is_none(), "too many bytes");
}

#[test]
fn a_photo_with_a_stripped_thumbnail_has_its_preview_on_disk_with_the_page() {
let mut wants = Vec::new();
let attachment = photo(&a_photo(vec![stripped(RIDGE), sized("m", 320, 240), sized("y", 1_280, 960)]), false, &mut wants).unwrap();
let preview = attachment.preview.as_ref().unwrap();
assert_eq!((preview.id.as_str(), preview.kind, preview.width, preview.height), ("p6016i", MediaKind::Image, 40, 30));
assert_eq!(attachment.thumb.as_ref().map(|thumb| thumb.id.as_str()), Some("p6016m"));
// Its bytes are in the want: nothing is asked of Telegram for it.
let kept = wants.iter().find(|want| want.file.id == "p6016i").unwrap();
assert_eq!(kept.inline.as_deref(), Some(stripped_jpeg(RIDGE).unwrap().2.as_slice()));
assert_eq!(kept.file.bytes, kept.inline.as_ref().unwrap().len() as u64);
assert!(!kept.original && !kept.watched && kept.row.is_none());
// The row's size is still fetched, and the original still waits for the viewer.
let thumb = wants.iter().find(|want| want.file.id == "p6016m").unwrap();
assert!(thumb.inline.is_none() && !thumb.original);
assert!(wants.iter().any(|want| want.file.id == "p6016y" && want.original));
}

#[test]
fn a_photo_without_a_usable_stripped_thumbnail_has_no_preview() {
for sizes in [
vec![sized("m", 320, 240)],
vec![stripped(&[1, 20, 30]), sized("m", 320, 240)],
vec![stripped(&[]), sized("m", 320, 240)],
] {
let mut wants = Vec::new();
let attachment = photo(&a_photo(sizes), false, &mut wants).unwrap();
assert!(attachment.preview.is_none());
assert!(wants.iter().all(|want| want.file.id != "p6016i"));
}
// A row's size that came inside the message is on disk as soon as the preview would be.
let cached = tl::types::PhotoCachedSize { r#type: "m".to_owned(), w: 320, h: 240, bytes: vec![0xff, 0xd8, 0xff, 0xd9] };
let mut wants = Vec::new();
let attachment = photo(&a_photo(vec![stripped(RIDGE), cached.into()]), false, &mut wants).unwrap();
assert!(attachment.preview.is_none());
}

#[test]
fn a_premium_emoji_keeps_its_alt_glyph_and_names_its_document_for_fetching() {
let text = "Cast off 🦀 at dawn";
Expand Down
Loading
Loading