Skip to content

Commit

Permalink
consolidate event filters directly into guild_templates
Browse files Browse the repository at this point in the history
  • Loading branch information
www committed Nov 18, 2024
1 parent 2e6c161 commit fb000f4
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 196 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions core/rust.bot_modules.core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ modules_ext = { path = "../rust.modules_ext" }
module_settings = { path = "../rust.module_settings" }
splashcore_rs = { path = "../rust.std" }
templating = { path = "../rust.templating" }
gwevent = { path = "../rust.gwevent" }
log = "0.4"

[dependencies.serenity]
Expand Down
12 changes: 12 additions & 0 deletions core/rust.bot_modules.core/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -976,6 +976,18 @@ pub static GUILD_TEMPLATES: LazyLock<ConfigOption> = LazyLock::new(|| {
ignored_for: vec![],
secret: false,
},
Column {
id: "events",
name: "Events",
description: "The events that this template can be dispatched on. If empty, this template is never dispatched.",
column_type: ColumnType::new_array(InnerColumnType::String { min_length: None, max_length: None, allowed_values: vec![], kind: InnerColumnTypeStringKind::Normal }),
nullable: true,
default: None,
unique: false,
suggestions: ColumnSuggestion::Static { suggestions: gwevent::core::event_list().to_vec() },
ignored_for: vec![],
secret: false,
},
module_settings::common_columns::created_at(),
module_settings::common_columns::created_by(),
module_settings::common_columns::last_updated_at(),
Expand Down
42 changes: 0 additions & 42 deletions core/rust.bot_modules.hooks/src/cache.rs

This file was deleted.

16 changes: 7 additions & 9 deletions core/rust.bot_modules.hooks/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,18 +224,18 @@ async fn dispatch_audit_log(
event_data: serde_json::Value,
guild_id: serenity::model::id::GuildId,
) -> Result<(), silverpelt::Error> {
let sinks = super::cache::get_sinks(guild_id, &data.pool).await?;
let templates = templating::cache::get_all_guild_templates(guild_id, &data.pool).await?;

if sinks.is_empty() {
if templates.is_empty() {
return Ok(());
}

for sink in sinks.iter() {
for template in templates.iter() {
// Verify event dispatch
if !should_dispatch_event(event_name, {
// False positive, unwrap_or_default cannot be used here as it moves the event out of the sink
#[allow(clippy::manual_unwrap_or_default)]
if let Some(ref events) = sink.events {
if let Some(ref events) = template.events {
events
} else {
&[]
Expand All @@ -248,16 +248,15 @@ async fn dispatch_audit_log(

templating::execute::<_, Option<()>>(
guild_id,
templating::Template::Named(sink.template.clone()),
templating::Template::Named(template.name.clone()),
data.pool.clone(),
ctx.clone(),
data.reqwest.clone(),
HookContext {
event_titlename: event_titlename.to_string(),
event_name: event_name.to_string(),
event_data: event_data.clone(),
sink_id: sink.id.to_string(),
sink: sink.sink.clone(),
template: template.clone(),
},
)
.await?;
Expand All @@ -273,8 +272,7 @@ struct HookContext {
pub event_titlename: String,
pub event_name: String,
pub event_data: serde_json::Value,
pub sink_id: String,
pub sink: String,
pub template: templating::GuildTemplate,
}

#[typetag::serde]
Expand Down
6 changes: 0 additions & 6 deletions core/rust.bot_modules.hooks/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
mod cache;
mod events;
mod settings;

pub struct Module;

Expand All @@ -25,10 +23,6 @@ impl silverpelt::module::Module for Module {
Some(Box::new(EventHandler))
}

fn config_options(&self) -> Vec<module_settings::types::ConfigOption> {
vec![(*settings::SINK).clone()]
}

fn full_command_list(&self) -> Vec<silverpelt::module::CommandObj> {
modules_ext::create_full_command_list(self)
}
Expand Down
138 changes: 0 additions & 138 deletions core/rust.bot_modules.hooks/src/settings.rs

This file was deleted.

5 changes: 4 additions & 1 deletion core/rust.templating/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ pub struct GuildTemplate {
pub name: String,
pub description: Option<String>,
pub shop_name: Option<String>,
pub events: Option<Vec<String>>,
pub content: String,
pub created_by: String,
pub created_at: chrono::DateTime<chrono::Utc>,
Expand Down Expand Up @@ -135,6 +136,7 @@ async fn get_template(
name: shop_template.name,
description: Some(shop_template.description),
shop_name: Some(template.to_string()),
events: None, // TODO
content: shop_template.content,
created_by: shop_template.created_by,
created_at: shop_template.created_at,
Expand All @@ -145,7 +147,7 @@ async fn get_template(
}
} else {
let rec = sqlx::query!(
"SELECT content, created_at, created_by, last_updated_at, last_updated_by FROM guild_templates WHERE guild_id = $1 AND name = $2",
"SELECT events, content, created_at, created_by, last_updated_at, last_updated_by FROM guild_templates WHERE guild_id = $1 AND name = $2",
guild_id.to_string(),
template
)
Expand All @@ -157,6 +159,7 @@ async fn get_template(
name: template.to_string(),
description: None,
shop_name: None,
events: rec.events,
content: rec.content,
created_by: rec.created_by,
created_at: rec.created_at,
Expand Down

0 comments on commit fb000f4

Please sign in to comment.