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
use gettextrs::gettext;
use gtk::{gio, glib, prelude::*};
use matrix_sdk::Client;
use ruma::{
    events::{
        room::message::{
            AudioMessageEventContent, FileMessageEventContent, FormattedBody,
            ImageMessageEventContent, MessageType, VideoMessageEventContent,
        },
        sticker::StickerEventContent,
    },
    UInt,
};
use tracing::{debug, error};

use crate::{
    prelude::*,
    toast,
    utils::{
        media::image::{ImageSource, ThumbnailDownloader, ThumbnailSettings},
        save_data_to_tmp_file,
    },
};

/// Get the filename of a media message.
macro_rules! filename {
    ($message:ident, $mime_fallback:expr) => {{
        let mut filename = match &$message.filename {
            Some(filename) if *filename != $message.body => filename.clone(),
            _ => $message.body.clone(),
        };

        if filename.is_empty() {
            let mimetype = $message
                .info
                .as_ref()
                .and_then(|info| info.mimetype.as_deref());

            filename = $crate::utils::media::filename_for_mime(mimetype, $mime_fallback);
        }

        filename
    }};
}

/// Get the caption of a media message.
macro_rules! caption {
    ($message:ident) => {{
        $message
            .filename
            .as_deref()
            .filter(|filename| *filename != $message.body)
            .map(|_| ($message.body.clone(), $message.formatted.clone()))
    }};
}

/// A media message.
#[derive(Debug, Clone)]
pub enum MediaMessage {
    /// An audio.
    Audio(AudioMessageEventContent),
    /// A file.
    File(FileMessageEventContent),
    /// An image.
    Image(ImageMessageEventContent),
    /// A video.
    Video(VideoMessageEventContent),
    /// A sticker.
    Sticker(StickerEventContent),
}

impl MediaMessage {
    /// Construct a `MediaMessage` from the given message.
    pub fn from_message(msgtype: &MessageType) -> Option<Self> {
        match msgtype {
            MessageType::Audio(c) => Some(Self::Audio(c.clone())),
            MessageType::File(c) => Some(Self::File(c.clone())),
            MessageType::Image(c) => Some(Self::Image(c.clone())),
            MessageType::Video(c) => Some(Self::Video(c.clone())),
            _ => None,
        }
    }

    /// The filename of the media.
    ///
    /// For a sticker, this returns the description of the sticker.
    pub fn filename(&self) -> String {
        match self {
            Self::Audio(c) => filename!(c, Some(mime::AUDIO)),
            Self::File(c) => filename!(c, None),
            Self::Image(c) => filename!(c, Some(mime::IMAGE)),
            Self::Video(c) => filename!(c, Some(mime::VIDEO)),
            Self::Sticker(c) => c.body.clone(),
        }
    }

    /// The caption of the media, if any.
    ///
    /// Returns `Some((body, formatted_body))` if the media includes a caption.
    pub fn caption(&self) -> Option<(String, Option<FormattedBody>)> {
        match self {
            Self::Audio(c) => caption!(c),
            Self::File(c) => caption!(c),
            Self::Image(c) => caption!(c),
            Self::Video(c) => caption!(c),
            Self::Sticker(_) => None,
        }
    }

    /// Fetch the content of the media with the given client.
    ///
    /// Returns an error if something occurred while fetching the content.
    pub async fn into_content(self, client: &Client) -> Result<Vec<u8>, matrix_sdk::Error> {
        let media = client.media();

        macro_rules! content {
            ($event_content:ident) => {{
                Ok(
                    $crate::spawn_tokio!(
                        async move { media.get_file(&$event_content, true).await }
                    )
                    .await
                    .unwrap()?
                    .expect("All media message types have a file"),
                )
            }};
        }

        match self {
            Self::Audio(c) => content!(c),
            Self::File(c) => content!(c),
            Self::Image(c) => content!(c),
            Self::Video(c) => content!(c),
            Self::Sticker(c) => content!(c),
        }
    }

    /// Fetch the content of the media with the given client and write it to a
    /// temporary file.
    ///
    /// Returns an error if something occurred while fetching the content.
    pub async fn into_tmp_file(self, client: &Client) -> Result<gio::File, MediaFileError> {
        let data = self.into_content(client).await?;
        Ok(save_data_to_tmp_file(&data)?)
    }

    /// Save the content of the media to a file selected by the user.
    ///
    /// Shows a dialog to the user to select a file on the system.
    pub async fn save_to_file(self, client: &Client, parent: &impl IsA<gtk::Widget>) {
        let filename = self.filename();

        let data = match self.into_content(client).await {
            Ok(data) => data,
            Err(error) => {
                error!("Could not retrieve media file: {error}");
                toast!(parent, error.to_user_facing());

                return;
            }
        };

        let dialog = gtk::FileDialog::builder()
            .title(gettext("Save File"))
            .modal(true)
            .accept_label(gettext("Save"))
            .initial_name(filename)
            .build();

        match dialog
            .save_future(parent.root().and_downcast_ref::<gtk::Window>())
            .await
        {
            Ok(file) => {
                if let Err(error) = file.replace_contents(
                    &data,
                    None,
                    false,
                    gio::FileCreateFlags::REPLACE_DESTINATION,
                    gio::Cancellable::NONE,
                ) {
                    error!("Could not save file: {error}");
                    toast!(parent, gettext("Could not save file"));
                }
            }
            Err(error) => {
                if error.matches(gtk::DialogError::Dismissed) {
                    debug!("File dialog dismissed by user");
                } else {
                    error!("Could not access file: {error}");
                    toast!(parent, gettext("Could not access file"));
                }
            }
        };
    }
}

impl From<AudioMessageEventContent> for MediaMessage {
    fn from(value: AudioMessageEventContent) -> Self {
        Self::Audio(value)
    }
}

impl From<FileMessageEventContent> for MediaMessage {
    fn from(value: FileMessageEventContent) -> Self {
        Self::File(value)
    }
}

impl From<StickerEventContent> for MediaMessage {
    fn from(value: StickerEventContent) -> Self {
        Self::Sticker(value)
    }
}

/// A visual media message.
#[derive(Debug, Clone)]
pub enum VisualMediaMessage {
    /// An image.
    Image(ImageMessageEventContent),
    /// A video.
    Video(VideoMessageEventContent),
    /// A sticker.
    Sticker(StickerEventContent),
}

impl VisualMediaMessage {
    /// Construct a `VisualMediaMessage` from the given message.
    pub fn from_message(msgtype: &MessageType) -> Option<Self> {
        match msgtype {
            MessageType::Image(c) => Some(Self::Image(c.clone())),
            MessageType::Video(c) => Some(Self::Video(c.clone())),
            _ => None,
        }
    }

    /// The filename of the media.
    ///
    /// For a sticker, this returns the description of the sticker.
    pub fn filename(&self) -> String {
        match self {
            Self::Image(c) => filename!(c, Some(mime::IMAGE)),
            Self::Video(c) => filename!(c, Some(mime::VIDEO)),
            Self::Sticker(c) => c.body.clone(),
        }
    }

    /// The caption of the media, if any.
    ///
    /// Returns `Some((body, formatted_body))` if the media includes a caption.
    pub fn caption(&self) -> Option<(String, Option<FormattedBody>)> {
        match self {
            Self::Image(c) => caption!(c),
            Self::Video(c) => caption!(c),
            Self::Sticker(_) => None,
        }
    }

    /// The dimensions of the media, if any.
    ///
    /// Returns a `(width, height)` tuple.
    pub fn dimensions(&self) -> Option<(UInt, UInt)> {
        match self {
            Self::Image(c) => c.info.as_ref().and_then(|i| i.width.zip(i.height)),
            Self::Video(c) => c.info.as_ref().and_then(|i| i.width.zip(i.height)),
            Self::Sticker(c) => c.info.width.zip(c.info.height),
        }
    }

    /// Fetch a thumbnail of the media with the given client and thumbnail
    /// settings and write it to a temporary file.
    ///
    /// This might not return a thumbnail at the requested size, depending on
    /// the message and the homeserver.
    ///
    /// Returns `Ok(None)` if no thumbnail could be retrieved and no fallback
    /// could be downloaded. This only applies to video messages.
    ///
    /// Returns an error if something occurred while fetching the content or
    /// saving the content to a file.
    pub async fn thumbnail_tmp_file(
        &self,
        client: &Client,
        settings: ThumbnailSettings,
    ) -> Result<Option<gio::File>, MediaFileError> {
        let downloader = match &self {
            Self::Image(c) => {
                let image_info = c.info.as_deref();
                ThumbnailDownloader {
                    main: ImageSource {
                        source: (&c.source).into(),
                        info: image_info.map(Into::into),
                    },
                    alt: image_info.and_then(|i| {
                        i.thumbnail_source.as_ref().map(|s| ImageSource {
                            source: s.into(),
                            info: i.thumbnail_info.as_deref().map(Into::into),
                        })
                    }),
                }
            }
            Self::Video(c) => {
                let Some(video_info) = c.info.as_deref() else {
                    return Ok(None);
                };
                let Some(thumbnail_source) = video_info.thumbnail_source.as_ref() else {
                    return Ok(None);
                };

                ThumbnailDownloader {
                    main: ImageSource {
                        source: thumbnail_source.into(),
                        info: video_info.thumbnail_info.as_deref().map(Into::into),
                    },
                    alt: None,
                }
            }
            Self::Sticker(c) => {
                let image_info = &c.info;
                ThumbnailDownloader {
                    main: ImageSource {
                        source: (&c.source).into(),
                        info: Some(image_info.into()),
                    },
                    alt: image_info.thumbnail_source.as_ref().map(|s| ImageSource {
                        source: s.into(),
                        info: image_info.thumbnail_info.as_deref().map(Into::into),
                    }),
                }
            }
        };

        let file = downloader.download_to_file(client, settings).await?;

        Ok(Some(file))
    }

    /// Fetch the content of the media with the given client.
    ///
    /// Returns an error if something occurred while fetching the content.
    pub async fn into_content(self, client: &Client) -> Result<Vec<u8>, matrix_sdk::Error> {
        MediaMessage::from(self).into_content(client).await
    }

    /// Fetch the content of the media with the given client and write it to a
    /// temporary file.
    ///
    /// Returns an error if something occurred while fetching the content or
    /// saving the content to a file.
    pub async fn into_tmp_file(self, client: &Client) -> Result<gio::File, MediaFileError> {
        MediaMessage::from(self).into_tmp_file(client).await
    }

    /// Save the content of the media to a file selected by the user.
    ///
    /// Shows a dialog to the user to select a file on the system.
    pub async fn save_to_file(self, client: &Client, parent: &impl IsA<gtk::Widget>) {
        MediaMessage::from(self).save_to_file(client, parent).await
    }
}

impl From<ImageMessageEventContent> for VisualMediaMessage {
    fn from(value: ImageMessageEventContent) -> Self {
        Self::Image(value)
    }
}

impl From<VideoMessageEventContent> for VisualMediaMessage {
    fn from(value: VideoMessageEventContent) -> Self {
        Self::Video(value)
    }
}

impl From<StickerEventContent> for VisualMediaMessage {
    fn from(value: StickerEventContent) -> Self {
        Self::Sticker(value)
    }
}

impl From<VisualMediaMessage> for MediaMessage {
    fn from(value: VisualMediaMessage) -> Self {
        match value {
            VisualMediaMessage::Image(c) => Self::Image(c),
            VisualMediaMessage::Video(c) => Self::Video(c),
            VisualMediaMessage::Sticker(c) => Self::Sticker(c),
        }
    }
}

/// All errors that can occur when downloading a media to a file.
#[derive(Debug, thiserror::Error)]
#[error(transparent)]
pub enum MediaFileError {
    /// An error occurred when downloading the media.
    Sdk(#[from] matrix_sdk::Error),
    /// An error occurred when writing the media to a file.
    File(#[from] glib::Error),
}