vaegram 0.3.0

Telegram MTProto client library for users and bots
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
use crate::Client;
use crate::enums::MediaKind;
use crate::errors::{Error, Result};
use crate::raw::{base, functions, types};
use crate::session::transfer::Report;
use crate::types::messages_and_media::{Source, input_document_of, input_photo_of};
use crate::types::{FileSource, InputMedia, MaskPosition, Upload};

impl Client {
    pub(crate) async fn input_media(
        &self,
        peer: &base::InputPeer,
        media: &InputMedia<'_>,
        progress: Option<Report<'_>>,
        business_connection_id: Option<&str>,
    ) -> Result<base::InputMedia> {
        let photo = matches!(media.kind, MediaKind::Photo | MediaKind::LivePhoto);
        match &media.file.0 {
            Source::Stored(base::InputMedia::InputMediaPhoto(_)) if !photo => {
                return Err(Error::InvalidArgument("a stored photo can only be sent as a photo"));
            }
            Source::Stored(base::InputMedia::InputMediaDocument(_)) if photo => {
                return Err(Error::InvalidArgument("a stored document can't be sent as a photo"));
            }
            // The external media types carry no attributes, so only kinds that need none can come from a URL.
            Source::Url(_) if !matches!(media.kind, MediaKind::Photo | MediaKind::Document) => {
                return Err(Error::InvalidArgument("only photos and documents can be sent by URL; upload the file"));
            }
            Source::Url(_) if media.thumb.is_some() || media.force_file => {
                return Err(Error::InvalidArgument("a document sent by URL takes no thumbnail or force_file"));
            }
            _ => {}
        }
        let live_photo = media.kind == MediaKind::LivePhoto;
        let video = async {
            match &media.video {
                Some(video) => self.live_video(peer, video, media, business_connection_id).await.map(Some),
                None => Ok(None),
            }
        };
        let extras = async { tokio::try_join!(video, self.cover(peer, media, business_connection_id)) };
        let video_timestamp = media.start_timestamp;
        Ok(match &media.file.0 {
            Source::Stored(stored) => {
                let (video, video_cover) = extras.await?;
                match stored.clone() {
                    base::InputMedia::InputMediaPhoto(mut photo) => {
                        (photo.spoiler, photo.live_photo, photo.video) = (media.spoiler, live_photo, video);
                        base::InputMedia::InputMediaPhoto(photo)
                    }
                    base::InputMedia::InputMediaDocument(mut document) => {
                        document.spoiler = media.spoiler;
                        (document.video_cover, document.video_timestamp) = (video_cover, video_timestamp);
                        base::InputMedia::InputMediaDocument(document)
                    }
                    other => other,
                }
            }
            Source::Url(url) if photo => {
                extras.await?;
                let external =
                    types::InputMediaPhotoExternal { spoiler: media.spoiler, url: url.clone(), ttl_seconds: None };
                base::InputMedia::InputMediaPhotoExternal(Box::new(external))
            }
            Source::Url(url) => {
                let (_, video_cover) = extras.await?;
                base::InputMedia::InputMediaDocumentExternal(Box::new(types::InputMediaDocumentExternal {
                    spoiler: media.spoiler,
                    url: url.clone(),
                    ttl_seconds: None,
                    video_cover,
                    video_timestamp,
                }))
            }
            Source::Upload(upload) => {
                let file = self.save_file(*upload).progress(progress).into_future();
                let uploaded = self.uploaded_media(media, media.upload_name(upload.name()), file, extras).await?;
                self.stored_for_business(peer, uploaded, business_connection_id).await?
            }
            Source::Uploaded(file) => {
                let name = media.upload_name(file.name());
                let uploaded = self.uploaded_media(media, name, async { Ok(file.0.clone()) }, extras).await?;
                self.stored_for_business(peer, uploaded, business_connection_id).await?
            }
        })
    }

    /// Uploaded parts stay on this client's data center, which needn't be the one serving a business connection, so
    /// a business request gets the stored photo or document that messages.uploadMedia makes of them.
    async fn stored_for_business(
        &self,
        peer: &base::InputPeer,
        media: base::InputMedia,
        business_connection_id: Option<&str>,
    ) -> Result<base::InputMedia> {
        match business_connection_id {
            Some(_) => self.upload_media(peer, media, business_connection_id).await,
            None => Ok(media),
        }
    }

    async fn uploaded_media(
        &self,
        media: &InputMedia<'_>,
        name: &str,
        file: impl Future<Output = Result<base::InputFile>>,
        extras: impl Future<Output = Result<(Option<base::InputDocument>, Option<base::InputPhoto>)>>,
    ) -> Result<base::InputMedia> {
        let photo = matches!(media.kind, MediaKind::Photo | MediaKind::LivePhoto);
        let thumb = async {
            match media.thumb {
                Some(thumb) if !photo => self.save_file(thumb).await.map(Some),
                _ => Ok(None),
            }
        };
        let ((video, video_cover), file, thumb) = tokio::try_join!(extras, file, thumb)?;
        Ok(if photo {
            base::InputMedia::InputMediaUploadedPhoto(Box::new(types::InputMediaUploadedPhoto {
                spoiler: media.spoiler,
                live_photo: media.kind == MediaKind::LivePhoto,
                file,
                stickers: None,
                ttl_seconds: None,
                video,
            }))
        } else {
            let mut document = uploaded_document(media.kind, media, name, file);
            (document.thumb, document.video_cover, document.video_timestamp) =
                (thumb, video_cover, media.start_timestamp);
            base::InputMedia::InputMediaUploadedDocument(Box::new(document))
        })
    }

    /// Business uploads go through the connection: the bot itself isn't in the customer's chat.
    pub(crate) async fn upload_media(
        &self,
        peer: &base::InputPeer,
        media: base::InputMedia,
        business_connection_id: Option<&str>,
    ) -> Result<base::InputMedia> {
        // The answer doesn't echo the per-message flags, so they are put back on the stored reference.
        let (spoiler, live, cover, timestamp) = match &media {
            base::InputMedia::InputMediaUploadedPhoto(m) => {
                (m.spoiler, Some((m.live_photo, m.video.clone())), None, None)
            }
            base::InputMedia::InputMediaUploadedDocument(m) => {
                (m.spoiler, None, m.video_cover.clone(), m.video_timestamp)
            }
            base::InputMedia::InputMediaPhotoExternal(m) => (m.spoiler, None, None, None),
            base::InputMedia::InputMediaDocumentExternal(m) => {
                (m.spoiler, None, m.video_cover.clone(), m.video_timestamp)
            }
            _ => return Ok(media),
        };
        let business_connection_id = business_connection_id.map(Into::into);
        let request = functions::messages::UploadMedia { business_connection_id, peer: peer.clone(), media };
        let mut stored = stored_media(self.invoke(&request).await?)
            .ok_or(Error::Unexpected("messages.uploadMedia returned no photo or document"))?;
        match &mut stored {
            base::InputMedia::InputMediaPhoto(p) => {
                p.spoiler = spoiler;
                if let Some((live_photo, video)) = live {
                    (p.live_photo, p.video) = (live_photo, video.or(p.video.take()));
                }
            }
            base::InputMedia::InputMediaDocument(d) => {
                d.spoiler = spoiler;
                (d.video_cover, d.video_timestamp) = (cover.or(d.video_cover.take()), timestamp.or(d.video_timestamp));
            }
            _ => {}
        }
        Ok(stored)
    }

    async fn live_video(
        &self,
        peer: &base::InputPeer,
        video: &FileSource<'_>,
        media: &InputMedia<'_>,
        business_connection_id: Option<&str>,
    ) -> Result<base::InputDocument> {
        let (name, file) = match &video.0 {
            Source::Stored(base::InputMedia::InputMediaDocument(d)) => return Ok(d.id.clone()),
            Source::Stored(_) => return Err(Error::InvalidArgument("a live photo's video must be a video")),
            Source::Url(_) => return Err(Error::InvalidArgument("a live photo's video can't be sent by URL")),
            Source::Upload(upload) => (upload.name(), self.save_file(*upload).await?),
            Source::Uploaded(file) => (file.name(), file.0.clone()),
        };
        let mut uploaded = uploaded_document(MediaKind::Video, media, name, file);
        uploaded.spoiler = false;
        let uploaded = base::InputMedia::InputMediaUploadedDocument(Box::new(uploaded));
        match self.upload_media(peer, uploaded, business_connection_id).await? {
            base::InputMedia::InputMediaDocument(d) => Ok(d.id),
            _ => Err(Error::Unexpected("messages.uploadMedia returned no document for the video")),
        }
    }

    async fn cover(
        &self,
        peer: &base::InputPeer,
        media: &InputMedia<'_>,
        business_connection_id: Option<&str>,
    ) -> Result<Option<base::InputPhoto>> {
        let uploaded = |file| {
            base::InputMedia::InputMediaUploadedPhoto(Box::new(types::InputMediaUploadedPhoto {
                spoiler: false,
                live_photo: false,
                file,
                stickers: None,
                ttl_seconds: None,
                video: None,
            }))
        };
        let photo = match media.cover.as_ref().map(|c| &c.0) {
            None => return Ok(None),
            Some(Source::Stored(base::InputMedia::InputMediaPhoto(p))) => return Ok(Some(p.id.clone())),
            Some(Source::Stored(_)) => return Err(Error::InvalidArgument("a video cover must be a photo")),
            Some(Source::Url(url)) => {
                let url = url.clone();
                base::InputMedia::InputMediaPhotoExternal(Box::new(types::InputMediaPhotoExternal {
                    spoiler: false,
                    url,
                    ttl_seconds: None,
                }))
            }
            Some(Source::Upload(upload)) => uploaded(self.save_file(*upload).await?),
            Some(Source::Uploaded(file)) => uploaded(file.0.clone()),
        };
        match self.upload_media(peer, photo, business_connection_id).await? {
            base::InputMedia::InputMediaPhoto(p) => Ok(Some(p.id)),
            _ => Err(Error::Unexpected("messages.uploadMedia returned no photo for the cover")),
        }
    }
}

/// Telegram tells a round video, a voice message or a looping GIF from a plain file only by these attributes.
fn uploaded_document(
    kind: MediaKind,
    media: &InputMedia<'_>,
    name: &str,
    file: base::InputFile,
) -> types::InputMediaUploadedDocument {
    let (duration, w, h) = (media.duration, media.width, media.height);
    let fallback = match kind {
        MediaKind::Video | MediaKind::Animation | MediaKind::VideoNote | MediaKind::LivePhoto => "video/mp4",
        MediaKind::Audio => "audio/mpeg",
        MediaKind::Voice => "audio/ogg",
        MediaKind::Sticker => "image/webp",
        _ => "application/octet-stream",
    };
    let video = |round_message, supports_streaming| {
        base::DocumentAttribute::DocumentAttributeVideo(Box::new(types::DocumentAttributeVideo {
            round_message,
            supports_streaming,
            nosound: false,
            duration: f64::from(duration),
            w,
            h,
            preload_prefix_size: None,
            video_start_ts: None,
            video_codec: None,
        }))
    };
    let mut attributes = match kind {
        MediaKind::Video | MediaKind::LivePhoto => vec![video(false, media.supports_streaming)],
        MediaKind::Animation => vec![video(false, false), base::DocumentAttribute::DocumentAttributeAnimated],
        MediaKind::VideoNote => vec![video(true, false)],
        MediaKind::Audio | MediaKind::Voice => {
            vec![base::DocumentAttribute::DocumentAttributeAudio(Box::new(types::DocumentAttributeAudio {
                voice: kind == MediaKind::Voice,
                duration,
                title: media.title.clone(),
                performer: media.performer.clone(),
                waveform: None,
            }))]
        }
        MediaKind::Sticker => {
            vec![base::DocumentAttribute::DocumentAttributeSticker(Box::new(types::DocumentAttributeSticker {
                mask: media.mask_position.is_some(),
                alt: media.emoji.clone(),
                stickerset: base::InputStickerSet::InputStickerSetEmpty,
                mask_coords: media.mask_position.as_ref().map(MaskPosition::write),
            }))]
        }
        _ => Vec::new(),
    };
    if !matches!(kind, MediaKind::Voice | MediaKind::VideoNote) {
        let file_name = types::DocumentAttributeFilename { file_name: name.into() };
        attributes.push(base::DocumentAttribute::DocumentAttributeFilename(Box::new(file_name)));
    }
    types::InputMediaUploadedDocument {
        nosound_video: kind == MediaKind::Animation,
        force_file: media.force_file,
        spoiler: media.spoiler,
        file,
        thumb: None,
        mime_type: match Upload::mime_type_of(name) {
            "application/octet-stream" => fallback,
            mime => mime,
        }
        .into(),
        attributes,
        stickers: None,
        video_cover: None,
        video_timestamp: None,
        ttl_seconds: None,
    }
}

fn stored_media(stored: base::MessageMedia) -> Option<base::InputMedia> {
    match stored {
        base::MessageMedia::MessageMediaPhoto(m) => {
            let video = m.video.and_then(input_document_of);
            Some(base::InputMedia::InputMediaPhoto(Box::new(types::InputMediaPhoto {
                spoiler: m.spoiler,
                live_photo: m.live_photo,
                id: input_photo_of(m.photo?)?,
                ttl_seconds: None,
                video,
            })))
        }
        base::MessageMedia::MessageMediaDocument(m) => {
            Some(base::InputMedia::InputMediaDocument(Box::new(types::InputMediaDocument {
                spoiler: m.spoiler,
                id: input_document_of(m.document?)?,
                video_cover: m.video_cover.and_then(input_photo_of),
                video_timestamp: m.video_timestamp,
                ttl_seconds: None,
                query: None,
            })))
        }
        _ => None,
    }
}

#[cfg(test)]
mod tests {
    use super::{stored_media, uploaded_document};
    use crate::Client;
    use crate::enums::{MaskPointType, MediaKind};
    use crate::errors::{Error, RpcError};
    use crate::raw::Identifiable;
    use crate::raw::functions::messages::UploadMedia;
    use crate::raw::functions::upload::{SaveBigFilePart, SaveFilePart};
    use crate::raw::{Serializable, base, types};
    use crate::types::messages_and_media::Source;
    use crate::types::messages_and_media::media::tests::document_media;
    use crate::types::{InputMedia, MaskPosition, Upload, UploadedFile};

    #[tokio::test]
    async fn a_live_photo_uploads_its_photo_and_video_side_by_side() {
        let (client, mut sent) = Client::recording();
        client.0.me.lock().unwrap().premium = Some(false);
        let media = InputMedia::live_photo(Upload::bytes("a.jpg", b"jpg"), Upload::bytes("a.mp4", b"mp4"));
        // The recording session never answers, so only parts that went out before the first failure are seen.
        assert!(client.input_media(&base::InputPeer::InputPeerEmpty, &media, None, None).await.is_err());
        assert_eq!(std::iter::from_fn(|| sent.try_recv().ok()).count(), 2);
    }

    #[tokio::test]
    async fn uploads_keep_their_flags_and_the_business_connection() {
        let (client, mut sent) = Client::scripted(vec![Ok(document_media("video/mp4", Vec::new()).to_bytes())]);
        let cover = types::InputPhoto { id: 7, access_hash: 8, file_reference: vec![9] };
        let mut uploaded = document(InputMedia::video("a.mp4"));
        uploaded.spoiler = true;
        (uploaded.video_cover, uploaded.video_timestamp) =
            (Some(base::InputPhoto::InputPhoto(Box::new(cover.clone()))), Some(5));
        let media = base::InputMedia::InputMediaUploadedDocument(Box::new(uploaded));
        let peer = base::InputPeer::InputPeerChat(types::InputPeerChat { chat_id: 5 });

        let stored = client.upload_media(&peer, media, Some("conn")).await.unwrap();
        let base::InputMedia::InputMediaDocument(d) = stored else { panic!("not a stored document") };
        assert!(d.spoiler);
        assert_eq!((d.video_cover, d.video_timestamp), (Some(base::InputPhoto::InputPhoto(Box::new(cover))), Some(5)));
        let body = sent.try_recv().unwrap();
        assert!(body.windows(5).any(|w| w == b"\x04conn"), "uploadMedia went out without the connection");
    }

    #[tokio::test]
    async fn a_business_send_stores_its_upload_first() {
        let stored = document_media("application/pdf", Vec::new()).to_bytes();
        let (client, mut sent) = Client::scripted(vec![Ok(true.to_bytes()), Ok(stored)]);
        let user = types::InputPeerUser { user_id: 7, access_hash: 1 };
        client.0.peers.lock().unwrap().insert(7, base::InputPeer::InputPeerUser(Box::new(user)));
        client.0.caches.know_business("c", 7, 2);
        let _ = client.send_document(7, Upload::bytes("a.pdf", b"x")).business_connection_id("c").await;
        let bodies: Vec<_> = std::iter::from_fn(|| sent.try_recv().ok()).collect();
        let ids: Vec<u32> = bodies.iter().map(|b| u32::from_le_bytes(b[..4].try_into().unwrap())).collect();
        let wrapper = crate::raw::functions::InvokeWithBusinessConnection::<
            crate::raw::functions::messages::SendMedia,
        >::CONSTRUCTOR_ID;
        assert_eq!(ids, [SaveFilePart::CONSTRUCTOR_ID, UploadMedia::CONSTRUCTOR_ID, wrapper]);
        // The connection's data center gets the stored document, not parts it never saw.
        let has = |id: u32| bodies[2].windows(4).any(|w| w == id.to_le_bytes());
        assert!(has(types::InputMediaDocument::CONSTRUCTOR_ID));
        assert!(!has(types::InputMediaUploadedDocument::CONSTRUCTOR_ID));
    }

    fn stored(name: &str) -> base::InputFile {
        let file = types::InputFile { id: 7, parts: 1, name: name.into(), md5_checksum: String::new() };
        base::InputFile::InputFile(Box::new(file))
    }

    fn uploaded_photo(file: base::InputFile) -> base::InputMedia {
        base::InputMedia::InputMediaUploadedPhoto(Box::new(types::InputMediaUploadedPhoto {
            spoiler: false,
            live_photo: false,
            file,
            stickers: None,
            ttl_seconds: None,
            video: None,
        }))
    }

    fn no_part_sent(bodies: &[Vec<u8>]) -> bool {
        let save_parts = [SaveFilePart::CONSTRUCTOR_ID, SaveBigFilePart::CONSTRUCTOR_ID].map(u32::to_le_bytes);
        bodies.iter().all(|b| !save_parts.iter().any(|id| b[..4] == *id))
    }

    #[tokio::test]
    async fn an_uploaded_file_is_sent_without_another_upload() {
        let (client, mut sent) = Client::recording();
        let peer = base::InputPeer::InputPeerEmpty;
        let photo = UploadedFile(stored("a.jpg"));
        let media = client.input_media(&peer, &InputMedia::photo(&photo), None, None).await.unwrap();
        assert_eq!(media, uploaded_photo(stored("a.jpg")));

        let song = UploadedFile(stored("a.mp3"));
        let audio = InputMedia::audio(&song).duration(3);
        let media = client.input_media(&peer, &audio, None, None).await.unwrap();
        let expected = uploaded_document(audio.kind, &audio, "a.mp3", stored("a.mp3"));
        assert_eq!(media, base::InputMedia::InputMediaUploadedDocument(Box::new(expected.clone())));
        assert_eq!((expected.mime_type.as_str(), expected.attributes.last()), ("audio/mpeg", Some(&name("a.mp3"))));

        let round = UploadedFile(stored("note.mp4"));
        let note = InputMedia::video_note(&round);
        let base::InputMedia::InputMediaUploadedDocument(d) =
            client.input_media(&peer, &note, None, None).await.unwrap()
        else {
            panic!("not an uploaded document")
        };
        assert_eq!((d.file, d.attributes), (stored("note.mp4"), vec![video(true, false, 0.0, 0, 0)]));
        assert!(sent.try_recv().is_err());
    }

    #[tokio::test]
    async fn an_uploaded_cover_and_live_video_go_through_upload_media() {
        let failed = || Err(Error::Rpc(RpcError::parse(400, "FILE_PART_0_MISSING")));
        let (client, mut sent) = Client::scripted(vec![failed(), failed()]);
        let peer = base::InputPeer::InputPeerEmpty;
        let (cover, clip) = (UploadedFile(stored("cover.jpg")), UploadedFile(stored("clip.mp4")));

        let with_cover = InputMedia::video(&clip).cover(&cover);
        assert!(client.input_media(&peer, &with_cover, None, None).await.is_err());
        let body = sent.try_recv().expect("no uploadMedia sent");
        assert_eq!(body[..4], UploadMedia::CONSTRUCTOR_ID.to_le_bytes());
        assert!(contains(&body, &uploaded_photo(stored("cover.jpg")).to_bytes()));

        let live = InputMedia::live_photo(&cover, &clip);
        assert!(client.input_media(&peer, &live, None, None).await.is_err());
        let body = sent.try_recv().expect("no uploadMedia sent");
        assert_eq!(body[..4], UploadMedia::CONSTRUCTOR_ID.to_le_bytes());
        let mut video = uploaded_document(MediaKind::Video, &live, "clip.mp4", stored("clip.mp4"));
        video.spoiler = false;
        let video = base::InputMedia::InputMediaUploadedDocument(Box::new(video));
        assert!(contains(&body, &video.to_bytes()));
        let rest: Vec<_> = std::iter::from_fn(|| sent.try_recv().ok()).collect();
        assert!(no_part_sent(&[body]) && no_part_sent(&rest) && rest.is_empty());
    }

    fn contains(body: &[u8], part: &[u8]) -> bool {
        body.windows(part.len()).any(|w| w == part)
    }

    fn document(media: InputMedia<'_>) -> types::InputMediaUploadedDocument {
        let Source::Upload(upload) = media.file.0 else { panic!("not an upload") };
        let file = types::InputFile { id: 1, parts: 1, name: String::new(), md5_checksum: String::new() };
        uploaded_document(
            media.kind,
            &media,
            media.upload_name(upload.name()),
            base::InputFile::InputFile(Box::new(file)),
        )
    }

    fn video(round_message: bool, supports_streaming: bool, duration: f64, w: i32, h: i32) -> base::DocumentAttribute {
        base::DocumentAttribute::DocumentAttributeVideo(Box::new(types::DocumentAttributeVideo {
            round_message,
            supports_streaming,
            nosound: false,
            duration,
            w,
            h,
            preload_prefix_size: None,
            video_start_ts: None,
            video_codec: None,
        }))
    }

    fn name(file_name: &str) -> base::DocumentAttribute {
        let name = types::DocumentAttributeFilename { file_name: file_name.into() };
        base::DocumentAttribute::DocumentAttributeFilename(Box::new(name))
    }

    #[test]
    fn video_kinds_get_their_video_attributes() {
        let sent = document(InputMedia::video("clip.MP4").duration(7).width(640).height(360).supports_streaming());
        assert_eq!((sent.mime_type.as_str(), sent.nosound_video), ("video/mp4", false));
        assert_eq!(sent.attributes, [video(false, true, 7.0, 640, 360), name("clip.MP4")]);

        let note = document(InputMedia::video_note(Upload::bytes("note", b"x")).length(240).duration(3));
        assert_eq!((note.mime_type.as_str(), note.attributes), ("video/mp4", vec![video(true, false, 3.0, 240, 240)]));

        let gif = document(InputMedia::animation("cat.gif").has_spoiler());
        assert_eq!((gif.mime_type.as_str(), gif.nosound_video, gif.spoiler), ("image/gif", true, true));
        let animated = base::DocumentAttribute::DocumentAttributeAnimated;
        assert_eq!(gif.attributes, [video(false, false, 0.0, 0, 0), animated, name("cat.gif")]);
    }

    #[test]
    fn audio_kinds_get_their_audio_attributes() {
        let audio = |voice, title: Option<&str>, performer: Option<&str>| {
            base::DocumentAttribute::DocumentAttributeAudio(Box::new(types::DocumentAttributeAudio {
                voice,
                duration: 42,
                title: title.map(Into::into),
                performer: performer.map(Into::into),
                waveform: None,
            }))
        };
        let song = document(InputMedia::audio("song.mp3").duration(42).performer("Band").title("Song"));
        assert_eq!(song.mime_type, "audio/mpeg");
        assert_eq!(song.attributes, [audio(false, Some("Song"), Some("Band")), name("song.mp3")]);

        let voice = document(InputMedia::voice(Upload::bytes("voice", b"x")).duration(42));
        assert_eq!((voice.mime_type.as_str(), voice.attributes), ("audio/ogg", vec![audio(true, None, None)]));
    }

    #[test]
    fn stickers_and_documents_keep_their_names() {
        let sticker = document(InputMedia::sticker("hi.tgs").emoji("👋"));
        let attribute = types::DocumentAttributeSticker {
            mask: false,
            alt: "👋".into(),
            stickerset: base::InputStickerSet::InputStickerSetEmpty,
            mask_coords: None,
        };
        let attribute = base::DocumentAttribute::DocumentAttributeSticker(Box::new(attribute));
        assert_eq!(sticker.mime_type, "application/x-tgsticker");
        assert_eq!(sticker.attributes, [attribute, name("hi.tgs")]);
        assert_eq!(document(InputMedia::sticker(Upload::bytes("s", b"x"))).mime_type, "image/webp");

        let file = document(InputMedia::document("report.PDF"));
        assert_eq!((file.mime_type.as_str(), file.attributes), ("application/pdf", vec![name("report.PDF")]));
        let unknown = ["data.xyz", "README"].map(|n| document(InputMedia::document(n)).mime_type);
        assert_eq!(unknown, ["application/octet-stream"; 2]);
    }

    #[test]
    fn a_mask_sticker_carries_its_coordinates() {
        let chin = MaskPosition::new(MaskPointType::Chin, -1.0, 0.5, 2.0);
        let sticker = document(InputMedia::sticker("mask.webp").emoji("🎭").mask_position(chin));
        let attribute = types::DocumentAttributeSticker {
            mask: true,
            alt: "🎭".into(),
            stickerset: base::InputStickerSet::InputStickerSetEmpty,
            mask_coords: Some(chin.write()),
        };
        let attribute = base::DocumentAttribute::DocumentAttributeSticker(Box::new(attribute));
        assert_eq!(sticker.attributes, [attribute, name("mask.webp")]);
    }

    #[test]
    fn file_name_renames_an_upload_and_picks_its_mime_type() {
        let zip = document(InputMedia::document("/tmp/x/7f3a.part").file_name("Album (2024).zip"));
        assert_eq!((zip.mime_type.as_str(), zip.attributes), ("application/zip", vec![name("Album (2024).zip")]));
        let song = document(InputMedia::audio(Upload::bytes("raw", b"x")).file_name("Song.flac"));
        assert_eq!(song.mime_type, "audio/flac");
        assert!(song.attributes.contains(&name("Song.flac")));
    }

    #[test]
    fn stored_media_becomes_a_reference() {
        let photo = types::Photo {
            has_stickers: false,
            id: 1,
            access_hash: 2,
            file_reference: vec![3],
            date: 0,
            sizes: Vec::new(),
            video_sizes: None,
            dc_id: 2,
        };
        let stored = types::MessageMediaPhoto {
            spoiler: true,
            live_photo: false,
            photo: Some(base::Photo::Photo(Box::new(photo))),
            ttl_seconds: None,
            video: None,
        };
        let Some(base::InputMedia::InputMediaPhoto(p)) =
            stored_media(base::MessageMedia::MessageMediaPhoto(Box::new(stored)))
        else {
            panic!("not a stored photo")
        };
        let id = types::InputPhoto { id: 1, access_hash: 2, file_reference: vec![3] };
        let id = base::InputPhoto::InputPhoto(Box::new(id));
        assert_eq!((p.spoiler, p.live_photo, &p.id, &p.video), (true, false, &id, &None));

        let document = types::Document {
            id: 4,
            access_hash: 5,
            file_reference: vec![6],
            date: 0,
            mime_type: "video/mp4".into(),
            size: 1,
            thumbs: None,
            video_thumbs: None,
            dc_id: 2,
            attributes: Vec::new(),
        };
        let stored = types::MessageMediaDocument {
            nopremium: false,
            spoiler: false,
            video: true,
            round: false,
            voice: false,
            document: Some(base::Document::Document(Box::new(document))),
            alt_documents: None,
            video_cover: None,
            video_timestamp: Some(9),
            ttl_seconds: None,
        };
        let Some(base::InputMedia::InputMediaDocument(d)) =
            stored_media(base::MessageMedia::MessageMediaDocument(Box::new(stored)))
        else {
            panic!("not a stored document")
        };
        let id = types::InputDocument { id: 4, access_hash: 5, file_reference: vec![6] };
        assert_eq!(d.id, base::InputDocument::InputDocument(Box::new(id)));
        assert_eq!((d.spoiler, d.video_timestamp, &d.video_cover), (false, Some(9), &None));
        assert!(stored_media(base::MessageMedia::MessageMediaEmpty).is_none());
    }
}