Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

scoped overrides #175

Merged
merged 1 commit into from
Jan 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions src/core/starboard/config.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::cmp::Ordering;

use twilight_model::id::{
marker::{ChannelMarker, GuildMarker},
Id,
Expand Down Expand Up @@ -36,8 +38,23 @@ macro_rules! update_from_override {
impl StarboardConfig {
pub fn new(
starboard: Starboard,
overrides: Vec<StarboardOverride>,
channel_ids: &[i64],
mut overrides: Vec<StarboardOverride>,
) -> serde_json::Result<Self> {
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()?)
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/core/starboard/link_events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
};
Expand Down
2 changes: 1 addition & 1 deletion src/interactions/commands/chat/overrides/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion src/interactions/commands/chat/random.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ pub async fn get_config(
) -> StarboardResult<StarboardConfig> {
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>(
Expand Down
2 changes: 1 addition & 1 deletion src/interactions/commands/chat/starboard/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion src/interactions/commands/chat/utils/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down