jax_daemon/http_server/api/v0/bucket/
unshare.rs1use axum::extract::{Json, State};
2use axum::response::{IntoResponse, Response};
3use common::prelude::MountError;
4use reqwest::{Client, RequestBuilder, Url};
5use serde::{Deserialize, Serialize};
6use uuid::Uuid;
7
8use common::crypto::PublicKey;
9
10use crate::http_server::api::client::ApiRequest;
11use crate::ServiceState;
12
13#[derive(Debug, Clone, Serialize, Deserialize, clap::Args)]
14pub struct UnshareRequest {
15 #[arg(long)]
17 pub bucket_id: Uuid,
18
19 #[arg(long)]
21 pub peer_public_key: String,
22}
23
24#[derive(Debug, Clone, Serialize, Deserialize)]
25pub struct UnshareResponse {
26 pub bucket_id: Uuid,
27 pub peer_public_key: String,
28 pub new_bucket_link: String,
29}
30
31pub async fn handler(
32 State(state): State<ServiceState>,
33 Json(req): Json<UnshareRequest>,
34) -> Result<impl IntoResponse, UnshareError> {
35 tracing::info!(
36 "UNSHARE API: Received unshare request for bucket {} removing peer {}",
37 req.bucket_id,
38 req.peer_public_key,
39 );
40
41 let peer_public_key = PublicKey::from_hex(&req.peer_public_key)
43 .map_err(|e| UnshareError::InvalidPublicKey(e.to_string()))?;
44
45 let mount = state.peer().mount(req.bucket_id).await?;
47
48 mount.remove_share(peer_public_key).await?;
50
51 let new_bucket_link = state.peer().save_mount(&mount, None).await?;
53
54 tracing::info!(
55 "UNSHARE API: Peer {} removed from bucket {}, new link: {}",
56 req.peer_public_key,
57 req.bucket_id,
58 new_bucket_link.hash()
59 );
60
61 Ok((
62 http::StatusCode::OK,
63 Json(UnshareResponse {
64 bucket_id: req.bucket_id,
65 peer_public_key: req.peer_public_key,
66 new_bucket_link: new_bucket_link.hash().to_string(),
67 }),
68 )
69 .into_response())
70}
71
72#[derive(Debug, thiserror::Error)]
73pub enum UnshareError {
74 #[error("Invalid public key: {0}")]
75 InvalidPublicKey(String),
76 #[error("Mount error: {0}")]
77 Mount(#[from] MountError),
78}
79
80impl IntoResponse for UnshareError {
81 fn into_response(self) -> Response {
82 match &self {
83 UnshareError::InvalidPublicKey(msg) => (
84 http::StatusCode::BAD_REQUEST,
85 format!("Invalid public key: {}", msg),
86 )
87 .into_response(),
88 UnshareError::Mount(MountError::Unauthorized) => (
89 http::StatusCode::FORBIDDEN,
90 "Unauthorized: only owners can remove shares".to_string(),
91 )
92 .into_response(),
93 UnshareError::Mount(MountError::ShareNotFound) => {
94 (http::StatusCode::NOT_FOUND, "Share not found".to_string()).into_response()
95 }
96 UnshareError::Mount(_) => (
97 http::StatusCode::INTERNAL_SERVER_ERROR,
98 "Unexpected error".to_string(),
99 )
100 .into_response(),
101 }
102 }
103}
104
105impl ApiRequest for UnshareRequest {
107 type Response = UnshareResponse;
108
109 fn build_request(self, base_url: &Url, client: &Client) -> RequestBuilder {
110 let full_url = base_url.join("/api/v0/bucket/unshare").unwrap();
111 client.post(full_url).json(&self)
112 }
113}