Preemptive anti-database-bloat measures

main
Charlotte Som 2022-04-16 11:04:38 +01:00
parent 6b07721ffa
commit baeaa8575b
3 changed files with 32 additions and 17 deletions

View File

@ -37,7 +37,9 @@ async fn handle_events(
) )
} }
if let Err(e) = link_messages(&mut conn, &message.origin, &resulting_messages).await if !resulting_messages.is_empty() {
if let Err(e) =
link_messages(&mut conn, &message.origin, &resulting_messages).await
{ {
tracing::error!("Failed to link messages: {e}"); tracing::error!("Failed to link messages: {e}");
} }
@ -45,6 +47,7 @@ async fn handle_events(
} }
} }
} }
}
#[tokio::main] #[tokio::main]
async fn main() -> Result<()> { async fn main() -> Result<()> {

View File

@ -74,12 +74,10 @@ pub async fn link_messages(
Ok(()) Ok(())
} }
pub async fn get_linked_messages<'a>( pub async fn get_message_link_id(
conn: &'a mut SqliteConnection, conn: &mut SqliteConnection,
dyn_service: DynServiceLookup,
message: &ChatMessageReference, message: &ChatMessageReference,
) -> sqlx::Result<BoxStream<'a, ChatMessageReference>> { ) -> sqlx::Result<i64> {
let link_id = {
let service = &message.channel.service; let service = &message.channel.service;
let channel = &message.channel.id; let channel = &message.channel.id;
let message_id = &message.message_id; let message_id = &message.message_id;
@ -89,9 +87,16 @@ pub async fn get_linked_messages<'a>(
channel, channel,
message_id message_id
); );
query.fetch_one(&mut *conn).await let r = query.fetch_one(&mut *conn).await?;
}? Ok(r.link_id)
.link_id; }
pub async fn get_linked_messages<'a>(
conn: &'a mut SqliteConnection,
dyn_service: DynServiceLookup,
message: &ChatMessageReference,
) -> sqlx::Result<BoxStream<'a, ChatMessageReference>> {
let link_id = get_message_link_id(&mut *conn, message).await?;
let stream = sqlx::query("SELECT * FROM messages WHERE link_id = ?") let stream = sqlx::query("SELECT * FROM messages WHERE link_id = ?")
.bind(link_id) .bind(link_id)

View File

@ -1,4 +1,4 @@
use phoebe::{mid_chat::*, prelude::*}; use phoebe::{get_message_link_id, mid_chat::*, prelude::*};
use serenity::{ use serenity::{
client::{Context, EventHandler}, client::{Context, EventHandler},
model::prelude::*, model::prelude::*,
@ -50,6 +50,13 @@ impl EventHandler for DiscordHandler {
async fn message(&self, ctx: Context, message: Message) { async fn message(&self, ctx: Context, message: Message) {
let origin = ChatMessageReference::new(discord_reference(message.channel_id), message.id); let origin = ChatMessageReference::new(discord_reference(message.channel_id), message.id);
// skip messages linked to ones we have already seen
if let Ok(mut conn) = self.core_db.acquire().await {
if get_message_link_id(&mut conn, &origin).await.is_ok() {
return;
}
}
let author = self.get_author(&ctx, &message).await; let author = self.get_author(&ctx, &message).await;
let content = discord_message_format::parse(&message.content); let content = discord_message_format::parse(&message.content);