2018-09-28 11:41:24 +00:00
|
|
|
<?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\Controller;
|
|
|
|
|
|
|
|
|
2018-11-16 10:47:59 +00:00
|
|
|
use daita\MySmallPhpTools\Traits\Nextcloud\TNCDataResponse;
|
2018-09-28 11:41:24 +00:00
|
|
|
use Exception;
|
2018-11-28 15:26:28 +00:00
|
|
|
use OC\AppFramework\Http;
|
2018-09-28 11:41:24 +00:00
|
|
|
use OCA\Social\AppInfo\Application;
|
2018-11-28 15:26:28 +00:00
|
|
|
use OCA\Social\Exceptions\SignatureIsGoneException;
|
2018-12-29 14:07:57 +00:00
|
|
|
use OCA\Social\Exceptions\ItemUnknownException;
|
|
|
|
use OCA\Social\Exceptions\UrlCloudException;
|
2018-12-17 09:12:27 +00:00
|
|
|
use OCA\Social\Service\CacheActorService;
|
|
|
|
use OCA\Social\Service\FollowService;
|
2018-11-12 22:56:22 +00:00
|
|
|
use OCA\Social\Service\ImportService;
|
2018-09-28 11:41:24 +00:00
|
|
|
use OCA\Social\Service\MiscService;
|
2018-12-17 09:12:27 +00:00
|
|
|
use OCA\Social\Service\SignatureService;
|
2018-09-28 11:41:24 +00:00
|
|
|
use OCP\AppFramework\Controller;
|
|
|
|
use OCP\AppFramework\Http\Response;
|
|
|
|
use OCP\IRequest;
|
|
|
|
|
|
|
|
|
|
|
|
class ActivityPubController extends Controller {
|
|
|
|
|
|
|
|
|
|
|
|
use TNCDataResponse;
|
|
|
|
|
|
|
|
|
|
|
|
/** @var SocialPubController */
|
|
|
|
private $socialPubController;
|
|
|
|
|
2018-12-17 09:12:27 +00:00
|
|
|
/** @var CacheActorService */
|
|
|
|
private $cacheActorService;
|
|
|
|
|
|
|
|
/** @var SignatureService */
|
|
|
|
private $signatureService;
|
2018-11-12 22:56:22 +00:00
|
|
|
|
|
|
|
/** @var ImportService */
|
|
|
|
private $importService;
|
|
|
|
|
|
|
|
/** @var FollowService */
|
|
|
|
private $followService;
|
2018-09-28 11:41:24 +00:00
|
|
|
|
|
|
|
/** @var MiscService */
|
|
|
|
private $miscService;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ActivityPubController constructor.
|
|
|
|
*
|
2018-11-12 22:56:22 +00:00
|
|
|
* @param IRequest $request
|
2018-09-28 11:41:24 +00:00
|
|
|
* @param SocialPubController $socialPubController
|
2018-12-17 09:12:27 +00:00
|
|
|
* @param CacheActorService $cacheActorService
|
|
|
|
* @param SignatureService $signatureService
|
2018-11-12 22:56:22 +00:00
|
|
|
* @param ImportService $importService
|
|
|
|
* @param FollowService $followService
|
2018-09-28 11:41:24 +00:00
|
|
|
* @param MiscService $miscService
|
|
|
|
*/
|
|
|
|
public function __construct(
|
2018-11-12 22:56:22 +00:00
|
|
|
IRequest $request, SocialPubController $socialPubController,
|
2018-12-17 09:12:27 +00:00
|
|
|
CacheActorService $cacheActorService, SignatureService $signatureService,
|
|
|
|
ImportService $importService, FollowService $followService, MiscService $miscService
|
2018-09-28 11:41:24 +00:00
|
|
|
) {
|
|
|
|
parent::__construct(Application::APP_NAME, $request);
|
|
|
|
|
|
|
|
$this->socialPubController = $socialPubController;
|
2018-12-17 09:12:27 +00:00
|
|
|
$this->cacheActorService = $cacheActorService;
|
|
|
|
$this->signatureService = $signatureService;
|
2018-11-12 22:56:22 +00:00
|
|
|
$this->importService = $importService;
|
|
|
|
$this->followService = $followService;
|
2018-09-28 11:41:24 +00:00
|
|
|
$this->miscService = $miscService;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2018-10-01 12:14:23 +00:00
|
|
|
* returns information about an Actor, based on the username.
|
|
|
|
*
|
|
|
|
* This method should be called when a remote ActivityPub server require information
|
|
|
|
* about a local Social account
|
|
|
|
*
|
|
|
|
* The format is pure Json
|
|
|
|
*
|
2018-09-28 11:41:24 +00:00
|
|
|
* @NoCSRFRequired
|
|
|
|
* @PublicPage
|
|
|
|
*
|
|
|
|
* @param string $username
|
|
|
|
*
|
|
|
|
* @return Response
|
2018-12-29 14:07:57 +00:00
|
|
|
* @throws UrlCloudException
|
2018-09-28 11:41:24 +00:00
|
|
|
*/
|
2018-10-01 12:14:23 +00:00
|
|
|
public function actor(string $username): Response {
|
2018-09-28 11:41:24 +00:00
|
|
|
if (!$this->checkSourceActivityStreams()) {
|
2018-11-29 17:41:55 +00:00
|
|
|
return $this->socialPubController->actor($username);
|
2018-09-28 11:41:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2018-12-17 09:12:27 +00:00
|
|
|
$actor = $this->cacheActorService->getFromLocalAccount($username);
|
2019-01-15 10:35:06 +00:00
|
|
|
$actor->setDisplayW3ContextSecurity(true);
|
2018-09-28 11:41:24 +00:00
|
|
|
|
|
|
|
return $this->directSuccess($actor);
|
|
|
|
} catch (Exception $e) {
|
2018-12-04 10:02:13 +00:00
|
|
|
http_response_code(404);
|
|
|
|
exit();
|
2018-09-28 11:41:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-12 22:56:22 +00:00
|
|
|
|
2018-09-28 11:41:24 +00:00
|
|
|
/**
|
2018-10-01 12:14:23 +00:00
|
|
|
* Alias to the actor() method.
|
|
|
|
*
|
|
|
|
* Normal path is /apps/social/users/username
|
|
|
|
* This alias is /apps/social/@username
|
|
|
|
*
|
2018-09-28 11:41:24 +00:00
|
|
|
* @NoCSRFRequired
|
|
|
|
* @PublicPage
|
|
|
|
*
|
|
|
|
* @param string $username
|
|
|
|
*
|
|
|
|
* @return Response
|
2018-12-29 14:07:57 +00:00
|
|
|
* @throws UrlCloudException
|
2018-09-28 11:41:24 +00:00
|
|
|
*/
|
2018-11-12 22:56:22 +00:00
|
|
|
public function actorAlias(string $username): Response {
|
2018-09-28 11:41:24 +00:00
|
|
|
return $this->actor($username);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2018-10-01 12:14:23 +00:00
|
|
|
* Shared inbox. does nothing.
|
|
|
|
*
|
2018-09-28 11:41:24 +00:00
|
|
|
* @NoCSRFRequired
|
|
|
|
* @PublicPage
|
|
|
|
*
|
|
|
|
* @return Response
|
|
|
|
*/
|
2018-10-01 12:14:23 +00:00
|
|
|
public function sharedInbox(): Response {
|
2018-11-20 12:47:14 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
$body = file_get_contents('php://input');
|
2018-12-04 23:46:04 +00:00
|
|
|
$this->miscService->log('[<<] shared-inbox: ' . $body, 1);
|
2018-12-03 10:09:19 +00:00
|
|
|
|
2018-12-24 10:32:31 +00:00
|
|
|
$requestTime = 0;
|
|
|
|
$origin = $this->signatureService->checkRequest($this->request, $requestTime);
|
2018-11-20 12:47:14 +00:00
|
|
|
|
2018-12-03 10:09:19 +00:00
|
|
|
$activity = $this->importService->importFromJson($body);
|
2018-12-19 01:14:24 +00:00
|
|
|
if (!$this->signatureService->checkObject($activity)) {
|
2018-12-24 10:32:31 +00:00
|
|
|
$activity->setOrigin($origin, SignatureService::ORIGIN_HEADER, $requestTime);
|
2018-12-19 01:13:33 +00:00
|
|
|
}
|
2018-12-19 17:50:17 +00:00
|
|
|
|
2018-11-20 12:47:14 +00:00
|
|
|
try {
|
2018-12-03 10:09:19 +00:00
|
|
|
$this->importService->parseIncomingRequest($activity);
|
2018-12-29 14:07:57 +00:00
|
|
|
} catch (ItemUnknownException $e) {
|
2018-11-20 12:47:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this->success([]);
|
2018-11-28 15:26:28 +00:00
|
|
|
} catch (SignatureIsGoneException $e) {
|
2018-12-04 10:13:49 +00:00
|
|
|
return $this->fail($e, [], Http::STATUS_GONE, false);
|
2018-11-20 12:47:14 +00:00
|
|
|
} catch (Exception $e) {
|
2018-11-20 22:38:55 +00:00
|
|
|
return $this->fail($e);
|
2018-11-20 12:47:14 +00:00
|
|
|
}
|
2018-09-28 11:41:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2018-10-01 12:14:23 +00:00
|
|
|
* Method is called when a remote ActivityPub server wants to POST in the INBOX of a USER
|
|
|
|
* Checking that the user exists, and that the header is properly signed.
|
|
|
|
*
|
2018-09-28 11:41:24 +00:00
|
|
|
* @NoCSRFRequired
|
|
|
|
* @PublicPage
|
|
|
|
*
|
2018-11-12 22:56:22 +00:00
|
|
|
* @param string $username
|
2018-10-01 12:14:23 +00:00
|
|
|
*
|
2018-09-28 11:41:24 +00:00
|
|
|
* @return Response
|
|
|
|
*/
|
2018-10-01 12:14:23 +00:00
|
|
|
public function inbox(string $username): Response {
|
2018-09-28 11:41:24 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
$body = file_get_contents('php://input');
|
2018-12-04 23:46:04 +00:00
|
|
|
$this->miscService->log('[<<] inbox: ' . $body, 1);
|
2018-12-03 10:09:19 +00:00
|
|
|
|
2018-12-24 10:32:31 +00:00
|
|
|
$requestTime = 0;
|
|
|
|
$origin = $this->signatureService->checkRequest($this->request, $requestTime);
|
2018-10-01 12:14:23 +00:00
|
|
|
|
2018-11-28 15:39:25 +00:00
|
|
|
// TODO - check the recipient <-> username
|
|
|
|
// $actor = $this->actorService->getActor($username);
|
|
|
|
|
2018-12-03 10:09:19 +00:00
|
|
|
$activity = $this->importService->importFromJson($body);
|
2018-12-19 01:14:24 +00:00
|
|
|
if (!$this->signatureService->checkObject($activity)) {
|
2018-12-24 10:32:31 +00:00
|
|
|
$activity->setOrigin($origin, SignatureService::ORIGIN_HEADER, $requestTime);
|
2018-12-19 01:13:33 +00:00
|
|
|
}
|
|
|
|
|
2018-11-12 22:56:22 +00:00
|
|
|
try {
|
2018-12-03 10:09:19 +00:00
|
|
|
$this->importService->parseIncomingRequest($activity);
|
2018-12-29 14:07:57 +00:00
|
|
|
} catch (ItemUnknownException $e) {
|
2018-11-12 22:56:22 +00:00
|
|
|
}
|
|
|
|
|
2018-09-28 11:41:24 +00:00
|
|
|
return $this->success([]);
|
2018-11-28 15:26:28 +00:00
|
|
|
} catch (SignatureIsGoneException $e) {
|
|
|
|
return $this->fail($e, [], Http::STATUS_GONE);
|
2018-09-28 11:41:24 +00:00
|
|
|
} catch (Exception $e) {
|
2018-11-20 22:38:55 +00:00
|
|
|
return $this->fail($e);
|
2018-09-28 11:41:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2018-10-01 12:14:23 +00:00
|
|
|
* Outbox. does nothing.
|
|
|
|
*
|
2018-09-28 11:41:24 +00:00
|
|
|
* @NoCSRFRequired
|
|
|
|
* @PublicPage
|
|
|
|
*
|
|
|
|
* @param string $username
|
|
|
|
*
|
|
|
|
* @return Response
|
|
|
|
*/
|
2018-10-01 12:14:23 +00:00
|
|
|
public function outbox(string $username): Response {
|
2018-09-28 11:41:24 +00:00
|
|
|
return $this->success([$username]);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2018-10-01 12:14:23 +00:00
|
|
|
* followers. does nothing.
|
|
|
|
*
|
2018-09-28 11:41:24 +00:00
|
|
|
* @NoCSRFRequired
|
|
|
|
* @PublicPage
|
|
|
|
*
|
|
|
|
* @param string $username
|
|
|
|
*
|
|
|
|
* @return Response
|
2018-12-29 14:07:57 +00:00
|
|
|
* @throws UrlCloudException
|
2018-09-28 11:41:24 +00:00
|
|
|
*/
|
2018-11-30 11:21:11 +00:00
|
|
|
public function followers(string $username): Response {
|
2018-11-12 22:56:22 +00:00
|
|
|
|
2018-09-28 11:41:24 +00:00
|
|
|
if (!$this->checkSourceActivityStreams()) {
|
|
|
|
return $this->socialPubController->followers($username);
|
|
|
|
}
|
|
|
|
|
2018-11-12 22:56:22 +00:00
|
|
|
try {
|
2018-12-17 09:12:27 +00:00
|
|
|
$actor = $this->cacheActorService->getFromLocalAccount($username);
|
2018-11-30 21:31:32 +00:00
|
|
|
$followers = $this->followService->getFollowersCollection($actor);
|
2018-11-20 12:47:14 +00:00
|
|
|
|
2018-11-19 10:34:54 +00:00
|
|
|
// $followers->setTopLevel(true);
|
2018-11-12 22:56:22 +00:00
|
|
|
|
|
|
|
return $this->directSuccess($followers);
|
|
|
|
} catch (Exception $e) {
|
2018-11-20 22:38:55 +00:00
|
|
|
return $this->fail($e);
|
2018-11-12 22:56:22 +00:00
|
|
|
}
|
2018-09-28 11:41:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2018-10-01 12:14:23 +00:00
|
|
|
* following. does nothing.
|
|
|
|
*
|
2018-09-28 11:41:24 +00:00
|
|
|
* @NoCSRFRequired
|
|
|
|
* @PublicPage
|
|
|
|
*
|
|
|
|
* @param string $username
|
|
|
|
*
|
|
|
|
* @return Response
|
2018-12-29 14:07:57 +00:00
|
|
|
* @throws UrlCloudException
|
2018-09-28 11:41:24 +00:00
|
|
|
*/
|
2018-10-01 12:14:23 +00:00
|
|
|
public function following(string $username): Response {
|
2018-09-28 11:41:24 +00:00
|
|
|
if (!$this->checkSourceActivityStreams()) {
|
|
|
|
return $this->socialPubController->following($username);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->success([$username]);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2018-10-01 12:14:23 +00:00
|
|
|
* should return data about a post. do nothing.
|
|
|
|
*
|
2018-09-28 11:41:24 +00:00
|
|
|
* @NoCSRFRequired
|
|
|
|
* @PublicPage
|
|
|
|
*
|
|
|
|
* @param string $username
|
|
|
|
* @param $postId
|
|
|
|
*
|
|
|
|
* @return Response
|
|
|
|
*/
|
|
|
|
public function displayPost($username, $postId) {
|
2018-10-01 12:14:23 +00:00
|
|
|
if (!$this->checkSourceActivityStreams()) {
|
|
|
|
return $this->socialPubController->displayPost($username, $postId);
|
|
|
|
}
|
|
|
|
|
2018-09-28 11:41:24 +00:00
|
|
|
return $this->success([$username, $postId]);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2018-10-01 12:14:23 +00:00
|
|
|
* Check that the request comes from an ActivityPub server, based on the header.
|
|
|
|
*
|
|
|
|
* If not, should forward to a readable webpage that displays content for navigation.
|
2018-09-28 11:41:24 +00:00
|
|
|
*
|
2018-10-01 12:14:23 +00:00
|
|
|
* @return bool
|
2018-09-28 11:41:24 +00:00
|
|
|
*/
|
2018-10-01 12:14:23 +00:00
|
|
|
private function checkSourceActivityStreams(): bool {
|
2018-12-31 10:34:53 +00:00
|
|
|
$accepted = [
|
|
|
|
'application/ld+json',
|
|
|
|
'application/activity+json'
|
|
|
|
];
|
2018-09-28 11:41:24 +00:00
|
|
|
|
2018-12-31 10:34:53 +00:00
|
|
|
$accepts = explode(',', $this->request->getHeader('Accept'));
|
|
|
|
$accepts = array_map([$this, 'trimHeader'], $accepts);
|
2018-10-01 12:14:23 +00:00
|
|
|
|
2018-12-31 10:34:53 +00:00
|
|
|
foreach ($accepts as $accept) {
|
|
|
|
if (in_array($accept, $accepted)) {
|
|
|
|
return true;
|
|
|
|
}
|
2018-12-04 23:50:36 +00:00
|
|
|
}
|
|
|
|
|
2018-12-31 10:34:53 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private function trimHeader(string $header) {
|
|
|
|
$header = trim($header);
|
|
|
|
|
|
|
|
$pos = strpos($header, ';');
|
|
|
|
if ($pos === false) {
|
|
|
|
return $header;
|
2018-09-28 11:41:24 +00:00
|
|
|
}
|
|
|
|
|
2018-12-31 10:34:53 +00:00
|
|
|
return substr($header, 0, $pos);
|
2018-09-28 11:41:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|