summaryrefslogtreecommitdiff
path: root/src/channel
diff options
context:
space:
mode:
authorOwen Jacobson <owen@grimoire.ca>2024-10-05 22:42:43 -0400
committerOwen Jacobson <owen@grimoire.ca>2024-10-05 22:47:12 -0400
commit6a10fcaf64938da52b326ea80013d9f30ed62a6c (patch)
tree08a3860b68391514390f42872ccc1cb4c6e6afd2 /src/channel
parent1fb26ad31d385ddc628e1b73d6a8764981ca6885 (diff)
Separate `/api/boot` into its own module.
Diffstat (limited to 'src/channel')
-rw-r--r--src/channel/app.rs48
-rw-r--r--src/channel/history.rs8
-rw-r--r--src/channel/repo.rs4
-rw-r--r--src/channel/routes/test/on_create.rs7
4 files changed, 15 insertions, 52 deletions
diff --git a/src/channel/app.rs b/src/channel/app.rs
index 1b2cc48..a9a9e84 100644
--- a/src/channel/app.rs
+++ b/src/channel/app.rs
@@ -7,7 +7,7 @@ use crate::{
clock::DateTime,
db::NotFound,
event::{broadcaster::Broadcaster, repo::Provider as _, Event, Sequence},
- message::{repo::Provider as _, Message},
+ message::repo::Provider as _,
};
pub struct Channels<'a> {
@@ -36,52 +36,6 @@ impl<'a> Channels<'a> {
Ok(channel.as_created())
}
- pub async fn all(&self, resume_point: Option<Sequence>) -> Result<Vec<Channel>, InternalError> {
- let mut tx = self.db.begin().await?;
- let channels = tx.channels().all(resume_point).await?;
- tx.commit().await?;
-
- let channels = channels
- .into_iter()
- .filter_map(|channel| {
- channel
- .events()
- .filter(Sequence::up_to(resume_point))
- .collect()
- })
- .collect();
-
- Ok(channels)
- }
-
- pub async fn messages(
- &self,
- channel: &Id,
- resume_point: Option<Sequence>,
- ) -> Result<Vec<Message>, Error> {
- let mut tx = self.db.begin().await?;
- let channel = tx
- .channels()
- .by_id(channel)
- .await
- .not_found(|| Error::NotFound(channel.clone()))?;
-
- let messages = tx
- .messages()
- .in_channel(&channel, resume_point)
- .await?
- .into_iter()
- .filter_map(|message| {
- message
- .events()
- .filter(Sequence::up_to(resume_point))
- .collect()
- })
- .collect();
-
- Ok(messages)
- }
-
pub async fn delete(&self, channel: &Id, deleted_at: &DateTime) -> Result<(), Error> {
let mut tx = self.db.begin().await?;
diff --git a/src/channel/history.rs b/src/channel/history.rs
index bd45d8d..383fb7b 100644
--- a/src/channel/history.rs
+++ b/src/channel/history.rs
@@ -2,7 +2,7 @@ use super::{
event::{Created, Deleted, Event},
Channel, Id,
};
-use crate::event::Instant;
+use crate::event::{Instant, ResumePoint, Sequence};
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct History {
@@ -25,6 +25,12 @@ impl History {
pub fn as_created(&self) -> Channel {
self.channel.clone()
}
+
+ pub fn as_of(&self, resume_point: impl Into<ResumePoint>) -> Option<Channel> {
+ self.events()
+ .filter(Sequence::up_to(resume_point.into()))
+ .collect()
+ }
}
// Event factories
diff --git a/src/channel/repo.rs b/src/channel/repo.rs
index 2b48436..2f57581 100644
--- a/src/channel/repo.rs
+++ b/src/channel/repo.rs
@@ -3,7 +3,7 @@ use sqlx::{sqlite::Sqlite, SqliteConnection, Transaction};
use crate::{
channel::{Channel, History, Id},
clock::DateTime,
- event::{Instant, Sequence},
+ event::{Instant, ResumePoint, Sequence},
};
pub trait Provider {
@@ -84,7 +84,7 @@ impl<'c> Channels<'c> {
Ok(channel)
}
- pub async fn all(&mut self, resume_at: Option<Sequence>) -> Result<Vec<History>, sqlx::Error> {
+ pub async fn all(&mut self, resume_at: ResumePoint) -> Result<Vec<History>, sqlx::Error> {
let channels = sqlx::query!(
r#"
select
diff --git a/src/channel/routes/test/on_create.rs b/src/channel/routes/test/on_create.rs
index 5733c9e..ed49017 100644
--- a/src/channel/routes/test/on_create.rs
+++ b/src/channel/routes/test/on_create.rs
@@ -33,8 +33,11 @@ async fn new_channel() {
// Verify the semantics
- let channels = app.channels().all(None).await.expect("always succeeds");
- assert!(channels.contains(&response_channel));
+ let snapshot = app.boot().snapshot().await.expect("boot always succeeds");
+ assert!(snapshot
+ .channels
+ .iter()
+ .any(|channel| channel.name == response_channel.name && channel.id == response_channel.id));
let mut events = app
.events()