From 663ce2ceac4fab748b73c7ae06b172b018423bb2 Mon Sep 17 00:00:00 2001 From: netkas Date: Fri, 6 Jun 2025 18:15:20 -0400 Subject: [PATCH] Implement LiftBlacklist functionality and related methods --- src/FederationServer/Classes/Enums/Method.php | 3 +- .../Classes/Managers/BlacklistManager.php | 26 ++++++++ .../Methods/Blacklist/LiftBlacklist.php | 61 +++++++++++++++++++ .../Objects/BlacklistRecord.php | 12 ++++ 4 files changed, 101 insertions(+), 1 deletion(-) create mode 100644 src/FederationServer/Methods/Blacklist/LiftBlacklist.php diff --git a/src/FederationServer/Classes/Enums/Method.php b/src/FederationServer/Classes/Enums/Method.php index f54c130..f9673bd 100644 --- a/src/FederationServer/Classes/Enums/Method.php +++ b/src/FederationServer/Classes/Enums/Method.php @@ -10,6 +10,7 @@ use FederationServer\Methods\Audit\ViewAuditEntry; use FederationServer\Methods\Blacklist\BlacklistEntity; use FederationServer\Methods\Blacklist\DeleteBlacklist; + use FederationServer\Methods\Blacklist\LiftBlacklist; use FederationServer\Methods\Blacklist\ListBlacklist; use FederationServer\Methods\Entities\DeleteEntity; use FederationServer\Methods\Entities\GetEntityRecord; @@ -201,7 +202,7 @@ DeleteBlacklist::handleRequest(); break; case self::LIFT_BLACKLIST: - throw new \Exception('To be implemented'); + LiftBlacklist::handleRequest(); break; case self::BLACKLIST_ATTACH_EVIDENCE: throw new \Exception('To be implemented'); diff --git a/src/FederationServer/Classes/Managers/BlacklistManager.php b/src/FederationServer/Classes/Managers/BlacklistManager.php index bd317f7..069d86b 100644 --- a/src/FederationServer/Classes/Managers/BlacklistManager.php +++ b/src/FederationServer/Classes/Managers/BlacklistManager.php @@ -189,6 +189,32 @@ } } + /** + * Lifts a blacklist record, marking it as no longer active. + * + * @param string $uuid The UUID of the blacklist record to lift. + * @throws InvalidArgumentException If the UUID is empty. + * @throws DatabaseOperationException If there is an error preparing or executing the SQL statement. + */ + public static function liftBlacklistRecord(string $uuid): void + { + if(empty($uuid)) + { + throw new InvalidArgumentException("UUID cannot be empty."); + } + + try + { + $stmt = DatabaseConnection::getConnection()->prepare("UPDATE blacklist SET lifted=1 WHERE uuid = :uuid"); + $stmt->bindParam(':uuid', $uuid); + $stmt->execute(); + } + catch (PDOException $e) + { + throw new DatabaseOperationException("Failed to lift blacklist record: " . $e->getMessage(), 0, $e); + } + } + /** * Returns an array of blacklist records in a pagination style for the global database * diff --git a/src/FederationServer/Methods/Blacklist/LiftBlacklist.php b/src/FederationServer/Methods/Blacklist/LiftBlacklist.php new file mode 100644 index 0000000..5cc3c21 --- /dev/null +++ b/src/FederationServer/Methods/Blacklist/LiftBlacklist.php @@ -0,0 +1,61 @@ +canManageBlacklist()) + { + throw new RequestException('Insufficient permissions to manage the blacklist', 401); + } + + if(!preg_match('#^/blacklist/([a-fA-F0-9\-]{36,})$#', FederationServer::getPath(), $matches)) + { + throw new RequestException('Blacklist UUID required', 405); + } + + $blacklistUuid = $matches[1]; + if(!$blacklistUuid || !Validate::uuid($blacklistUuid)) + { + throw new RequestException('Invalid blacklist UUID', 400); + } + + try + { + $blacklistRecord = BlacklistManager::getBlacklistEntry($blacklistUuid); + + if($blacklistRecord === null) + { + throw new RequestException('Blacklist record not found', 404); + } + + if($blacklistRecord->isLifted()) + { + throw new RequestException('Blacklist record is already lifted', 400); + } + + BlacklistManager::liftBlacklistRecord($blacklistUuid); + } + catch (DatabaseOperationException $e) + { + throw new RequestException('Unable to retrieve blacklist records', 500, $e); + } + + self::successResponse(); + } + } + diff --git a/src/FederationServer/Objects/BlacklistRecord.php b/src/FederationServer/Objects/BlacklistRecord.php index d02e188..d3af20e 100644 --- a/src/FederationServer/Objects/BlacklistRecord.php +++ b/src/FederationServer/Objects/BlacklistRecord.php @@ -13,6 +13,7 @@ private string $entity; private ?string $evidence; private BlacklistType $type; + private bool $lifted; private ?int $expires; private int $created; @@ -35,6 +36,7 @@ $this->entity = $data['entity'] ?? ''; $this->evidence = $data['evidence'] ?? null; $this->type = isset($data['type']) ? BlacklistType::from($data['type']) : BlacklistType::OTHER; + $this->lifted = isset($data['lifted']) ? (bool)$data['lifted'] : false; $this->expires = isset($data['expires']) ? (int)$data['expires'] : null; $this->created = isset($data['created']) ? (int)$data['created'] : time(); } @@ -92,6 +94,16 @@ return $this->type; } + /** + * Check if the blacklist record has been lifted. + * + * @return bool True if the record is lifted, false otherwise. + */ + public function isLifted(): bool + { + return $this->lifted; + } + /** * Get the expiration timestamp of the blacklist record. *