kopia lustrzana https://github.com/nextcloud/social
				
				
				
			using IWebfingerManager
Signed-off-by: Maxence Lange <maxence@artificial-owl.com>pull/954/head
							rodzic
							
								
									8043004d62
								
							
						
					
					
						commit
						888d993141
					
				| 
						 | 
				
			
			@ -37,5 +37,6 @@ require_once __DIR__ . '/autoload.php';
 | 
			
		|||
$app = \OC::$server->query(Application::class);
 | 
			
		||||
 | 
			
		||||
$app->checkUpgradeStatus();
 | 
			
		||||
$app->registerWebfinger();
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
| 
						 | 
				
			
			@ -77,6 +81,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) {
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
		);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 *
 | 
			
		||||
	 */
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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();
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -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()) {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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 === ''
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
		Ładowanie…
	
		Reference in New Issue