diff options
author | Maxence Lange <maxence@artificial-owl.com> | 2020-07-28 23:50:21 -0100 |
---|---|---|
committer | Maxence Lange <maxence@artificial-owl.com> | 2020-09-23 11:41:28 -0100 |
commit | 888d9931413f5ddbb540ca8d9ca4833cf74ddbde (patch) | |
tree | b5f2d2755d14cf1819c119bf8242f373cd569d32 /lib | |
parent | 8043004d62e55f353c64ed8f5d22594f70bbfee3 (diff) |
using IWebfingerManager
Signed-off-by: Maxence Lange <maxence@artificial-owl.com>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/AppInfo/Application.php | 24 | ||||
-rw-r--r-- | lib/Db/StreamActionsRequest.php | 4 | ||||
-rw-r--r-- | lib/Model/WebfingerLink.php | 148 | ||||
-rw-r--r-- | lib/Service/ActorService.php | 8 | ||||
-rw-r--r-- | lib/Service/CacheActorService.php | 18 | ||||
-rw-r--r-- | lib/Service/WebfingerService.php | 133 |
6 files changed, 329 insertions, 6 deletions
diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php index 18996bac..9aeb978a 100644 --- a/lib/AppInfo/Application.php +++ b/lib/AppInfo/Application.php @@ -32,12 +32,16 @@ namespace OCA\Social\AppInfo; use OC\DB\SchemaWrapper; +use OC\Webfinger\Event\WebfingerEvent; +use OC\Webfinger\Model\WebfingerObject; use OCA\Social\Notification\Notifier; use OCA\Social\Service\ConfigService; use OCA\Social\Service\UpdateService; +use OCA\Social\Service\WebfingerService; use OCP\AppFramework\App; use OCP\AppFramework\IAppContainer; use OCP\AppFramework\QueryException; +use OCP\EventDispatcher\IEventDispatcher; /** @@ -80,6 +84,26 @@ class Application extends App { /** * */ + public function registerWebfinger() { + /** @var IEventDispatcher $eventDispatcher */ + $eventDispatcher = \OC::$server->query(IEventDispatcher::class); + $eventDispatcher->addListener( + '\OC\Webfinger::onRequest', + function(WebfingerEvent $e) { + /** @var WebfingerService $webfingerService */ + $webfingerService = $this->container->query(WebfingerService::class); + try { + $webfingerService->webfinger($e); + } catch (\Exception $e) { + } + } + ); + } + + + /** + * + */ public function checkUpgradeStatus() { $upgradeChecked = $this->container->getServer() ->getConfig() diff --git a/lib/Db/StreamActionsRequest.php b/lib/Db/StreamActionsRequest.php index 99cf906d..2bacf14b 100644 --- a/lib/Db/StreamActionsRequest.php +++ b/lib/Db/StreamActionsRequest.php @@ -96,9 +96,7 @@ class StreamActionsRequest extends StreamActionsRequestBuilder { $this->limitToActorId($qb, $action->getActorId()); $this->limitToStreamId($qb, $action->getStreamId()); - $count = $qb->execute(); - - return $count; + return $qb->execute(); } diff --git a/lib/Model/WebfingerLink.php b/lib/Model/WebfingerLink.php new file mode 100644 index 00000000..5e44f593 --- /dev/null +++ b/lib/Model/WebfingerLink.php @@ -0,0 +1,148 @@ +<?php +declare(strict_types=1); + + +/** + * Nextcloud - Social Support + * + * This file is licensed under the Affero General Public License version 3 or + * later. See the COPYING file. + * + * @author Maxence Lange <maxence@artificial-owl.com> + * @copyright 2018, Maxence Lange <maxence@artificial-owl.com> + * @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 <http://www.gnu.org/licenses/>. + * + */ + + +namespace OCA\Social\Model; + +use JsonSerializable; + + +/** + * Class WebfingerLink + * + * @package OCA\Social\Model + */ +class WebfingerLink implements JsonSerializable { + + + /** @var string */ + private $href = ''; + + /** @var string */ + private $rel = ''; + + /** @var string */ + private $template = ''; + + /** @var string */ + private $type = ''; + + + /** + * @return string + */ + public function getHref(): string { + return $this->href; + } + + /** + * @param string $value + * + * @return WebfingerLink + */ + public function setHref(string $value): self { + $this->href = $value; + + return $this; + } + + + /** + * @return string + */ + public function getType(): string { + return $this->type; + } + + /** + * @param string $value + * + * @return WebfingerLink + */ + public function setType(string $value): self { + $this->type = $value; + + return $this; + } + + + /** + * @return string + */ + public function getRel(): string { + return $this->rel; + } + + /** + * @param string $value + * + * @return WebfingerLink + */ + public function setRel(string $value): self { + $this->rel = $value; + + return $this; + } + + + /** + * @return string + */ + public function getTemplate(): string { + return $this->template; + } + + /** + * @param string $value + * + * @return WebfingerLink + */ + public function setTemplate(string $value): self { + $this->template = $value; + + return $this; + } + + + /** + * @return array + */ + public function jsonSerialize(): array { + $data = [ + 'rel' => $this->getRel(), + 'type' => $this->getType(), + 'template' => $this->getTemplate(), + 'href' => $this->getHref() + ]; + + return array_filter($data); + } + +} + diff --git a/lib/Service/ActorService.php b/lib/Service/ActorService.php index 429ebf88..a8fe47d1 100644 --- a/lib/Service/ActorService.php +++ b/lib/Service/ActorService.php @@ -36,6 +36,7 @@ use OCA\Social\Db\CacheActorsRequest; use OCA\Social\Db\CacheDocumentsRequest; use OCA\Social\Exceptions\CacheActorDoesNotExistException; use OCA\Social\Exceptions\CacheDocumentDoesNotExistException; +use OCA\Social\Exceptions\ItemAlreadyExistsException; use OCA\Social\Exceptions\ItemUnknownException; use OCA\Social\Model\ActivityPub\Actor\Person; @@ -92,6 +93,8 @@ class ActorService { /** * @param Person $actor + * + * @throws ItemAlreadyExistsException */ public function cacheLocalActor(Person $actor) { $actor->setLocal(true); @@ -108,6 +111,8 @@ class ActorService { /** * @param Person $actor + * + * @throws ItemAlreadyExistsException */ public function save(Person $actor) { $this->cacheDocumentIfNeeded($actor); @@ -119,6 +124,7 @@ class ActorService { * @param Person $actor * * @return int + * @throws ItemAlreadyExistsException */ public function update(Person $actor): int { $this->cacheDocumentIfNeeded($actor); @@ -129,6 +135,8 @@ class ActorService { /** * @param Person $actor + * + * @throws ItemAlreadyExistsException */ private function cacheDocumentIfNeeded(Person $actor) { if ($actor->hasIcon()) { diff --git a/lib/Service/CacheActorService.php b/lib/Service/CacheActorService.php index f049c99a..b448a682 100644 --- a/lib/Service/CacheActorService.php +++ b/lib/Service/CacheActorService.php @@ -50,6 +50,7 @@ use OCA\Social\Exceptions\RetrieveAccountFormatException; use OCA\Social\Exceptions\SocialAppConfigException; use OCA\Social\Exceptions\UnauthorizedFediverseException; use OCA\Social\Model\ActivityPub\Actor\Person; +use OCP\IURLGenerator; class CacheActorService { @@ -58,12 +59,18 @@ class CacheActorService { use TArrayTools; + /** @var IURLGenerator */ + private $urlGenerator; + /** @var CacheActorsRequest */ private $cacheActorsRequest; /** @var CurlService */ private $curlService; + /** @var FediverseService */ + private $fediverseService; + /** @var ConfigService */ private $configService; @@ -74,17 +81,21 @@ class CacheActorService { /** * CacheService constructor. * + * @param IUrlGenerator $urlGenerator * @param CacheActorsRequest $cacheActorsRequest * @param CurlService $curlService + * @param FediverseService $fediverseService * @param ConfigService $configService * @param MiscService $miscService */ public function __construct( - CacheActorsRequest $cacheActorsRequest, CurlService $curlService, - ConfigService $configService, MiscService $miscService + IUrlGenerator $urlGenerator, CacheActorsRequest $cacheActorsRequest, CurlService $curlService, + FediverseService $fediverseService, ConfigService $configService, MiscService $miscService ) { + $this->urlGenerator = $urlGenerator; $this->cacheActorsRequest = $cacheActorsRequest; $this->curlService = $curlService; + $this->fediverseService = $fediverseService; $this->configService = $configService; $this->miscService = $miscService; } @@ -166,8 +177,9 @@ class CacheActorService { */ public function getFromLocalAccount(string $account): Person { $instance = ''; + $account = ltrim($account, '@'); if (strrpos($account, '@')) { - list($account, $instance) = explode('@', $account); + list($account, $instance) = explode('@', $account, 2); } if ($instance === '' diff --git a/lib/Service/WebfingerService.php b/lib/Service/WebfingerService.php new file mode 100644 index 00000000..c0169886 --- /dev/null +++ b/lib/Service/WebfingerService.php @@ -0,0 +1,133 @@ +<?php +declare(strict_types=1); + + +/** + * Nextcloud - Social Support + * + * This file is licensed under the Affero General Public License version 3 or + * later. See the COPYING file. + * + * @author Maxence Lange <maxence@artificial-owl.com> + * @copyright 2018, Maxence Lange <maxence@artificial-owl.com> + * @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 <http://www.gnu.org/licenses/>. + * + */ + +namespace OCA\Social\Service; + + +use OC\Webfinger\Event\WebfingerEvent; +use OCA\Social\Db\CacheActorsRequest; +use OCA\Social\Exceptions\CacheActorDoesNotExistException; +use OCA\Social\Exceptions\SocialAppConfigException; +use OCA\Social\Exceptions\UnauthorizedFediverseException; +use OCA\Social\Model\WebfingerLink; +use OCP\IURLGenerator; + +class WebfingerService { + + + /** @var IURLGenerator */ + private $urlGenerator; + + /** @var CacheActorsRequest */ + private $cacheActorsRequest; + + /** @var CacheActorService */ + private $cacheActorService; + + /** @var FediverseService */ + private $fediverseService; + + /** @var ConfigService */ + private $configService; + + /** @var MiscService */ + private $miscService; + + + /** + * WebfingerService constructor. + * + * @param IURLGenerator $urlGenerator + * @param CacheActorsRequest $cacheActorsRequest + * @param CacheActorService $cacheActorService + * @param FediverseService $fediverseService + * @param ConfigService $configService + * @param MiscService $miscService + */ + public function __construct( + IURLGenerator $urlGenerator, CacheActorsRequest $cacheActorsRequest, + CacheActorService $cacheActorService, FediverseService $fediverseService, + ConfigService $configService, MiscService $miscService + ) { + $this->urlGenerator = $urlGenerator; + $this->cacheActorsRequest = $cacheActorsRequest; + $this->cacheActorService = $cacheActorService; + $this->fediverseService = $fediverseService; + $this->configService = $configService; + $this->miscService = $miscService; + + } + + + /** + * @param WebfingerEvent $event + * + * @throws CacheActorDoesNotExistException + * @throws UnauthorizedFediverseException + * @throws SocialAppConfigException + */ + public function webfinger(WebfingerEvent $event) { + $this->fediverseService->jailed(); + + $subject = $event->getWebfinger() + ->getSubject(); + + if (strpos($subject, 'acct:') === 0) { + $subject = substr($subject, 5); + } + + try { + $actor = $this->cacheActorService->getFromLocalAccount($subject); + } catch (CacheActorDoesNotExistException $e) { + $actor = $this->cacheActorsRequest->getFromId($subject); + if (!$actor->isLocal()) { + throw new CacheActorDoesNotExistException(); + } + } + + $href = $this->configService->getSocialUrl() . '@' . $actor->getPreferredUsername(); + $href = rtrim($href, '/'); + + $linkPerson = new WebfingerLink(); + $linkPerson->setRel('self'); + $linkPerson->setType('application/activity+json'); + $linkPerson->setHref($href); + + $linkOstatus = new WebfingerLink(); + $linkOstatus->setRel('http://ostatus.org/schema/1.0/subscribe'); + $subscribe = $this->urlGenerator->linkToRouteAbsolute('social.OStatus.subscribe') . '?uri={uri}'; + $linkOstatus->setTemplate($subscribe); + + $event->getWebfinger() + ->addLinkSerialized($linkPerson) + ->addLinkSerialized($linkOstatus); + } + +} + |