diff options
| author | Owen Jacobson <owen@grimoire.ca> | 2025-10-27 18:03:36 -0400 |
|---|---|---|
| committer | Owen Jacobson <owen@grimoire.ca> | 2025-10-28 01:43:09 -0400 |
| commit | c2a3a010c67776b9a459d7ba0930630ff25a3a51 (patch) | |
| tree | 446a7598080f23eec28fab01c7d2d0fc5f30f47d | |
| parent | 38ac83aef9667f1a4fe86e03e53565376081179f (diff) | |
Convert the `Messages` component to a freestanding struct.
| -rw-r--r-- | src/app.rs | 10 | ||||
| -rw-r--r-- | src/conversation/handlers/send/mod.rs | 11 | ||||
| -rw-r--r-- | src/conversation/handlers/send/test.rs | 6 | ||||
| -rw-r--r-- | src/message/app.rs | 10 | ||||
| -rw-r--r-- | src/message/handlers/delete/mod.rs | 10 | ||||
| -rw-r--r-- | src/message/handlers/delete/test.rs | 12 | ||||
| -rw-r--r-- | src/test/fixtures/message.rs | 13 |
7 files changed, 42 insertions, 30 deletions
@@ -54,8 +54,8 @@ impl App { Logins::new(self.db.clone(), self.token_events.clone()) } - pub const fn messages(&self) -> Messages<'_> { - Messages::new(&self.db, &self.events) + pub fn messages(&self) -> Messages { + Messages::new(self.db.clone(), self.events.clone()) } pub const fn setup(&self) -> Setup<'_> { @@ -95,3 +95,9 @@ impl FromRef<App> for Logins { app.logins() } } + +impl FromRef<App> for Messages { + fn from_ref(app: &App) -> Self { + app.messages() + } +} diff --git a/src/conversation/handlers/send/mod.rs b/src/conversation/handlers/send/mod.rs index c8be59c..ff63652 100644 --- a/src/conversation/handlers/send/mod.rs +++ b/src/conversation/handlers/send/mod.rs @@ -5,11 +5,13 @@ use axum::{ }; use crate::{ - app::App, clock::RequestedAt, conversation::handlers::PathInfo, error::{Internal, NotFound}, - message::{Body, Message, app::SendError}, + message::{ + Body, Message, + app::{Messages, SendError}, + }, token::extract::Identity, }; @@ -17,14 +19,13 @@ use crate::{ mod test; pub async fn handler( - State(app): State<App>, + State(messages): State<Messages>, Path(conversation): Path<PathInfo>, RequestedAt(sent_at): RequestedAt, identity: Identity, Json(request): Json<Request>, ) -> Result<Response, Error> { - let message = app - .messages() + let message = messages .send(&conversation, &identity.login, &sent_at, &request.body) .await?; diff --git a/src/conversation/handlers/send/test.rs b/src/conversation/handlers/send/test.rs index 8863090..013aaa4 100644 --- a/src/conversation/handlers/send/test.rs +++ b/src/conversation/handlers/send/test.rs @@ -28,7 +28,7 @@ async fn messages_in_order() { let request = super::Request { body: body.clone() }; let _ = super::handler( - State(app.clone()), + State(app.messages()), Path(conversation.id.clone()), sent_at.clone(), sender.clone(), @@ -75,7 +75,7 @@ async fn nonexistent_conversation() { body: fixtures::message::propose(), }; let super::Error(error) = super::handler( - State(app), + State(app.messages()), Path(conversation.clone()), sent_at, sender, @@ -112,7 +112,7 @@ async fn deleted_conversation() { body: fixtures::message::propose(), }; let super::Error(error) = super::handler( - State(app), + State(app.messages()), Path(conversation.id.clone()), sent_at, sender, diff --git a/src/message/app.rs b/src/message/app.rs index 647152e..cbcbff9 100644 --- a/src/message/app.rs +++ b/src/message/app.rs @@ -13,13 +13,13 @@ use crate::{ user::{self, repo::Provider as _}, }; -pub struct Messages<'a> { - db: &'a SqlitePool, - events: &'a Broadcaster, +pub struct Messages { + db: SqlitePool, + events: Broadcaster, } -impl<'a> Messages<'a> { - pub const fn new(db: &'a SqlitePool, events: &'a Broadcaster) -> Self { +impl Messages { + pub const fn new(db: SqlitePool, events: Broadcaster) -> Self { Self { db, events } } diff --git a/src/message/handlers/delete/mod.rs b/src/message/handlers/delete/mod.rs index 3e9a212..c09a752 100644 --- a/src/message/handlers/delete/mod.rs +++ b/src/message/handlers/delete/mod.rs @@ -5,10 +5,12 @@ use axum::{ }; use crate::{ - app::App, clock::RequestedAt, error::{Internal, NotFound}, - message::{self, app::DeleteError}, + message::{ + self, + app::{DeleteError, Messages}, + }, token::extract::Identity, }; @@ -16,12 +18,12 @@ use crate::{ mod test; pub async fn handler( - State(app): State<App>, + State(messages): State<Messages>, Path(message): Path<message::Id>, RequestedAt(deleted_at): RequestedAt, identity: Identity, ) -> Result<Response, Error> { - app.messages() + messages .delete(&identity.login, &message, &deleted_at) .await?; diff --git a/src/message/handlers/delete/test.rs b/src/message/handlers/delete/test.rs index 05d9344..198728b 100644 --- a/src/message/handlers/delete/test.rs +++ b/src/message/handlers/delete/test.rs @@ -16,7 +16,7 @@ pub async fn delete_message() { // Send the request let response = super::handler( - State(app.clone()), + State(app.messages()), Path(message.id.clone()), fixtures::now(), sender, @@ -52,7 +52,7 @@ pub async fn delete_invalid_message_id() { let deleter = fixtures::identity::create(&app, &fixtures::now()).await; let message = fixtures::message::fictitious(); let super::Error(error) = super::handler( - State(app.clone()), + State(app.messages()), Path(message.clone()), fixtures::now(), deleter, @@ -83,7 +83,7 @@ pub async fn delete_deleted() { // Send the request let super::Error(error) = super::handler( - State(app.clone()), + State(app.messages()), Path(message.id.clone()), fixtures::now(), sender, @@ -114,7 +114,7 @@ pub async fn delete_expired() { // Send the request let super::Error(error) = super::handler( - State(app.clone()), + State(app.messages()), Path(message.id.clone()), fixtures::now(), sender, @@ -150,7 +150,7 @@ pub async fn delete_purged() { let deleter = fixtures::identity::create(&app, &fixtures::now()).await; let super::Error(error) = super::handler( - State(app.clone()), + State(app.messages()), Path(message.id.clone()), fixtures::now(), deleter, @@ -176,7 +176,7 @@ pub async fn delete_not_sender() { let deleter = fixtures::identity::create(&app, &fixtures::now()).await; let super::Error(error) = super::handler( - State(app.clone()), + State(app.messages()), Path(message.id.clone()), fixtures::now(), deleter.clone(), diff --git a/src/test/fixtures/message.rs b/src/test/fixtures/message.rs index 92ac1f5..0bd0b7a 100644 --- a/src/test/fixtures/message.rs +++ b/src/test/fixtures/message.rs @@ -1,22 +1,25 @@ +use axum::extract::FromRef; use faker_rand::lorem::Paragraphs; use crate::{ - app::App, clock::RequestedAt, conversation::Conversation, login::Login, - message::{self, Body, Message}, + message::{self, Body, Message, app::Messages}, }; -pub async fn send( +pub async fn send<App>( app: &App, conversation: &Conversation, sender: &Login, sent_at: &RequestedAt, -) -> Message { +) -> Message +where + Messages: FromRef<App>, +{ let body = propose(); - app.messages() + Messages::from_ref(app) .send(&conversation.id, sender, sent_at, &body) .await .expect("should succeed if the conversation exists") |
