Add `GatewayHandle` to `UserMeta`

This commit is contained in:
bitfl0wer 2023-07-22 14:38:55 +02:00
parent 24450571de
commit 36ea1fd7b2
No known key found for this signature in database
GPG Key ID: 0ACD574FCF5226CF
2 changed files with 23 additions and 16 deletions

View File

@ -8,6 +8,7 @@ use serde::{Deserialize, Serialize};
use crate::api::{Limit, LimitType}; use crate::api::{Limit, LimitType};
use crate::errors::ChorusResult; use crate::errors::ChorusResult;
use crate::gateway::{Gateway, GatewayHandle};
use crate::ratelimiter::ChorusRequest; use crate::ratelimiter::ChorusRequest;
use crate::types::types::subconfigs::limits::rates::RateLimits; use crate::types::types::subconfigs::limits::rates::RateLimits;
use crate::types::{GeneralConfiguration, User, UserSettings}; use crate::types::{GeneralConfiguration, User, UserSettings};
@ -88,13 +89,14 @@ impl fmt::Display for Token {
} }
} }
#[derive(Debug, Clone)] #[derive(Debug)]
pub struct UserMeta { pub struct UserMeta {
pub belongs_to: Rc<RefCell<Instance>>, pub belongs_to: Rc<RefCell<Instance>>,
pub token: String, pub token: String,
pub limits: Option<HashMap<LimitType, Limit>>, pub limits: Option<HashMap<LimitType, Limit>>,
pub settings: UserSettings, pub settings: UserSettings,
pub object: User, pub object: User,
pub gateway: GatewayHandle,
} }
impl UserMeta { impl UserMeta {
@ -112,6 +114,7 @@ impl UserMeta {
limits: Option<HashMap<LimitType, Limit>>, limits: Option<HashMap<LimitType, Limit>>,
settings: UserSettings, settings: UserSettings,
object: User, object: User,
gateway: GatewayHandle,
) -> UserMeta { ) -> UserMeta {
UserMeta { UserMeta {
belongs_to, belongs_to,
@ -119,19 +122,24 @@ impl UserMeta {
limits, limits,
settings, settings,
object, object,
gateway,
} }
} }
/// Creates a new 'shell' of a user. The user does not exist as an object, and exists so that you have /// Creates a new 'shell' of a user. The user does not exist as an object, and exists so that you have
/// a UserMeta object to make Rate Limited requests with. This is useful in scenarios like /// a UserMeta object to make Rate Limited requests with. This is useful in scenarios like
/// registering or logging in to the Instance, where you do not yet have a User object, but still /// registering or logging in to the Instance, where you do not yet have a User object, but still
/// need to make a RateLimited request. /// need to make a RateLimited request. To use the [`GatewayHandle`], you will have to identify
pub(crate) fn shell(instance: Rc<RefCell<Instance>>, token: String) -> UserMeta { /// first.
pub(crate) async fn shell(instance: Rc<RefCell<Instance>>, token: String) -> UserMeta {
let settings = UserSettings::default(); let settings = UserSettings::default();
let object = User::default(); let object = User::default();
let wss_url = instance.borrow().urls.wss.clone();
// Dummy gateway object
let gateway = Gateway::new(wss_url).await.unwrap();
UserMeta { UserMeta {
belongs_to: instance.clone(),
token, token,
belongs_to: instance.clone(),
limits: instance limits: instance
.borrow() .borrow()
.limits_information .limits_information
@ -139,6 +147,7 @@ impl UserMeta {
.map(|info| info.ratelimits.clone()), .map(|info| info.ratelimits.clone()),
settings, settings,
object, object,
gateway,
} }
} }
} }

View File

@ -8,7 +8,8 @@ use chorus::types::{self, Channel};
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).await.unwrap(); Gateway::new(bundle.urls.wss.clone()).await.unwrap();
common::teardown(bundle).await
} }
#[tokio::test] #[tokio::test]
@ -16,25 +17,21 @@ 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).await.unwrap(); let gateway = Gateway::new(bundle.urls.wss.clone()).await.unwrap();
let mut identify = types::GatewayIdentifyPayload::common(); let mut identify = types::GatewayIdentifyPayload::common();
identify.token = bundle.user.token; identify.token = bundle.user.token.clone();
gateway.send_identify(identify).await; gateway.send_identify(identify).await;
common::teardown(bundle).await
} }
#[tokio::test] #[tokio::test]
async fn test_self_updating_structs() { async fn test_self_updating_structs() {
let mut bundle = common::setup().await; let mut bundle = common::setup().await;
let gateway = Gateway::new(bundle.urls.wss).await.unwrap(); let channel_updater = bundle.user.gateway.observe(bundle.channel.clone()).await;
let mut identify = types::GatewayIdentifyPayload::common(); let received_channel = channel_updater.borrow().clone();
identify.token = bundle.user.token.clone(); assert_eq!(received_channel, bundle.channel);
gateway.send_identify(identify).await;
let channel_receiver = gateway.observe(bundle.channel.clone()).await;
let received_channel = channel_receiver.borrow();
assert_eq!(*received_channel, bundle.channel);
drop(received_channel);
let channel = &mut bundle.channel; let channel = &mut bundle.channel;
let modify_data = types::ChannelModifySchema { let modify_data = types::ChannelModifySchema {
name: Some("beepboop".to_string()), name: Some("beepboop".to_string()),
@ -43,6 +40,7 @@ async fn test_self_updating_structs() {
Channel::modify(channel, modify_data, channel.id, &mut bundle.user) Channel::modify(channel, modify_data, channel.id, &mut bundle.user)
.await .await
.unwrap(); .unwrap();
let received_channel = channel_receiver.borrow(); let received_channel = channel_updater.borrow();
assert_eq!(received_channel.name.as_ref().unwrap(), "beepboop"); assert_eq!(received_channel.name.as_ref().unwrap(), "beepboop");
common::teardown(bundle).await
} }