From 20c9066e6f71c436c94be47998078d04a0fa3a20 Mon Sep 17 00:00:00 2001 From: bitfl0wer Date: Wed, 15 Nov 2023 20:18:50 +0100 Subject: [PATCH] Properly extract all extractable methods from GatewayHandle into Trait --- examples/gateway_observers.rs | 2 +- examples/gateway_simple.rs | 2 +- src/api/auth/login.rs | 2 +- src/api/auth/mod.rs | 2 +- src/api/auth/register.rs | 2 +- src/gateway/handle.rs | 212 +++++++++++++++++++--------------- 6 files changed, 122 insertions(+), 100 deletions(-) diff --git a/examples/gateway_observers.rs b/examples/gateway_observers.rs index 562a366..d0ba9dd 100644 --- a/examples/gateway_observers.rs +++ b/examples/gateway_observers.rs @@ -1,5 +1,5 @@ use async_trait::async_trait; -use chorus::gateway::GatewayCapable; +use chorus::gateway::{GatewayCapable, GatewayHandleCapable}; use chorus::{ self, gateway::{Gateway, Observer}, diff --git a/examples/gateway_simple.rs b/examples/gateway_simple.rs index 4549a2c..b522b47 100644 --- a/examples/gateway_simple.rs +++ b/examples/gateway_simple.rs @@ -1,6 +1,6 @@ use std::time::Duration; -use chorus::gateway::GatewayCapable; +use chorus::gateway::{GatewayCapable, GatewayHandleCapable}; use chorus::{self, gateway::Gateway, types::GatewayIdentifyPayload}; use tokio::time::sleep; diff --git a/src/api/auth/login.rs b/src/api/auth/login.rs index 750d01e..027056a 100644 --- a/src/api/auth/login.rs +++ b/src/api/auth/login.rs @@ -4,7 +4,7 @@ use reqwest::Client; use serde_json::to_string; use crate::errors::ChorusResult; -use crate::gateway::{Gateway, GatewayCapable}; +use crate::gateway::{Gateway, GatewayCapable, GatewayHandleCapable}; use crate::instance::{ChorusUser, Instance}; use crate::ratelimiter::ChorusRequest; use crate::types::{GatewayIdentifyPayload, LimitType, LoginResult, LoginSchema}; diff --git a/src/api/auth/mod.rs b/src/api/auth/mod.rs index 091975c..480c771 100644 --- a/src/api/auth/mod.rs +++ b/src/api/auth/mod.rs @@ -3,7 +3,7 @@ use std::sync::{Arc, RwLock}; pub use login::*; pub use register::*; -use crate::gateway::GatewayCapable; +use crate::gateway::{GatewayCapable, GatewayHandleCapable}; use crate::{ errors::ChorusResult, gateway::Gateway, diff --git a/src/api/auth/register.rs b/src/api/auth/register.rs index ed6fcbf..fa5c59e 100644 --- a/src/api/auth/register.rs +++ b/src/api/auth/register.rs @@ -3,7 +3,7 @@ use std::sync::{Arc, RwLock}; use reqwest::Client; use serde_json::to_string; -use crate::gateway::{Gateway, GatewayCapable, GatewayHandle}; +use crate::gateway::{Gateway, GatewayCapable, GatewayHandle, GatewayHandleCapable}; use crate::types::GatewayIdentifyPayload; use crate::{ errors::ChorusResult, diff --git a/src/gateway/handle.rs b/src/gateway/handle.rs index 8a18a5d..b5d748e 100644 --- a/src/gateway/handle.rs +++ b/src/gateway/handle.rs @@ -1,11 +1,128 @@ use super::{event::Events, *}; use crate::types::{self, Composite}; +#[async_trait(?Send)] pub trait GatewayHandleCapable where R: Stream, S: Sink, { + /// Sends json to the gateway with an opcode + async fn send_json_event(&self, op_code: u8, to_send: serde_json::Value); + + /// Observes an Item ``, which will update itself, if new information about this + /// item arrives on the corresponding Gateway Thread + async fn observe + Send + Sync>( + &self, + object: Arc>, + ) -> Arc>; + + /// Recursively observes and updates all updateable fields on the struct T. Returns an object `T` + /// with all of its observable fields being observed. + async fn observe_and_into_inner>( + &self, + object: Arc>, + ) -> T { + let channel = self.observe(object.clone()).await; + let object = channel.read().unwrap().clone(); + object + } + + /// Sends an identify event to the gateway + async fn send_identify(&self, to_send: types::GatewayIdentifyPayload) { + let to_send_value = serde_json::to_value(&to_send).unwrap(); + + trace!("GW: Sending Identify.."); + + self.send_json_event(GATEWAY_IDENTIFY, to_send_value).await; + } + + /// Sends an update presence event to the gateway + async fn send_update_presence(&self, to_send: types::UpdatePresence) { + let to_send_value = serde_json::to_value(&to_send).unwrap(); + + trace!("GW: Sending Update Presence.."); + + self.send_json_event(GATEWAY_UPDATE_PRESENCE, to_send_value) + .await; + } + + /// Sends a resume event to the gateway + async fn send_resume(&self, to_send: types::GatewayResume) { + let to_send_value = serde_json::to_value(&to_send).unwrap(); + + trace!("GW: Sending Resume.."); + + self.send_json_event(GATEWAY_RESUME, to_send_value).await; + } + + /// Sends a request guild members to the server + async fn send_request_guild_members(&self, to_send: types::GatewayRequestGuildMembers) { + let to_send_value = serde_json::to_value(&to_send).unwrap(); + + trace!("GW: Sending Request Guild Members.."); + + self.send_json_event(GATEWAY_REQUEST_GUILD_MEMBERS, to_send_value) + .await; + } + + /// Sends an update voice state to the server + async fn send_update_voice_state(&self, to_send: types::UpdateVoiceState) { + let to_send_value = serde_json::to_value(to_send).unwrap(); + + trace!("GW: Sending Update Voice State.."); + + self.send_json_event(GATEWAY_UPDATE_VOICE_STATE, to_send_value) + .await; + } + + /// Sends a call sync to the server + async fn send_call_sync(&self, to_send: types::CallSync) { + let to_send_value = serde_json::to_value(&to_send).unwrap(); + + trace!("GW: Sending Call Sync.."); + + self.send_json_event(GATEWAY_CALL_SYNC, to_send_value).await; + } + + /// Sends a Lazy Request + async fn send_lazy_request(&self, to_send: types::LazyRequest) { + let to_send_value = serde_json::to_value(&to_send).unwrap(); + + trace!("GW: Sending Lazy Request.."); + + self.send_json_event(GATEWAY_LAZY_REQUEST, to_send_value) + .await; + } + + /// Closes the websocket connection and stops all gateway tasks; + /// + /// Esentially pulls the plug on the gateway, leaving it possible to resume; + async fn close(&self); +} + +#[async_trait(?Send)] +impl + GatewayHandleCapable< + WebSocketStream>, + WebSocketStream>, + > for GatewayHandle +{ + async fn send_json_event(&self, op_code: u8, to_send: serde_json::Value) { + self.send_json_event(op_code, to_send).await + } + + async fn observe>( + &self, + object: Arc>, + ) -> Arc> { + self.observe(object).await + } + + async fn close(&self) { + self.kill_send.send(()).unwrap(); + self.websocket_send.lock().await.close().await.unwrap(); + } } /// Represents a handle to a Gateway connection. A Gateway connection will create observable @@ -30,7 +147,6 @@ pub struct GatewayHandle { } impl GatewayHandle { - /// Sends json to the gateway with an opcode async fn send_json_event(&self, op_code: u8, to_send: serde_json::Value) { let gateway_payload = types::GatewaySendPayload { op_code, @@ -89,98 +205,4 @@ impl GatewayHandle { wrapped } } - - /// Recursively observes and updates all updateable fields on the struct T. Returns an object `T` - /// with all of its observable fields being observed. - pub async fn observe_and_into_inner>( - &self, - object: Arc>, - ) -> T { - let channel = self.observe(object.clone()).await; - let object = channel.read().unwrap().clone(); - object - } - - /// Sends an identify event to the gateway - pub async fn send_identify(&self, to_send: types::GatewayIdentifyPayload) { - let to_send_value = serde_json::to_value(&to_send).unwrap(); - - trace!("GW: Sending Identify.."); - - self.send_json_event(GATEWAY_IDENTIFY, to_send_value).await; - } - - /// Sends a resume event to the gateway - pub async fn send_resume(&self, to_send: types::GatewayResume) { - let to_send_value = serde_json::to_value(&to_send).unwrap(); - - trace!("GW: Sending Resume.."); - - self.send_json_event(GATEWAY_RESUME, to_send_value).await; - } - - /// Sends an update presence event to the gateway - pub async fn send_update_presence(&self, to_send: types::UpdatePresence) { - let to_send_value = serde_json::to_value(&to_send).unwrap(); - - trace!("GW: Sending Update Presence.."); - - self.send_json_event(GATEWAY_UPDATE_PRESENCE, to_send_value) - .await; - } - - /// Sends a request guild members to the server - pub async fn send_request_guild_members(&self, to_send: types::GatewayRequestGuildMembers) { - let to_send_value = serde_json::to_value(&to_send).unwrap(); - - trace!("GW: Sending Request Guild Members.."); - - self.send_json_event(GATEWAY_REQUEST_GUILD_MEMBERS, to_send_value) - .await; - } - - /// Sends an update voice state to the server - pub async fn send_update_voice_state(&self, to_send: types::UpdateVoiceState) { - let to_send_value = serde_json::to_value(to_send).unwrap(); - - trace!("GW: Sending Update Voice State.."); - - self.send_json_event(GATEWAY_UPDATE_VOICE_STATE, to_send_value) - .await; - } - - /// Sends a call sync to the server - pub async fn send_call_sync(&self, to_send: types::CallSync) { - let to_send_value = serde_json::to_value(&to_send).unwrap(); - - trace!("GW: Sending Call Sync.."); - - self.send_json_event(GATEWAY_CALL_SYNC, to_send_value).await; - } - - /// Sends a Lazy Request - pub async fn send_lazy_request(&self, to_send: types::LazyRequest) { - let to_send_value = serde_json::to_value(&to_send).unwrap(); - - trace!("GW: Sending Lazy Request.."); - - self.send_json_event(GATEWAY_LAZY_REQUEST, to_send_value) - .await; - } - - /// Closes the websocket connection and stops all gateway tasks; - /// - /// Esentially pulls the plug on the gateway, leaving it possible to resume; - pub async fn close(&self) { - self.kill_send.send(()).unwrap(); - self.websocket_send.lock().await.close().await.unwrap(); - } -} - -impl - GatewayHandleCapable< - WebSocketStream>, - WebSocketStream>, - > for GatewayHandle -{ }