From 745810e2b0b67b06da20dd5c53d938cbe02bfc06 Mon Sep 17 00:00:00 2001 From: Maxence Lange Date: Wed, 17 Jun 2020 12:41:58 -0100 Subject: [PATCH] database & requests Signed-off-by: Maxence Lange --- appinfo/routes.php | 8 +- lib/Command/StreamDetails.php | 2 +- lib/Command/Timeline.php | 15 +- lib/Controller/LocalController.php | 2 +- lib/Cron/Cache.php | 10 - lib/Db/StreamRequest.php | 5 +- lib/Exceptions/RequestContentException.php | 39 --- lib/Exceptions/RequestNetworkException.php | 39 --- .../RequestResultNotJsonException.php | 39 --- lib/Exceptions/RequestResultSizeException.php | 39 --- lib/Exceptions/RequestServerException.php | 39 --- lib/Interfaces/Object/AnnounceInterface.php | 10 +- lib/Interfaces/Object/FollowInterface.php | 10 +- lib/Service/ActivityService.php | 47 +-- lib/Service/CacheActorService.php | 15 +- lib/Service/CacheDocumentService.php | 13 +- lib/Service/CurlService.php | 315 +++--------------- lib/Service/DocumentService.php | 14 +- lib/Service/FollowService.php | 10 +- lib/Service/PostService.php | 10 +- lib/Service/SignatureService.php | 10 +- lib/Service/StreamQueueService.php | 10 +- lib/Service/StreamService.php | 31 +- package-lock.json | 3 +- 24 files changed, 147 insertions(+), 588 deletions(-) delete mode 100644 lib/Exceptions/RequestContentException.php delete mode 100644 lib/Exceptions/RequestNetworkException.php delete mode 100644 lib/Exceptions/RequestResultNotJsonException.php delete mode 100644 lib/Exceptions/RequestResultSizeException.php delete mode 100644 lib/Exceptions/RequestServerException.php diff --git a/appinfo/routes.php b/appinfo/routes.php index b7e5d6e6..ad824468 100644 --- a/appinfo/routes.php +++ b/appinfo/routes.php @@ -52,16 +52,18 @@ return [ ['name' => 'ActivityPub#actorAlias', 'url' => '/@{username}/', 'verb' => 'GET'], ['name' => 'ActivityPub#inbox', 'url' => '/@{username}/inbox', 'verb' => 'POST'], ['name' => 'ActivityPub#sharedInbox', 'url' => '/inbox', 'verb' => 'POST'], - ['name' => 'ActivityPub#outbox', 'url' => '/@{username}/outbox', 'verb' => 'POST'], + + ['name' => 'ActivityPub#outbox', 'url' => '/@{username}/outbox', 'verb' => 'GET'], + ['name' => 'ActivityPub#outbox', 'url' => '/@{username}/outbox', 'verb' => 'POST'], // Check if needed ['name' => 'ActivityPub#followers', 'url' => '/@{username}/followers', 'verb' => 'GET'], ['name' => 'ActivityPub#following', 'url' => '/@{username}/following', 'verb' => 'GET'], + ['name' => 'ActivityPub#displayPost', 'url' => '/@{username}/{token}', 'verb' => 'GET'], + ['name' => 'OStatus#subscribe', 'url' => '/ostatus/follow/{uri}', 'verb' => 'GET'], ['name' => 'OStatus#followRemote', 'url' => '/api/v1/ostatus/followRemote/{local}', 'verb' => 'GET'], ['name' => 'OStatus#getLink', 'url' => '/api/v1/ostatus/link/{local}/{account}', 'verb' => 'GET'], - ['name' => 'ActivityPub#displayPost', 'url' => '/@{username}/{token}', 'verb' => 'GET'], - ['name' => 'Local#streamHome', 'url' => '/api/v1/stream/home', 'verb' => 'GET'], ['name' => 'Local#streamNotifications', 'url' => '/api/v1/stream/notifications', 'verb' => 'GET'], ['name' => 'Local#streamTimeline', 'url' => '/api/v1/stream/timeline', 'verb' => 'GET'], diff --git a/lib/Command/StreamDetails.php b/lib/Command/StreamDetails.php index f6e952b5..3d8a5956 100644 --- a/lib/Command/StreamDetails.php +++ b/lib/Command/StreamDetails.php @@ -138,7 +138,7 @@ class StreamDetails extends ExtendedBase { $this->output->writeln('* Direct: ' . json_encode($direct, JSON_PRETTY_PRINT)); $this->output->writeln('* Public: ' . ($details->isPublic() ? 'true' : 'false')); - $this->output->writeln('* Federated: ' . ($details->isFederated() ? 'true' : 'true')); + $this->output->writeln('* Federated: ' . ($details->isFederated() ? 'true' : 'false')); } } diff --git a/lib/Command/Timeline.php b/lib/Command/Timeline.php index 2379cb93..6dc5e1c8 100644 --- a/lib/Command/Timeline.php +++ b/lib/Command/Timeline.php @@ -139,7 +139,6 @@ class Timeline extends ExtendedBase { } - /** * @param Person $actor * @param string $timeline @@ -147,19 +146,25 @@ class Timeline extends ExtendedBase { * @throws Exception */ private function displayStream(Person $actor, string $timeline) { + $this->streamRequest->setViewer($actor); switch ($timeline) { case 'home': - $stream = $this->streamRequest->getTimelineHome($actor, 0, $this->count); + $stream = $this->streamRequest->getTimelineHome(0, $this->count); $this->outputStreams($stream); break; case 'direct': - $stream = $this->streamRequest->getTimelineDirect($actor, 0, $this->count); + $stream = $this->streamRequest->getTimelineDirect(0, $this->count); $this->outputStreams($stream); break; case 'notifications': - $stream = $this->streamRequest->getTimelineNotifications($actor, 0, $this->count); + $stream = $this->streamRequest->getTimelineNotifications(0, $this->count); + $this->outputStreams($stream); + break; + + case 'liked': + $stream = $this->streamRequest->getTimelineLiked(0, $this->count); $this->outputStreams($stream); break; @@ -175,7 +180,7 @@ class Timeline extends ExtendedBase { default: throw new Exception( - 'Unknown timeline. Try home, direct, local, global, notification.' + 'Unknown timeline. Try home, direct, notifications, liked, local, global.' ); } } diff --git a/lib/Controller/LocalController.php b/lib/Controller/LocalController.php index b9f9b1bc..1021d334 100644 --- a/lib/Controller/LocalController.php +++ b/lib/Controller/LocalController.php @@ -400,7 +400,7 @@ class LocalController extends Controller { public function streamNotifications($since = 0, int $limit = 5): DataResponse { try { $this->initViewer(true); - $posts = $this->streamService->getStreamNotifications($this->viewer, $since, $limit); + $posts = $this->streamService->getStreamNotifications($since, $limit); return $this->success($posts); } catch (Exception $e) { diff --git a/lib/Cron/Cache.php b/lib/Cron/Cache.php index 9932e3c1..2552766f 100644 --- a/lib/Cron/Cache.php +++ b/lib/Cron/Cache.php @@ -36,10 +36,8 @@ use OC\BackgroundJob\TimedJob; use OCA\Social\AppInfo\Application; use OCA\Social\Service\AccountService; use OCA\Social\Service\CacheActorService; -use OCA\Social\Service\ConfigService; use OCA\Social\Service\DocumentService; use OCA\Social\Service\HashtagService; -use OCA\Social\Service\MiscService; use OCP\AppFramework\QueryException; @@ -63,12 +61,6 @@ class Cache extends TimedJob { /** @var HashtagService */ private $hashtagService; - /** @var ConfigService */ - private $configService; - - /** @var MiscService */ - private $miscService; - /** * Cache constructor. @@ -91,8 +83,6 @@ class Cache extends TimedJob { $this->cacheActorService = $c->query(CacheActorService::class); $this->documentService = $c->query(DocumentService::class); $this->hashtagService = $c->query(HashtagService::class); - $this->configService = $c->query(ConfigService::class); - $this->miscService = $c->query(MiscService::class); $this->manageCache(); } diff --git a/lib/Db/StreamRequest.php b/lib/Db/StreamRequest.php index db1480f4..fc54fc0e 100644 --- a/lib/Db/StreamRequest.php +++ b/lib/Db/StreamRequest.php @@ -396,16 +396,17 @@ class StreamRequest extends StreamRequestBuilder { * - someone wants to follow you (not yet) * - someone is following you (not yet) * - * @param Person $actor * @param int $since * @param int $limit * * @return Stream[] * @throws DateTimeException */ - public function getTimelineNotifications(Person $actor, int $since = 0, int $limit = 5): array { + public function getTimelineNotifications(int $since = 0, int $limit = 5): array { $qb = $this->getStreamSelectSql(); + $actor = $qb->getViewer(); + $qb->limitPaginate($since, $limit); $qb->selectDestFollowing('sd', ''); diff --git a/lib/Exceptions/RequestContentException.php b/lib/Exceptions/RequestContentException.php deleted file mode 100644 index f83a17a7..00000000 --- a/lib/Exceptions/RequestContentException.php +++ /dev/null @@ -1,39 +0,0 @@ - - * @copyright 2018, Maxence Lange - * @license GNU AGPL version 3 or any later version - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - * - */ - - -namespace OCA\Social\Exceptions; - - -use Exception; - - -class RequestContentException extends Exception { - -} - diff --git a/lib/Exceptions/RequestNetworkException.php b/lib/Exceptions/RequestNetworkException.php deleted file mode 100644 index e80c2b71..00000000 --- a/lib/Exceptions/RequestNetworkException.php +++ /dev/null @@ -1,39 +0,0 @@ - - * @copyright 2018, Maxence Lange - * @license GNU AGPL version 3 or any later version - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - * - */ - - -namespace OCA\Social\Exceptions; - - -use Exception; - - -class RequestNetworkException extends Exception { - -} - diff --git a/lib/Exceptions/RequestResultNotJsonException.php b/lib/Exceptions/RequestResultNotJsonException.php deleted file mode 100644 index 87785365..00000000 --- a/lib/Exceptions/RequestResultNotJsonException.php +++ /dev/null @@ -1,39 +0,0 @@ - - * @copyright 2018, Maxence Lange - * @license GNU AGPL version 3 or any later version - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - * - */ - - -namespace OCA\Social\Exceptions; - - -use Exception; - - -class RequestResultNotJsonException extends Exception { - -} - diff --git a/lib/Exceptions/RequestResultSizeException.php b/lib/Exceptions/RequestResultSizeException.php deleted file mode 100644 index d20b589f..00000000 --- a/lib/Exceptions/RequestResultSizeException.php +++ /dev/null @@ -1,39 +0,0 @@ - - * @copyright 2018, Maxence Lange - * @license GNU AGPL version 3 or any later version - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - * - */ - - -namespace OCA\Social\Exceptions; - - -use Exception; - - -class RequestResultSizeException extends Exception { - -} - diff --git a/lib/Exceptions/RequestServerException.php b/lib/Exceptions/RequestServerException.php deleted file mode 100644 index 34d629e0..00000000 --- a/lib/Exceptions/RequestServerException.php +++ /dev/null @@ -1,39 +0,0 @@ - - * @copyright 2018, Maxence Lange - * @license GNU AGPL version 3 or any later version - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - * - */ - - -namespace OCA\Social\Exceptions; - - -use Exception; - - -class RequestServerException extends Exception { - -} - diff --git a/lib/Interfaces/Object/AnnounceInterface.php b/lib/Interfaces/Object/AnnounceInterface.php index ea2487be..92ac256e 100644 --- a/lib/Interfaces/Object/AnnounceInterface.php +++ b/lib/Interfaces/Object/AnnounceInterface.php @@ -44,11 +44,11 @@ use OCA\Social\Exceptions\InvalidResourceException; use OCA\Social\Exceptions\ItemNotFoundException; use OCA\Social\Exceptions\ItemUnknownException; use OCA\Social\Exceptions\RedundancyLimitException; -use OCA\Social\Exceptions\RequestContentException; -use OCA\Social\Exceptions\RequestNetworkException; -use OCA\Social\Exceptions\RequestResultNotJsonException; -use OCA\Social\Exceptions\RequestResultSizeException; -use OCA\Social\Exceptions\RequestServerException; +use daita\MySmallPhpTools\Exceptions\RequestContentException; +use daita\MySmallPhpTools\Exceptions\RequestNetworkException; +use daita\MySmallPhpTools\Exceptions\RequestResultNotJsonException; +use daita\MySmallPhpTools\Exceptions\RequestResultSizeException; +use daita\MySmallPhpTools\Exceptions\RequestServerException; use OCA\Social\Exceptions\SocialAppConfigException; use OCA\Social\Exceptions\StreamNotFoundException; use OCA\Social\Exceptions\UnauthorizedFediverseException; diff --git a/lib/Interfaces/Object/FollowInterface.php b/lib/Interfaces/Object/FollowInterface.php index a72eeb02..47eea5bf 100644 --- a/lib/Interfaces/Object/FollowInterface.php +++ b/lib/Interfaces/Object/FollowInterface.php @@ -41,11 +41,11 @@ use OCA\Social\Exceptions\InvalidResourceException; use OCA\Social\Exceptions\ItemNotFoundException; use OCA\Social\Exceptions\ItemUnknownException; use OCA\Social\Exceptions\RedundancyLimitException; -use OCA\Social\Exceptions\RequestContentException; -use OCA\Social\Exceptions\RequestNetworkException; -use OCA\Social\Exceptions\RequestResultNotJsonException; -use OCA\Social\Exceptions\RequestResultSizeException; -use OCA\Social\Exceptions\RequestServerException; +use daita\MySmallPhpTools\Exceptions\RequestContentException; +use daita\MySmallPhpTools\Exceptions\RequestNetworkException; +use daita\MySmallPhpTools\Exceptions\RequestResultNotJsonException; +use daita\MySmallPhpTools\Exceptions\RequestResultSizeException; +use daita\MySmallPhpTools\Exceptions\RequestServerException; use OCA\Social\Exceptions\SocialAppConfigException; use OCA\Social\Interfaces\IActivityPubInterface; use OCA\Social\Interfaces\Internal\SocialAppNotificationInterface; diff --git a/lib/Service/ActivityService.php b/lib/Service/ActivityService.php index 8bf7707c..41372d98 100644 --- a/lib/Service/ActivityService.php +++ b/lib/Service/ActivityService.php @@ -30,6 +30,11 @@ declare(strict_types=1); namespace OCA\Social\Service; +use daita\MySmallPhpTools\Exceptions\RequestContentException; +use daita\MySmallPhpTools\Exceptions\RequestNetworkException; +use daita\MySmallPhpTools\Exceptions\RequestResultNotJsonException; +use daita\MySmallPhpTools\Exceptions\RequestResultSizeException; +use daita\MySmallPhpTools\Exceptions\RequestServerException; use daita\MySmallPhpTools\Model\Request; use daita\MySmallPhpTools\Traits\TArrayTools; use Exception; @@ -43,11 +48,6 @@ use OCA\Social\Exceptions\ItemAlreadyExistsException; use OCA\Social\Exceptions\ItemUnknownException; use OCA\Social\Exceptions\NoHighPriorityRequestException; use OCA\Social\Exceptions\QueueStatusException; -use OCA\Social\Exceptions\RequestContentException; -use OCA\Social\Exceptions\RequestNetworkException; -use OCA\Social\Exceptions\RequestResultNotJsonException; -use OCA\Social\Exceptions\RequestResultSizeException; -use OCA\Social\Exceptions\RequestServerException; use OCA\Social\Exceptions\SocialAppConfigException; use OCA\Social\Exceptions\UnauthorizedFediverseException; use OCA\Social\Model\ActivityPub\ACore; @@ -284,41 +284,20 @@ class ActivityService { try { $this->signatureService->signRequest($request, $queue); - $this->curlService->request($request); + $this->curlService->retrieveJson($request); $this->requestQueueService->endRequest($queue, true); - } catch (UnauthorizedFediverseException $e) { + } catch (UnauthorizedFediverseException | RequestResultNotJsonException $e) { $this->requestQueueService->endRequest($queue, true); - } catch (RequestResultNotJsonException $e) { - $this->requestQueueService->endRequest($queue, true); - } catch (ActorDoesNotExistException $e) { + } catch (ActorDoesNotExistException | RequestContentException | RequestResultSizeException $e) { $this->miscService->log( - 'Error while managing request: ' . json_encode($request) . ' ' . $e->getMessage(), 1 - ); - $this->requestQueueService->deleteRequest($queue); - } catch (RequestContentException $e) { - $this->miscService->log( - 'Error while managing request: ' . json_encode($request) . ' ' . $e->getMessage(), 1 - ); - $this->requestQueueService->deleteRequest($queue); - } catch (RequestResultSizeException $e) { - $this->miscService->log( - 'Error while managing request: ' . json_encode($request) . ' ' . $e->getMessage(), 1 - ); - $this->requestQueueService->deleteRequest($queue); - } catch (RequestServerException $e) { - $this->miscService->log( - 'Temporary error while managing request: RequestServerException - ' . json_encode( - $request - ) . ' - ' + 'Error while managing request: ' . json_encode($request) . ' ' . get_class($e) . ': ' . $e->getMessage(), 1 ); - $this->requestQueueService->endRequest($queue, false); - $this->failInstances[] = $host; - } catch (RequestNetworkException $e) { + $this->requestQueueService->deleteRequest($queue); + } catch (RequestNetworkException | RequestServerException $e) { $this->miscService->log( - 'Temporary error while managing request: RequestNetworkException - ' . json_encode( - $request - ) . ' - ' . $e->getMessage(), 1 + 'Temporary error while managing request: RequestServerException - ' . json_encode($request) + . ' - ' . get_class($e) . ': ' . $e->getMessage(), 1 ); $this->requestQueueService->endRequest($queue, false); $this->failInstances[] = $host; diff --git a/lib/Service/CacheActorService.php b/lib/Service/CacheActorService.php index 9d5c143f..1b145215 100644 --- a/lib/Service/CacheActorService.php +++ b/lib/Service/CacheActorService.php @@ -31,6 +31,11 @@ namespace OCA\Social\Service; use daita\MySmallPhpTools\Exceptions\MalformedArrayException; +use daita\MySmallPhpTools\Exceptions\RequestContentException; +use daita\MySmallPhpTools\Exceptions\RequestNetworkException; +use daita\MySmallPhpTools\Exceptions\RequestResultNotJsonException; +use daita\MySmallPhpTools\Exceptions\RequestResultSizeException; +use daita\MySmallPhpTools\Exceptions\RequestServerException; use daita\MySmallPhpTools\Traits\TArrayTools; use Exception; use OCA\Social\AP; @@ -41,11 +46,6 @@ use OCA\Social\Exceptions\InvalidResourceException; use OCA\Social\Exceptions\ItemAlreadyExistsException; use OCA\Social\Exceptions\ItemUnknownException; use OCA\Social\Exceptions\RedundancyLimitException; -use OCA\Social\Exceptions\RequestContentException; -use OCA\Social\Exceptions\RequestNetworkException; -use OCA\Social\Exceptions\RequestResultNotJsonException; -use OCA\Social\Exceptions\RequestResultSizeException; -use OCA\Social\Exceptions\RequestServerException; use OCA\Social\Exceptions\RetrieveAccountFormatException; use OCA\Social\Exceptions\SocialAppConfigException; use OCA\Social\Exceptions\UnauthorizedFediverseException; @@ -71,10 +71,6 @@ class CacheActorService { private $miscService; - /** @var Person */ - private $viewer = null; - - /** * CacheService constructor. * @@ -98,7 +94,6 @@ class CacheActorService { * @param Person $viewer */ public function setViewer(Person $viewer) { - $this->viewer = $viewer; $this->cacheActorsRequest->setViewer($viewer); } diff --git a/lib/Service/CacheDocumentService.php b/lib/Service/CacheDocumentService.php index 579d379b..0b7ef70e 100644 --- a/lib/Service/CacheDocumentService.php +++ b/lib/Service/CacheDocumentService.php @@ -31,6 +31,10 @@ namespace OCA\Social\Service; use daita\MySmallPhpTools\Exceptions\MalformedArrayException; +use daita\MySmallPhpTools\Exceptions\RequestContentException; +use daita\MySmallPhpTools\Exceptions\RequestNetworkException; +use daita\MySmallPhpTools\Exceptions\RequestResultSizeException; +use daita\MySmallPhpTools\Exceptions\RequestServerException; use daita\MySmallPhpTools\Model\Request; use daita\MySmallPhpTools\Traits\TArrayTools; use daita\MySmallPhpTools\Traits\TStringTools; @@ -40,11 +44,6 @@ use Gumlet\ImageResizeException; use OCA\Social\Exceptions\CacheContentException; use OCA\Social\Exceptions\CacheContentMimeTypeException; use OCA\Social\Exceptions\CacheDocumentDoesNotExistException; -use OCA\Social\Exceptions\RequestContentException; -use OCA\Social\Exceptions\RequestNetworkException; -use OCA\Social\Exceptions\RequestResultNotJsonException; -use OCA\Social\Exceptions\RequestResultSizeException; -use OCA\Social\Exceptions\RequestServerException; use OCA\Social\Exceptions\SocialAppConfigException; use OCA\Social\Exceptions\UnauthorizedFediverseException; use OCA\Social\Model\ActivityPub\Object\Document; @@ -122,7 +121,6 @@ class CacheDocumentService { * @throws NotPermittedException * @throws RequestContentException * @throws RequestNetworkException - * @throws RequestResultNotJsonException * @throws RequestResultSizeException * @throws RequestServerException * @throws SocialAppConfigException @@ -260,7 +258,6 @@ class CacheDocumentService { * @throws MalformedArrayException * @throws RequestContentException * @throws RequestNetworkException - * @throws RequestResultNotJsonException * @throws RequestResultSizeException * @throws RequestServerException * @throws SocialAppConfigException @@ -273,7 +270,7 @@ class CacheDocumentService { $request->setAddress($url['host']); $request->setProtocol($url['scheme']); - $content = $this->curlService->request($request); + $content = $this->curlService->doRequest($request); return $content; } diff --git a/lib/Service/CurlService.php b/lib/Service/CurlService.php index 47d2f068..018b5de5 100644 --- a/lib/Service/CurlService.php +++ b/lib/Service/CurlService.php @@ -32,9 +32,15 @@ namespace OCA\Social\Service; use daita\MySmallPhpTools\Exceptions\ArrayNotFoundException; use daita\MySmallPhpTools\Exceptions\MalformedArrayException; +use daita\MySmallPhpTools\Exceptions\RequestContentException; +use daita\MySmallPhpTools\Exceptions\RequestNetworkException; +use daita\MySmallPhpTools\Exceptions\RequestResultNotJsonException; +use daita\MySmallPhpTools\Exceptions\RequestResultSizeException; +use daita\MySmallPhpTools\Exceptions\RequestServerException; use daita\MySmallPhpTools\Model\Request; use daita\MySmallPhpTools\Traits\TArrayTools; use daita\MySmallPhpTools\Traits\TPathTools; +use daita\MySmallPhpTools\Traits\TRequest; use Exception; use OCA\Social\AP; use OCA\Social\Exceptions\HostMetaException; @@ -42,11 +48,6 @@ use OCA\Social\Exceptions\InvalidOriginException; use OCA\Social\Exceptions\InvalidResourceException; use OCA\Social\Exceptions\ItemUnknownException; use OCA\Social\Exceptions\RedundancyLimitException; -use OCA\Social\Exceptions\RequestContentException; -use OCA\Social\Exceptions\RequestNetworkException; -use OCA\Social\Exceptions\RequestResultNotJsonException; -use OCA\Social\Exceptions\RequestResultSizeException; -use OCA\Social\Exceptions\RequestServerException; use OCA\Social\Exceptions\RetrieveAccountFormatException; use OCA\Social\Exceptions\SocialAppConfigException; use OCA\Social\Exceptions\UnauthorizedFediverseException; @@ -57,6 +58,12 @@ class CurlService { use TArrayTools; use TPathTools; + use TRequest { + initRequestPost as initRequestPostOrig; + initRequestGet as initRequestGetOrig; + retrieveJson as retrieveJsonOrig; + doRequest as doRequestOrig; + } const ASYNC_REQUEST_TOKEN = '/async/request/{token}'; @@ -73,13 +80,6 @@ class CurlService { private $miscService; - /** @var int */ - private $maxDownloadSize = 0; - - /** @var bool */ - private $maxDownloadSizeReached = false; - - /** * CurlService constructor. * @@ -93,6 +93,9 @@ class CurlService { $this->configService = $configService; $this->fediverseService = $fediverseService; $this->miscService = $miscService; + + $maxDlSize = $this->configService->getAppValue(ConfigService::SOCIAL_MAX_SIZE) * (1024 * 1024); + $this->setMaxDownloadSize($maxDlSize); } @@ -131,15 +134,8 @@ class CurlService { $request = new Request($path); $request->addData('resource', 'acct:' . $account); $request->setAddress($host); - - try { - $result = $this->request($request); - } catch (RequestNetworkException $e) { - if ($e->getCode() === CURLE_COULDNT_CONNECT) { - $request->setProtocol('http'); - $result = $this->request($request); - } else throw $e; - } + $request->setProtocols(['https', 'http']); + $result = $this->retrieveJson($request); $subject = $this->get('subject', $result, ''); list($type, $temp) = explode(':', $subject, 2); @@ -162,7 +158,7 @@ class CurlService { $request->setAddress($host); try { - $result = $this->request($request); + $result = $this->retrieveJson($request); } catch (Exception $e) { $this->miscService->log( 'hostMeta Exception - ' . get_class($e) . ' - ' . $e->getMessage(), 0 @@ -237,14 +233,13 @@ class CurlService { * @throws UnauthorizedFediverseException */ public function retrieveObject($id): array { - $url = parse_url($id); $this->mustContains(['path', 'host', 'scheme'], $url); $request = new Request($url['path'], Request::TYPE_GET); $request->setAddress($url['host']); $request->setProtocol($url['scheme']); - $result = $this->request($request); + $result = $this->retrieveJson($request); if (is_array($result)) { $result['_host'] = $request->getAddress(); } @@ -256,60 +251,46 @@ class CurlService { /** * @param Request $request * - * @return mixed + * @return array + * @throws SocialAppConfigException + * @throws UnauthorizedFediverseException * @throws RequestContentException * @throws RequestNetworkException * @throws RequestResultNotJsonException * @throws RequestResultSizeException * @throws RequestServerException + */ + public function retrieveJson(Request $request): array { + try { + $result = $this->retrieveJsonOrig($request); + } catch (RequestResultSizeException | RequestResultNotJsonException $e) { + $this->miscService->log( + '[!!] request: ' . json_encode($request) . ' - content-type: ' + . $request->getContentType() . ' - ' . $e->getMessage(), 1 + ); + throw $e; + } + + return $result; + } + + + /** + * @param Request $request + * + * @return mixed * @throws SocialAppConfigException * @throws UnauthorizedFediverseException + * @throws RequestContentException + * @throws RequestNetworkException + * @throws RequestResultSizeException + * @throws RequestServerException */ - public function request(Request $request) { + public function doRequest(Request $request) { $this->fediverseService->authorized($request->getAddress()); - - $this->maxDownloadSizeReached = false; $this->assignUserAgent($request); - $curl = $this->initRequest($request); - - $this->initRequestGet($request); - $this->initRequestPost($curl, $request); - $this->initRequestPut($curl, $request); - $this->initRequestDelete($curl, $request); - - $this->initRequestHeaders($curl, $request); - - $result = curl_exec($curl); - - if ($this->maxDownloadSizeReached === true) { - throw new RequestResultSizeException(); - } - $this->parseRequestResult($curl, $request); - - if ($request->isBinary()) { - $this->miscService->log('[>>] request (binary): ' . json_encode($request), 1); - - return $result; - } - - $this->miscService->log( - '[>>] request: ' . json_encode($request) . ' - content-type: ' - . $request->getContentType() . ' - result: ' . $result, 1 - ); - - if (strpos($request->getContentType(), 'application/xrd') === 0) { - $xml = simplexml_load_string($result); - $result = json_encode($xml, JSON_UNESCAPED_SLASHES); - $this->miscService->log('XRD conversion to JSON: ' . $result, 1); - } - - $result = json_decode((string)$result, true); - if (is_array($result)) { - return $result; - } - - throw new RequestResultNotJsonException(); + return $this->doRequestOrig($request); } @@ -340,7 +321,7 @@ class CurlService { $request->setProtocol(parse_url($address, PHP_URL_SCHEME)); try { - $this->request($request); + $this->retrieveJson($request); } catch (RequestResultNotJsonException $e) { } catch (Exception $e) { $this->miscService->log( @@ -353,73 +334,8 @@ class CurlService { /** * @param Request $request - * - * @return resource */ - private function initRequest(Request $request) { - - $curl = $this->generateCurlRequest($request); - - curl_setopt($curl, CURLOPT_USERAGENT, $request->getUserAgent()); - curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, $request->getTimeout()); - curl_setopt($curl, CURLOPT_TIMEOUT, $request->getTimeout()); - - curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); - curl_setopt($curl, CURLOPT_BINARYTRANSFER, $request->isBinary()); - - curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); - curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); - - $this->maxDownloadSize = - $this->configService->getAppValue(ConfigService::SOCIAL_MAX_SIZE) * (1024 * 1024); - curl_setopt($curl, CURLOPT_BUFFERSIZE, 128); - curl_setopt($curl, CURLOPT_NOPROGRESS, false); - curl_setopt( - /** - * @param $downloadSize - * @param int $downloaded - * @param $uploadSize - * @param int $uploaded - * - * @return int - */ - $curl, CURLOPT_PROGRESSFUNCTION, - function($downloadSize, int $downloaded, $uploadSize, int $uploaded) { - if ($downloaded > $this->maxDownloadSize) { - $this->maxDownloadSizeReached = true; - - return 1; - } - - return 0; - } - ); - - return $curl; - } - - - /** - * @param Request $request - * - * @return resource - */ - private function generateCurlRequest(Request $request) { - $url = $request->getProtocol() . '://' . $request->getAddress() . $request->getParsedUrl(); - if ($request->getType() !== Request::TYPE_GET) { - $curl = curl_init($url); - } else { - $curl = curl_init($url . '?' . $request->getUrlData()); - } - - return $curl; - } - - - /** - * @param Request $request - */ - private function initRequestGet(Request $request) { + protected function initRequestGet(Request $request) { if ($request->getType() !== Request::TYPE_GET) { return; } @@ -430,6 +346,8 @@ class CurlService { $request->addHeader( 'Accept: application/ld+json; profile="https://www.w3.org/ns/activitystreams"' ); + + $this->initRequestGetOrig($request); } @@ -437,7 +355,7 @@ class CurlService { * @param resource $curl * @param Request $request */ - private function initRequestPost($curl, Request $request) { + protected function initRequestPost($curl, Request $request) { if ($request->getType() !== Request::TYPE_POST) { return; } @@ -446,132 +364,7 @@ class CurlService { 'Content-Type: application/ld+json; profile="https://www.w3.org/ns/activitystreams"' ); - curl_setopt($curl, CURLOPT_POST, true); - curl_setopt($curl, CURLOPT_POSTFIELDS, $request->getDataBody()); - } - - - /** - * @param resource $curl - * @param Request $request - */ - private function initRequestPut($curl, Request $request) { - if ($request->getType() !== Request::TYPE_PUT) { - return; - } - - curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT"); - curl_setopt($curl, CURLOPT_POSTFIELDS, $request->getDataBody()); - } - - - /** - * @param resource $curl - * @param Request $request - */ - private function initRequestDelete($curl, Request $request) { - if ($request->getType() !== Request::TYPE_DELETE) { - return; - } - - curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "DELETE"); - curl_setopt($curl, CURLOPT_POSTFIELDS, $request->getDataBody()); - } - - - /** - * @param resource $curl - * @param Request $request - */ - private function initRequestHeaders($curl, Request $request) { - $headers = $request->getHeaders(); - - curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); - } - - - /** - * @param resource $curl - * @param Request $request - * - * @throws RequestContentException - * @throws RequestServerException - * @throws RequestNetworkException - */ - private function parseRequestResult($curl, Request &$request) { - $this->parseRequestResultCurl($curl, $request); - - $code = curl_getinfo($curl, CURLINFO_HTTP_CODE); - $type = curl_getinfo($curl, CURLINFO_CONTENT_TYPE); - $request->setContentType((is_null($type) || is_bool($type)) ? '' : $type); - $request->setResultCode($code); - - $this->parseRequestResultCode301($code, $request); - $this->parseRequestResultCode4xx($code, $request); - $this->parseRequestResultCode5xx($code, $request); - } - - - /** - * @param resource $curl - * @param Request $request - * - * @throws RequestNetworkException - */ - private function parseRequestResultCurl($curl, Request $request) { - $errno = curl_errno($curl); - if ($errno > 0) { - throw new RequestNetworkException( - $errno . ' - ' . curl_error($curl) . ' - ' . json_encode( - $request, JSON_UNESCAPED_SLASHES - ), $errno - ); - } - } - - - /** - * @param int $code - * @param Request $request - * - * @throws RequestContentException - */ - private function parseRequestResultCode301($code, Request $request) { - if ($code === 301) { - throw new RequestContentException( - '301 - ' . json_encode($request, JSON_UNESCAPED_SLASHES) - ); - } - } - - - /** - * @param int $code - * @param Request $request - * - * @throws RequestContentException - */ - private function parseRequestResultCode4xx(int $code, Request $request) { - if ($code === 404 || $code === 410) { - throw new RequestContentException( - $code . ' - ' . json_encode($request, JSON_UNESCAPED_SLASHES) - ); - } - } - - - /** - * @param int $code - * @param Request $request - * - * @throws RequestServerException - */ - private function parseRequestResultCode5xx(int $code, Request $request) { - if ($code === 500) { - throw new RequestServerException( - $code . ' - ' . json_encode($request, JSON_UNESCAPED_SLASHES) - ); - } + $this->initRequestPostOrig($curl, $request); } } diff --git a/lib/Service/DocumentService.php b/lib/Service/DocumentService.php index c5318bc3..d164d87e 100644 --- a/lib/Service/DocumentService.php +++ b/lib/Service/DocumentService.php @@ -32,6 +32,11 @@ namespace OCA\Social\Service; use daita\MySmallPhpTools\Exceptions\MalformedArrayException; +use daita\MySmallPhpTools\Exceptions\RequestContentException; +use daita\MySmallPhpTools\Exceptions\RequestNetworkException; +use daita\MySmallPhpTools\Exceptions\RequestResultNotJsonException; +use daita\MySmallPhpTools\Exceptions\RequestResultSizeException; +use daita\MySmallPhpTools\Exceptions\RequestServerException; use Exception; use OCA\Social\AP; use OCA\Social\Db\ActorsRequest; @@ -42,11 +47,6 @@ use OCA\Social\Exceptions\CacheContentMimeTypeException; use OCA\Social\Exceptions\CacheDocumentDoesNotExistException; use OCA\Social\Exceptions\ItemAlreadyExistsException; use OCA\Social\Exceptions\ItemUnknownException; -use OCA\Social\Exceptions\RequestContentException; -use OCA\Social\Exceptions\RequestNetworkException; -use OCA\Social\Exceptions\RequestResultNotJsonException; -use OCA\Social\Exceptions\RequestResultSizeException; -use OCA\Social\Exceptions\RequestServerException; use OCA\Social\Exceptions\SocialAppConfigException; use OCA\Social\Exceptions\UnauthorizedFediverseException; use OCA\Social\Exceptions\UrlCloudException; @@ -122,7 +122,6 @@ class DocumentService { * @return Document * @throws CacheDocumentDoesNotExistException * @throws MalformedArrayException - * @throws RequestResultNotJsonException * @throws SocialAppConfigException */ public function cacheRemoteDocument(string $id, bool $public = false) { @@ -217,10 +216,9 @@ class DocumentService { * @throws CacheContentException * @throws CacheDocumentDoesNotExistException * @throws MalformedArrayException - * @throws RequestResultNotJsonException * @throws SocialAppConfigException */ - public function getFromCache(string $id, string &$mimeType = '', bool $public = false) { + public function getFromCache(string $id, string &$mimeType = '', bool $public = false): ISimpleFile { $document = $this->cacheRemoteDocument($id, $public); $mimeType = $document->getMimeType(); diff --git a/lib/Service/FollowService.php b/lib/Service/FollowService.php index a4f4b033..200928a6 100644 --- a/lib/Service/FollowService.php +++ b/lib/Service/FollowService.php @@ -41,11 +41,11 @@ use OCA\Social\Exceptions\InvalidOriginException; use OCA\Social\Exceptions\InvalidResourceException; use OCA\Social\Exceptions\ItemUnknownException; use OCA\Social\Exceptions\RedundancyLimitException; -use OCA\Social\Exceptions\RequestContentException; -use OCA\Social\Exceptions\RequestNetworkException; -use OCA\Social\Exceptions\RequestResultNotJsonException; -use OCA\Social\Exceptions\RequestResultSizeException; -use OCA\Social\Exceptions\RequestServerException; +use daita\MySmallPhpTools\Exceptions\RequestContentException; +use daita\MySmallPhpTools\Exceptions\RequestNetworkException; +use daita\MySmallPhpTools\Exceptions\RequestResultNotJsonException; +use daita\MySmallPhpTools\Exceptions\RequestResultSizeException; +use daita\MySmallPhpTools\Exceptions\RequestServerException; use OCA\Social\Exceptions\RetrieveAccountFormatException; use OCA\Social\Exceptions\SocialAppConfigException; use OCA\Social\Exceptions\UnauthorizedFediverseException; diff --git a/lib/Service/PostService.php b/lib/Service/PostService.php index d9222fb3..abd1e01d 100644 --- a/lib/Service/PostService.php +++ b/lib/Service/PostService.php @@ -38,11 +38,11 @@ use OCA\Social\Exceptions\InvalidOriginException; use OCA\Social\Exceptions\InvalidResourceException; use OCA\Social\Exceptions\ItemUnknownException; use OCA\Social\Exceptions\RedundancyLimitException; -use OCA\Social\Exceptions\RequestContentException; -use OCA\Social\Exceptions\RequestNetworkException; -use OCA\Social\Exceptions\RequestResultNotJsonException; -use OCA\Social\Exceptions\RequestResultSizeException; -use OCA\Social\Exceptions\RequestServerException; +use daita\MySmallPhpTools\Exceptions\RequestContentException; +use daita\MySmallPhpTools\Exceptions\RequestNetworkException; +use daita\MySmallPhpTools\Exceptions\RequestResultNotJsonException; +use daita\MySmallPhpTools\Exceptions\RequestResultSizeException; +use daita\MySmallPhpTools\Exceptions\RequestServerException; use OCA\Social\Exceptions\SocialAppConfigException; use OCA\Social\Exceptions\StreamNotFoundException; use OCA\Social\Exceptions\UnauthorizedFediverseException; diff --git a/lib/Service/SignatureService.php b/lib/Service/SignatureService.php index d991c205..093a9d3e 100644 --- a/lib/Service/SignatureService.php +++ b/lib/Service/SignatureService.php @@ -46,11 +46,11 @@ use OCA\Social\Exceptions\InvalidResourceException; use OCA\Social\Exceptions\ItemUnknownException; use OCA\Social\Exceptions\LinkedDataSignatureMissingException; use OCA\Social\Exceptions\RedundancyLimitException; -use OCA\Social\Exceptions\RequestContentException; -use OCA\Social\Exceptions\RequestNetworkException; -use OCA\Social\Exceptions\RequestResultNotJsonException; -use OCA\Social\Exceptions\RequestResultSizeException; -use OCA\Social\Exceptions\RequestServerException; +use daita\MySmallPhpTools\Exceptions\RequestContentException; +use daita\MySmallPhpTools\Exceptions\RequestNetworkException; +use daita\MySmallPhpTools\Exceptions\RequestResultNotJsonException; +use daita\MySmallPhpTools\Exceptions\RequestResultSizeException; +use daita\MySmallPhpTools\Exceptions\RequestServerException; use OCA\Social\Exceptions\SignatureException; use OCA\Social\Exceptions\SignatureIsGoneException; use OCA\Social\Exceptions\SocialAppConfigException; diff --git a/lib/Service/StreamQueueService.php b/lib/Service/StreamQueueService.php index 6c0534f3..e51319dc 100644 --- a/lib/Service/StreamQueueService.php +++ b/lib/Service/StreamQueueService.php @@ -42,11 +42,11 @@ use OCA\Social\Exceptions\ItemUnknownException; use OCA\Social\Exceptions\StreamNotFoundException; use OCA\Social\Exceptions\QueueStatusException; use OCA\Social\Exceptions\RedundancyLimitException; -use OCA\Social\Exceptions\RequestContentException; -use OCA\Social\Exceptions\RequestNetworkException; -use OCA\Social\Exceptions\RequestResultNotJsonException; -use OCA\Social\Exceptions\RequestResultSizeException; -use OCA\Social\Exceptions\RequestServerException; +use daita\MySmallPhpTools\Exceptions\RequestContentException; +use daita\MySmallPhpTools\Exceptions\RequestNetworkException; +use daita\MySmallPhpTools\Exceptions\RequestResultNotJsonException; +use daita\MySmallPhpTools\Exceptions\RequestResultSizeException; +use daita\MySmallPhpTools\Exceptions\RequestServerException; use OCA\Social\Exceptions\SocialAppConfigException; use OCA\Social\Exceptions\UnauthorizedFediverseException; use OCA\Social\Model\ActivityPub\Object\Note; diff --git a/lib/Service/StreamService.php b/lib/Service/StreamService.php index d6bb64c2..8c77e589 100644 --- a/lib/Service/StreamService.php +++ b/lib/Service/StreamService.php @@ -30,18 +30,19 @@ declare(strict_types=1); namespace OCA\Social\Service; +use daita\MySmallPhpTools\Exceptions\DateTimeException; use daita\MySmallPhpTools\Exceptions\MalformedArrayException; +use daita\MySmallPhpTools\Exceptions\RequestContentException; +use daita\MySmallPhpTools\Exceptions\RequestNetworkException; +use daita\MySmallPhpTools\Exceptions\RequestResultNotJsonException; +use daita\MySmallPhpTools\Exceptions\RequestResultSizeException; +use daita\MySmallPhpTools\Exceptions\RequestServerException; use Exception; use OCA\Social\Db\StreamRequest; use OCA\Social\Exceptions\InvalidOriginException; use OCA\Social\Exceptions\InvalidResourceException; use OCA\Social\Exceptions\ItemUnknownException; use OCA\Social\Exceptions\RedundancyLimitException; -use OCA\Social\Exceptions\RequestContentException; -use OCA\Social\Exceptions\RequestNetworkException; -use OCA\Social\Exceptions\RequestResultNotJsonException; -use OCA\Social\Exceptions\RequestResultSizeException; -use OCA\Social\Exceptions\RequestServerException; use OCA\Social\Exceptions\SocialAppConfigException; use OCA\Social\Exceptions\StreamNotFoundException; use OCA\Social\Exceptions\UnauthorizedFediverseException; @@ -81,10 +82,6 @@ class StreamService { private $miscService; - /** @var Person */ - private $viewer = null; - - /** * NoteService constructor. * @@ -118,7 +115,6 @@ class StreamService { * @param Person $viewer */ public function setViewer(Person $viewer) { - $this->viewer = $viewer; $this->streamRequest->setViewer($viewer); } @@ -149,7 +145,7 @@ class StreamService { * * @throws Exception */ - public function assignStream(Stream &$stream) { + public function assignStream(Stream $stream) { $stream->convertPublished(); } @@ -384,7 +380,6 @@ class StreamService { * * @return Stream * @throws StreamNotFoundException - * @throws SocialAppConfigException */ public function getStreamById(string $id, bool $asViewer = false): Stream { return $this->streamRequest->getStreamById($id, $asViewer); @@ -399,14 +394,15 @@ class StreamService { * * @return Stream[] * @throws StreamNotFoundException + * @throws DateTimeException */ - public function getRepliesByParentId(string $id, int $since = 0, int $limit = 5, bool $asViewer = false): array { + public function getRepliesByParentId(string $id, int $since = 0, int $limit = 5, bool $asViewer = false + ): array { return $this->streamRequest->getRepliesByParentId($id, $since, $limit, $asViewer); } /** - * @param Person $actor * @param int $since * @param int $limit * @@ -419,15 +415,14 @@ class StreamService { /** - * @param Person $actor * @param int $since * @param int $limit * * @return Note[] * @throws Exception */ - public function getStreamNotifications(Person $actor, int $since = 0, int $limit = 5): array { - return $this->streamRequest->getTimelineNotifications($actor, $since, $limit); + public function getStreamNotifications(int $since = 0, int $limit = 5): array { + return $this->streamRequest->getTimelineNotifications($since, $limit); } @@ -445,7 +440,6 @@ class StreamService { /** - * @param Person $actor * @param int $since * @param int $limit * @@ -470,7 +464,6 @@ class StreamService { /** - * @param Person $actor * @param string $hashtag * @param int $since * @param int $limit diff --git a/package-lock.json b/package-lock.json index d134390d..2d4bf897 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10083,7 +10083,8 @@ "jquery": { "version": "3.5.1", "resolved": "https://registry.npmjs.org/jquery/-/jquery-3.5.1.tgz", - "integrity": "sha512-XwIBPqcMn57FxfT+Go5pzySnm4KWkT1Tv7gjrpT1srtf8Weynl6R273VJ5GjkRb51IzMp5nbaPjJXMWeju2MKg==" + "integrity": "sha512-XwIBPqcMn57FxfT+Go5pzySnm4KWkT1Tv7gjrpT1srtf8Weynl6R273VJ5GjkRb51IzMp5nbaPjJXMWeju2MKg==", + "optional": true }, "js-base64": { "version": "2.5.1",