ruma_events/room/message/
image.rs

1use ruma_common::OwnedMxcUri;
2use serde::{Deserialize, Serialize};
3
4use super::FormattedBody;
5use crate::room::{
6    message::media_caption::{caption, formatted_caption},
7    EncryptedFile, ImageInfo, MediaSource,
8};
9
10/// The payload for an image message.
11#[derive(Clone, Debug, Deserialize, Serialize)]
12#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
13#[serde(tag = "msgtype", rename = "m.image")]
14pub struct ImageMessageEventContent {
15    /// A textual representation of the image.
16    ///
17    /// If the `filename` field is not set or has the same value, this is the filename of the
18    /// uploaded file. Otherwise, this should be interpreted as a user-written media caption.
19    pub body: String,
20
21    /// Formatted form of the message `body`.
22    ///
23    /// This should only be set if the body represents a caption.
24    #[serde(flatten)]
25    pub formatted: Option<FormattedBody>,
26
27    /// The original filename of the uploaded file as deserialized from the event.
28    ///
29    /// It is recommended to use the `filename` method to get the filename which automatically
30    /// falls back to the `body` field when the `filename` field is not set.
31    #[serde(skip_serializing_if = "Option::is_none")]
32    pub filename: Option<String>,
33
34    /// The source of the image.
35    #[serde(flatten)]
36    pub source: MediaSource,
37
38    /// Metadata about the image referred to in `source`.
39    #[serde(skip_serializing_if = "Option::is_none")]
40    pub info: Option<Box<ImageInfo>>,
41}
42
43impl ImageMessageEventContent {
44    /// Creates a new `ImageMessageEventContent` with the given body and source.
45    pub fn new(body: String, source: MediaSource) -> Self {
46        Self { body, formatted: None, filename: None, source, info: None }
47    }
48
49    /// Creates a new non-encrypted `ImageMessageEventContent` with the given body and url.
50    pub fn plain(body: String, url: OwnedMxcUri) -> Self {
51        Self::new(body, MediaSource::Plain(url))
52    }
53
54    /// Creates a new encrypted `ImageMessageEventContent` with the given body and encrypted
55    /// file.
56    pub fn encrypted(body: String, file: EncryptedFile) -> Self {
57        Self::new(body, MediaSource::Encrypted(Box::new(file)))
58    }
59
60    /// Creates a new `ImageMessageEventContent` from `self` with the `info` field set to the given
61    /// value.
62    ///
63    /// Since the field is public, you can also assign to it directly. This method merely acts
64    /// as a shorthand for that, because it is very common to set this field.
65    pub fn info(self, info: impl Into<Option<Box<ImageInfo>>>) -> Self {
66        Self { info: info.into(), ..self }
67    }
68
69    /// Computes the filename of the image as defined by the [spec](https://spec.matrix.org/latest/client-server-api/#media-captions).
70    ///
71    /// This differs from the `filename` field as this method falls back to the `body` field when
72    /// the `filename` field is not set.
73    pub fn filename(&self) -> &str {
74        self.filename.as_deref().unwrap_or(&self.body)
75    }
76
77    /// Returns the caption for the image as defined by the [spec](https://spec.matrix.org/latest/client-server-api/#media-captions).
78    ///
79    /// In short, this is the `body` field if the `filename` field exists and has a different value,
80    /// otherwise the media file does not have a caption.
81    pub fn caption(&self) -> Option<&str> {
82        caption(&self.body, self.filename.as_deref())
83    }
84
85    /// Returns the formatted caption for the image as defined by the [spec](https://spec.matrix.org/latest/client-server-api/#media-captions).
86    ///
87    /// This is the same as `caption`, but returns the formatted body instead of the plain body.
88    pub fn formatted_caption(&self) -> Option<&FormattedBody> {
89        formatted_caption(&self.body, self.formatted.as_ref(), self.filename.as_deref())
90    }
91}