forked from jmug/stoatchat
refactor: tests for more group routes
This commit is contained in:
@@ -4,8 +4,8 @@ use serde::{Deserialize, Serialize};
|
||||
use revolt_models::v0::{
|
||||
AppendMessage, Channel, Emoji, FieldsChannel, FieldsMember, FieldsRole, FieldsServer,
|
||||
FieldsUser, FieldsWebhook, MemberCompositeKey, Message, PartialChannel, PartialMember,
|
||||
PartialMessage, PartialRole, PartialServer, PartialUser, PartialWebhook, Server, UserSettings,
|
||||
Webhook,
|
||||
PartialMessage, PartialRole, PartialServer, PartialUser, PartialWebhook, Server, User,
|
||||
UserSettings, Webhook,
|
||||
};
|
||||
use revolt_result::Error;
|
||||
|
||||
@@ -154,17 +154,12 @@ pub enum EventV1 {
|
||||
event_id: Option<String>,
|
||||
},
|
||||
|
||||
/*/// Relationship with another user changed
|
||||
UserRelationship {
|
||||
id: String,
|
||||
user: User,
|
||||
// ! this field can be deprecated
|
||||
status: RelationshipStatus,
|
||||
},*/
|
||||
/// Relationship with another user changed
|
||||
UserRelationship { id: String, user: User },
|
||||
/// Settings updated remotely
|
||||
UserSettingsUpdate { id: String, update: UserSettings },
|
||||
|
||||
/*/// User has been platform banned or deleted their account
|
||||
/// User has been platform banned or deleted their account
|
||||
///
|
||||
/// Clients should remove the following associated data:
|
||||
/// - Messages
|
||||
@@ -173,7 +168,7 @@ pub enum EventV1 {
|
||||
/// - Server Memberships
|
||||
///
|
||||
/// User flags are specified to explain why a wipe is occurring though not all reasons will necessarily ever appear.
|
||||
UserPlatformWipe { user_id: String, flags: i32 }, */
|
||||
UserPlatformWipe { user_id: String, flags: i32 },
|
||||
/// New emoji
|
||||
EmojiCreate(Emoji),
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use revolt_config::config;
|
||||
use revolt_models::v0::{self, MessageAuthor};
|
||||
use revolt_permissions::OverrideField;
|
||||
use revolt_result::Result;
|
||||
@@ -191,9 +192,18 @@ impl Channel {
|
||||
/// Create a group
|
||||
pub async fn create_group(
|
||||
db: &Database,
|
||||
data: v0::DataCreateGroup,
|
||||
mut data: v0::DataCreateGroup,
|
||||
owner_id: String,
|
||||
) -> Result<Channel> {
|
||||
data.users.insert(owner_id.to_string());
|
||||
|
||||
let config = config().await;
|
||||
if data.users.len() > config.features.limits.default.group_size {
|
||||
return Err(create_error!(GroupTooLarge {
|
||||
max: config.features.limits.default.group_size,
|
||||
}));
|
||||
}
|
||||
|
||||
let recipients = data.users.into_iter().collect::<Vec<String>>();
|
||||
let channel = Channel::Group {
|
||||
id: ulid::Ulid::new().to_string(),
|
||||
@@ -247,10 +257,6 @@ impl Channel {
|
||||
.p(id.to_string())
|
||||
.await;
|
||||
|
||||
EventV1::ChannelCreate(self.clone().into())
|
||||
.private(user.id.to_string())
|
||||
.await;
|
||||
|
||||
SystemMessage::UserAdded {
|
||||
id: user.id.to_string(),
|
||||
by: by_id.to_string(),
|
||||
@@ -268,6 +274,10 @@ impl Channel {
|
||||
.await
|
||||
.ok();
|
||||
|
||||
EventV1::ChannelCreate(self.clone().into())
|
||||
.private(user.id.to_string())
|
||||
.await;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
_ => Err(create_error!(InvalidOperation)),
|
||||
@@ -295,7 +305,7 @@ impl Channel {
|
||||
}
|
||||
}
|
||||
|
||||
/// Get a reference to this channel's id
|
||||
/// Clone this channel's id
|
||||
pub fn id(&self) -> String {
|
||||
match self {
|
||||
Channel::DirectMessage { id, .. }
|
||||
|
||||
@@ -197,6 +197,13 @@ impl User {
|
||||
RelationshipStatus::None
|
||||
}
|
||||
|
||||
pub fn is_friends_with(&self, user_b: &str) -> bool {
|
||||
matches!(
|
||||
self.relationship_with(user_b),
|
||||
RelationshipStatus::Friend | RelationshipStatus::User
|
||||
)
|
||||
}
|
||||
|
||||
/// Check whether two users have a mutual connection
|
||||
///
|
||||
/// This will check if user and user_b share a server or a group.
|
||||
@@ -341,6 +348,167 @@ impl User {
|
||||
}
|
||||
}
|
||||
|
||||
/// Set a relationship to another user
|
||||
pub async fn set_relationship(
|
||||
&mut self,
|
||||
db: &Database,
|
||||
user_b: &User,
|
||||
status: RelationshipStatus,
|
||||
) -> Result<()> {
|
||||
db.set_relationship(&self.id, &user_b.id, &status).await?;
|
||||
|
||||
if let RelationshipStatus::None | RelationshipStatus::User = status {
|
||||
if let Some(relations) = &mut self.relations {
|
||||
relations.retain(|relation| relation.id != user_b.id);
|
||||
}
|
||||
} else {
|
||||
let relation = Relationship {
|
||||
id: user_b.id.to_string(),
|
||||
status,
|
||||
};
|
||||
|
||||
if let Some(relations) = &mut self.relations {
|
||||
relations.retain(|relation| relation.id != user_b.id);
|
||||
relations.push(relation);
|
||||
} else {
|
||||
self.relations = Some(vec![relation]);
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Apply a certain relationship between two users
|
||||
pub async fn apply_relationship(
|
||||
&mut self,
|
||||
db: &Database,
|
||||
target: &mut User,
|
||||
local: RelationshipStatus,
|
||||
remote: RelationshipStatus,
|
||||
) -> Result<()> {
|
||||
target.set_relationship(db, self, remote).await?;
|
||||
self.set_relationship(db, target, local).await?;
|
||||
|
||||
EventV1::UserRelationship {
|
||||
id: target.id.clone(),
|
||||
user: self.clone().into(Some(&*target)).await,
|
||||
}
|
||||
.private(target.id.clone())
|
||||
.await;
|
||||
|
||||
EventV1::UserRelationship {
|
||||
id: self.id.clone(),
|
||||
user: target.clone().into(Some(&*self)).await,
|
||||
}
|
||||
.private(self.id.clone())
|
||||
.await;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Add another user as a friend
|
||||
pub async fn add_friend(&mut self, db: &Database, target: &mut User) -> Result<()> {
|
||||
match self.relationship_with(&target.id) {
|
||||
RelationshipStatus::User => Err(create_error!(NoEffect)),
|
||||
RelationshipStatus::Friend => Err(create_error!(AlreadyFriends)),
|
||||
RelationshipStatus::Outgoing => Err(create_error!(AlreadySentRequest)),
|
||||
RelationshipStatus::Blocked => Err(create_error!(Blocked)),
|
||||
RelationshipStatus::BlockedOther => Err(create_error!(BlockedByOther)),
|
||||
RelationshipStatus::Incoming => {
|
||||
self.apply_relationship(
|
||||
db,
|
||||
target,
|
||||
RelationshipStatus::Friend,
|
||||
RelationshipStatus::Friend,
|
||||
)
|
||||
.await
|
||||
}
|
||||
RelationshipStatus::None => {
|
||||
self.apply_relationship(
|
||||
db,
|
||||
target,
|
||||
RelationshipStatus::Outgoing,
|
||||
RelationshipStatus::Incoming,
|
||||
)
|
||||
.await
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Remove another user as a friend
|
||||
pub async fn remove_friend(&mut self, db: &Database, target: &mut User) -> Result<()> {
|
||||
match self.relationship_with(&target.id) {
|
||||
RelationshipStatus::Friend
|
||||
| RelationshipStatus::Outgoing
|
||||
| RelationshipStatus::Incoming => {
|
||||
self.apply_relationship(
|
||||
db,
|
||||
target,
|
||||
RelationshipStatus::None,
|
||||
RelationshipStatus::None,
|
||||
)
|
||||
.await
|
||||
}
|
||||
_ => Err(create_error!(NoEffect)),
|
||||
}
|
||||
}
|
||||
|
||||
/// Block another user
|
||||
pub async fn block_user(&mut self, db: &Database, target: &mut User) -> Result<()> {
|
||||
match self.relationship_with(&target.id) {
|
||||
RelationshipStatus::User | RelationshipStatus::Blocked => Err(create_error!(NoEffect)),
|
||||
RelationshipStatus::BlockedOther => {
|
||||
self.apply_relationship(
|
||||
db,
|
||||
target,
|
||||
RelationshipStatus::Blocked,
|
||||
RelationshipStatus::Blocked,
|
||||
)
|
||||
.await
|
||||
}
|
||||
RelationshipStatus::None
|
||||
| RelationshipStatus::Friend
|
||||
| RelationshipStatus::Incoming
|
||||
| RelationshipStatus::Outgoing => {
|
||||
self.apply_relationship(
|
||||
db,
|
||||
target,
|
||||
RelationshipStatus::Blocked,
|
||||
RelationshipStatus::BlockedOther,
|
||||
)
|
||||
.await
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Unblock another user
|
||||
pub async fn unblock_user(&mut self, db: &Database, target: &mut User) -> Result<()> {
|
||||
match self.relationship_with(&target.id) {
|
||||
RelationshipStatus::Blocked => match target.relationship_with(&self.id) {
|
||||
RelationshipStatus::Blocked => {
|
||||
self.apply_relationship(
|
||||
db,
|
||||
target,
|
||||
RelationshipStatus::BlockedOther,
|
||||
RelationshipStatus::Blocked,
|
||||
)
|
||||
.await
|
||||
}
|
||||
RelationshipStatus::BlockedOther => {
|
||||
self.apply_relationship(
|
||||
db,
|
||||
target,
|
||||
RelationshipStatus::None,
|
||||
RelationshipStatus::None,
|
||||
)
|
||||
.await
|
||||
}
|
||||
_ => Err(create_error!(InternalError)),
|
||||
},
|
||||
_ => Err(create_error!(NoEffect)),
|
||||
}
|
||||
}
|
||||
|
||||
/// Update user data
|
||||
pub async fn update<'a>(
|
||||
&mut self,
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use revolt_result::Result;
|
||||
|
||||
use crate::ReferenceDb;
|
||||
use crate::{FieldsUser, PartialUser, RelationshipStatus, User};
|
||||
use crate::{ReferenceDb, Relationship};
|
||||
|
||||
use super::AbstractUsers;
|
||||
|
||||
@@ -106,19 +106,49 @@ impl AbstractUsers for ReferenceDb {
|
||||
|
||||
/// Set relationship with another user
|
||||
///
|
||||
/// This should use pull_relationship if relationship is None.
|
||||
/// This should use pull_relationship if relationship is None or User.
|
||||
async fn set_relationship(
|
||||
&self,
|
||||
_user_id: &str,
|
||||
_target_id: &str,
|
||||
_relationship: &RelationshipStatus,
|
||||
user_id: &str,
|
||||
target_id: &str,
|
||||
relationship: &RelationshipStatus,
|
||||
) -> Result<()> {
|
||||
todo!()
|
||||
if let RelationshipStatus::User | RelationshipStatus::None = &relationship {
|
||||
self.pull_relationship(user_id, target_id).await
|
||||
} else {
|
||||
let mut users = self.users.lock().await;
|
||||
let user = users
|
||||
.get_mut(user_id)
|
||||
.ok_or_else(|| create_error!(NotFound))?;
|
||||
|
||||
let relation = Relationship {
|
||||
id: target_id.to_string(),
|
||||
status: relationship.clone(),
|
||||
};
|
||||
|
||||
if let Some(relations) = &mut user.relations {
|
||||
relations.retain(|relation| relation.id != target_id);
|
||||
relations.push(relation);
|
||||
} else {
|
||||
user.relations = Some(vec![relation]);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// Remove relationship with another user
|
||||
async fn pull_relationship(&self, _user_id: &str, _target_id: &str) -> Result<()> {
|
||||
todo!()
|
||||
async fn pull_relationship(&self, user_id: &str, target_id: &str) -> Result<()> {
|
||||
let mut users = self.users.lock().await;
|
||||
let user = users
|
||||
.get_mut(user_id)
|
||||
.ok_or_else(|| create_error!(NotFound))?;
|
||||
|
||||
if let Some(relations) = &mut user.relations {
|
||||
relations.retain(|relation| relation.id != target_id);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Delete a user by their id
|
||||
|
||||
@@ -7,7 +7,7 @@ use schemars::{
|
||||
JsonSchema,
|
||||
};
|
||||
|
||||
use crate::{Bot, Channel, Database, Emoji, Message, Webhook};
|
||||
use crate::{Bot, Channel, Database, Emoji, Message, User, Webhook};
|
||||
|
||||
/// Reference to some object in the database
|
||||
#[derive(Serialize, Deserialize)]
|
||||
@@ -42,6 +42,11 @@ impl Reference {
|
||||
db.fetch_message(&self.id).await
|
||||
}
|
||||
|
||||
/// Fetch user from Ref
|
||||
pub async fn as_user(&self, db: &Database) -> Result<User> {
|
||||
db.fetch_user(&self.id).await
|
||||
}
|
||||
|
||||
/// Fetch webhook from Ref
|
||||
pub async fn as_webhook(&self, db: &Database) -> Result<Webhook> {
|
||||
db.fetch_webhook(&self.id).await
|
||||
|
||||
Reference in New Issue
Block a user