summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorMaxence Lange <maxence@artificial-owl.com>2020-09-01 13:14:19 -0100
committerMaxence Lange <maxence@artificial-owl.com>2020-09-01 13:14:19 -0100
commit8e7cf73e85d98cf66c97181150e07eade03183ad (patch)
tree6d898cb5790322348f2a35e328ae9cb7a7ff4c29 /lib
parent8965acea039fc93b41f31d104c1b7330f11905df (diff)
more endpoints
Signed-off-by: Maxence Lange <maxence@artificial-owl.com>
Diffstat (limited to 'lib')
-rw-r--r--lib/Controller/ApiController.php80
-rw-r--r--lib/Controller/OAuthController.php121
-rw-r--r--lib/Db/CacheActorsRequest.php8
-rw-r--r--lib/Db/CacheActorsRequestBuilder.php7
-rw-r--r--lib/Db/CoreRequestBuilder.php58
-rw-r--r--lib/Db/InstancesRequest.php82
-rw-r--r--lib/Db/InstancesRequestBuilder.php171
-rw-r--r--lib/Db/SocialCrossQueryBuilder.php97
-rw-r--r--lib/Db/StreamRequest.php26
-rw-r--r--lib/Db/StreamRequestBuilder.php1
-rw-r--r--lib/Exceptions/InstanceDoesNotExistException.php39
-rw-r--r--lib/Migration/Version0003Date20200611000001.php128
-rw-r--r--lib/Migration/Version0003Date20200823023911.php130
-rw-r--r--lib/Model/ActivityPub/Actor/Person.php150
-rw-r--r--lib/Model/Client/ClientApp.php8
-rw-r--r--lib/Model/Instance.php441
-rw-r--r--lib/Service/AccountService.php9
-rw-r--r--lib/Service/InstanceService.php90
18 files changed, 1474 insertions, 172 deletions
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';
@@ -1084,61 +1083,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
* @param bool $asFollower
* @param string $prefix
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 @@
+<?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\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 @@
+<?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\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'