From 43782e39cb563927dfa346bd5e3de28d2f64440e Mon Sep 17 00:00:00 2001 From: Fico <70634661+fbozic@users.noreply.github.com> Date: Fri, 18 Oct 2024 12:08:35 +0200 Subject: [PATCH] feat(server): implement invite_to_context endpoint (#820) --- crates/server-primitives/src/admin.rs | 38 +++++++++++++++++++ crates/server/src/admin/handlers/context.rs | 1 + .../handlers/context/invite_to_context.rs | 27 +++++++++++++ 3 files changed, 66 insertions(+) create mode 100644 crates/server/src/admin/handlers/context/invite_to_context.rs diff --git a/crates/server-primitives/src/admin.rs b/crates/server-primitives/src/admin.rs index 3e1deb07f..68122c932 100644 --- a/crates/server-primitives/src/admin.rs +++ b/crates/server-primitives/src/admin.rs @@ -421,6 +421,44 @@ impl CreateContextRequest { } } } +#[derive(Debug, Deserialize, Serialize)] +#[non_exhaustive] +pub struct InviteToContextRequest { + pub context_id: ContextId, + pub inviter_id: PublicKey, + pub invitee_id: PublicKey, +} + +impl InviteToContextRequest { + #[must_use] + pub const fn new(context_id: ContextId, inviter_id: PublicKey, invitee_id: PublicKey) -> Self { + Self { + context_id, + inviter_id, + invitee_id, + } + } +} + +#[derive(Debug, Deserialize, Serialize)] +#[non_exhaustive] +pub struct InviteToContextResponse { + pub invitation_payload: Option, +} + +impl InviteToContextResponse { + #[must_use] + pub const fn new(invitation_payload: Option) -> Self { + Self { invitation_payload } + } +} + +#[derive(Debug, Deserialize, Serialize)] +#[non_exhaustive] +pub struct InviteToContextResponseData { + pub context_id: ContextId, + pub invitee_public_key: PublicKey, +} #[derive(Debug, Deserialize, Serialize)] #[non_exhaustive] diff --git a/crates/server/src/admin/handlers/context.rs b/crates/server/src/admin/handlers/context.rs index 146f48442..0317bcba2 100644 --- a/crates/server/src/admin/handlers/context.rs +++ b/crates/server/src/admin/handlers/context.rs @@ -6,5 +6,6 @@ pub mod get_context_identities; pub mod get_context_storage; pub mod get_context_users; pub mod get_contexts; +pub mod invite_to_context; pub mod join_context; pub mod update_application_id; diff --git a/crates/server/src/admin/handlers/context/invite_to_context.rs b/crates/server/src/admin/handlers/context/invite_to_context.rs new file mode 100644 index 000000000..51c1a18e1 --- /dev/null +++ b/crates/server/src/admin/handlers/context/invite_to_context.rs @@ -0,0 +1,27 @@ +use std::sync::Arc; + +use axum::response::IntoResponse; +use axum::{Extension, Json}; +use calimero_server_primitives::admin::{InviteToContextRequest, InviteToContextResponse}; + +use crate::admin::service::{parse_api_error, ApiResponse}; +use crate::AdminState; + +pub async fn handler( + Extension(state): Extension>, + Json(req): Json, +) -> impl IntoResponse { + let result = state + .ctx_manager + .invite_to_context(req.context_id, req.inviter_id, req.invitee_id) + .await + .map_err(parse_api_error); + + match result { + Ok(invitation_payload) => ApiResponse { + payload: InviteToContextResponse::new(invitation_payload), + } + .into_response(), + Err(err) => err.into_response(), + } +}