diff options
author | Maxence Lange <maxence@artificial-owl.com> | 2020-09-21 09:53:54 -0100 |
---|---|---|
committer | Maxence Lange <maxence@artificial-owl.com> | 2020-09-21 09:54:03 -0100 |
commit | f5bd9ca6f0c4316fb0cdc166991b938f1e75cf70 (patch) | |
tree | 72aa24f768b79369a1c22137f873181d444a0206 /lib | |
parent | 638838cd50ea498f634ef8b54ffbc75fce3f4216 (diff) |
cleaning
Signed-off-by: Maxence Lange <maxence@artificial-owl.com>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/Controller/ApiController.php | 35 | ||||
-rw-r--r-- | lib/Controller/OAuthController.php | 5 | ||||
-rw-r--r-- | lib/Db/ClientRequest.php | 13 | ||||
-rw-r--r-- | lib/Db/ClientRequestBuilder.php | 6 | ||||
-rw-r--r-- | lib/Exceptions/ClientNotFoundException.php (renamed from lib/Exceptions/ClientDoesNotExistException.php) | 2 | ||||
-rw-r--r-- | lib/Service/ClientService.php | 12 |
6 files changed, 38 insertions, 35 deletions
diff --git a/lib/Controller/ApiController.php b/lib/Controller/ApiController.php index 9f0b1169..1b4be8a7 100644 --- a/lib/Controller/ApiController.php +++ b/lib/Controller/ApiController.php @@ -34,7 +34,7 @@ use daita\MySmallPhpTools\Traits\Nextcloud\TNCDataResponse; use Exception; use OCA\Social\AppInfo\Application; use OCA\Social\Exceptions\AccountDoesNotExistException; -use OCA\Social\Exceptions\ClientDoesNotExistException; +use OCA\Social\Exceptions\ClientNotFoundException; use OCA\Social\Exceptions\InstanceDoesNotExistException; use OCA\Social\Model\ActivityPub\ACore; use OCA\Social\Model\ActivityPub\Actor\Person; @@ -172,9 +172,8 @@ class ApiController extends Controller { ], Http::STATUS_OK ); } - } catch (Exception $e) { - return $this->fail($e, [], Http::STATUS_UNAUTHORIZED); + return $this->error($e->getMessage()); } } @@ -192,8 +191,7 @@ class ApiController extends Controller { return new DataResponse($this->viewer, Http::STATUS_OK); } catch (Exception $e) { - return $this->fail($e, [], Http::STATUS_UNAUTHORIZED); - + return $this->error($e->getMessage()); } } @@ -221,7 +219,7 @@ class ApiController extends Controller { return new DataResponse([], Http::STATUS_OK); } catch (Exception $e) { - return $this->fail($e, [], Http::STATUS_UNAUTHORIZED); + return $this->error($e->getMessage()); } } @@ -238,7 +236,7 @@ class ApiController extends Controller { return new DataResponse([], Http::STATUS_OK); } catch (Exception $e) { - return $this->fail($e, [], Http::STATUS_UNAUTHORIZED); + return $this->error($e->getMessage()); } } @@ -270,6 +268,7 @@ class ApiController extends Controller { $options = new TimelineOptions($this->request); $options->setFormat(Stream::FORMAT_LOCAL); $options->setTimeline($timeline); + $options->setLimit($limit); try { $this->initViewer(true); @@ -277,11 +276,7 @@ class ApiController extends Controller { return new DataResponse($posts, Http::STATUS_OK); } catch (Exception $e) { - return new DataResponse( - [ - 'error' => 'The access token was revoked' - ], Http::STATUS_UNAUTHORIZED - ); + return $this->error($e->getMessage()); } } @@ -291,7 +286,7 @@ class ApiController extends Controller { * @param bool $exception * * @return bool - * @throws Exception + * @throws ClientNotFoundException */ private function initViewer(bool $exception = false): bool { try { @@ -312,7 +307,7 @@ class ApiController extends Controller { return true; } catch (Exception $e) { if ($exception) { - throw $e; + throw new ClientNotFoundException('the access_token was revoked'); } } @@ -323,7 +318,7 @@ class ApiController extends Controller { /** * @return string * @throws AccountDoesNotExistException - * @throws ClientDoesNotExistException + * @throws ClientNotFoundException */ private function currentSession(): string { $user = $this->userSession->getUser(); @@ -340,6 +335,16 @@ class ApiController extends Controller { throw new AccountDoesNotExistException('userId not defined'); } + + /** + * @param string $error + * + * @return DataResponse + */ + private function error(string $error): DataResponse { + return new DataResponse(['error' => $error], Http::STATUS_UNAUTHORIZED); + } + } diff --git a/lib/Controller/OAuthController.php b/lib/Controller/OAuthController.php index 7a2e1d43..3f46cdc9 100644 --- a/lib/Controller/OAuthController.php +++ b/lib/Controller/OAuthController.php @@ -34,6 +34,7 @@ use daita\MySmallPhpTools\Traits\Nextcloud\TNCDataResponse; use Exception; use OCA\Social\AppInfo\Application; use OCA\Social\Exceptions\ClientException; +use OCA\Social\Exceptions\ClientNotFoundException; use OCA\Social\Exceptions\InstanceDoesNotExistException; use OCA\Social\Model\Client\SocialClient; use OCA\Social\Service\AccountService; @@ -266,7 +267,6 @@ class OAuthController extends Controller { // "created_at" => 1573979017 ], Http::STATUS_OK ); - } catch (Exception $e) { return new DataResponse(['error' => $e->getMessage()], Http::STATUS_UNAUTHORIZED); } @@ -331,6 +331,8 @@ class OAuthController extends Controller { "created_at" => $client->getCreation() ], Http::STATUS_OK ); + } catch (ClientNotFoundException $e) { + return new DataResponse(['error' => 'unknown client_id'], Http::STATUS_UNAUTHORIZED); } catch (Exception $e) { return new DataResponse(['error' => $e->getMessage()], Http::STATUS_UNAUTHORIZED); } @@ -338,4 +340,3 @@ class OAuthController extends Controller { } - diff --git a/lib/Db/ClientRequest.php b/lib/Db/ClientRequest.php index dd009a89..f1df957e 100644 --- a/lib/Db/ClientRequest.php +++ b/lib/Db/ClientRequest.php @@ -34,7 +34,7 @@ namespace OCA\Social\Db; use daita\MySmallPhpTools\Traits\TArrayTools; use DateTime; use Exception; -use OCA\Social\Exceptions\ClientDoesNotExistException; +use OCA\Social\Exceptions\ClientNotFoundException; use OCA\Social\Model\Client\SocialClient; use OCA\Social\Service\ClientService; use OCP\DB\QueryBuilder\IQueryBuilder; @@ -68,10 +68,9 @@ class ClientRequest extends ClientRequestBuilder { ->setValue('app_scopes', $qb->createNamedParameter(json_encode($client->getAppScopes()))); try { - $qb->setValue( - 'creation', - $qb->createNamedParameter(new DateTime('now'), IQueryBuilder::PARAM_DATE) - ); + $dt = new DateTime('now'); + $qb->setValue('last_update', $qb->createNamedParameter($dt, IQueryBuilder::PARAM_DATE)); + $qb->setValue('creation', $qb->createNamedParameter($dt, IQueryBuilder::PARAM_DATE)); } catch (Exception $e) { } @@ -131,7 +130,7 @@ class ClientRequest extends ClientRequestBuilder { * @param string $clientId * * @return SocialClient - * @throws ClientDoesNotExistException + * @throws ClientNotFoundException */ public function getFromClientId(string $clientId): SocialClient { $qb = $this->getClientSelectSql(); @@ -145,7 +144,7 @@ class ClientRequest extends ClientRequestBuilder { * @param string $token * * @return SocialClient - * @throws ClientDoesNotExistException + * @throws ClientNotFoundException */ public function getFromToken(string $token): SocialClient { $qb = $this->getClientSelectSql(); diff --git a/lib/Db/ClientRequestBuilder.php b/lib/Db/ClientRequestBuilder.php index e3c00a85..a3011816 100644 --- a/lib/Db/ClientRequestBuilder.php +++ b/lib/Db/ClientRequestBuilder.php @@ -34,7 +34,7 @@ namespace OCA\Social\Db; use daita\MySmallPhpTools\Exceptions\RowNotFoundException; use daita\MySmallPhpTools\Traits\TArrayTools; use Exception; -use OCA\Social\Exceptions\ClientDoesNotExistException; +use OCA\Social\Exceptions\ClientNotFoundException; use OCA\Social\Model\Client\SocialClient; @@ -115,14 +115,14 @@ class ClientRequestBuilder extends CoreRequestBuilder { * @param SocialQueryBuilder $qb * * @return SocialClient - * @throws ClientDoesNotExistException + * @throws ClientNotFoundException */ public function getClientFromRequest(SocialQueryBuilder $qb): SocialClient { /** @var SocialClient $result */ try { $result = $qb->getRow([$this, 'parseClientSelectSql']); } catch (RowNotFoundException $e) { - throw new ClientDoesNotExistException($e->getMessage()); + throw new ClientNotFoundException($e->getMessage()); } return $result; diff --git a/lib/Exceptions/ClientDoesNotExistException.php b/lib/Exceptions/ClientNotFoundException.php index 5061b085..0277a0fd 100644 --- a/lib/Exceptions/ClientDoesNotExistException.php +++ b/lib/Exceptions/ClientNotFoundException.php @@ -33,7 +33,7 @@ namespace OCA\Social\Exceptions; use Exception; -class ClientDoesNotExistException extends Exception { +class ClientNotFoundException extends Exception { } diff --git a/lib/Service/ClientService.php b/lib/Service/ClientService.php index 88791fb8..9c02ebe2 100644 --- a/lib/Service/ClientService.php +++ b/lib/Service/ClientService.php @@ -33,8 +33,8 @@ namespace OCA\Social\Service; use daita\MySmallPhpTools\Traits\TStringTools; use Exception; use OCA\Social\Db\ClientRequest; -use OCA\Social\Exceptions\ClientDoesNotExistException; use OCA\Social\Exceptions\ClientException; +use OCA\Social\Exceptions\ClientNotFoundException; use OCA\Social\Model\Client\SocialClient; @@ -122,7 +122,7 @@ class ClientService { * @param string $clientId * * @return SocialClient - * @throws ClientDoesNotExistException + * @throws ClientNotFoundException */ public function getFromClientId(string $clientId): SocialClient { return $this->clientRequest->getFromClientId($clientId); @@ -131,12 +131,11 @@ class ClientService { /** * @param string $token - * @param bool $refresh * * @return SocialClient - * @throws ClientDoesNotExistException + * @throws ClientNotFoundException */ - public function getFromToken(string $token, bool $refresh = true): SocialClient { + public function getFromToken(string $token): SocialClient { $client = $this->clientRequest->getFromToken($token); if ($client->getLastUpdate() + self::TIME_TOKEN_TTL < time()) { @@ -145,11 +144,10 @@ class ClientService { } catch (Exception $e) { } - throw new ClientDoesNotExistException(); + throw new ClientNotFoundException(); } if ($client->getLastUpdate() + self::TIME_TOKEN_REFRESH > time()) { - $this->miscService->log('__updating ' . $client->getLastUpdate()); $this->clientRequest->updateTime($client); } |