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 | |
parent | 638838cd50ea498f634ef8b54ffbc75fce3f4216 (diff) |
cleaning
Signed-off-by: Maxence Lange <maxence@artificial-owl.com>
-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 | ||||
-rw-r--r-- | timeline.json | 91 |
7 files changed, 38 insertions, 126 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); } diff --git a/timeline.json b/timeline.json deleted file mode 100644 index aacfd68d..00000000 --- a/timeline.json +++ /dev/null @@ -1,91 +0,0 @@ -{ - "id": "104790603799482162", - "created_at": "2020-09-01T16:28:39.000Z", - "in_reply_to_id": null, - "in_reply_to_account_id": null, - "sensitive": false, - "spoiler_text": "", - "visibility": "unlisted", - "language": "en", - "uri": "https://mastodon.xyz/users/nextcloud/statuses/104790603813976407", - "url": "https://mastodon.xyz/@nextcloud/104790603813976407", - "replies_count": 0, - "reblogs_count": 2, - "favourites_count": 0, - "favourited": false, - "reblogged": false, - "muted": false, - "bookmarked": false, - "content": "\u003cp\u003eDid you know there's an experimental \u003ca href=\"https://mastodon.xyz/tags/Nextcloud\" class=\"mention hashtag\" rel=\"nofollow noopener noreferrer\" target=\"_blank\"\u003e#\u003cspan\u003eNextcloud\u003c/span\u003e\u003c/a\u003e app that lets you block logins from specific countries or geographic regions? Check out Geoblocker if that is something you could use!\u003cbr\u003e\u003ca href=\"https://apps.nextcloud.com/apps/geoblocker\" rel=\"nofollow noopener noreferrer\" target=\"_blank\"\u003e\u003cspan class=\"invisible\"\u003ehttps://\u003c/span\u003e\u003cspan class=\"ellipsis\"\u003eapps.nextcloud.com/apps/geoblo\u003c/span\u003e\u003cspan class=\"invisible\"\u003ecker\u003c/span\u003e\u003c/a\u003e\u003c/p\u003e", - "reblog": null, - "account": { - "id": "126222", - "username": "nextcloud", - "acct": "nextcloud@mastodon.xyz", - "display_name": "Nextcloud 📱☁️💻", - "locked": false, - "bot": false, - "discoverable": false, - "group": false, - "created_at": "2017-05-02T09:56:41.951Z", - "note": "\u003cp\u003eA safe home for all your data, community-driven \u0026amp; open source!\u003cbr\u003e\u003ca href=\"https://nextcloud.com\" rel=\"nofollow noopener noreferrer\" target=\"_blank\"\u003e\u003cspan class=\"invisible\"\u003ehttps://\u003c/span\u003e\u003cspan class=\"\"\u003enextcloud.com\u003c/span\u003e\u003cspan class=\"invisible\"\u003e\u003c/span\u003e\u003c/a\u003e\u003cbr\u003eJoin us at \u003ca href=\"https://help.nextcloud.com\" rel=\"nofollow noopener noreferrer\" target=\"_blank\"\u003e\u003cspan class=\"invisible\"\u003ehttps://\u003c/span\u003e\u003cspan class=\"\"\u003ehelp.nextcloud.com\u003c/span\u003e\u003cspan class=\"invisible\"\u003e\u003c/span\u003e\u003c/a\u003e and \u003ca href=\"https://github.com/nextcloud\" rel=\"nofollow noopener noreferrer\" target=\"_blank\"\u003e\u003cspan class=\"invisible\"\u003ehttps://\u003c/span\u003e\u003cspan class=\"\"\u003egithub.com/nextcloud\u003c/span\u003e\u003cspan class=\"invisible\"\u003e\u003c/span\u003e\u003c/a\u003e 😊\u003c/p\u003e", - "url": "https://mastodon.xyz/@nextcloud", - "avatar": "https://files.mastodon.social/accounts/avatars/000/126/222/original/50785214e44d10cc.jpeg", - "avatar_static": "https://files.mastodon.social/accounts/avatars/000/126/222/original/50785214e44d10cc.jpeg", - "header": "https://files.mastodon.social/accounts/headers/000/126/222/original/6d7b41fdd92cfd6f.jpeg", - "header_static": "https://files.mastodon.social/accounts/headers/000/126/222/original/6d7b41fdd92cfd6f.jpeg", - "followers_count": 9525, - "following_count": 131, - "statuses_count": 3036, - "last_status_at": "2020-09-01", - "emojis": [], - "fields": [ - { - "name": "💻 Website", - "value": "\u003ca href=\"https://nextcloud.com\" rel=\"nofollow noopener noreferrer\" target=\"_blank\"\u003e\u003cspan class=\"invisible\"\u003ehttps://\u003c/span\u003e\u003cspan class=\"\"\u003enextcloud.com\u003c/span\u003e\u003cspan class=\"invisible\"\u003e\u003c/span\u003e\u003c/a\u003e", - "verified_at": null - }, - { - "name": "😍 Contribute", - "value": "\u003ca href=\"https://nextcloud.com/contribute\" rel=\"nofollow noopener noreferrer\" target=\"_blank\"\u003e\u003cspan class=\"invisible\"\u003ehttps://\u003c/span\u003e\u003cspan class=\"\"\u003enextcloud.com/contribute\u003c/span\u003e\u003cspan class=\"invisible\"\u003e\u003c/span\u003e\u003c/a\u003e", - "verified_at": null - }, - { - "name": "🌈 Include initiative", - "value": "\u003ca href=\"https://nextcloud.com/include\" rel=\"nofollow noopener noreferrer\" target=\"_blank\"\u003e\u003cspan class=\"invisible\"\u003ehttps://\u003c/span\u003e\u003cspan class=\"\"\u003enextcloud.com/include\u003c/span\u003e\u003cspan class=\"invisible\"\u003e\u003c/span\u003e\u003c/a\u003e", - "verified_at": null - }, - { - "name": "💬 Forum", - "value": "\u003ca href=\"https://help.nextcloud.com\" rel=\"nofollow noopener noreferrer\" target=\"_blank\"\u003e\u003cspan class=\"invisible\"\u003ehttps://\u003c/span\u003e\u003cspan class=\"\"\u003ehelp.nextcloud.com\u003c/span\u003e\u003cspan class=\"invisible\"\u003e\u003c/span\u003e\u003c/a\u003e", - "verified_at": null - } - ] - }, - "media_attachments": [], - "mentions": [], - "tags": [ - { - "name": "nextcloud", - "url": "https://mastodon.social/tags/nextcloud" - } - ], - "emojis": [], - "card": { - "url": "https://apps.nextcloud.com/apps/geoblocker", - "title": "GeoBlocker - Apps - App Store - Nextcloud", - "description": "The Nextcloud App Store - Upload your apps and install new apps onto your Nextcloud", - "type": "link", - "author_name": "", - "author_url": "", - "provider_name": "", - "provider_url": "", - "html": "", - "width": 0, - "height": 0, - "image": null, - "embed_url": "", - "blurhash": null - }, - "poll": null -} |