From c5aaa670a90d2de4e29d51ce64e7abd82a54561b Mon Sep 17 00:00:00 2001 From: prateek banga Date: Tue, 14 Nov 2023 15:20:21 +0530 Subject: [PATCH] adds provider details in message search attribute --- app/controllers/api/messaging.php | 13 ++++----- app/init.php | 18 +++++++------ src/Appwrite/Platform/Workers/Messaging.php | 29 +++++++++++++++------ 3 files changed, 38 insertions(+), 22 deletions(-) diff --git a/app/controllers/api/messaging.php b/app/controllers/api/messaging.php index 7bac2abf88..2cc9639fbd 100644 --- a/app/controllers/api/messaging.php +++ b/app/controllers/api/messaging.php @@ -1,5 +1,6 @@ label('sdk.response.model', Response::MODEL_PROVIDER) ->param('providerId', '', new CustomId(), 'Provider ID. Choose a custom ID or generate a random ID with `ID.unique()`. Valid chars are a-z, A-Z, 0-9, period, hyphen, and underscore. Can\'t start with a special char. Max length is 36 chars.') ->param('name', '', new Text(128), 'Provider name.') - ->param('from', '', new Text(256), 'Sender email address.') + ->param('from', '', new Email(), 'Sender email address.') ->param('apiKey', '', new Text(0), 'Sendgrid API key.') ->param('enabled', true, new Boolean(), 'Set as enabled.', true) ->inject('queueForEvents') @@ -180,7 +181,7 @@ App::post('/v1/messaging/providers/msg91') ->label('sdk.response.model', Response::MODEL_PROVIDER) ->param('providerId', '', new CustomId(), 'Provider ID. Choose a custom ID or generate a random ID with `ID.unique()`. Valid chars are a-z, A-Z, 0-9, period, hyphen, and underscore. Can\'t start with a special char. Max length is 36 chars.') ->param('name', '', new Text(128), 'Provider name.') - ->param('from', '', new Text(256), 'Sender number.') + ->param('from', '', new Phone(), 'Sender Phone number. Format this number with a leading \'+\' and a country code, e.g., +16175551212.') ->param('senderId', '', new Text(0), 'Msg91 Sender ID.') ->param('authKey', '', new Text(0), 'Msg91 Auth Key.') ->param('enabled', true, new Boolean(), 'Set as enabled.', true) @@ -244,7 +245,7 @@ App::post('/v1/messaging/providers/telesign') ->label('sdk.response.model', Response::MODEL_PROVIDER) ->param('providerId', '', new CustomId(), 'Provider ID. Choose a custom ID or generate a random ID with `ID.unique()`. Valid chars are a-z, A-Z, 0-9, period, hyphen, and underscore. Can\'t start with a special char. Max length is 36 chars.') ->param('name', '', new Text(128), 'Provider name.') - ->param('from', '', new Text(256), 'Sender number.') + ->param('from', '', new Phone(), 'Sender Phone number. Format this number with a leading \'+\' and a country code, e.g., +16175551212.') ->param('username', '', new Text(0), 'Telesign username.') ->param('password', '', new Text(0), 'Telesign password.') ->param('enabled', true, new Boolean(), 'Set as enabled.', true) @@ -308,7 +309,7 @@ App::post('/v1/messaging/providers/textmagic') ->label('sdk.response.model', Response::MODEL_PROVIDER) ->param('providerId', '', new CustomId(), 'Provider ID. Choose a custom ID or generate a random ID with `ID.unique()`. Valid chars are a-z, A-Z, 0-9, period, hyphen, and underscore. Can\'t start with a special char. Max length is 36 chars.') ->param('name', '', new Text(128), 'Provider name.') - ->param('from', '', new Text(256), 'Sender number.') + ->param('from', '', new Phone(), 'Sender Phone number. Format this number with a leading \'+\' and a country code, e.g., +16175551212.') ->param('username', '', new Text(0), 'Textmagic username.') ->param('apiKey', '', new Text(0), 'Textmagic apiKey.') ->param('enabled', true, new Boolean(), 'Set as enabled.', true) @@ -372,7 +373,7 @@ App::post('/v1/messaging/providers/twilio') ->label('sdk.response.model', Response::MODEL_PROVIDER) ->param('providerId', '', new CustomId(), 'Provider ID. Choose a custom ID or generate a random ID with `ID.unique()`. Valid chars are a-z, A-Z, 0-9, period, hyphen, and underscore. Can\'t start with a special char. Max length is 36 chars.') ->param('name', '', new Text(128), 'Provider name.') - ->param('from', '', new Text(256), 'Sender number.') + ->param('from', '', new Phone(), 'Sender Phone number. Format this number with a leading \'+\' and a country code, e.g., +16175551212.') ->param('accountSid', '', new Text(0), 'Twilio account secret ID.') ->param('authToken', '', new Text(0), 'Twilio authentication token.') ->param('enabled', true, new Boolean(), 'Set as enabled.', true) @@ -436,7 +437,7 @@ App::post('/v1/messaging/providers/vonage') ->label('sdk.response.model', Response::MODEL_PROVIDER) ->param('providerId', '', new CustomId(), 'Provider ID. Choose a custom ID or generate a random ID with `ID.unique()`. Valid chars are a-z, A-Z, 0-9, period, hyphen, and underscore. Can\'t start with a special char. Max length is 36 chars.') ->param('name', '', new Text(128), 'Provider name.') - ->param('from', '', new Text(256), 'Sender number.') + ->param('from', '', new Phone(), 'Sender Phone number. Format this number with a leading \'+\' and a country code, e.g., +16175551212.') ->param('apiKey', '', new Text(0), 'Vonage API key.') ->param('apiSecret', '', new Text(0), 'Vonage API secret.') ->param('enabled', true, new Boolean(), 'Set as enabled.', true) diff --git a/app/init.php b/app/init.php index 2e0aa36eb0..adac72ed50 100644 --- a/app/init.php +++ b/app/init.php @@ -568,7 +568,7 @@ Database::addFilter( $provider->getAttribute('type', '') ]; - $search = implode(' ', \array_filter($searchValues)); + $search = \implode(' ', \array_filter($searchValues)); return $search; }, @@ -586,7 +586,7 @@ Database::addFilter( $topic->getAttribute('description', ''), ]; - $search = implode(' ', \array_filter($searchValues)); + $search = \implode(' ', \array_filter($searchValues)); return $search; }, @@ -604,15 +604,17 @@ Database::addFilter( $message->getAttribute('status', ''), ]; - if (\array_key_exists('subject', $message->getAttribute('data'))) { - $searchValues[] = \array_merge($searchValues, [$message->getAttribute('data')['subject'], 'email']); - } else if (\array_key_exists('content', $message->getAttribute('data'))) { - $searchValues[] = \array_merge($searchValues, [$message->getAttribute('data')['content'], 'sms']); + $data = \json_decode($message->getAttribute('data', []), true); + + if (\array_key_exists('subject', $data)) { + $searchValues = \array_merge($searchValues, [$data['subject'], 'email']); + } elseif (\array_key_exists('content', $data)) { + $searchValues = \array_merge($searchValues, [$data['content'], 'sms']); } else { - $searchValues[] = \array_merge($searchValues, [$message->getAttribute('data')['title'], 'push']); + $searchValues = \array_merge($searchValues, [$data['title'], 'push']); } - $search = implode(' ', \array_filter($searchValues)); + $search = \implode(' ', \array_filter($searchValues)); return $search; }, diff --git a/src/Appwrite/Platform/Workers/Messaging.php b/src/Appwrite/Platform/Workers/Messaging.php index 63e0ff651a..dcef84c2df 100644 --- a/src/Appwrite/Platform/Workers/Messaging.php +++ b/src/Appwrite/Platform/Workers/Messaging.php @@ -10,7 +10,6 @@ use Utopia\Database\Database; use Utopia\Database\DateTime; use Utopia\Database\Document; use Utopia\Database\Query; -use Utopia\Database\Validator\Authorization; use Utopia\Messaging\Adapters\SMS as SMSAdapter; use Utopia\Messaging\Adapters\SMS\Mock; use Utopia\Messaging\Adapters\SMS\Msg91; @@ -99,22 +98,31 @@ class Messaging extends Action $recipients = \array_merge($recipients, $targets); } + /** + * @var array> $identifiersByProviderId + */ + $identifiersByProviderId = []; + + /** + * @var Document[] $providers + */ $providers = []; foreach ($recipients as $recipient) { $providerId = $recipient->getAttribute('providerId'); - if (!isset($providers[$providerId])) { - $providers[$providerId] = []; + if (!isset($identifiersByProviderId[$providerId])) { + $identifiersByProviderId[$providerId] = []; } - $providers[$providerId][] = $recipient->getAttribute('identifier'); + $identifiersByProviderId[$providerId][] = $recipient->getAttribute('identifier'); } /** * @var array[] $results */ - $results = batch(\array_map(function ($providerId) use ($providers, $message, $dbForProject) { - return function () use ($providerId, $providers, $message, $dbForProject) { + $results = batch(\array_map(function ($providerId) use ($identifiersByProviderId, $providers, $message, $dbForProject) { + return function () use ($providerId, $identifiersByProviderId, $providers, $message, $dbForProject) { $provider = $dbForProject->getDocument('providers', $providerId); - $identifiers = $providers[$providerId]; + $providers[] = $provider; + $identifiers = $identifiersByProviderId[$providerId]; $adapter = match ($provider->getAttribute('type')) { 'sms' => $this->sms($provider), 'push' => $this->push($provider), @@ -154,7 +162,7 @@ class Messaging extends Action return $results; }; - }, \array_keys($providers))); + }, \array_keys($identifiersByProviderId))); $results = array_merge(...$results); @@ -172,6 +180,11 @@ class Messaging extends Action $message->setAttribute('status', 'sent'); } $message->removeAttribute('to'); + + foreach ($providers as $provider) { + $message->setAttribute('search', "{$message->getAttribute('search')} {$provider->getAttribute('name')} {$provider->getAttribute('provider')} {$provider->getAttribute('type')}"); + } + $message->setAttribute('deliveredTotal', $deliveredTotal); $message->setAttribute('deliveredAt', DateTime::now());