Skip to content

Commit

Permalink
Merge pull request #9945 from nextcloud/feat/document-ocs-apis
Browse files Browse the repository at this point in the history
feat(ocs): document get api and amend return types
  • Loading branch information
miaulalala authored Aug 5, 2024
2 parents 01dba1a + 929873d commit 5280498
Show file tree
Hide file tree
Showing 29 changed files with 156 additions and 18 deletions.
2 changes: 2 additions & 0 deletions lib/Controller/AccountsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,15 @@
use OCA\Mail\Service\Sync\SyncService;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\Attribute\OpenAPI;
use OCP\AppFramework\Http\JSONResponse;
use OCP\IConfig;
use OCP\IL10N;
use OCP\IRequest;
use OCP\Security\IRemoteHostValidator;
use Psr\Log\LoggerInterface;

#[OpenAPI(scope: OpenAPI::SCOPE_IGNORE)]
class AccountsController extends Controller {
private AccountService $accountService;
private string $currentUserId;
Expand Down
2 changes: 2 additions & 0 deletions lib/Controller/AliasesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
use OCP\AppFramework\Controller;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\Attribute\OpenAPI;
use OCP\AppFramework\Http\JSONResponse;
use OCP\IRequest;

#[OpenAPI(scope: OpenAPI::SCOPE_IGNORE)]
class AliasesController extends Controller {
private AliasesService $aliasService;
private string $currentUserId;
Expand Down
2 changes: 2 additions & 0 deletions lib/Controller/AutoCompleteController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@
use OCA\Mail\Http\TrapError;
use OCA\Mail\Service\AutoCompletion\AutoCompleteService;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\Attribute\OpenAPI;
use OCP\AppFramework\Http\JSONResponse;
use OCP\IRequest;

#[OpenAPI(scope: OpenAPI::SCOPE_IGNORE)]
class AutoCompleteController extends Controller {
private AutoCompleteService $service;
private ?string $userId;
Expand Down
2 changes: 2 additions & 0 deletions lib/Controller/AutoConfigController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@
use OCA\Mail\Service\AutoConfig\MxRecord;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\Attribute\OpenAPI;
use OCP\AppFramework\Http\Attribute\UserRateLimit;
use OCP\IRequest;
use OCP\Security\IRemoteHostValidator;
use function in_array;

#[OpenAPI(scope: OpenAPI::SCOPE_IGNORE)]
class AutoConfigController extends Controller {
private IspDb $ispDb;
private MxRecord $mxRecord;
Expand Down
2 changes: 2 additions & 0 deletions lib/Controller/AvatarsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@
use OCA\Mail\Http\TrapError;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\Attribute\OpenAPI;
use OCP\AppFramework\Http\JSONResponse;
use OCP\AppFramework\Http\Response;
use OCP\IRequest;

#[OpenAPI(scope: OpenAPI::SCOPE_IGNORE)]
class AvatarsController extends Controller {
private IAvatarService $avatarService;
private string $uid;
Expand Down
2 changes: 2 additions & 0 deletions lib/Controller/ContactIntegrationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@
use OCA\Mail\Service\ContactIntegration\ContactIntegrationService;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\Attribute\OpenAPI;
use OCP\AppFramework\Http\JSONResponse;
use OCP\IRequest;

#[OpenAPI(scope: OpenAPI::SCOPE_IGNORE)]
class ContactIntegrationController extends Controller {
private ContactIntegrationService $service;

Expand Down
2 changes: 2 additions & 0 deletions lib/Controller/DraftsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@
use OCP\AppFramework\Controller;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\Attribute\OpenAPI;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\IRequest;

#[OpenAPI(scope: OpenAPI::SCOPE_IGNORE)]
class DraftsController extends Controller {
private DraftsService $service;
private string $userId;
Expand Down
2 changes: 2 additions & 0 deletions lib/Controller/FollowUpController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
use OCP\AppFramework\Http\Attribute\OpenAPI;
use OCP\IRequest;

#[OpenAPI(scope: OpenAPI::SCOPE_IGNORE)]
class FollowUpController extends Controller {

public function __construct(
Expand Down
2 changes: 2 additions & 0 deletions lib/Controller/GoogleIntegrationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use OCA\Mail\Service\AccountService;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\Attribute\OpenAPI;
use OCP\AppFramework\Http\Response;
use OCP\AppFramework\Http\StandaloneTemplateResponse;
use OCP\IRequest;
Expand All @@ -27,6 +28,7 @@

use function filter_var;

#[OpenAPI(scope: OpenAPI::SCOPE_IGNORE)]
class GoogleIntegrationController extends Controller {
private ?string $userId;
private GoogleIntegration $googleIntegration;
Expand Down
2 changes: 2 additions & 0 deletions lib/Controller/ListController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@
use OCP\AppFramework\Controller;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\Attribute\OpenAPI;
use OCP\Http\Client\IClientService;
use OCP\IRequest;
use Psr\Log\LoggerInterface;

#[OpenAPI(scope: OpenAPI::SCOPE_IGNORE)]
class ListController extends Controller {
private IMailManager $mailManager;
private AccountService $accountService;
Expand Down
2 changes: 2 additions & 0 deletions lib/Controller/LocalAttachmentsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
use OCA\Mail\Service\Attachment\UploadedFile;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\Attribute\OpenAPI;
use OCP\AppFramework\Http\JSONResponse;
use OCP\IRequest;

#[OpenAPI(scope: OpenAPI::SCOPE_IGNORE)]
class LocalAttachmentsController extends Controller {
private IAttachmentService $attachmentService;
private string $userId;
Expand Down
2 changes: 2 additions & 0 deletions lib/Controller/MailboxesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@
use OCA\Mail\Service\Sync\SyncService;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\Attribute\OpenAPI;
use OCP\AppFramework\Http\JSONResponse;
use OCP\IRequest;

#[OpenAPI(scope: OpenAPI::SCOPE_IGNORE)]
class MailboxesController extends Controller {
private AccountService $accountService;
private ?string $currentUserId;
Expand Down
61 changes: 48 additions & 13 deletions lib/Controller/MessageApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use OCA\Mail\Http\TrapError;
use OCA\Mail\IMAP\IMAPClientFactory;
use OCA\Mail\Model\SmimeData;
use OCA\Mail\ResponseDefinitions;
use OCA\Mail\Service\AccountService;
use OCA\Mail\Service\AliasesService;
use OCA\Mail\Service\Attachment\AttachmentService;
Expand All @@ -41,6 +42,10 @@
use function array_map;
use function array_merge;

/**
* @psalm-import-type MessageApiResponse from ResponseDefinitions
* @psalm-import-type MessageApiAttachment from ResponseDefinitions
*/
class MessageApiController extends OCSController {

private ?string $userId;
Expand Down Expand Up @@ -203,7 +208,13 @@ public function send(

/**
* @param int $id
* @return DataResponse
* @return DataResponse<Http::STATUS_OK|Http::STATUS_PARTIAL_CONTENT, MessageApiResponse, array{}>|DataResponse<Http::STATUS_NOT_FOUND|Http::STATUS_INTERNAL_SERVER_ERROR, string, array{}>
*
* 200: Message found
* 206: Message could not be decrypted, no "body" data returned
* 404: User was not logged in
* 404: Message, Account or Mailbox not found
* 500: Could not connect to IMAP server
*/
#[BruteForceProtection('mailGetMessage')]
#[NoAdminRequired]
Expand Down Expand Up @@ -251,7 +262,7 @@ public function get(int $id): DataResponse {
$json = $imapMessage->getFullMessage($id, $loadBody);
$itineraries = $this->itineraryService->getCached($account, $mailbox, $message->getUid());
if ($itineraries) {
$json['itineraries'] = $itineraries;
$json['itineraries'] = $itineraries->jsonSerialize();
}
$json['attachments'] = array_map(function ($a) use ($id) {
return $this->enrichDownloadUrl(
Expand All @@ -268,7 +279,7 @@ public function get(int $id): DataResponse {
$smimeData->setIsSigned(true);
$smimeData->setSignatureIsValid($imapMessage->isSignatureValid());
}
$json['smime'] = $smimeData;
$json['smime'] = $smimeData->jsonSerialize();

$dkimResult = $this->dkimService->getCached($account, $mailbox, $message->getUid());
if (is_bool($dkimResult)) {
Expand All @@ -284,17 +295,30 @@ public function get(int $id): DataResponse {
return new DataResponse($json, Http::STATUS_OK);
}

/**
* @param int $id the id of the message
* @return DataResponse<Http::STATUS_OK|?string, array{}>|DataResponse<Http::STATUS_NOT_FOUND, string, array{}>
*
* 200: Message found
* 404: User was not logged in
* 404: Message, Account or Mailbox not found
* 404: Could not find message on IMAP
*/
#[BruteForceProtection('mailGetRawMessage')]
#[NoAdminRequired]
#[NoCSRFRequired]
public function getRaw(int $id): DataResponse {
if ($this->userId === null) {
return new DataResponse('Account not found.', Http::STATUS_NOT_FOUND);
}

try {
$message = $this->mailManager->getMessage($this->userId, $id);
$mailbox = $this->mailManager->getMailbox($this->userId, $message->getMailboxId());
$account = $this->accountService->find($this->userId, $mailbox->getAccountId());
} catch (ClientException | DoesNotExistException $e) {
$this->logger->error('Message, Account or Mailbox not found', ['exception' => $e->getMessage()]);
return new DataResponse($e, Http::STATUS_FORBIDDEN);
return new DataResponse('Message, Account or Mailbox not found', Http::STATUS_NOT_FOUND);
}

$client = $this->clientFactory->getClient($account);
Expand All @@ -306,8 +330,8 @@ public function getRaw(int $id): DataResponse {
$message->getUid()
);
} catch (ServiceException $e) {
$this->logger->error('Message not found on IMAP or mail server went away', ['exception' => $e->getMessage()]);
return new DataResponse($e, Http::STATUS_NOT_FOUND);
$this->logger->error('Message not found on IMAP, or mail server went away', ['exception' => $e->getMessage()]);
return new DataResponse('Message not found', Http::STATUS_NOT_FOUND);
} finally {
$client->logout();
}
Expand All @@ -316,12 +340,12 @@ public function getRaw(int $id): DataResponse {
}

/**
* @param int $id
* @param int $id the id of the message
* @param array $attachment
*
* @return array
*/
private function enrichDownloadUrl(int $id, array $attachment) {
private function enrichDownloadUrl(int $id, array $attachment): array {
$downloadUrl = $this->urlGenerator->linkToOCSRouteAbsolute('mail.messageApi.getAttachment',
[
'id' => $id,
Expand All @@ -331,17 +355,28 @@ private function enrichDownloadUrl(int $id, array $attachment) {
return $attachment;
}

/**
* @param int $id
* @param string $attachmentId
* @return DataResponse<Http::STATUS_OK, MessageApiAttachment, array{}>|DataResponse<Http::STATUS_NOT_FOUND|Http::STATUS_INTERNAL_SERVER_ERROR, string, array{}>
*
* 200: Message found
* 404: User was not logged in
* 404: Message, Account or Mailbox not found
* 404: Could not find attachment
* 500: Could not process attachment
*
*/
#[NoCSRFRequired]
#[NoAdminRequired]
#[TrapError]
public function getAttachment(int $id,
string $attachmentId): DataResponse {
public function getAttachment(int $id, string $attachmentId): DataResponse {
try {
$message = $this->mailManager->getMessage($this->userId, $id);
$mailbox = $this->mailManager->getMailbox($this->userId, $message->getMailboxId());
$account = $this->accountService->find($this->userId, $mailbox->getAccountId());
} catch (DoesNotExistException | ClientException $e) {
return new DataResponse($e, Http::STATUS_FORBIDDEN);
return new DataResponse('Message, Account or Mailbox not found', Http::STATUS_NOT_FOUND);
}

try {
Expand All @@ -353,10 +388,10 @@ public function getAttachment(int $id,
);
} catch (\Horde_Imap_Client_Exception_NoSupportExtension | \Horde_Imap_Client_Exception | \Horde_Mime_Exception $e) {
$this->logger->error('Error when trying to process the attachment', ['exception' => $e]);
return new DataResponse($e, Http::STATUS_INTERNAL_SERVER_ERROR);
return new DataResponse('Error when trying to process the attachment', Http::STATUS_INTERNAL_SERVER_ERROR);
} catch (ServiceException | DoesNotExistException $e) {
$this->logger->error('Could not find attachment', ['exception' => $e]);
return new DataResponse($e, Http::STATUS_NOT_FOUND);
return new DataResponse('Could not find attachment', Http::STATUS_NOT_FOUND);
}

// Body party and embedded messages do not have a name
Expand Down
2 changes: 2 additions & 0 deletions lib/Controller/MessagesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
use OCP\AppFramework\Controller;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\Attribute\OpenAPI;
use OCP\AppFramework\Http\ContentSecurityPolicy;
use OCP\AppFramework\Http\JSONResponse;
use OCP\AppFramework\Http\Response;
Expand All @@ -51,6 +52,7 @@
use Psr\Log\LoggerInterface;
use function array_map;

#[OpenAPI(scope: OpenAPI::SCOPE_IGNORE)]
class MessagesController extends Controller {
private AccountService $accountService;
private IMailManager $mailManager;
Expand Down
2 changes: 2 additions & 0 deletions lib/Controller/MicrosoftIntegrationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@
use OCA\Mail\Service\AccountService;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\Attribute\OpenAPI;
use OCP\AppFramework\Http\Response;
use OCP\AppFramework\Http\StandaloneTemplateResponse;
use OCP\IRequest;
use Psr\Log\LoggerInterface;
use function filter_var;

#[OpenAPI(scope: OpenAPI::SCOPE_IGNORE)]
class MicrosoftIntegrationController extends Controller {
private ?string $userId;
private AccountService $accountService;
Expand Down
2 changes: 2 additions & 0 deletions lib/Controller/OutOfOfficeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@
use OCA\Mail\Service\OutOfOfficeService;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\Attribute\OpenAPI;
use OCP\IRequest;
use OCP\IUserSession;
use OCP\User\IAvailabilityCoordinator;
use Psr\Container\ContainerInterface;

#[OpenAPI(scope: OpenAPI::SCOPE_IGNORE)]
class OutOfOfficeController extends Controller {
public function __construct(
IRequest $request,
Expand Down
2 changes: 2 additions & 0 deletions lib/Controller/OutboxController.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@
use OCP\AppFramework\Controller;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\Attribute\OpenAPI;
use OCP\IRequest;

#[OpenAPI(scope: OpenAPI::SCOPE_IGNORE)]
class OutboxController extends Controller {
private OutboxService $service;
private string $userId;
Expand Down
2 changes: 2 additions & 0 deletions lib/Controller/PageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use OCA\Mail\Service\SmimeService;
use OCA\Viewer\Event\LoadViewer;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\Attribute\OpenAPI;
use OCP\AppFramework\Http\ContentSecurityPolicy;
use OCP\AppFramework\Http\RedirectResponse;
use OCP\AppFramework\Http\TemplateResponse;
Expand All @@ -48,6 +49,7 @@
use function http_build_query;
use function json_decode;

#[OpenAPI(scope: OpenAPI::SCOPE_IGNORE)]
class PageController extends Controller {
private IURLGenerator $urlGenerator;
private IConfig $config;
Expand Down
2 changes: 2 additions & 0 deletions lib/Controller/PreferencesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@
use OCA\Mail\Exception\ClientException;
use OCA\Mail\Http\TrapError;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\Attribute\OpenAPI;
use OCP\AppFramework\Http\JSONResponse;
use OCP\IRequest;

#[OpenAPI(scope: OpenAPI::SCOPE_IGNORE)]
class PreferencesController extends Controller {
private IUserPreferences $userPreference;
private string $userId;
Expand Down
Loading

0 comments on commit 5280498

Please sign in to comment.