Skip to content

Commit

Permalink
fixup! feat(polls): allow editing of draft polls
Browse files Browse the repository at this point in the history
  • Loading branch information
miaulalala committed Dec 9, 2024
1 parent d0c855c commit 2aeb3ef
Show file tree
Hide file tree
Showing 7 changed files with 350 additions and 248 deletions.
17 changes: 7 additions & 10 deletions docs/poll.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ Base endpoint is: `/ocs/v2.php/apps/spreed/api/v1`

# Edit a draft poll in a conversation

* Federation capability: `federation-v1`
* Required capability: `edit-draft-poll`
* Method: `POST`
* Endpoint: `/poll/{token}/draft/{pollId]}`
* Endpoint: `/poll/{token}/draft/{pollId}`
* Data:

| field | type | Description |
Expand All @@ -46,14 +46,11 @@ Base endpoint is: `/ocs/v2.php/apps/spreed/api/v1`

* Response:
- Status code:
+ `201 Created`
+ `400 Bad Request` When the room is a one-to-one conversation
+ `400 Bad Request` When the question or the options were too long or invalid (not strings)
+ `403 Forbidden` When the conversation is read-only
+ `403 Forbidden` When the actor does not have chat permissions
+ `404 Not Found` When the conversation could not be found for the participant
+ `412 Precondition Failed` When the lobby is active and the user is not a moderator

+ `200 OK`
+ `400 Bad Request` Modifying poll is not possible
+ `403 Forbidden` No permission to modify this poll
+ `404 Not Found` When the draft poll could not be found
- Data:

See [Poll data](#poll-data)
Expand Down
28 changes: 12 additions & 16 deletions lib/Controller/PollController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

namespace OCA\Talk\Controller;

use Exception;
use JsonException;
use OCA\Talk\Chat\ChatManager;
use OCA\Talk\Exceptions\PollPropertyException;
Expand Down Expand Up @@ -62,7 +61,7 @@ public function __construct(
* @psalm-param Poll::MODE_* $resultMode Mode how the results will be shown
* @param int $maxVotes Number of maximum votes per voter
* @param bool $draft Whether the poll should be saved as a draft (only allowed for moderators and with `talk-polls-drafts` capability)
* @return DataResponse<Http::STATUS_OK, TalkPollDraft, array{}>|DataResponse<Http::STATUS_CREATED, TalkPoll, array{}>|DataResponse<Http::STATUS_BAD_REQUEST, array{error: 'draft'|'options'|'question'|'room'}|array{error: string}, array{}>
* @return DataResponse<Http::STATUS_OK, TalkPollDraft, array{}>|DataResponse<Http::STATUS_CREATED, TalkPoll, array{}>|DataResponse<Http::STATUS_BAD_REQUEST, array{error: 'draft'|'options'|'poll'|'question'|'room'}, array{}>
*
* 200: Draft created successfully
* 201: Poll created successfully
Expand Down Expand Up @@ -105,7 +104,7 @@ public function createPoll(string $question, array $options, int $resultMode, in
);
} catch (PollPropertyException $e) {
$this->logger->error('Error creating poll', ['exception' => $e]);
return new DataResponse(['error' => $e->getReason()], Http::STATUS_BAD_REQUEST);
return new DataResponse(['error' => PollPropertyException::REASON_POLL], Http::STATUS_BAD_REQUEST);
}

if ($draft) {
Expand All @@ -127,7 +126,7 @@ public function createPoll(string $question, array $options, int $resultMode, in

try {
$this->chatManager->addSystemMessage($this->room, $attendee->getActorType(), $attendee->getActorId(), $message, $this->timeFactory->getDateTime(), true);
} catch (Exception $e) {
} catch (\Exception $e) {
$this->logger->error($e->getMessage(), ['exception' => $e]);
}

Expand All @@ -146,10 +145,11 @@ public function createPoll(string $question, array $options, int $resultMode, in
* @param 0|1 $resultMode Mode how the results will be shown
* @psalm-param Poll::MODE_* $resultMode Mode how the results will be shown
* @param int $maxVotes Number of maximum votes per voter
* @return DataResponse<Http::STATUS_OK, TalkPollDraft, array{}>|DataResponse<Http::STATUS_BAD_REQUEST, array{error: 'draft'|'options'|'question'|'room'}, array{}>|DataResponse<Http::STATUS_NOT_FOUND, array{error: string}, array{}>
* @return DataResponse<Http::STATUS_OK, TalkPollDraft, array{}>|DataResponse<Http::STATUS_BAD_REQUEST|Http::STATUS_FORBIDDEN, array{error: 'draft'|'options'|'question'|'room'}, array{}>|DataResponse<Http::STATUS_NOT_FOUND, array{error: string}, array{}>
*
* 200: Draft modified successfully
* 400: Modifying poll is not possible
* 403: No permission to modify this poll
* 404: No draft poll exists
*/
#[FederationSupported]
Expand Down Expand Up @@ -200,7 +200,7 @@ public function updateDraftPoll(int $pollId, string $question, array $options, i
$this->pollService->updatePoll($this->participant, $poll);
} catch (WrongPermissionsException $e) {
$this->logger->error('Error modifying poll', ['exception' => $e]);
return new DataResponse(['error' => $e->getMessage()], Http::STATUS_BAD_REQUEST);
return new DataResponse(['error' => 'poll'], Http::STATUS_FORBIDDEN);
}

return new DataResponse($poll->renderAsDraft());
Expand Down Expand Up @@ -333,7 +333,7 @@ public function votePoll(int $pollId, array $optionIds = []): DataResponse {
],
], JSON_THROW_ON_ERROR);
$this->chatManager->addSystemMessage($this->room, $attendee->getActorType(), $attendee->getActorId(), $message, $this->timeFactory->getDateTime(), false);
} catch (Exception $e) {
} catch (\Exception $e) {
$this->logger->error($e->getMessage(), ['exception' => $e]);
}
}
Expand All @@ -346,14 +346,13 @@ 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, null, array{}>|DataResponse<Http::STATUS_BAD_REQUEST|Http::STATUS_FORBIDDEN|Http::STATUS_NOT_FOUND|Http::STATUS_INTERNAL_SERVER_ERROR, array{error: string}, 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: 'poll'}|array{error: string}, array{}>
*
* 200: Poll closed successfully
* 202: Poll draft was deleted successfully
* 400: Poll already closed
* 403: Missing permissions to close poll
* 404: Poll not found
* 500: Poll could not be closed
*/
#[FederationSupported]
#[PublicPage]
Expand All @@ -369,7 +368,7 @@ public function closePoll(int $pollId): DataResponse {
try {
$poll = $this->pollService->getPoll($this->room->getId(), $pollId);
} catch (DoesNotExistException) {
return new DataResponse(['error' => 'poll'], Http::STATUS_NOT_FOUND);
return new DataResponse(['error' => PollPropertyException::REASON_POLL], Http::STATUS_NOT_FOUND);
}

if ($poll->getStatus() === Poll::STATUS_DRAFT) {
Expand All @@ -378,16 +377,13 @@ public function closePoll(int $pollId): DataResponse {
}

if ($poll->getStatus() === Poll::STATUS_CLOSED) {
return new DataResponse(['error' => 'poll'], Http::STATUS_BAD_REQUEST);
return new DataResponse(['error' => PollPropertyException::REASON_POLL], Http::STATUS_BAD_REQUEST);
}

try {
$this->pollService->closePoll($this->participant, $poll);
} catch (WrongPermissionsException $e) {
return new DataResponse(['error' => 'poll'], Http::STATUS_FORBIDDEN);
} catch (Exception $e) {
$this->logger->error($e->getMessage(), ['exception' => $e]);
return new DataResponse(['error' => 'poll'], Http::STATUS_INTERNAL_SERVER_ERROR);
return new DataResponse(['error' => PollPropertyException::REASON_POLL], Http::STATUS_FORBIDDEN);
}

$attendee = $this->participant->getAttendee();
Expand All @@ -403,7 +399,7 @@ public function closePoll(int $pollId): DataResponse {
],
], JSON_THROW_ON_ERROR);
$this->chatManager->addSystemMessage($this->room, $attendee->getActorType(), $attendee->getActorId(), $message, $this->timeFactory->getDateTime(), true);
} catch (Exception $e) {
} catch (\Exception $e) {
$this->logger->error($e->getMessage(), ['exception' => $e]);
}

Expand Down
5 changes: 3 additions & 2 deletions lib/Federation/Proxy/TalkV1/Controller/PollController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace OCA\Talk\Federation\Proxy\TalkV1\Controller;

use OCA\Talk\Exceptions\CannotReachRemoteException;
use OCA\Talk\Exceptions\PollPropertyException;
use OCA\Talk\Federation\Proxy\TalkV1\ProxyRequest;
use OCA\Talk\Federation\Proxy\TalkV1\UserConverter;
use OCA\Talk\Participant;
Expand Down Expand Up @@ -210,7 +211,7 @@ public function updateDraftPoll(int $pollId, Room $room, Participant $participan
}

/**
* @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{}>
* @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: 'poll'}|array{error: string}, array{}>
* @throws CannotReachRemoteException
*
* 200: Poll closed successfully
Expand Down Expand Up @@ -238,7 +239,7 @@ public function closePoll(Room $room, Participant $participant, int $pollId): Da
}
/** @var array{error?: string} $data */
$data = $this->proxy->getOCSData($proxy);
return new DataResponse(['error' => $data['error'] ?? 'poll'], $statusCode);
return new DataResponse(['error' => $data['error'] ?? PollPropertyException::REASON_POLL], $statusCode);
}

/** @var TalkPoll $data */
Expand Down
Loading

0 comments on commit 2aeb3ef

Please sign in to comment.