summaryrefslogtreecommitdiff
path: root/src/message/app.rs
diff options
context:
space:
mode:
authorOwen Jacobson <owen@grimoire.ca>2025-07-01 01:42:38 -0400
committerOwen Jacobson <owen@grimoire.ca>2025-07-03 21:47:41 -0400
commitb4db819ef8daa583a165aed01eb3d70d98e37fc8 (patch)
tree81f18139d11f6f197f90958a7a28a83aab6c14cf /src/message/app.rs
parentb3ce81945621e9026e687b590e7aa541008575ac (diff)
Prevent sending messages to deleted channels.
I've opted to make it clear in the error message which scenario - deleted vs. non-existant - a channel falls into. This isn't particularly consistent with the rest of the API, so we might need to review this decision later, but it's at least relatively harmless if it's mistaken. (Formally, they're both 404s, so clients that go by error code won't notice.)
Diffstat (limited to 'src/message/app.rs')
-rw-r--r--src/message/app.rs16
1 files changed, 11 insertions, 5 deletions
diff --git a/src/message/app.rs b/src/message/app.rs
index 3c74628..9792c8f 100644
--- a/src/message/app.rs
+++ b/src/message/app.rs
@@ -29,13 +29,17 @@ impl<'a> Messages<'a> {
sent_at: &DateTime,
body: &Body,
) -> Result<Message, SendError> {
+ let to_not_found = || SendError::ChannelNotFound(channel.clone());
+ let to_deleted = || SendError::ChannelDeleted(channel.clone());
+
let mut tx = self.db.begin().await?;
- let channel = tx
- .channels()
- .by_id(channel)
- .await
- .not_found(|| SendError::ChannelNotFound(channel.clone()))?;
+ let channel = tx.channels().by_id(channel).await.not_found(to_not_found)?;
+
+ // Ordering: don't bother allocating a sequence number before we know the channel might
+ // exist.
let sent = tx.sequence().next(sent_at).await?;
+ let channel = channel.as_of(sent).ok_or_else(to_deleted)?;
+
let message = tx.messages().create(&channel, sender, &sent, body).await?;
tx.commit().await?;
@@ -126,6 +130,8 @@ impl<'a> Messages<'a> {
pub enum SendError {
#[error("channel {0} not found")]
ChannelNotFound(channel::Id),
+ #[error("channel {0} deleted")]
+ ChannelDeleted(channel::Id),
#[error(transparent)]
Database(#[from] sqlx::Error),
#[error(transparent)]