Can get current user identity
This commit is contained in:
23
src/server/api/account.rs
Normal file
23
src/server/api/account.rs
Normal file
@@ -0,0 +1,23 @@
|
||||
use crate::extractors::client_auth::APIClientAuth;
|
||||
use crate::server::HttpResult;
|
||||
use actix_web::HttpResponse;
|
||||
use ruma::api::client::account;
|
||||
use ruma::DeviceId;
|
||||
|
||||
#[derive(serde::Serialize)]
|
||||
struct WhoAmIResponse {
|
||||
user_id: String,
|
||||
device_id: Option<String>,
|
||||
}
|
||||
|
||||
/// Get current user identity
|
||||
pub async fn who_am_i(auth: APIClientAuth) -> HttpResult {
|
||||
let res = auth
|
||||
.send_request(account::whoami::v3::Request::default())
|
||||
.await?;
|
||||
|
||||
Ok(HttpResponse::Ok().json(WhoAmIResponse {
|
||||
user_id: res.user_id.to_string(),
|
||||
device_id: res.device_id.as_deref().map(DeviceId::to_string),
|
||||
}))
|
||||
}
|
@@ -2,6 +2,8 @@ use crate::extractors::client_auth::APIClientAuth;
|
||||
use crate::server::HttpResult;
|
||||
use actix_web::HttpResponse;
|
||||
|
||||
pub mod account;
|
||||
|
||||
/// API Home route
|
||||
pub async fn api_home(auth: APIClientAuth) -> HttpResult {
|
||||
Ok(HttpResponse::Ok().body(format!("Welcome user {}!", auth.user.user_id.0)))
|
@@ -1,6 +1,7 @@
|
||||
use actix_web::http::StatusCode;
|
||||
use actix_web::{HttpResponse, ResponseError};
|
||||
use std::error::Error;
|
||||
use std::fmt::Debug;
|
||||
|
||||
pub mod api;
|
||||
pub mod web_ui;
|
||||
@@ -21,6 +22,10 @@ pub enum HttpFailure {
|
||||
FetchUserConfig(anyhow::Error),
|
||||
#[error("an unspecified internal error occurred: {0}")]
|
||||
InternalError(#[from] anyhow::Error),
|
||||
#[error("a matrix api client error occurred: {0}")]
|
||||
MatrixApiClientError(#[from] ruma::api::client::Error),
|
||||
#[error("a matrix client error occurred: {0}")]
|
||||
MatrixClientError(String),
|
||||
}
|
||||
|
||||
impl ResponseError for HttpFailure {
|
||||
@@ -37,4 +42,4 @@ impl ResponseError for HttpFailure {
|
||||
}
|
||||
}
|
||||
|
||||
pub type HttpResult = std::result::Result<HttpResponse, HttpFailure>;
|
||||
pub type HttpResult = Result<HttpResponse, HttpFailure>;
|
||||
|
Reference in New Issue
Block a user