Skip to content

Commit

Permalink
feat: lazy evaluate events
Browse files Browse the repository at this point in the history
  • Loading branch information
www committed Nov 24, 2024
1 parent 2b61287 commit 1db5246
Show file tree
Hide file tree
Showing 10 changed files with 206 additions and 90 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.

4 changes: 2 additions & 2 deletions core/rust.bot_modules.core/src/tasks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ pub async fn punishment_expiry_task(

// Dispatch event
let punishment_id = punishment.id;
let event = silverpelt::ar_event::AntiraidEvent::PunishmentExpire(punishment);
let event = silverpelt::ar_event::AntiraidEvent::PunishmentExpire(punishment.into());

let event_handler_context =
std::sync::Arc::new(silverpelt::ar_event::EventHandlerContext {
Expand Down Expand Up @@ -124,7 +124,7 @@ pub async fn stings_expiry_task(

// Dispatch event
let sting_id = sting.id;
let event = silverpelt::ar_event::AntiraidEvent::StingExpire(sting);
let event = silverpelt::ar_event::AntiraidEvent::StingExpire(sting.into());

let event_handler_context =
std::sync::Arc::new(silverpelt::ar_event::EventHandlerContext {
Expand Down
1 change: 1 addition & 0 deletions core/rust.bot_modules.hooks/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ regex = "1"
url = "2"
typetag = "0.2"
async-trait = "0.1"
erased-serde = "0.4"

# Anti-Raid specific
silverpelt = { path = "../rust.silverpelt" }
Expand Down
135 changes: 71 additions & 64 deletions core/rust.bot_modules.hooks/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,25 @@ pub(crate) async fn event_listener(ectx: &EventHandlerContext) -> Result<(), sil
dispatch_audit_log(
ctx,
&ectx.data,
"AR/TrustedWebEvent",
"(Anti Raid) Trusted Web Event",
{
let mut m = serde_json::Map::new();
m.insert(
"event_name".to_string(),
serde_json::Value::String(event_name.to_string()),
);
m.insert("data".to_string(), data.clone());
serde_json::Value::Object(m)
},
templating::event::CreateEventArc::new(
"(Anti Raid) Trusted Web Event".to_string(),
"TrustedWebEvent".to_string(),
event_name.to_string(),
data.clone(),
false,
),
ectx.guild_id,
)
.await
}
AntiraidEvent::Discord(ref event) => {
if not_audit_loggable_event().contains(&event.into()) {
let event_ref = event.as_ref();
if not_audit_loggable_event().contains(&event_ref.into()) {
return Ok(());
}

// Ignore ourselves
match event {
match event_ref {
FullEvent::GuildAuditLogEntryCreate { .. } => {}
_ => match gwevent::core::get_event_user_id(event) {
Ok(user_id) => {
Expand All @@ -56,8 +53,6 @@ pub(crate) async fn event_listener(ectx: &EventHandlerContext) -> Result<(), sil
},
}

let event_data = serde_json::to_value(event)?;

// Convert to titlecase by capitalizing the first letter of each word
let event_titlename = event
.snake_case_name()
Expand All @@ -72,14 +67,16 @@ pub(crate) async fn event_listener(ectx: &EventHandlerContext) -> Result<(), sil
.collect::<Vec<String>>()
.join(" ");

let event_name: &'static str = event.into();

dispatch_audit_log(
ctx,
&ectx.data,
event_name,
&event_titlename,
event_data,
templating::event::CreateEventArc::new_arc(
event_titlename,
"Discord".to_string(),
event.snake_case_name().to_string(),
event.clone(),
false,
),
ectx.guild_id,
)
.await
Expand All @@ -89,82 +86,96 @@ pub(crate) async fn event_listener(ectx: &EventHandlerContext) -> Result<(), sil
dispatch_audit_log(
ctx,
&ectx.data,
&event.event_name,
&event.event_titlename,
event.event_data.clone(),
templating::event::CreateEventArc::new(
event.event_titlename.clone(),
"Custom".to_string(),
event.event_name.clone(),
event.event_data.clone(),
false,
),
ectx.guild_id,
)
.await
}
AntiraidEvent::StingCreate(ref sting) => {
let sting = serde_json::to_value(sting)?;

dispatch_audit_log(
ctx,
&ectx.data,
"AR/StingCreate",
"(Anti Raid) Sting Created",
serde_json::to_value(sting)?,
templating::event::CreateEventArc::new_arc(
"(Anti Raid) Sting Created".to_string(),
"StingCreate".to_string(),
"StingCreate".to_string(),
sting.clone(),
false,
),
ectx.guild_id,
)
.await?;

Ok(())
}
AntiraidEvent::StingExpire(ref sting) => {
let sting = serde_json::to_value(sting)?;

dispatch_audit_log(
ctx,
&ectx.data,
"AR/StingExpire",
"(Anti Raid) Sting Expired",
serde_json::to_value(sting)?,
templating::event::CreateEventArc::new_arc(
"(Anti Raid) Sting Expired".to_string(),
"StingExpire".to_string(),
"StingExpire".to_string(),
sting.clone(),
false,
),
ectx.guild_id,
)
.await?;

Ok(())
}
AntiraidEvent::StingDelete(ref sting) => {
let sting = serde_json::to_value(sting)?;

dispatch_audit_log(
ctx,
&ectx.data,
"AR/StingDelete",
"(Anti Raid) Sting Deleted",
serde_json::to_value(sting)?,
templating::event::CreateEventArc::new_arc(
"(Anti Raid) Sting Deleted".to_string(),
"StingDelete".to_string(),
"StingDelete".to_string(),
sting.clone(),
false,
),
ectx.guild_id,
)
.await?;

Ok(())
}
AntiraidEvent::PunishmentCreate(ref punishment) => {
let punishment = serde_json::to_value(punishment)?;

dispatch_audit_log(
ctx,
&ectx.data,
"AR/PunishmentCreate",
"(Anti Raid) Punishment Created",
serde_json::to_value(punishment)?,
templating::event::CreateEventArc::new_arc(
"(Anti Raid) Punishment Created".to_string(),
"PunishmentCreate".to_string(),
"PunishmentCreate".to_string(),
punishment.clone(),
false,
),
ectx.guild_id,
)
.await?;

Ok(())
}
AntiraidEvent::PunishmentExpire(ref punishment) => {
let punishment = serde_json::to_value(punishment)?;

dispatch_audit_log(
ctx,
&ectx.data,
"AR/PunishmentExpire",
"(Anti Raid) Punishment Expired",
serde_json::to_value(punishment)?,
templating::event::CreateEventArc::new_arc(
"(Anti Raid) Punishment Expired".to_string(),
"PunishmentExpire".to_string(),
"PunishmentExpire".to_string(),
punishment.clone(),
false,
),
ectx.guild_id,
)
.await?;
Expand All @@ -175,11 +186,15 @@ pub(crate) async fn event_listener(ectx: &EventHandlerContext) -> Result<(), sil
dispatch_audit_log(
ctx,
&ectx.data,
"AR/OnStartup",
"(Anti Raid) On Startup",
serde_json::json!({
"targets": modified
}),
templating::event::CreateEventArc::new(
"(Anti Raid) On Startup".to_string(),
"OnStartup".to_string(),
"OnStartup".to_string(),
serde_json::json!({
"targets": modified
}),
false,
),
ectx.guild_id,
)
.await
Expand Down Expand Up @@ -219,9 +234,7 @@ pub(crate) async fn should_dispatch_event(
async fn dispatch_audit_log(
ctx: &serenity::all::client::Context,
data: &silverpelt::data::Data,
event_name: &str,
event_titlename: &str,
event_data: serde_json::Value,
event: templating::event::CreateEventArc,
guild_id: serenity::model::id::GuildId,
) -> Result<(), silverpelt::Error> {
let templates = templating::cache::get_all_guild_templates(guild_id, &data.pool).await?;
Expand All @@ -232,7 +245,7 @@ async fn dispatch_audit_log(

for template in templates.iter() {
// Verify event dispatch
if !should_dispatch_event(event_name, {
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) = template.events {
Expand All @@ -252,13 +265,7 @@ async fn dispatch_audit_log(
data.pool.clone(),
ctx.clone(),
data.reqwest.clone(),
templating::event::Event::new(
event_titlename.to_string(),
event_name.to_string(),
event_data.clone(),
false,
Some(template.clone()),
),
event.into_event(),
)
.await?;
}
Expand Down
4 changes: 2 additions & 2 deletions core/rust.rpc_server.bot/src/templating_exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ pub(crate) async fn execute_template(
data.pool.clone(),
serenity_context.clone(),
data.reqwest.clone(),
templating::event::Event::new(
templating::event::Event::new_boxed(
"(Anti-Raid) Template Execution".to_string(),
"AR/Virtual_ExecTemplate".to_string(),
"AR/Virtual_ExecTemplate".to_string(),
serde_json::json!({
"args": req.args,
"user_id": user_id,
}),
false,
None,
),
)
.await;
Expand Down
12 changes: 6 additions & 6 deletions core/rust.silverpelt/src/ar_event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,22 @@ pub enum AntiraidEvent {
TrustedWebEvent((String, serde_json::Value)),

/// A regular discord event
Discord(serenity::all::FullEvent),
Discord(Arc<serenity::all::FullEvent>),

/// A sting create event. Dispatched when a sting is created
StingCreate(super::stings::Sting),
StingCreate(Arc<super::stings::Sting>),

/// A sting expiry event. Dispatched when a sting expires
StingExpire(super::stings::Sting),
StingExpire(Arc<super::stings::Sting>),

/// A sting delete event. Dispatched when a sting is manually deleted
StingDelete(super::stings::Sting),
StingDelete(Arc<super::stings::Sting>),

/// A punishment create event. Dispatched when a punishment is created
PunishmentCreate(super::punishments::Punishment),
PunishmentCreate(Arc<super::punishments::Punishment>),

/// A punishment expiration event. Dispatched when a punishment expires
PunishmentExpire(super::punishments::Punishment),
PunishmentExpire(Arc<super::punishments::Punishment>),

/// An on startup event is fired *at least once* when the bot starts up or the set of templates are modified
///
Expand Down
2 changes: 1 addition & 1 deletion core/rust.silverpelt/src/punishments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ impl Punishment {
crate::ar_event::EventHandlerContext {
guild_id: self.guild_id,
data: ctx.data::<crate::data::Data>(),
event: crate::ar_event::AntiraidEvent::PunishmentCreate(self),
event: crate::ar_event::AntiraidEvent::PunishmentCreate(self.into()),
serenity_context: ctx,
},
))
Expand Down
4 changes: 2 additions & 2 deletions core/rust.silverpelt/src/stings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ impl Sting {
crate::ar_event::EventHandlerContext {
guild_id: self.guild_id,
data: ctx.data::<crate::data::Data>(),
event: crate::ar_event::AntiraidEvent::StingCreate(self),
event: crate::ar_event::AntiraidEvent::StingCreate(self.into()),
serenity_context: ctx,
},
))
Expand All @@ -154,7 +154,7 @@ impl Sting {
crate::ar_event::EventHandlerContext {
guild_id: self.guild_id,
data: ctx.data::<crate::data::Data>(),
event: crate::ar_event::AntiraidEvent::StingDelete(self),
event: crate::ar_event::AntiraidEvent::StingDelete(self.into()),
serenity_context: ctx,
},
))
Expand Down
Loading

0 comments on commit 1db5246

Please sign in to comment.