Can get user media
This commit is contained in:
parent
4589b3b339
commit
fa4665280d
13
src/main.rs
13
src/main.rs
@ -57,7 +57,18 @@ async fn main() -> std::io::Result<()> {
|
||||
.route("/api", web::get().to(api::api_home))
|
||||
.route("/api", web::post().to(api::api_home))
|
||||
.route("/api/account/whoami", web::get().to(api::account::who_am_i))
|
||||
.route("/api/room/{room_id}/name", web::get().to(api::rooms::name))
|
||||
.route("/api/room/{room_id}/name", web::get().to(api::room::name))
|
||||
.route(
|
||||
"/api/room/{room_id}/avatar",
|
||||
web::get().to(api::room::avatar),
|
||||
)
|
||||
.route(
|
||||
"/api/media/{server_name}/{media_id}/download",
|
||||
web::get().to(api::media::download),
|
||||
)
|
||||
// TODO : handle media thumbnail
|
||||
// TODO : handle space
|
||||
// TODO : handle user information
|
||||
.service(web::resource("/api/ws").route(web::get().to(api::ws::ws)))
|
||||
})
|
||||
.workers(4)
|
||||
|
28
src/server/api/media.rs
Normal file
28
src/server/api/media.rs
Normal file
@ -0,0 +1,28 @@
|
||||
use crate::extractors::client_auth::APIClientAuth;
|
||||
use crate::server::HttpResult;
|
||||
use actix_web::{web, HttpResponse};
|
||||
use ruma::api::client::{ media};
|
||||
use ruma::OwnedServerName;
|
||||
|
||||
#[derive(serde::Deserialize)]
|
||||
pub struct MediaInfoInPath {
|
||||
server_name: OwnedServerName,
|
||||
media_id: String,
|
||||
}
|
||||
|
||||
/// Download a media
|
||||
pub async fn download(auth: APIClientAuth, path: web::Path<MediaInfoInPath>) -> HttpResult {
|
||||
let res = auth
|
||||
.send_request(media::get_content::v3::Request::new(
|
||||
path.media_id.clone(),
|
||||
path.server_name.clone(),
|
||||
))
|
||||
.await?;
|
||||
|
||||
let mut http_res = HttpResponse::Ok();
|
||||
if let Some(content_type) = res.content_type {
|
||||
http_res.content_type(content_type);
|
||||
}
|
||||
|
||||
Ok(http_res.body(res.file))
|
||||
}
|
@ -3,7 +3,8 @@ use crate::server::HttpResult;
|
||||
use actix_web::HttpResponse;
|
||||
|
||||
pub mod account;
|
||||
pub mod rooms;
|
||||
pub mod media;
|
||||
pub mod room;
|
||||
pub mod ws;
|
||||
|
||||
/// API Home route
|
||||
|
@ -29,3 +29,23 @@ pub async fn name(auth: APIClientAuth, path: web::Path<RoomIDInPath>) -> HttpRes
|
||||
name: res.content.get_field("name")?.unwrap_or("").to_string(),
|
||||
}))
|
||||
}
|
||||
|
||||
#[derive(serde::Serialize)]
|
||||
struct GetRoomAvatarResponse {
|
||||
url: String,
|
||||
}
|
||||
|
||||
/// Get room avatar
|
||||
pub async fn avatar(auth: APIClientAuth, path: web::Path<RoomIDInPath>) -> HttpResult {
|
||||
let res = auth
|
||||
.send_request(state::get_state_events_for_key::v3::Request::new(
|
||||
path.room_id.clone(),
|
||||
StateEventType::RoomAvatar,
|
||||
String::default(),
|
||||
))
|
||||
.await?;
|
||||
|
||||
let avatar_url = res.content.get_field("url")?.unwrap_or("").to_string();
|
||||
|
||||
Ok(HttpResponse::Ok().json(GetRoomAvatarResponse { url: avatar_url }))
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user