Prepare for wasm32-unknown-unknown adoption (#442)
Building for wasm32 requires changes throughout the project (e.g. conditional compiling). This PR sets up most of the things needed to build for this target.
This commit is contained in:
commit
5dbb3b1bf0
File diff suppressed because it is too large
Load Diff
51
Cargo.toml
51
Cargo.toml
|
@ -11,33 +11,38 @@ website = ["https://discord.com/invite/m3FpcapGDD"]
|
||||||
|
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
default = ["client"]
|
default = ["client", "rt-multi-thread"]
|
||||||
backend = ["poem", "sqlx"]
|
backend = ["dep:poem", "dep:sqlx"]
|
||||||
|
rt-multi-thread = ["tokio/rt-multi-thread"]
|
||||||
|
rt = ["tokio/rt"]
|
||||||
client = []
|
client = []
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
tokio = { version = "1.29.1", features = ["macros"] }
|
tokio = { version = "1.34.0", features = ["macros", "sync"] }
|
||||||
serde = { version = "1.0.188", features = ["derive", "rc"] }
|
serde = { version = "1.0.188", features = ["derive", "rc"] }
|
||||||
serde_json = { version = "1.0.105", features = ["raw_value"] }
|
serde_json = { version = "1.0.105", features = ["raw_value"] }
|
||||||
serde-aux = "4.2.0"
|
serde-aux = "4.2.0"
|
||||||
serde_with = "3.3.0"
|
serde_with = "3.3.0"
|
||||||
serde_repr = "0.1.16"
|
serde_repr = "0.1.16"
|
||||||
reqwest = { version = "0.11.20", features = ["multipart", "json"] }
|
reqwest = { git = "https://github.com/bitfl0wer/reqwest.git", branch = "wasm-headers", features = [
|
||||||
|
"multipart",
|
||||||
|
"json",
|
||||||
|
] }
|
||||||
url = "2.4.0"
|
url = "2.4.0"
|
||||||
chrono = { version = "0.4.26", features = ["serde"] }
|
chrono = { version = "0.4.26", features = ["serde"] }
|
||||||
regex = "1.9.4"
|
regex = "1.9.4"
|
||||||
custom_error = "1.9.2"
|
custom_error = "1.9.2"
|
||||||
tokio-tungstenite = { version = "0.20.1", features = [
|
|
||||||
"rustls-tls-native-roots",
|
|
||||||
"rustls-native-certs",
|
|
||||||
] }
|
|
||||||
futures-util = "0.3.28"
|
futures-util = "0.3.28"
|
||||||
http = "0.2.9"
|
http = "0.2.9"
|
||||||
base64 = "0.21.3"
|
base64 = "0.21.3"
|
||||||
hostname = "0.3.1"
|
|
||||||
bitflags = { version = "2.4.0", features = ["serde"] }
|
bitflags = { version = "2.4.0", features = ["serde"] }
|
||||||
lazy_static = "1.4.0"
|
lazy_static = "1.4.0"
|
||||||
poem = { version = "1.3.57", optional = true }
|
poem = { version = "1.3.57", optional = true }
|
||||||
|
thiserror = "1.0.47"
|
||||||
|
jsonwebtoken = "8.3.0"
|
||||||
|
log = "0.4.20"
|
||||||
|
async-trait = "0.1.73"
|
||||||
|
chorus-macros = "0.2.0"
|
||||||
sqlx = { version = "0.7.1", features = [
|
sqlx = { version = "0.7.1", features = [
|
||||||
"mysql",
|
"mysql",
|
||||||
"sqlite",
|
"sqlite",
|
||||||
|
@ -47,16 +52,28 @@ sqlx = { version = "0.7.1", features = [
|
||||||
"runtime-tokio-native-tls",
|
"runtime-tokio-native-tls",
|
||||||
"any",
|
"any",
|
||||||
], optional = true }
|
], optional = true }
|
||||||
thiserror = "1.0.47"
|
safina-timer = "0.1.11"
|
||||||
jsonwebtoken = "8.3.0"
|
rand = "0.8.5"
|
||||||
log = "0.4.20"
|
# TODO: Remove the below 2 imports for production!
|
||||||
async-trait = "0.1.73"
|
ws_stream_wasm = "0.7.4"
|
||||||
chorus-macros = "0.2.0"
|
pharos = "0.5.3"
|
||||||
|
|
||||||
|
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
|
||||||
rustls = "0.21.8"
|
rustls = "0.21.8"
|
||||||
rustls-native-certs = "0.6.3"
|
rustls-native-certs = "0.6.3"
|
||||||
rand = "0.8.5"
|
tokio-tungstenite = { version = "0.20.1", features = [
|
||||||
|
"rustls-tls-native-roots",
|
||||||
|
"rustls-native-certs",
|
||||||
|
] }
|
||||||
|
native-tls = "0.2.11"
|
||||||
|
hostname = "0.3.1"
|
||||||
|
|
||||||
|
[target.'cfg(target_arch = "wasm32")'.dependencies]
|
||||||
|
getrandom = { version = "0.2.11", features = ["js"] }
|
||||||
|
tokio-tungstenite = { version = "0.20.1", default-features = false }
|
||||||
|
ws_stream_wasm = "0.7.4"
|
||||||
|
pharos = "0.5.3"
|
||||||
|
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
tokio = { version = "1.32.0", features = ["full"] }
|
|
||||||
lazy_static = "1.4.0"
|
lazy_static = "1.4.0"
|
||||||
rusty-hook = "0.11.2"
|
|
||||||
|
|
|
@ -1,7 +1,8 @@
|
||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
|
use chorus::gateway::Gateway;
|
||||||
use chorus::{
|
use chorus::{
|
||||||
self,
|
self,
|
||||||
gateway::{Gateway, Observer},
|
gateway::Observer,
|
||||||
types::{GatewayIdentifyPayload, GatewayReady},
|
types::{GatewayIdentifyPayload, GatewayReady},
|
||||||
};
|
};
|
||||||
use std::{sync::Arc, time::Duration};
|
use std::{sync::Arc, time::Duration};
|
||||||
|
@ -24,13 +25,13 @@ impl Observer<GatewayReady> for ExampleObserver {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main(flavor = "current_thread")]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
// Find the gateway websocket url of the server we want to connect to
|
// Find the gateway websocket url of the server we want to connect to
|
||||||
let websocket_url_spacebar = "wss://gateway.old.server.spacebar.chat/".to_string();
|
let websocket_url_spacebar = "wss://gateway.old.server.spacebar.chat/".to_string();
|
||||||
|
|
||||||
// Initiate the gateway connection
|
// Initiate the gateway connection
|
||||||
let gateway = Gateway::new(websocket_url_spacebar).await.unwrap();
|
let gateway = Gateway::spawn(websocket_url_spacebar).await.unwrap();
|
||||||
|
|
||||||
// Create an instance of our observer
|
// Create an instance of our observer
|
||||||
let observer = ExampleObserver {};
|
let observer = ExampleObserver {};
|
||||||
|
|
|
@ -1,16 +1,17 @@
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
use chorus::{self, gateway::Gateway, types::GatewayIdentifyPayload};
|
use chorus::gateway::Gateway;
|
||||||
|
use chorus::{self, types::GatewayIdentifyPayload};
|
||||||
use tokio::time::sleep;
|
use tokio::time::sleep;
|
||||||
|
|
||||||
/// This example creates a simple gateway connection and a session with an Identify event
|
/// This example creates a simple gateway connection and a session with an Identify event
|
||||||
#[tokio::main]
|
#[tokio::main(flavor = "current_thread")]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
// Find the gateway websocket url of the server we want to connect to
|
// Find the gateway websocket url of the server we want to connect to
|
||||||
let websocket_url_spacebar = "wss://gateway.old.server.spacebar.chat/".to_string();
|
let websocket_url_spacebar = "wss://gateway.old.server.spacebar.chat/".to_string();
|
||||||
|
|
||||||
// Initiate the gateway connection, starting a listener in one thread and a heartbeat handler in another
|
// Initiate the gateway connection, starting a listener in one thread and a heartbeat handler in another
|
||||||
let gateway = Gateway::new(websocket_url_spacebar).await.unwrap();
|
let gateway = Gateway::spawn(websocket_url_spacebar).await.unwrap();
|
||||||
|
|
||||||
// At this point, we are connected to the server and are sending heartbeats, however we still haven't authenticated
|
// At this point, we are connected to the server and are sending heartbeats, however we still haven't authenticated
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
use chorus::instance::Instance;
|
use chorus::instance::Instance;
|
||||||
use chorus::UrlBundle;
|
use chorus::UrlBundle;
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main(flavor = "current_thread")]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
let bundle = UrlBundle::new(
|
let bundle = UrlBundle::new(
|
||||||
"https://example.com/api".to_string(),
|
"https://example.com/api".to_string(),
|
||||||
|
|
|
@ -2,7 +2,7 @@ use chorus::instance::Instance;
|
||||||
use chorus::types::LoginSchema;
|
use chorus::types::LoginSchema;
|
||||||
use chorus::UrlBundle;
|
use chorus::UrlBundle;
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main(flavor = "current_thread")]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
let bundle = UrlBundle::new(
|
let bundle = UrlBundle::new(
|
||||||
"https://example.com/api".to_string(),
|
"https://example.com/api".to_string(),
|
||||||
|
|
|
@ -36,7 +36,7 @@ impl Instance {
|
||||||
self.limits_information.as_mut().unwrap().ratelimits = shell.limits.clone().unwrap();
|
self.limits_information.as_mut().unwrap().ratelimits = shell.limits.clone().unwrap();
|
||||||
}
|
}
|
||||||
let mut identify = GatewayIdentifyPayload::common();
|
let mut identify = GatewayIdentifyPayload::common();
|
||||||
let gateway = Gateway::new(self.urls.wss.clone()).await.unwrap();
|
let gateway = Gateway::spawn(self.urls.wss.clone()).await.unwrap();
|
||||||
identify.token = login_result.token.clone();
|
identify.token = login_result.token.clone();
|
||||||
gateway.send_identify(identify).await;
|
gateway.send_identify(identify).await;
|
||||||
let user = ChorusUser::new(
|
let user = ChorusUser::new(
|
||||||
|
|
|
@ -3,9 +3,9 @@ use std::sync::{Arc, RwLock};
|
||||||
pub use login::*;
|
pub use login::*;
|
||||||
pub use register::*;
|
pub use register::*;
|
||||||
|
|
||||||
|
use crate::gateway::Gateway;
|
||||||
use crate::{
|
use crate::{
|
||||||
errors::ChorusResult,
|
errors::ChorusResult,
|
||||||
gateway::Gateway,
|
|
||||||
instance::{ChorusUser, Instance},
|
instance::{ChorusUser, Instance},
|
||||||
types::{GatewayIdentifyPayload, User},
|
types::{GatewayIdentifyPayload, User},
|
||||||
};
|
};
|
||||||
|
@ -25,7 +25,7 @@ impl Instance {
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
let mut identify = GatewayIdentifyPayload::common();
|
let mut identify = GatewayIdentifyPayload::common();
|
||||||
let gateway = Gateway::new(self.urls.wss.clone()).await.unwrap();
|
let gateway = Gateway::spawn(self.urls.wss.clone()).await.unwrap();
|
||||||
identify.token = token.clone();
|
identify.token = token.clone();
|
||||||
gateway.send_identify(identify).await;
|
gateway.send_identify(identify).await;
|
||||||
let user = ChorusUser::new(
|
let user = ChorusUser::new(
|
||||||
|
|
|
@ -3,7 +3,7 @@ use std::sync::{Arc, RwLock};
|
||||||
use reqwest::Client;
|
use reqwest::Client;
|
||||||
use serde_json::to_string;
|
use serde_json::to_string;
|
||||||
|
|
||||||
use crate::gateway::Gateway;
|
use crate::gateway::{Gateway, GatewayHandle};
|
||||||
use crate::types::GatewayIdentifyPayload;
|
use crate::types::GatewayIdentifyPayload;
|
||||||
use crate::{
|
use crate::{
|
||||||
errors::ChorusResult,
|
errors::ChorusResult,
|
||||||
|
@ -45,7 +45,7 @@ impl Instance {
|
||||||
let user_object = self.get_user(token.clone(), None).await.unwrap();
|
let user_object = self.get_user(token.clone(), None).await.unwrap();
|
||||||
let settings = ChorusUser::get_settings(&token, &self.urls.api.clone(), &mut self).await?;
|
let settings = ChorusUser::get_settings(&token, &self.urls.api.clone(), &mut self).await?;
|
||||||
let mut identify = GatewayIdentifyPayload::common();
|
let mut identify = GatewayIdentifyPayload::common();
|
||||||
let gateway = Gateway::new(self.urls.wss.clone()).await.unwrap();
|
let gateway: GatewayHandle = Gateway::spawn(self.urls.wss.clone()).await.unwrap();
|
||||||
identify.token = token.clone();
|
identify.token = token.clone();
|
||||||
gateway.send_identify(identify).await;
|
gateway.send_identify(identify).await;
|
||||||
let user = ChorusUser::new(
|
let user = ChorusUser::new(
|
||||||
|
|
|
@ -0,0 +1,65 @@
|
||||||
|
use futures_util::{
|
||||||
|
stream::{SplitSink, SplitStream},
|
||||||
|
StreamExt,
|
||||||
|
};
|
||||||
|
use tokio::net::TcpStream;
|
||||||
|
use tokio_tungstenite::{
|
||||||
|
connect_async_tls_with_config, tungstenite, Connector, MaybeTlsStream, WebSocketStream,
|
||||||
|
};
|
||||||
|
|
||||||
|
use super::GatewayMessage;
|
||||||
|
use crate::errors::GatewayError;
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub struct WebSocketBackend;
|
||||||
|
|
||||||
|
// These could be made into inherent associated types when that's stabilized
|
||||||
|
pub type WsSink = SplitSink<WebSocketStream<MaybeTlsStream<TcpStream>>, tungstenite::Message>;
|
||||||
|
pub type WsStream = SplitStream<WebSocketStream<MaybeTlsStream<TcpStream>>>;
|
||||||
|
|
||||||
|
impl WebSocketBackend {
|
||||||
|
pub async fn connect(
|
||||||
|
websocket_url: &str,
|
||||||
|
) -> Result<(WsSink, WsStream), crate::errors::GatewayError> {
|
||||||
|
let mut roots = rustls::RootCertStore::empty();
|
||||||
|
for cert in rustls_native_certs::load_native_certs().expect("could not load platform certs")
|
||||||
|
{
|
||||||
|
roots.add(&rustls::Certificate(cert.0)).unwrap();
|
||||||
|
}
|
||||||
|
let (websocket_stream, _) = match connect_async_tls_with_config(
|
||||||
|
websocket_url,
|
||||||
|
None,
|
||||||
|
false,
|
||||||
|
Some(Connector::Rustls(
|
||||||
|
rustls::ClientConfig::builder()
|
||||||
|
.with_safe_defaults()
|
||||||
|
.with_root_certificates(roots)
|
||||||
|
.with_no_client_auth()
|
||||||
|
.into(),
|
||||||
|
)),
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
Ok(websocket_stream) => websocket_stream,
|
||||||
|
Err(e) => {
|
||||||
|
return Err(GatewayError::CannotConnect {
|
||||||
|
error: e.to_string(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
Ok(websocket_stream.split())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<GatewayMessage> for tungstenite::Message {
|
||||||
|
fn from(message: GatewayMessage) -> Self {
|
||||||
|
Self::Text(message.0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<tungstenite::Message> for GatewayMessage {
|
||||||
|
fn from(value: tungstenite::Message) -> Self {
|
||||||
|
Self(value.to_string())
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,160 @@
|
||||||
|
use super::*;
|
||||||
|
use crate::types;
|
||||||
|
|
||||||
|
#[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<types::GatewayIdentifyPayload>,
|
||||||
|
pub gateway_resume: GatewayEvent<types::GatewayResume>,
|
||||||
|
pub error: GatewayEvent<GatewayError>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Default, Debug)]
|
||||||
|
pub struct Application {
|
||||||
|
pub command_permissions_update: GatewayEvent<types::ApplicationCommandPermissionsUpdate>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Default, Debug)]
|
||||||
|
pub struct AutoModeration {
|
||||||
|
pub rule_create: GatewayEvent<types::AutoModerationRuleCreate>,
|
||||||
|
pub rule_update: GatewayEvent<types::AutoModerationRuleUpdate>,
|
||||||
|
pub rule_delete: GatewayEvent<types::AutoModerationRuleDelete>,
|
||||||
|
pub action_execution: GatewayEvent<types::AutoModerationActionExecution>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Default, Debug)]
|
||||||
|
pub struct Session {
|
||||||
|
pub ready: GatewayEvent<types::GatewayReady>,
|
||||||
|
pub ready_supplemental: GatewayEvent<types::GatewayReadySupplemental>,
|
||||||
|
pub replace: GatewayEvent<types::SessionsReplace>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Default, Debug)]
|
||||||
|
pub struct StageInstance {
|
||||||
|
pub create: GatewayEvent<types::StageInstanceCreate>,
|
||||||
|
pub update: GatewayEvent<types::StageInstanceUpdate>,
|
||||||
|
pub delete: GatewayEvent<types::StageInstanceDelete>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Default, Debug)]
|
||||||
|
pub struct Message {
|
||||||
|
pub create: GatewayEvent<types::MessageCreate>,
|
||||||
|
pub update: GatewayEvent<types::MessageUpdate>,
|
||||||
|
pub delete: GatewayEvent<types::MessageDelete>,
|
||||||
|
pub delete_bulk: GatewayEvent<types::MessageDeleteBulk>,
|
||||||
|
pub reaction_add: GatewayEvent<types::MessageReactionAdd>,
|
||||||
|
pub reaction_remove: GatewayEvent<types::MessageReactionRemove>,
|
||||||
|
pub reaction_remove_all: GatewayEvent<types::MessageReactionRemoveAll>,
|
||||||
|
pub reaction_remove_emoji: GatewayEvent<types::MessageReactionRemoveEmoji>,
|
||||||
|
pub ack: GatewayEvent<types::MessageACK>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Default, Debug)]
|
||||||
|
pub struct User {
|
||||||
|
pub update: GatewayEvent<types::UserUpdate>,
|
||||||
|
pub guild_settings_update: GatewayEvent<types::UserGuildSettingsUpdate>,
|
||||||
|
pub presence_update: GatewayEvent<types::PresenceUpdate>,
|
||||||
|
pub typing_start: GatewayEvent<types::TypingStartEvent>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Default, Debug)]
|
||||||
|
pub struct Relationship {
|
||||||
|
pub add: GatewayEvent<types::RelationshipAdd>,
|
||||||
|
pub remove: GatewayEvent<types::RelationshipRemove>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Default, Debug)]
|
||||||
|
pub struct Channel {
|
||||||
|
pub create: GatewayEvent<types::ChannelCreate>,
|
||||||
|
pub update: GatewayEvent<types::ChannelUpdate>,
|
||||||
|
pub unread_update: GatewayEvent<types::ChannelUnreadUpdate>,
|
||||||
|
pub delete: GatewayEvent<types::ChannelDelete>,
|
||||||
|
pub pins_update: GatewayEvent<types::ChannelPinsUpdate>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Default, Debug)]
|
||||||
|
pub struct Thread {
|
||||||
|
pub create: GatewayEvent<types::ThreadCreate>,
|
||||||
|
pub update: GatewayEvent<types::ThreadUpdate>,
|
||||||
|
pub delete: GatewayEvent<types::ThreadDelete>,
|
||||||
|
pub list_sync: GatewayEvent<types::ThreadListSync>,
|
||||||
|
pub member_update: GatewayEvent<types::ThreadMemberUpdate>,
|
||||||
|
pub members_update: GatewayEvent<types::ThreadMembersUpdate>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Default, Debug)]
|
||||||
|
pub struct Guild {
|
||||||
|
pub create: GatewayEvent<types::GuildCreate>,
|
||||||
|
pub update: GatewayEvent<types::GuildUpdate>,
|
||||||
|
pub delete: GatewayEvent<types::GuildDelete>,
|
||||||
|
pub audit_log_entry_create: GatewayEvent<types::GuildAuditLogEntryCreate>,
|
||||||
|
pub ban_add: GatewayEvent<types::GuildBanAdd>,
|
||||||
|
pub ban_remove: GatewayEvent<types::GuildBanRemove>,
|
||||||
|
pub emojis_update: GatewayEvent<types::GuildEmojisUpdate>,
|
||||||
|
pub stickers_update: GatewayEvent<types::GuildStickersUpdate>,
|
||||||
|
pub integrations_update: GatewayEvent<types::GuildIntegrationsUpdate>,
|
||||||
|
pub member_add: GatewayEvent<types::GuildMemberAdd>,
|
||||||
|
pub member_remove: GatewayEvent<types::GuildMemberRemove>,
|
||||||
|
pub member_update: GatewayEvent<types::GuildMemberUpdate>,
|
||||||
|
pub members_chunk: GatewayEvent<types::GuildMembersChunk>,
|
||||||
|
pub role_create: GatewayEvent<types::GuildRoleCreate>,
|
||||||
|
pub role_update: GatewayEvent<types::GuildRoleUpdate>,
|
||||||
|
pub role_delete: GatewayEvent<types::GuildRoleDelete>,
|
||||||
|
pub role_scheduled_event_create: GatewayEvent<types::GuildScheduledEventCreate>,
|
||||||
|
pub role_scheduled_event_update: GatewayEvent<types::GuildScheduledEventUpdate>,
|
||||||
|
pub role_scheduled_event_delete: GatewayEvent<types::GuildScheduledEventDelete>,
|
||||||
|
pub role_scheduled_event_user_add: GatewayEvent<types::GuildScheduledEventUserAdd>,
|
||||||
|
pub role_scheduled_event_user_remove: GatewayEvent<types::GuildScheduledEventUserRemove>,
|
||||||
|
pub passive_update_v1: GatewayEvent<types::PassiveUpdateV1>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Default, Debug)]
|
||||||
|
pub struct Invite {
|
||||||
|
pub create: GatewayEvent<types::InviteCreate>,
|
||||||
|
pub delete: GatewayEvent<types::InviteDelete>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Default, Debug)]
|
||||||
|
pub struct Integration {
|
||||||
|
pub create: GatewayEvent<types::IntegrationCreate>,
|
||||||
|
pub update: GatewayEvent<types::IntegrationUpdate>,
|
||||||
|
pub delete: GatewayEvent<types::IntegrationDelete>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Default, Debug)]
|
||||||
|
pub struct Interaction {
|
||||||
|
pub create: GatewayEvent<types::InteractionCreate>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Default, Debug)]
|
||||||
|
pub struct Call {
|
||||||
|
pub create: GatewayEvent<types::CallCreate>,
|
||||||
|
pub update: GatewayEvent<types::CallUpdate>,
|
||||||
|
pub delete: GatewayEvent<types::CallDelete>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Default, Debug)]
|
||||||
|
pub struct Voice {
|
||||||
|
pub state_update: GatewayEvent<types::VoiceStateUpdate>,
|
||||||
|
pub server_update: GatewayEvent<types::VoiceServerUpdate>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Default, Debug)]
|
||||||
|
pub struct Webhooks {
|
||||||
|
pub update: GatewayEvent<types::WebhooksUpdate>,
|
||||||
|
}
|
|
@ -1,5 +1,12 @@
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
|
use futures_util::{SinkExt, StreamExt};
|
||||||
|
use log::*;
|
||||||
|
use tokio::task;
|
||||||
|
|
||||||
use self::event::Events;
|
use self::event::Events;
|
||||||
use super::*;
|
use super::*;
|
||||||
|
use super::{WsSink, WsStream};
|
||||||
use crate::types::{
|
use crate::types::{
|
||||||
self, AutoModerationRule, AutoModerationRuleUpdate, Channel, ChannelCreate, ChannelDelete,
|
self, AutoModerationRule, AutoModerationRuleUpdate, Channel, ChannelCreate, ChannelDelete,
|
||||||
ChannelUpdate, Guild, GuildRoleCreate, GuildRoleUpdate, JsonField, RoleObject, SourceUrlField,
|
ChannelUpdate, Guild, GuildRoleCreate, GuildRoleUpdate, JsonField, RoleObject, SourceUrlField,
|
||||||
|
@ -10,15 +17,8 @@ use crate::types::{
|
||||||
pub struct Gateway {
|
pub struct Gateway {
|
||||||
events: Arc<Mutex<Events>>,
|
events: Arc<Mutex<Events>>,
|
||||||
heartbeat_handler: HeartbeatHandler,
|
heartbeat_handler: HeartbeatHandler,
|
||||||
websocket_send: Arc<
|
websocket_send: Arc<Mutex<WsSink>>,
|
||||||
Mutex<
|
websocket_receive: WsStream,
|
||||||
SplitSink<
|
|
||||||
WebSocketStream<MaybeTlsStream<TcpStream>>,
|
|
||||||
tokio_tungstenite::tungstenite::Message,
|
|
||||||
>,
|
|
||||||
>,
|
|
||||||
>,
|
|
||||||
websocket_receive: SplitStream<WebSocketStream<MaybeTlsStream<TcpStream>>>,
|
|
||||||
kill_send: tokio::sync::broadcast::Sender<()>,
|
kill_send: tokio::sync::broadcast::Sender<()>,
|
||||||
store: Arc<Mutex<HashMap<Snowflake, Arc<RwLock<ObservableObject>>>>>,
|
store: Arc<Mutex<HashMap<Snowflake, Arc<RwLock<ObservableObject>>>>>,
|
||||||
url: String,
|
url: String,
|
||||||
|
@ -26,35 +26,9 @@ pub struct Gateway {
|
||||||
|
|
||||||
impl Gateway {
|
impl Gateway {
|
||||||
#[allow(clippy::new_ret_no_self)]
|
#[allow(clippy::new_ret_no_self)]
|
||||||
pub async fn new(websocket_url: String) -> Result<GatewayHandle, GatewayError> {
|
pub async fn spawn(websocket_url: String) -> Result<GatewayHandle, GatewayError> {
|
||||||
let mut roots = rustls::RootCertStore::empty();
|
let (websocket_send, mut websocket_receive) =
|
||||||
for cert in rustls_native_certs::load_native_certs().expect("could not load platform certs")
|
WebSocketBackend::connect(&websocket_url).await?;
|
||||||
{
|
|
||||||
roots.add(&rustls::Certificate(cert.0)).unwrap();
|
|
||||||
}
|
|
||||||
let (websocket_stream, _) = match connect_async_tls_with_config(
|
|
||||||
&websocket_url,
|
|
||||||
None,
|
|
||||||
false,
|
|
||||||
Some(Connector::Rustls(
|
|
||||||
rustls::ClientConfig::builder()
|
|
||||||
.with_safe_defaults()
|
|
||||||
.with_root_certificates(roots)
|
|
||||||
.with_no_client_auth()
|
|
||||||
.into(),
|
|
||||||
)),
|
|
||||||
)
|
|
||||||
.await
|
|
||||||
{
|
|
||||||
Ok(websocket_stream) => websocket_stream,
|
|
||||||
Err(e) => {
|
|
||||||
return Err(GatewayError::CannotConnect {
|
|
||||||
error: e.to_string(),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
let (websocket_send, mut websocket_receive) = websocket_stream.split();
|
|
||||||
|
|
||||||
let shared_websocket_send = Arc::new(Mutex::new(websocket_send));
|
let shared_websocket_send = Arc::new(Mutex::new(websocket_send));
|
||||||
|
|
||||||
|
@ -63,9 +37,8 @@ impl Gateway {
|
||||||
|
|
||||||
// Wait for the first hello and then spawn both tasks so we avoid nested tasks
|
// Wait for the first hello and then spawn both tasks so we avoid nested tasks
|
||||||
// This automatically spawns the heartbeat task, but from the main thread
|
// This automatically spawns the heartbeat task, but from the main thread
|
||||||
let msg = websocket_receive.next().await.unwrap().unwrap();
|
let msg: GatewayMessage = websocket_receive.next().await.unwrap().unwrap().into();
|
||||||
let gateway_payload: types::GatewayReceivePayload =
|
let gateway_payload: types::GatewayReceivePayload = serde_json::from_str(&msg.0).unwrap();
|
||||||
serde_json::from_str(msg.to_text().unwrap()).unwrap();
|
|
||||||
|
|
||||||
if gateway_payload.op_code != GATEWAY_HELLO {
|
if gateway_payload.op_code != GATEWAY_HELLO {
|
||||||
return Err(GatewayError::NonHelloOnInitiate {
|
return Err(GatewayError::NonHelloOnInitiate {
|
||||||
|
@ -120,8 +93,7 @@ impl Gateway {
|
||||||
|
|
||||||
// This if chain can be much better but if let is unstable on stable rust
|
// This if chain can be much better but if let is unstable on stable rust
|
||||||
if let Some(Ok(message)) = msg {
|
if let Some(Ok(message)) = msg {
|
||||||
self.handle_message(GatewayMessage::from_tungstenite_message(message))
|
self.handle_message(message.into()).await;
|
||||||
.await;
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -156,31 +128,23 @@ impl Gateway {
|
||||||
|
|
||||||
/// This handles a message as a websocket event and updates its events along with the events' observers
|
/// This handles a message as a websocket event and updates its events along with the events' observers
|
||||||
pub async fn handle_message(&mut self, msg: GatewayMessage) {
|
pub async fn handle_message(&mut self, msg: GatewayMessage) {
|
||||||
if msg.is_empty() {
|
if msg.0.is_empty() {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if !msg.is_error() && !msg.is_payload() {
|
let Ok(gateway_payload) = msg.payload() else {
|
||||||
warn!(
|
if let Some(error) = msg.error() {
|
||||||
"Message unrecognised: {:?}, please open an issue on the chorus github",
|
warn!("GW: Received error {:?}, connection will close..", error);
|
||||||
msg.message.to_string()
|
self.close().await;
|
||||||
);
|
self.events.lock().await.error.notify(error).await;
|
||||||
|
} else {
|
||||||
|
warn!(
|
||||||
|
"Message unrecognised: {:?}, please open an issue on the chorus github",
|
||||||
|
msg.0
|
||||||
|
);
|
||||||
|
}
|
||||||
return;
|
return;
|
||||||
}
|
};
|
||||||
|
|
||||||
if msg.is_error() {
|
|
||||||
let error = msg.error().unwrap();
|
|
||||||
|
|
||||||
warn!("GW: Received error {:?}, connection will close..", error);
|
|
||||||
|
|
||||||
self.close().await;
|
|
||||||
|
|
||||||
self.events.lock().await.error.notify(error).await;
|
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
let gateway_payload = msg.payload().unwrap();
|
|
||||||
|
|
||||||
// See https://discord.com/developers/docs/topics/opcodes-and-status-codes#gateway-gateway-opcodes
|
// See https://discord.com/developers/docs/topics/opcodes-and-status-codes#gateway-gateway-opcodes
|
||||||
match gateway_payload.op_code {
|
match gateway_payload.op_code {
|
||||||
|
|
|
@ -1,3 +1,8 @@
|
||||||
|
use futures_util::SinkExt;
|
||||||
|
use log::*;
|
||||||
|
|
||||||
|
use std::fmt::Debug;
|
||||||
|
|
||||||
use super::{event::Events, *};
|
use super::{event::Events, *};
|
||||||
use crate::types::{self, Composite};
|
use crate::types::{self, Composite};
|
||||||
|
|
||||||
|
@ -9,14 +14,7 @@ use crate::types::{self, Composite};
|
||||||
pub struct GatewayHandle {
|
pub struct GatewayHandle {
|
||||||
pub url: String,
|
pub url: String,
|
||||||
pub events: Arc<Mutex<Events>>,
|
pub events: Arc<Mutex<Events>>,
|
||||||
pub websocket_send: Arc<
|
pub websocket_send: Arc<Mutex<WsSink>>,
|
||||||
Mutex<
|
|
||||||
SplitSink<
|
|
||||||
WebSocketStream<MaybeTlsStream<TcpStream>>,
|
|
||||||
tokio_tungstenite::tungstenite::Message,
|
|
||||||
>,
|
|
||||||
>,
|
|
||||||
>,
|
|
||||||
/// Tells gateway tasks to close
|
/// Tells gateway tasks to close
|
||||||
pub(super) kill_send: tokio::sync::broadcast::Sender<()>,
|
pub(super) kill_send: tokio::sync::broadcast::Sender<()>,
|
||||||
pub(crate) store: Arc<Mutex<HashMap<Snowflake, Arc<RwLock<ObservableObject>>>>>,
|
pub(crate) store: Arc<Mutex<HashMap<Snowflake, Arc<RwLock<ObservableObject>>>>>,
|
||||||
|
@ -32,13 +30,12 @@ impl GatewayHandle {
|
||||||
};
|
};
|
||||||
|
|
||||||
let payload_json = serde_json::to_string(&gateway_payload).unwrap();
|
let payload_json = serde_json::to_string(&gateway_payload).unwrap();
|
||||||
|
let message = GatewayMessage(payload_json);
|
||||||
let message = tokio_tungstenite::tungstenite::Message::text(payload_json);
|
|
||||||
|
|
||||||
self.websocket_send
|
self.websocket_send
|
||||||
.lock()
|
.lock()
|
||||||
.await
|
.await
|
||||||
.send(message)
|
.send(message.into())
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,13 @@
|
||||||
use crate::types;
|
use futures_util::SinkExt;
|
||||||
|
use log::*;
|
||||||
|
use std::time::{self, Duration, Instant};
|
||||||
|
use tokio::sync::mpsc::{Receiver, Sender};
|
||||||
|
|
||||||
|
use safina_timer::sleep_until;
|
||||||
|
use tokio::task::{self, JoinHandle};
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
|
use crate::types;
|
||||||
|
|
||||||
/// The amount of time we wait for a heartbeat ack before resending our heartbeat in ms
|
/// The amount of time we wait for a heartbeat ack before resending our heartbeat in ms
|
||||||
const HEARTBEAT_ACK_TIMEOUT: u64 = 2000;
|
const HEARTBEAT_ACK_TIMEOUT: u64 = 2000;
|
||||||
|
@ -20,27 +27,14 @@ pub(super) struct HeartbeatHandler {
|
||||||
impl HeartbeatHandler {
|
impl HeartbeatHandler {
|
||||||
pub fn new(
|
pub fn new(
|
||||||
heartbeat_interval: Duration,
|
heartbeat_interval: Duration,
|
||||||
websocket_tx: Arc<
|
websocket_tx: Arc<Mutex<WsSink>>,
|
||||||
Mutex<
|
|
||||||
SplitSink<
|
|
||||||
WebSocketStream<MaybeTlsStream<TcpStream>>,
|
|
||||||
tokio_tungstenite::tungstenite::Message,
|
|
||||||
>,
|
|
||||||
>,
|
|
||||||
>,
|
|
||||||
kill_rc: tokio::sync::broadcast::Receiver<()>,
|
kill_rc: tokio::sync::broadcast::Receiver<()>,
|
||||||
) -> HeartbeatHandler {
|
) -> Self {
|
||||||
let (send, receive) = tokio::sync::mpsc::channel(32);
|
let (send, receive) = tokio::sync::mpsc::channel(32);
|
||||||
let kill_receive = kill_rc.resubscribe();
|
let kill_receive = kill_rc.resubscribe();
|
||||||
|
|
||||||
let handle: JoinHandle<()> = task::spawn(async move {
|
let handle: JoinHandle<()> = task::spawn(async move {
|
||||||
HeartbeatHandler::heartbeat_task(
|
Self::heartbeat_task(websocket_tx, heartbeat_interval, receive, kill_receive).await;
|
||||||
websocket_tx,
|
|
||||||
heartbeat_interval,
|
|
||||||
receive,
|
|
||||||
kill_receive,
|
|
||||||
)
|
|
||||||
.await;
|
|
||||||
});
|
});
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
|
@ -55,22 +49,17 @@ impl HeartbeatHandler {
|
||||||
/// Can be killed by the kill broadcast;
|
/// Can be killed by the kill broadcast;
|
||||||
/// If the websocket is closed, will die out next time it tries to send a heartbeat;
|
/// If the websocket is closed, will die out next time it tries to send a heartbeat;
|
||||||
pub async fn heartbeat_task(
|
pub async fn heartbeat_task(
|
||||||
websocket_tx: Arc<
|
websocket_tx: Arc<Mutex<WsSink>>,
|
||||||
Mutex<
|
|
||||||
SplitSink<
|
|
||||||
WebSocketStream<MaybeTlsStream<TcpStream>>,
|
|
||||||
tokio_tungstenite::tungstenite::Message,
|
|
||||||
>,
|
|
||||||
>,
|
|
||||||
>,
|
|
||||||
heartbeat_interval: Duration,
|
heartbeat_interval: Duration,
|
||||||
mut receive: tokio::sync::mpsc::Receiver<HeartbeatThreadCommunication>,
|
mut receive: Receiver<HeartbeatThreadCommunication>,
|
||||||
mut kill_receive: tokio::sync::broadcast::Receiver<()>,
|
mut kill_receive: tokio::sync::broadcast::Receiver<()>,
|
||||||
) {
|
) {
|
||||||
let mut last_heartbeat_timestamp: Instant = time::Instant::now();
|
let mut last_heartbeat_timestamp: Instant = time::Instant::now();
|
||||||
let mut last_heartbeat_acknowledged = true;
|
let mut last_heartbeat_acknowledged = true;
|
||||||
let mut last_seq_number: Option<u64> = None;
|
let mut last_seq_number: Option<u64> = None;
|
||||||
|
|
||||||
|
safina_timer::start_timer_thread();
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
if kill_receive.try_recv().is_ok() {
|
if kill_receive.try_recv().is_ok() {
|
||||||
trace!("GW: Closing heartbeat task");
|
trace!("GW: Closing heartbeat task");
|
||||||
|
@ -122,9 +111,9 @@ impl HeartbeatHandler {
|
||||||
|
|
||||||
let heartbeat_json = serde_json::to_string(&heartbeat).unwrap();
|
let heartbeat_json = serde_json::to_string(&heartbeat).unwrap();
|
||||||
|
|
||||||
let msg = tokio_tungstenite::tungstenite::Message::text(heartbeat_json);
|
let msg = GatewayMessage(heartbeat_json);
|
||||||
|
|
||||||
let send_result = websocket_tx.lock().await.send(msg).await;
|
let send_result = websocket_tx.lock().await.send(msg.into()).await;
|
||||||
if send_result.is_err() {
|
if send_result.is_err() {
|
||||||
// We couldn't send, the websocket is broken
|
// We couldn't send, the websocket is broken
|
||||||
warn!("GW: Couldnt send heartbeat, websocket seems broken");
|
warn!("GW: Couldnt send heartbeat, websocket seems broken");
|
||||||
|
|
|
@ -5,24 +5,14 @@ use super::*;
|
||||||
/// Represents a messsage received from the gateway. This will be either a [types::GatewayReceivePayload], containing events, or a [GatewayError].
|
/// Represents a messsage received from the gateway. This will be either a [types::GatewayReceivePayload], containing events, or a [GatewayError].
|
||||||
/// This struct is used internally when handling messages.
|
/// This struct is used internally when handling messages.
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct GatewayMessage {
|
pub struct GatewayMessage(pub String);
|
||||||
/// The message we received from the server
|
|
||||||
pub(super) message: tokio_tungstenite::tungstenite::Message,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl GatewayMessage {
|
impl GatewayMessage {
|
||||||
/// Creates self from a tungstenite message
|
|
||||||
pub fn from_tungstenite_message(message: tokio_tungstenite::tungstenite::Message) -> Self {
|
|
||||||
Self { message }
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Parses the message as an error;
|
/// Parses the message as an error;
|
||||||
/// Returns the error if succesfully parsed, None if the message isn't an error
|
/// Returns the error if succesfully parsed, None if the message isn't an error
|
||||||
pub fn error(&self) -> Option<GatewayError> {
|
pub fn error(&self) -> Option<GatewayError> {
|
||||||
let content = self.message.to_string();
|
|
||||||
|
|
||||||
// Some error strings have dots on the end, which we don't care about
|
// Some error strings have dots on the end, which we don't care about
|
||||||
let processed_content = content.to_lowercase().replace('.', "");
|
let processed_content = self.0.to_lowercase().replace('.', "");
|
||||||
|
|
||||||
match processed_content.as_str() {
|
match processed_content.as_str() {
|
||||||
"unknown error" | "4000" => Some(GatewayError::Unknown),
|
"unknown error" | "4000" => Some(GatewayError::Unknown),
|
||||||
|
@ -45,29 +35,9 @@ impl GatewayMessage {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns whether or not the message is an error
|
|
||||||
pub fn is_error(&self) -> bool {
|
|
||||||
self.error().is_some()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Parses the message as a payload;
|
/// Parses the message as a payload;
|
||||||
/// Returns a result of deserializing
|
/// Returns a result of deserializing
|
||||||
pub fn payload(&self) -> Result<types::GatewayReceivePayload, serde_json::Error> {
|
pub fn payload(&self) -> Result<types::GatewayReceivePayload, serde_json::Error> {
|
||||||
return serde_json::from_str(self.message.to_text().unwrap());
|
serde_json::from_str(&self.0)
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns whether or not the message is a payload
|
|
||||||
pub fn is_payload(&self) -> bool {
|
|
||||||
// close messages are never payloads, payloads are only text messages
|
|
||||||
if self.message.is_close() | !self.message.is_text() {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return self.payload().is_ok();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns whether or not the message is empty
|
|
||||||
pub fn is_empty(&self) -> bool {
|
|
||||||
self.message.is_empty()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,3 +1,8 @@
|
||||||
|
use async_trait::async_trait;
|
||||||
|
|
||||||
|
#[cfg(not(target_arch = "wasm32"))]
|
||||||
|
pub mod backend_tungstenite;
|
||||||
|
pub mod events;
|
||||||
pub mod gateway;
|
pub mod gateway;
|
||||||
pub mod handle;
|
pub mod handle;
|
||||||
pub mod heartbeat;
|
pub mod heartbeat;
|
||||||
|
@ -11,28 +16,18 @@ pub use message::*;
|
||||||
use crate::errors::GatewayError;
|
use crate::errors::GatewayError;
|
||||||
use crate::types::{Snowflake, WebSocketEvent};
|
use crate::types::{Snowflake, WebSocketEvent};
|
||||||
|
|
||||||
use async_trait::async_trait;
|
|
||||||
use std::any::Any;
|
use std::any::Any;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::fmt::Debug;
|
|
||||||
use std::sync::{Arc, RwLock};
|
use std::sync::{Arc, RwLock};
|
||||||
use std::time::Duration;
|
|
||||||
use tokio::time::sleep_until;
|
|
||||||
|
|
||||||
use futures_util::stream::SplitSink;
|
|
||||||
use futures_util::stream::SplitStream;
|
|
||||||
use futures_util::SinkExt;
|
|
||||||
use futures_util::StreamExt;
|
|
||||||
use log::{info, trace, warn};
|
|
||||||
use tokio::net::TcpStream;
|
|
||||||
use tokio::sync::mpsc::Sender;
|
|
||||||
use tokio::sync::Mutex;
|
use tokio::sync::Mutex;
|
||||||
use tokio::task;
|
|
||||||
use tokio::task::JoinHandle;
|
#[cfg(not(target_arch = "wasm32"))]
|
||||||
use tokio::time;
|
pub type WsSink = backend_tungstenite::WsSink;
|
||||||
use tokio::time::Instant;
|
#[cfg(not(target_arch = "wasm32"))]
|
||||||
use tokio_tungstenite::MaybeTlsStream;
|
pub type WsStream = backend_tungstenite::WsStream;
|
||||||
use tokio_tungstenite::{connect_async_tls_with_config, Connector, WebSocketStream};
|
#[cfg(not(target_arch = "wasm32"))]
|
||||||
|
pub type WebSocketBackend = backend_tungstenite::WebSocketBackend;
|
||||||
|
|
||||||
// Gateway opcodes
|
// Gateway opcodes
|
||||||
/// Opcode received when the server dispatches a [crate::types::WebSocketEvent]
|
/// Opcode received when the server dispatches a [crate::types::WebSocketEvent]
|
||||||
|
@ -134,54 +129,3 @@ impl<T: WebSocketEvent> GatewayEvent<T> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod example {
|
|
||||||
use crate::types;
|
|
||||||
|
|
||||||
use super::*;
|
|
||||||
use std::sync::atomic::{AtomicI32, Ordering::Relaxed};
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
struct Consumer {
|
|
||||||
_name: String,
|
|
||||||
events_received: AtomicI32,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[async_trait]
|
|
||||||
impl Observer<types::GatewayResume> for Consumer {
|
|
||||||
async fn update(&self, _data: &types::GatewayResume) {
|
|
||||||
self.events_received.fetch_add(1, Relaxed);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[tokio::test]
|
|
||||||
async fn test_observer_behavior() {
|
|
||||||
let mut event = GatewayEvent::default();
|
|
||||||
|
|
||||||
let new_data = types::GatewayResume {
|
|
||||||
token: "token_3276ha37am3".to_string(),
|
|
||||||
session_id: "89346671230".to_string(),
|
|
||||||
seq: "3".to_string(),
|
|
||||||
};
|
|
||||||
|
|
||||||
let consumer = Arc::new(Consumer {
|
|
||||||
_name: "first".into(),
|
|
||||||
events_received: 0.into(),
|
|
||||||
});
|
|
||||||
event.subscribe(consumer.clone());
|
|
||||||
|
|
||||||
let second_consumer = Arc::new(Consumer {
|
|
||||||
_name: "second".into(),
|
|
||||||
events_received: 0.into(),
|
|
||||||
});
|
|
||||||
event.subscribe(second_consumer.clone());
|
|
||||||
|
|
||||||
event.notify(new_data.clone()).await;
|
|
||||||
event.unsubscribe(&*consumer);
|
|
||||||
event.notify(new_data).await;
|
|
||||||
|
|
||||||
assert_eq!(consumer.events_received.load(Relaxed), 1);
|
|
||||||
assert_eq!(second_consumer.events_received.load(Relaxed), 2);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -36,14 +36,11 @@ impl Instance {
|
||||||
pub async fn new(urls: UrlBundle, limited: bool) -> ChorusResult<Instance> {
|
pub async fn new(urls: UrlBundle, limited: bool) -> ChorusResult<Instance> {
|
||||||
let limits_information;
|
let limits_information;
|
||||||
if limited {
|
if limited {
|
||||||
let limits_configuration =
|
let limits_configuration = ChorusRequest::get_limits_config(&urls.api).await?.rate;
|
||||||
Some(ChorusRequest::get_limits_config(&urls.api).await?.rate);
|
let limits = ChorusRequest::limits_config_to_hashmap(&limits_configuration);
|
||||||
let limits = Some(ChorusRequest::limits_config_to_hashmap(
|
|
||||||
limits_configuration.as_ref().unwrap(),
|
|
||||||
));
|
|
||||||
limits_information = Some(LimitsInformation {
|
limits_information = Some(LimitsInformation {
|
||||||
ratelimits: limits.unwrap(),
|
ratelimits: limits,
|
||||||
configuration: limits_configuration.unwrap(),
|
configuration: limits_configuration,
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
limits_information = None;
|
limits_information = None;
|
||||||
|
@ -138,7 +135,7 @@ impl ChorusUser {
|
||||||
let object = Arc::new(RwLock::new(User::default()));
|
let object = Arc::new(RwLock::new(User::default()));
|
||||||
let wss_url = instance.read().unwrap().urls.wss.clone();
|
let wss_url = instance.read().unwrap().urls.wss.clone();
|
||||||
// Dummy gateway object
|
// Dummy gateway object
|
||||||
let gateway = Gateway::new(wss_url).await.unwrap();
|
let gateway = Gateway::spawn(wss_url).await.unwrap();
|
||||||
ChorusUser {
|
ChorusUser {
|
||||||
token,
|
token,
|
||||||
belongs_to: instance.clone(),
|
belongs_to: instance.clone(),
|
||||||
|
|
|
@ -14,6 +14,8 @@
|
||||||
clippy::new_without_default,
|
clippy::new_without_default,
|
||||||
clippy::useless_conversion
|
clippy::useless_conversion
|
||||||
)]
|
)]
|
||||||
|
#[cfg(all(feature = "rt", feature = "rt_multi_thread"))]
|
||||||
|
compile_error!("feature \"rt\" and feature \"rt_multi_thread\" cannot be enabled at the same time");
|
||||||
|
|
||||||
use url::{ParseError, Url};
|
use url::{ParseError, Url};
|
||||||
|
|
||||||
|
|
|
@ -11,6 +11,20 @@ pub struct SentryConfiguration {
|
||||||
pub environment: String,
|
pub environment: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl SentryConfiguration {
|
||||||
|
#[cfg(not(target_arch = "wasm32"))]
|
||||||
|
fn get_hostname() -> std::io::Result<OsString> {
|
||||||
|
hostname::get()
|
||||||
|
}
|
||||||
|
#[cfg(target_arch = "wasm32")]
|
||||||
|
fn get_hostname() -> std::io::Result<OsString> {
|
||||||
|
Err(std::io::Error::new(
|
||||||
|
std::io::ErrorKind::Unsupported,
|
||||||
|
"Unsupported: wasm targets do not have a hostname",
|
||||||
|
))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl Default for SentryConfiguration {
|
impl Default for SentryConfiguration {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self {
|
Self {
|
||||||
|
@ -19,7 +33,7 @@ impl Default for SentryConfiguration {
|
||||||
"https://241c6fb08adb469da1bb82522b25c99f@sentry.quartzinc.space/3",
|
"https://241c6fb08adb469da1bb82522b25c99f@sentry.quartzinc.space/3",
|
||||||
),
|
),
|
||||||
trace_sample_rate: 1.0,
|
trace_sample_rate: 1.0,
|
||||||
environment: hostname::get()
|
environment: SentryConfiguration::get_hostname()
|
||||||
.unwrap_or_else(|_| OsString::new())
|
.unwrap_or_else(|_| OsString::new())
|
||||||
.to_string_lossy()
|
.to_string_lossy()
|
||||||
.to_string(),
|
.to_string(),
|
||||||
|
|
|
@ -15,7 +15,10 @@ use crate::types::{
|
||||||
use crate::types::Composite;
|
use crate::types::Composite;
|
||||||
|
|
||||||
#[cfg(feature = "client")]
|
#[cfg(feature = "client")]
|
||||||
use crate::gateway::{GatewayHandle, Updateable};
|
use crate::gateway::GatewayHandle;
|
||||||
|
|
||||||
|
#[cfg(feature = "client")]
|
||||||
|
use crate::gateway::Updateable;
|
||||||
|
|
||||||
#[cfg(feature = "client")]
|
#[cfg(feature = "client")]
|
||||||
use chorus_macros::{observe_option_vec, Composite, Updateable};
|
use chorus_macros::{observe_option_vec, Composite, Updateable};
|
||||||
|
|
|
@ -6,11 +6,14 @@ use serde::{Deserialize, Serialize};
|
||||||
use crate::types::entities::User;
|
use crate::types::entities::User;
|
||||||
use crate::types::Snowflake;
|
use crate::types::Snowflake;
|
||||||
|
|
||||||
|
#[cfg(feature = "client")]
|
||||||
|
use crate::gateway::GatewayHandle;
|
||||||
|
|
||||||
#[cfg(feature = "client")]
|
#[cfg(feature = "client")]
|
||||||
use crate::types::Composite;
|
use crate::types::Composite;
|
||||||
|
|
||||||
#[cfg(feature = "client")]
|
#[cfg(feature = "client")]
|
||||||
use crate::gateway::{GatewayHandle, Updateable};
|
use crate::gateway::Updateable;
|
||||||
|
|
||||||
#[cfg(feature = "client")]
|
#[cfg(feature = "client")]
|
||||||
use chorus_macros::{Composite, Updateable};
|
use chorus_macros::{Composite, Updateable};
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
use std::fmt::Debug;
|
use std::fmt::Debug;
|
||||||
use std::sync::{Arc, RwLock};
|
use std::sync::{Arc, RwLock};
|
||||||
|
|
||||||
|
use bitflags::bitflags;
|
||||||
use chrono::{DateTime, Utc};
|
use chrono::{DateTime, Utc};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use serde_repr::{Deserialize_repr, Serialize_repr};
|
use serde_repr::{Deserialize_repr, Serialize_repr};
|
||||||
|
@ -11,12 +12,11 @@ use crate::types::{
|
||||||
interfaces::WelcomeScreenObject,
|
interfaces::WelcomeScreenObject,
|
||||||
utils::Snowflake,
|
utils::Snowflake,
|
||||||
};
|
};
|
||||||
use bitflags::bitflags;
|
|
||||||
|
|
||||||
use super::PublicUser;
|
use super::PublicUser;
|
||||||
|
|
||||||
#[cfg(feature = "client")]
|
#[cfg(feature = "client")]
|
||||||
use crate::gateway::{GatewayHandle, Updateable};
|
use crate::gateway::Updateable;
|
||||||
|
|
||||||
#[cfg(feature = "client")]
|
#[cfg(feature = "client")]
|
||||||
use chorus_macros::{observe_option_vec, observe_vec, Composite, Updateable};
|
use chorus_macros::{observe_option_vec, observe_vec, Composite, Updateable};
|
||||||
|
@ -24,6 +24,9 @@ use chorus_macros::{observe_option_vec, observe_vec, Composite, Updateable};
|
||||||
#[cfg(feature = "client")]
|
#[cfg(feature = "client")]
|
||||||
use crate::types::Composite;
|
use crate::types::Composite;
|
||||||
|
|
||||||
|
#[cfg(feature = "client")]
|
||||||
|
use crate::gateway::GatewayHandle;
|
||||||
|
|
||||||
/// See <https://discord.com/developers/docs/resources/guild>
|
/// See <https://discord.com/developers/docs/resources/guild>
|
||||||
#[derive(Serialize, Deserialize, Debug, Default, Clone)]
|
#[derive(Serialize, Deserialize, Debug, Default, Clone)]
|
||||||
#[cfg_attr(feature = "client", derive(Updateable, Composite))]
|
#[cfg_attr(feature = "client", derive(Updateable, Composite))]
|
||||||
|
|
|
@ -24,7 +24,10 @@ pub use voice_state::*;
|
||||||
pub use webhook::*;
|
pub use webhook::*;
|
||||||
|
|
||||||
#[cfg(feature = "client")]
|
#[cfg(feature = "client")]
|
||||||
use crate::gateway::{GatewayHandle, Updateable};
|
use crate::gateway::Updateable;
|
||||||
|
|
||||||
|
#[cfg(feature = "client")]
|
||||||
|
use crate::gateway::GatewayHandle;
|
||||||
|
|
||||||
#[cfg(feature = "client")]
|
#[cfg(feature = "client")]
|
||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
|
|
|
@ -9,11 +9,14 @@ use crate::types::utils::Snowflake;
|
||||||
use chorus_macros::{Composite, Updateable};
|
use chorus_macros::{Composite, Updateable};
|
||||||
|
|
||||||
#[cfg(feature = "client")]
|
#[cfg(feature = "client")]
|
||||||
use crate::gateway::{GatewayHandle, Updateable};
|
use crate::gateway::Updateable;
|
||||||
|
|
||||||
#[cfg(feature = "client")]
|
#[cfg(feature = "client")]
|
||||||
use crate::types::Composite;
|
use crate::types::Composite;
|
||||||
|
|
||||||
|
#[cfg(feature = "client")]
|
||||||
|
use crate::gateway::GatewayHandle;
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug, Default, Clone, PartialEq)]
|
#[derive(Serialize, Deserialize, Debug, Default, Clone, PartialEq)]
|
||||||
#[cfg_attr(feature = "client", derive(Updateable, Composite))]
|
#[cfg_attr(feature = "client", derive(Updateable, Composite))]
|
||||||
#[cfg_attr(feature = "sqlx", derive(sqlx::FromRow))]
|
#[cfg_attr(feature = "sqlx", derive(sqlx::FromRow))]
|
||||||
|
|
|
@ -5,11 +5,14 @@ use serde_aux::prelude::deserialize_option_number_from_string;
|
||||||
use std::fmt::Debug;
|
use std::fmt::Debug;
|
||||||
|
|
||||||
#[cfg(feature = "client")]
|
#[cfg(feature = "client")]
|
||||||
use crate::gateway::{GatewayHandle, Updateable};
|
use crate::gateway::Updateable;
|
||||||
|
|
||||||
#[cfg(feature = "client")]
|
#[cfg(feature = "client")]
|
||||||
use crate::types::Composite;
|
use crate::types::Composite;
|
||||||
|
|
||||||
|
#[cfg(feature = "client")]
|
||||||
|
use crate::gateway::GatewayHandle;
|
||||||
|
|
||||||
#[cfg(feature = "client")]
|
#[cfg(feature = "client")]
|
||||||
use chorus_macros::{Composite, Updateable};
|
use chorus_macros::{Composite, Updateable};
|
||||||
|
|
||||||
|
|
|
@ -1,13 +1,16 @@
|
||||||
use std::sync::{Arc, RwLock};
|
use std::sync::{Arc, RwLock};
|
||||||
|
|
||||||
#[cfg(feature = "client")]
|
#[cfg(feature = "client")]
|
||||||
use chorus_macros::{Composite, Updateable};
|
use chorus_macros::Composite;
|
||||||
|
|
||||||
#[cfg(feature = "client")]
|
#[cfg(feature = "client")]
|
||||||
use crate::types::Composite;
|
use crate::types::Composite;
|
||||||
|
|
||||||
#[cfg(feature = "client")]
|
#[cfg(feature = "client")]
|
||||||
use crate::gateway::{GatewayHandle, Updateable};
|
use crate::gateway::GatewayHandle;
|
||||||
|
|
||||||
|
#[cfg(feature = "client")]
|
||||||
|
use crate::gateway::Updateable;
|
||||||
|
|
||||||
use chrono::{DateTime, Utc};
|
use chrono::{DateTime, Utc};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
|
@ -4,7 +4,7 @@ use std::sync::{Arc, RwLock};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
#[cfg(feature = "client")]
|
#[cfg(feature = "client")]
|
||||||
use crate::gateway::{GatewayHandle, Updateable};
|
use crate::gateway::Updateable;
|
||||||
|
|
||||||
#[cfg(feature = "client")]
|
#[cfg(feature = "client")]
|
||||||
use chorus_macros::{Composite, Updateable};
|
use chorus_macros::{Composite, Updateable};
|
||||||
|
@ -12,6 +12,9 @@ use chorus_macros::{Composite, Updateable};
|
||||||
#[cfg(feature = "client")]
|
#[cfg(feature = "client")]
|
||||||
use crate::types::Composite;
|
use crate::types::Composite;
|
||||||
|
|
||||||
|
#[cfg(feature = "client")]
|
||||||
|
use crate::gateway::GatewayHandle;
|
||||||
|
|
||||||
use crate::types::{
|
use crate::types::{
|
||||||
entities::{Guild, User},
|
entities::{Guild, User},
|
||||||
utils::Snowflake,
|
utils::Snowflake,
|
||||||
|
|
|
@ -43,7 +43,9 @@ impl TestBundle {
|
||||||
limits: self.user.limits.clone(),
|
limits: self.user.limits.clone(),
|
||||||
settings: self.user.settings.clone(),
|
settings: self.user.settings.clone(),
|
||||||
object: self.user.object.clone(),
|
object: self.user.object.clone(),
|
||||||
gateway: Gateway::new(self.instance.urls.wss.clone()).await.unwrap(),
|
gateway: Gateway::spawn(self.instance.urls.wss.clone())
|
||||||
|
.await
|
||||||
|
.unwrap(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,7 +10,7 @@ use chorus::types::{self, ChannelModifySchema, RoleCreateModifySchema, RoleObjec
|
||||||
async fn test_gateway_establish() {
|
async fn test_gateway_establish() {
|
||||||
let bundle = common::setup().await;
|
let bundle = common::setup().await;
|
||||||
|
|
||||||
Gateway::new(bundle.urls.wss.clone()).await.unwrap();
|
let _: GatewayHandle = Gateway::spawn(bundle.urls.wss.clone()).await.unwrap();
|
||||||
common::teardown(bundle).await
|
common::teardown(bundle).await
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -19,7 +19,7 @@ async fn test_gateway_establish() {
|
||||||
async fn test_gateway_authenticate() {
|
async fn test_gateway_authenticate() {
|
||||||
let bundle = common::setup().await;
|
let bundle = common::setup().await;
|
||||||
|
|
||||||
let gateway = Gateway::new(bundle.urls.wss.clone()).await.unwrap();
|
let gateway: GatewayHandle = Gateway::spawn(bundle.urls.wss.clone()).await.unwrap();
|
||||||
|
|
||||||
let mut identify = types::GatewayIdentifyPayload::common();
|
let mut identify = types::GatewayIdentifyPayload::common();
|
||||||
identify.token = bundle.user.token.clone();
|
identify.token = bundle.user.token.clone();
|
||||||
|
|
Loading…
Reference in New Issue