Signed-off-by: Maxence Lange <maxence@artificial-owl.com>
pull/964/head
Maxence Lange 2020-08-01 00:12:01 -01:00
rodzic 3fb6ed3ef5
commit 58f22f961d
14 zmienionych plików z 461 dodań i 69 usunięć

Wyświetl plik

@ -37,9 +37,10 @@ use OCA\Social\Service\CurlService;
return [
'routes' => [
['name' => 'Navigation#navigate', 'url' => '/', 'verb' => 'GET'],
['name' => 'Navigation#test', 'url' => '/test', 'verb' => 'GET'],
['name' => 'Config#remote', 'url' => '/test/{account}/', 'verb' => 'GET'],
['name' => 'Config#local', 'url' => '/local/', 'verb' => 'GET'],
[
'name' => 'Navigation#timeline', 'url' => '/timeline/{path}', 'verb' => 'GET',
'name' => 'Navigation#timeline', 'url' => '/timeline/{path}', 'verb' => 'GET',
'requirements' => ['path' => '.+'],
'defaults' => ['path' => '']
],
@ -92,8 +93,14 @@ return [
['name' => 'Local#currentFollowing', 'url' => '/api/v1/current/following', 'verb' => 'GET'],
['name' => 'Local#accountInfo', 'url' => '/api/v1/account/{username}/info', 'verb' => 'GET'],
['name' => 'Local#accountFollowers', 'url' => '/api/v1/account/{username}/followers', 'verb' => 'GET'],
['name' => 'Local#accountFollowing', 'url' => '/api/v1/account/{username}/following', 'verb' => 'GET'],
[
'name' => 'Local#accountFollowers', 'url' => '/api/v1/account/{username}/followers',
'verb' => 'GET'
],
[
'name' => 'Local#accountFollowing', 'url' => '/api/v1/account/{username}/following',
'verb' => 'GET'
],
['name' => 'Local#globalAccountInfo', 'url' => '/api/v1/global/account/info', 'verb' => 'GET'],
['name' => 'Local#globalActorInfo', 'url' => '/api/v1/global/actor/info', 'verb' => 'GET'],
@ -101,9 +108,9 @@ return [
['name' => 'Local#globalAccountsSearch', 'url' => '/api/v1/global/accounts/search', 'verb' => 'GET'],
['name' => 'Local#globalTagsSearch', 'url' => '/api/v1/global/tags/search', 'verb' => 'GET'],
// ['name' => 'Local#documentsCache', 'url' => '/api/v1/documents/cache', 'verb' => 'POST'],
// ['name' => 'Local#documentsCache', 'url' => '/api/v1/documents/cache', 'verb' => 'POST'],
['name' => 'Local#search', 'url' => '/api/v1/search', 'verb' => 'GET'],
['name' => 'Local#search', 'url' => '/api/v1/search', 'verb' => 'GET'],
['name' => 'Queue#asyncForRequest', 'url' => CurlService::ASYNC_REQUEST_TOKEN, 'verb' => 'POST'],

Wyświetl plik

@ -41,19 +41,19 @@ use OCA\Social\Interfaces\Activity\AddInterface;
use OCA\Social\Interfaces\Activity\BlockInterface;
use OCA\Social\Interfaces\Activity\CreateInterface;
use OCA\Social\Interfaces\Activity\DeleteInterface;
use OCA\Social\Interfaces\Actor\ServiceInterface;
use OCA\Social\Interfaces\Object\FollowInterface;
use OCA\Social\Interfaces\Object\LikeInterface;
use OCA\Social\Interfaces\Activity\RejectInterface;
use OCA\Social\Interfaces\Activity\RemoveInterface;
use OCA\Social\Interfaces\Activity\UndoInterface;
use OCA\Social\Interfaces\Activity\UpdateInterface;
use OCA\Social\Interfaces\Actor\PersonInterface;
use OCA\Social\Interfaces\Actor\ServiceInterface;
use OCA\Social\Interfaces\IActivityPubInterface;
use OCA\Social\Interfaces\Internal\SocialAppNotificationInterface;
use OCA\Social\Interfaces\Object\DocumentInterface;
use OCA\Social\Interfaces\Object\ImageInterface;
use OCA\Social\Interfaces\Object\AnnounceInterface;
use OCA\Social\Interfaces\Object\DocumentInterface;
use OCA\Social\Interfaces\Object\FollowInterface;
use OCA\Social\Interfaces\Object\ImageInterface;
use OCA\Social\Interfaces\Object\LikeInterface;
use OCA\Social\Interfaces\Object\NoteInterface;
use OCA\Social\Model\ActivityPub\ACore;
use OCA\Social\Model\ActivityPub\Activity\Accept;
@ -61,19 +61,19 @@ use OCA\Social\Model\ActivityPub\Activity\Add;
use OCA\Social\Model\ActivityPub\Activity\Block;
use OCA\Social\Model\ActivityPub\Activity\Create;
use OCA\Social\Model\ActivityPub\Activity\Delete;
use OCA\Social\Model\ActivityPub\Actor\Service;
use OCA\Social\Model\ActivityPub\Object\Follow;
use OCA\Social\Model\ActivityPub\Object\Like;
use OCA\Social\Model\ActivityPub\Activity\Reject;
use OCA\Social\Model\ActivityPub\Activity\Remove;
use OCA\Social\Model\ActivityPub\Activity\Undo;
use OCA\Social\Model\ActivityPub\Activity\Update;
use OCA\Social\Model\ActivityPub\Actor\Person;
use OCA\Social\Model\ActivityPub\Actor\Service;
use OCA\Social\Model\ActivityPub\Internal\SocialAppNotification;
use OCA\Social\Model\ActivityPub\Object\Announce;
use OCA\Social\Model\ActivityPub\Object\Document;
use OCA\Social\Model\ActivityPub\Object\Follow;
use OCA\Social\Model\ActivityPub\Object\Image;
use OCA\Social\Model\ActivityPub\Object\Like;
use OCA\Social\Model\ActivityPub\Object\Note;
use OCA\Social\Model\ActivityPub\Internal\SocialAppNotification;
use OCA\Social\Model\ActivityPub\Object\Tombstone;
use OCA\Social\Model\ActivityPub\Stream;
use OCA\Social\Service\ConfigService;
@ -208,7 +208,7 @@ class AP {
* @throws SocialAppConfigException
* @throws ItemUnknownException
*/
public function getItemFromData(array $data, $parent = null, int $level = 0): ACore {
public function getItemFromData(array $data, ACore $parent = null, int $level = 0): ACore {
if (++$level > self::REDUNDANCY_LIMIT) {
throw new RedundancyLimitException($level);
}

Wyświetl plik

@ -25,20 +25,44 @@ declare(strict_types=1);
namespace OCA\Social\Controller;
use daita\MySmallPhpTools\Model\SimpleDataStore;
use daita\MySmallPhpTools\Traits\Nextcloud\TNCDataResponse;
use Exception;
use OCA\Social\Exceptions\SocialAppConfigException;
use OCA\Social\Service\ConfigService;
use OCA\Social\Service\MiscService;
use OCA\Social\Service\TestService;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\DataResponse;
use OCP\IRequest;
class ConfigController extends Controller {
use TNCDataResponse;
/** @var TestService */
private $testService;
/** @var ConfigService */
private $configService;
public function __construct(string $appName, IRequest $request, ConfigService $configService) {
/** @var MiscService */
private $miscService;
public function __construct(
string $appName, IRequest $request, TestService $testService,
ConfigService $configService, MiscService $miscService
) {
parent::__construct($appName, $request);
$this->testService = $testService;
$this->configService = $configService;
$this->miscService = $miscService;
}
/**
@ -51,4 +75,59 @@ class ConfigController extends Controller {
return new DataResponse([]);
}
/**
* Local Version+Setup Test
*
* @NoCSRFRequired
* @PublicPage
*
* @return DataResponse
*/
public function local(): DataResponse {
$setup = false;
try {
$this->configService->getCloudUrl();
$setup = true;
} catch (SocialAppConfigException $e) {
}
return $this->success(
[
'version' => $this->configService->getAppValue('installed_version'),
'setup' => $setup
]
);
}
/**
* Actor Test
*
* @NoCSRFRequired
* @PublicPage
*
* @param string $account
*
* @return DataResponse
*/
public function remote(string $account): DataResponse {
try {
$this->configService->getCloudUrl();
} catch (SocialAppConfigException $e) {
return $this->success(['error' => 'error on my side: my own Social App is not configured']);
}
$tests = new SimpleDataStore(['account' => $account]);
try {
$this->testService->testWebfinger($tests);
} catch (Exception $e) {
return $this->fail($e, ['result' => $tests], Http::STATUS_OK);
}
return $this->success([$tests]);
}
}

Wyświetl plik

@ -46,7 +46,6 @@ use OCA\Social\Service\ConfigService;
use OCA\Social\Service\DocumentService;
use OCA\Social\Service\MiscService;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\Http\FileDisplayResponse;
use OCP\AppFramework\Http\Response;
use OCP\AppFramework\Http\TemplateResponse;
@ -232,31 +231,6 @@ class NavigationController extends Controller {
return $url;
}
/**
* Display the navigation page of the Social app.
*
* @NoCSRFRequired
* @PublicPage
*
* @return DataResponse
*/
public function test(): DataResponse {
$setup = false;
try {
$this->configService->getCloudUrl();
$setup = true;
} catch (SocialAppConfigException $e) {
}
return $this->success(
[
'version' => $this->configService->getAppValue('installed_version'),
'setup' => $setup
]
);
}
/**
* Display the navigation page of the Social app.

Wyświetl plik

@ -91,7 +91,7 @@ class CacheActorsRequestBuilder extends CoreRequestBuilder {
/** @deprecated */
$this->defaultSelectAlias = 'ca';
$qb->setDefaultSelectAlias('ca');
return $qb;
}
@ -120,7 +120,7 @@ class CacheActorsRequestBuilder extends CoreRequestBuilder {
try {
$result = $qb->getRow([$this, 'parseCacheActorsSelectSql']);
} catch (RowNotFoundException $e) {
throw new CacheActorDoesNotExistException($e->getMessage());
throw new CacheActorDoesNotExistException('Actor is not known');
}
return $result;

Wyświetl plik

@ -308,11 +308,11 @@ class ACore extends Item implements JsonSerializable {
/**
* @param $id
* @param string $id
*
* @throws InvalidOriginException
*/
public function checkOrigin($id) {
public function checkOrigin(string $id) {
$host = parse_url($id, PHP_URL_HOST);
$origin = $this->getRoot()
->getOrigin();

Wyświetl plik

@ -428,6 +428,7 @@ class Person extends ACore implements IQueryRow, JsonSerializable {
/** @var Image $icon */
$icon = AP::$activityPub->getItemFromType(Image::TYPE);
$icon->setParent($this);
$icon->import($this->getArray('icon', $data, []));
if ($icon->getType() === Image::TYPE) {

Wyświetl plik

@ -252,7 +252,8 @@ class Document extends ACore implements JsonSerializable {
if ($this->getId() === '') {
$this->generateUniqueId('/documents/g');
} else {
$this->checkOrigin($this->getId());
// TODO: question if we need this, and why during the import ?
// $this->checkOrigin($this->getId());
}
}

152
lib/Model/Test.php 100644
Wyświetl plik

@ -0,0 +1,152 @@
<?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 daita\MySmallPhpTools\Model\SimpleDataStore;
use daita\MySmallPhpTools\Traits\TArrayTools;
use JsonSerializable;
/**
* Class Test
*
* @package OCA\Social\Model
*/
class Test extends SimpleDataStore implements JsonSerializable {
use TArrayTools;
const SEVERITY_USELESS = 'useless';
const SEVERITY_OPTIONAL = 'optional';
const SEVERITY_MANDATORY = 'mandatory';
/** @var string */
private $name;
/** @var string */
private $severity;
/** @var bool */
private $success = false;
/** @var array */
private $messages = [];
/**
* Test constructor.
*
* @param string $name
* @param string $severity
*/
public function __construct(string $name = '', string $severity = self::SEVERITY_OPTIONAL) {
$this->name = $name;
$this->severity = $severity;
}
/**
* @return string
*/
public function getName(): string {
return $this->name;
}
/**
* @return string
*/
public function getSeverity(): string {
return $this->severity;
}
/**
* @return bool
*/
public function isSuccess(): bool {
return $this->success;
}
/**
* @param bool $success
*
* @return Test
*/
public function setSuccess(bool $success): self {
$this->success = $success;
return $this;
}
/**
* @return array
*/
public function getMessages(): array {
return $this->messages;
}
/**
* @param string $message
*
* @return Test
*/
public function addMessage(string $message): self {
$this->messages[] = $message;
return $this;
}
/**
* @return array
*/
public function jsonSerialize(): array {
$result = array_filter(
[
'name' => $this->getName(),
'severity' => $this->getSeverity(),
'details' => $this->gAll(),
'message' => $this->getMessages()
]
);
$result['success'] = $this->isSuccess();
return $result;
}
}

Wyświetl plik

@ -176,7 +176,7 @@ class CacheActorService {
return $this->cacheActorsRequest->getFromLocalAccount($account);
}
throw new CacheActorDoesNotExistException();
throw new CacheActorDoesNotExistException('Address does is not local');
}
/**

Wyświetl plik

@ -125,8 +125,9 @@ class CurlService {
throw new InvalidResourceException();
}
$protocols = ['https', 'http'];
try {
$path = $this->hostMeta($host);
$path = $this->hostMeta($host, $protocols);
} catch (HostMetaException $e) {
$path = '/.well-known/webfinger';
}
@ -134,7 +135,7 @@ class CurlService {
$request = new Request($path);
$request->addData('resource', 'acct:' . $account);
$request->setAddress($host);
$request->setProtocols(['https', 'http']);
$request->setProtocols($protocols);
$result = $this->retrieveJson($request);
$subject = $this->get('subject', $result, '');
@ -149,25 +150,26 @@ class CurlService {
/**
* @param string $host
* @param array $protocols
*
* @return string
* @throws HostMetaException
*/
public function hostMeta(string &$host): string {
public function hostMeta(string &$host, array &$protocols): string {
$request = new Request('/.well-known/host-meta');
$request->setAddress($host);
$request->setProtocols($protocols);
try {
$result = $this->retrieveJson($request);
} catch (Exception $e) {
$this->miscService->log(
'hostMeta Exception - ' . get_class($e) . ' - ' . $e->getMessage(), 0
);
throw new HostMetaException($e->getMessage());
$this->miscService->log('hostMeta Exception - ' . get_class($e) . ' - ' . $e->getMessage(), 0);
throw new HostMetaException(get_class($e) . ' - ' . $e->getMessage());
}
$url = $this->get('Link.@attributes.template', $result, '');
$host = parse_url($url, PHP_URL_HOST);
$protocols = [parse_url($url, PHP_URL_SCHEME)];
return parse_url($url, PHP_URL_PATH);
}

Wyświetl plik

@ -0,0 +1,188 @@
<?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 daita\MySmallPhpTools\Exceptions\ArrayNotFoundException;
use daita\MySmallPhpTools\Model\Request;
use daita\MySmallPhpTools\Model\SimpleDataStore;
use daita\MySmallPhpTools\Traits\TArrayTools;
use Exception;
use OCA\Social\AP;
use OCA\Social\Exceptions\HostMetaException;
use OCA\Social\Exceptions\InvalidOriginException;
use OCA\Social\Exceptions\InvalidResourceException;
use OCA\Social\Exceptions\ItemUnknownException;
use OCA\Social\Model\ActivityPub\Actor\Person;
use OCA\Social\Model\Test;
/**
* Class TestService
*
* @package OCA\Social\Service
*/
class TestService {
use TArrayTools;
/** @var CurlService */
private $curlService;
/** @var ConfigService */
private $configService;
/** @var MiscService */
private $miscService;
/**
* PostService constructor.
*
* @param CurlService $curlService
* @param ConfigService $configService
* @param MiscService $miscService
*/
public function __construct(
CurlService $curlService, ConfigService $configService, MiscService $miscService
) {
$this->curlService = $curlService;
$this->configService = $configService;
$this->miscService = $miscService;
}
public function testWebfinger(SimpleDataStore $tests) {
$account = ltrim($tests->g('account'), '@');
if (!filter_var($account, FILTER_VALIDATE_EMAIL)) {
throw new InvalidResourceException('account format is not valid');
}
list($username, $host) = explode('@', $account);
if ($username === null || $host === null) {
throw new InvalidResourceException('account format should be valid');
}
$testHostMeta = new Test('host-meta', Test::SEVERITY_OPTIONAL);
$protocols = ['https', 'http'];
try {
$path = $this->curlService->hostMeta($host, $protocols);
$testHostMeta->s('host', $host);
$testHostMeta->s('path', $path);
$testHostMeta->sArray('protocol', $protocols);
$testHostMeta->setSuccess(true);
} catch (HostMetaException $e) {
$testHostMeta->addMessage($e->getMessage());
$path = '/.well-known/webfinger';
}
$tests->aObj('tests', $testHostMeta);
$request = new Request($path);
$request->addData('resource', 'acct:' . $account);
$request->setAddress($host);
$request->setProtocols($protocols);
$testWebfinger = new Test('webfinger', Test::SEVERITY_MANDATORY);
$testWebfinger->sObj('request', $request);
$result = [];
try {
$result = $this->curlService->retrieveJson($request);
$testWebfinger->sArray('result', $result);
$testWebfinger->setSuccess(true);
} catch (Exception $e) {
$testWebfinger->addMessage(get_class($e));
$testWebfinger->addMessage($e->getMessage());
}
$tests->aObj('tests', $testWebfinger);
$testActorLink = new Test('actor-link', Test::SEVERITY_MANDATORY);
try {
$links = $this->getArray('links', $result);
$link = $this->extractArray('rel', 'self', $links);
$testActorLink->aArray('link', $link);
$testActorLink->setSuccess(true);
} catch (ArrayNotFoundException $e) {
$testActorLink->addMessage(get_class($e));
$testActorLink->addMessage('cannot find actor-link in links');
$testActorLink->aArray('links', $links);
}
$tests->aObj('tests', $testActorLink);
$id = $this->get('href', $link, '');
$testActorData = new Test('actor-data', Test::SEVERITY_MANDATORY);
$testActorData->a('id', $id);
$data = [];
try {
$data = $this->curlService->retrieveObject($id);
$testActorData->setSuccess(true);
$testActorData->sArray('data', $data);
} catch (Exception $e) {
$testActorData->addMessage(get_class($e));
$testActorData->addMessage($e->getMessage());
}
$tests->aObj('tests', $testActorData);
$testActor = new Test('actor', Test::SEVERITY_MANDATORY);
try {
/** @var Person $actor */
$actor = AP::$activityPub->getItemFromData($data);
if (!AP::$activityPub->isActor($actor)) {
throw new ItemUnknownException('Actor is not an Actor');
}
if (strtolower($actor->getId()) !== strtolower($id)) {
throw new InvalidOriginException(
'CurlService::retrieveAccount - id: ' . $id . ' - actorId: ' . $actor->getId()
);
}
$testActor->setSuccess(true);
$testActor->sObj('actor', $actor);
} catch (Exception $e) {
echo $e->getTraceAsString();
$testActor->addMessage(get_class($e));
$testActor->addMessage($e->getMessage());
}
$tests->aObj('tests', $testActor);
}
}

Wyświetl plik

@ -49,7 +49,7 @@ try {
exit;
}
header('Content-type: application/xdr+xml');
header('Content-type: application/xrd+xml');
try {
$url = $configService->getCloudUrl(true) . '/.well-known/webfinger?resource={uri}';

Wyświetl plik

@ -39,7 +39,6 @@ use OCP\AppFramework\QueryException;
require_once(__DIR__ . '/../appinfo/autoload.php');
if (!array_key_exists('resource', $_GET)) {
echo 'missing resource';
exit();
@ -75,17 +74,6 @@ try {
try {
$fediverseService->jailed();
if ($configService->getSocialAddress() !== $instance) {
if ($configService->getCloudHost() === $instance) {
$instance = $configService->getSocialAddress();
} else {
throw new Exception(
'instance is ' . $instance . ', expected ' . $configService->getSocialAddress()
. ' or ' . $configService->getCloudHost()
);
}
}
$cacheActorService->getFromLocalAccount($username);
} catch (Exception $e) {
if ($type !== '') {