Can get user information

This commit is contained in:
2025-02-27 20:08:13 +01:00
parent 6adc0c1fbb
commit 3640f72d73
5 changed files with 59 additions and 24 deletions

View File

@ -4,6 +4,7 @@ use actix_web::HttpResponse;
pub mod account;
pub mod media;
pub mod profile;
pub mod room;
pub mod ws;

29
src/server/api/profile.rs Normal file
View File

@ -0,0 +1,29 @@
use crate::extractors::client_auth::APIClientAuth;
use crate::server::HttpResult;
use crate::utils::matrix_utils::ApiMxcURI;
use actix_web::{web, HttpResponse};
use ruma::api::client::profile;
use ruma::OwnedUserId;
#[derive(serde::Deserialize)]
pub struct UserIDInPath {
user_id: OwnedUserId,
}
#[derive(serde::Serialize)]
struct ProfileResponse {
display_name: Option<String>,
avatar: Option<ApiMxcURI>,
}
/// Get user profile
pub async fn get_profile(auth: APIClientAuth, path: web::Path<UserIDInPath>) -> HttpResult {
let res = auth
.send_request(profile::get_profile::v3::Request::new(path.user_id.clone()))
.await?;
Ok(HttpResponse::Ok().json(ProfileResponse {
display_name: res.displayname,
avatar: res.avatar_url.map(ApiMxcURI),
}))
}

View File

@ -1,10 +1,10 @@
use crate::extractors::client_auth::APIClientAuth;
use crate::server::HttpResult;
use crate::utils::matrix_utils::parse_mxc_url;
use crate::utils::matrix_utils::ApiMxcURI;
use actix_web::{web, HttpResponse};
use ruma::api::client::state;
use ruma::events::StateEventType;
use ruma::{OwnedRoomId, OwnedServerName};
use ruma::{OwnedMxcUri, OwnedRoomId};
#[derive(serde::Deserialize)]
pub struct RoomIDInPath {
@ -32,11 +32,7 @@ pub async fn name(auth: APIClientAuth, path: web::Path<RoomIDInPath>) -> HttpRes
}
#[derive(serde::Serialize)]
struct GetRoomAvatarResponse {
url: String,
server_name: OwnedServerName,
media_id: String,
}
struct GetRoomAvatarResponse(ApiMxcURI);
/// Get room avatar
pub async fn avatar(auth: APIClientAuth, path: web::Path<RoomIDInPath>) -> HttpResult {
@ -48,14 +44,12 @@ 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"));
};
let avatar_url: Option<OwnedMxcUri> = res.content.get_field("url")?;
Ok(HttpResponse::Ok().json(GetRoomAvatarResponse {
url: avatar_url.to_string(),
server_name,
media_id: media_id.to_string(),
}))
match avatar_url {
None => Ok(HttpResponse::NotFound().body("No avatar found for this room.")),
Some(avatar_url) => {
Ok(HttpResponse::Ok().json(GetRoomAvatarResponse(ApiMxcURI(avatar_url))))
}
}
}