-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #60 from nostrver-se/kriptonix-relay-responses-han…
…dling Improve relay responses handling
- Loading branch information
Showing
19 changed files
with
357 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace swentel\nostr\RelayResponse; | ||
|
||
use swentel\nostr\RelayResponseInterface; | ||
|
||
class RelayResponse implements RelayResponseInterface | ||
{ | ||
public string $type; | ||
|
||
public bool $isSuccess; | ||
|
||
public string $message; | ||
|
||
public function __construct(array $response) | ||
{ | ||
$this->isSuccess = false; | ||
if (isset($response[0])) { | ||
$this->isSuccess = true; | ||
$this->type = RelayResponseEnum::from($response[0])->value; | ||
// Piece of legacy to support version <=1.3.3 | ||
if ($this->type === 'OK' && $response[2] === false) { | ||
$this->isSuccess = false; | ||
} | ||
$this->message = !empty($response[3]) ? $response[3] : ''; | ||
} | ||
} | ||
|
||
/** | ||
* Create a response object based on the given type using a match expression. | ||
* | ||
* @param array $response The response data to be used for creating the object. | ||
* @param string $type The type of response to determine which object to create. | ||
* @return object The created response object based on the type. | ||
*/ | ||
public static function createResponse(array $response, string $type): mixed | ||
{ | ||
return match ($type) { | ||
'ERROR', 'NOTICE' => new RelayResponseNotice($response), | ||
'EVENT' => new RelayResponseEvent($response), | ||
'OK' => new RelayResponseOk($response), | ||
'EOSE' => new RelayResponseEose($response), | ||
'CLOSED' => new RelayResponseClosed($response), | ||
'AUTH' => new RelayResponseAuth($response), | ||
default => new self($response), | ||
}; | ||
} | ||
|
||
/** | ||
* Static method to create a response object based on the given type using a match expression. | ||
* | ||
* @param array $response The response data to be used for creating the object. | ||
* @return object The created response object based on the determined type. | ||
*/ | ||
public static function create(array $response): mixed | ||
{ | ||
$type = RelayResponseEnum::from($response[0])->value; | ||
return self::createResponse($response, $type); | ||
} | ||
|
||
/** | ||
* Backwards compatability support for <=1.3.3 where we used the CommandResultInterface as a response. | ||
* | ||
* @return bool | ||
*/ | ||
public function isSuccess(): bool | ||
{ | ||
return $this->isSuccess; | ||
} | ||
|
||
/** | ||
* Backwards compatability support for <=1.3.3 where we used the CommandResultInterface as a response. | ||
* | ||
* @return string | ||
*/ | ||
public function message(): string | ||
{ | ||
return $this->message; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace swentel\nostr\RelayResponse; | ||
|
||
class RelayResponseAuth extends RelayResponse | ||
{ | ||
public string $message; | ||
|
||
public function __construct($response) | ||
{ | ||
parent::__construct($response); | ||
$this->message = $response[1]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace swentel\nostr\RelayResponse; | ||
|
||
class RelayResponseClosed extends RelayResponse | ||
{ | ||
public string $subscriptionId; | ||
|
||
public string $message; | ||
|
||
public function __construct($response) | ||
{ | ||
parent::__construct($response); | ||
$this->subscriptionId = $response[1]; | ||
$this->message = $response[2]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace swentel\nostr\RelayResponse; | ||
|
||
/** | ||
* Enum with response types. | ||
*/ | ||
enum RelayResponseEnum: string | ||
{ | ||
case EVENT = 'EVENT'; | ||
case OK = 'OK'; | ||
case EOSE = 'EOSE'; | ||
case CLOSED = 'CLOSED'; | ||
case NOTICE = 'NOTICE'; | ||
/** | ||
* NIP-42 support - Authentication of clients to relays | ||
* https://github.com/nostr-protocol/nips/blob/master/42.md | ||
*/ | ||
case AUTH = 'AUTH'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace swentel\nostr\RelayResponse; | ||
|
||
class RelayResponseEose extends RelayResponse | ||
{ | ||
public string $subscriptionId; | ||
|
||
public function __construct($response) | ||
{ | ||
parent::__construct($response); | ||
$this->subscriptionId = $response[1]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace swentel\nostr\RelayResponse; | ||
|
||
class RelayResponseEvent extends RelayResponse | ||
{ | ||
public string $subscriptionId; | ||
|
||
public \stdClass $event; | ||
|
||
public function __construct($response) | ||
{ | ||
parent::__construct($response); | ||
$this->subscriptionId = $response[1]; | ||
$this->event = $response[2]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace swentel\nostr\RelayResponse; | ||
|
||
class RelayResponseNotice extends RelayResponse | ||
{ | ||
public string $message; | ||
|
||
public function __construct($response) | ||
{ | ||
parent::__construct($response); | ||
$this->message = $response[1]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace swentel\nostr\RelayResponse; | ||
|
||
class RelayResponseOk extends RelayResponse | ||
{ | ||
public string $eventId; | ||
|
||
public bool $status; | ||
|
||
public string $message; | ||
|
||
public function __construct($response) | ||
{ | ||
parent::__construct($response); | ||
$this->eventId = $response[1]; | ||
$this->status = $response[2]; | ||
$this->message = $response[3]; | ||
} | ||
} |
Oops, something went wrong.