Skip to content

Commit

Permalink
Version 0.14.0 (#463)
Browse files Browse the repository at this point in the history
## Fixes

- Fix gateway heartbeat on WASM: #460

## Changes

- Update `GuildDefaults` to use new enums
- Make new enums derive `sqlx::Type`
- Feature lock UpdateMessage implementations

## Package changes

- Bump package version, especially reqwest from 0.11.22->0.11.23 -
removes need for custom git branch in Cargo.toml
  • Loading branch information
bitfl0wer authored Jan 19, 2024
2 parents 75f3360 + 65d9de8 commit bc2dbb8
Show file tree
Hide file tree
Showing 8 changed files with 218 additions and 197 deletions.
302 changes: 147 additions & 155 deletions Cargo.lock

Large diffs are not rendered by default.

50 changes: 25 additions & 25 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,32 +18,32 @@ rt = ["tokio/rt"]
client = []

[dependencies]
tokio = { version = "1.34.0", features = ["macros", "sync"] }
serde = { version = "1.0.188", features = ["derive", "rc"] }
serde_json = { version = "1.0.105", features = ["raw_value"] }
serde-aux = "4.2.0"
serde_with = "3.3.0"
serde_repr = "0.1.16"
reqwest = { git = "https://github.com/bitfl0wer/reqwest.git", branch = "wasm-headers", features = [
tokio = { version = "1.35.1", features = ["macros", "sync"] }
serde = { version = "1.0.195", features = ["derive", "rc"] }
serde_json = { version = "1.0.111", features = ["raw_value"] }
serde-aux = "4.3.1"
serde_with = "3.4.0"
serde_repr = "0.1.18"
reqwest = { features = [
"multipart",
"json",
], version = "0.11.22" } # reqwest versions > 0.11.22 will have adequate support for WASM. Until there is such a version, we will use a fork of reqwest v.0.11.22
url = "2.4.0"
chrono = { version = "0.4.26", features = ["serde"] }
regex = "1.9.4"
], version = "0.11.23" }
url = "2.5.0"
chrono = { version = "0.4.31", features = ["serde"] }
regex = "1.10.2"
custom_error = "1.9.2"
futures-util = "0.3.28"
http = "0.2.9"
base64 = "0.21.3"
bitflags = { version = "2.4.0", features = ["serde"] }
futures-util = "0.3.30"
http = "0.2.11"
base64 = "0.21.7"
bitflags = { version = "2.4.1", features = ["serde"] }
lazy_static = "1.4.0"
poem = { version = "1.3.57", optional = true }
thiserror = "1.0.47"
poem = { version = "1.3.59", optional = true }
thiserror = "1.0.56"
jsonwebtoken = "8.3.0"
log = "0.4.20"
async-trait = "0.1.73"
async-trait = "0.1.77"
chorus-macros = "0.2.0"
sqlx = { version = "0.7.1", features = [
sqlx = { version = "0.7.3", features = [
"mysql",
"sqlite",
"json",
Expand All @@ -52,11 +52,10 @@ sqlx = { version = "0.7.1", features = [
"runtime-tokio-native-tls",
"any",
], optional = true }
safina-timer = "0.1.11"
rand = "0.8.5"

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
rustls = "0.21.8"
rustls = "0.21.10"
rustls-native-certs = "0.6.3"
tokio-tungstenite = { version = "0.20.1", features = [
"rustls-tls-native-roots",
Expand All @@ -66,11 +65,12 @@ native-tls = "0.2.11"
hostname = "0.3.1"

[target.'cfg(target_arch = "wasm32")'.dependencies]
getrandom = { version = "0.2.11", features = ["js"] }
getrandom = { version = "0.2.12", features = ["js"] }
ws_stream_wasm = "0.7.4"
wasm-bindgen-futures = "0.4.38"
wasm-bindgen-futures = "0.4.39"
wasmtimer = "0.2.0"

[dev-dependencies]
lazy_static = "1.4.0"
wasm-bindgen-test = "0.3.38"
wasm-bindgen = "0.2.88"
wasm-bindgen-test = "0.3.39"
wasm-bindgen = "0.2.89"
8 changes: 6 additions & 2 deletions examples/gateway_observers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ use chorus::{
use std::{sync::Arc, time::Duration};
use tokio::{self};

#[cfg(not(target_arch = "wasm32"))]
use tokio::time::sleep;
#[cfg(target_arch = "wasm32")]
use wasmtimer::tokio::sleep;

// This example creates a simple gateway connection and a basic observer struct

// Due to certain limitations all observers must impl debug
Expand Down Expand Up @@ -54,10 +59,9 @@ async fn main() {
let mut identify = GatewayIdentifyPayload::common();
identify.token = token;
gateway.send_identify(identify).await;
safina_timer::start_timer_thread();

// Do something on the main thread so we don't quit
loop {
safina_timer::sleep_for(Duration::MAX).await
sleep(Duration::from_secs(3600)).await;
}
}
13 changes: 9 additions & 4 deletions examples/gateway_simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,19 @@ use std::time::Duration;
use chorus::gateway::Gateway;
use chorus::{self, types::GatewayIdentifyPayload};

#[cfg(not(target_arch = "wasm32"))]
use tokio::time::sleep;
#[cfg(target_arch = "wasm32")]
use wasmtimer::tokio::sleep;

/// This example creates a simple gateway connection and a session with an Identify event
#[tokio::main(flavor = "current_thread")]
async fn main() {
// 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();

// Initiate the gateway connection, starting a listener in one thread and a heartbeat handler in another
let _ = Gateway::spawn(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

Expand All @@ -26,10 +31,10 @@ async fn main() {
identify.token = token;

// Send off the event
safina_timer::start_timer_thread();

gateway.send_identify(identify).await;
// Do something on the main thread so we don't quit
loop {
safina_timer::sleep_for(Duration::MAX).await
sleep(Duration::from_secs(3600)).await;
}
}
23 changes: 16 additions & 7 deletions src/gateway/heartbeat.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
use futures_util::SinkExt;
use log::*;
use std::time::{self, Duration, Instant};

#[cfg(not(target_arch = "wasm32"))]
use tokio::time::Instant;
#[cfg(target_arch = "wasm32")]
use wasmtimer::std::Instant;

#[cfg(not(target_arch = "wasm32"))]
use tokio::time::sleep_until;
#[cfg(target_arch = "wasm32")]
use wasmtimer::tokio::sleep_until;

use std::time::Duration;

use tokio::sync::mpsc::{Receiver, Sender};

use safina_timer::sleep_until;
#[cfg(not(target_arch = "wasm32"))]
use tokio::task;

Expand Down Expand Up @@ -57,12 +68,10 @@ impl HeartbeatHandler {
mut receive: Receiver<HeartbeatThreadCommunication>,
mut kill_receive: tokio::sync::broadcast::Receiver<()>,
) {
let mut last_heartbeat_timestamp: Instant = time::Instant::now();
let mut last_heartbeat_timestamp: Instant = Instant::now();
let mut last_heartbeat_acknowledged = true;
let mut last_seq_number: Option<u64> = None;

safina_timer::start_timer_thread();


loop {
if kill_receive.try_recv().is_ok() {
trace!("GW: Closing heartbeat task");
Expand Down Expand Up @@ -123,7 +132,7 @@ impl HeartbeatHandler {
break;
}

last_heartbeat_timestamp = time::Instant::now();
last_heartbeat_timestamp = Instant::now();
last_heartbeat_acknowledged = false;
}
}
Expand Down
10 changes: 6 additions & 4 deletions src/types/config/types/subconfigs/defaults/guild.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
use serde::{Deserialize, Serialize};

use crate::types::{ExplicitContentFilterLevel, MessageNotificationLevel};

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct GuildDefaults {
pub max_presences: u64,
pub max_video_channel_users: u16,
pub afk_timeout: u16,
pub default_message_notifications: u8,
pub explicit_content_filter: u8,
pub default_message_notifications: MessageNotificationLevel,
pub explicit_content_filter: ExplicitContentFilterLevel,
}

impl Default for GuildDefaults {
Expand All @@ -16,8 +18,8 @@ impl Default for GuildDefaults {
max_presences: 250_000,
max_video_channel_users: 200,
afk_timeout: 300,
default_message_notifications: 1,
explicit_content_filter: 0,
default_message_notifications: MessageNotificationLevel::OnlyMentions,
explicit_content_filter: ExplicitContentFilterLevel::Disabled,
}
}
}
6 changes: 6 additions & 0 deletions src/types/entities/guild.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,7 @@ pub struct VoiceRegion {
}

#[derive(Serialize_repr, Deserialize_repr, Debug, Default, Clone, Eq, PartialEq, Hash, Copy)]
#[cfg_attr(feature = "sqlx", derive(sqlx::Type))]
#[repr(u8)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
/// See <https://discord-userdoccers.vercel.app/resources/guild#message-notification-level>
Expand All @@ -356,6 +357,7 @@ pub enum MessageNotificationLevel {
}

#[derive(Serialize_repr, Deserialize_repr, Debug, Default, Clone, Eq, PartialEq, Hash, Copy)]
#[cfg_attr(feature = "sqlx", derive(sqlx::Type))]
#[repr(u8)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
/// See <https://discord-userdoccers.vercel.app/resources/guild#explicit-content-filter-level>
Expand All @@ -367,6 +369,7 @@ pub enum ExplicitContentFilterLevel {
}

#[derive(Serialize_repr, Deserialize_repr, Debug, Default, Clone, Eq, PartialEq, Hash, Copy)]
#[cfg_attr(feature = "sqlx", derive(sqlx::Type))]
#[repr(u8)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
/// See <https://discord-userdoccers.vercel.app/resources/guild#verification-level>
Expand All @@ -380,6 +383,7 @@ pub enum VerificationLevel {
}

#[derive(Serialize_repr, Deserialize_repr, Debug, Default, Clone, Eq, PartialEq, Hash, Copy)]
#[cfg_attr(feature = "sqlx", derive(sqlx::Type))]
#[repr(u8)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
/// See <https://discord-userdoccers.vercel.app/resources/guild#verification-level>
Expand All @@ -390,6 +394,7 @@ pub enum MFALevel {
}

#[derive(Serialize_repr, Deserialize_repr, Debug, Default, Clone, Eq, PartialEq, Hash, Copy)]
#[cfg_attr(feature = "sqlx", derive(sqlx::Type))]
#[repr(u8)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
/// See <https://discord-userdoccers.vercel.app/resources/guild#verification-level>
Expand All @@ -402,6 +407,7 @@ pub enum NSFWLevel {
}

#[derive(Serialize_repr, Deserialize_repr, Debug, Default, Clone, Eq, PartialEq, Hash, Copy)]
#[cfg_attr(feature = "sqlx", derive(sqlx::Type))]
#[repr(u8)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
/// See <https://discord-userdoccers.vercel.app/resources/guild#verification-level>
Expand Down
3 changes: 3 additions & 0 deletions src/types/events/guild.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ pub struct GuildCreate {
pub json: String,
}

#[cfg(feature = "client")]
impl UpdateMessage<Guild> for GuildCreate {
fn id(&self) -> Option<Snowflake> {
match &self.d {
Expand Down Expand Up @@ -89,6 +90,7 @@ pub struct GuildUpdate {

impl WebSocketEvent for GuildUpdate {}

#[cfg(feature = "client")]
impl UpdateMessage<Guild> for GuildUpdate {
fn id(&self) -> Option<Snowflake> {
Some(self.guild.id)
Expand All @@ -107,6 +109,7 @@ pub struct GuildDelete {
pub json: String,
}

#[cfg(feature = "client")]
impl UpdateMessage<Guild> for GuildDelete {
fn id(&self) -> Option<Snowflake> {
Some(self.guild.id)
Expand Down

0 comments on commit bc2dbb8

Please sign in to comment.