diff --git a/migrations/20261108095003_add_indexes.sql b/migrations/20261108095003_add_indexes.sql new file mode 100644 index 0000000..23d3132 --- /dev/null +++ b/migrations/20261108095003_add_indexes.sql @@ -0,0 +1,9 @@ +ALTER TABLE sounds + ADD INDEX sounds_name (name), + ADD INDEX sounds_server_id (server_id), + ADD INDEX sounds_uploader_id (uploader_id), + ALGORITHM=INPLACE, LOCK=NONE; + +ALTER TABLE join_sounds + ADD INDEX join_sounds_user_guild (`user`, guild), + ALGORITHM=INPLACE, LOCK=NONE; diff --git a/src/cmds/search.rs b/src/cmds/search.rs index 4cbfc3a..8e51c78 100644 --- a/src/cmds/search.rs +++ b/src/cmds/search.rs @@ -2,7 +2,8 @@ use poise::{ serenity_prelude, serenity_prelude::{ constants::MESSAGE_CODE_LIMIT, ButtonStyle, ComponentInteraction, CreateActionRow, - CreateButton, CreateEmbed, EditInteractionResponse, GuildId, UserId, + CreateButton, CreateEmbed, CreateInteractionResponse, EditInteractionResponse, GuildId, + UserId, }, CreateReply, }; @@ -192,15 +193,25 @@ impl SoundPager { })) } + /// Handle a pager button click. Returns `Ok(false)` if the interaction is not a pager button, + /// so the caller can route it elsewhere. Errors mean it was a pager button that failed. pub async fn handle_interaction( ctx: &serenity_prelude::Context, data: &Data, interaction: &ComponentInteraction, - ) -> Result<(), Error> { + ) -> Result { + let Ok(pager) = serde_json::from_str::(&interaction.data.custom_id) else { + return Ok(false); + }; + let user_id = interaction.user.id; let guild_id = interaction.guild_id.unwrap(); - let pager = serde_json::from_str::(&interaction.data.custom_id)?; + // Acknowledge straight away so the DB queries below can't exceed Discord's 3s deadline + interaction + .create_response(&ctx, CreateInteractionResponse::Acknowledge) + .await?; + let sounds = pager.get_page(data, user_id, guild_id).await?; let count = match pager.context { ListContext::User => data.count_user_sounds(user_id).await?, @@ -211,13 +222,13 @@ impl SoundPager { interaction .edit_response( &ctx, - EditInteractionResponse::default() - .add_embed(pager.embed(&sounds, count)) - .components(vec![pager.create_action_row(count / 25)]), + EditInteractionResponse::new() + .embed(pager.embed(&sounds, count)) + .components(vec![pager.create_action_row(count.saturating_sub(1) / 25)]), ) .await?; - Ok(()) + Ok(true) } async fn reply(&self, ctx: Context<'_>) -> Result<(), Error> { @@ -238,7 +249,7 @@ impl SoundPager { CreateReply::default() .ephemeral(true) .embed(self.embed(&sounds, count)) - .components(vec![self.create_action_row(count / 25)]), + .components(vec![self.create_action_row(count.saturating_sub(1) / 25)]), ) .await?; diff --git a/src/event_handlers.rs b/src/event_handlers.rs index db479a6..7363402 100644 --- a/src/event_handlers.rs +++ b/src/event_handlers.rs @@ -94,8 +94,16 @@ pub async fn listener(ctx: &Context, event: &FullEvent, data: &Data) -> Result<( FullEvent::InteractionCreate { interaction } => match interaction { Interaction::Component(component) => { if let Some(guild_id) = component.guild_id { - if let Ok(()) = SoundPager::handle_interaction(ctx, &data, component).await { - } else { + let handled = match SoundPager::handle_interaction(ctx, &data, component).await + { + Ok(handled) => handled, + Err(e) => { + log::warn!("Failed to handle pager interaction: {:?}", e); + true + } + }; + + if !handled { component.defer(&ctx).await.unwrap(); let mode = component.data.custom_id.as_str(); match mode { diff --git a/src/models/sound.rs b/src/models/sound.rs index d683335..181873a 100644 --- a/src/models/sound.rs +++ b/src/models/sound.rs @@ -253,7 +253,7 @@ impl SoundCtx for Data { LIMIT ?, ?", user_id.into(), page * 25, - (page + 1) * 25 + 25u64 ) .fetch_all(&self.database) .await? @@ -294,7 +294,7 @@ impl SoundCtx for Data { LIMIT ?, ?", user_id.into(), page * 25, - (page + 1) * 25 + 25u64 ) .fetch_all(&self.database) .await? @@ -335,7 +335,7 @@ impl SoundCtx for Data { LIMIT ?, ?", guild_id.into(), page * 25, - (page + 1) * 25 + 25u64 ) .fetch_all(&self.database) .await?