forked from jmug/stoatchat
feat(core): add a limit to no. of outgoing pending friend requests
This commit is contained in:
@@ -47,6 +47,8 @@ webhooks_enabled = false
|
||||
[features.limits]
|
||||
|
||||
[features.limits.default]
|
||||
outgoing_friend_requests = 10
|
||||
|
||||
group_size = 100
|
||||
bots = 5
|
||||
message_length = 2000
|
||||
|
||||
@@ -106,6 +106,8 @@ pub struct Api {
|
||||
|
||||
#[derive(Deserialize, Debug, Clone)]
|
||||
pub struct FeaturesLimits {
|
||||
pub outgoing_friend_requests: usize,
|
||||
|
||||
pub group_size: usize,
|
||||
pub bots: usize,
|
||||
pub message_length: usize,
|
||||
|
||||
@@ -470,6 +470,24 @@ impl User {
|
||||
|
||||
/// Add another user as a friend
|
||||
pub async fn add_friend(&mut self, db: &Database, target: &mut User) -> Result<()> {
|
||||
let count = self
|
||||
.relations
|
||||
.as_ref()
|
||||
.map(|relations| {
|
||||
relations
|
||||
.iter()
|
||||
.filter(|r| matches!(r.status, RelationshipStatus::Outgoing))
|
||||
.count()
|
||||
})
|
||||
.unwrap_or_default();
|
||||
|
||||
let config = config().await;
|
||||
if count >= config.features.limits.default.outgoing_friend_requests {
|
||||
return Err(create_error!(TooManyPendingFriendRequests {
|
||||
max: config.features.limits.default.outgoing_friend_requests
|
||||
}));
|
||||
}
|
||||
|
||||
match self.relationship_with(&target.id) {
|
||||
RelationshipStatus::User => Err(create_error!(NoEffect)),
|
||||
RelationshipStatus::Friend => Err(create_error!(AlreadyFriends)),
|
||||
|
||||
@@ -60,6 +60,9 @@ pub enum ErrorType {
|
||||
Blocked,
|
||||
BlockedByOther,
|
||||
NotFriends,
|
||||
TooManyPendingFriendRequests {
|
||||
max: usize,
|
||||
},
|
||||
|
||||
// ? Channel related errors
|
||||
UnknownChannel,
|
||||
|
||||
@@ -25,6 +25,7 @@ impl<'r> Responder<'r, 'static> for Error {
|
||||
ErrorType::Blocked => Status::Conflict,
|
||||
ErrorType::BlockedByOther => Status::Forbidden,
|
||||
ErrorType::NotFriends => Status::Forbidden,
|
||||
ErrorType::TooManyPendingFriendRequests { .. } => Status::BadRequest,
|
||||
|
||||
ErrorType::UnknownChannel => Status::NotFound,
|
||||
ErrorType::UnknownMessage => Status::NotFound,
|
||||
|
||||
Reference in New Issue
Block a user