Skip to content

Commit

Permalink
fix(openapi): Fix reactions API empty array cases
Browse files Browse the repository at this point in the history
Signed-off-by: Joas Schilling <coding@schilljs.com>
  • Loading branch information
nickvergessen committed Nov 27, 2024
1 parent b6efaeb commit 8bc276e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 22 deletions.
2 changes: 1 addition & 1 deletion lib/Chat/ReactionManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public function deleteReactionMessage(Room $chat, string $actorType, string $act
}

/**
* @return array<string, TalkReaction[]>
* @return array<string, list<TalkReaction>>
* @throws PreConditionNotMetException
*/
public function retrieveReactionMessages(Room $chat, Participant $participant, int $messageId, ?string $reaction = null): array {
Expand Down
20 changes: 10 additions & 10 deletions lib/Controller/ReactionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function __construct(
* @param int $messageId ID of the message
* @psalm-param non-negative-int $messageId
* @param string $reaction Emoji to add
* @return DataResponse<Http::STATUS_OK|Http::STATUS_CREATED, array<string, TalkReaction[]>|\stdClass, array{}>|DataResponse<Http::STATUS_BAD_REQUEST|Http::STATUS_NOT_FOUND, array<empty>, array{}>
* @return DataResponse<Http::STATUS_OK|Http::STATUS_CREATED, array<string, list<TalkReaction>>|\stdClass, array{}>|DataResponse<Http::STATUS_BAD_REQUEST|Http::STATUS_NOT_FOUND, null, array{}>
*
* 200: Reaction already existed
* 201: Reaction added successfully
Expand Down Expand Up @@ -73,11 +73,11 @@ public function react(int $messageId, string $reaction): DataResponse {
);
$status = Http::STATUS_CREATED;
} catch (NotFoundException $e) {
return new DataResponse([], Http::STATUS_NOT_FOUND);
return new DataResponse(null, Http::STATUS_NOT_FOUND);
} catch (ReactionAlreadyExistsException $e) {
$status = Http::STATUS_OK;
} catch (ReactionNotSupportedException|ReactionOutOfContextException|\Exception $e) {
return new DataResponse([], Http::STATUS_BAD_REQUEST);
return new DataResponse(null, Http::STATUS_BAD_REQUEST);
}
$reactions = $this->reactionManager->retrieveReactionMessages($this->getRoom(), $this->getParticipant(), $messageId);
return new DataResponse($this->formatReactions($reactions), $status);
Expand All @@ -89,7 +89,7 @@ public function react(int $messageId, string $reaction): DataResponse {
* @param int $messageId ID of the message
* @psalm-param non-negative-int $messageId
* @param string $reaction Emoji to remove
* @return DataResponse<Http::STATUS_OK, array<string, TalkReaction[]>|\stdClass, array{}>|DataResponse<Http::STATUS_BAD_REQUEST|Http::STATUS_NOT_FOUND, array<empty>, array{}>
* @return DataResponse<Http::STATUS_OK, array<string, list<TalkReaction>>|\stdClass, array{}>|DataResponse<Http::STATUS_BAD_REQUEST|Http::STATUS_NOT_FOUND, null, array{}>
*
* 200: Reaction deleted successfully
* 400: Deleting reaction is not possible
Expand Down Expand Up @@ -118,9 +118,9 @@ public function delete(int $messageId, string $reaction): DataResponse {
);
$reactions = $this->reactionManager->retrieveReactionMessages($this->getRoom(), $this->getParticipant(), $messageId);
} catch (ReactionNotSupportedException|ReactionOutOfContextException|NotFoundException $e) {
return new DataResponse([], Http::STATUS_NOT_FOUND);
return new DataResponse(null, Http::STATUS_NOT_FOUND);
} catch (\Exception $e) {
return new DataResponse([], Http::STATUS_BAD_REQUEST);
return new DataResponse(null, Http::STATUS_BAD_REQUEST);
}

return new DataResponse($this->formatReactions($reactions), Http::STATUS_OK);
Expand All @@ -132,7 +132,7 @@ public function delete(int $messageId, string $reaction): DataResponse {
* @param int $messageId ID of the message
* @psalm-param non-negative-int $messageId
* @param string|null $reaction Emoji to filter
* @return DataResponse<Http::STATUS_OK, array<string, TalkReaction[]>|\stdClass, array{}>|DataResponse<Http::STATUS_NOT_FOUND, array<empty>, array{}>
* @return DataResponse<Http::STATUS_OK, array<string, list<TalkReaction>>|\stdClass, array{}>|DataResponse<Http::STATUS_NOT_FOUND, null, array{}>
*
* 200: Reactions returned
* 404: Message or reaction not found
Expand All @@ -152,7 +152,7 @@ public function getReactions(int $messageId, ?string $reaction): DataResponse {
// Verify that messageId is part of the room
$this->reactionManager->getCommentToReact($this->getRoom(), (string)$messageId);
} catch (ReactionNotSupportedException|ReactionOutOfContextException|NotFoundException $e) {
return new DataResponse([], Http::STATUS_NOT_FOUND);
return new DataResponse(null, Http::STATUS_NOT_FOUND);
}

$reactions = $this->reactionManager->retrieveReactionMessages($this->getRoom(), $this->getParticipant(), $messageId, $reaction);
Expand All @@ -161,8 +161,8 @@ public function getReactions(int $messageId, ?string $reaction): DataResponse {
}

/**
* @param array<string, TalkReaction[]> $reactions
* @return array<string, TalkReaction[]>|\stdClass
* @param array<string, list<TalkReaction>> $reactions
* @return array<string, list<TalkReaction>>|\stdClass
*/
protected function formatReactions(array $reactions): array|\stdClass {
if ($this->getResponseFormat() === 'json' && empty($reactions)) {
Expand Down
22 changes: 11 additions & 11 deletions lib/Federation/Proxy/TalkV1/Controller/ReactionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function __construct(
* @param int $messageId ID of the message
* @psalm-param non-negative-int $messageId
* @param string $reaction Emoji to add
* @return DataResponse<Http::STATUS_OK|Http::STATUS_CREATED, array<string, TalkReaction[]>|\stdClass, array{}>|DataResponse<Http::STATUS_BAD_REQUEST|Http::STATUS_NOT_FOUND, array<empty>, array{}>
* @return DataResponse<Http::STATUS_OK|Http::STATUS_CREATED, array<string, list<TalkReaction>>|\stdClass, array{}>|DataResponse<Http::STATUS_BAD_REQUEST|Http::STATUS_NOT_FOUND, null, array{}>
*
* 200: Reaction already existed
* 201: Reaction added successfully
Expand All @@ -59,10 +59,10 @@ public function react(Room $room, Participant $participant, int $messageId, stri
], true)) {
$statusCode = $this->proxy->logUnexpectedStatusCode(__METHOD__, $statusCode);
}
return new DataResponse([], $statusCode);
return new DataResponse(null, $statusCode);
}

/** @var array<string, TalkReaction[]> $data */
/** @var array<string, list<TalkReaction>> $data */
$data = $this->proxy->getOCSData($proxy, [Http::STATUS_CREATED, Http::STATUS_OK]);
$data = $this->userConverter->convertReactionsList($room, $data);

Expand All @@ -75,7 +75,7 @@ public function react(Room $room, Participant $participant, int $messageId, stri
* @param int $messageId ID of the message
* @psalm-param non-negative-int $messageId
* @param string $reaction Emoji to remove
* @return DataResponse<Http::STATUS_OK, array<string, TalkReaction[]>|\stdClass, array{}>|DataResponse<Http::STATUS_BAD_REQUEST|Http::STATUS_NOT_FOUND, array<empty>, array{}>
* @return DataResponse<Http::STATUS_OK, array<string, list<TalkReaction>>|\stdClass, array{}>|DataResponse<Http::STATUS_BAD_REQUEST|Http::STATUS_NOT_FOUND, null, array{}>
*
* 200: Reaction deleted successfully
* 400: Deleting reaction is not possible
Expand All @@ -101,10 +101,10 @@ public function delete(Room $room, Participant $participant, int $messageId, str
], true)) {
$statusCode = $this->proxy->logUnexpectedStatusCode(__METHOD__, $statusCode);
}
return new DataResponse([], $statusCode);
return new DataResponse(null, $statusCode);
}

/** @var array<string, TalkReaction[]> $data */
/** @var array<string, list<TalkReaction>> $data */
$data = $this->proxy->getOCSData($proxy);
$data = $this->userConverter->convertReactionsList($room, $data);

Expand All @@ -118,7 +118,7 @@ public function delete(Room $room, Participant $participant, int $messageId, str
* @param int $messageId ID of the message
* @psalm-param non-negative-int $messageId
* @param string|null $reaction Emoji to filter
* @return DataResponse<Http::STATUS_OK, array<string, TalkReaction[]>|\stdClass, array{}>|DataResponse<Http::STATUS_NOT_FOUND, array<empty>, array{}>
* @return DataResponse<Http::STATUS_OK, array<string, list<TalkReaction>>|\stdClass, array{}>|DataResponse<Http::STATUS_NOT_FOUND, null, array{}>
*
* 200: Reactions returned
* 404: Message or reaction not found
Expand All @@ -140,19 +140,19 @@ public function getReactions(Room $room, Participant $participant, int $messageI
if ($statusCode !== Http::STATUS_NOT_FOUND) {
$this->proxy->logUnexpectedStatusCode(__METHOD__, $statusCode);
}
return new DataResponse([], Http::STATUS_NOT_FOUND);
return new DataResponse(null, Http::STATUS_NOT_FOUND);
}

/** @var array<string, TalkReaction[]> $data */
/** @var array<string, list<TalkReaction>> $data */
$data = $this->proxy->getOCSData($proxy);
$data = $this->userConverter->convertReactionsList($room, $data);

return new DataResponse($this->formatReactions($format, $data), $statusCode);
}

/**
* @param array<string, TalkReaction[]> $reactions
* @return array<string, TalkReaction[]>|\stdClass
* @param array<string, list<TalkReaction>> $reactions
* @return array<string, list<TalkReaction>>|\stdClass
*/
protected function formatReactions(string $format, array $reactions): array|\stdClass {
if ($format === 'json' && empty($reactions)) {
Expand Down

0 comments on commit 8bc276e

Please sign in to comment.