summaryrefslogtreecommitdiff
path: root/src/channel/routes.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/channel/routes.rs')
-rw-r--r--src/channel/routes.rs76
1 files changed, 64 insertions, 12 deletions
diff --git a/src/channel/routes.rs b/src/channel/routes.rs
index bce634e..23c0602 100644
--- a/src/channel/routes.rs
+++ b/src/channel/routes.rs
@@ -7,13 +7,14 @@ use axum::{
};
use axum_extra::extract::Query;
-use super::{
- app::{self, DeleteError},
- Channel, Id,
-};
+use super::{app, Channel, Id};
use crate::{
- app::App, clock::RequestedAt, error::Internal, event::Sequence, login::Login,
- message::app::SendError,
+ app::App,
+ clock::RequestedAt,
+ error::Internal,
+ event::{Instant, Sequence},
+ login::Login,
+ message::{self, app::SendError},
};
#[cfg(test)]
@@ -25,17 +26,18 @@ pub fn router() -> Router<App> {
.route("/api/channels", post(on_create))
.route("/api/channels/:channel", post(on_send))
.route("/api/channels/:channel", delete(on_delete))
+ .route("/api/channels/:channel/messages", get(messages))
}
#[derive(Default, serde::Deserialize)]
-struct ListQuery {
+struct ResumeQuery {
resume_point: Option<Sequence>,
}
async fn list(
State(app): State<App>,
_: Login,
- Query(query): Query<ListQuery>,
+ Query(query): Query<ResumeQuery>,
) -> Result<Channels, Internal> {
let channels = app.channels().all(query.resume_point).await?;
let response = Channels(channels);
@@ -127,7 +129,7 @@ async fn on_delete(
Path(channel): Path<Id>,
RequestedAt(deleted_at): RequestedAt,
_: Login,
-) -> Result<StatusCode, DeleteErrorResponse> {
+) -> Result<StatusCode, ErrorResponse> {
app.channels().delete(&channel, &deleted_at).await?;
Ok(StatusCode::ACCEPTED)
@@ -135,16 +137,66 @@ async fn on_delete(
#[derive(Debug, thiserror::Error)]
#[error(transparent)]
-struct DeleteErrorResponse(#[from] DeleteError);
+struct ErrorResponse(#[from] app::Error);
-impl IntoResponse for DeleteErrorResponse {
+impl IntoResponse for ErrorResponse {
fn into_response(self) -> Response {
let Self(error) = self;
match error {
- not_found @ DeleteError::NotFound(_) => {
+ not_found @ app::Error::NotFound(_) => {
(StatusCode::NOT_FOUND, not_found.to_string()).into_response()
}
other => Internal::from(other).into_response(),
}
}
}
+
+async fn messages(
+ State(app): State<App>,
+ Path(channel): Path<Id>,
+ _: Login,
+ Query(query): Query<ResumeQuery>,
+) -> Result<Messages, ErrorResponse> {
+ let messages = app
+ .channels()
+ .messages(&channel, query.resume_point)
+ .await?;
+ let response = Messages(
+ messages
+ .into_iter()
+ .map(|message| MessageView {
+ sent: message.sent,
+ sender: message.sender,
+ message: MessageInner {
+ id: message.id,
+ body: message.body,
+ },
+ })
+ .collect(),
+ );
+
+ Ok(response)
+}
+
+struct Messages(Vec<MessageView>);
+
+#[derive(serde::Serialize)]
+struct MessageView {
+ #[serde(flatten)]
+ sent: Instant,
+ sender: Login,
+ message: MessageInner,
+}
+
+#[derive(serde::Serialize)]
+struct MessageInner {
+ id: message::Id,
+ body: String,
+}
+
+impl IntoResponse for Messages {
+ fn into_response(self) -> Response {
+ let Self(messages) = self;
+ Json(messages).into_response()
+ }
+}