Compare commits
4 Commits
ecbe4885c1
...
35b53fee5c
| Author | SHA1 | Date | |
|---|---|---|---|
| 35b53fee5c | |||
| 934e6a4cc1 | |||
| b744265242 | |||
| e8ce97eea0 |
21
matrixgw_backend/Cargo.lock
generated
21
matrixgw_backend/Cargo.lock
generated
@@ -777,6 +777,17 @@ version = "0.4.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4f4c707c6a209cbe82d10abd08e1ea8995e9ea937d2550646e02798948992be0"
|
||||
|
||||
[[package]]
|
||||
name = "cfb"
|
||||
version = "0.7.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d38f2da7a0a2c4ccf0065be06397cc26a81f4e528be095826eee9d4adbb8c60f"
|
||||
dependencies = [
|
||||
"byteorder",
|
||||
"fnv",
|
||||
"uuid",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "cfg-if"
|
||||
version = "1.0.4"
|
||||
@@ -2357,6 +2368,15 @@ dependencies = [
|
||||
"serde_core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "infer"
|
||||
version = "0.19.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a588916bfdfd92e71cacef98a63d9b1f0d74d6599980d11894290e7ddefffcf7"
|
||||
dependencies = [
|
||||
"cfb",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "inout"
|
||||
version = "0.1.4"
|
||||
@@ -3048,6 +3068,7 @@ dependencies = [
|
||||
"env_logger",
|
||||
"futures-util",
|
||||
"hex",
|
||||
"infer",
|
||||
"ipnet",
|
||||
"jwt-simple",
|
||||
"lazy-regex",
|
||||
|
||||
@@ -32,4 +32,5 @@ url = "2.5.7"
|
||||
ractor = "0.15.9"
|
||||
serde_json = "1.0.145"
|
||||
lazy-regex = "3.4.2"
|
||||
actix-ws = "0.3.0"
|
||||
actix-ws = "0.3.0"
|
||||
infer = "0.19.0"
|
||||
@@ -0,0 +1,67 @@
|
||||
use crate::controllers::HttpResult;
|
||||
use crate::extractors::matrix_client_extractor::MatrixClientExtractor;
|
||||
use crate::utils::crypt_utils::sha512;
|
||||
use actix_web::dev::Payload;
|
||||
use actix_web::http::header;
|
||||
use actix_web::{FromRequest, HttpRequest, HttpResponse, web};
|
||||
use matrix_sdk::media::{MediaFormat, MediaRequestParameters, MediaThumbnailSettings};
|
||||
use matrix_sdk::ruma::events::room::MediaSource;
|
||||
use matrix_sdk::ruma::{OwnedMxcUri, UInt};
|
||||
|
||||
#[derive(serde::Deserialize)]
|
||||
struct MediaQuery {
|
||||
#[serde(default)]
|
||||
thumbnail: bool,
|
||||
}
|
||||
|
||||
/// Serve a media file
|
||||
pub async fn serve_media(req: HttpRequest, media: OwnedMxcUri) -> HttpResult {
|
||||
let query = web::Query::<MediaQuery>::from_request(&req, &mut Payload::None).await?;
|
||||
let client = MatrixClientExtractor::from_request(&req, &mut Payload::None).await?;
|
||||
|
||||
let media = client
|
||||
.client
|
||||
.client
|
||||
.media()
|
||||
.get_media_content(
|
||||
&MediaRequestParameters {
|
||||
source: MediaSource::Plain(media),
|
||||
format: match query.thumbnail {
|
||||
true => MediaFormat::Thumbnail(MediaThumbnailSettings::new(
|
||||
UInt::new(100).unwrap(),
|
||||
UInt::new(100).unwrap(),
|
||||
)),
|
||||
false => MediaFormat::File,
|
||||
},
|
||||
},
|
||||
true,
|
||||
)
|
||||
.await?;
|
||||
|
||||
let digest = sha512(&media);
|
||||
|
||||
let mime_type = infer::get(&media).map(|x| x.mime_type());
|
||||
|
||||
// Check if the browser already knows the etag
|
||||
if let Some(c) = req.headers().get(header::IF_NONE_MATCH)
|
||||
&& c.to_str().unwrap_or("") == digest
|
||||
{
|
||||
return Ok(HttpResponse::NotModified().finish());
|
||||
}
|
||||
|
||||
Ok(HttpResponse::Ok()
|
||||
.content_type(mime_type.unwrap_or("application/octet-stream"))
|
||||
.insert_header(("etag", digest))
|
||||
.insert_header(("cache-control", "max-age=360000"))
|
||||
.body(media))
|
||||
}
|
||||
|
||||
#[derive(serde::Deserialize)]
|
||||
pub struct MediaMXCInPath {
|
||||
mxc: OwnedMxcUri,
|
||||
}
|
||||
|
||||
/// Save media resource handler
|
||||
pub async fn serve_media_res(req: HttpRequest, media: web::Path<MediaMXCInPath>) -> HttpResult {
|
||||
serve_media(req, media.into_inner().mxc).await
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
use crate::controllers::HttpResult;
|
||||
use crate::extractors::matrix_client_extractor::MatrixClientExtractor;
|
||||
use actix_web::{HttpResponse, web};
|
||||
use futures_util::{StreamExt, stream};
|
||||
use matrix_sdk::ruma::api::client::profile::{AvatarUrl, DisplayName, get_profile};
|
||||
use matrix_sdk::ruma::{OwnedMxcUri, OwnedUserId};
|
||||
|
||||
#[derive(serde::Deserialize)]
|
||||
pub struct UserIDInPath {
|
||||
user_id: OwnedUserId,
|
||||
}
|
||||
|
||||
#[derive(serde::Serialize)]
|
||||
struct ProfileResponse {
|
||||
user_id: OwnedUserId,
|
||||
display_name: Option<String>,
|
||||
avatar: Option<OwnedMxcUri>,
|
||||
}
|
||||
|
||||
impl ProfileResponse {
|
||||
pub fn from(user_id: OwnedUserId, r: get_profile::v3::Response) -> anyhow::Result<Self> {
|
||||
Ok(Self {
|
||||
user_id,
|
||||
display_name: r.get_static::<DisplayName>()?,
|
||||
avatar: r.get_static::<AvatarUrl>()?,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// Get user profile
|
||||
pub async fn get_profile(
|
||||
client: MatrixClientExtractor,
|
||||
path: web::Path<UserIDInPath>,
|
||||
) -> HttpResult {
|
||||
let profile = client
|
||||
.client
|
||||
.client
|
||||
.account()
|
||||
.fetch_user_profile_of(&path.user_id)
|
||||
.await?;
|
||||
|
||||
Ok(HttpResponse::Ok().json(ProfileResponse::from(path.user_id.clone(), profile)?))
|
||||
}
|
||||
|
||||
/// Get multiple users profiles
|
||||
pub async fn get_multiple(client: MatrixClientExtractor) -> HttpResult {
|
||||
let users = client.auth.decode_json_body::<Vec<OwnedUserId>>()?;
|
||||
|
||||
let list = stream::iter(users)
|
||||
.then(async |user_id| {
|
||||
client
|
||||
.client
|
||||
.client
|
||||
.account()
|
||||
.fetch_user_profile_of(&user_id)
|
||||
.await
|
||||
.map(|r| ProfileResponse::from(user_id, r))
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
.await
|
||||
.into_iter()
|
||||
.collect::<Result<Vec<_>, _>>()?
|
||||
.into_iter()
|
||||
.collect::<Result<Vec<_>, _>>()?;
|
||||
|
||||
Ok(HttpResponse::Ok().json(list))
|
||||
}
|
||||
@@ -1,8 +1,9 @@
|
||||
use crate::controllers::HttpResult;
|
||||
use crate::controllers::matrix::matrix_media_controller;
|
||||
use crate::extractors::matrix_client_extractor::MatrixClientExtractor;
|
||||
use actix_web::{HttpResponse, web};
|
||||
use actix_web::{HttpRequest, HttpResponse, web};
|
||||
use futures_util::{StreamExt, stream};
|
||||
use matrix_sdk::ruma::{OwnedRoomId, OwnedUserId};
|
||||
use matrix_sdk::ruma::{OwnedMxcUri, OwnedRoomId, OwnedUserId};
|
||||
use matrix_sdk::{Room, RoomMemberships};
|
||||
|
||||
#[derive(serde::Serialize)]
|
||||
@@ -10,7 +11,7 @@ pub struct APIRoomInfo {
|
||||
id: OwnedRoomId,
|
||||
name: Option<String>,
|
||||
members: Vec<OwnedUserId>,
|
||||
has_avatar: bool,
|
||||
avatar: Option<OwnedMxcUri>,
|
||||
}
|
||||
|
||||
impl APIRoomInfo {
|
||||
@@ -24,7 +25,7 @@ impl APIRoomInfo {
|
||||
.into_iter()
|
||||
.map(|r| r.user_id().to_owned())
|
||||
.collect::<Vec<_>>(),
|
||||
has_avatar: r.avatar_url().is_some(),
|
||||
avatar: r.avatar_url(),
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -56,3 +57,20 @@ pub async fn single_room_info(
|
||||
Some(r) => HttpResponse::Ok().json(APIRoomInfo::from_room(&r).await?),
|
||||
})
|
||||
}
|
||||
|
||||
/// Get room avatar
|
||||
pub async fn room_avatar(
|
||||
req: HttpRequest,
|
||||
client: MatrixClientExtractor,
|
||||
path: web::Path<RoomIdInPath>,
|
||||
) -> HttpResult {
|
||||
let Some(room) = client.client.client.get_room(&path.id) else {
|
||||
return Ok(HttpResponse::NotFound().json("Room not found"));
|
||||
};
|
||||
|
||||
let Some(uri) = room.avatar_url() else {
|
||||
return Ok(HttpResponse::NotFound().json("Room has no avatar"));
|
||||
};
|
||||
|
||||
matrix_media_controller::serve_media(req, uri).await
|
||||
}
|
||||
|
||||
@@ -1 +1,3 @@
|
||||
pub mod matrix_media_controller;
|
||||
pub mod matrix_profile_controller;
|
||||
pub mod matrix_room_controller;
|
||||
|
||||
@@ -22,6 +22,8 @@ pub enum HttpFailure {
|
||||
InternalError(#[from] anyhow::Error),
|
||||
#[error("Actix web error: {0}")]
|
||||
ActixError(#[from] actix_web::Error),
|
||||
#[error("Matrix error: {0}")]
|
||||
MatrixError(#[from] matrix_sdk::Error),
|
||||
}
|
||||
|
||||
impl ResponseError for HttpFailure {
|
||||
|
||||
@@ -9,7 +9,9 @@ use actix_web::{App, HttpServer, web};
|
||||
use matrixgw_backend::app_config::AppConfig;
|
||||
use matrixgw_backend::broadcast_messages::BroadcastMessage;
|
||||
use matrixgw_backend::constants;
|
||||
use matrixgw_backend::controllers::matrix::matrix_room_controller;
|
||||
use matrixgw_backend::controllers::matrix::{
|
||||
matrix_media_controller, matrix_profile_controller, matrix_room_controller,
|
||||
};
|
||||
use matrixgw_backend::controllers::{
|
||||
auth_controller, matrix_link_controller, matrix_sync_thread_controller, server_controller,
|
||||
tokens_controller, ws_controller,
|
||||
@@ -144,6 +146,24 @@ async fn main() -> std::io::Result<()> {
|
||||
"/api/matrix/room/{id}",
|
||||
web::get().to(matrix_room_controller::single_room_info),
|
||||
)
|
||||
.route(
|
||||
"/api/matrix/room/{id}/avatar",
|
||||
web::get().to(matrix_room_controller::room_avatar),
|
||||
)
|
||||
// Matrix profile controller
|
||||
.route(
|
||||
"/api/matrix/profile/{user_id}",
|
||||
web::get().to(matrix_profile_controller::get_profile),
|
||||
)
|
||||
.route(
|
||||
"/api/matrix/profile/get_multiple",
|
||||
web::post().to(matrix_profile_controller::get_multiple),
|
||||
)
|
||||
// Matrix media controller
|
||||
.route(
|
||||
"/api/matrix/media/{mxc}",
|
||||
web::get().to(matrix_media_controller::serve_media_res),
|
||||
)
|
||||
})
|
||||
.workers(4)
|
||||
.bind(&AppConfig::get().listen_address)?
|
||||
|
||||
@@ -1,6 +1,11 @@
|
||||
use sha2::{Digest, Sha256};
|
||||
use sha2::{Digest, Sha256, Sha512};
|
||||
|
||||
/// Compute SHA256sum of a given string
|
||||
pub fn sha256str(input: &str) -> String {
|
||||
hex::encode(Sha256::digest(input.as_bytes()))
|
||||
}
|
||||
|
||||
/// Compute SHA256sum of a given byte array
|
||||
pub fn sha512(input: &[u8]) -> String {
|
||||
hex::encode(Sha512::digest(input))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user