Browse Source

feat(polls): Use different status code for drafts

Signed-off-by: Joas Schilling <coding@schilljs.com>
pull/13506/head
Joas Schilling 1 year ago
parent
commit
02e6a68dbb
No known key found for this signature in database GPG Key ID: F72FA5B49FFA96B0
  1. 41
      lib/Controller/PollController.php
  2. 10
      lib/Federation/Proxy/TalkV1/Controller/PollController.php
  3. 30
      openapi-full.json
  4. 30
      openapi.json
  5. 14
      src/types/openapi/openapi-full.ts
  6. 14
      src/types/openapi/openapi.ts
  7. 2
      tests/integration/features/bootstrap/FeatureContext.php
  8. 4
      tests/integration/features/chat-3/poll.feature

41
lib/Controller/PollController.php

@ -61,8 +61,9 @@ class PollController extends AEnvironmentAwareController {
* @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_CREATED, TalkPoll, array{}>|DataResponse<Http::STATUS_BAD_REQUEST, array<empty>, array{}>
* @return DataResponse<Http::STATUS_OK, TalkPollDraft, array{}>|DataResponse<Http::STATUS_CREATED, TalkPoll, array{}>|DataResponse<Http::STATUS_BAD_REQUEST, array<empty>, array{}>
*
* 200: Draft created successfully
* 201: Poll created successfully
* 400: Creating poll is not possible
*/
@ -106,25 +107,27 @@ class PollController extends AEnvironmentAwareController {
return new DataResponse([], Http::STATUS_BAD_REQUEST);
}
if (!$draft) {
$message = json_encode([
'message' => 'object_shared',
'parameters' => [
'objectType' => 'talk-poll',
'objectId' => $poll->getId(),
'metaData' => [
'type' => 'talk-poll',
'id' => $poll->getId(),
'name' => $question,
]
],
], JSON_THROW_ON_ERROR);
if ($draft) {
return new DataResponse($poll->renderAsDraft());
}
try {
$this->chatManager->addSystemMessage($this->room, $attendee->getActorType(), $attendee->getActorId(), $message, $this->timeFactory->getDateTime(), true);
} catch (\Exception $e) {
$this->logger->error($e->getMessage(), ['exception' => $e]);
}
$message = json_encode([
'message' => 'object_shared',
'parameters' => [
'objectType' => 'talk-poll',
'objectId' => $poll->getId(),
'metaData' => [
'type' => 'talk-poll',
'id' => $poll->getId(),
'name' => $question,
]
],
], JSON_THROW_ON_ERROR);
try {
$this->chatManager->addSystemMessage($this->room, $attendee->getActorType(), $attendee->getActorId(), $message, $this->timeFactory->getDateTime(), true);
} catch (\Exception $e) {
$this->logger->error($e->getMessage(), ['exception' => $e]);
}
return new DataResponse($this->renderPoll($poll), Http::STATUS_CREATED);

10
lib/Federation/Proxy/TalkV1/Controller/PollController.php

@ -126,7 +126,7 @@ class PollController {
/**
* @return DataResponse<Http::STATUS_CREATED, TalkPoll, array{}>|DataResponse<Http::STATUS_BAD_REQUEST, array<empty>, array{}>
* @return DataResponse<Http::STATUS_OK, TalkPollDraft, array{}>|DataResponse<Http::STATUS_CREATED, TalkPoll, array{}>|DataResponse<Http::STATUS_BAD_REQUEST, array<empty>, array{}>
* @throws CannotReachRemoteException
*
* 201: Poll created successfully
@ -148,14 +148,18 @@ class PollController {
],
);
if ($proxy->getStatusCode() === Http::STATUS_BAD_REQUEST) {
$status = $proxy->getStatusCode();
if ($status === Http::STATUS_BAD_REQUEST) {
return new DataResponse([], Http::STATUS_BAD_REQUEST);
}
/** @var TalkPoll $data */
$data = $this->proxy->getOCSData($proxy, [Http::STATUS_CREATED]);
$data = $this->proxy->getOCSData($proxy, [Http::STATUS_OK, Http::STATUS_CREATED]);
$data = $this->userConverter->convertPoll($room, $data);
if ($status === Http::STATUS_OK) {
return new DataResponse($data);
}
return new DataResponse($data, Http::STATUS_CREATED);
}

30
openapi-full.json

@ -8781,6 +8781,36 @@
}
],
"responses": {
"200": {
"description": "Draft created successfully",
"content": {
"application/json": {
"schema": {
"type": "object",
"required": [
"ocs"
],
"properties": {
"ocs": {
"type": "object",
"required": [
"meta",
"data"
],
"properties": {
"meta": {
"$ref": "#/components/schemas/OCSMeta"
},
"data": {
"$ref": "#/components/schemas/PollDraft"
}
}
}
}
}
}
}
},
"201": {
"description": "Poll created successfully",
"content": {

30
openapi.json

@ -8668,6 +8668,36 @@
}
],
"responses": {
"200": {
"description": "Draft created successfully",
"content": {
"application/json": {
"schema": {
"type": "object",
"required": [
"ocs"
],
"properties": {
"ocs": {
"type": "object",
"required": [
"meta",
"data"
],
"properties": {
"meta": {
"$ref": "#/components/schemas/OCSMeta"
},
"data": {
"$ref": "#/components/schemas/PollDraft"
}
}
}
}
}
}
}
},
"201": {
"description": "Poll created successfully",
"content": {

14
src/types/openapi/openapi-full.ts

@ -5143,6 +5143,20 @@ export interface operations {
};
};
responses: {
/** @description Draft created successfully */
200: {
headers: {
[name: string]: unknown;
};
content: {
"application/json": {
ocs: {
meta: components["schemas"]["OCSMeta"];
data: components["schemas"]["PollDraft"];
};
};
};
};
/** @description Poll created successfully */
201: {
headers: {

14
src/types/openapi/openapi.ts

@ -4624,6 +4624,20 @@ export interface operations {
};
};
responses: {
/** @description Draft created successfully */
200: {
headers: {
[name: string]: unknown;
};
content: {
"application/json": {
ocs: {
meta: components["schemas"]["OCSMeta"];
data: components["schemas"]["PollDraft"];
};
};
};
};
/** @description Poll created successfully */
201: {
headers: {

2
tests/integration/features/bootstrap/FeatureContext.php

@ -2418,7 +2418,7 @@ class FeatureContext implements Context, SnippetAcceptingContext {
);
$this->assertStatusCode($this->response, $statusCode);
if ($statusCode !== '201') {
if ($statusCode !== '200' && $statusCode !== '201') {
return;
}

4
tests/integration/features/chat-3/poll.feature

@ -811,13 +811,13 @@ Feature: chat-2/poll
| roomType | 2 |
| roomName | room |
When user "participant1" adds user "participant2" to room "room" with 200 (v4)
When user "participant1" creates a poll in room "room" with 201
When user "participant1" creates a poll in room "room" with 200
| question | What is the question? |
| options | ["You","me"] |
| resultMode | public |
| maxVotes | unlimited |
| draft | 1 |
When user "participant1" creates a poll in room "room" with 201
When user "participant1" creates a poll in room "room" with 200
| question | Shall we draft 2 questions? |
| options | ["Yes","No"] |
| resultMode | hidden |

Loading…
Cancel
Save