diff options
Diffstat (limited to 'src/api/events.rs')
| -rw-r--r-- | src/api/events.rs | 55 |
1 files changed, 33 insertions, 22 deletions
diff --git a/src/api/events.rs b/src/api/events.rs index 65c6238..8eb243d 100644 --- a/src/api/events.rs +++ b/src/api/events.rs @@ -16,9 +16,6 @@ use serde_json::{Value as JsonValue, json}; use super::auth::AuthenticatedUser; use super::{ApiError, error_response, store_status}; -use crate::document::events; -use crate::document::typed::{TimestampFormat, Val}; -use crate::document::{ScalarValue, store}; use crate::federation::fold::Federation; use crate::federation::jcs::Json as Jcs; use crate::federation::store::Id; @@ -33,6 +30,7 @@ pub(super) fn router() -> Router<AppState> { .route("/events/{id}", get(event)) .route("/admin/events/drop", post(drop_taken)) .route("/admin/events/allow", post(allow)) + .route("/admin/events/vote", post(vote)) } /// The answer while the node holds no store. @@ -221,29 +219,42 @@ async fn put( Some(Offered::Held) => "held", Some(Offered::Waiting) => "waiting", }; - let written = state - .mutate_doc_for( - Some(user), - crate::document::signatures::Sign::Protocol, - move |doc| { - if events::bytes(doc, &id).is_none() { - store::write_val_path( - doc, - &events::event(&id), - &Val::Scalar(ScalarValue::Bytes(bytes)), - store::Writing::sticky(TimestampFormat::default()), - )?; - } - Ok::<(), store::StoreError>(()) - }, - ) - .await; - match written { - Ok(_) => Json(json!({"id": id.to_string(), "event": became})).into_response(), + match crate::federation::held::write(&state, Some(user), bytes).await { + Ok(()) => Json(json!({"id": id.to_string(), "event": became})).into_response(), Err(error) => store_status(error, "Failed to put an event").into_response(), } } +/// A vote by hand. +#[derive(Deserialize)] +struct ByHand { + proposal: String, + yes: bool, +} + +/// `POST /admin/events/vote`: a vote by hand through this node, which votes for its party +/// by itself and keeps its party's one record: `yes` on the proposal of that hash on the +/// current version, by the record, and with a `true` by the voters' rule; signed with the +/// party's key the node keeps, recorded, and put. +async fn vote( + State(state): State<AppState>, + Extension(AuthenticatedUser(user)): Extension<AuthenticatedUser>, + Json(by_hand): Json<ByHand>, +) -> Response { + let Some(proposal) = Id::from_hex(&by_hand.proposal) else { + return error_response(StatusCode::BAD_REQUEST, "not a proposal's hash", None); + }; + let cast = match state.holding.vote_by_hand(proposal, by_hand.yes) { + Ok(cast) => cast, + Err(why) => return error_response(StatusCode::UNPROCESSABLE_ENTITY, why, None), + }; + let id = Id::of(&cast); + match crate::federation::held::write(&state, Some(user), cast).await { + Ok(()) => Json(json!({"id": id.to_string()})).into_response(), + Err(error) => store_status(error, "Failed to put a vote").into_response(), + } +} + /// The events an operator names. #[derive(Deserialize)] struct Named { |
