Can get multiple profiles information
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
use crate::controllers::HttpResult;
|
use crate::controllers::HttpResult;
|
||||||
use crate::extractors::matrix_client_extractor::MatrixClientExtractor;
|
use crate::extractors::matrix_client_extractor::MatrixClientExtractor;
|
||||||
use actix_web::{HttpResponse, web};
|
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::api::client::profile::{AvatarUrl, DisplayName, get_profile};
|
||||||
use matrix_sdk::ruma::{OwnedMxcUri, OwnedUserId};
|
use matrix_sdk::ruma::{OwnedMxcUri, OwnedUserId};
|
||||||
|
|
||||||
@@ -11,13 +12,15 @@ pub struct UserIDInPath {
|
|||||||
|
|
||||||
#[derive(serde::Serialize)]
|
#[derive(serde::Serialize)]
|
||||||
struct ProfileResponse {
|
struct ProfileResponse {
|
||||||
|
user_id: OwnedUserId,
|
||||||
display_name: Option<String>,
|
display_name: Option<String>,
|
||||||
avatar: Option<OwnedMxcUri>,
|
avatar: Option<OwnedMxcUri>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ProfileResponse {
|
impl ProfileResponse {
|
||||||
pub fn from(r: get_profile::v3::Response) -> anyhow::Result<Self> {
|
pub fn from(user_id: OwnedUserId, r: get_profile::v3::Response) -> anyhow::Result<Self> {
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
|
user_id,
|
||||||
display_name: r.get_static::<DisplayName>()?,
|
display_name: r.get_static::<DisplayName>()?,
|
||||||
avatar: r.get_static::<AvatarUrl>()?,
|
avatar: r.get_static::<AvatarUrl>()?,
|
||||||
})
|
})
|
||||||
@@ -36,5 +39,29 @@ pub async fn get_profile(
|
|||||||
.fetch_user_profile_of(&path.user_id)
|
.fetch_user_profile_of(&path.user_id)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
Ok(HttpResponse::Ok().json(ProfileResponse::from(profile)?))
|
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))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -153,6 +153,10 @@ async fn main() -> std::io::Result<()> {
|
|||||||
"/api/matrix/profile/{user_id}",
|
"/api/matrix/profile/{user_id}",
|
||||||
web::get().to(matrix_profile_controller::get_profile),
|
web::get().to(matrix_profile_controller::get_profile),
|
||||||
)
|
)
|
||||||
|
.route(
|
||||||
|
"/api/matrix/profile/get_multiple",
|
||||||
|
web::post().to(matrix_profile_controller::get_multiple),
|
||||||
|
)
|
||||||
})
|
})
|
||||||
.workers(4)
|
.workers(4)
|
||||||
.bind(&AppConfig::get().listen_address)?
|
.bind(&AppConfig::get().listen_address)?
|
||||||
|
|||||||
Reference in New Issue
Block a user