Browse Source

Preemptive anti-database-bloat measures

main
Charlotte Som 10 months ago
parent
commit
baeaa8575b
  1. 9
      phoebe-main/src/main.rs
  2. 31
      phoebe/src/lib.rs
  3. 9
      services/phoebe-discord/src/handler.rs

9
phoebe-main/src/main.rs

@ -37,9 +37,12 @@ async fn handle_events(
)
}
if let Err(e) = link_messages(&mut conn, &message.origin, &resulting_messages).await
{
tracing::error!("Failed to link messages: {e}");
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}");
}
}
}
}

31
phoebe/src/lib.rs

@ -74,24 +74,29 @@ pub async fn link_messages(
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>(
conn: &'a mut SqliteConnection,
dyn_service: DynServiceLookup,
message: &ChatMessageReference,
) -> sqlx::Result<BoxStream<'a, ChatMessageReference>> {
let link_id = {
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 link_id = get_message_link_id(&mut *conn, message).await?;
let stream = sqlx::query("SELECT * FROM messages WHERE link_id = ?")
.bind(link_id)

9
services/phoebe-discord/src/handler.rs

@ -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);

Loading…
Cancel
Save