diff --git a/blahd/src/lib.rs b/blahd/src/lib.rs index abd43ed..e85ee86 100644 --- a/blahd/src/lib.rs +++ b/blahd/src/lib.rs @@ -23,7 +23,7 @@ use blah_types::{get_timestamp, Id, PubKey, Signed, UserKey}; use data_encoding::BASE64_NOPAD; use database::{Transaction, TransactionOps}; use id::IdExt; -use middleware::{Auth, ETag, MaybeAuth, ResultExt as _, SignedJson}; +use middleware::{Auth, ETag, MaybeAuth, NoContent, ResultExt as _, SignedJson}; use parking_lot::Mutex; use serde::de::DeserializeOwned; use serde::{Deserialize, Deserializer, Serialize}; @@ -218,7 +218,7 @@ async fn get_user(State(st): ArcState, auth: MaybeAuth) -> Response { })(); match ret { - Ok(_) => StatusCode::NO_CONTENT.into_response(), + Ok(_) => NoContent.into_response(), Err(err) => { let (status, raw_err) = err.to_raw(); if status != StatusCode::NOT_FOUND { @@ -475,7 +475,7 @@ async fn post_room_admin( st: ArcState, R(Path(rid), _): RE>, SignedJson(op): SignedJson, -) -> Result { +) -> Result { // TODO: This is a temporary endpoint so just reserialize them. fn transcode(v: &T) -> SignedJson { let v = serde_json::to_value(v).expect("serialization cannot fail"); @@ -501,7 +501,7 @@ async fn post_room_member( st: ArcState, R(Path(rid), _): RE>, SignedJson(op): SignedJson, -) -> Result { +) -> Result { api_ensure!(rid == op.signee.payload.room, "room id mismatch with URI"); api_ensure!( !rid.is_peer_chat(), @@ -523,7 +523,7 @@ async fn post_room_member( // Sanity check. assert!(!attrs.contains(RoomAttrs::PEER_CHAT)); txn.add_room_member(rid, uid, member.permission)?; - Ok(StatusCode::NO_CONTENT) + Ok(NoContent) }) } @@ -531,7 +531,7 @@ async fn delete_room_member( st: ArcState, R(Path((rid, id_key)), _): RE>, SignedJson(op): SignedJson, -) -> Result { +) -> Result { api_ensure!(rid == op.signee.payload.room, "room id mismatch with URI"); api_ensure!( !rid.is_peer_chat(), @@ -550,7 +550,7 @@ async fn delete_room_member( // Sanity check. assert!(!attrs.contains(RoomAttrs::PEER_CHAT)); txn.remove_room_member(rid, uid)?; - Ok(StatusCode::NO_CONTENT) + Ok(NoContent) }) } @@ -558,7 +558,7 @@ async fn delete_room( st: ArcState, R(Path(rid), _): RE>, SignedJson(op): SignedJson, -) -> Result { +) -> Result { api_ensure!(rid == op.signee.payload.room, "room id mismatch with URI"); st.db.with_write(|txn| { // TODO: Should we only shadow delete here? @@ -568,7 +568,7 @@ async fn delete_room( ApiError::PermissionDenied("the user does not have permission to delete the room") ); txn.delete_room(rid)?; - Ok(StatusCode::NO_CONTENT) + Ok(NoContent) }) } @@ -576,7 +576,7 @@ async fn post_room_msg_seen( st: ArcState, R(Path((rid, cid)), _): RE>, Auth(user): Auth, -) -> Result { +) -> Result { st.db.with_write(|txn| { let (uid, _perm, prev_seen_cid) = txn.get_room_member(rid, &user)?; if cid < prev_seen_cid.0 { @@ -584,7 +584,7 @@ async fn post_room_msg_seen( } txn.mark_room_msg_seen(rid, uid, Id(cid as _)) })?; - Ok(StatusCode::NO_CONTENT) + Ok(NoContent) } async fn list_room_member( diff --git a/blahd/src/middleware.rs b/blahd/src/middleware.rs index 4f28d1b..3a6684a 100644 --- a/blahd/src/middleware.rs +++ b/blahd/src/middleware.rs @@ -280,3 +280,13 @@ impl IntoResponseParts for ETag { Ok(res) } } + +// WAIT: https://github.com/tokio-rs/axum/pull/2978 +#[derive(Debug, Clone, Copy)] +pub struct NoContent; + +impl IntoResponse for NoContent { + fn into_response(self) -> Response { + StatusCode::NO_CONTENT.into_response() + } +} diff --git a/blahd/src/register.rs b/blahd/src/register.rs index 83284af..6429e70 100644 --- a/blahd/src/register.rs +++ b/blahd/src/register.rs @@ -2,7 +2,6 @@ use std::num::NonZero; use std::time::Duration; use anyhow::{anyhow, ensure}; -use axum::http::StatusCode; use blah_types::get_timestamp; use blah_types::identity::{IdUrl, UserIdentityDesc}; use blah_types::msg::{UserRegisterChallengeResponse, UserRegisterPayload}; @@ -15,7 +14,7 @@ use serde::Deserialize; use sha2::{Digest, Sha256}; use crate::database::TransactionOps; -use crate::middleware::SignedJson; +use crate::middleware::{NoContent, SignedJson}; use crate::utils::Instant; use crate::{ApiError, ArcState, SERVER_AND_VERSION}; @@ -163,7 +162,7 @@ impl State { pub async fn post_user( axum::extract::State(st): ArcState, SignedJson(msg): SignedJson, -) -> Result { +) -> Result { if !st.config.register.enable_public { return Err(ApiError::Disabled("public registration is disabled")); } @@ -252,7 +251,7 @@ pub async fn post_user( st.db .with_write(|txn| txn.create_user(&id_desc, &id_desc_json, fetch_time))?; - Ok(StatusCode::NO_CONTENT) + Ok(NoContent) } #[cfg(test)]