Can get user information
This commit is contained in:
parent
6adc0c1fbb
commit
3640f72d73
@ -68,7 +68,10 @@ async fn main() -> std::io::Result<()> {
|
|||||||
)
|
)
|
||||||
// TODO : handle media thumbnail
|
// TODO : handle media thumbnail
|
||||||
// TODO : handle space
|
// TODO : handle space
|
||||||
// TODO : handle user information
|
.route(
|
||||||
|
"/api/profile/{user_id}",
|
||||||
|
web::get().to(api::profile::get_profile),
|
||||||
|
)
|
||||||
.service(web::resource("/api/ws").route(web::get().to(api::ws::ws)))
|
.service(web::resource("/api/ws").route(web::get().to(api::ws::ws)))
|
||||||
})
|
})
|
||||||
.workers(4)
|
.workers(4)
|
||||||
|
@ -4,6 +4,7 @@ use actix_web::HttpResponse;
|
|||||||
|
|
||||||
pub mod account;
|
pub mod account;
|
||||||
pub mod media;
|
pub mod media;
|
||||||
|
pub mod profile;
|
||||||
pub mod room;
|
pub mod room;
|
||||||
pub mod ws;
|
pub mod ws;
|
||||||
|
|
||||||
|
29
src/server/api/profile.rs
Normal file
29
src/server/api/profile.rs
Normal 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),
|
||||||
|
}))
|
||||||
|
}
|
@ -1,10 +1,10 @@
|
|||||||
use crate::extractors::client_auth::APIClientAuth;
|
use crate::extractors::client_auth::APIClientAuth;
|
||||||
use crate::server::HttpResult;
|
use crate::server::HttpResult;
|
||||||
use crate::utils::matrix_utils::parse_mxc_url;
|
use crate::utils::matrix_utils::ApiMxcURI;
|
||||||
use actix_web::{web, HttpResponse};
|
use actix_web::{web, HttpResponse};
|
||||||
use ruma::api::client::state;
|
use ruma::api::client::state;
|
||||||
use ruma::events::StateEventType;
|
use ruma::events::StateEventType;
|
||||||
use ruma::{OwnedRoomId, OwnedServerName};
|
use ruma::{OwnedMxcUri, OwnedRoomId};
|
||||||
|
|
||||||
#[derive(serde::Deserialize)]
|
#[derive(serde::Deserialize)]
|
||||||
pub struct RoomIDInPath {
|
pub struct RoomIDInPath {
|
||||||
@ -32,11 +32,7 @@ pub async fn name(auth: APIClientAuth, path: web::Path<RoomIDInPath>) -> HttpRes
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(serde::Serialize)]
|
#[derive(serde::Serialize)]
|
||||||
struct GetRoomAvatarResponse {
|
struct GetRoomAvatarResponse(ApiMxcURI);
|
||||||
url: String,
|
|
||||||
server_name: OwnedServerName,
|
|
||||||
media_id: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Get room avatar
|
/// Get room avatar
|
||||||
pub async fn avatar(auth: APIClientAuth, path: web::Path<RoomIDInPath>) -> HttpResult {
|
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?;
|
.await?;
|
||||||
|
|
||||||
let avatar_url = res.content.get_field("url")?.unwrap_or("").to_string();
|
let avatar_url: Option<OwnedMxcUri> = res.content.get_field("url")?;
|
||||||
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 {
|
match avatar_url {
|
||||||
url: avatar_url.to_string(),
|
None => Ok(HttpResponse::NotFound().body("No avatar found for this room.")),
|
||||||
server_name,
|
Some(avatar_url) => {
|
||||||
media_id: media_id.to_string(),
|
Ok(HttpResponse::Ok().json(GetRoomAvatarResponse(ApiMxcURI(avatar_url))))
|
||||||
}))
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,10 +1,18 @@
|
|||||||
use ruma::OwnedServerName;
|
use ruma::OwnedMxcUri;
|
||||||
use std::str::FromStr;
|
use serde::ser::SerializeMap;
|
||||||
|
use serde::{Serialize, Serializer};
|
||||||
|
|
||||||
/// Parse Matrix media URL returning media id and server name
|
pub struct ApiMxcURI(pub OwnedMxcUri);
|
||||||
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()?))
|
impl Serialize for ApiMxcURI {
|
||||||
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
|
where
|
||||||
|
S: Serializer,
|
||||||
|
{
|
||||||
|
let mut map = serializer.serialize_map(Some(3))?;
|
||||||
|
map.serialize_entry("uri", &self.0)?;
|
||||||
|
map.serialize_entry("server_name", &self.0.server_name().ok())?;
|
||||||
|
map.serialize_entry("media_id", &self.0.media_id().ok())?;
|
||||||
|
map.end()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user