|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * @copyright Copyright (c) 2024 Joas Schilling <[email protected]> |
| 7 | + * |
| 8 | + * @author Joas Schilling <[email protected]> |
| 9 | + * |
| 10 | + * @license GNU AGPL version 3 or any later version |
| 11 | + * |
| 12 | + * This program is free software: you can redistribute it and/or modify |
| 13 | + * it under the terms of the GNU Affero General Public License as |
| 14 | + * published by the Free Software Foundation, either version 3 of the |
| 15 | + * License, or (at your option) any later version. |
| 16 | + * |
| 17 | + * This program is distributed in the hope that it will be useful, |
| 18 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | + * GNU Affero General Public License for more details. |
| 21 | + * |
| 22 | + * You should have received a copy of the GNU Affero General Public License |
| 23 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 24 | + * |
| 25 | + */ |
| 26 | + |
| 27 | +namespace OCA\Talk\Federation\Proxy\TalkV1\Controller; |
| 28 | + |
| 29 | +use OCA\Talk\Exceptions\CannotReachRemoteException; |
| 30 | +use OCA\Talk\Federation\Proxy\TalkV1\ProxyRequest; |
| 31 | +use OCA\Talk\Federation\Proxy\TalkV1\UserConverter; |
| 32 | +use OCA\Talk\Participant; |
| 33 | +use OCA\Talk\ResponseDefinitions; |
| 34 | +use OCA\Talk\Room; |
| 35 | +use OCP\AppFramework\Http; |
| 36 | +use OCP\AppFramework\Http\DataResponse; |
| 37 | + |
| 38 | +/** |
| 39 | + * @psalm-import-type TalkPoll from ResponseDefinitions |
| 40 | + */ |
| 41 | +class PollController { |
| 42 | + public function __construct( |
| 43 | + protected ProxyRequest $proxy, |
| 44 | + protected UserConverter $userConverter, |
| 45 | + ) { |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * @return DataResponse<Http::STATUS_OK, TalkPoll, array{}>|DataResponse<Http::STATUS_NOT_FOUND, array<empty>, array{}> |
| 50 | + * @throws CannotReachRemoteException |
| 51 | + * |
| 52 | + * 200: Poll returned |
| 53 | + * 404: Poll not found |
| 54 | + * |
| 55 | + * @see \OCA\Talk\Controller\PollController::showPoll() |
| 56 | + */ |
| 57 | + public function showPoll(Room $room, Participant $participant, int $pollId): DataResponse { |
| 58 | + $proxy = $this->proxy->get( |
| 59 | + $participant->getAttendee()->getInvitedCloudId(), |
| 60 | + $participant->getAttendee()->getAccessToken(), |
| 61 | + $room->getRemoteServer() . '/ocs/v2.php/apps/spreed/api/v1/poll/' . $room->getRemoteToken() . '/' . $pollId, |
| 62 | + ); |
| 63 | + |
| 64 | + if ($proxy->getStatusCode() === Http::STATUS_NOT_FOUND) { |
| 65 | + return new DataResponse([], Http::STATUS_NOT_FOUND); |
| 66 | + } |
| 67 | + |
| 68 | + /** @var TalkPoll $data */ |
| 69 | + $data = $this->proxy->getOCSData($proxy); |
| 70 | + $data = $this->userConverter->convertPoll($room, $data); |
| 71 | + |
| 72 | + return new DataResponse($data); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * @return DataResponse<Http::STATUS_OK, TalkPoll, array{}>|DataResponse<Http::STATUS_BAD_REQUEST|Http::STATUS_NOT_FOUND, array<empty>, array{}> |
| 77 | + * @throws CannotReachRemoteException |
| 78 | + * |
| 79 | + * 200: Voted successfully |
| 80 | + * 400: Voting is not possible |
| 81 | + * 404: Poll not found |
| 82 | + * |
| 83 | + * @see \OCA\Talk\Controller\PollController::votePoll() |
| 84 | + */ |
| 85 | + public function votePoll(Room $room, Participant $participant, int $pollId, array $optionIds): DataResponse { |
| 86 | + $proxy = $this->proxy->post( |
| 87 | + $participant->getAttendee()->getInvitedCloudId(), |
| 88 | + $participant->getAttendee()->getAccessToken(), |
| 89 | + $room->getRemoteServer() . '/ocs/v2.php/apps/spreed/api/v1/poll/' . $room->getRemoteToken() . '/' . $pollId, |
| 90 | + ['optionIds' => $optionIds], |
| 91 | + ); |
| 92 | + |
| 93 | + $statusCode = $proxy->getStatusCode(); |
| 94 | + if ($statusCode !== Http::STATUS_OK) { |
| 95 | + if (!in_array($statusCode, [ |
| 96 | + Http::STATUS_BAD_REQUEST, |
| 97 | + Http::STATUS_NOT_FOUND, |
| 98 | + ], true)) { |
| 99 | + $statusCode = $this->proxy->logUnexpectedStatusCode(__METHOD__, $statusCode); |
| 100 | + } |
| 101 | + return new DataResponse([], $statusCode); |
| 102 | + } |
| 103 | + |
| 104 | + /** @var TalkPoll $data */ |
| 105 | + $data = $this->proxy->getOCSData($proxy); |
| 106 | + $data = $this->userConverter->convertPoll($room, $data); |
| 107 | + |
| 108 | + return new DataResponse($data); |
| 109 | + } |
| 110 | + |
| 111 | + |
| 112 | + /** |
| 113 | + * @return DataResponse<Http::STATUS_CREATED, TalkPoll, array{}>|DataResponse<Http::STATUS_BAD_REQUEST, array<empty>, array{}> |
| 114 | + * @throws CannotReachRemoteException |
| 115 | + * |
| 116 | + * 201: Poll created successfully |
| 117 | + * 400: Creating poll is not possible |
| 118 | + * |
| 119 | + * @see \OCA\Talk\Controller\PollController::createPoll() |
| 120 | + */ |
| 121 | + public function createPoll(Room $room, Participant $participant, string $question, array $options, int $resultMode, int $maxVotes): DataResponse { |
| 122 | + $proxy = $this->proxy->post( |
| 123 | + $participant->getAttendee()->getInvitedCloudId(), |
| 124 | + $participant->getAttendee()->getAccessToken(), |
| 125 | + $room->getRemoteServer() . '/ocs/v2.php/apps/spreed/api/v1/poll/' . $room->getRemoteToken(), |
| 126 | + [ |
| 127 | + 'question' => $question, |
| 128 | + 'options' => $options, |
| 129 | + 'resultMode' => $resultMode, |
| 130 | + 'maxVotes' => $maxVotes, |
| 131 | + ], |
| 132 | + ); |
| 133 | + |
| 134 | + if ($proxy->getStatusCode() === Http::STATUS_BAD_REQUEST) { |
| 135 | + return new DataResponse([], Http::STATUS_BAD_REQUEST); |
| 136 | + } |
| 137 | + |
| 138 | + /** @var TalkPoll $data */ |
| 139 | + $data = $this->proxy->getOCSData($proxy, [Http::STATUS_CREATED]); |
| 140 | + $data = $this->userConverter->convertPoll($room, $data); |
| 141 | + |
| 142 | + return new DataResponse($data, Http::STATUS_CREATED); |
| 143 | + } |
| 144 | + |
| 145 | + /** |
| 146 | + * @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{}> |
| 147 | + * @throws CannotReachRemoteException |
| 148 | + * |
| 149 | + * 200: Poll closed successfully |
| 150 | + * 400: Poll already closed |
| 151 | + * 403: Missing permissions to close poll |
| 152 | + * 404: Poll not found |
| 153 | + * |
| 154 | + * @see \OCA\Talk\Controller\PollController::closePoll() |
| 155 | + */ |
| 156 | + public function closePoll(Room $room, Participant $participant, int $pollId): DataResponse { |
| 157 | + $proxy = $this->proxy->delete( |
| 158 | + $participant->getAttendee()->getInvitedCloudId(), |
| 159 | + $participant->getAttendee()->getAccessToken(), |
| 160 | + $room->getRemoteServer() . '/ocs/v2.php/apps/spreed/api/v1/poll/' . $room->getRemoteToken() . '/' . $pollId, |
| 161 | + ); |
| 162 | + |
| 163 | + $statusCode = $proxy->getStatusCode(); |
| 164 | + if ($statusCode !== Http::STATUS_OK) { |
| 165 | + if (!in_array($statusCode, [ |
| 166 | + Http::STATUS_BAD_REQUEST, |
| 167 | + Http::STATUS_FORBIDDEN, |
| 168 | + Http::STATUS_NOT_FOUND, |
| 169 | + ], true)) { |
| 170 | + $statusCode = $this->proxy->logUnexpectedStatusCode(__METHOD__, $statusCode); |
| 171 | + } |
| 172 | + return new DataResponse([], $statusCode); |
| 173 | + } |
| 174 | + |
| 175 | + /** @var TalkPoll $data */ |
| 176 | + $data = $this->proxy->getOCSData($proxy); |
| 177 | + $data = $this->userConverter->convertPoll($room, $data); |
| 178 | + |
| 179 | + return new DataResponse($data); |
| 180 | + } |
| 181 | +} |
0 commit comments