Preemptive anti-database-bloat measures
parent
6b07721ffa
commit
baeaa8575b
|
@ -37,9 +37,12 @@ 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) =
|
||||||
tracing::error!("Failed to link messages: {e}");
|
link_messages(&mut conn, &message.origin, &resulting_messages).await
|
||||||
|
{
|
||||||
|
tracing::error!("Failed to link messages: {e}");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -74,24 +74,29 @@ pub async fn link_messages(
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn get_message_link_id(
|
||||||
|
conn: &mut SqliteConnection,
|
||||||
|
message: &ChatMessageReference,
|
||||||
|
) -> sqlx::Result<i64> {
|
||||||
|
let service = &message.channel.service;
|
||||||
|
let channel = &message.channel.id;
|
||||||
|
let message_id = &message.message_id;
|
||||||
|
let query = sqlx::query!(
|
||||||
|
"SELECT link_id FROM messages WHERE service = ? AND channel = ? AND message = ?",
|
||||||
|
service,
|
||||||
|
channel,
|
||||||
|
message_id
|
||||||
|
);
|
||||||
|
let r = query.fetch_one(&mut *conn).await?;
|
||||||
|
Ok(r.link_id)
|
||||||
|
}
|
||||||
|
|
||||||
pub async fn get_linked_messages<'a>(
|
pub async fn get_linked_messages<'a>(
|
||||||
conn: &'a mut SqliteConnection,
|
conn: &'a mut SqliteConnection,
|
||||||
dyn_service: DynServiceLookup,
|
dyn_service: DynServiceLookup,
|
||||||
message: &ChatMessageReference,
|
message: &ChatMessageReference,
|
||||||
) -> sqlx::Result<BoxStream<'a, ChatMessageReference>> {
|
) -> sqlx::Result<BoxStream<'a, ChatMessageReference>> {
|
||||||
let link_id = {
|
let link_id = get_message_link_id(&mut *conn, message).await?;
|
||||||
let service = &message.channel.service;
|
|
||||||
let channel = &message.channel.id;
|
|
||||||
let message_id = &message.message_id;
|
|
||||||
let query = sqlx::query!(
|
|
||||||
"SELECT link_id FROM messages WHERE service = ? AND channel = ? AND message = ?",
|
|
||||||
service,
|
|
||||||
channel,
|
|
||||||
message_id
|
|
||||||
);
|
|
||||||
query.fetch_one(&mut *conn).await
|
|
||||||
}?
|
|
||||||
.link_id;
|
|
||||||
|
|
||||||
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)
|
||||||
|
|
|
@ -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);
|
||||||
|
|
Loading…
Reference in New Issue