diff --git a/appinfo/routes.php b/appinfo/routes.php index fe1daf03..303816d6 100644 --- a/appinfo/routes.php +++ b/appinfo/routes.php @@ -68,16 +68,19 @@ return [ ['name' => 'OStatus#getLink', 'url' => '/api/v1/ostatus/link/{local}/{account}', 'verb' => 'GET'], // OAuth + ['name' => 'OAuth#nodeinfo', 'url' => '/.well-known/nodeinfo', 'verb' => 'GET'], + ['name' => 'OAuth#nodeinfo2', 'url' => '/nodeinfo/2.0', 'verb' => 'GET'], ['name' => 'OAuth#apps', 'url' => '/api/v1/apps', 'verb' => 'POST'], ['name' => 'OAuth#authorize', 'url' => '/oauth/authorize', 'verb' => 'GET'], ['name' => 'OAuth#token', 'url' => '/oauth/token', 'verb' => 'POST'], ['name' => 'OAuth#appsCredentials', 'url' => '/api/v1/apps/verify_credentials', 'verb' => 'GET'], - [ - 'name' => 'OAuth#accountsCredentials', 'url' => '/api/v1/accounts/verify_credentials', - 'verb' => 'GET' - ], // Api for 3rd party + [ + 'name' => 'Api#verifyCredentials', 'url' => '/api/v1/accounts/verify_credentials', + 'verb' => 'GET' + ], + ['name' => 'Api#instance', 'url' => '/api/v1/instance/', 'verb' => 'GET'], ['name' => 'Api#timelines', 'url' => '/api/v1/timelines/{timeline}/', 'verb' => 'GET'], // Api for local front-end diff --git a/lib/Controller/ApiController.php b/lib/Controller/ApiController.php index d27febc2..570d4c24 100644 --- a/lib/Controller/ApiController.php +++ b/lib/Controller/ApiController.php @@ -35,6 +35,8 @@ use Exception; use OCA\Social\AppInfo\Application; use OCA\Social\Exceptions\AccountDoesNotExistException; use OCA\Social\Exceptions\ClientAuthDoesNotExistException; +use OCA\Social\Exceptions\InstanceDoesNotExistException; +use OCA\Social\Model\ActivityPub\ACore; use OCA\Social\Model\ActivityPub\Actor\Person; use OCA\Social\Model\ActivityPub\Stream; use OCA\Social\Service\AccountService; @@ -42,6 +44,7 @@ use OCA\Social\Service\CacheActorService; use OCA\Social\Service\ClientService; use OCA\Social\Service\ConfigService; use OCA\Social\Service\FollowService; +use OCA\Social\Service\InstanceService; use OCA\Social\Service\MiscService; use OCA\Social\Service\StreamService; use OCP\AppFramework\Controller; @@ -60,6 +63,9 @@ class ApiController extends Controller { /** @var IUserSession */ private $userSession; + /** @var InstanceService */ + private $instanceService; + /** @var ClientService */ private $clientService; @@ -94,6 +100,7 @@ class ApiController extends Controller { * * @param IRequest $request * @param IUserSession $userSession + * @param InstanceService $instanceService * @param ClientService $clientService * @param AccountService $accountService * @param CacheActorService $cacheActorService @@ -103,14 +110,15 @@ class ApiController extends Controller { * @param MiscService $miscService */ public function __construct( - IRequest $request, IUserSession $userSession, ClientService $clientService, - AccountService $accountService, - CacheActorService $cacheActorService, FollowService $followService, StreamService $streamService, - ConfigService $configService, MiscService $miscService + IRequest $request, IUserSession $userSession, InstanceService $instanceService, + ClientService $clientService, AccountService $accountService, CacheActorService $cacheActorService, + FollowService $followService, StreamService $streamService, ConfigService $configService, + MiscService $miscService ) { parent::__construct(Application::APP_NAME, $request); $this->userSession = $userSession; + $this->instanceService = $instanceService; $this->clientService = $clientService; $this->accountService = $accountService; $this->cacheActorService = $cacheActorService; @@ -120,9 +128,12 @@ class ApiController extends Controller { $this->miscService = $miscService; $authHeader = trim($this->request->getHeader('Authorization')); - list($authType, $authToken) = explode(' ', $authHeader); - if (strtolower($authType) === 'bearer') { - $this->bearer = $authToken; + + if (strpos($authHeader, ' ')) { + list($authType, $authToken) = explode(' ', $authHeader); + if (strtolower($authType) === 'bearer') { + $this->bearer = $authToken; + } } } @@ -132,6 +143,37 @@ class ApiController extends Controller { * @NoAdminRequired * @PublicPage * + * @return DataResponse + */ + public function verifyCredentials() { + try { + $this->initViewer(true); + + return new DataResponse($this->viewer, Http::STATUS_OK); + } catch (Exception $e) { + return $this->fail($e); + } + } + + + /** + * @NoCSRFRequired + * @PublicPage + * + * @return DataResponse + * @throws InstanceDoesNotExistException + */ + public function instance(): DataResponse { + $local = $this->instanceService->getLocal(Stream::FORMAT_LOCAL); + + return new DataResponse($local, Http::STATUS_OK); + } + + + /** + * @NoCSRFRequired + * @PublicPage + * * @param string $timeline * @param int $limit * @@ -154,38 +196,36 @@ class ApiController extends Controller { * @param bool $exception * * @return bool - * @throws AccountDoesNotExistException + * @throws Exception */ private function initViewer(bool $exception = false): bool { - $userId = $this->currentSession($exception); - try { - $this->viewer = $this->accountService->getActorFromUserId($userId); + $userId = $this->currentSession(); + + $account = $this->accountService->getActorFromUserId($userId); + $this->viewer = $this->cacheActorService->getFromLocalAccount($account->getPreferredUsername()); + $this->viewer->setExportFormat(ACore::FORMAT_LOCAL); $this->streamService->setViewer($this->viewer); $this->followService->setViewer($this->viewer); $this->cacheActorService->setViewer($this->viewer); + + return true; } catch (Exception $e) { if ($exception) { - throw new AccountDoesNotExistException( - 'unable to initViewer - ' . get_class($e) . ' - ' . $e->getMessage() - ); + throw $e; } - - return false; } - return true; + return false; } /** - * @param bool $exception - * * @return string * @throws AccountDoesNotExistException */ - private function currentSession(bool $exception): string { + private function currentSession(): string { $user = $this->userSession->getUser(); if ($user !== null) { return $user->getUID(); diff --git a/lib/Controller/OAuthController.php b/lib/Controller/OAuthController.php index 89fba1b0..d19d6f4d 100644 --- a/lib/Controller/OAuthController.php +++ b/lib/Controller/OAuthController.php @@ -38,6 +38,7 @@ use OCA\Social\Exceptions\ActorDoesNotExistException; use OCA\Social\Exceptions\ClientAppDoesNotExistException; use OCA\Social\Exceptions\ClientAuthDoesNotExistException; use OCA\Social\Exceptions\ClientException; +use OCA\Social\Exceptions\InstanceDoesNotExistException; use OCA\Social\Exceptions\ItemAlreadyExistsException; use OCA\Social\Exceptions\SocialAppConfigException; use OCA\Social\Exceptions\UrlCloudException; @@ -48,12 +49,14 @@ use OCA\Social\Service\AccountService; use OCA\Social\Service\CacheActorService; use OCA\Social\Service\ClientService; use OCA\Social\Service\ConfigService; +use OCA\Social\Service\InstanceService; use OCA\Social\Service\MiscService; use OCP\AppFramework\Controller; use OCP\AppFramework\Http; use OCP\AppFramework\Http\DataResponse; use OCP\AppFramework\Http\Response; use OCP\IRequest; +use OCP\IURLGenerator; use OCP\IUserSession; @@ -66,6 +69,12 @@ class OAuthController extends Controller { /** @var IUserSession */ private $userSession; + /** @var IURLGenerator */ + private $urlGenerator; + + /** @var InstanceService */ + private $instanceService; + /** @var AccountService */ private $accountService; @@ -87,6 +96,8 @@ class OAuthController extends Controller { * * @param IRequest $request * @param IUserSession $userSession + * @param IURLGenerator $urlGenerator + * @param InstanceService $instanceService * @param AccountService $accountService * @param CacheActorService $cacheActorService * @param ClientService $clientService @@ -94,13 +105,16 @@ class OAuthController extends Controller { * @param MiscService $miscService */ public function __construct( - IRequest $request, IUserSession $userSession, AccountService $accountService, + IRequest $request, IUserSession $userSession, IURLGenerator $urlGenerator, + InstanceService $instanceService, AccountService $accountService, CacheActorService $cacheActorService, ClientService $clientService, ConfigService $configService, MiscService $miscService ) { parent::__construct(Application::APP_NAME, $request); $this->userSession = $userSession; + $this->urlGenerator = $urlGenerator; + $this->instanceService = $instanceService; $this->accountService = $accountService; $this->cacheActorService = $cacheActorService; $this->clientService = $clientService; @@ -108,7 +122,63 @@ class OAuthController extends Controller { $this->miscService = $miscService; $body = file_get_contents('php://input'); - $this->miscService->log('[ClientService] input: ' . $body, 1); + $this->miscService->log('[OAuthController] input: ' . $body, 2); + } + + + /** + * @NoCSRFRequired + * @PublicPage + * + * @return Response + */ + public function nodeinfo(): Response { + $nodeInfo = [ + 'links' => [ + 'rel' => 'http://nodeinfo.diaspora.software/ns/schema/2.0', + 'href' => $this->urlGenerator->linkToRouteAbsolute('social.OAuth.nodeinfo2') + ] + ]; + + return new DataResponse($nodeInfo, Http::STATUS_OK); + } + + + /** + * @NoCSRFRequired + * @PublicPage + * + * @return Response + */ + public function nodeinfo2() { + try { + $local = $this->instanceService->getLocal(); + $name = $local->getTitle(); + + $version = $local->getVersion(); + $usage = $local->getUsage(); + $openReg = $local->isRegistrations(); + } catch (InstanceDoesNotExistException $e) { + $name = 'Nextcloud Social'; + $version = $this->configService->getAppValue('installed_version'); + $usage = []; + $openReg = false; + } + + $nodeInfo = [ + "version" => "2.0", + "software" => [ + "name" => $name, + "version" => $version + ], + "protocols" => [ + "activitypub" + ], + "usage" => $usage, + "openRegistrations" => $openReg + ]; + + return new DataResponse($nodeInfo, Http::STATUS_OK); } @@ -125,7 +195,7 @@ class OAuthController extends Controller { * @throws ClientException */ public function apps( - string $client_name, string $redirect_uris, string $website = '', string $scopes = 'read' + string $client_name = '', string $redirect_uris = '', string $website = '', string $scopes = 'read' ): Response { // TODO: manage array from request if (!is_array($redirect_uris)) { @@ -288,51 +358,6 @@ class OAuthController extends Controller { ); } - - /** - * @NoCSRFRequired - * @NoAdminRequired - * @PublicPage - * - * @return DataResponse - */ - public function accountsCredentials() { - return new DataResponse( - [ - "id" => "137148", - "username" => "cult", - "acct" => "cult", - "display_name" => "Maxence Lange", - "locked" => false, - "bot" => false, - "discoverable" => null, - "group" => false, - "created_at" => "2017-05-11T09=>20=>28.055Z", - "note" => "\u003cp\u003e\u003c/p\u003e", - "url" => "https://test.artificial-owl.com/index.php/apps/social/@cult", - "avatar" => "https://mastodon.social/avatars/original/missing.png", - "avatar_static" => "https://mastodon.social/avatars/original/missing.png", - "header" => "https://mastodon.social/headers/original/missing.png", - "header_static" => "https://mastodon.social/headers/original/missing.png", - "followers_count" => 9, - "following_count" => 5, - "statuses_count" => 13, - "last_status_at" => "2019-09-15", - "source" => [ - "privacy" => "public", - "sensitive" => false, - "language" => null, - "note" => "", - "fields" => [], - "follow_requests_count" => 0 - ], - "emojis" => [], - "fields" => [] - ], 200 - ); - } - - } diff --git a/lib/Db/CacheActorsRequest.php b/lib/Db/CacheActorsRequest.php index 3ccc6739..201e49ef 100644 --- a/lib/Db/CacheActorsRequest.php +++ b/lib/Db/CacheActorsRequest.php @@ -170,7 +170,7 @@ class CacheActorsRequest extends CacheActorsRequestBuilder { public function getFromId(string $id): Person { $qb = $this->getCacheActorsSelectSql(); $qb->limitToIdString($id); - $this->leftJoinCacheDocuments($qb, 'icon_id'); + $qb->leftJoinCacheDocuments('icon_id'); return $this->getCacheActorFromRequest($qb); } @@ -187,7 +187,7 @@ class CacheActorsRequest extends CacheActorsRequestBuilder { public function getFromAccount(string $account): Person { $qb = $this->getCacheActorsSelectSql(); $qb->limitToAccount($account); - $this->leftJoinCacheDocuments($qb, 'icon_id'); + $qb->leftJoinCacheDocuments('icon_id'); $this->leftJoinDetails($qb); return $this->getCacheActorFromRequest($qb); @@ -206,7 +206,7 @@ class CacheActorsRequest extends CacheActorsRequestBuilder { $qb = $this->getCacheActorsSelectSql(); $this->limitToPreferredUsername($qb, $account); $this->limitToLocal($qb, true); - $this->leftJoinCacheDocuments($qb, 'icon_id'); + $qb->leftJoinCacheDocuments('icon_id'); $this->leftJoinDetails($qb); return $this->getCacheActorFromRequest($qb); @@ -221,7 +221,7 @@ class CacheActorsRequest extends CacheActorsRequestBuilder { public function searchAccounts(string $search): array { $qb = $this->getCacheActorsSelectSql(); $this->searchInAccount($qb, $search); - $this->leftJoinCacheDocuments($qb, 'icon_id'); + $qb->leftJoinCacheDocuments('icon_id'); $this->leftJoinDetails($qb); $this->limitResults($qb, 25); diff --git a/lib/Db/CacheActorsRequestBuilder.php b/lib/Db/CacheActorsRequestBuilder.php index c4dc0783..77decaca 100644 --- a/lib/Db/CacheActorsRequestBuilder.php +++ b/lib/Db/CacheActorsRequestBuilder.php @@ -79,9 +79,8 @@ class CacheActorsRequestBuilder extends CoreRequestBuilder { /** @noinspection PhpMethodParametersCountMismatchInspection */ $qb->select( - 'ca.id', 'ca.account', 'ca.following', 'ca.followers', 'ca.inbox', - 'ca.shared_inbox', 'ca.outbox', 'ca.featured', 'ca.url', 'ca.type', - 'ca.preferred_username', 'ca.name', 'ca.summary', + 'ca.id', 'ca.account', 'ca.following', 'ca.followers', 'ca.inbox', 'ca.shared_inbox', 'ca.outbox', + 'ca.featured', 'ca.url', 'ca.type', 'ca.preferred_username', 'ca.name', 'ca.summary', 'ca.public_key', 'ca.local', 'ca.details', 'ca.source', 'ca.creation' ) ->from(self::TABLE_CACHE_ACTORS, 'ca'); @@ -154,7 +153,7 @@ class CacheActorsRequestBuilder extends CoreRequestBuilder { $this->assignViewerLink($qb, $actor); try { - $icon = $this->parseCacheDocumentsLeftJoin($data); + $icon = $qb->parseLeftJoinCacheDocuments($data); $actor->setIcon($icon); } catch (InvalidResourceException $e) { } diff --git a/lib/Db/CoreRequestBuilder.php b/lib/Db/CoreRequestBuilder.php index 70422b3e..48195f1d 100644 --- a/lib/Db/CoreRequestBuilder.php +++ b/lib/Db/CoreRequestBuilder.php @@ -41,9 +41,7 @@ use OC\DB\SchemaWrapper; use OCA\Social\AP; use OCA\Social\Exceptions\InvalidResourceException; use OCA\Social\Model\ActivityPub\Actor\Person; -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\StreamAction; use OCA\Social\Service\ConfigService; use OCA\Social\Service\MiscService; @@ -61,6 +59,7 @@ class CoreRequestBuilder { const TABLE_REQUEST_QUEUE = 'social_3_req_queue'; + const TABLE_INSTANCE = 'social_3_instance'; const TABLE_ACTORS = 'social_3_actor'; const TABLE_STREAM = 'social_3_stream'; @@ -1082,61 +1081,6 @@ class CoreRequestBuilder { } - /** - * @param IQueryBuilder $qb - * @param string $fieldDocumentId - */ - protected function leftJoinCacheDocuments(IQueryBuilder &$qb, string $fieldDocumentId) { - if ($qb->getType() !== QueryBuilder::SELECT) { - return; - } - - $expr = $qb->expr(); - $func = $qb->func(); - $pf = $this->defaultSelectAlias; - - $qb->selectAlias('cd.id', 'cachedocument_id') - ->selectAlias('cd.type', 'cachedocument_type') - ->selectAlias('cd.mime_type', 'cachedocument_mime_type') - ->selectAlias('cd.media_type', 'cachedocument_media_type') - ->selectAlias('cd.url', 'cachedocument_url') - ->selectAlias('cd.local_copy', 'cachedocument_local_copy') - ->selectAlias('cd.caching', 'cachedocument_caching') - ->selectAlias('cd.public', 'cachedocument_public') - ->selectAlias('cd.error', 'cachedocument_error') - ->selectAlias('ca.creation', 'cachedocument_creation') - ->leftJoin( - $this->defaultSelectAlias, CoreRequestBuilder::TABLE_CACHE_DOCUMENTS, 'cd', - $expr->eq($func->lower($pf . '.' . $fieldDocumentId), $func->lower('cd.id')) - ); - } - - - /** - * @param array $data - * - * @return Document - * @throws InvalidResourceException - */ - protected function parseCacheDocumentsLeftJoin(array $data): Document { - $new = []; - foreach ($data as $k => $v) { - if (substr($k, 0, 14) === 'cachedocument_') { - $new[substr($k, 14)] = $v; - } - } - - $document = new Document(); - $document->importFromDatabase($new); - - if ($document->getType() !== Image::TYPE) { - throw new InvalidResourceException(); - } - - return $document; - } - - /** * @param IQueryBuilder $qb * @param string $fieldActorId diff --git a/lib/Db/InstancesRequest.php b/lib/Db/InstancesRequest.php new file mode 100644 index 00000000..2614072f --- /dev/null +++ b/lib/Db/InstancesRequest.php @@ -0,0 +1,82 @@ + + * @copyright 2018, Maxence Lange + * @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 . + * + */ + + +namespace OCA\Social\Db; + + +use daita\MySmallPhpTools\Traits\TArrayTools; +use OCA\Social\Exceptions\InstanceDoesNotExistException; +use OCA\Social\Model\ActivityPub\ACore; +use OCA\Social\Model\Instance; + + +/** + * Class InstancesRequest + * + * @package OCA\Social\Db + */ +class InstancesRequest extends InstancesRequestBuilder { + + + use TArrayTools; + + + /** + * @param Instance $instance + * TODO: store instance in db + */ + public function save(Instance $instance) { +// $now = new DateTime('now'); +// $instance->setCreation($now->getTimestamp()); + + $qb = $this->getInstanceInsertSql(); + $qb->setValue('uri', $qb->createNamedParameter($instance->getUri())); + + $qb->execute(); + } + + + /** + * @param int $format + * + * @return Instance + * @throws InstanceDoesNotExistException + */ + public function getLocal(int $format = ACore::FORMAT_ACTIVITYPUB): Instance { + $qb = $this->getInstanceSelectSql($format); + $qb->linkToCacheActors('ca', 'account_prim'); + $qb->limitToDBFieldInt('local', 1); + $qb->leftJoinCacheDocuments('icon_id', 'ca'); + + return $this->getInstanceFromRequest($qb); + } + +} + diff --git a/lib/Db/InstancesRequestBuilder.php b/lib/Db/InstancesRequestBuilder.php new file mode 100644 index 00000000..9b44808d --- /dev/null +++ b/lib/Db/InstancesRequestBuilder.php @@ -0,0 +1,171 @@ + + * @copyright 2018, Maxence Lange + * @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 . + * + */ + +namespace OCA\Social\Db; + + +use daita\MySmallPhpTools\Exceptions\RowNotFoundException; +use daita\MySmallPhpTools\Traits\TArrayTools; +use OCA\Social\Exceptions\InstanceDoesNotExistException; +use OCA\Social\Exceptions\InvalidResourceException; +use OCA\Social\Model\ActivityPub\ACore; +use OCA\Social\Model\Instance; +use OCP\DB\QueryBuilder\IQueryBuilder; + +class InstancesRequestBuilder extends CoreRequestBuilder { + + + use TArrayTools; + + + /** + * Base of the Sql Insert request + * + * @return SocialQueryBuilder + */ + protected function getInstanceInsertSql(): SocialQueryBuilder { + $qb = $this->getQueryBuilder(); + $qb->insert(self::TABLE_INSTANCE); + + return $qb; + } + + + /** + * Base of the Sql Update request + * + * @return IQueryBuilder + */ + protected function getInstanceUpdateSql(): IQueryBuilder { + $qb = $this->getQueryBuilder(); + $qb->update(self::TABLE_INSTANCE); + + return $qb; + } + + + /** + * Base of the Sql Select request for Shares + * + * @param int $format + * + * @return SocialQueryBuilder + */ + protected function getInstanceSelectSql(int $format = ACore::FORMAT_ACTIVITYPUB): SocialQueryBuilder { + $qb = $this->getQueryBuilder(); + $qb->setFormat($format); + + /** @noinspection PhpMethodParametersCountMismatchInspection */ + $qb->select( + 'i.local', 'i.uri', 'i.title', 'i.version', 'i.short_description', 'i.description', 'i.email', + 'i.urls', 'i.stats', 'i.usage', 'i.image', 'i.languages', 'i.contact', 'i.account_prim' + ) + ->from(self::TABLE_INSTANCE, 'i'); + + $qb->setDefaultSelectAlias('i'); + + return $qb; + } + + + /** + * Base of the Sql Delete request + * + * @return IQueryBuilder + */ + protected function getInstanceDeleteSql(): IQueryBuilder { + $qb = $this->getQueryBuilder(); + $qb->delete(self::TABLE_INSTANCE); + + return $qb; + } + + + /** + * @param SocialQueryBuilder $qb + * + * @return Instance + * @throws InstanceDoesNotExistException + */ + protected function getInstanceFromRequest(SocialQueryBuilder $qb): Instance { + /** @var Instance $result */ + try { + $result = $qb->getRow([$this, 'parseInstanceSelectSql']); + } catch (RowNotFoundException $e) { + throw new InstanceDoesNotExistException($e->getMessage()); + } + + return $result; + } + + + /** + * @param SocialQueryBuilder $qb + * + * @return ACore[] + */ + public function getInstancesFromRequest(SocialQueryBuilder $qb): array { + /** @var ACore[] $result */ + $result = $qb->getRows([$this, 'parseInstanceSelectSql']); + + return $result; + } + + + /** + * @param array $data + * @param SocialQueryBuilder $qb + * + * @return Instance + */ + public function parseInstanceSelectSql($data, SocialQueryBuilder $qb): Instance { + $instance = new Instance(); + $instance->importFromDatabase($data); + + try { + $actor = $qb->parseLeftJoinCacheActors($data); + $actor->setExportFormat($qb->getFormat()); + try { + $icon = $qb->parseLeftJoinCacheDocuments($data); + $actor->setIcon($icon); + } catch (InvalidResourceException $e) { + } + $instance->setContactAccount($actor); + } catch (InvalidResourceException $e) { + } + + if ($instance->isLocal() && $instance->getVersion() === '%CURRENT%') { + $instance->setVersion($this->configService->getAppValue('installed_version')); + } + + return $instance; + } + +} + diff --git a/lib/Db/SocialCrossQueryBuilder.php b/lib/Db/SocialCrossQueryBuilder.php index 7652b1d0..cf70cfe7 100644 --- a/lib/Db/SocialCrossQueryBuilder.php +++ b/lib/Db/SocialCrossQueryBuilder.php @@ -32,7 +32,11 @@ namespace OCA\Social\Db; use Doctrine\DBAL\Query\QueryBuilder; +use OCA\Social\AP; use OCA\Social\Exceptions\InvalidResourceException; +use OCA\Social\Model\ActivityPub\Actor\Person; +use OCA\Social\Model\ActivityPub\Object\Document; +use OCA\Social\Model\ActivityPub\Object\Image; use OCA\Social\Model\Client\ClientToken; use OCP\DB\QueryBuilder\ICompositeExpression; @@ -91,7 +95,17 @@ class SocialCrossQueryBuilder extends SocialCoreQueryBuilder { } $pf = (($alias === '') ? $this->getDefaultSelectAlias() : $alias); - $this->from(CoreRequestBuilder::TABLE_CACHE_ACTORS, $pf); + + $expr = $this->expr(); + if ($link !== '') { + $this->innerJoin( + $this->getDefaultSelectAlias(), CoreRequestBuilder::TABLE_CACHE_ACTORS, $pf, + $expr->eq('ca.id_prim', $link) + ); + } else { + $this->from(CoreRequestBuilder::TABLE_CACHE_ACTORS, $pf); + } + $this->selectAlias($pf . '.id', 'cacheactor_id') ->selectAlias($pf . '.type', 'cacheactor_type') ->selectAlias($pf . '.account', 'cacheactor_account') @@ -107,14 +121,89 @@ class SocialCrossQueryBuilder extends SocialCoreQueryBuilder { ->selectAlias($pf . '.summary', 'cacheactor_summary') ->selectAlias($pf . '.public_key', 'cacheactor_public_key') ->selectAlias($pf . '.source', 'cacheactor_source') + ->selectAlias($pf . '.details', 'cacheactor_details') ->selectAlias($pf . '.creation', 'cacheactor_creation') ->selectAlias($pf . '.local', 'cacheactor_local'); + } - if ($link !== '') { - $expr = $this->expr(); - $this->andWhere($expr->eq('ca.id_prim', $link)); + + /** + * @param array $data + * + * @return Person + * @throws InvalidResourceException + */ + public function parseLeftJoinCacheActors(array $data): Person { + $new = []; + + foreach ($data as $k => $v) { + if (substr($k, 0, 11) === 'cacheactor_') { + $new[substr($k, 11)] = $v; + } } + $actor = new Person(); + $actor->importFromDatabase($new); + if (!AP::$activityPub->isActor($actor)) { + throw new InvalidResourceException(); + } + + return $actor; + } + + + /** + * @param string $fieldDocumentId + * @param string $alias + */ + public function leftJoinCacheDocuments(string $fieldDocumentId, string $alias = '') { + if ($this->getType() !== QueryBuilder::SELECT) { + return; + } + + $expr = $this->expr(); + $func = $this->func(); + + $pf = (($alias === '') ? $this->getDefaultSelectAlias() : $alias); + $this->selectAlias('cd.id', 'cachedocument_id') + ->selectAlias('cd.type', 'cachedocument_type') + ->selectAlias('cd.mime_type', 'cachedocument_mime_type') + ->selectAlias('cd.media_type', 'cachedocument_media_type') + ->selectAlias('cd.url', 'cachedocument_url') + ->selectAlias('cd.local_copy', 'cachedocument_local_copy') + ->selectAlias('cd.caching', 'cachedocument_caching') + ->selectAlias('cd.public', 'cachedocument_public') + ->selectAlias('cd.error', 'cachedocument_error') + ->selectAlias('cd.creation', 'cachedocument_creation') + ->leftJoin( + $this->getDefaultSelectAlias(), CoreRequestBuilder::TABLE_CACHE_DOCUMENTS, 'cd', + $expr->eq($func->lower($pf . '.' . $fieldDocumentId), $func->lower('cd.id')) + ); + } + + + /** + * @param array $data + * + * @return Document + * @throws InvalidResourceException + */ + public function parseLeftJoinCacheDocuments(array $data): Document { + $new = []; + foreach ($data as $k => $v) { + if (substr($k, 0, 14) === 'cachedocument_') { + $new[substr($k, 14)] = $v; + } + } + + $document = new Document(); + $document->importFromDatabase($new); + + if ($document->getType() !== Image::TYPE) { + throw new InvalidResourceException(); + } + + return $document; } diff --git a/lib/Db/StreamRequest.php b/lib/Db/StreamRequest.php index 6e833f62..4f8d7589 100644 --- a/lib/Db/StreamRequest.php +++ b/lib/Db/StreamRequest.php @@ -357,6 +357,32 @@ class StreamRequest extends StreamRequestBuilder { } + /** + * @param string $actorId + * + * @return Stream + * @throws StreamNotFoundException + */ + public function lastNoteFromActorId(string $actorId): Stream { + $qb = $this->getStreamSelectSql(); + $qb->limitToAttributedTo($actorId, true); + $qb->limitToType(Note::TYPE); + + $qb->selectDestFollowing('sd', ''); + $qb->innerJoinSteamDest('recipient', 'id_prim', 'sd', 's'); + $qb->limitToDest(ACore::CONTEXT_PUBLIC, 'recipient', '', 'sd'); + + $qb->orderBy('id', 'desc'); + $qb->setMaxResults(1); + + $cursor = $qb->execute(); + $data = $cursor->fetch(); + $cursor->closeCursor(); + + return $this->getStreamFromRequest($qb); + } + + /** * Should returns: * * Own posts, diff --git a/lib/Db/StreamRequestBuilder.php b/lib/Db/StreamRequestBuilder.php index 1d1f9835..f88b5708 100644 --- a/lib/Db/StreamRequestBuilder.php +++ b/lib/Db/StreamRequestBuilder.php @@ -197,7 +197,6 @@ class StreamRequestBuilder extends CoreRequestBuilder { /** * @param array $data - * * @param SocialQueryBuilder $qb * * @return Stream diff --git a/lib/Exceptions/InstanceDoesNotExistException.php b/lib/Exceptions/InstanceDoesNotExistException.php new file mode 100644 index 00000000..73bef200 --- /dev/null +++ b/lib/Exceptions/InstanceDoesNotExistException.php @@ -0,0 +1,39 @@ + + * @copyright 2018, Maxence Lange + * @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 . + * + */ + + +namespace OCA\Social\Exceptions; + + +use Exception; + + +class InstanceDoesNotExistException extends Exception { + +} + diff --git a/lib/Migration/Version0003Date20200611000001.php b/lib/Migration/Version0003Date20200611000001.php index 20c8f888..0488fcb5 100644 --- a/lib/Migration/Version0003Date20200611000001.php +++ b/lib/Migration/Version0003Date20200611000001.php @@ -81,6 +81,7 @@ class Version0003Date20200611000001 extends SimpleMigrationStep { $this->createClientToken($schema); $this->createFollows($schema); $this->createHashtags($schema); + $this->createInstance($schema); $this->createRequestQueue($schema); $this->createStreams($schema); $this->createStreamActions($schema); @@ -390,6 +391,133 @@ class Version0003Date20200611000001 extends SimpleMigrationStep { } + /** + * @param ISchemaWrapper $schema + */ + private function createInstance(ISchemaWrapper $schema) { + if ($schema->hasTable('social_3_instance')) { + return; + } + + $table = $schema->createTable('social_3_instance'); + $table->addColumn( + 'local', 'smallint', + [ + 'notnull' => false, + 'length' => 1, + 'default' => 0, + 'unsigned' => true + ] + ); + $table->addColumn( + 'uri', 'string', + [ + 'notnull' => false, + 'length' => 255, + ] + ); + $table->addColumn( + 'title', 'string', + [ + 'notnull' => false, + 'length' => 255, + 'default' => '' + ] + ); + $table->addColumn( + 'version', 'string', + [ + 'notnull' => false, + 'length' => 31, + 'default' => '' + ] + ); + $table->addColumn( + 'short_description', 'text', + [ + 'notnull' => false, + 'default' => '' + ] + ); + $table->addColumn( + 'description', 'text', + [ + 'notnull' => false, + 'default' => '' + ] + ); + $table->addColumn( + 'email', 'string', + [ + 'notnull' => false, + 'length' => 255, + 'default' => '' + ] + ); + $table->addColumn( + 'urls', 'text', + [ + 'notnull' => false, + 'default' => '[]' + ] + ); + $table->addColumn( + 'stats', 'text', + [ + 'notnull' => false, + 'default' => '[]' + ] + ); + $table->addColumn( + 'usage', 'text', + [ + 'notnull' => false, + 'default' => '[]' + ] + ); + $table->addColumn( + 'image', 'string', + [ + 'notnull' => false, + 'length' => 255, + 'default' => '' + ] + ); + $table->addColumn( + 'languages', 'text', + [ + 'notnull' => false, + 'default' => '[]' + ] + ); + $table->addColumn( + 'contact', 'string', + [ + 'notnull' => false, + 'length' => 127, + 'default' => '' + ] + ); + $table->addColumn( + 'account_prim', 'string', + [ + 'notnull' => false, + 'length' => 128, + 'default' => '' + ] + ); + $table->addColumn( + 'creation', 'datetime', + [ + 'notnull' => false, + ] + ); + + $table->setPrimaryKey(['uri']); + $table->addIndex(['local', 'uri', 'account_prim']); + } + + /** * @param ISchemaWrapper $schema */ diff --git a/lib/Migration/Version0003Date20200823023911.php b/lib/Migration/Version0003Date20200823023911.php index e15ea5c8..68089d79 100644 --- a/lib/Migration/Version0003Date20200823023911.php +++ b/lib/Migration/Version0003Date20200823023911.php @@ -41,7 +41,7 @@ use OCP\Migration\SimpleMigrationStep; /** - * Class Version0003Date20200611000001 + * Class Version0003Date20200823023911 * * @package OCA\Social\Migration */ @@ -76,6 +76,7 @@ class Version0003Date20200823023911 extends SimpleMigrationStep { $this->createClient($schema); $this->createClientAuth($schema); $this->createClientToken($schema); + $this->createInstance($schema); $this->addChunkToTable($schema, 'social_3_stream', ''); $this->addChunkToTable($schema, 'social_3_stream_act', '_act'); @@ -234,6 +235,133 @@ class Version0003Date20200823023911 extends SimpleMigrationStep { } + /** + * @param ISchemaWrapper $schema + */ + private function createInstance(ISchemaWrapper $schema) { + if ($schema->hasTable('social_3_instance')) { + return; + } + + $table = $schema->createTable('social_3_instance'); + $table->addColumn( + 'local', 'smallint', + [ + 'notnull' => false, + 'length' => 1, + 'default' => 0, + 'unsigned' => true + ] + ); + $table->addColumn( + 'uri', 'string', + [ + 'notnull' => false, + 'length' => 255, + ] + ); + $table->addColumn( + 'title', 'string', + [ + 'notnull' => false, + 'length' => 255, + 'default' => '' + ] + ); + $table->addColumn( + 'version', 'string', + [ + 'notnull' => false, + 'length' => 31, + 'default' => '' + ] + ); + $table->addColumn( + 'short_description', 'text', + [ + 'notnull' => false, + 'default' => '' + ] + ); + $table->addColumn( + 'description', 'text', + [ + 'notnull' => false, + 'default' => '' + ] + ); + $table->addColumn( + 'email', 'string', + [ + 'notnull' => false, + 'length' => 255, + 'default' => '' + ] + ); + $table->addColumn( + 'urls', 'text', + [ + 'notnull' => false, + 'default' => '[]' + ] + ); + $table->addColumn( + 'stats', 'text', + [ + 'notnull' => false, + 'default' => '[]' + ] + ); + $table->addColumn( + 'usage', 'text', + [ + 'notnull' => false, + 'default' => '[]' + ] + ); + $table->addColumn( + 'image', 'string', + [ + 'notnull' => false, + 'length' => 255, + 'default' => '' + ] + ); + $table->addColumn( + 'languages', 'text', + [ + 'notnull' => false, + 'default' => '[]' + ] + ); + $table->addColumn( + 'contact', 'string', + [ + 'notnull' => false, + 'length' => 127, + 'default' => '' + ] + ); + $table->addColumn( + 'account_prim', 'string', + [ + 'notnull' => false, + 'length' => 128, + 'default' => '' + ] + ); + $table->addColumn( + 'creation', 'datetime', + [ + 'notnull' => false, + ] + ); + + $table->setPrimaryKey(['uri']); + $table->addIndex(['local', 'uri', 'account_prim']); + } + + /** * @param ISchemaWrapper $schema */ diff --git a/lib/Model/ActivityPub/Actor/Person.php b/lib/Model/ActivityPub/Actor/Person.php index 163325d5..d169c60b 100644 --- a/lib/Model/ActivityPub/Actor/Person.php +++ b/lib/Model/ActivityPub/Actor/Person.php @@ -109,6 +109,9 @@ class Person extends ACore implements IQueryRow, JsonSerializable { /** @var string */ private $featured = ''; + /** @var string */ + private $header = ''; + /** @var bool */ private $locked = false; @@ -118,6 +121,15 @@ class Person extends ACore implements IQueryRow, JsonSerializable { /** @var bool */ private $discoverable = false; + /** @var string */ + private $privacy = 'public'; + + /** @var bool */ + private $sensitive = false; + + /** @var string */ + private $language = 'en'; + /** @var int */ private $avatarVersion = -1; @@ -185,9 +197,15 @@ class Person extends ACore implements IQueryRow, JsonSerializable { return $this->displayName; } - - public function setDisplayName(string $displayName): string { + /** + * @param string $displayName + * + * @return $this + */ + public function setDisplayName(string $displayName): self { $this->displayName = $displayName; + + return $this; } @@ -210,6 +228,42 @@ class Person extends ACore implements IQueryRow, JsonSerializable { } + /** + * @return string + */ + public function getAvatar(): string { + if ($this->hasIcon()) { + return $this->getIcon() + ->getId(); + } + + return ''; + } + + + /** + * @return string + */ + public function getHeader(): string { + if ($this->header === '') { + return $this->getAvatar(); + } + + return $this->header; + } + + /** + * @param string $header + * + * @return $this + */ + public function setHeader(string $header): self { + $this->header = $header; + + return $this; + } + + /** * @return string */ @@ -475,6 +529,63 @@ class Person extends ACore implements IQueryRow, JsonSerializable { } + /** + * @return string + */ + public function getPrivacy(): string { + return $this->privacy; + } + + /** + * @param string $privacy + * + * @return Person + */ + public function setPrivacy(string $privacy): self { + $this->privacy = $privacy; + + return $this; + } + + + /** + * @return bool + */ + public function isSensitive(): bool { + return $this->sensitive; + } + + /** + * @param bool $sensitive + * + * @return Person + */ + public function setSensitive(bool $sensitive): self { + $this->sensitive = $sensitive; + + return $this; + } + + + /** + * @return string + */ + public function getLanguage(): string { + return $this->language; + } + + /** + * @param string $language + * + * @return $this + */ + public function setLanguage(string $language): self { + $this->language = $language; + + return $this; + } + + /** * @return int */ @@ -612,31 +723,42 @@ class Person extends ACore implements IQueryRow, JsonSerializable { * @return array */ public function exportAsLocal(): array { + $details = $this->getDetailsAll(); $result = [ + "id" => $this->getId(), "username" => $this->getPreferredUsername(), - "acct" => $this->getAccount(), + "acct" => $this->getPreferredUsername(), "display_name" => $this->getDisplayName(), "locked" => $this->isLocked(), "bot" => $this->isBot(), "discoverable" => $this->isDiscoverable(), "group" => false, - "created_at" => "2017-05-02T09=>56=>41.951Z", + "created_at" => date('Y-m-d\TH:i:s', $this->getCreation()) . '.000Z', "note" => $this->getDescription(), "url" => $this->getId(), - "avatar" => "https://files.mastodon.social/accounts/avatars/000/126/222/original/50785214e44d10cc.jpeg", - "avatar_static" => "https://files.mastodon.social/accounts/avatars/000/126/222/original/50785214e44d10cc.jpeg", - "header" => "https://files.mastodon.social/accounts/headers/000/126/222/original/6d7b41fdd92cfd6f.jpeg", - "header_static" => "https://files.mastodon.social/accounts/headers/000/126/222/original/6d7b41fdd92cfd6f.jpeg", - "followers_count" => 9451, - "following_count" => 132, - "statuses_count" => 3020, - "last_status_at" => "2020-08-24", - "emojis" => '' + "avatar" => $this->getAvatar(), + // "avatar_static" => "https://files.mastodon.social/accounts/avatars/000/126/222/original/50785214e44d10cc.jpeg", + "avatar_static" => $this->getAvatar(), + "header" => $this->getHeader(), + "header_static" => $this->getHeader(), + "followers_count" => $this->getInt('count.followers', $details), + "following_count" => $this->getInt('count.following', $details), + "statuses_count" => $this->getInt('count.post', $details), + "last_status_at" => $this->get('last_post_creation', $details), + "source" => [ + "privacy" => $this->getPrivacy(), + "sensitive" => $this->isSensitive(), + "language" => $this->getLanguage(), + "note" => $this->getDescription(), + "fields" => [], + "follow_requests_count" => 0 + ], + "emojis" => [], + "fields" => [] ]; return array_merge(parent::exportAsLocal(), $result); } - } diff --git a/lib/Model/Client/ClientApp.php b/lib/Model/Client/ClientApp.php index 3fd5988f..b2ac14c6 100644 --- a/lib/Model/Client/ClientApp.php +++ b/lib/Model/Client/ClientApp.php @@ -246,12 +246,18 @@ class ClientApp implements IQueryRow, JsonSerializable { 'id' => $this->getId(), 'name' => $this->getName(), 'website' => $this->getWebsite(), - 'redirect_uri' => $this->getRedirectUris(), 'scopes' => implode(' ', $this->getScopes()), 'client_id' => $this->getClientId(), 'client_secret' => $this->getClientSecret() ]; + $uris = $this->getRedirectUris(); + if (sizeof($uris) > 1) { + $arr['redirect_uris'] = $uris; + } else { + $arr['redirect_uri'] = $uris[0]; + } + return array_filter($arr); } diff --git a/lib/Model/Instance.php b/lib/Model/Instance.php index a1ab08d6..65550556 100644 --- a/lib/Model/Instance.php +++ b/lib/Model/Instance.php @@ -30,59 +30,464 @@ declare(strict_types=1); namespace OCA\Social\Model; +use daita\MySmallPhpTools\IQueryRow; use daita\MySmallPhpTools\Traits\TArrayTools; use JsonSerializable; +use OCA\Social\Model\ActivityPub\Actor\Person; + + +/** + * Class Instance + * + * @package OCA\Social\Model + */ +class Instance implements IQueryRow, JsonSerializable { -class Instance implements JsonSerializable { use TArrayTools; + /** @var bool */ + private $local = false; + /** @var string */ - private $address; + private $uri = ''; - /** @var InstancePath[] */ - private $instancePaths = []; + /** @var string */ + private $title = ''; - public function __construct(string $address = '') { - $this->address = $address; + /** @var string */ + private $version = ''; + + /** @var string */ + private $shortDescription = ''; + + /** @var string */ + private $description = ''; + + /** @var string */ + private $email = ''; + + /** @var array */ + private $urls = []; + + /** @var array */ + private $stats = []; + + /** @var array */ + private $usage = []; + + /** @var string */ + private $image = ''; + + /** @var array */ + private $languages = []; + + /** @var bool */ + private $registrations = false; + + /** @var bool */ + private $approvalRequired = false; + + /** @var bool */ + private $invitesEnabled = false; + + /** @var Person */ + private $contactAccount; + + /** @var string */ + private $accountPrim; + + + /** + * Instance constructor. + */ + public function __construct() { + } + + + /** + * @return bool + */ + public function isLocal(): bool { + return $this->local; + } + + /** + * @param bool $local + * + * @return Instance + */ + public function setLocal(bool $local): self { + $this->local = $local; + + return $this; } /** * @return string */ - public function getAddress(): string { - return $this->address; + public function getUri(): string { + return $this->uri; + } + + /** + * @param string $uri + * + * @return Instance + */ + public function setUri(string $uri): self { + $this->uri = $uri; + + return $this; } /** - * @param InstancePath $path + * @return string + */ + public function getTitle(): string { + return $this->title; + } + + /** + * @param string $title * * @return Instance */ - public function addPath(InstancePath $path): Instance { - $this->instancePaths[] = $path; + public function setTitle(string $title): self { + $this->title = $title; + + return $this; + } + + + /** + * @return string + */ + public function getVersion(): string { + return $this->version; + } + + /** + * @param string $version + * + * @return Instance + */ + public function setVersion(string $version): self { + $this->version = $version; + + return $this; + } + + + /** + * @return string + */ + public function getShortDescription(): string { + return $this->shortDescription; + } + + /** + * @param string $shortDescription + * + * @return Instance + */ + public function setShortDescription(string $shortDescription): self { + $this->shortDescription = $shortDescription; + + return $this; + } + + + /** + * @return string + */ + public function getDescription(): string { + return $this->description; + } + + /** + * @param string $description + * + * @return Instance + */ + public function setDescription(string $description): self { + $this->description = $description; + + return $this; + } + + + /** + * @return string + */ + public function getEmail(): string { + return $this->email; + } + + /** + * @param string $email + * + * @return Instance + */ + public function setEmail(string $email): self { + $this->email = $email; + + return $this; + } + + + /** + * @return array + */ + public function getUrls(): array { + return $this->urls; + } + + /** + * @param array $urls + * + * @return Instance + */ + public function setUrls(array $urls): self { + $this->urls = $urls; + + return $this; + } + + + /** + * @return array + */ + public function getStats(): array { + return $this->stats; + } + + /** + * @param array $stats + * + * @return Instance + */ + public function setStats(array $stats): self { + $this->stats = $stats; + + return $this; + } + + + /** + * @return array + */ + public function getUsage(): array { + return $this->usage; + } + + /** + * @param array $usage + * + * @return Instance + */ + public function setUsage(array $usage): self { + $this->usage = $usage; + + return $this; + } + + + /** + * @return string + */ + public function getImage(): string { + return $this->image; + } + + /** + * @param string $image + * + * @return Instance + */ + public function setImage(string $image): self { + $this->image = $image; + + return $this; + } + + + /** + * @return array + */ + public function getLanguages(): array { + return $this->languages; + } + + /** + * @param array $languages + * + * @return Instance + */ + public function setLanguages(array $languages): self { + $this->languages = $languages; + + return $this; + } + + + /** + * @return bool + */ + public function isRegistrations(): bool { + return $this->registrations; + } + + /** + * @param bool $registrations + * + * @return Instance + */ + public function setRegistrations(bool $registrations): self { + $this->registrations = $registrations; + + return $this; + } + + + /** + * @return bool + */ + public function isApprovalRequired(): bool { + return $this->approvalRequired; + } + + /** + * @param bool $approvalRequired + * + * @return Instance + */ + public function setApprovalRequired(bool $approvalRequired): self { + $this->approvalRequired = $approvalRequired; + + return $this; + } + + + /** + * @return bool + */ + public function isInvitesEnabled(): bool { + return $this->invitesEnabled; + } + + /** + * @param bool $invitesEnabled + * + * @return Instance + */ + public function setInvitesEnabled(bool $invitesEnabled): self { + $this->invitesEnabled = $invitesEnabled; + + return $this; + } + + + /** + * @return bool + */ + public function hasContactAccount(): bool { + return ($this->contactAccount !== null); + } + + /** + * @return Person + */ + public function getContactAccount(): Person { + return $this->contactAccount; + } + + /** + * @param Person $account + * + * @return Instance + */ + public function setContactAccount(Person $account): self { + $this->contactAccount = $account; return $this; } /** - * @return InstancePath[] + * @return string */ - public function getInstancePaths(): array { - return $this->instancePaths; + public function getAccountPrim(): string { + return $this->accountPrim; + } + + /** + * @param string $prim + * + * @return Instance + */ + public function setAccountPrim(string $prim): self { + $this->accountPrim = $prim; + + return $this; } + /** + * @param array $data + * + * @return $this + */ + public function importFromDatabase(array $data): self { + $this->setLocal($this->getBool('local', $data)); + $this->setUri($this->get('uri', $data)); + $this->setTitle($this->get('title', $data)); + $this->setVersion($this->get('version', $data)); + $this->setShortDescription($this->get('short_description', $data)); + $this->setDescription($this->get('description', $data)); + $this->setEmail($this->get('email', $data)); + $this->setUrls($this->getArray('urls', $data)); + $this->setStats($this->getArray('stats', $data)); + $this->setUsage($this->getArray('usage', $data)); + $this->setImage($this->get('image', $data)); + $this->setLanguages($this->getArray('languages', $data)); + $this->setAccountPrim($this->get('account_prim', $data)); + +// $contact = new Person(); +// $this->setContactAccount($contact); + + return $this; + } + + + /** + * @return array + */ public function jsonSerialize(): array { - return [ - 'address' => $this->address, - 'instancePaths' => $this->getInstancePaths() + $arr = [ + 'uri' => $this->getUri(), + 'title' => $this->getTitle(), + 'version' => $this->getVersion(), + 'short_description' => $this->getShortDescription(), + 'description' => $this->getDescription(), + 'email' => $this->getEmail(), + 'urls' => $this->getUrls(), + 'stats' => $this->getStats(), + 'thumbnail' => $this->getImage(), + 'languages' => $this->getLanguages(), + 'registrations' => $this->isRegistrations(), + 'approval_required' => $this->isApprovalRequired(), + 'invites_enabled' => $this->isInvitesEnabled() ]; - } + if ($this->hasContactAccount()) { + $arr['contact_account'] = $this->getContactAccount(); + } + + return $arr; + } } diff --git a/lib/Service/AccountService.php b/lib/Service/AccountService.php index 7a513217..769a901f 100644 --- a/lib/Service/AccountService.php +++ b/lib/Service/AccountService.php @@ -42,6 +42,7 @@ use OCA\Social\Exceptions\ActorDoesNotExistException; use OCA\Social\Exceptions\ItemAlreadyExistsException; use OCA\Social\Exceptions\ItemUnknownException; use OCA\Social\Exceptions\SocialAppConfigException; +use OCA\Social\Exceptions\StreamNotFoundException; use OCA\Social\Exceptions\UrlCloudException; use OCA\Social\Model\ActivityPub\Actor\Person; use OCP\Accounts\IAccountManager; @@ -309,12 +310,20 @@ class AccountService { * @param Person $actor */ public function addLocalActorDetailCount(Person &$actor) { + $lastPostCreation = ''; + try { + $lastPost = $this->streamRequest->lastNoteFromActorId($actor->getId()); + $lastPostCreation = date('y-m-d', $lastPost->getPublishedTime()); + } catch (StreamNotFoundException $e) { + } + $count = [ 'followers' => $this->followsRequest->countFollowers($actor->getId()), 'following' => $this->followsRequest->countFollowing($actor->getId()), 'post' => $this->streamRequest->countNotesFromActorId($actor->getId()) ]; $actor->setDetailArray('count', $count); + $actor->setDetail('last_post_creation', $lastPostCreation); } diff --git a/lib/Service/InstanceService.php b/lib/Service/InstanceService.php new file mode 100644 index 00000000..0a974407 --- /dev/null +++ b/lib/Service/InstanceService.php @@ -0,0 +1,90 @@ + + * @copyright 2018, Maxence Lange + * @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 . + * + */ + + +namespace OCA\Social\Service; + + +use daita\MySmallPhpTools\Traits\TArrayTools; +use OCA\Social\Db\InstancesRequest; +use OCA\Social\Exceptions\InstanceDoesNotExistException; +use OCA\Social\Model\ActivityPub\ACore; +use OCA\Social\Model\Instance; + + +class InstanceService { + + + use TArrayTools; + + + private $instancesRequest; + + /** @var ConfigService */ + private $configService; + + /** @var MiscService */ + private $miscService; + + + public function __construct( + InstancesRequest $instancesRequest, ConfigService $configService, MiscService $miscService + ) { + $this->instancesRequest = $instancesRequest; + $this->configService = $configService; + $this->miscService = $miscService; + } + + + /** + * + */ + public function createLocal(): void { + + } + + /** + * @param int $format + * + * @return Instance + * @throws InstanceDoesNotExistException + */ + public function getLocal(int $format = ACore::FORMAT_LOCAL): Instance { + try { + return $this->instancesRequest->getLocal($format); + } catch (InstanceDoesNotExistException $e) { + } + + $this->createLocal(); + + return $this->instancesRequest->getLocal($format); + } + +} +