summaryrefslogtreecommitdiff
path: root/src/channel
diff options
context:
space:
mode:
Diffstat (limited to 'src/channel')
-rw-r--r--src/channel/app.rs2
-rw-r--r--src/channel/event.rs32
-rw-r--r--src/channel/history.rs18
-rw-r--r--src/channel/routes.rs16
-rw-r--r--src/channel/routes/test/on_create.rs10
-rw-r--r--src/channel/routes/test/on_send.rs24
-rw-r--r--src/channel/snapshot.rs8
7 files changed, 55 insertions, 55 deletions
diff --git a/src/channel/app.rs b/src/channel/app.rs
index a9a9e84..cb7ad32 100644
--- a/src/channel/app.rs
+++ b/src/channel/app.rs
@@ -6,7 +6,7 @@ use super::{repo::Provider as _, Channel, Id};
use crate::{
clock::DateTime,
db::NotFound,
- event::{broadcaster::Broadcaster, repo::Provider as _, Event, Sequence},
+ event::{repo::Provider as _, Broadcaster, Event, Sequence},
message::repo::Provider as _,
};
diff --git a/src/channel/event.rs b/src/channel/event.rs
index 9c54174..f3dca3e 100644
--- a/src/channel/event.rs
+++ b/src/channel/event.rs
@@ -5,32 +5,30 @@ use crate::{
};
#[derive(Clone, Debug, Eq, PartialEq, serde::Serialize)]
-pub struct Event {
- #[serde(flatten)]
- pub instant: Instant,
- #[serde(flatten)]
- pub kind: Kind,
+#[serde(tag = "event", rename_all = "snake_case")]
+pub enum Event {
+ Created(Created),
+ Deleted(Deleted),
}
impl Sequenced for Event {
fn instant(&self) -> Instant {
- self.instant
+ match self {
+ Self::Created(event) => event.instant,
+ Self::Deleted(event) => event.instant,
+ }
}
}
#[derive(Clone, Debug, Eq, PartialEq, serde::Serialize)]
-#[serde(tag = "type", rename_all = "snake_case")]
-pub enum Kind {
- Created(Created),
- Deleted(Deleted),
-}
-
-#[derive(Clone, Debug, Eq, PartialEq, serde::Serialize)]
pub struct Created {
+ #[serde(flatten)]
+ pub instant: Instant,
+ #[serde(flatten)]
pub channel: Channel,
}
-impl From<Created> for Kind {
+impl From<Created> for Event {
fn from(event: Created) -> Self {
Self::Created(event)
}
@@ -38,10 +36,12 @@ impl From<Created> for Kind {
#[derive(Clone, Debug, Eq, PartialEq, serde::Serialize)]
pub struct Deleted {
- pub channel: channel::Id,
+ #[serde(flatten)]
+ pub instant: Instant,
+ pub id: channel::Id,
}
-impl From<Deleted> for Kind {
+impl From<Deleted> for Event {
fn from(event: Deleted) -> Self {
Self::Deleted(event)
}
diff --git a/src/channel/history.rs b/src/channel/history.rs
index 383fb7b..78b3437 100644
--- a/src/channel/history.rs
+++ b/src/channel/history.rs
@@ -40,22 +40,20 @@ impl History {
}
fn created(&self) -> Event {
- Event {
+ Created {
instant: self.created,
- kind: Created {
- channel: self.channel.clone(),
- }
- .into(),
+ channel: self.channel.clone(),
}
+ .into()
}
fn deleted(&self) -> Option<Event> {
- self.deleted.map(|instant| Event {
- instant,
- kind: Deleted {
- channel: self.channel.id.clone(),
+ self.deleted.map(|instant| {
+ Deleted {
+ instant,
+ id: self.channel.id.clone(),
}
- .into(),
+ .into()
})
}
}
diff --git a/src/channel/routes.rs b/src/channel/routes.rs
index 5d67af8..e97c447 100644
--- a/src/channel/routes.rs
+++ b/src/channel/routes.rs
@@ -7,7 +7,13 @@ use axum::{
};
use super::{app, Channel, Id};
-use crate::{app::App, clock::RequestedAt, error::Internal, login::Login, message::app::SendError};
+use crate::{
+ app::App,
+ clock::RequestedAt,
+ error::{Internal, NotFound},
+ login::Login,
+ message::app::SendError,
+};
#[cfg(test)]
mod test;
@@ -56,7 +62,7 @@ impl IntoResponse for CreateError {
#[derive(Clone, serde::Deserialize)]
struct SendRequest {
- message: String,
+ body: String,
}
async fn on_send(
@@ -67,7 +73,7 @@ async fn on_send(
Json(request): Json<SendRequest>,
) -> Result<StatusCode, SendErrorResponse> {
app.messages()
- .send(&channel, &login, &sent_at, &request.message)
+ .send(&channel, &login, &sent_at, &request.body)
.await?;
Ok(StatusCode::ACCEPTED)
@@ -81,9 +87,7 @@ impl IntoResponse for SendErrorResponse {
fn into_response(self) -> Response {
let Self(error) = self;
match error {
- not_found @ SendError::ChannelNotFound(_) => {
- (StatusCode::NOT_FOUND, not_found.to_string()).into_response()
- }
+ not_found @ SendError::ChannelNotFound(_) => NotFound(not_found).into_response(),
other => Internal::from(other).into_response(),
}
}
diff --git a/src/channel/routes/test/on_create.rs b/src/channel/routes/test/on_create.rs
index ed49017..eeecc7f 100644
--- a/src/channel/routes/test/on_create.rs
+++ b/src/channel/routes/test/on_create.rs
@@ -2,7 +2,7 @@ use axum::extract::{Json, State};
use futures::stream::StreamExt as _;
use crate::{
- channel::{app, routes},
+ channel::{self, app, routes},
event,
test::fixtures::{self, future::Immediately as _},
};
@@ -12,7 +12,7 @@ async fn new_channel() {
// Set up the environment
let app = fixtures::scratch_app().await;
- let creator = fixtures::login::create(&app).await;
+ let creator = fixtures::login::create(&app, &fixtures::now()).await;
// Call the endpoint
@@ -53,8 +53,8 @@ async fn new_channel() {
.expect("creation event published");
assert!(matches!(
- event.kind,
- event::Kind::ChannelCreated(event)
+ event,
+ event::Event::Channel(channel::Event::Created(event))
if event.channel == response_channel
));
}
@@ -64,7 +64,7 @@ async fn duplicate_name() {
// Set up the environment
let app = fixtures::scratch_app().await;
- let creator = fixtures::login::create(&app).await;
+ let creator = fixtures::login::create(&app, &fixtures::now()).await;
let channel = fixtures::channel::create(&app, &fixtures::now()).await;
// Call the endpoint
diff --git a/src/channel/routes/test/on_send.rs b/src/channel/routes/test/on_send.rs
index 3297093..293cc56 100644
--- a/src/channel/routes/test/on_send.rs
+++ b/src/channel/routes/test/on_send.rs
@@ -4,8 +4,8 @@ use futures::stream::StreamExt;
use crate::{
channel,
channel::routes,
- event,
- message::app::SendError,
+ event::{self, Sequenced},
+ message::{self, app::SendError},
test::fixtures::{self, future::Immediately as _},
};
@@ -14,7 +14,7 @@ async fn messages_in_order() {
// Set up the environment
let app = fixtures::scratch_app().await;
- let sender = fixtures::login::create(&app).await;
+ let sender = fixtures::login::create(&app, &fixtures::now()).await;
let channel = fixtures::channel::create(&app, &fixtures::now()).await;
// Call the endpoint (twice)
@@ -24,10 +24,8 @@ async fn messages_in_order() {
(fixtures::now(), fixtures::message::propose()),
];
- for (sent_at, message) in &requests {
- let request = routes::SendRequest {
- message: message.clone(),
- };
+ for (sent_at, body) in &requests {
+ let request = routes::SendRequest { body: body.clone() };
routes::on_send(
State(app.clone()),
@@ -53,11 +51,11 @@ async fn messages_in_order() {
let events = events.collect::<Vec<_>>().immediately().await;
for ((sent_at, message), event) in requests.into_iter().zip(events) {
- assert_eq!(*sent_at, event.instant.at);
+ assert_eq!(*sent_at, event.at());
assert!(matches!(
- event.kind,
- event::Kind::MessageSent(event)
- if event.message.sender == sender
+ event,
+ event::Event::Message(message::Event::Sent(event))
+ if event.message.sender == sender.id
&& event.message.body == message
));
}
@@ -68,14 +66,14 @@ async fn nonexistent_channel() {
// Set up the environment
let app = fixtures::scratch_app().await;
- let login = fixtures::login::create(&app).await;
+ let login = fixtures::login::create(&app, &fixtures::now()).await;
// Call the endpoint
let sent_at = fixtures::now();
let channel = channel::Id::generate();
let request = routes::SendRequest {
- message: fixtures::message::propose(),
+ body: fixtures::message::propose(),
};
let routes::SendErrorResponse(error) = routes::on_send(
State(app),
diff --git a/src/channel/snapshot.rs b/src/channel/snapshot.rs
index 6462f25..d4d1d27 100644
--- a/src/channel/snapshot.rs
+++ b/src/channel/snapshot.rs
@@ -1,5 +1,5 @@
use super::{
- event::{Created, Event, Kind},
+ event::{Created, Event},
Id,
};
@@ -11,9 +11,9 @@ pub struct Channel {
impl Channel {
fn apply(state: Option<Self>, event: Event) -> Option<Self> {
- match (state, event.kind) {
- (None, Kind::Created(event)) => Some(event.into()),
- (Some(channel), Kind::Deleted(event)) if channel.id == event.channel => None,
+ match (state, event) {
+ (None, Event::Created(event)) => Some(event.into()),
+ (Some(channel), Event::Deleted(event)) if channel.id == event.id => None,
(state, event) => panic!("invalid channel event {event:#?} for state {state:#?}"),
}
}