From 1a6c5c6a827055ce30ab9c9c51a25a179f08eade Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Thu, 21 Nov 2024 09:07:39 +0100 Subject: [PATCH] fix(openapi): Fix poll returned lists Signed-off-by: Joas Schilling --- lib/Controller/PollController.php | 37 +++++++++---------- .../TalkV1/Controller/PollController.php | 19 +++++++--- lib/ResponseDefinitions.php | 4 +- lib/Service/PollService.php | 1 - 4 files changed, 32 insertions(+), 29 deletions(-) diff --git a/lib/Controller/PollController.php b/lib/Controller/PollController.php index 0b4722d47c6..c2ac0b99592 100644 --- a/lib/Controller/PollController.php +++ b/lib/Controller/PollController.php @@ -169,7 +169,7 @@ public function getAllDraftPolls(): DataResponse { * * @param int $pollId ID of the poll * @psalm-param non-negative-int $pollId - * @return DataResponse|DataResponse, array{}> + * @return DataResponse|DataResponse * * 200: Poll returned * 404: Poll not found @@ -188,11 +188,11 @@ public function showPoll(int $pollId): DataResponse { try { $poll = $this->pollService->getPoll($this->room->getId(), $pollId); } catch (DoesNotExistException) { - return new DataResponse([], Http::STATUS_NOT_FOUND); + return new DataResponse(['error' => 'poll'], Http::STATUS_NOT_FOUND); } if ($poll->getStatus() === Poll::STATUS_DRAFT && !$this->participant->hasModeratorPermissions()) { - return new DataResponse([], Http::STATUS_NOT_FOUND); + return new DataResponse(['error' => 'poll'], Http::STATUS_NOT_FOUND); } $votedSelf = $this->pollService->getVotesForActor($this->participant, $poll); @@ -209,8 +209,8 @@ public function showPoll(int $pollId): DataResponse { * * @param int $pollId ID of the poll * @psalm-param non-negative-int $pollId - * @param int[] $optionIds IDs of the selected options - * @return DataResponse|DataResponse, array{}> + * @param list $optionIds IDs of the selected options + * @return DataResponse|DataResponse * * 200: Voted successfully * 400: Voting is not possible @@ -230,21 +230,21 @@ public function votePoll(int $pollId, array $optionIds = []): DataResponse { try { $poll = $this->pollService->getPoll($this->room->getId(), $pollId); } catch (DoesNotExistException) { - return new DataResponse([], Http::STATUS_NOT_FOUND); + return new DataResponse(['error' => 'poll'], Http::STATUS_NOT_FOUND); } if ($poll->getStatus() === Poll::STATUS_DRAFT) { - return new DataResponse([], Http::STATUS_NOT_FOUND); + return new DataResponse(['error' => 'poll'], Http::STATUS_NOT_FOUND); } if ($poll->getStatus() === Poll::STATUS_CLOSED) { - return new DataResponse([], Http::STATUS_BAD_REQUEST); + return new DataResponse(['error' => 'poll'], Http::STATUS_BAD_REQUEST); } try { $votedSelf = $this->pollService->votePoll($this->participant, $poll, $optionIds); } catch (\RuntimeException $e) { - return new DataResponse([], Http::STATUS_BAD_REQUEST); + return new DataResponse(['error' => 'options'], Http::STATUS_BAD_REQUEST); } if ($poll->getResultMode() === Poll::MODE_PUBLIC) { @@ -274,7 +274,7 @@ public function votePoll(int $pollId, array $optionIds = []): DataResponse { * * @param int $pollId ID of the poll * @psalm-param non-negative-int $pollId - * @return DataResponse|DataResponse, array{}> + * @return DataResponse|DataResponse|DataResponse * * 200: Poll closed successfully * 202: Poll draft was deleted successfully @@ -296,27 +296,24 @@ public function closePoll(int $pollId): DataResponse { try { $poll = $this->pollService->getPoll($this->room->getId(), $pollId); } catch (DoesNotExistException) { - return new DataResponse([], Http::STATUS_NOT_FOUND); + return new DataResponse(['error' => 'poll'], Http::STATUS_NOT_FOUND); } if ($poll->getStatus() === Poll::STATUS_DRAFT) { $this->pollService->deleteByPollId($poll->getId()); - return new DataResponse([], Http::STATUS_ACCEPTED); + return new DataResponse(null, Http::STATUS_ACCEPTED); } if ($poll->getStatus() === Poll::STATUS_CLOSED) { - return new DataResponse([], Http::STATUS_BAD_REQUEST); + return new DataResponse(['error' => 'poll'], Http::STATUS_BAD_REQUEST); } $poll->setStatus(Poll::STATUS_CLOSED); try { $this->pollService->updatePoll($this->participant, $poll); - } catch (WrongPermissionsException $e) { - return new DataResponse([], Http::STATUS_FORBIDDEN); - } catch (Exception $e) { - $this->logger->error($e->getMessage(), ['exception' => $e]); - return new DataResponse([], Http::STATUS_INTERNAL_SERVER_ERROR); + } catch (WrongPermissionsException) { + return new DataResponse(['error' => 'poll'], Http::STATUS_FORBIDDEN); } $attendee = $this->participant->getAttendee(); @@ -366,10 +363,10 @@ protected function renderPoll(Poll $poll, array $votedSelf = [], array $detailed $data['numVoters'] = 0; } } elseif ($poll->getResultMode() === Poll::MODE_PUBLIC && $poll->getStatus() === Poll::STATUS_CLOSED) { - $data['details'] = array_map(static fn (Vote $vote) => $vote->asArray(), $detailedVotes); + $data['details'] = array_values(array_map(static fn (Vote $vote) => $vote->asArray(), $detailedVotes)); } - $data['votedSelf'] = array_map(static fn (Vote $vote) => $vote->getOptionId(), $votedSelf); + $data['votedSelf'] = array_values(array_map(static fn (Vote $vote) => $vote->getOptionId(), $votedSelf)); return $data; } diff --git a/lib/Federation/Proxy/TalkV1/Controller/PollController.php b/lib/Federation/Proxy/TalkV1/Controller/PollController.php index 7708b020962..8601806ecfa 100644 --- a/lib/Federation/Proxy/TalkV1/Controller/PollController.php +++ b/lib/Federation/Proxy/TalkV1/Controller/PollController.php @@ -62,7 +62,7 @@ public function getDraftsForRoom(Room $room, Participant $participant): DataResp } /** - * @return DataResponse|DataResponse, array{}> + * @return DataResponse|DataResponse * @throws CannotReachRemoteException * * 200: Poll returned @@ -78,7 +78,9 @@ public function showPoll(Room $room, Participant $participant, int $pollId): Dat ); if ($proxy->getStatusCode() === Http::STATUS_NOT_FOUND) { - return new DataResponse([], Http::STATUS_NOT_FOUND); + /** @var array{error?: string} $data */ + $data = $this->proxy->getOCSData($proxy); + return new DataResponse(['error' => $data['error'] ?? 'poll'], Http::STATUS_NOT_FOUND); } /** @var TalkPoll $data */ @@ -89,7 +91,8 @@ public function showPoll(Room $room, Participant $participant, int $pollId): Dat } /** - * @return DataResponse|DataResponse, array{}> + * @param list $optionIds + * @return DataResponse|DataResponse * @throws CannotReachRemoteException * * 200: Voted successfully @@ -114,7 +117,9 @@ public function votePoll(Room $room, Participant $participant, int $pollId, arra ], true)) { $statusCode = $this->proxy->logUnexpectedStatusCode(__METHOD__, $statusCode); } - return new DataResponse([], $statusCode); + /** @var array{error?: string} $data */ + $data = $this->proxy->getOCSData($proxy); + return new DataResponse(['error' => $data['error'] ?? 'poll'], $statusCode); } /** @var TalkPoll $data */ @@ -166,7 +171,7 @@ public function createPoll(Room $room, Participant $participant, string $questio } /** - * @return DataResponse|DataResponse, array{}> + * @return DataResponse|DataResponse|DataResponse * @throws CannotReachRemoteException * * 200: Poll closed successfully @@ -192,7 +197,9 @@ public function closePoll(Room $room, Participant $participant, int $pollId): Da ], true)) { $statusCode = $this->proxy->logUnexpectedStatusCode(__METHOD__, $statusCode); } - return new DataResponse([], $statusCode); + /** @var array{error?: string} $data */ + $data = $this->proxy->getOCSData($proxy); + return new DataResponse(['error' => $data['error'] ?? 'poll'], $statusCode); } /** @var TalkPoll $data */ diff --git a/lib/ResponseDefinitions.php b/lib/ResponseDefinitions.php index f2854d51362..02105a8a216 100644 --- a/lib/ResponseDefinitions.php +++ b/lib/ResponseDefinitions.php @@ -213,9 +213,9 @@ * } * * @psalm-type TalkPoll = TalkPollDraft&array{ - * details?: TalkPollVote[], + * details?: list, * numVoters?: int<0, max>, - * votedSelf?: int[], + * votedSelf?: list, * votes?: array, * } * diff --git a/lib/Service/PollService.php b/lib/Service/PollService.php index 3048a2ed9fa..95d93a8fe7f 100644 --- a/lib/Service/PollService.php +++ b/lib/Service/PollService.php @@ -119,7 +119,6 @@ public function getPoll(int $roomId, int $pollId): Poll { * @param Participant $participant * @param Poll $poll * @throws WrongPermissionsException - * @throws Exception */ public function updatePoll(Participant $participant, Poll $poll): void { if (!$participant->hasModeratorPermissions()