fix(core/database): only run outgoing friend checks if we are creating new request

closes #327
This commit is contained in:
Paul Makles
2024-06-25 18:07:16 +01:00
parent 1ec8f46c1d
commit de5add09d0

View File

@@ -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<()> {
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 => {
// Accept incoming friend request
self.apply_relationship(
db,
target,
RelationshipStatus::Friend,
RelationshipStatus::Friend,
)
.await
}
RelationshipStatus::None => {
// Get this user's current count of outgoing friend requests
let count = self let count = self
.relations .relations
.as_ref() .as_ref()
@@ -481,6 +499,7 @@ impl User {
}) })
.unwrap_or_default(); .unwrap_or_default();
// If we're over the limit, don't allow creating more requests
let config = config().await; let config = config().await;
if count >= config.features.limits.default.outgoing_friend_requests { if count >= config.features.limits.default.outgoing_friend_requests {
return Err(create_error!(TooManyPendingFriendRequests { return Err(create_error!(TooManyPendingFriendRequests {
@@ -488,22 +507,7 @@ impl User {
})); }));
} }
match self.relationship_with(&target.id) { // Send the friend request
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( self.apply_relationship(
db, db,
target, target,