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]
|
||||||
|
|
||||||
[features.limits.default]
|
[features.limits.default]
|
||||||
|
outgoing_friend_requests = 10
|
||||||
|
|
||||||
group_size = 100
|
group_size = 100
|
||||||
bots = 5
|
bots = 5
|
||||||
message_length = 2000
|
message_length = 2000
|
||||||
|
|||||||
@@ -106,6 +106,8 @@ pub struct Api {
|
|||||||
|
|
||||||
#[derive(Deserialize, Debug, Clone)]
|
#[derive(Deserialize, Debug, Clone)]
|
||||||
pub struct FeaturesLimits {
|
pub struct FeaturesLimits {
|
||||||
|
pub outgoing_friend_requests: usize,
|
||||||
|
|
||||||
pub group_size: usize,
|
pub group_size: usize,
|
||||||
pub bots: usize,
|
pub bots: usize,
|
||||||
pub message_length: usize,
|
pub message_length: usize,
|
||||||
|
|||||||
@@ -470,6 +470,24 @@ impl User {
|
|||||||
|
|
||||||
/// Add another user as a friend
|
/// Add another user as a friend
|
||||||
pub async fn add_friend(&mut self, db: &Database, target: &mut User) -> Result<()> {
|
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) {
|
match self.relationship_with(&target.id) {
|
||||||
RelationshipStatus::User => Err(create_error!(NoEffect)),
|
RelationshipStatus::User => Err(create_error!(NoEffect)),
|
||||||
RelationshipStatus::Friend => Err(create_error!(AlreadyFriends)),
|
RelationshipStatus::Friend => Err(create_error!(AlreadyFriends)),
|
||||||
|
|||||||
@@ -60,6 +60,9 @@ pub enum ErrorType {
|
|||||||
Blocked,
|
Blocked,
|
||||||
BlockedByOther,
|
BlockedByOther,
|
||||||
NotFriends,
|
NotFriends,
|
||||||
|
TooManyPendingFriendRequests {
|
||||||
|
max: usize,
|
||||||
|
},
|
||||||
|
|
||||||
// ? Channel related errors
|
// ? Channel related errors
|
||||||
UnknownChannel,
|
UnknownChannel,
|
||||||
|
|||||||
@@ -25,6 +25,7 @@ impl<'r> Responder<'r, 'static> for Error {
|
|||||||
ErrorType::Blocked => Status::Conflict,
|
ErrorType::Blocked => Status::Conflict,
|
||||||
ErrorType::BlockedByOther => Status::Forbidden,
|
ErrorType::BlockedByOther => Status::Forbidden,
|
||||||
ErrorType::NotFriends => Status::Forbidden,
|
ErrorType::NotFriends => Status::Forbidden,
|
||||||
|
ErrorType::TooManyPendingFriendRequests { .. } => Status::BadRequest,
|
||||||
|
|
||||||
ErrorType::UnknownChannel => Status::NotFound,
|
ErrorType::UnknownChannel => Status::NotFound,
|
||||||
ErrorType::UnknownMessage => Status::NotFound,
|
ErrorType::UnknownMessage => Status::NotFound,
|
||||||
|
|||||||
Reference in New Issue
Block a user