Integrate component behaviour everywhere

This commit is contained in:
bitfl0wer 2023-08-04 11:19:23 +02:00
parent 94eac6eba9
commit fb792f8356
7 changed files with 109 additions and 50 deletions

View File

@ -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)

View File

@ -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)

View File

@ -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::<User>(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)
}

View File

@ -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<RefCell<Instance>>,
pub token: String,
pub limits: Option<HashMap<LimitType, Limit>>,
pub settings: UserSettings,
pub object: User,
pub settings: Arc<Mutex<UserSettings>>,
pub object: Arc<Mutex<User>>,
pub gateway: GatewayHandle,
}
@ -113,8 +114,8 @@ impl UserMeta {
belongs_to: Rc<RefCell<Instance>>,
token: String,
limits: Option<HashMap<LimitType, Limit>>,
settings: UserSettings,
object: User,
settings: Arc<Mutex<UserSettings>>,
object: Arc<Mutex<User>>,
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<RefCell<Instance>>, 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();

View File

@ -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
);
}

View File

@ -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

View File

@ -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::<Relationship>::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::<Relationship>::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::<Relationship>::new()