Give `GatewayHandle` and `Gateway` common trait to call `watch_whole()` or observe() from a `Gateway`
This commit is contained in:
parent
2b21933cb3
commit
d36b1de170
|
@ -105,7 +105,7 @@ pub fn composite_derive(input: TokenStream) -> TokenStream {
|
|||
let expanded = quote! {
|
||||
#[async_trait::async_trait(?Send)]
|
||||
impl<T: Updateable + Clone> Composite<T> for #ident {
|
||||
async fn watch_whole(self, gateway: &GatewayHandle) -> Self {
|
||||
async fn watch_whole(self, gateway: &(impl GatewayObject + ?Sized)) -> Self {
|
||||
Self {
|
||||
#(#field_exprs,)*
|
||||
}
|
||||
|
|
|
@ -180,32 +180,16 @@ pub trait Updateable: 'static + Send + Sync {
|
|||
fn id(&self) -> Snowflake;
|
||||
}
|
||||
|
||||
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,
|
||||
event_data: Some(to_send),
|
||||
sequence_number: None,
|
||||
};
|
||||
|
||||
let payload_json = serde_json::to_string(&gateway_payload).unwrap();
|
||||
|
||||
let message = tokio_tungstenite::tungstenite::Message::text(payload_json);
|
||||
|
||||
self.websocket_send
|
||||
.lock()
|
||||
.await
|
||||
.send(message)
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
pub async fn observe<T: Updateable + Clone + Composite<T>>(
|
||||
#[async_trait(?Send)]
|
||||
pub trait GatewayObject {
|
||||
fn store(&self) -> Arc<Mutex<HashMap<Snowflake, Box<dyn Send + Any>>>>;
|
||||
fn events(&self) -> Arc<Mutex<Events>>;
|
||||
async fn observe<T: Updateable + Clone + Composite<T>>(
|
||||
&self,
|
||||
object: Arc<RwLock<T>>,
|
||||
) -> watch::Receiver<Arc<RwLock<T>>> {
|
||||
let mut store = self.store.lock().await;
|
||||
let store = self.store();
|
||||
let mut store = store.lock().await;
|
||||
let id = object.read().unwrap().id();
|
||||
if let Some(channel) = store.get(&id) {
|
||||
let (_, rx) = channel
|
||||
|
@ -230,8 +214,7 @@ impl GatewayHandle {
|
|||
receiver
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn observe_and_get<T: Updateable + Clone + Composite<T>>(
|
||||
async fn observe_and_get<T: Updateable + Clone + Composite<T>>(
|
||||
&self,
|
||||
object: Arc<RwLock<T>>,
|
||||
) -> Arc<RwLock<T>> {
|
||||
|
@ -239,6 +222,50 @@ impl GatewayHandle {
|
|||
let object = channel.borrow().clone();
|
||||
object
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait(?Send)]
|
||||
impl GatewayObject for GatewayHandle {
|
||||
fn store(&self) -> Arc<Mutex<HashMap<Snowflake, Box<dyn Send + Any>>>> {
|
||||
self.store.clone()
|
||||
}
|
||||
|
||||
fn events(&self) -> Arc<Mutex<Events>> {
|
||||
self.events.clone()
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait(?Send)]
|
||||
impl GatewayObject for Gateway {
|
||||
fn store(&self) -> Arc<Mutex<HashMap<Snowflake, Box<dyn Send + Any>>>> {
|
||||
self.store.clone()
|
||||
}
|
||||
|
||||
fn events(&self) -> Arc<Mutex<Events>> {
|
||||
self.events.clone()
|
||||
}
|
||||
}
|
||||
|
||||
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,
|
||||
event_data: Some(to_send),
|
||||
sequence_number: None,
|
||||
};
|
||||
|
||||
let payload_json = serde_json::to_string(&gateway_payload).unwrap();
|
||||
|
||||
let message = tokio_tungstenite::tungstenite::Message::text(payload_json);
|
||||
|
||||
self.websocket_send
|
||||
.lock()
|
||||
.await
|
||||
.send(message)
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
/// Sends an identify event to the gateway
|
||||
pub async fn send_identify(&self, to_send: types::GatewayIdentifyPayload) {
|
||||
|
|
|
@ -6,7 +6,7 @@ use serde::{Deserialize, Serialize};
|
|||
use serde_aux::prelude::deserialize_string_from_number;
|
||||
use serde_repr::{Deserialize_repr, Serialize_repr};
|
||||
|
||||
use crate::gateway::{GatewayHandle, Updateable};
|
||||
use crate::gateway::{GatewayObject, Updateable};
|
||||
use crate::types::{
|
||||
entities::{GuildMember, User},
|
||||
utils::Snowflake,
|
||||
|
|
|
@ -3,7 +3,7 @@ use std::sync::{Arc, RwLock};
|
|||
use chorus_macros::{Composite, Updateable};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::gateway::{GatewayHandle, Updateable};
|
||||
use crate::gateway::{GatewayObject, Updateable};
|
||||
use crate::types::entities::User;
|
||||
use crate::types::{Composite, Snowflake};
|
||||
|
||||
|
|
|
@ -5,7 +5,7 @@ use chrono::{DateTime, Utc};
|
|||
use serde::{Deserialize, Serialize};
|
||||
use serde_repr::{Deserialize_repr, Serialize_repr};
|
||||
|
||||
use crate::gateway::{GatewayHandle, Updateable};
|
||||
use crate::gateway::{GatewayObject, Updateable};
|
||||
use crate::types::types::guild_configuration::GuildFeaturesList;
|
||||
use crate::types::{
|
||||
entities::{Channel, Emoji, RoleObject, Sticker, User, VoiceState, Webhook},
|
||||
|
|
|
@ -22,7 +22,7 @@ pub use user_settings::*;
|
|||
pub use voice_state::*;
|
||||
pub use webhook::*;
|
||||
|
||||
use crate::gateway::{GatewayHandle, Updateable};
|
||||
use crate::gateway::{GatewayObject, Updateable};
|
||||
use async_trait::async_trait;
|
||||
use std::sync::{Arc, RwLock};
|
||||
|
||||
|
@ -52,11 +52,11 @@ mod webhook;
|
|||
|
||||
#[async_trait(?Send)]
|
||||
pub trait Composite<T: Updateable + Clone> {
|
||||
async fn watch_whole(self, gateway: &GatewayHandle) -> Self;
|
||||
async fn watch_whole(self, gateway: &(impl GatewayObject + ?Sized)) -> Self;
|
||||
|
||||
async fn option_observe_fn(
|
||||
value: Option<Arc<RwLock<T>>>,
|
||||
gateway: &GatewayHandle,
|
||||
gateway: &(impl GatewayObject + ?Sized),
|
||||
) -> Option<Arc<RwLock<T>>>
|
||||
where
|
||||
T: Composite<T>,
|
||||
|
@ -71,7 +71,7 @@ pub trait Composite<T: Updateable + Clone> {
|
|||
|
||||
async fn option_vec_observe_fn(
|
||||
value: Option<Vec<Arc<RwLock<T>>>>,
|
||||
gateway: &GatewayHandle,
|
||||
gateway: &(impl GatewayObject + ?Sized),
|
||||
) -> Option<Vec<Arc<RwLock<T>>>>
|
||||
where
|
||||
T: Composite<T>,
|
||||
|
@ -87,7 +87,10 @@ pub trait Composite<T: Updateable + Clone> {
|
|||
}
|
||||
}
|
||||
|
||||
async fn value_observe_fn(value: Arc<RwLock<T>>, gateway: &GatewayHandle) -> Arc<RwLock<T>>
|
||||
async fn value_observe_fn(
|
||||
value: Arc<RwLock<T>>,
|
||||
gateway: &(impl GatewayObject + ?Sized),
|
||||
) -> Arc<RwLock<T>>
|
||||
where
|
||||
T: Composite<T>,
|
||||
{
|
||||
|
@ -96,7 +99,7 @@ pub trait Composite<T: Updateable + Clone> {
|
|||
|
||||
async fn vec_observe_fn(
|
||||
value: Vec<Arc<RwLock<T>>>,
|
||||
gateway: &GatewayHandle,
|
||||
gateway: &(impl GatewayObject + ?Sized),
|
||||
) -> Vec<Arc<RwLock<T>>>
|
||||
where
|
||||
T: Composite<T>,
|
||||
|
|
|
@ -3,7 +3,7 @@ use chorus_macros::{Composite, Updateable};
|
|||
use serde::{Deserialize, Serialize};
|
||||
use serde_aux::prelude::{deserialize_option_number_from_string, deserialize_string_from_number};
|
||||
|
||||
use crate::gateway::{GatewayHandle, Updateable};
|
||||
use crate::gateway::{GatewayObject, Updateable};
|
||||
use crate::types::{utils::Snowflake, Composite};
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, Default, Clone, PartialEq, Updateable, Composite)]
|
||||
|
|
|
@ -3,7 +3,7 @@ use chrono::{DateTime, Utc};
|
|||
use serde::{Deserialize, Serialize};
|
||||
use serde_aux::prelude::deserialize_option_number_from_string;
|
||||
|
||||
use crate::gateway::{GatewayHandle, Updateable};
|
||||
use crate::gateway::{GatewayObject, Updateable};
|
||||
use crate::types::{utils::Snowflake, Composite};
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
|
||||
|
|
|
@ -4,7 +4,7 @@ use chorus_macros::{Composite, Updateable};
|
|||
use chrono::{DateTime, Utc};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::gateway::{GatewayHandle, Updateable};
|
||||
use crate::gateway::{GatewayObject, Updateable};
|
||||
use crate::types::{
|
||||
entities::{Guild, GuildMember},
|
||||
utils::Snowflake,
|
||||
|
|
|
@ -3,7 +3,7 @@ use std::sync::{Arc, RwLock};
|
|||
use chorus_macros::{Composite, Updateable};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::gateway::{GatewayHandle, Updateable};
|
||||
use crate::gateway::{GatewayObject, Updateable};
|
||||
use crate::types::{
|
||||
entities::{Guild, User},
|
||||
utils::Snowflake,
|
||||
|
|
|
@ -1,13 +1,17 @@
|
|||
use std::sync::{Arc, RwLock};
|
||||
|
||||
use chorus_macros::JsonField;
|
||||
use chrono::{DateTime, Utc};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::types::entities::{Guild, PublicUser, UnavailableGuild};
|
||||
use crate::types::events::WebSocketEvent;
|
||||
use crate::types::{
|
||||
AuditLogEntry, Emoji, GuildMember, GuildScheduledEvent, RoleObject, Snowflake, Sticker,
|
||||
AuditLogEntry, Emoji, GuildMember, GuildScheduledEvent, JsonField, RoleObject, Snowflake,
|
||||
Sticker,
|
||||
};
|
||||
|
||||
use super::PresenceUpdate;
|
||||
use super::{PresenceUpdate, UpdateMessage};
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize, Default, Clone)]
|
||||
/// See <https://discord.com/developers/docs/topics/gateway-events#guild-create>;
|
||||
|
@ -164,15 +168,34 @@ pub struct GuildMembersChunk {
|
|||
|
||||
impl WebSocketEvent for GuildMembersChunk {}
|
||||
|
||||
#[derive(Debug, Default, Deserialize, Serialize, Clone)]
|
||||
#[derive(Debug, Default, Deserialize, Serialize, Clone, JsonField)]
|
||||
/// See <https://discord.com/developers/docs/topics/gateway-events#guild-role-create>
|
||||
pub struct GuildRoleCreate {
|
||||
pub guild_id: Snowflake,
|
||||
pub role: RoleObject,
|
||||
#[serde(skip)]
|
||||
pub json: String,
|
||||
}
|
||||
|
||||
impl WebSocketEvent for GuildRoleCreate {}
|
||||
|
||||
impl UpdateMessage<Guild> for GuildRoleCreate {
|
||||
fn id(&self) -> Snowflake {
|
||||
self.role.id
|
||||
}
|
||||
|
||||
fn update(&mut self, object_to_update: std::sync::Arc<std::sync::RwLock<Guild>>) {
|
||||
let mut write = object_to_update.write().unwrap();
|
||||
if write.roles.is_some() {
|
||||
write
|
||||
.roles
|
||||
.as_mut()
|
||||
.unwrap()
|
||||
.push(Arc::new(RwLock::new(self.role.clone())));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Default, Deserialize, Serialize, Clone)]
|
||||
/// See <https://discord.com/developers/docs/topics/gateway-events#guild-role-update>
|
||||
pub struct GuildRoleUpdate {
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
mod common;
|
||||
|
||||
use chorus::gateway::*;
|
||||
use chorus::types::{self, Channel, ChannelModifySchema};
|
||||
use chorus::types::{self, Channel, ChannelModifySchema, Guild};
|
||||
|
||||
#[tokio::test]
|
||||
/// Tests establishing a connection (hello and heartbeats) on the local gateway;
|
||||
|
@ -63,3 +63,6 @@ async fn test_self_updating_structs() {
|
|||
|
||||
common::teardown(bundle).await
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_recursive_self_updating_structs() {}
|
||||
|
|
Loading…
Reference in New Issue