diff options
author | Maxence Lange <maxence@artificial-owl.com> | 2019-10-11 10:55:21 -0100 |
---|---|---|
committer | Maxence Lange <maxence@artificial-owl.com> | 2019-10-14 10:47:07 -0100 |
commit | ee79f483d41af19f28342d125ada74d9a2ce5a49 (patch) | |
tree | 2bf19ed58b7788dc3ef0fa677d8e82f29c4b3c52 | |
parent | e2d33c376498695bb922dab56cf42fb66e2b2a4d (diff) |
new route for ostatus
Signed-off-by: Maxence Lange <maxence@artificial-owl.com>
-rw-r--r-- | appinfo/routes.php | 4 | ||||
-rw-r--r-- | lib/Controller/OStatusController.php | 74 | ||||
-rw-r--r-- | lib/Service/StreamService.php | 86 | ||||
-rw-r--r-- | lib/webfinger.php | 7 |
4 files changed, 148 insertions, 23 deletions
diff --git a/appinfo/routes.php b/appinfo/routes.php index b7e5d6e6..1474fe02 100644 --- a/appinfo/routes.php +++ b/appinfo/routes.php @@ -56,7 +56,9 @@ return [ ['name' => 'ActivityPub#followers', 'url' => '/@{username}/followers', 'verb' => 'GET'], ['name' => 'ActivityPub#following', 'url' => '/@{username}/following', 'verb' => 'GET'], - ['name' => 'OStatus#subscribe', 'url' => '/ostatus/follow/{uri}', 'verb' => 'GET'], + ['name' => 'OStatus#subscribeOld', 'url' => '/ostatus/follow/{uri}', 'verb' => 'GET'], + ['name' => 'OStatus#subscribe', 'url' => '/ostatus/interaction', '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'], diff --git a/lib/Controller/OStatusController.php b/lib/Controller/OStatusController.php index cc012e87..8af7604f 100644 --- a/lib/Controller/OStatusController.php +++ b/lib/Controller/OStatusController.php @@ -36,10 +36,12 @@ use daita\MySmallPhpTools\Traits\TArrayTools; use Exception; use OCA\Social\AppInfo\Application; use OCA\Social\Exceptions\RetrieveAccountFormatException; +use OCA\Social\Model\ActivityPub\Actor\Person; use OCA\Social\Service\AccountService; use OCA\Social\Service\CacheActorService; use OCA\Social\Service\CurlService; use OCA\Social\Service\MiscService; +use OCA\Social\Service\StreamService; use OCP\AppFramework\Controller; use OCP\AppFramework\Http\Response; use OCP\AppFramework\Http\TemplateResponse; @@ -55,9 +57,15 @@ class OStatusController extends Controller { use TArrayTools; + /** @var IUserManager */ + private $userSession; + /** @var CacheActorService */ private $cacheActorService; + /** @var StreamService */ + private $streamService; + /** @var AccountService */ private $accountService; @@ -67,14 +75,12 @@ class OStatusController extends Controller { /** @var MiscService */ private $miscService; - /** @var IUserManager */ - private $userSession; - /** * OStatusController constructor. * * @param IRequest $request + * @param StreamService $streamService * @param CacheActorService $cacheActorService * @param AccountService $accountService * @param CurlService $curlService @@ -82,12 +88,14 @@ class OStatusController extends Controller { * @param IUserSession $userSession */ public function __construct( - IRequest $request, CacheActorService $cacheActorService, AccountService $accountService, - CurlService $curlService, MiscService $miscService, IUserSession $userSession + IUserSession $userSession, IRequest $request, StreamService $streamService, + CacheActorService $cacheActorService, AccountService $accountService, CurlService $curlService, + MiscService $miscService ) { parent::__construct(Application::APP_NAME, $request); $this->cacheActorService = $cacheActorService; + $this->streamService = $streamService; $this->accountService = $accountService; $this->curlService = $curlService; $this->miscService = $miscService; @@ -103,25 +111,62 @@ class OStatusController extends Controller { * * @return Response */ - public function subscribe(string $uri): Response { + public function subscribeOld(string $uri): Response { + return $this->subscribe($uri); + } + + /** + * @NoCSRFRequired + * @NoAdminRequired + * + * @param string $uri + * + * @return Response + * @throws Exception + */ + public function subscribe(string $uri): Response { try { $actor = $this->cacheActorService->getFromAccount($uri); + return $this->subscribeLocalAccount($actor); + } catch (Exception $e) { + } + + try { + $post = $this->streamService->getStreamById($uri, true, true); + + return $this->directSuccess($post); + } catch (Exception $e) { + } + + return $this->fail(new Exception('unknown protocol')); + } + + + /** + * @param Person $actor + * + * @return Response + */ + private function subscribeLocalAccount(Person $actor): Response { + try { $user = $this->userSession->getUser(); if ($user === null) { throw new Exception('Failed to retrieve current user'); } - return new TemplateResponse('social', 'ostatus', [ + return new TemplateResponse( + 'social', 'ostatus', [ 'serverData' => [ - 'account' => $actor->getAccount(), + 'account' => $actor->getAccount(), 'currentUser' => [ - 'uid' => $user->getUID(), + 'uid' => $user->getUID(), 'displayName' => $user->getDisplayName(), ] ] - ], 'guest'); + ], 'guest' + ); } catch (Exception $e) { return $this->fail($e); } @@ -134,18 +179,21 @@ class OStatusController extends Controller { * @PublicPage * * @param string $local + * * @return Response */ public function followRemote(string $local): Response { try { $following = $this->accountService->getActor($local); - return new TemplateResponse('social', 'ostatus', [ + return new TemplateResponse( + 'social', 'ostatus', [ 'serverData' => [ - 'local' => $local, + 'local' => $local, 'account' => $following->getAccount() ] - ], 'guest'); + ], 'guest' + ); } catch (Exception $e) { return $this->fail($e); } diff --git a/lib/Service/StreamService.php b/lib/Service/StreamService.php index a14f2656..986aa94b 100644 --- a/lib/Service/StreamService.php +++ b/lib/Service/StreamService.php @@ -32,9 +32,11 @@ namespace OCA\Social\Service; use daita\MySmallPhpTools\Exceptions\MalformedArrayException; use Exception; +use OCA\Social\AP; use OCA\Social\Db\StreamRequest; use OCA\Social\Exceptions\InvalidOriginException; 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; @@ -74,6 +76,9 @@ class StreamService { /** @var CacheActorService */ private $cacheActorService; + /** @var CurlService */ + private $curlService; + /** @var ConfigService */ private $configService; @@ -94,6 +99,7 @@ class StreamService { * @param SignatureService $signatureService * @param StreamQueueService $streamQueueService * @param CacheActorService $cacheActorService + * @param CurlService $curlService * @param ConfigService $configService * @param MiscService $miscService */ @@ -101,7 +107,7 @@ class StreamService { StreamRequest $streamRequest, ActivityService $activityService, AccountService $accountService, SignatureService $signatureService, StreamQueueService $streamQueueService, CacheActorService $cacheActorService, - ConfigService $configService, MiscService $miscService + CurlService $curlService, ConfigService $configService, MiscService $miscService ) { $this->streamRequest = $streamRequest; $this->activityService = $activityService; @@ -109,6 +115,7 @@ class StreamService { $this->signatureService = $signatureService; $this->streamQueueService = $streamQueueService; $this->cacheActorService = $cacheActorService; + $this->curlService = $curlService; $this->configService = $configService; $this->miscService = $miscService; } @@ -381,13 +388,83 @@ class StreamService { /** * @param string $id * @param bool $asViewer + * @param bool $retrieve * * @return Stream * @throws StreamNotFoundException + * @throws Exception + */ + public function getStreamById(string $id, bool $asViewer = false, bool $retrieve = false): Stream { + try { + return $this->streamRequest->getStreamById($id, $asViewer); + } catch (StreamNotFoundException $e) { + if (!$retrieve) { + throw $e; + } + + if ($asViewer) { + try { + $this->streamRequest->getStreamById($id, false); + throw $e; + } catch (StreamNotFoundException $e) { + } + } + } + + return $this->retrieveStream($id); + } + + + /** + * @param string $id + * + * @return Stream + * @throws InvalidOriginException + * @throws InvalidResourceException + * @throws ItemUnknownException + * @throws MalformedArrayException + * @throws RedundancyLimitException + * @throws RequestContentException + * @throws RequestNetworkException + * @throws RequestResultNotJsonException + * @throws RequestResultSizeException + * @throws RequestServerException * @throws SocialAppConfigException + * @throws UnauthorizedFediverseException + * @throws StreamNotFoundException */ - public function getStreamById(string $id, bool $asViewer = false): Stream { - return $this->streamRequest->getStreamById($id, $asViewer); + public function retrieveStream(string $id) { + $data = $this->curlService->retrieveObject($id); + $object = AP::$activityPub->getItemFromData($data); + + $origin = parse_url($id, PHP_URL_HOST); + $object->setOrigin($origin, SignatureService::ORIGIN_REQUEST, time()); + + if ($object->getId() !== $id) { + throw new InvalidOriginException( + 'StreamServiceStreamQueueService::getStreamById - objectId: ' . $object->getId() . ' - id: ' + . $id + ); + } + + if ($object->getType() !== Note::TYPE + // do we also retrieve Announce ? + //|| $object->getType() !== Announce:TYPE + ) { + throw new InvalidResourceException(); + } + + /** @var Stream $object */ + $this->cacheActorService->getFromId($object->getAttributedTo()); + + $interface = AP::$activityPub->getInterfaceForItem($object); + try { + $interface->save($object); + } catch (ItemAlreadyExistsException $e) { + } + + + return $this->streamRequest->getStreamById($id); } @@ -400,7 +477,8 @@ class StreamService { * @return Stream[] * @throws StreamNotFoundException */ - 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); } diff --git a/lib/webfinger.php b/lib/webfinger.php index 499dfac4..abafdeba 100644 --- a/lib/webfinger.php +++ b/lib/webfinger.php @@ -128,11 +128,8 @@ $finger = [ ], [ 'rel' => 'http://ostatus.org/schema/1.0/subscribe', - 'template' => urldecode( - $href = $urlGenerator->linkToRouteAbsolute( - 'social.OStatus.subscribe', ['uri' => '{uri}'] - ) - ) + 'template' => urldecode($href = $urlGenerator->linkToRouteAbsolute('social.OStatus.subscribe')) + . '?uri={uri}' ] ] ]; |