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 3, 2024
1 parent 9dcf018 commit f7b6615
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 15 deletions.
15 changes: 6 additions & 9 deletions lib/Controller/PollController.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ 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, array{error: 'draft'|'options'|'question'|'room'}, array{}>|DataResponse<Http::STATUS_NOT_FOUND, array{error: string}, array{}>

Check failure on line 148 in lib/Controller/PollController.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis

InvalidReturnType

lib/Controller/PollController.php:148:13: InvalidReturnType: The declared return type 'OCP\AppFramework\Http\DataResponse<200|400|404, array{actorDisplayName?: string, actorId?: non-empty-string, actorType?: 'bots'|'bridged'|'circles'|'emails'|'federated_users'|'groups'|'guests'|'phones'|'users', error?: string, id?: int<1, max>, maxVotes?: int<0, max>, options?: list<string>, question?: non-empty-string, resultMode?: 0|1, status?: 0|1|2}, array<never, never>>' for OCA\Talk\Controller\PollController::updateDraftPoll is incorrect, got 'OCP\AppFramework\Http\DataResponse<200|201|400|404, array{actorDisplayName?: string, actorId?: non-empty-string, actorType?: 'bots'|'bridged'|'circles'|'emails'|'federated_users'|'groups'|'guests'|'phones'|'users', details?: list<array{actorDisplayName: string, actorId: string, actorType: string, optionId: int}>, error?: mixed|string, id?: int<1, max>, maxVotes?: int<0, max>, numVoters?: int<0, max>, options?: list<string>, question?: non-empty-string, resultMode?: 0|1, status?: 0|1|2, votedSelf?: list<int>, votes?: array<string, int>}, array<never, never>>' (see https://psalm.dev/011)
*
* 200: Draft modified successfully
* 400: Modifying poll is not possible
Expand All @@ -161,7 +161,7 @@ public function updateDraftPoll(int $pollId, string $question, array $options, i
if ($this->room->isFederatedConversation()) {
/** @var \OCA\Talk\Federation\Proxy\TalkV1\Controller\PollController $proxy */
$proxy = \OCP\Server::get(\OCA\Talk\Federation\Proxy\TalkV1\Controller\PollController::class);
return $proxy->updateDraftPoll($pollId, $this->room, $this->participant, $question, $options, $resultMode, $maxVotes, $draft);
return $proxy->updateDraftPoll($pollId, $this->room, $this->participant, $question, $options, $resultMode, $maxVotes);

Check failure on line 164 in lib/Controller/PollController.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis

InvalidReturnStatement

lib/Controller/PollController.php:164:11: InvalidReturnStatement: The inferred type 'OCP\AppFramework\Http\DataResponse<200|201|400, array{actorDisplayName?: string, actorId?: non-empty-string, actorType?: 'bots'|'bridged'|'circles'|'emails'|'federated_users'|'groups'|'guests'|'phones'|'users', details?: list<array{actorDisplayName: string, actorId: string, actorType: string, optionId: int}>, error?: 'draft'|'options'|'question'|'room', id?: int<1, max>, maxVotes?: int<0, max>, numVoters?: int<0, max>, options?: list<string>, question?: non-empty-string, resultMode?: 0|1, status?: 0|1|2, votedSelf?: list<int>, votes?: array<string, int>}, array<never, never>>' does not match the declared return type 'OCP\AppFramework\Http\DataResponse<200|400|404, array{actorDisplayName?: string, actorId?: non-empty-string, actorType?: 'bots'|'bridged'|'circles'|'emails'|'federated_users'|'groups'|'guests'|'phones'|'users', error?: string, id?: int<1, max>, maxVotes?: int<0, max>, options?: list<string>, question?: non-empty-string, resultMode?: 0|1, status?: 0|1|2}, array<never, never>>' for OCA\Talk\Controller\PollController::updateDraftPoll (see https://psalm.dev/128)
}

if ($this->room->getType() !== Room::TYPE_GROUP
Expand All @@ -186,18 +186,15 @@ public function updateDraftPoll(int $pollId, string $question, array $options, i
}

try {
$question = $this->pollService->validatePollQuestion($question);
$encodedOptions = $this->pollService->validatePollOptions($options);
$poll->setQuestion($question);
$poll->setOptions($options);
$poll->setResultMode($resultMode);
$poll->setMaxVotes($maxVotes);
} catch (PollPropertyException $e) {
$this->logger->error('Error modifying poll', ['exception' => $e]);
return new DataResponse(['error' => $e->getReason()], Http::STATUS_BAD_REQUEST);
}

$poll->setQuestion($question);
$poll->setOptions($encodedOptions);
$poll->setResultMode($resultMode);
$poll->setMaxVotes($maxVotes);

try {
$this->pollService->updatePoll($this->participant, $poll);
} catch (WrongPermissionsException $e) {
Expand Down
52 changes: 50 additions & 2 deletions lib/Model/Poll.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace OCA\Talk\Model;

use OCA\Talk\Exceptions\PollPropertyException;
use OCA\Talk\ResponseDefinitions;
use OCP\AppFramework\Db\Entity;
use OCP\DB\Types;
Expand All @@ -18,10 +19,8 @@
* @method void setRoomId(int $roomId)
* @method int getRoomId()
* @psalm-method int<1, max> getRoomId()
* @method void setQuestion(string $question)
* @method string getQuestion()
* @psalm-method non-empty-string getQuestion()
* @method void setOptions(string $options)
* @method string getOptions()
* @method void setVotes(string $votes)
* @method string getVotes()
Expand Down Expand Up @@ -125,4 +124,53 @@ public function renderAsDraft(): array {
public function isDraft(): bool {
return $this->getStatus() === self::STATUS_DRAFT;
}

/**
* @param array $options
* @return void
* @throws PollPropertyException
*/
public function setOptions(array $options): void {
try {
$jsonOptions = json_encode($options, JSON_THROW_ON_ERROR, 1);
} catch (\Exception) {
throw new PollPropertyException(PollPropertyException::REASON_OPTIONS);
}

$validOptions = [];
foreach ($options as $option) {
if (!is_string($option)) {
throw new PollPropertyException(PollPropertyException::REASON_OPTIONS);
}

$option = trim($option);
if ($option !== '') {
$validOptions[] = $option;
}
}

if (count($validOptions) < 2) {
throw new PollPropertyException(PollPropertyException::REASON_OPTIONS);
}

if (strlen($jsonOptions) > 60_000) {
throw new PollPropertyException(PollPropertyException::REASON_OPTIONS);
}

$this->options = $jsonOptions;
}

/**
* @param string $question
* @return void
* @throws PollPropertyException
*/
public function setQuestion(string $question): void {
$question = trim($question);
if ($question === '' || strlen($question) > 32_000) {
throw new PollPropertyException(PollPropertyException::REASON_QUESTION);
}

$this->question = $question;
}
}
5 changes: 1 addition & 4 deletions lib/Service/PollService.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,13 @@ public function __construct(
* @throws PollPropertyException
*/
public function createPoll(int $roomId, string $actorType, string $actorId, string $displayName, string $question, array $options, int $resultMode, int $maxVotes, bool $draft): Poll {
$question = $this->validatePollQuestion($question);
$jsonOptions = $this->validatePollOptions($options);

$poll = new Poll();
$poll->setRoomId($roomId);
$poll->setActorType($actorType);
$poll->setActorId($actorId);
$poll->setDisplayName($displayName);
$poll->setQuestion($question);
$poll->setOptions($jsonOptions);
$poll->setOptions($options);
$poll->setVotes(json_encode([]));
$poll->setResultMode($resultMode);
$poll->setMaxVotes($maxVotes);
Expand Down

0 comments on commit f7b6615

Please sign in to comment.