Truncate channel_messages when they've been seen by all users
Diff
src/persistence.rs | 36 +++++++++++++++++++++++++++++++++++-
1 file changed, 35 insertions(+), 1 deletion(-)
@@ -1,6 +1,8 @@
pub mod events;
use actix::{Context, Handler, ResponseFuture};
use std::time::Duration;
use actix::{AsyncContext, Context, Handler, ResponseFuture, WrapFuture};
use itertools::Itertools;
use tracing::instrument;
@@ -18,6 +20,15 @@
impl actix::Actor for Persistence {
type Context = Context<Self>;
fn started(&mut self, ctx: &mut Self::Context) {
ctx.run_interval(Duration::from_secs(300), |this, ctx| {
let database = this.database.clone();
ctx.spawn(truncate_seen_messages(database).into_actor(this));
});
}
}
@@ -197,5 +208,28 @@
res
})
}
}
pub async fn truncate_seen_messages(db: sqlx::Pool<sqlx::Any>) {
let messages = sqlx::query_as::<_, (i64, i64)>(
"SELECT channel, MIN(last_seen_message_idx)
FROM channel_users
GROUP BY channel",
)
.fetch_all(&db)
.await
.unwrap();
for (channel, min_seen_id) in messages {
sqlx::query("DELETE FROM channel_messages WHERE channel = ? AND idx < ?")
.bind(channel)
.bind(min_seen_id)
.execute(&db)
.await
.unwrap();
}
}