Skip to content
This repository has been archived by the owner on Apr 29, 2024. It is now read-only.

Commit

Permalink
httpd: Announce refs in project POST/PATCH routes
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastinez authored and cloudhead committed Nov 24, 2023
1 parent 1cbca28 commit 638c2a3
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
13 changes: 12 additions & 1 deletion radicle-httpd/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@ use radicle::cob::{issue, Uri};
use radicle::cob::{patch, Embed};
use radicle::identity::{DocAt, Id};
use radicle::node::routing::Store;
use radicle::node::Handle;
use radicle::storage::{Oid, ReadRepository, ReadStorage};
use radicle::Profile;
use radicle::{Node, Profile};

mod error;
mod json;
mod v1;

use crate::api::error::Error;
use crate::cache::Cache;
use crate::Options;

Expand Down Expand Up @@ -232,6 +234,15 @@ impl TryFrom<&Uri> for DataUri {
}
}

/// Announce refs to the network for the given RID.
pub fn announce_refs(mut node: Node, rid: Id) -> Result<(), Error> {
match node.announce_refs(rid) {
Ok(_) => Ok(()),
Err(e) if e.is_connection_err() => Ok(()),
Err(e) => return Err(e.into()),
}
}

/// Resolve an embed with a URI to one with actual data.
pub fn resolve_embed(repo: &impl ReadRepository, embed: Embed<Uri>) -> Option<Embed<Vec<u8>>> {
DataUri::try_from(&embed.content)
Expand Down
19 changes: 15 additions & 4 deletions radicle-httpd/src/api/v1/projects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,14 @@ use tower_http::set_header::SetResponseHeaderLayer;
use radicle::cob::{issue, patch, Embed, Label, Uri};
use radicle::identity::{Did, Id, Visibility};
use radicle::node::routing::Store;
use radicle::node::AliasStore;
use radicle::node::NodeId;
use radicle::node::{AliasStore, Node, NodeId};
use radicle::storage::git::paths;
use radicle::storage::{ReadRepository, ReadStorage, RemoteRepository, WriteRepository};
use radicle_surf::{diff, Glob, Oid, Repository};

use crate::api::error::Error;
use crate::api::project::Info;
use crate::api::{self, resolve_embed, CobsQuery, Context, PaginationQuery};
use crate::api::{self, announce_refs, resolve_embed, CobsQuery, Context, PaginationQuery};
use crate::axum_extra::{Path, Query};

const CACHE_1_HOUR: &str = "public, max-age=3600, must-revalidate";
Expand Down Expand Up @@ -604,6 +603,7 @@ async fn issue_create_handler(
Json(issue): Json<IssueCreate>,
) -> impl IntoResponse {
api::auth::validate(&ctx, &token).await?;
let node = Node::new(ctx.profile.socket());
let storage = &ctx.profile.storage;
let signer = ctx
.profile
Expand All @@ -628,6 +628,8 @@ async fn issue_create_handler(
)
.map_err(Error::from)?;

announce_refs(node, repo.id())?;

Ok::<_, Error>((
StatusCode::CREATED,
Json(json!({ "success": true, "id": issue.id().to_string() })),
Expand All @@ -643,7 +645,7 @@ async fn issue_update_handler(
Json(action): Json<issue::Action>,
) -> impl IntoResponse {
api::auth::validate(&ctx, &token).await?;

let node = Node::new(ctx.profile.socket());
let storage = &ctx.profile.storage;
let signer = ctx.profile.signer()?;
let repo = storage.repository(project)?;
Expand Down Expand Up @@ -685,6 +687,8 @@ async fn issue_update_handler(
issue::Action::CommentRedact { id } => issue.redact_comment(id, &signer)?,
};

announce_refs(node, repo.id())?;

Ok::<_, Error>(Json(json!({ "success": true, "id": id })))
}

Expand Down Expand Up @@ -722,6 +726,7 @@ async fn patch_create_handler(
Json(patch): Json<PatchCreate>,
) -> impl IntoResponse {
api::auth::validate(&ctx, &token).await?;
let node = Node::new(ctx.profile.socket());
let storage = &ctx.profile.storage;
let signer = ctx
.profile
Expand All @@ -743,11 +748,14 @@ async fn patch_create_handler(
)
.map_err(Error::from)?;

announce_refs(node, repo.id())?;

Ok::<_, Error>((
StatusCode::CREATED,
Json(json!({ "success": true, "id": patch.id.to_string() })),
))
}

/// Update an patch.
/// `PATCH /projects/:project/patches/:id`
async fn patch_update_handler(
Expand All @@ -757,6 +765,7 @@ async fn patch_update_handler(
Json(action): Json<patch::Action>,
) -> impl IntoResponse {
api::auth::validate(&ctx, &token).await?;
let node = Node::new(ctx.profile.socket());
let storage = &ctx.profile.storage;
let signer = ctx
.profile
Expand Down Expand Up @@ -876,6 +885,8 @@ async fn patch_update_handler(
}
};

announce_refs(node, repo.id())?;

Ok::<_, Error>(Json(json!({ "success": true, "id": id })))
}

Expand Down

0 comments on commit 638c2a3

Please sign in to comment.