From f4f1aa4b5b09bd37a767662c9915b34ac8783f52 Mon Sep 17 00:00:00 2001 From: Prateek Banga Date: Mon, 11 Sep 2023 19:27:45 +0530 Subject: [PATCH] review changes and adds missing subqueries --- app/config/collections.php | 88 ++++++++++++++++++++++++++++++-------- app/init.php | 41 ++++++++++++++++++ 2 files changed, 110 insertions(+), 19 deletions(-) diff --git a/app/config/collections.php b/app/config/collections.php index a316b02290..398db4929f 100644 --- a/app/config/collections.php +++ b/app/config/collections.php @@ -1437,22 +1437,61 @@ $commonCollections = [ 'default' => null, 'array' => false, 'filters' => ['json', 'encrypt'], - ] + ], + [ + '$id' => ID::custom('search'), + 'type' => Database::VAR_STRING, + 'format' => '', + 'size' => 65535, + 'signed' => true, + 'required' => false, + 'default' => null, + 'array' => false, + 'filters' => [], + ], ], 'indexes' => [ [ '$id' => ID::custom('_key_provider'), 'type' => Database::INDEX_KEY, 'attributes' => ['provider'], - 'lengths' => [128], + 'lengths' => [], 'orders' => [Database::ORDER_ASC], ], [ '$id' => ID::custom('_key_name'), 'type' => Database::INDEX_FULLTEXT, 'attributes' => ['name'], - 'lengths' => [128], + 'lengths' => [], 'orders' => [Database::ORDER_ASC], + ], + [ + '$id' => ID::custom('_key_type'), + 'type' => Database::INDEX_KEY, + 'attributes' => ['type'], + 'lengths' => [], + 'orders' => [Database::ORDER_ASC], + ], + [ + '$id' => ID::custom('_key_default'), + 'type' => Database::INDEX_KEY, + 'attributes' => ['default'], + 'lengths' => [], + 'orders' => [Database::ORDER_ASC], + ], + [ + '$id' => ID::custom('_key_default_type'), + 'type' => Database::INDEX_KEY, + 'attributes' => ['default, type'], + 'lengths' => [], + 'orders' => [Database::ORDER_ASC], + ], + [ + '$id' => ID::custom('_key_search'), + 'type' => Database::INDEX_FULLTEXT, + 'attributes' => ['search'], + 'lengths' => [], + 'orders' => [], ] ], ], @@ -1488,7 +1527,7 @@ $commonCollections = [ '$id' => ID::custom('data'), 'type' => Database::VAR_STRING, 'format' => '', - 'size' => 16384, + 'size' => 65535, 'signed' => true, 'required' => true, 'default' => null, @@ -1499,7 +1538,7 @@ $commonCollections = [ '$id' => ID::custom('to'), 'type' => Database::VAR_STRING, 'format' => '', - 'size' => 16834, + 'size' => 65535, 'signed' => true, 'required' => true, 'default' => null, @@ -1518,10 +1557,10 @@ $commonCollections = [ 'filters' => ['datetime'], ], [ - '$id' => ID::custom('deliveryError'), + '$id' => ID::custom('deliveryErrors'), 'type' => Database::VAR_STRING, 'format' => '', - 'size' => 16834, + 'size' => 65535, 'signed' => true, 'required' => false, 'default' => null, @@ -1567,14 +1606,14 @@ $commonCollections = [ '$id' => ID::custom('_key_providerId'), 'type' => Database::INDEX_KEY, 'attributes' => ['providerId'], - 'lengths' => [128], + 'lengths' => [], 'orders' => [Database::ORDER_ASC], ], [ '$id' => ID::custom('_key_providerInternalId'), 'type' => Database::INDEX_KEY, 'attributes' => ['providerInternalId'], - 'lengths' => [128], + 'lengths' => [], 'orders' => [Database::ORDER_ASC], ], [ @@ -1646,21 +1685,32 @@ $commonCollections = [ 'default' => null, 'array' => false, 'filters' => ['subQueryTopicTargets'], - ] + ], + [ + '$id' => ID::custom('search'), + 'type' => Database::VAR_STRING, + 'format' => '', + 'size' => 16384, + 'signed' => true, + 'required' => false, + 'default' => null, + 'array' => false, + 'filters' => [], + ], ], 'indexes' => [ [ '$id' => ID::custom('_key_providerId'), 'type' => Database::INDEX_KEY, 'attributes' => ['providerId'], - 'lengths' => [128], + 'lengths' => [], 'orders' => [Database::ORDER_ASC], ], [ '$id' => ID::custom('_key_providerInternalId'), 'type' => Database::INDEX_KEY, 'attributes' => ['providerInternalId'], - 'lengths' => [128], + 'lengths' => [], 'orders' => [Database::ORDER_ASC], ], [ @@ -1671,12 +1721,12 @@ $commonCollections = [ 'orders' => [], ], [ - '$id' => ID::custom('_key_description'), + '$id' => ID::custom('_key_search'), 'type' => Database::INDEX_FULLTEXT, - 'attributes' => ['description'], + 'attributes' => ['name'], 'lengths' => [], - 'orders' => [], - ], + 'orders' => [Database::ORDER_ASC], + ] ], ], @@ -1826,7 +1876,7 @@ $commonCollections = [ '$id' => ID::custom('identifier'), 'type' => Database::VAR_STRING, 'format' => '', - 'size' => Database::LENGTH_KEY, + 'size' => 2048, 'signed' => true, 'required' => true, 'default' => null, @@ -1839,14 +1889,14 @@ $commonCollections = [ '$id' => ID::custom('_key_userId'), 'type' => Database::INDEX_KEY, 'attributes' => ['userId'], - 'lengths' => [128], + 'lengths' => [], 'orders' => [Database::ORDER_ASC], ], [ '$id' => ID::custom('_key_userInternalId'), 'type' => Database::INDEX_KEY, 'attributes' => ['userInternalId'], - 'lengths' => [128], + 'lengths' => [], 'orders' => [Database::ORDER_ASC], ], [ diff --git a/app/init.php b/app/init.php index f2d7bc7db4..0df1280051 100644 --- a/app/init.php +++ b/app/init.php @@ -543,6 +543,47 @@ Database::addFilter( ])); } ); + +Database::addFilter( + 'subQueryProviderType', + function (mixed $value) { + return null; + }, + function (mixed $value, Document $document, Database $database) { + $provider = Authorization::skip(fn () => $database + ->getDocument( + 'providers', + $document->getAttribute('providerId'), + [Query::select(['type'])] + )); + if ($provider) { + return $provider->getAttribute('type'); + } + return null; + } +); + + +Database::addFilter( + 'subQueryTopicTargets', + function (mixed $value) { + return null; + }, + function (mixed $value, Document $document, Database $database) { + $targetIds = Authorization::skip(fn () => \array_map( + fn ($document) => $document->getAttribute('targetId'), + $database + ->find('subscribers', [ + Query::equal('topicInternalId', [$document->getInternalId()]), + Query::limit(APP_LIMIT_SUBQUERY), + ]) + )); + if (\count($targetIds) > 0) { + return $database->find('targets', [Query::equal('$id', $targetIds)]); + } + return []; + } +); /** * DB Formats */