Add a route to get a single inbox entry

This commit is contained in:
2025-05-08 16:47:32 +02:00
parent df7f395e8b
commit b6af889dc5
6 changed files with 87 additions and 5 deletions

View File

@ -0,0 +1,64 @@
use crate::extractors::auth_extractor::AuthExtractor;
use crate::models::inbox::{InboxEntry, InboxEntryID};
use crate::services::inbox_service;
use actix_web::dev::Payload;
use actix_web::{FromRequest, HttpRequest};
use serde::Deserialize;
#[derive(Deserialize)]
struct InboxEntryIdInPath {
inbox_id: InboxEntryID,
}
#[derive(thiserror::Error, Debug)]
enum InboxEntryExtractorError {
#[error("Current user does not own the entry!")]
UserDoesNotOwnInboxEntry,
}
pub struct InboxEntryInPath(InboxEntry);
impl InboxEntryInPath {
pub async fn load_inbox_entry_from_path(
auth: &AuthExtractor,
id: InboxEntryID,
) -> anyhow::Result<Self> {
let entry = inbox_service::get_by_id(id).await?;
if entry.user_id() != auth.user_id() {
return Err(InboxEntryExtractorError::UserDoesNotOwnInboxEntry.into());
}
Ok(Self(entry))
}
}
impl FromRequest for InboxEntryInPath {
type Error = actix_web::Error;
type Future = futures_util::future::LocalBoxFuture<'static, Result<Self, Self::Error>>;
fn from_request(req: &HttpRequest, _payload: &mut Payload) -> Self::Future {
let req = req.clone();
Box::pin(async move {
let auth = AuthExtractor::extract(&req).await?;
let entry_id =
actix_web::web::Path::<InboxEntryIdInPath>::from_request(&req, &mut Payload::None)
.await?
.inbox_id;
Self::load_inbox_entry_from_path(&auth, entry_id)
.await
.map_err(|e| {
log::error!("Failed to extract inbox entry ID from URL! {}", e);
actix_web::error::ErrorNotFound("Could not fetch inbox entry information!")
})
})
}
}
impl AsRef<InboxEntry> for InboxEntryInPath {
fn as_ref(&self) -> &InboxEntry {
&self.0
}
}

View File

@ -2,5 +2,6 @@ pub mod account_extractor;
pub mod auth_extractor;
pub mod file_extractor;
pub mod file_id_extractor;
pub mod inbox_entry_extractor;
pub mod money_session;
pub mod movement_extractor;