From 5ffd42c1bfe11d56fe397020b13c72f9dc943546 Mon Sep 17 00:00:00 2001 From: bitfl0wer Date: Thu, 16 Nov 2023 16:24:25 +0100 Subject: [PATCH] Move events to shared location --- src/gateway/default/gateway.rs | 164 +-------------------------------- src/gateway/default/handle.rs | 2 +- src/gateway/events.rs | 159 ++++++++++++++++++++++++++++++++ src/gateway/mod.rs | 8 +- 4 files changed, 168 insertions(+), 165 deletions(-) create mode 100644 src/gateway/events.rs diff --git a/src/gateway/default/gateway.rs b/src/gateway/default/gateway.rs index a90a2e9..49ce89e 100644 --- a/src/gateway/default/gateway.rs +++ b/src/gateway/default/gateway.rs @@ -1,6 +1,6 @@ use futures_util::StreamExt; -use self::event::Events; +use super::events::Events; use super::*; use crate::types::{self, WebSocketEvent}; @@ -185,165 +185,3 @@ impl DefaultGateway { Ok(()) } } - -pub mod event { - use super::*; - - #[derive(Default, Debug)] - pub struct Events { - pub application: Application, - pub auto_moderation: AutoModeration, - pub session: Session, - pub message: Message, - pub user: User, - pub relationship: Relationship, - pub channel: Channel, - pub thread: Thread, - pub guild: Guild, - pub invite: Invite, - pub integration: Integration, - pub interaction: Interaction, - pub stage_instance: StageInstance, - pub call: Call, - pub voice: Voice, - pub webhooks: Webhooks, - pub gateway_identify_payload: GatewayEvent, - pub gateway_resume: GatewayEvent, - pub error: GatewayEvent, - } - - #[derive(Default, Debug)] - pub struct Application { - pub command_permissions_update: GatewayEvent, - } - - #[derive(Default, Debug)] - pub struct AutoModeration { - pub rule_create: GatewayEvent, - pub rule_update: GatewayEvent, - pub rule_delete: GatewayEvent, - pub action_execution: GatewayEvent, - } - - #[derive(Default, Debug)] - pub struct Session { - pub ready: GatewayEvent, - pub ready_supplemental: GatewayEvent, - pub replace: GatewayEvent, - } - - #[derive(Default, Debug)] - pub struct StageInstance { - pub create: GatewayEvent, - pub update: GatewayEvent, - pub delete: GatewayEvent, - } - - #[derive(Default, Debug)] - pub struct Message { - pub create: GatewayEvent, - pub update: GatewayEvent, - pub delete: GatewayEvent, - pub delete_bulk: GatewayEvent, - pub reaction_add: GatewayEvent, - pub reaction_remove: GatewayEvent, - pub reaction_remove_all: GatewayEvent, - pub reaction_remove_emoji: GatewayEvent, - pub ack: GatewayEvent, - } - - #[derive(Default, Debug)] - pub struct User { - pub update: GatewayEvent, - pub guild_settings_update: GatewayEvent, - pub presence_update: GatewayEvent, - pub typing_start: GatewayEvent, - } - - #[derive(Default, Debug)] - pub struct Relationship { - pub add: GatewayEvent, - pub remove: GatewayEvent, - } - - #[derive(Default, Debug)] - pub struct Channel { - pub create: GatewayEvent, - pub update: GatewayEvent, - pub unread_update: GatewayEvent, - pub delete: GatewayEvent, - pub pins_update: GatewayEvent, - } - - #[derive(Default, Debug)] - pub struct Thread { - pub create: GatewayEvent, - pub update: GatewayEvent, - pub delete: GatewayEvent, - pub list_sync: GatewayEvent, - pub member_update: GatewayEvent, - pub members_update: GatewayEvent, - } - - #[derive(Default, Debug)] - pub struct Guild { - pub create: GatewayEvent, - pub update: GatewayEvent, - pub delete: GatewayEvent, - pub audit_log_entry_create: GatewayEvent, - pub ban_add: GatewayEvent, - pub ban_remove: GatewayEvent, - pub emojis_update: GatewayEvent, - pub stickers_update: GatewayEvent, - pub integrations_update: GatewayEvent, - pub member_add: GatewayEvent, - pub member_remove: GatewayEvent, - pub member_update: GatewayEvent, - pub members_chunk: GatewayEvent, - pub role_create: GatewayEvent, - pub role_update: GatewayEvent, - pub role_delete: GatewayEvent, - pub role_scheduled_event_create: GatewayEvent, - pub role_scheduled_event_update: GatewayEvent, - pub role_scheduled_event_delete: GatewayEvent, - pub role_scheduled_event_user_add: GatewayEvent, - pub role_scheduled_event_user_remove: GatewayEvent, - pub passive_update_v1: GatewayEvent, - } - - #[derive(Default, Debug)] - pub struct Invite { - pub create: GatewayEvent, - pub delete: GatewayEvent, - } - - #[derive(Default, Debug)] - pub struct Integration { - pub create: GatewayEvent, - pub update: GatewayEvent, - pub delete: GatewayEvent, - } - - #[derive(Default, Debug)] - pub struct Interaction { - pub create: GatewayEvent, - } - - #[derive(Default, Debug)] - pub struct Call { - pub create: GatewayEvent, - pub update: GatewayEvent, - pub delete: GatewayEvent, - } - - #[derive(Default, Debug)] - pub struct Voice { - pub state_update: GatewayEvent, - pub server_update: GatewayEvent, - } - - #[derive(Default, Debug)] - pub struct Webhooks { - pub update: GatewayEvent, - } -} diff --git a/src/gateway/default/handle.rs b/src/gateway/default/handle.rs index 14eb9cb..e76c0f1 100644 --- a/src/gateway/default/handle.rs +++ b/src/gateway/default/handle.rs @@ -1,4 +1,4 @@ -use super::{event::Events, *}; +use super::{events::Events, *}; use crate::types::{self, Composite}; #[async_trait(?Send)] diff --git a/src/gateway/events.rs b/src/gateway/events.rs new file mode 100644 index 0000000..bc8565e --- /dev/null +++ b/src/gateway/events.rs @@ -0,0 +1,159 @@ +use super::*; + +#[derive(Default, Debug)] +pub struct Events { + pub application: Application, + pub auto_moderation: AutoModeration, + pub session: Session, + pub message: Message, + pub user: User, + pub relationship: Relationship, + pub channel: Channel, + pub thread: Thread, + pub guild: Guild, + pub invite: Invite, + pub integration: Integration, + pub interaction: Interaction, + pub stage_instance: StageInstance, + pub call: Call, + pub voice: Voice, + pub webhooks: Webhooks, + pub gateway_identify_payload: GatewayEvent, + pub gateway_resume: GatewayEvent, + pub error: GatewayEvent, +} + +#[derive(Default, Debug)] +pub struct Application { + pub command_permissions_update: GatewayEvent, +} + +#[derive(Default, Debug)] +pub struct AutoModeration { + pub rule_create: GatewayEvent, + pub rule_update: GatewayEvent, + pub rule_delete: GatewayEvent, + pub action_execution: GatewayEvent, +} + +#[derive(Default, Debug)] +pub struct Session { + pub ready: GatewayEvent, + pub ready_supplemental: GatewayEvent, + pub replace: GatewayEvent, +} + +#[derive(Default, Debug)] +pub struct StageInstance { + pub create: GatewayEvent, + pub update: GatewayEvent, + pub delete: GatewayEvent, +} + +#[derive(Default, Debug)] +pub struct Message { + pub create: GatewayEvent, + pub update: GatewayEvent, + pub delete: GatewayEvent, + pub delete_bulk: GatewayEvent, + pub reaction_add: GatewayEvent, + pub reaction_remove: GatewayEvent, + pub reaction_remove_all: GatewayEvent, + pub reaction_remove_emoji: GatewayEvent, + pub ack: GatewayEvent, +} + +#[derive(Default, Debug)] +pub struct User { + pub update: GatewayEvent, + pub guild_settings_update: GatewayEvent, + pub presence_update: GatewayEvent, + pub typing_start: GatewayEvent, +} + +#[derive(Default, Debug)] +pub struct Relationship { + pub add: GatewayEvent, + pub remove: GatewayEvent, +} + +#[derive(Default, Debug)] +pub struct Channel { + pub create: GatewayEvent, + pub update: GatewayEvent, + pub unread_update: GatewayEvent, + pub delete: GatewayEvent, + pub pins_update: GatewayEvent, +} + +#[derive(Default, Debug)] +pub struct Thread { + pub create: GatewayEvent, + pub update: GatewayEvent, + pub delete: GatewayEvent, + pub list_sync: GatewayEvent, + pub member_update: GatewayEvent, + pub members_update: GatewayEvent, +} + +#[derive(Default, Debug)] +pub struct Guild { + pub create: GatewayEvent, + pub update: GatewayEvent, + pub delete: GatewayEvent, + pub audit_log_entry_create: GatewayEvent, + pub ban_add: GatewayEvent, + pub ban_remove: GatewayEvent, + pub emojis_update: GatewayEvent, + pub stickers_update: GatewayEvent, + pub integrations_update: GatewayEvent, + pub member_add: GatewayEvent, + pub member_remove: GatewayEvent, + pub member_update: GatewayEvent, + pub members_chunk: GatewayEvent, + pub role_create: GatewayEvent, + pub role_update: GatewayEvent, + pub role_delete: GatewayEvent, + pub role_scheduled_event_create: GatewayEvent, + pub role_scheduled_event_update: GatewayEvent, + pub role_scheduled_event_delete: GatewayEvent, + pub role_scheduled_event_user_add: GatewayEvent, + pub role_scheduled_event_user_remove: GatewayEvent, + pub passive_update_v1: GatewayEvent, +} + +#[derive(Default, Debug)] +pub struct Invite { + pub create: GatewayEvent, + pub delete: GatewayEvent, +} + +#[derive(Default, Debug)] +pub struct Integration { + pub create: GatewayEvent, + pub update: GatewayEvent, + pub delete: GatewayEvent, +} + +#[derive(Default, Debug)] +pub struct Interaction { + pub create: GatewayEvent, +} + +#[derive(Default, Debug)] +pub struct Call { + pub create: GatewayEvent, + pub update: GatewayEvent, + pub delete: GatewayEvent, +} + +#[derive(Default, Debug)] +pub struct Voice { + pub state_update: GatewayEvent, + pub server_update: GatewayEvent, +} + +#[derive(Default, Debug)] +pub struct Webhooks { + pub update: GatewayEvent, +} diff --git a/src/gateway/mod.rs b/src/gateway/mod.rs index 92ee61e..c33e7cb 100644 --- a/src/gateway/mod.rs +++ b/src/gateway/mod.rs @@ -1,4 +1,7 @@ +#[cfg(all(not(target_arch = "wasm32"), feature = "client"))] pub mod default; +pub mod events; +#[cfg(all(target_arch = "wasm32", feature = "client"))] pub mod wasm; #[cfg(all(not(target_arch = "wasm32"), feature = "client"))] @@ -6,7 +9,7 @@ pub use default::*; #[cfg(all(target_arch = "wasm32", feature = "client"))] pub use wasm::*; -use self::event::Events; +use self::events::Events; use crate::errors::GatewayError; use crate::types::{ self, AutoModerationRule, AutoModerationRuleUpdate, Channel, ChannelCreate, ChannelDelete, @@ -31,6 +34,9 @@ use tokio_tungstenite::tungstenite::Message; pub type GatewayStore = Arc>>>>; +/// The amount of time we wait for a heartbeat ack before resending our heartbeat in ms +const HEARTBEAT_ACK_TIMEOUT: u64 = 2000; + // Gateway opcodes /// Opcode received when the server dispatches a [crate::types::WebSocketEvent] const GATEWAY_DISPATCH: u8 = 0;