matrix_appservice_rs/
matrix.rs1use ruma::api::exports::http::uri;
2use ruma::identifiers::{EventId, MxcUri, RoomId, UserId};
3
4#[derive(Debug, Clone, Hash, PartialEq, Eq)]
6pub enum MatrixToItem<'a> {
7 Event(&'a RoomId, &'a EventId),
9 User(&'a UserId),
11 Group(&'a String),
13}
14
15impl<'a> MatrixToItem<'a> {
16 pub fn to_url_string(&self) -> String {
18 let slug = match self {
19 MatrixToItem::Event(room_id, event_id) => format!("{}/{}", room_id, event_id),
20 MatrixToItem::User(user_id) => user_id.to_string(),
21 MatrixToItem::Group(group_id) => group_id.to_string(),
22 };
23
24 format!("https://matrix.to/#/{}", slug)
25 }
26}
27
28#[derive(Debug)]
30pub enum MxcConversionError {
31 InvalidMxc,
33 UriParseError(uri::InvalidUri),
35}
36
37impl From<uri::InvalidUri> for MxcConversionError {
38 fn from(err: uri::InvalidUri) -> Self {
39 MxcConversionError::UriParseError(err)
40 }
41}
42
43pub fn mxc_to_url(
46 homeserver_url: &uri::Uri,
47 mxc_uri: &MxcUri,
48) -> Result<uri::Uri, MxcConversionError> {
49 let (server_name, id) = mxc_uri.parts().ok_or(MxcConversionError::InvalidMxc)?;
50
51 let res = format!(
52 "{}_matrix/media/r0/download/{}/{}",
53 homeserver_url, server_name, id
54 );
55 Ok(res.parse()?)
56}