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-12 22:56:22 +00:00
|
|
|
use OCA\Social\Db\NotesRequest;
|
2018-11-28 15:26:28 +00:00
|
|
|
use OCA\Social\Exceptions\SignatureIsGoneException;
|
2018-11-20 12:47:14 +00:00
|
|
|
use OCA\Social\Exceptions\UnknownItemException;
|
2018-11-12 22:56:22 +00:00
|
|
|
use OCA\Social\Service\ActivityPub\FollowService;
|
|
|
|
use OCA\Social\Service\ActivityService;
|
2018-09-28 11:41:24 +00:00
|
|
|
use OCA\Social\Service\ActorService;
|
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;
|
|
|
|
use OCP\AppFramework\Controller;
|
2018-12-04 10:02:13 +00:00
|
|
|
use OCP\AppFramework\Http\NotFoundResponse;
|
2018-09-28 11:41:24 +00:00
|
|
|
use OCP\AppFramework\Http\Response;
|
|
|
|
use OCP\IRequest;
|
|
|
|
|
|
|
|
|
|
|
|
class ActivityPubController extends Controller {
|
|
|
|
|
|
|
|
|
|
|
|
use TNCDataResponse;
|
|
|
|
|
|
|
|
|
|
|
|
/** @var SocialPubController */
|
|
|
|
private $socialPubController;
|
|
|
|
|
2018-11-12 22:56:22 +00:00
|
|
|
/** @var ActivityService */
|
|
|
|
private $activityService;
|
|
|
|
|
|
|
|
/** @var ImportService */
|
|
|
|
private $importService;
|
|
|
|
|
|
|
|
/** @var FollowService */
|
|
|
|
private $followService;
|
2018-09-28 11:41:24 +00:00
|
|
|
|
|
|
|
/** @var ActorService */
|
|
|
|
private $actorService;
|
|
|
|
|
2018-11-12 22:56:22 +00:00
|
|
|
/** @var NotesRequest */
|
|
|
|
private $notesRequest;
|
|
|
|
|
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-11-12 22:56:22 +00:00
|
|
|
* @param ActivityService $activityService
|
|
|
|
* @param ImportService $importService
|
|
|
|
* @param FollowService $followService
|
2018-09-28 11:41:24 +00:00
|
|
|
* @param ActorService $actorService
|
2018-11-12 22:56:22 +00:00
|
|
|
* @param NotesRequest $notesRequest
|
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,
|
|
|
|
ActivityService $activityService, ImportService $importService,
|
|
|
|
FollowService $followService, ActorService $actorService, NotesRequest $notesRequest,
|
2018-09-28 11:41:24 +00:00
|
|
|
MiscService $miscService
|
|
|
|
) {
|
|
|
|
parent::__construct(Application::APP_NAME, $request);
|
|
|
|
|
|
|
|
$this->socialPubController = $socialPubController;
|
2018-11-12 22:56:22 +00:00
|
|
|
|
|
|
|
$this->activityService = $activityService;
|
|
|
|
$this->importService = $importService;
|
|
|
|
$this->followService = $followService;
|
2018-09-28 11:41:24 +00:00
|
|
|
$this->actorService = $actorService;
|
2018-11-12 22:56:22 +00:00
|
|
|
$this->notesRequest = $notesRequest;
|
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-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 {
|
|
|
|
$actor = $this->actorService->getActor($username);
|
2018-11-20 12:47:14 +00:00
|
|
|
|
2018-11-19 10:34:54 +00:00
|
|
|
// $actor->setTopLevel(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-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-03 10:09:19 +00:00
|
|
|
$this->miscService->log('Shared Inbox: ' . $body, 0);
|
|
|
|
|
|
|
|
$origin = $this->activityService->checkRequest($this->request);
|
2018-11-20 12:47:14 +00:00
|
|
|
|
2018-12-03 10:09:19 +00:00
|
|
|
$activity = $this->importService->importFromJson($body);
|
|
|
|
$activity->setOrigin($origin);
|
2018-11-20 12:47:14 +00:00
|
|
|
try {
|
2018-12-03 10:09:19 +00:00
|
|
|
$this->importService->parseIncomingRequest($activity);
|
2018-11-20 12:47:14 +00:00
|
|
|
} catch (UnknownItemException $e) {
|
|
|
|
}
|
|
|
|
|
|
|
|
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-03 10:09:19 +00:00
|
|
|
$this->miscService->log('Inbox: ' . $body, 0);
|
|
|
|
|
|
|
|
$origin = $this->activityService->checkRequest($this->request);
|
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);
|
|
|
|
$activity->setOrigin($origin);
|
2018-11-12 22:56:22 +00:00
|
|
|
try {
|
2018-12-03 10:09:19 +00:00
|
|
|
$this->importService->parseIncomingRequest($activity);
|
2018-11-20 12:47:14 +00:00
|
|
|
} catch (UnknownItemException $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-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 {
|
|
|
|
$actor = $this->actorService->getActor($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-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-09-28 11:41:24 +00:00
|
|
|
|
2018-10-12 06:44:35 +00:00
|
|
|
// uncomment this line to display the result that would be return to an ActivityPub service (TEST)
|
2018-11-21 11:45:17 +00:00
|
|
|
// return true;
|
2018-10-01 12:14:23 +00:00
|
|
|
|
2018-09-28 11:41:24 +00:00
|
|
|
if ($this->request->getHeader('Accept')
|
|
|
|
=== 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"') {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|