Merge pull request #13263 from appwrite/post-messaging-topics

Messaging targets subquery
This commit is contained in:
Shmuel Fogel authored and GitHub committed 2026-08-20 11:35:32 +03:00
commit aa25d1c191
6 files changed
+91 -33

No files matched your search

+20 -10
View File
@@ -2651,7 +2651,11 @@ Http::get('/v1/messaging/topics')
$cursor->setValue($cursorDocument[0]);
}
try {
$topics = $dbForProject->find('topics', $queries);
// Safe to skip subquery, Does not return in Response
$topics = $dbForProject->skipFilters(
fn () => $dbForProject->find('topics', $queries),
['subQueryTopicTargets']
);
$total = $includeTotal ? $dbForProject->count('topics', $queries, APP_LIMIT_COUNT) : 0;
} catch (OrderException $e) {
throw new Exception(Exception::DATABASE_QUERY_ORDER_NULL, "The order attribute '{$e->getAttribute()}' had a null value. Cursor pagination requires all documents order attribute values are non-null.");
@@ -2874,10 +2878,13 @@ Http::post('/v1/messaging/topics/:topicId/subscribers')
default => throw new Exception(Exception::TARGET_PROVIDER_INVALID_TYPE),
};
$authorization->skip(fn () => $dbForProject->increaseDocumentAttribute(
'topics',
$topicId,
$totalAttribute,
$authorization->skip(fn () => $dbForProject->skipFilters(
fn () => $dbForProject->increaseDocumentAttribute(
'topics',
$topicId,
$totalAttribute,
),
['subQueryTopicTargets']
));
} catch (DuplicateException) {
throw new Exception(Exception::SUBSCRIBER_ALREADY_EXISTS);
@@ -3081,11 +3088,14 @@ Http::delete('/v1/messaging/topics/:topicId/subscribers/:subscriberId')
default => throw new Exception(Exception::TARGET_PROVIDER_INVALID_TYPE),
};
$authorization->skip(fn () => $dbForProject->decreaseDocumentAttribute(
'topics',
$topicId,
$totalAttribute,
min: 0
$authorization->skip(fn () => $dbForProject->skipFilters(
fn () => $dbForProject->decreaseDocumentAttribute(
'topics',
$topicId,
$totalAttribute,
min: 0
),
['subQueryTopicTargets']
));
$queueForEvents
+1 -1
View File
@@ -42,7 +42,7 @@ const APP_LIMIT_ARRAY_LABELS_SIZE = 1000; // Default maximum of how many labels
const APP_LIMIT_ARRAY_SCOPES_SIZE = 200; // Default maximum of how many scope elements can there be in API parameter that expects array value
const APP_LIMIT_ARRAY_ELEMENT_SIZE = 4096; // Default maximum length of element in array parameter represented by maximum URL length.
const APP_LIMIT_SUBQUERY = 1000;
const APP_LIMIT_SUBSCRIBERS_SUBQUERY = 1_000_000;
const APP_LIMIT_SUBSCRIBERS_SUBQUERY = 25;
const APP_LIMIT_WRITE_RATE_DEFAULT = 60; // Default maximum write rate per rate period
const APP_LIMIT_WRITE_RATE_PERIOD_DEFAULT = 60; // Default maximum write rate period in seconds
const APP_LIMIT_LIST_DEFAULT = 25; // Default maximum number of items to return in list API calls
+18 -12
View File
@@ -357,19 +357,25 @@ Database::addFilter(
return;
},
function (mixed $value, Document $document, Database $database) {
$targetIds = $database->getAuthorization()->skip(fn () => \array_map(
fn ($document) => $document->getAttribute('targetInternalId'),
$database->find('subscribers', [
Query::equal('topicInternalId', [$document->getSequence()]),
Query::limit(APP_LIMIT_SUBSCRIBERS_SUBQUERY)
])
));
if (\count($targetIds) > 0) {
return $database->skipValidation(fn () => $database->find('targets', [
Query::equal('$sequence', $targetIds)
]));
$subscribers = $database->getAuthorization()->skip(fn () => $database->find('subscribers', [
Query::select(['targetInternalId']),
Query::equal('topicInternalId', [$document->getSequence()]),
Query::limit(APP_LIMIT_SUBSCRIBERS_SUBQUERY)
]));
$targetIds = \array_map(
fn (Document $subscriber) => $subscriber->getAttribute('targetInternalId'),
$subscribers
);
if (\count($targetIds) === 0) {
return [];
}
return [];
return $database->skipValidation(fn () => $database->find('targets', [
Query::equal('$sequence', $targetIds),
Query::limit(\count($targetIds))
]));
}
);
+9 -4
View File
@@ -36,8 +36,13 @@ class Targets
function (Document $subscriber) use ($database, $target) {
$topicId = $subscriber->getAttribute('topicId');
$topicInternalId = $subscriber->getAttribute('topicInternalId');
$topic = $database->getDocument('topics', $topicId);
if (!$topic->isEmpty() && $topic->getSequence() === $topicInternalId) {
$topic = $database->skipFilters(fn () => $database->findOne('topics', [
Query::select(['$id', '$sequence']),
Query::equal('$sequence', [$topicInternalId]),
]), ['subQueryTopicTargets']);
if (!$topic->isEmpty()) {
$totalAttribute = match ($target->getAttribute('providerType')) {
MESSAGE_TYPE_EMAIL => 'emailTotal',
MESSAGE_TYPE_SMS => 'smsTotal',
@@ -46,12 +51,12 @@ class Targets
};
try {
$database->decreaseDocumentAttribute(
$database->skipFilters(fn () => $database->decreaseDocumentAttribute(
'topics',
$topicId,
$totalAttribute,
min: 0
);
), ['subQueryTopicTargets']);
} catch (LimitException $e) {
Console::error("Delete subscribers decreaseDocumentAttribute (topicId={$topicId}): {$e->getMessage()}");
}
+11 -6
View File
@@ -362,7 +362,9 @@ class Messaging extends Action
*
* Peak memory is O(MESSAGE_RECIPIENTS_PAGE_SIZE), never O(topic size): topics are walked through the
* subscribers collection with cursor pagination rather than reading the topic's `targets` attribute, which
* triggers the subQueryTopicTargets filter and loads up to APP_LIMIT_SUBSCRIBERS_SUBQUERY rows at once.
* is capped at APP_LIMIT_SUBSCRIBERS_SUBQUERY and would silently drop the rest of the recipients.
* Decode filters run regardless of Query::select, so the topic lookup below skips that filter explicitly
* rather than relying on selecting only $sequence.
*
* @param array<string> $topicIds
* @param array<string> $userIds
@@ -379,11 +381,14 @@ class Messaging extends Action
Document $default
): \Generator {
if (\count($topicIds) > 0) {
$topics = $dbForProject->find('topics', [
Query::select(['$sequence']),
Query::equal('$id', $topicIds),
Query::limit(\count($topicIds)),
]);
$topics = $dbForProject->skipFilters(
fn () => $dbForProject->find('topics', [
Query::select(['$sequence']),
Query::equal('$id', $topicIds),
Query::limit(\count($topicIds)),
]),
['subQueryTopicTargets']
);
foreach ($topics as $topic) {
$cursor = null;
@@ -1208,6 +1208,10 @@ trait MessagingBase
$this->assertEquals(200, $response['headers']['status-code']);
$this->assertGreaterThanOrEqual(2, \count($response['body']['topics']));
foreach ($response['body']['topics'] as $topic) {
$this->assertArrayNotHasKey('targets', $topic);
}
$response = $this->client->call(Client::METHOD_GET, '/messaging/topics', [
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
@@ -1240,6 +1244,7 @@ trait MessagingBase
$this->assertEquals(0, $response['body']['emailTotal']);
$this->assertEquals(0, $response['body']['smsTotal']);
$this->assertEquals(0, $response['body']['pushTotal']);
$this->assertArrayNotHasKey('targets', $response['body']);
}
public function testCreateSubscriber(): void
@@ -1373,6 +1378,33 @@ trait MessagingBase
$this->assertEquals(201, $response['headers']['status-code']);
}
// The topic's `targets` sub-query is internal and capped far below the subscriber count,
// so it must never surface. Endpoints that load a topic without running the filter have to
// stay indistinguishable from those that run it.
$response = $this->client->call(Client::METHOD_GET, '/messaging/topics/' . $topic['$id'], [
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
]);
$this->assertEquals(200, $response['headers']['status-code']);
$this->assertEquals(101, $response['body']['emailTotal']);
$this->assertArrayNotHasKey('targets', $response['body']);
$response = $this->client->call(Client::METHOD_GET, '/messaging/topics', [
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
], [
'queries' => [
Query::equal('$id', [$topic['$id']])->toString(),
],
]);
$this->assertEquals(200, $response['headers']['status-code']);
$this->assertSame(1, \count($response['body']['topics']));
$this->assertArrayNotHasKey('targets', $response['body']['topics'][0]);
}
public function testGetSubscriber(): void