diff --git a/src/api/auth/login.rs b/src/api/auth/login.rs index a360170..447e4c0 100644 --- a/src/api/auth/login.rs +++ b/src/api/auth/login.rs @@ -1,5 +1,6 @@ use std::cell::RefCell; use std::rc::Rc; +use std::sync::{Arc, Mutex}; use reqwest::Client; use serde_json::to_string; @@ -45,7 +46,7 @@ impl Instance { login_result.token, self.clone_limits_if_some(), login_result.settings, - object, + Arc::new(Mutex::new(object)), gateway, ); Ok(user) diff --git a/src/api/auth/register.rs b/src/api/auth/register.rs index f1f279e..ea74f29 100644 --- a/src/api/auth/register.rs +++ b/src/api/auth/register.rs @@ -1,3 +1,4 @@ +use std::sync::{Arc, Mutex}; use std::{cell::RefCell, rc::Rc}; use reqwest::Client; @@ -51,8 +52,8 @@ impl Instance { Rc::new(RefCell::new(self.clone())), token.clone(), self.clone_limits_if_some(), - settings, - user_object, + Arc::new(Mutex::new(settings)), + Arc::new(Mutex::new(user_object)), gateway, ); Ok(user) diff --git a/src/api/users/users.rs b/src/api/users/users.rs index 3d95e54..03af2f9 100644 --- a/src/api/users/users.rs +++ b/src/api/users/users.rs @@ -1,3 +1,4 @@ +use std::sync::{Arc, Mutex}; use std::{cell::RefCell, rc::Rc}; use reqwest::Client; @@ -59,7 +60,7 @@ impl UserMeta { .deserialize_response::(self) .await .unwrap(); - let _ = std::mem::replace(&mut self.object, user_updated.clone()); + self.object = Arc::new(Mutex::new(user_updated.clone())); Ok(user_updated) } diff --git a/src/instance.rs b/src/instance.rs index f034513..5c2e98b 100644 --- a/src/instance.rs +++ b/src/instance.rs @@ -2,6 +2,7 @@ use std::cell::RefCell; use std::collections::HashMap; use std::fmt; use std::rc::Rc; +use std::sync::{Arc, Mutex}; use reqwest::Client; use serde::{Deserialize, Serialize}; @@ -90,8 +91,8 @@ pub struct UserMeta { pub belongs_to: Rc>, pub token: String, pub limits: Option>, - pub settings: UserSettings, - pub object: User, + pub settings: Arc>, + pub object: Arc>, pub gateway: GatewayHandle, } @@ -113,8 +114,8 @@ impl UserMeta { belongs_to: Rc>, token: String, limits: Option>, - settings: UserSettings, - object: User, + settings: Arc>, + object: Arc>, gateway: GatewayHandle, ) -> UserMeta { UserMeta { @@ -133,8 +134,8 @@ impl UserMeta { /// need to make a RateLimited request. To use the [`GatewayHandle`], you will have to identify /// first. pub(crate) async fn shell(instance: Rc>, token: String) -> UserMeta { - let settings = UserSettings::default(); - let object = User::default(); + let settings = Arc::new(Mutex::new(UserSettings::default())); + let object = Arc::new(Mutex::new(User::default())); let wss_url = instance.borrow().urls.wss.clone(); // Dummy gateway object let gateway = Gateway::new(wss_url).await.unwrap(); diff --git a/tests/channels.rs b/tests/channels.rs index c8564d7..d810c10 100644 --- a/tests/channels.rs +++ b/tests/channels.rs @@ -59,8 +59,9 @@ async fn modify_channel() { PermissionFlags::MANAGE_CHANNELS, PermissionFlags::MANAGE_MESSAGES, ])); + let user_id: types::Snowflake = bundle.user.object.lock().unwrap().id; let permission_override = PermissionOverwrite { - id: bundle.user.object.id, + id: user_id, overwrite_type: "1".to_string(), allow: permission_override, deny: "0".to_string(), @@ -143,7 +144,7 @@ async fn create_dm() { let other_user = bundle.create_user("integrationtestuser2").await; let user = &mut bundle.user; let private_channel_create_schema = PrivateChannelCreateSchema { - recipients: Some(Vec::from([other_user.object.id])), + recipients: Some(Vec::from([other_user.object.lock().unwrap().id])), access_tokens: None, nicks: None, }; @@ -153,26 +154,47 @@ async fn create_dm() { .unwrap(); assert!(dm_channel.recipients.is_some()); assert_eq!( - dm_channel.recipients.as_ref().unwrap().get(0).unwrap().id, - other_user.object.id + dm_channel + .recipients + .as_ref() + .unwrap() + .get(0) + .unwrap() + .lock() + .unwrap() + .id + .clone(), + other_user.object.lock().unwrap().id ); assert_eq!( - dm_channel.recipients.as_ref().unwrap().get(1).unwrap().id, - user.object.id + dm_channel + .recipients + .as_ref() + .unwrap() + .get(1) + .unwrap() + .lock() + .unwrap() + .id + .clone(), + user.object.lock().unwrap().id.clone() ); common::teardown(bundle).await; } // #[tokio::test] -// This test currently is broken due to an issue with the Spacebar Server. +// TODO This test currently is broken due to an issue with the Spacebar Server. #[allow(dead_code)] async fn remove_add_person_from_to_dm() { let mut bundle = common::setup().await; let mut other_user = bundle.create_user("integrationtestuser2").await; let mut third_user = bundle.create_user("integrationtestuser3").await; + let third_user_id = third_user.object.lock().unwrap().id; + let other_user_id = other_user.object.lock().unwrap().id; + let user_id = bundle.user.object.lock().unwrap().id; let user = &mut bundle.user; let private_channel_create_schema = PrivateChannelCreateSchema { - recipients: Some(Vec::from([other_user.object.id, third_user.object.id])), + recipients: Some(Vec::from([other_user_id, third_user_id])), access_tokens: None, nicks: None, }; @@ -181,36 +203,52 @@ async fn remove_add_person_from_to_dm() { .await .unwrap(); // Creates the Channel and stores the response Channel object dm_channel - .remove_channel_recipient(other_user.object.id, user) + .remove_channel_recipient(other_user_id, user) .await .unwrap(); assert!(dm_channel.recipients.as_ref().unwrap().get(1).is_none()); other_user - .modify_user_relationship(user.object.id, RelationshipType::Friends) + .modify_user_relationship(user_id, RelationshipType::Friends) .await .unwrap(); - user.modify_user_relationship(other_user.object.id, RelationshipType::Friends) + user.modify_user_relationship(other_user_id, RelationshipType::Friends) .await .unwrap(); third_user - .modify_user_relationship(user.object.id, RelationshipType::Friends) + .modify_user_relationship(user_id, RelationshipType::Friends) .await .unwrap(); - user.modify_user_relationship(third_user.object.id, RelationshipType::Friends) + user.modify_user_relationship(third_user_id, RelationshipType::Friends) .await .unwrap(); // Users 1-2 and 1-3 are now friends dm_channel - .add_channel_recipient(other_user.object.id, user, None) + .add_channel_recipient(other_user_id, user, None) .await .unwrap(); assert!(dm_channel.recipients.is_some()); assert_eq!( - dm_channel.recipients.as_ref().unwrap().get(0).unwrap().id, - other_user.object.id + dm_channel + .recipients + .as_ref() + .unwrap() + .get(0) + .unwrap() + .lock() + .unwrap() + .id, + other_user_id ); assert_eq!( - dm_channel.recipients.as_ref().unwrap().get(1).unwrap().id, - user.object.id + dm_channel + .recipients + .as_ref() + .unwrap() + .get(1) + .unwrap() + .lock() + .unwrap() + .id, + user_id ); } diff --git a/tests/members.rs b/tests/members.rs index 9710d7f..a314be7 100644 --- a/tests/members.rs +++ b/tests/members.rs @@ -7,7 +7,7 @@ async fn add_remove_role() -> ChorusResult<()> { let mut bundle = common::setup().await; let guild = bundle.guild.id; let role = bundle.role.id; - let member_id = bundle.user.object.id; + let member_id = bundle.user.object.lock().unwrap().id; GuildMember::add_role(&mut bundle.user, guild, member_id, role).await?; let member = GuildMember::get(&mut bundle.user, guild, member_id) .await diff --git a/tests/relationships.rs b/tests/relationships.rs index 00ef9cf..1bd4976 100644 --- a/tests/relationships.rs +++ b/tests/relationships.rs @@ -7,15 +7,13 @@ async fn test_get_mutual_relationships() { let mut bundle = common::setup().await; let mut other_user = bundle.create_user("integrationtestuser2").await; let user = &mut bundle.user; + let other_user_id: types::Snowflake = other_user.object.lock().unwrap().id; let friend_request_schema = types::FriendRequestSendSchema { - username: user.object.username.clone(), - discriminator: Some(user.object.discriminator.clone()), + username: user.object.lock().unwrap().username.clone(), + discriminator: Some(user.object.lock().unwrap().discriminator.clone()), }; let _ = other_user.send_friend_request(friend_request_schema).await; - let relationships = user - .get_mutual_relationships(other_user.object.id) - .await - .unwrap(); + let relationships = user.get_mutual_relationships(other_user_id).await.unwrap(); println!("{:?}", relationships); common::teardown(bundle).await } @@ -26,15 +24,18 @@ async fn test_get_relationships() { let mut other_user = bundle.create_user("integrationtestuser2").await; let user = &mut bundle.user; let friend_request_schema = types::FriendRequestSendSchema { - username: user.object.username.clone(), - discriminator: Some(user.object.discriminator.clone()), + username: user.object.lock().unwrap().username.clone(), + discriminator: Some(user.object.lock().unwrap().discriminator.clone()), }; other_user .send_friend_request(friend_request_schema) .await .unwrap(); let relationships = user.get_relationships().await.unwrap(); - assert_eq!(relationships.get(0).unwrap().id, other_user.object.id); + assert_eq!( + relationships.get(0).unwrap().id, + other_user.object.lock().unwrap().id + ); common::teardown(bundle).await } @@ -43,23 +44,33 @@ async fn test_modify_relationship_friends() { let mut bundle = common::setup().await; let mut other_user = bundle.create_user("integrationtestuser2").await; let user = &mut bundle.user; - let _ = other_user - .modify_user_relationship(user.object.id, types::RelationshipType::Friends) - .await; + let user_id: types::Snowflake = user.object.lock().unwrap().id; + let other_user_id: types::Snowflake = other_user.object.lock().unwrap().id; + + other_user + .modify_user_relationship(user_id, types::RelationshipType::Friends) + .await + .unwrap(); let relationships = user.get_relationships().await.unwrap(); - assert_eq!(relationships.get(0).unwrap().id, other_user.object.id); + assert_eq!( + relationships.get(0).unwrap().id, + other_user.object.lock().unwrap().id + ); assert_eq!( relationships.get(0).unwrap().relationship_type, RelationshipType::Incoming ); let relationships = other_user.get_relationships().await.unwrap(); - assert_eq!(relationships.get(0).unwrap().id, user.object.id); + assert_eq!( + relationships.get(0).unwrap().id, + user.object.lock().unwrap().id + ); assert_eq!( relationships.get(0).unwrap().relationship_type, RelationshipType::Outgoing ); let _ = user - .modify_user_relationship(other_user.object.id, RelationshipType::Friends) + .modify_user_relationship(other_user_id, RelationshipType::Friends) .await; assert_eq!( other_user @@ -71,7 +82,7 @@ async fn test_modify_relationship_friends() { .relationship_type, RelationshipType::Friends ); - let _ = user.remove_relationship(other_user.object.id).await; + let _ = user.remove_relationship(other_user_id).await; assert_eq!( other_user.get_relationships().await.unwrap(), Vec::::new() @@ -84,18 +95,24 @@ async fn test_modify_relationship_block() { let mut bundle = common::setup().await; let mut other_user = bundle.create_user("integrationtestuser2").await; let user = &mut bundle.user; - let _ = other_user - .modify_user_relationship(user.object.id, types::RelationshipType::Blocked) - .await; + let user_id: types::Snowflake = user.object.lock().unwrap().id; + + other_user + .modify_user_relationship(user_id, types::RelationshipType::Blocked) + .await + .unwrap(); let relationships = user.get_relationships().await.unwrap(); assert_eq!(relationships, Vec::::new()); let relationships = other_user.get_relationships().await.unwrap(); - assert_eq!(relationships.get(0).unwrap().id, user.object.id); + assert_eq!( + relationships.get(0).unwrap().id, + user.object.lock().unwrap().id + ); assert_eq!( relationships.get(0).unwrap().relationship_type, RelationshipType::Blocked ); - let _ = other_user.remove_relationship(user.object.id).await; + other_user.remove_relationship(user_id).await.unwrap(); assert_eq!( other_user.get_relationships().await.unwrap(), Vec::::new()