From 70e2a05f734243dec76ee315d5348c3e6b6d59bf Mon Sep 17 00:00:00 2001 From: CircuitSacul Date: Mon, 23 Jan 2023 16:47:46 -0500 Subject: [PATCH] scoped overrides --- src/core/starboard/config.rs | 21 +++++++++++++++++-- src/core/starboard/link_events.rs | 2 +- .../chat/overrides/edit/requirements.rs | 2 +- .../commands/chat/overrides/view.rs | 2 +- src/interactions/commands/chat/random.rs | 2 +- .../commands/chat/starboard/view.rs | 2 +- src/interactions/commands/chat/utils/info.rs | 2 +- 7 files changed, 25 insertions(+), 8 deletions(-) diff --git a/src/core/starboard/config.rs b/src/core/starboard/config.rs index 7fb37539..7a128e0c 100644 --- a/src/core/starboard/config.rs +++ b/src/core/starboard/config.rs @@ -1,3 +1,5 @@ +use std::cmp::Ordering; + use twilight_model::id::{ marker::{ChannelMarker, GuildMarker}, Id, @@ -36,8 +38,23 @@ macro_rules! update_from_override { impl StarboardConfig { pub fn new( starboard: Starboard, - overrides: Vec, + channel_ids: &[i64], + mut overrides: Vec, ) -> serde_json::Result { + overrides.sort_by(|a, b| { + for cmp in channel_ids + .iter() + .map(|id| (a.channel_ids.contains(id), b.channel_ids.contains(id))) + { + match cmp.0.cmp(&cmp.1) { + Ordering::Equal => continue, + val => return val, + } + } + + Ordering::Equal + }); + let mut settings = starboard.settings.clone(); for ov in overrides.iter() { call_with_override_settings!(update_from_override, settings, ov.get_overrides()?) @@ -70,7 +87,7 @@ impl StarboardConfig { let overrides = StarboardOverride::list_by_starboard_and_channels(&bot.pool, sb.id, &channel_ids) .await?; - configs.push(Self::new(sb, overrides)?); + configs.push(Self::new(sb, &channel_ids, overrides)?); } Ok(configs) diff --git a/src/core/starboard/link_events.rs b/src/core/starboard/link_events.rs index 9d4145a4..60463634 100644 --- a/src/core/starboard/link_events.rs +++ b/src/core/starboard/link_events.rs @@ -77,7 +77,7 @@ pub async fn handle_message_delete( StarboardOverride::list_by_starboard_and_channels(&bot.pool, sb.id, &channel_ids) .await?; - let config = StarboardConfig::new(sb, overrides)?; + let config = StarboardConfig::new(sb, &channel_ids, overrides)?; match config.resolved.on_delete { 0 => false, // refresh diff --git a/src/interactions/commands/chat/overrides/edit/requirements.rs b/src/interactions/commands/chat/overrides/edit/requirements.rs index 2671b2bb..efb05454 100644 --- a/src/interactions/commands/chat/overrides/edit/requirements.rs +++ b/src/interactions/commands/chat/overrides/edit/requirements.rs @@ -71,7 +71,7 @@ impl EditRequirements { let starboard = Starboard::get(&ctx.bot.pool, ov.starboard_id) .await? .unwrap(); - let mut resolved = StarboardConfig::new(starboard, vec![ov])?; + let mut resolved = StarboardConfig::new(starboard, &[], vec![ov])?; (resolved.overrides.remove(0), resolved.resolved) }; diff --git a/src/interactions/commands/chat/overrides/view.rs b/src/interactions/commands/chat/overrides/view.rs index 35187d3a..2e0a3dc5 100644 --- a/src/interactions/commands/chat/overrides/view.rs +++ b/src/interactions/commands/chat/overrides/view.rs @@ -41,7 +41,7 @@ impl ViewOverride { let channels: Vec<_> = ov.channel_ids.iter().map(|id| format!("<#{id}>")).collect(); let channels = channels.join(", "); - let config = StarboardConfig::new(sb, vec![ov])?; + let config = StarboardConfig::new(sb, &[], vec![ov])?; let pretty = format_settings(&ctx.bot, guild_id, &config).await?; let embed = embed::build() diff --git a/src/interactions/commands/chat/random.rs b/src/interactions/commands/chat/random.rs index fe20c935..55dd3db5 100644 --- a/src/interactions/commands/chat/random.rs +++ b/src/interactions/commands/chat/random.rs @@ -71,7 +71,7 @@ pub async fn get_config( ) -> StarboardResult { let overrides = StarboardOverride::list_by_starboard_and_channels(&bot.pool, sb.id, &[channel_id]).await?; - Ok(StarboardConfig::new(sb, overrides)?) + Ok(StarboardConfig::new(sb, &[channel_id], overrides)?) } pub async fn get_embedder<'config, 'bot>( diff --git a/src/interactions/commands/chat/starboard/view.rs b/src/interactions/commands/chat/starboard/view.rs index 1dc07c59..6afa73f5 100644 --- a/src/interactions/commands/chat/starboard/view.rs +++ b/src/interactions/commands/chat/starboard/view.rs @@ -29,7 +29,7 @@ impl ViewStarboard { Starboard::get_by_name(&ctx.bot.pool, &name, guild_id.get_i64()).await?; if let Some(starboard) = starboard { - let config = StarboardConfig::new(starboard, vec![])?; + let config = StarboardConfig::new(starboard, &[], vec![])?; let pretty = format_settings(&ctx.bot, guild_id, &config).await?; let mut desc = String::new(); diff --git a/src/interactions/commands/chat/utils/info.rs b/src/interactions/commands/chat/utils/info.rs index 9e765a14..fe62c456 100644 --- a/src/interactions/commands/chat/utils/info.rs +++ b/src/interactions/commands/chat/utils/info.rs @@ -74,7 +74,7 @@ impl Info { ) .await?; - let config = StarboardConfig::new(starboard, overrides)?; + let config = StarboardConfig::new(starboard, &channel_ids, overrides)?; let sb_msg = StarboardMessage::get_by_starboard( &ctx.bot.pool,