Skip to content

Commit

Permalink
fix(openapi): Fix empty array on polls API
Browse files Browse the repository at this point in the history
Signed-off-by: Joas Schilling <[email protected]>
  • Loading branch information
nickvergessen committed Nov 21, 2024
1 parent 44f1714 commit 7be9167
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 25 deletions.
33 changes: 15 additions & 18 deletions lib/Controller/PollController.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ public function getAllDraftPolls(): DataResponse {
*
* @param int $pollId ID of the poll
* @psalm-param non-negative-int $pollId
* @return DataResponse<Http::STATUS_OK, TalkPoll, array{}>|DataResponse<Http::STATUS_NOT_FOUND, array<empty>, array{}>
* @return DataResponse<Http::STATUS_OK, TalkPoll, array{}>|DataResponse<Http::STATUS_NOT_FOUND, array{error: string}, array{}>
*
* 200: Poll returned
* 404: Poll not found
Expand All @@ -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);
Expand All @@ -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<Http::STATUS_OK, TalkPoll, array{}>|DataResponse<Http::STATUS_BAD_REQUEST|Http::STATUS_NOT_FOUND, array<empty>, array{}>
* @param list<int> $optionIds IDs of the selected options
* @return DataResponse<Http::STATUS_OK, TalkPoll, array{}>|DataResponse<Http::STATUS_BAD_REQUEST|Http::STATUS_NOT_FOUND, array{error: string}, array{}>
*
* 200: Voted successfully
* 400: Voting is not possible
Expand All @@ -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) {
Expand Down Expand Up @@ -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<Http::STATUS_OK, TalkPoll, array{}>|DataResponse<Http::STATUS_ACCEPTED|Http::STATUS_BAD_REQUEST|Http::STATUS_FORBIDDEN|Http::STATUS_NOT_FOUND|Http::STATUS_INTERNAL_SERVER_ERROR, array<empty>, array{}>
* @return DataResponse<Http::STATUS_OK, TalkPoll, array{}>|DataResponse<Http::STATUS_ACCEPTED, null, array{}>|DataResponse<Http::STATUS_BAD_REQUEST|Http::STATUS_FORBIDDEN|Http::STATUS_NOT_FOUND, array{error: string}, array{}>
*
* 200: Poll closed successfully
* 202: Poll draft was deleted successfully
Expand All @@ -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();
Expand Down
19 changes: 13 additions & 6 deletions lib/Federation/Proxy/TalkV1/Controller/PollController.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function getDraftsForRoom(Room $room, Participant $participant): DataResp
}

/**
* @return DataResponse<Http::STATUS_OK, TalkPoll, array{}>|DataResponse<Http::STATUS_NOT_FOUND, array<empty>, array{}>
* @return DataResponse<Http::STATUS_OK, TalkPoll, array{}>|DataResponse<Http::STATUS_NOT_FOUND, array{error: string}, array{}>
* @throws CannotReachRemoteException
*
* 200: Poll returned
Expand All @@ -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 */
Expand All @@ -89,7 +91,8 @@ public function showPoll(Room $room, Participant $participant, int $pollId): Dat
}

/**
* @return DataResponse<Http::STATUS_OK, TalkPoll, array{}>|DataResponse<Http::STATUS_BAD_REQUEST|Http::STATUS_NOT_FOUND, array<empty>, array{}>
* @param list<int> $optionIds
* @return DataResponse<Http::STATUS_OK, TalkPoll, array{}>|DataResponse<Http::STATUS_BAD_REQUEST|Http::STATUS_NOT_FOUND, array{error: string}, array{}>
* @throws CannotReachRemoteException
*
* 200: Voted successfully
Expand All @@ -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 */
Expand Down Expand Up @@ -166,7 +171,7 @@ public function createPoll(Room $room, Participant $participant, string $questio
}

/**
* @return DataResponse<Http::STATUS_OK, TalkPoll, array{}>|DataResponse<Http::STATUS_BAD_REQUEST|Http::STATUS_FORBIDDEN|Http::STATUS_NOT_FOUND|Http::STATUS_INTERNAL_SERVER_ERROR, array<empty>, array{}>
* @return DataResponse<Http::STATUS_OK, TalkPoll, array{}>|DataResponse<Http::STATUS_ACCEPTED, null, array{}>|DataResponse<Http::STATUS_BAD_REQUEST|Http::STATUS_FORBIDDEN|Http::STATUS_NOT_FOUND, array{error: string}, array{}>
* @throws CannotReachRemoteException
*
* 200: Poll closed successfully
Expand All @@ -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 */
Expand Down
1 change: 0 additions & 1 deletion lib/Service/PollService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 7be9167

Please sign in to comment.