diff --git a/app/config/collections.php b/app/config/collections.php index 43954d667c..96e7cd7b25 100644 --- a/app/config/collections.php +++ b/app/config/collections.php @@ -2036,6 +2036,17 @@ $commonCollections = [ 'array' => false, 'filters' => [], ], + [ + '$id' => ID::custom('expired'), + 'type' => Database::VAR_BOOLEAN, + 'format' => '', + 'size' => 0, + 'signed' => true, + 'required' => false, + 'default' => false, + 'array' => false, + 'filters' => [], + ], ], 'indexes' => [ [ diff --git a/app/config/errors.php b/app/config/errors.php index e5c2d4e5bd..4cc8e734ee 100644 --- a/app/config/errors.php +++ b/app/config/errors.php @@ -811,6 +811,11 @@ return [ 'description' => 'Provider with the requested ID is of the incorrect type.', 'code' => 400, ], + Exception::PROVIDER_MISSING_CREDENTIALS => [ + 'name' => Exception::PROVIDER_MISSING_CREDENTIALS, + 'description' => 'Provider with the requested ID is missing credentials.', + 'code' => 400, + ], /** Topics */ Exception::TOPIC_NOT_FOUND => [ diff --git a/app/controllers/api/messaging.php b/app/controllers/api/messaging.php index 93c907a76f..b2ebd3c2f0 100644 --- a/app/controllers/api/messaging.php +++ b/app/controllers/api/messaging.php @@ -62,10 +62,10 @@ App::post('/v1/messaging/providers/mailgun') ->param('domain', '', new Text(0), 'Mailgun Domain.', true) ->param('isEuRegion', null, new Boolean(), 'Set as EU region.', true) ->param('enabled', null, new Boolean(), 'Set as enabled.', true) - ->param('fromName', '', new Text(128), 'Sender Name.', true) + ->param('fromName', '', new Text(128, 0), 'Sender Name.', true) ->param('fromEmail', '', new Email(), 'Sender email address.', true) - ->param('replyToName', '', new Text(128), 'Name set in the reply to field for the mail. Default value is sender name. Reply to name must have reply to email as well.', true) - ->param('replyToEmail', '', new Text(128), 'Email set in the reply to field for the mail. Default value is sender email. Reply to email must have reply to name as well.', true) + ->param('replyToName', '', new Text(128, 0), 'Name set in the reply to field for the mail. Default value is sender name. Reply to name must have reply to email as well.', true) + ->param('replyToEmail', '', new Email(), 'Email set in the reply to field for the mail. Default value is sender email. Reply to email must have reply to name as well.', true) ->inject('queueForEvents') ->inject('dbForProject') ->inject('response') @@ -101,7 +101,7 @@ App::post('/v1/messaging/providers/mailgun') \array_key_exists('isEuRegion', $credentials) && \array_key_exists('apiKey', $credentials) && \array_key_exists('domain', $credentials) && - \array_key_exists('from', $options) + \array_key_exists('fromEmail', $options) ) { $enabled = true; } else { @@ -150,10 +150,10 @@ App::post('/v1/messaging/providers/sendgrid') ->param('name', '', new Text(128), 'Provider name.') ->param('apiKey', '', new Text(0), 'Sendgrid API key.', true) ->param('enabled', null, new Boolean(), 'Set as enabled.', true) - ->param('fromName', '', new Text(128), 'Sender Name.', true) + ->param('fromName', '', new Text(128, 0), 'Sender Name.', true) ->param('fromEmail', '', new Email(), 'Sender email address.', true) - ->param('replyToName', '', new Text(128), 'Name set in the reply to field for the mail. Default value is sender name.', true) - ->param('replyToEmail', '', new Text(128), 'Email set in the reply to field for the mail. Default value is sender email.', true) + ->param('replyToName', '', new Text(128, 0), 'Name set in the reply to field for the mail. Default value is sender name.', true) + ->param('replyToEmail', '', new Email(), 'Email set in the reply to field for the mail. Default value is sender email.', true) ->inject('queueForEvents') ->inject('dbForProject') ->inject('response') @@ -179,7 +179,7 @@ App::post('/v1/messaging/providers/sendgrid') if ( $enabled === true && \array_key_exists('apiKey', $credentials) - && \array_key_exists('from', $options) + && \array_key_exists('fromEmail', $options) ) { $enabled = true; } else { @@ -616,9 +616,13 @@ App::post('/v1/messaging/providers/fcm') ->inject('queueForEvents') ->inject('dbForProject') ->inject('response') - ->action(function (string $providerId, string $name, ?array $serviceAccountJSON, ?bool $enabled, Event $queueForEvents, Database $dbForProject, Response $response) { + ->action(function (string $providerId, string $name, array|string|null $serviceAccountJSON, ?bool $enabled, Event $queueForEvents, Database $dbForProject, Response $response) { $providerId = $providerId == 'unique()' ? ID::unique() : $providerId; + $serviceAccountJSON = \is_string($serviceAccountJSON) + ? \json_decode($serviceAccountJSON, true) + : $serviceAccountJSON; + $credentials = []; if (!\is_null($serviceAccountJSON)) { @@ -917,9 +921,10 @@ App::patch('/v1/messaging/providers/mailgun/:providerId') if ($provider->isEmpty()) { throw new Exception(Exception::PROVIDER_NOT_FOUND); } - $providerAttr = $provider->getAttribute('provider'); - if ($providerAttr !== 'mailgun') { + $providerProvider = $provider->getAttribute('provider'); + + if ($providerProvider !== 'mailgun') { throw new Exception(Exception::PROVIDER_INCORRECT_TYPE); } @@ -949,7 +954,7 @@ App::patch('/v1/messaging/providers/mailgun/:providerId') $credentials = $provider->getAttribute('credentials'); - if ($isEuRegion === true || $isEuRegion === false) { + if (!\is_null($isEuRegion)) { $credentials['isEuRegion'] = $isEuRegion; } @@ -963,19 +968,21 @@ App::patch('/v1/messaging/providers/mailgun/:providerId') $provider->setAttribute('credentials', $credentials); - if ($enabled === true || $enabled === false) { - if ( - $enabled === true && - \array_key_exists('isEuRegion', $credentials) && - \array_key_exists('apiKey', $credentials) && - \array_key_exists('domain', $credentials) && - \array_key_exists('from', $provider->getAttribute('options')) - ) { - $enabled = true; + if (!\is_null($enabled)) { + if ($enabled) { + if ( + \array_key_exists('isEuRegion', $credentials) && + \array_key_exists('apiKey', $credentials) && + \array_key_exists('domain', $credentials) && + \array_key_exists('fromEmail', $options) + ) { + $provider->setAttribute('enabled', true); + } else { + throw new Exception(Exception::PROVIDER_MISSING_CREDENTIALS); + } } else { - $enabled = false; + $provider->setAttribute('enabled', false); } - $provider->setAttribute('enabled', $enabled); } $provider = $dbForProject->updateDocument('providers', $provider->getId(), $provider); @@ -1054,17 +1061,19 @@ App::patch('/v1/messaging/providers/sendgrid/:providerId') ]); } - if ($enabled === true || $enabled === false) { - if ( - $enabled === true - && \array_key_exists('apiKey', $provider->getAttribute('credentials')) - && \array_key_exists('from', $provider->getAttribute('options')) - ) { - $enabled = true; + if (!\is_null($enabled)) { + if ($enabled) { + if ( + \array_key_exists('apiKey', $provider->getAttribute('credentials')) && + \array_key_exists('fromEmail', $provider->getAttribute('options')) + ) { + $provider->setAttribute('enabled', true); + } else { + throw new Exception(Exception::PROVIDER_MISSING_CREDENTIALS); + } } else { - $enabled = false; + $provider->setAttribute('enabled', false); } - $provider->setAttribute('enabled', $enabled); } $provider = $dbForProject->updateDocument('providers', $provider->getId(), $provider); @@ -1133,18 +1142,20 @@ App::patch('/v1/messaging/providers/msg91/:providerId') $provider->setAttribute('credentials', $credentials); - if ($enabled === true || $enabled === false) { - if ( - $enabled === true - && \array_key_exists('senderId', $credentials) - && \array_key_exists('authKey', $credentials) - && \array_key_exists('from', $provider->getAttribute('options')) - ) { - $enabled = true; + if (!\is_null($enabled)) { + if ($enabled) { + if ( + \array_key_exists('senderId', $credentials) && + \array_key_exists('authKey', $credentials) && + \array_key_exists('from', $provider->getAttribute('options')) + ) { + $provider->setAttribute('enabled', true); + } else { + throw new Exception(Exception::PROVIDER_MISSING_CREDENTIALS); + } } else { - $enabled = false; + $provider->setAttribute('enabled', false); } - $provider->setAttribute('enabled', $enabled); } $provider = $dbForProject->updateDocument('providers', $provider->getId(), $provider); @@ -1213,19 +1224,20 @@ App::patch('/v1/messaging/providers/telesign/:providerId') $provider->setAttribute('credentials', $credentials); - if ($enabled === true || $enabled === false) { - if ( - $enabled === true - && \array_key_exists('username', $credentials) - && \array_key_exists('password', $credentials) - && \array_key_exists('from', $provider->getAttribute('options')) - ) { - $enabled = true; + if (!\is_null($enabled)) { + if ($enabled) { + if ( + \array_key_exists('username', $credentials) && + \array_key_exists('password', $credentials) && + \array_key_exists('from', $provider->getAttribute('options')) + ) { + $provider->setAttribute('enabled', true); + } else { + throw new Exception(Exception::PROVIDER_MISSING_CREDENTIALS); + } } else { - $enabled = false; + $provider->setAttribute('enabled', false); } - - $provider->setAttribute('enabled', $enabled); } $provider = $dbForProject->updateDocument('providers', $provider->getId(), $provider); @@ -1294,19 +1306,20 @@ App::patch('/v1/messaging/providers/textmagic/:providerId') $provider->setAttribute('credentials', $credentials); - if ($enabled === true || $enabled === false) { - if ( - $enabled === true - && \array_key_exists('username', $credentials) - && \array_key_exists('apiKey', $credentials) - && \array_key_exists('from', $provider->getAttribute('options')) - ) { - $enabled = true; + if (!\is_null($enabled)) { + if ($enabled) { + if ( + \array_key_exists('username', $credentials) && + \array_key_exists('apiKey', $credentials) && + \array_key_exists('from', $provider->getAttribute('options')) + ) { + $provider->setAttribute('enabled', true); + } else { + throw new Exception(Exception::PROVIDER_MISSING_CREDENTIALS); + } } else { - $enabled = false; + $provider->setAttribute('enabled', false); } - - $provider->setAttribute('enabled', $enabled); } $provider = $dbForProject->updateDocument('providers', $provider->getId(), $provider); @@ -1375,19 +1388,20 @@ App::patch('/v1/messaging/providers/twilio/:providerId') $provider->setAttribute('credentials', $credentials); - if ($enabled === true || $enabled === false) { - if ( - $enabled === true - && \array_key_exists('accountSid', $credentials) - && \array_key_exists('authToken', $credentials) - && \array_key_exists('from', $provider->getAttribute('options')) - ) { - $enabled = true; + if (!\is_null($enabled)) { + if ($enabled) { + if ( + \array_key_exists('accountSid', $credentials) && + \array_key_exists('authToken', $credentials) && + \array_key_exists('from', $provider->getAttribute('options')) + ) { + $provider->setAttribute('enabled', true); + } else { + throw new Exception(Exception::PROVIDER_MISSING_CREDENTIALS); + } } else { - $enabled = false; + $provider->setAttribute('enabled', false); } - - $provider->setAttribute('enabled', $enabled); } $provider = $dbForProject->updateDocument('providers', $provider->getId(), $provider); @@ -1456,19 +1470,20 @@ App::patch('/v1/messaging/providers/vonage/:providerId') $provider->setAttribute('credentials', $credentials); - if ($enabled === true || $enabled === false) { - if ( - $enabled === true - && \array_key_exists('apiKey', $credentials) - && \array_key_exists('apiSecret', $credentials) - && \array_key_exists('from', $provider->getAttribute('options')) - ) { - $enabled = true; + if (!\is_null($enabled)) { + if ($enabled) { + if ( + \array_key_exists('apiKey', $credentials) && + \array_key_exists('apiSecret', $credentials) && + \array_key_exists('from', $provider->getAttribute('options')) + ) { + $provider->setAttribute('enabled', true); + } else { + throw new Exception(Exception::PROVIDER_MISSING_CREDENTIALS); + } } else { - $enabled = false; + $provider->setAttribute('enabled', false); } - - $provider->setAttribute('enabled', $enabled); } $provider = $dbForProject->updateDocument('providers', $provider->getId(), $provider); @@ -1501,7 +1516,7 @@ App::patch('/v1/messaging/providers/fcm/:providerId') ->inject('queueForEvents') ->inject('dbForProject') ->inject('response') - ->action(function (string $providerId, string $name, ?bool $enabled, ?array $serviceAccountJSON, Event $queueForEvents, Database $dbForProject, Response $response) { + ->action(function (string $providerId, string $name, ?bool $enabled, array|string|null $serviceAccountJSON, Event $queueForEvents, Database $dbForProject, Response $response) { $provider = $dbForProject->getDocument('providers', $providerId); if ($provider->isEmpty()) { @@ -1518,17 +1533,25 @@ App::patch('/v1/messaging/providers/fcm/:providerId') } if (!\is_null($serviceAccountJSON)) { - $provider->setAttribute('credentials', ['serviceAccountJSON' => $serviceAccountJSON]); + $serviceAccountJSON = \is_string($serviceAccountJSON) + ? \json_decode($serviceAccountJSON, true) + : $serviceAccountJSON; + + $provider->setAttribute('credentials', [ + 'serviceAccountJSON' => $serviceAccountJSON + ]); } - if ($enabled === true || $enabled === false) { - if ($enabled === true && \array_key_exists('serviceAccountJSON', $provider->getAttribute('credentials'))) { - $enabled = true; + if (!\is_null($enabled)) { + if ($enabled) { + if (\array_key_exists('serviceAccountJSON', $provider->getAttribute('credentials'))) { + $provider->setAttribute('enabled', true); + } else { + throw new Exception(Exception::PROVIDER_MISSING_CREDENTIALS); + } } else { - $enabled = false; + $provider->setAttribute('enabled', false); } - - $provider->setAttribute('enabled', $enabled); } $provider = $dbForProject->updateDocument('providers', $provider->getId(), $provider); @@ -1601,20 +1624,21 @@ App::patch('/v1/messaging/providers/apns/:providerId') $provider->setAttribute('credentials', $credentials); - if ($enabled === true || $enabled === false) { - if ( - $enabled === true - && \array_key_exists('authKey', $credentials) - && \array_key_exists('authKeyId', $credentials) - && \array_key_exists('teamId', $credentials) - && \array_key_exists('bundleId', $credentials) - ) { - $enabled = true; + if (!\is_null($enabled)) { + if ($enabled) { + if ( + \array_key_exists('authKey', $credentials) && + \array_key_exists('authKeyId', $credentials) && + \array_key_exists('teamId', $credentials) && + \array_key_exists('bundleId', $credentials) + ) { + $provider->setAttribute('enabled', true); + } else { + throw new Exception(Exception::PROVIDER_MISSING_CREDENTIALS); + } } else { - $enabled = false; + $provider->setAttribute('enabled', false); } - - $provider->setAttribute('enabled', $enabled); } $provider = $dbForProject->updateDocument('providers', $provider->getId(), $provider); diff --git a/app/controllers/api/users.php b/app/controllers/api/users.php index 0f9a04639d..efe7e4fff1 100644 --- a/app/controllers/api/users.php +++ b/app/controllers/api/users.php @@ -1689,10 +1689,10 @@ App::delete('/v1/users/:userId/targets/:targetId') ->param('userId', '', new UID(), 'User ID.') ->param('targetId', '', new UID(), 'Target ID.') ->inject('queueForEvents') + ->inject('queueForDeletes') ->inject('response') ->inject('dbForProject') - ->action(function (string $userId, string $targetId, Event $queueForEvents, Response $response, Database $dbForProject) { - + ->action(function (string $userId, string $targetId, Event $queueForEvents, Delete $queueForDeletes, Response $response, Database $dbForProject) { $user = $dbForProject->getDocument('users', $userId); if ($user->isEmpty()) { @@ -1712,6 +1712,10 @@ App::delete('/v1/users/:userId/targets/:targetId') $dbForProject->deleteDocument('targets', $target->getId()); $dbForProject->purgeCachedDocument('users', $user->getId()); + $queueForDeletes + ->setType(DELETE_TYPE_TARGET) + ->setDocument($target); + $queueForEvents ->setParam('userId', $user->getId()) ->setParam('targetId', $target->getId()); diff --git a/app/init.php b/app/init.php index 0f2885744d..cf43739f0b 100644 --- a/app/init.php +++ b/app/init.php @@ -175,6 +175,7 @@ const DELETE_TYPE_CACHE_BY_RESOURCE = 'cacheByResource'; const DELETE_TYPE_SCHEDULES = 'schedules'; const DELETE_TYPE_TOPIC = 'topic'; const DELETE_TYPE_TARGET = 'target'; +const DELETE_TYPE_EXPIRED_TARGETS = 'invalid_targets'; // Mail Types const MAIL_TYPE_VERIFICATION = 'verification'; const MAIL_TYPE_MAGIC_SESSION = 'magicSession'; diff --git a/src/Appwrite/Extend/Exception.php b/src/Appwrite/Extend/Exception.php index 183f4a812a..d81589ed9d 100644 --- a/src/Appwrite/Extend/Exception.php +++ b/src/Appwrite/Extend/Exception.php @@ -244,7 +244,7 @@ class Exception extends \Exception public const PROVIDER_NOT_FOUND = 'provider_not_found'; public const PROVIDER_ALREADY_EXISTS = 'provider_already_exists'; public const PROVIDER_INCORRECT_TYPE = 'provider_incorrect_type'; - public const PROVIDER_INTERNAL_UPDATE_DISABLED = 'provider_internal_update_disabled'; + public const PROVIDER_MISSING_CREDENTIALS = 'provider_missing_credentials'; /** Topic */ public const TOPIC_NOT_FOUND = 'topic_not_found'; diff --git a/src/Appwrite/Platform/Tasks/Maintenance.php b/src/Appwrite/Platform/Tasks/Maintenance.php index 2980f13375..e46de1b522 100644 --- a/src/Appwrite/Platform/Tasks/Maintenance.php +++ b/src/Appwrite/Platform/Tasks/Maintenance.php @@ -59,6 +59,7 @@ class Maintenance extends Action $this->renewCertificates($dbForConsole, $queueForCertificates); $this->notifyDeleteCache($cacheRetention, $queueForDeletes); $this->notifyDeleteSchedules($schedulesDeletionRetention, $queueForDeletes); + $this->notifyDeleteTargets($queueForDeletes); }, $interval); } @@ -161,8 +162,7 @@ class Maintenance extends Action private function notifyDeleteCache($interval, Delete $queueForDeletes): void { - - ($queueForDeletes) + $queueForDeletes ->setType(DELETE_TYPE_CACHE_BY_TIMESTAMP) ->setDatetime(DateTime::addSeconds(new \DateTime(), -1 * $interval)) ->trigger(); @@ -170,10 +170,16 @@ class Maintenance extends Action private function notifyDeleteSchedules($interval, Delete $queueForDeletes): void { - - ($queueForDeletes) + $queueForDeletes ->setType(DELETE_TYPE_SCHEDULES) ->setDatetime(DateTime::addSeconds(new \DateTime(), -1 * $interval)) ->trigger(); } + + private function notifyDeleteTargets(Delete $queueForDeletes): void + { + $queueForDeletes + ->setType(DELETE_TYPE_EXPIRED_TARGETS) + ->trigger(); + } } diff --git a/src/Appwrite/Platform/Tasks/ScheduleBase.php b/src/Appwrite/Platform/Tasks/ScheduleBase.php index 1ec8e471bb..f0aa559a7f 100644 --- a/src/Appwrite/Platform/Tasks/ScheduleBase.php +++ b/src/Appwrite/Platform/Tasks/ScheduleBase.php @@ -73,6 +73,7 @@ abstract class ScheduleBase extends Action '$id' => $schedule->getId(), 'resourceId' => $schedule->getAttribute('resourceId'), 'schedule' => $schedule->getAttribute('schedule'), + 'active' => $schedule->getAttribute('active'), 'resourceUpdatedAt' => $schedule->getAttribute('resourceUpdatedAt'), 'project' => $project, // TODO: @Meldiron Send only ID to worker to reduce memory usage here 'resource' => $resource, // TODO: @Meldiron Send only ID to worker to reduce memory usage here diff --git a/src/Appwrite/Platform/Tasks/ScheduleMessages.php b/src/Appwrite/Platform/Tasks/ScheduleMessages.php index 19fc426637..cc641b434a 100644 --- a/src/Appwrite/Platform/Tasks/ScheduleMessages.php +++ b/src/Appwrite/Platform/Tasks/ScheduleMessages.php @@ -33,6 +33,10 @@ class ScheduleMessages extends ScheduleBase protected function enqueueResources(Group $pools, Database $dbForConsole): void { foreach ($this->schedules as $schedule) { + if (!$schedule['active']) { + continue; + } + $now = DateTime::now(); $scheduledAt = DateTime::formatTz($schedule['schedule']); @@ -44,24 +48,17 @@ class ScheduleMessages extends ScheduleBase $queue = $pools->get('queue')->pop(); $connection = $queue->getResource(); $queueForMessaging = new Messaging($connection); - $queueForDeletes = new Delete($connection); $queueForMessaging ->setMessageId($schedule['resourceId']) ->setProject($schedule['project']) ->trigger(); - $dbForConsole->updateDocument( + $dbForConsole->deleteDocument( 'schedules', $schedule['$id'], - new Document(['active' => false]) ); - $queueForDeletes - ->setType(DELETE_TYPE_SCHEDULES) - ->setDocument($schedule) - ->trigger(); - $queue->reclaim(); unset($this->schedules[$schedule['resourceId']]); diff --git a/src/Appwrite/Platform/Workers/Deletes.php b/src/Appwrite/Platform/Workers/Deletes.php index cbd63a4c6f..dd1949f845 100644 --- a/src/Appwrite/Platform/Workers/Deletes.php +++ b/src/Appwrite/Platform/Workers/Deletes.php @@ -122,11 +122,9 @@ class Deletes extends Action break; } break; - case DELETE_TYPE_EXECUTIONS: $this->deleteExecutionLogs($project, $getProjectDB, $executionRetention); break; - case DELETE_TYPE_AUDIT: if (!$project->isEmpty()) { $this->deleteAuditLogs($project, $getProjectDB, $auditRetention); @@ -139,11 +137,9 @@ class Deletes extends Action case DELETE_TYPE_ABUSE: $this->deleteAbuseLogs($project, $getProjectDB, $abuseRetention); break; - case DELETE_TYPE_REALTIME: $this->deleteRealtimeUsage($dbForConsole, $datetime); break; - case DELETE_TYPE_SESSIONS: $this->deleteExpiredSessions($project, $getProjectDB); break; @@ -157,17 +153,19 @@ class Deletes extends Action $this->deleteCacheByDate($project, $getProjectDB, $datetime); break; case DELETE_TYPE_SCHEDULES: - $this->deleteSchedules($dbForConsole, $getProjectDB, $datetime, $document); + $this->deleteSchedules($dbForConsole, $getProjectDB, $datetime); break; case DELETE_TYPE_TOPIC: $this->deleteTopic($project, $getProjectDB, $document); break; case DELETE_TYPE_TARGET: - $this->deleteTarget($project, $getProjectDB, $document); + $this->deleteTargetSubscribers($project, $getProjectDB, $document); + break; + case DELETE_TYPE_EXPIRED_TARGETS: + $this->deleteExpiredTargets($project, $getProjectDB); break; default: throw new \Exception('No delete operation for type: ' . \strval($type)); - break; } } @@ -183,13 +181,12 @@ class Deletes extends Action * @throws Structure * @throws DatabaseException */ - private function deleteSchedules(Database $dbForConsole, callable $getProjectDB, string $datetime, ?Document $document = null): void + private function deleteSchedules(Database $dbForConsole, callable $getProjectDB, string $datetime): void { $this->listByGroup( 'schedules', [ Query::equal('region', [App::getEnv('_APP_REGION', 'default')]), - Query::equal('resourceType', [$document->getAttribute('resourceType')]), Query::lessThanEqual('resourceUpdatedAt', $datetime), Query::equal('active', [false]), ], @@ -230,17 +227,20 @@ class Deletes extends Action * @param Document $topic * @throws Exception */ - protected function deleteTopic(Document $project, callable $getProjectDB, Document $topic) + private function deleteTopic(Document $project, callable $getProjectDB, Document $topic) { if ($topic->isEmpty()) { Console::error('Failed to delete subscribers. Topic not found'); return; } - $dbForProject = $getProjectDB($project); - $this->deleteByGroup('subscribers', [ - Query::equal('topicInternalId', [$topic->getInternalId()]) - ], $dbForProject); + $this->deleteByGroup( + 'subscribers', + [ + Query::equal('topicInternalId', [$topic->getInternalId()]) + ], + $getProjectDB($project) + ); } /** @@ -249,7 +249,7 @@ class Deletes extends Action * @param Document $target * @throws Exception */ - protected function deleteTarget(Document $project, callable $getProjectDB, Document $target) + private function deleteTargetSubscribers(Document $project, callable $getProjectDB, Document $target) { /** @var Database */ $dbForProject = $getProjectDB($project); @@ -272,6 +272,27 @@ class Deletes extends Action ); } + /** + * @param Document $project + * @param callable $getProjectDB + * @param Document $target + * @return void + * @throws Exception + */ + private function deleteExpiredTargets(Document $project, callable $getProjectDB) + { + $this->deleteByGroup( + 'targets', + [ + Query::equal('expired', [true]) + ], + $getProjectDB($project), + function (Document $target) use ($getProjectDB, $project) { + $this->deleteTargetSubscribers($project, $getProjectDB, $target); + } + ); + } + /** * @param Document $project * @param callable $getProjectDB @@ -613,14 +634,14 @@ class Deletes extends Action ], $dbForProject); // Delete targets - $this->listByGroup( + $this->deleteByGroup( 'targets', [ Query::equal('userInternalId', [$userInternalId]) ], $dbForProject, function (Document $target) use ($getProjectDB, $project) { - $this->deleteTarget($project, $getProjectDB, $target); + $this->deleteTargetSubscribers($project, $getProjectDB, $target); } ); } diff --git a/src/Appwrite/Platform/Workers/Messaging.php b/src/Appwrite/Platform/Workers/Messaging.php index e55ef12bd4..a5c8154b91 100644 --- a/src/Appwrite/Platform/Workers/Messaging.php +++ b/src/Appwrite/Platform/Workers/Messaging.php @@ -248,7 +248,11 @@ class Messaging extends Action ]); if ($target instanceof Document && !$target->isEmpty()) { - $dbForProject->deleteDocument('targets', $target->getId()); + $dbForProject->updateDocument( + 'targets', + $target->getId(), + $target->setAttribute('expired', true) + ); } } } diff --git a/tests/e2e/Services/Messaging/MessagingBase.php b/tests/e2e/Services/Messaging/MessagingBase.php index f766750ad5..e339aec6a2 100644 --- a/tests/e2e/Services/Messaging/MessagingBase.php +++ b/tests/e2e/Services/Messaging/MessagingBase.php @@ -186,6 +186,32 @@ trait MessagingBase return $providers; } + public function testUpdateProviderMissingCredentialsThrows(): void + { + // Create new FCM provider with no serviceAccountJSON + $response = $this->client->call(Client::METHOD_POST, '/messaging/providers/fcm', [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'], + ], [ + 'providerId' => ID::unique(), + 'name' => 'FCM3', + ]); + + $this->assertEquals(201, $response['headers']['status-code']); + + // Enable provider with no serviceAccountJSON + $response = $this->client->call(Client::METHOD_PATCH, '/messaging/providers/fcm/' . $response['body']['$id'], [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'], + ], [ + 'enabled' => true, + ]); + + $this->assertEquals(400, $response['headers']['status-code']); + } + /** * @depends testUpdateProviders */ @@ -198,7 +224,7 @@ trait MessagingBase ]); $this->assertEquals(200, $response['headers']['status-code']); - $this->assertEquals(\count($providers), \count($response['body']['providers'])); + $this->assertEquals(10, \count($response['body']['providers'])); return $providers; }