use std::collections::BTreeMap;
use js_int::UInt;
use ruma_macros::EventContent;
use serde::{Deserialize, Serialize};
use super::{
message::MessageContent,
room::{
message::{
FileInfo, FileMessageEventContent, MessageType, Relation, RoomMessageEventContent,
},
EncryptedFile, JsonWebKey, MediaSource,
},
};
use crate::{serde::Base64, OwnedMxcUri};
#[derive(Clone, Debug, Serialize, Deserialize, EventContent)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
#[ruma_event(type = "m.file", kind = MessageLike)]
pub struct FileEventContent {
#[serde(flatten)]
pub message: MessageContent,
#[serde(rename = "m.file")]
pub file: FileContent,
#[serde(flatten, skip_serializing_if = "Option::is_none")]
pub relates_to: Option<Relation>,
}
impl FileEventContent {
pub fn plain(
message: impl Into<String>,
url: OwnedMxcUri,
info: Option<Box<FileContentInfo>>,
) -> Self {
Self {
message: MessageContent::plain(message),
file: FileContent::plain(url, info),
relates_to: None,
}
}
pub fn plain_message(
message: MessageContent,
url: OwnedMxcUri,
info: Option<Box<FileContentInfo>>,
) -> Self {
Self { message, file: FileContent::plain(url, info), relates_to: None }
}
pub fn encrypted(
message: impl Into<String>,
url: OwnedMxcUri,
encryption_info: EncryptedContent,
info: Option<Box<FileContentInfo>>,
) -> Self {
Self {
message: MessageContent::plain(message),
file: FileContent::encrypted(url, encryption_info, info),
relates_to: None,
}
}
pub fn encrypted_message(
message: MessageContent,
url: OwnedMxcUri,
encryption_info: EncryptedContent,
info: Option<Box<FileContentInfo>>,
) -> Self {
Self { message, file: FileContent::encrypted(url, encryption_info, info), relates_to: None }
}
pub fn from_file_room_message(
content: FileMessageEventContent,
relates_to: Option<Relation>,
) -> Self {
let FileMessageEventContent { body, filename, source, info, message, file } = content;
let FileInfo { mimetype, size, .. } = info.map(|info| *info).unwrap_or_default();
let message = message.unwrap_or_else(|| MessageContent::plain(body));
let file = file.unwrap_or_else(|| {
FileContent::from_room_message_content(source, filename, mimetype, size)
});
Self { message, file, relates_to }
}
}
impl From<FileEventContent> for RoomMessageEventContent {
fn from(content: FileEventContent) -> Self {
let FileEventContent { message, file, relates_to } = content;
Self {
msgtype: MessageType::File(FileMessageEventContent::from_extensible_content(
message, file,
)),
relates_to,
}
}
}
#[derive(Clone, Debug, Serialize, Deserialize)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
pub struct FileContent {
pub url: OwnedMxcUri,
#[serde(flatten, skip_serializing_if = "Option::is_none")]
pub info: Option<Box<FileContentInfo>>,
#[serde(flatten, skip_serializing_if = "Option::is_none")]
pub encryption_info: Option<Box<EncryptedContent>>,
}
impl FileContent {
pub fn plain(url: OwnedMxcUri, info: Option<Box<FileContentInfo>>) -> Self {
Self { url, info, encryption_info: None }
}
pub fn encrypted(
url: OwnedMxcUri,
encryption_info: EncryptedContent,
info: Option<Box<FileContentInfo>>,
) -> Self {
Self { url, info, encryption_info: Some(Box::new(encryption_info)) }
}
pub(crate) fn from_room_message_content(
source: MediaSource,
filename: Option<String>,
mimetype: Option<String>,
size: Option<UInt>,
) -> Self {
let (url, encryption_info) = source.into_extensible_content();
let info =
FileContentInfo::from_room_message_content(filename, mimetype, size).map(Box::new);
Self { url, encryption_info: encryption_info.map(Box::new), info }
}
pub fn is_encrypted(&self) -> bool {
self.encryption_info.is_some()
}
}
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
pub struct FileContentInfo {
#[serde(skip_serializing_if = "Option::is_none")]
pub name: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub mimetype: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub size: Option<UInt>,
}
impl FileContentInfo {
pub fn new() -> Self {
Self::default()
}
pub(crate) fn from_room_message_content(
filename: Option<String>,
mimetype: Option<String>,
size: Option<UInt>,
) -> Option<Self> {
if filename.is_none() && mimetype.is_none() && size.is_none() {
None
} else {
Some(Self { name: filename, mimetype, size })
}
}
}
#[derive(Clone, Debug, Deserialize, Serialize)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
pub struct EncryptedContent {
pub key: JsonWebKey,
pub iv: Base64,
pub hashes: BTreeMap<String, Base64>,
pub v: String,
}
#[derive(Debug)]
#[allow(clippy::exhaustive_structs)]
pub struct EncryptedContentInit {
pub key: JsonWebKey,
pub iv: Base64,
pub hashes: BTreeMap<String, Base64>,
pub v: String,
}
impl From<EncryptedContentInit> for EncryptedContent {
fn from(init: EncryptedContentInit) -> Self {
let EncryptedContentInit { key, iv, hashes, v } = init;
Self { key, iv, hashes, v }
}
}
impl From<&EncryptedFile> for EncryptedContent {
fn from(encrypted: &EncryptedFile) -> Self {
let EncryptedFile { key, iv, hashes, v, .. } = encrypted;
Self { key: key.to_owned(), iv: iv.to_owned(), hashes: hashes.to_owned(), v: v.to_owned() }
}
}