diff --git a/crates/primitives/src/identity.rs b/crates/primitives/src/identity.rs index c623d84ec..2d2aa5e9f 100644 --- a/crates/primitives/src/identity.rs +++ b/crates/primitives/src/identity.rs @@ -17,6 +17,7 @@ pub struct RootKey { } #[derive(Debug, Serialize, Deserialize, Clone)] +#[serde(rename_all = "camelCase")] pub struct ClientKey { #[serde(rename = "wallet")] pub wallet_type: WalletType, @@ -25,6 +26,13 @@ pub struct ClientKey { pub context_id: String, } +#[derive(Debug, Serialize, Deserialize, Clone)] +#[serde(rename_all = "camelCase")] +pub struct ContextUser { + pub user_id: String, + pub joined_at: u64, +} + #[derive(Debug, Serialize, Deserialize, Clone)] #[serde(rename_all = "camelCase")] pub struct Context { diff --git a/crates/server/src/admin/handlers/context.rs b/crates/server/src/admin/handlers/context.rs index 8fe92070c..ac2963c87 100644 --- a/crates/server/src/admin/handlers/context.rs +++ b/crates/server/src/admin/handlers/context.rs @@ -3,7 +3,7 @@ use std::sync::Arc; use axum::extract::Path; use axum::response::IntoResponse; use axum::{Extension, Json}; -use calimero_primitives::identity::{ClientKey, Context}; +use calimero_primitives::identity::{ClientKey, Context, ContextUser}; use calimero_server_primitives::admin::ContextStorage; use rand::RngCore; use reqwest::StatusCode; @@ -14,17 +14,9 @@ use crate::admin::service::{parse_api_error, AdminState, ApiError, ApiResponse}; use crate::admin::storage::client_keys::get_context_client_key; use crate::admin::storage::context::{add_context, delete_context, get_context, get_contexts}; -#[derive(Debug, Serialize, Deserialize)] -#[serde(rename_all = "camelCase")] -pub struct ContextData { - context: Context, - client_keys: Vec, - users: Vec, -} - #[derive(Debug, Serialize, Deserialize)] pub struct GetContextResponse { - data: ContextData, + data: Context, } pub async fn get_context_handler( @@ -36,24 +28,10 @@ pub async fn get_context_handler( match context_result { Ok(ctx) => match ctx { - Some(context) => { - let client_keys_result = - get_context_client_key(&state.store, &context.application_id) - .map_err(|err| parse_api_error(err).into_response()); - match client_keys_result { - Ok(client_keys) => ApiResponse { - payload: GetContextResponse { - data: ContextData { - context, - client_keys, - users: vec![], - }, - }, - } - .into_response(), - Err(err) => err.into_response(), - } + Some(context) => ApiResponse { + payload: GetContextResponse { data: context }, } + .into_response(), None => ApiError { status_code: StatusCode::NOT_FOUND, message: "Context not found".into(), @@ -64,6 +42,59 @@ pub async fn get_context_handler( } } +#[derive(Debug, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct ClientKeys { + client_keys: Vec, +} + +#[derive(Debug, Serialize, Deserialize)] +pub struct GetContextClientKeysResponse { + data: ClientKeys, +} + +pub async fn get_context_client_keys_handler( + Path(context_id): Path, + Extension(state): Extension>, +) -> impl IntoResponse { + let client_keys_result = get_context_client_key(&state.store, &context_id) + .map_err(|err| parse_api_error(err).into_response()); + match client_keys_result { + Ok(client_keys) => ApiResponse { + payload: GetContextClientKeysResponse { + data: ClientKeys { client_keys }, + }, + } + .into_response(), + Err(err) => err.into_response(), + } +} + +#[derive(Debug, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +struct ContextUsers { + context_users: Vec, +} + +#[derive(Debug, Serialize, Deserialize)] +pub struct GetContextUsersResponse { + data: ContextUsers, +} + +pub async fn get_context_users_handler( + Path(context_id): Path, + Extension(state): Extension>, +) -> impl IntoResponse { + ApiResponse { + payload: GetContextUsersResponse { + data: ContextUsers { + context_users: vec![], + }, + }, + } + .into_response() +} + #[derive(Debug, Serialize, Deserialize)] pub struct GetContextsResponse { data: Vec, @@ -149,8 +180,10 @@ pub async fn get_context_storage_handler( Path(_context_id): Path, Extension(_state): Extension>, ) -> impl IntoResponse { - ApiResponse { - payload: GetContextStorageResponse { data: ContextStorage { size_in_bytes: 0}}, - } - .into_response() + ApiResponse { + payload: GetContextStorageResponse { + data: ContextStorage { size_in_bytes: 0 }, + }, + } + .into_response() } diff --git a/crates/server/src/admin/service.rs b/crates/server/src/admin/service.rs index 9c0b029b7..486e75d59 100644 --- a/crates/server/src/admin/service.rs +++ b/crates/server/src/admin/service.rs @@ -20,7 +20,7 @@ use super::handlers::add_client_key::add_client_key_handler; use super::handlers::challenge::request_challenge_handler; use super::handlers::context::{ create_context_handler, delete_context_handler, get_context_handler, get_contexts_handler, - get_context_storage_handler, + get_context_storage_handler, get_context_client_keys_handler, get_context_users_handler }; use super::handlers::fetch_did::fetch_did_handler; use super::handlers::root_keys::create_root_key_handler; @@ -72,6 +72,8 @@ pub(crate) fn setup( .route("/contexts", post(create_context_handler)) .route("/contexts/:context_id", delete(delete_context_handler)) .route("/contexts/:context_id", get(get_context_handler)) + .route("/contexts/:context_id/users", get(get_context_users_handler)) + .route("/contexts/:context_id/client-keys", get(get_context_client_keys_handler)) .route("/contexts/:context_id/storage", get(get_context_storage_handler)) .route("/contexts", get(get_contexts_handler)) .layer(Extension(shared_state))