From 2667d1fb0747d19dcf35f380882d5a6e5cdbdfdb Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Wed, 21 Feb 2024 02:50:44 +1300 Subject: [PATCH] Inject always local file device --- app/worker.php | 4 ++ src/Appwrite/Platform/Workers/Messaging.php | 44 ++++++++++++--------- 2 files changed, 29 insertions(+), 19 deletions(-) diff --git a/app/worker.php b/app/worker.php index 9390a302f..c9da10628 100644 --- a/app/worker.php +++ b/app/worker.php @@ -218,6 +218,10 @@ Server::setResource('cacheDevice', function (Document $project) { return getDevice(APP_STORAGE_CACHE . '/app-' . $project->getId()); }, ['project']); +Server::setResource('filesLocalDevice', function (Document $project) { + return new Local(APP_STORAGE_UPLOADS . '/app-' . $project->getId()); +}, ['project']); + $pools = $register->get('pools'); $platform = new Appwrite(); $args = $_SERVER['argv']; diff --git a/src/Appwrite/Platform/Workers/Messaging.php b/src/Appwrite/Platform/Workers/Messaging.php index bf2c5d863..eab223404 100644 --- a/src/Appwrite/Platform/Workers/Messaging.php +++ b/src/Appwrite/Platform/Workers/Messaging.php @@ -59,9 +59,10 @@ class Messaging extends Action ->inject('message') ->inject('log') ->inject('dbForProject') - ->inject('getLocalCache') + ->inject('filesDevice') + ->inject('filesLocalDevice') ->inject('queueForUsage') - ->callback(fn(Message $message, Log $log, Database $dbForProject, callable $getLocalCache, Usage $queueForUsage) => $this->action($message, $log, $dbForProject, $getLocalCache, $queueForUsage)); + ->callback(fn(Message $message, Log $log, Database $dbForProject, Device $filesDevice, Device $filesLocalDevice, Usage $queueForUsage) => $this->action($message, $log, $dbForProject, $filesDevice, $filesLocalDevice, $queueForUsage)); } /** @@ -77,7 +78,8 @@ class Messaging extends Action Message $message, Log $log, Database $dbForProject, - callable $getLocalCache, + Device $filesDevice, + Device $filesLocalDevice, Usage $queueForUsage ): void { $payload = $message->getPayload() ?? []; @@ -99,15 +101,19 @@ class Messaging extends Action case MESSAGE_SEND_TYPE_EXTERNAL: $message = $dbForProject->getDocument('messages', $payload['messageId']); - $this->sendExternalMessage($dbForProject, $message); + $this->sendExternalMessage($dbForProject, $message, $filesDevice, $filesLocalDevice,); break; default: throw new Exception('Unknown message type: ' . $type); } } - private function sendExternalMessage(Database $dbForProject, Document $message): void - { + private function sendExternalMessage( + Database $dbForProject, + Document $message, + Device $filesDevice, + Device $filesLocalDevice, + ): void { $topicIds = $message->getAttribute('topics', []); $targetIds = $message->getAttribute('targets', []); $userIds = $message->getAttribute('users', []); @@ -211,8 +217,8 @@ class Messaging extends Action /** * @var array $results */ - $results = batch(\array_map(function ($providerId) use ($identifiers, $providers, $fallback, $message, $dbForProject, $localCache, $deviceFiles) { - return function () use ($providerId, $identifiers, $providers, $fallback, $message, $dbForProject, $localCache, $deviceFiles) { + $results = batch(\array_map(function ($providerId) use ($identifiers, $providers, $fallback, $message, $dbForProject, $filesDevice, $filesLocalDevice) { + return function () use ($providerId, $identifiers, $providers, $fallback, $message, $dbForProject, $filesDevice, $filesLocalDevice) { if (\array_key_exists($providerId, $providers)) { $provider = $providers[$providerId]; } else { @@ -238,8 +244,8 @@ class Messaging extends Action $batches = \array_chunk($identifiers, $maxBatchSize); $batchIndex = 0; - return batch(\array_map(function ($batch) use ($message, $provider, $adapter, &$batchIndex, $dbForProject, $localCache, $deviceFiles) { - return function () use ($batch, $message, $provider, $adapter, &$batchIndex, $dbForProject, $localCache, $deviceFiles) { + return batch(\array_map(function ($batch) use ($message, $provider, $adapter, &$batchIndex, $dbForProject, $filesDevice, $filesLocalDevice) { + return function () use ($batch, $message, $provider, $adapter, &$batchIndex, $dbForProject, $filesDevice, $filesLocalDevice) { $deliveredTotal = 0; $deliveryErrors = []; $messageData = clone $message; @@ -248,7 +254,7 @@ class Messaging extends Action $data = match ($provider->getAttribute('type')) { MESSAGE_TYPE_SMS => $this->buildSmsMessage($messageData, $provider), MESSAGE_TYPE_PUSH => $this->buildPushMessage($messageData), - MESSAGE_TYPE_EMAIL => $this->buildEmailMessage($dbForProject, $messageData, $provider, $deviceFiles, $localCache), + MESSAGE_TYPE_EMAIL => $this->buildEmailMessage($dbForProject, $messageData, $provider, $filesDevice, $filesLocalDevice), default => throw new Exception(Exception::PROVIDER_INCORRECT_TYPE) }; @@ -325,7 +331,7 @@ class Messaging extends Action // Delete any attachments that were downloaded to the local cache if ($provider->getAttribute('type') === MESSAGE_TYPE_EMAIL) { - if ($deviceFiles->getType() === Storage::DEVICE_LOCAL) { + if ($filesDevice->getType() === Storage::DEVICE_LOCAL) { return; } @@ -348,8 +354,8 @@ class Messaging extends Action $path = $file->getAttribute('path', ''); - if ($localCache->exists($path)) { - $localCache->delete($path); + if ($filesLocalDevice->exists($path)) { + $filesLocalDevice->delete($path); } } } @@ -506,8 +512,8 @@ class Messaging extends Action Database $dbForProject, Document $message, Document $provider, - Device $deviceFiles, - Device $localCache, + Device $filesDevice, + Device $filesLocalDevice, ): Email { $fromName = $provider['options']['fromName'] ?? null; $fromEmail = $provider['options']['fromEmail'] ?? null; @@ -558,7 +564,7 @@ class Messaging extends Action $mimes = Config::getParam('storage-mimes'); $path = $file->getAttribute('path', ''); - if (!$deviceFiles->exists($path)) { + if (!$filesDevice->exists($path)) { throw new Exception(Exception::STORAGE_FILE_NOT_FOUND, 'File not found in ' . $path); } @@ -568,8 +574,8 @@ class Messaging extends Action $contentType = $file->getAttribute('mimeType'); } - if ($deviceFiles->getType() !== Storage::DEVICE_LOCAL) { - $deviceFiles->transfer($path, $path, $localCache); + if ($filesDevice->getType() !== Storage::DEVICE_LOCAL) { + $filesDevice->transfer($path, $path, $filesLocalDevice); } $attachment = new Attachment(