From 18387cb68094b167a30371b62beda93627c2c467 Mon Sep 17 00:00:00 2001 From: tv Date: Mon, 16 Nov 2026 22:25:47 +0000 Subject: api: a vote by hand, through the node that votes for its party A party that has a node vote by itself votes by hand only through that node, which keeps its one record (`docs/federation.md`, Votes a node casts by itself): `POST /admin/events/vote`, `{"proposal": , "yes": }`, by the record and, with a `true`, by the voters' rule; signed with the party's key the node keeps, recorded, and put. Refused (`422`) where the record or the rule forbids it, or the node votes for no party. Putting an event through the API puts it into the document as the node's own votes are put (`held::write`). --- src/api/events.rs | 55 +++++++++++++++++++++++++++++++++---------------------- 1 file changed, 33 insertions(+), 22 deletions(-) (limited to 'src/api/events.rs') 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 { .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, + Extension(AuthenticatedUser(user)): Extension, + Json(by_hand): Json, +) -> 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 { -- cgit v1.3.1