adds provider details in message search attribute

This commit is contained in:
prateek banga
2023-11-14 15:20:21 +05:30
parent f84a00dded
commit c5aaa670a9
3 changed files with 38 additions and 22 deletions
+7 -6
View File
@@ -1,5 +1,6 @@
<?php
use Appwrite\Auth\Validator\Phone;
use Appwrite\Detector\Detector;
use Appwrite\Event\Delete;
use Appwrite\Event\Event;
@@ -118,7 +119,7 @@ App::post('/v1/messaging/providers/sendgrid')
->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)
+10 -8
View File
@@ -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;
},
+21 -8
View File
@@ -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<string, array<string>> $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());