Parse Matrix media URL for clients
This commit is contained in:
		| @@ -1,7 +1,7 @@ | ||||
| use crate::constants::USER_SESSION_KEY; | ||||
| use crate::server::HttpFailure; | ||||
| use crate::user::{APIClient, APIClientID, RumaClient, User, UserConfig, UserID}; | ||||
| use crate::utils::curr_time; | ||||
| use crate::utils::base_utils::curr_time; | ||||
| use actix_remote_ip::RemoteIP; | ||||
| use actix_session::Session; | ||||
| use actix_web::dev::Payload; | ||||
|   | ||||
| @@ -1,7 +1,7 @@ | ||||
| use crate::extractors::client_auth::APIClientAuth; | ||||
| use crate::server::HttpResult; | ||||
| use actix_web::{web, HttpResponse}; | ||||
| use ruma::api::client::{ media}; | ||||
| use ruma::api::client::media; | ||||
| use ruma::OwnedServerName; | ||||
|  | ||||
| #[derive(serde::Deserialize)] | ||||
|   | ||||
| @@ -1,9 +1,10 @@ | ||||
| use crate::extractors::client_auth::APIClientAuth; | ||||
| use crate::server::HttpResult; | ||||
| use crate::utils::matrix_utils::parse_mxc_url; | ||||
| use actix_web::{web, HttpResponse}; | ||||
| use ruma::api::client::state; | ||||
| use ruma::events::StateEventType; | ||||
| use ruma::OwnedRoomId; | ||||
| use ruma::{OwnedRoomId, OwnedServerName}; | ||||
|  | ||||
| #[derive(serde::Deserialize)] | ||||
| pub struct RoomIDInPath { | ||||
| @@ -33,6 +34,8 @@ pub async fn name(auth: APIClientAuth, path: web::Path<RoomIDInPath>) -> HttpRes | ||||
| #[derive(serde::Serialize)] | ||||
| struct GetRoomAvatarResponse { | ||||
|     url: String, | ||||
|     server_name: OwnedServerName, | ||||
|     media_id: String, | ||||
| } | ||||
|  | ||||
| /// Get room avatar | ||||
| @@ -46,6 +49,13 @@ pub async fn avatar(auth: APIClientAuth, path: web::Path<RoomIDInPath>) -> HttpR | ||||
|         .await?; | ||||
|  | ||||
|     let avatar_url = res.content.get_field("url")?.unwrap_or("").to_string(); | ||||
|     let Some((media_id, server_name)) = parse_mxc_url(&avatar_url) else { | ||||
|         return Ok(HttpResponse::InternalServerError().body("Invalid Matrix resource URL")); | ||||
|     }; | ||||
|  | ||||
|     Ok(HttpResponse::Ok().json(GetRoomAvatarResponse { url: avatar_url })) | ||||
|     Ok(HttpResponse::Ok().json(GetRoomAvatarResponse { | ||||
|         url: avatar_url.to_string(), | ||||
|         server_name, | ||||
|         media_id: media_id.to_string(), | ||||
|     })) | ||||
| } | ||||
|   | ||||
| @@ -3,7 +3,7 @@ use crate::broadcast_messages::BroadcastMessage; | ||||
| use crate::constants::{STATE_KEY, USER_SESSION_KEY}; | ||||
| use crate::server::{HttpFailure, HttpResult}; | ||||
| use crate::user::{APIClient, APIClientID, User, UserConfig, UserID}; | ||||
| use crate::utils; | ||||
| use crate::utils::base_utils; | ||||
| use actix_session::Session; | ||||
| use actix_web::{web, HttpResponse}; | ||||
| use askama::Template; | ||||
| @@ -70,7 +70,7 @@ pub async fn home( | ||||
|     // Get user information, requesting authentication if information is missing | ||||
|     let Some(user): Option<User> = session.get(USER_SESSION_KEY)? else { | ||||
|         // Generate auth state | ||||
|         let state = utils::rand_str(50); | ||||
|         let state = base_utils::rand_str(50); | ||||
|         session.insert(STATE_KEY, &state)?; | ||||
|  | ||||
|         let oidc = AppConfig::get().openid_provider(); | ||||
|   | ||||
| @@ -6,7 +6,7 @@ use thiserror::Error; | ||||
|  | ||||
| use crate::app_config::AppConfig; | ||||
| use crate::constants::TOKEN_LEN; | ||||
| use crate::utils::{curr_time, format_time, rand_str}; | ||||
| use crate::utils::base_utils::{curr_time, format_time, rand_str}; | ||||
|  | ||||
| type HttpClient = ruma::client::http_client::HyperNativeTls; | ||||
| pub type RumaClient = ruma::Client<HttpClient>; | ||||
|   | ||||
							
								
								
									
										10
									
								
								src/utils/matrix_utils.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								src/utils/matrix_utils.rs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,10 @@ | ||||
| use ruma::OwnedServerName; | ||||
| use std::str::FromStr; | ||||
|  | ||||
| /// Parse Matrix media URL returning media id and server name | ||||
| pub fn parse_mxc_url(url: &str) -> Option<(&str, OwnedServerName)> { | ||||
|     let strip = url.strip_prefix("mxc://")?; | ||||
|     let parts = strip.split_once('/')?; | ||||
|  | ||||
|     Some((parts.0, OwnedServerName::from_str(parts.1).ok()?)) | ||||
| } | ||||
							
								
								
									
										2
									
								
								src/utils/mod.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										2
									
								
								src/utils/mod.rs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,2 @@ | ||||
| pub mod base_utils; | ||||
| pub mod matrix_utils; | ||||
		Reference in New Issue
	
	Block a user