diff options
Diffstat (limited to 'lib/Db')
-rw-r--r-- | lib/Db/CacheActorsRequest.php | 8 | ||||
-rw-r--r-- | lib/Db/CacheActorsRequestBuilder.php | 7 | ||||
-rw-r--r-- | lib/Db/CoreRequestBuilder.php | 58 | ||||
-rw-r--r-- | lib/Db/InstancesRequest.php | 82 | ||||
-rw-r--r-- | lib/Db/InstancesRequestBuilder.php | 171 | ||||
-rw-r--r-- | lib/Db/SocialCrossQueryBuilder.php | 97 | ||||
-rw-r--r-- | lib/Db/StreamRequest.php | 26 | ||||
-rw-r--r-- | lib/Db/StreamRequestBuilder.php | 1 |
8 files changed, 380 insertions, 70 deletions
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', '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 @@ -358,6 +358,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, * * Followed accounts 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 |