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,13 +37,16 @@ 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}");
}
}
}
}
}
}
#[tokio::main]

View File

@ -74,12 +74,10 @@ pub async fn link_messages(
Ok(())
}
pub async fn get_linked_messages<'a>(
conn: &'a mut SqliteConnection,
dyn_service: DynServiceLookup,
pub async fn get_message_link_id(
conn: &mut SqliteConnection,
message: &ChatMessageReference,
) -> sqlx::Result<BoxStream<'a, ChatMessageReference>> {
let link_id = {
) -> sqlx::Result<i64> {
let service = &message.channel.service;
let channel = &message.channel.id;
let message_id = &message.message_id;
@ -89,9 +87,16 @@ pub async fn get_linked_messages<'a>(
channel,
message_id
);
query.fetch_one(&mut *conn).await
}?
.link_id;
let r = query.fetch_one(&mut *conn).await?;
Ok(r.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 = ?")
.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::{
client::{Context, EventHandler},
model::prelude::*,
@ -50,6 +50,13 @@ impl EventHandler for DiscordHandler {
async fn message(&self, ctx: Context, message: Message) {
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 content = discord_message_format::parse(&message.content);