1
0
Fork 0
mirror of synced 2024-07-04 14:10:33 +12:00

Update create and update message APIs to support draft messages

This commit is contained in:
Steven Nguyen 2024-01-10 17:15:37 -08:00
parent 34af90b65e
commit 6eeb8c4cac
No known key found for this signature in database

View file

@ -2,6 +2,7 @@
use Appwrite\Auth\Validator\Phone; use Appwrite\Auth\Validator\Phone;
use Appwrite\Detector\Detector; use Appwrite\Detector\Detector;
use Appwrite\Enum\MessageStatus;
use Appwrite\Event\Delete; use Appwrite\Event\Delete;
use Appwrite\Event\Event; use Appwrite\Event\Event;
use Appwrite\Event\Messaging; use Appwrite\Event\Messaging;
@ -2282,7 +2283,7 @@ App::post('/v1/messaging/messages/email')
->param('cc', [], new ArrayList(new UID()), 'Array of target IDs to be added as CC.', true) ->param('cc', [], new ArrayList(new UID()), 'Array of target IDs to be added as CC.', true)
->param('bcc', [], new ArrayList(new UID()), 'Array of target IDs to be added as BCC.', true) ->param('bcc', [], new ArrayList(new UID()), 'Array of target IDs to be added as BCC.', true)
->param('description', '', new Text(256), 'Description for message.', true) ->param('description', '', new Text(256), 'Description for message.', true)
->param('status', 'processing', new WhiteList(['draft', 'canceled', 'processing']), 'Message Status. Value must be either draft or cancelled or processing.', true) ->param('status', MessageStatus::DRAFT, new WhiteList([MessageStatus::DRAFT, MessageStatus::SCHEDULED, MessageStatus::PROCESSING]), 'Message Status. Value must be one of: ' . implode(', ', [MessageStatus::DRAFT, MessageStatus::SCHEDULED, MessageStatus::PROCESSING]) . '.', true)
->param('html', false, new Boolean(), 'Is content of type HTML', true) ->param('html', false, new Boolean(), 'Is content of type HTML', true)
->param('scheduledAt', null, new DatetimeValidator(requireDateInFuture: true), 'Scheduled delivery time for message in [ISO 8601](https://www.iso.org/iso-8601-date-and-time-format.html) format. DateTime value must be in future.', true) ->param('scheduledAt', null, new DatetimeValidator(requireDateInFuture: true), 'Scheduled delivery time for message in [ISO 8601](https://www.iso.org/iso-8601-date-and-time-format.html) format. DateTime value must be in future.', true)
->inject('queueForEvents') ->inject('queueForEvents')
@ -2295,7 +2296,7 @@ App::post('/v1/messaging/messages/email')
? ID::unique() ? ID::unique()
: $messageId; : $messageId;
if (\count($topics) === 0 && \count($users) === 0 && \count($targets) === 0) { if ($status !== MessageStatus::DRAFT && \count($topics) === 0 && \count($users) === 0 && \count($targets) === 0) {
throw new Exception(Exception::MESSAGE_MISSING_TARGET); throw new Exception(Exception::MESSAGE_MISSING_TARGET);
} }
@ -2336,7 +2337,7 @@ App::post('/v1/messaging/messages/email')
'status' => $status, 'status' => $status,
])); ]));
if ($status === 'processing') { if ($status === MessageStatus::PROCESSING) {
$queueForMessaging $queueForMessaging
->setMessageId($message->getId()) ->setMessageId($message->getId())
->setProject($project) ->setProject($project)
@ -2371,7 +2372,7 @@ App::post('/v1/messaging/messages/sms')
->param('users', [], new ArrayList(new UID()), 'List of User IDs.', true) ->param('users', [], new ArrayList(new UID()), 'List of User IDs.', true)
->param('targets', [], new ArrayList(new UID()), 'List of Targets IDs.', true) ->param('targets', [], new ArrayList(new UID()), 'List of Targets IDs.', true)
->param('description', '', new Text(256), 'Description for Message.', true) ->param('description', '', new Text(256), 'Description for Message.', true)
->param('status', 'processing', new WhiteList(['draft', 'canceled', 'processing']), 'Message Status. Value must be either draft or cancelled or processing.', true) ->param('status', MessageStatus::DRAFT, new WhiteList([MessageStatus::DRAFT, MessageStatus::SCHEDULED, MessageStatus::PROCESSING]), 'Message Status. Value must be one of: ' . implode(', ', [MessageStatus::DRAFT, MessageStatus::SCHEDULED, MessageStatus::PROCESSING]) . '.', true)
->param('scheduledAt', null, new DatetimeValidator(requireDateInFuture: true), 'Scheduled delivery time for message in [ISO 8601](https://www.iso.org/iso-8601-date-and-time-format.html) format. DateTime value must be in future.', true) ->param('scheduledAt', null, new DatetimeValidator(requireDateInFuture: true), 'Scheduled delivery time for message in [ISO 8601](https://www.iso.org/iso-8601-date-and-time-format.html) format. DateTime value must be in future.', true)
->inject('queueForEvents') ->inject('queueForEvents')
->inject('dbForProject') ->inject('dbForProject')
@ -2383,23 +2384,25 @@ App::post('/v1/messaging/messages/sms')
? ID::unique() ? ID::unique()
: $messageId; : $messageId;
if (\count($topics) === 0 && \count($users) === 0 && \count($targets) === 0) { if ($status !== MessageStatus::DRAFT && \count($topics) === 0 && \count($users) === 0 && \count($targets) === 0) {
throw new Exception(Exception::MESSAGE_MISSING_TARGET); throw new Exception(Exception::MESSAGE_MISSING_TARGET);
} }
$foundTargets = $dbForProject->find('targets', [ if (!empty($targets)) {
Query::equal('$id', $targets), $foundTargets = $dbForProject->find('targets', [
Query::equal('providerType', [MESSAGE_TYPE_SMS]), Query::equal('$id', $targets),
Query::limit(\count($targets)), Query::equal('providerType', [MESSAGE_TYPE_SMS]),
]); Query::limit(\count($targets)),
]);
if (\count($foundTargets) !== \count($targets)) { if (\count($foundTargets) !== \count($targets)) {
throw new Exception(Exception::MESSAGE_TARGET_NOT_SMS); throw new Exception(Exception::MESSAGE_TARGET_NOT_SMS);
} }
foreach ($foundTargets as $target) { foreach ($foundTargets as $target) {
if ($target->isEmpty()) { if ($target->isEmpty()) {
throw new Exception(Exception::USER_TARGET_NOT_FOUND); throw new Exception(Exception::USER_TARGET_NOT_FOUND);
}
} }
} }
@ -2416,7 +2419,7 @@ App::post('/v1/messaging/messages/sms')
'status' => $status, 'status' => $status,
])); ]));
if ($status === 'processing') { if ($status === MessageStatus::PROCESSING) {
$queueForMessaging $queueForMessaging
->setMessageId($message->getId()) ->setMessageId($message->getId())
->setProject($project) ->setProject($project)
@ -2459,7 +2462,7 @@ App::post('/v1/messaging/messages/push')
->param('color', '', new Text(256), 'Color for push notification. Available only for Android Platform.', true) ->param('color', '', new Text(256), 'Color for push notification. Available only for Android Platform.', true)
->param('tag', '', new Text(256), 'Tag for push notification. Available only for Android Platform.', true) ->param('tag', '', new Text(256), 'Tag for push notification. Available only for Android Platform.', true)
->param('badge', '', new Text(256), 'Badge for push notification. Available only for IOS Platform.', true) ->param('badge', '', new Text(256), 'Badge for push notification. Available only for IOS Platform.', true)
->param('status', 'processing', new WhiteList(['draft', 'canceled', 'processing']), 'Message Status. Value must be either draft or cancelled or processing.', true) ->param('status', MessageStatus::DRAFT, new WhiteList([MessageStatus::DRAFT, MessageStatus::SCHEDULED, MessageStatus::PROCESSING]), 'Message Status. Value must be one of: ' . implode(', ', [MessageStatus::DRAFT, MessageStatus::SCHEDULED, MessageStatus::PROCESSING]) . '.', true)
->param('scheduledAt', null, new DatetimeValidator(requireDateInFuture: true), 'Scheduled delivery time for message in [ISO 8601](https://www.iso.org/iso-8601-date-and-time-format.html) format. DateTime value must be in future.', true) ->param('scheduledAt', null, new DatetimeValidator(requireDateInFuture: true), 'Scheduled delivery time for message in [ISO 8601](https://www.iso.org/iso-8601-date-and-time-format.html) format. DateTime value must be in future.', true)
->inject('queueForEvents') ->inject('queueForEvents')
->inject('dbForProject') ->inject('dbForProject')
@ -2471,23 +2474,25 @@ App::post('/v1/messaging/messages/push')
? ID::unique() ? ID::unique()
: $messageId; : $messageId;
if (\count($topics) === 0 && \count($users) === 0 && \count($targets) === 0) { if ($status !== MessageStatus::DRAFT && \count($topics) === 0 && \count($users) === 0 && \count($targets) === 0) {
throw new Exception(Exception::MESSAGE_MISSING_TARGET); throw new Exception(Exception::MESSAGE_MISSING_TARGET);
} }
$foundTargets = $dbForProject->find('targets', [ if (!empty($targets)) {
Query::equal('$id', $targets), $foundTargets = $dbForProject->find('targets', [
Query::equal('providerType', [MESSAGE_TYPE_PUSH]), Query::equal('$id', $targets),
Query::limit(\count($targets)), Query::equal('providerType', [MESSAGE_TYPE_PUSH]),
]); Query::limit(\count($targets)),
]);
if (\count($foundTargets) !== \count($targets)) { if (\count($foundTargets) !== \count($targets)) {
throw new Exception(Exception::MESSAGE_TARGET_NOT_PUSH); throw new Exception(Exception::MESSAGE_TARGET_NOT_PUSH);
} }
foreach ($foundTargets as $target) { foreach ($foundTargets as $target) {
if ($target->isEmpty()) { if ($target->isEmpty()) {
throw new Exception(Exception::USER_TARGET_NOT_FOUND); throw new Exception(Exception::USER_TARGET_NOT_FOUND);
}
} }
} }
@ -2513,7 +2518,7 @@ App::post('/v1/messaging/messages/push')
'status' => $status, 'status' => $status,
])); ]));
if ($status === 'processing') { if ($status === MessageStatus::PROCESSING) {
$queueForMessaging $queueForMessaging
->setMessageId($message->getId()) ->setMessageId($message->getId())
->setProject($project) ->setProject($project)
@ -2700,7 +2705,7 @@ App::patch('/v1/messaging/messages/email/:messageId')
->param('subject', null, new Text(998), 'Email Subject.', true) ->param('subject', null, new Text(998), 'Email Subject.', true)
->param('description', null, new Text(256), 'Description for Message.', true) ->param('description', null, new Text(256), 'Description for Message.', true)
->param('content', null, new Text(64230), 'Email Content.', true) ->param('content', null, new Text(64230), 'Email Content.', true)
->param('status', null, new WhiteList(['draft', 'cancelled', 'processing']), 'Message Status. Value must be either draft or cancelled or processing.', true) ->param('status', MessageStatus::DRAFT, new WhiteList([MessageStatus::DRAFT, MessageStatus::SCHEDULED, MessageStatus::PROCESSING]), 'Message Status. Value must be one of: ' . implode(', ', [MessageStatus::DRAFT, MessageStatus::SCHEDULED, MessageStatus::PROCESSING]) . '.', true)
->param('html', null, new Boolean(), 'Is content of type HTML', true) ->param('html', null, new Boolean(), 'Is content of type HTML', true)
->param('cc', null, new ArrayList(new UID()), 'Array of target IDs to be added as CC.', true) ->param('cc', null, new ArrayList(new UID()), 'Array of target IDs to be added as CC.', true)
->param('bcc', null, new ArrayList(new UID()), 'Array of target IDs to be added as BCC.', true) ->param('bcc', null, new ArrayList(new UID()), 'Array of target IDs to be added as BCC.', true)
@ -2717,7 +2722,7 @@ App::patch('/v1/messaging/messages/email/:messageId')
throw new Exception(Exception::MESSAGE_NOT_FOUND); throw new Exception(Exception::MESSAGE_NOT_FOUND);
} }
if ($message->getAttribute('status') === 'sent') { if ($message->getAttribute('status') === MessageStatus::SENT) {
throw new Exception(Exception::MESSAGE_ALREADY_SENT); throw new Exception(Exception::MESSAGE_ALREADY_SENT);
} }
@ -2736,17 +2741,19 @@ App::patch('/v1/messaging/messages/email/:messageId')
if (!\is_null($targets) || !\is_null($cc) || !\is_null($bcc)) { if (!\is_null($targets) || !\is_null($cc) || !\is_null($bcc)) {
$mergedTargets = \array_merge(...\array_filter([$targets, $cc, $bcc])); $mergedTargets = \array_merge(...\array_filter([$targets, $cc, $bcc]));
$foundTargets = $dbForProject->find('targets', [ if (!empty($mergedTargets)) {
Query::equal('$id', $mergedTargets), $foundTargets = $dbForProject->find('targets', [
Query::equal('providerType', [MESSAGE_TYPE_EMAIL]), Query::equal('$id', $mergedTargets),
Query::limit(\count($mergedTargets)), Query::equal('providerType', [MESSAGE_TYPE_EMAIL]),
]); Query::limit(\count($mergedTargets)),
if (\count($foundTargets) !== \count($mergedTargets)) { ]);
throw new Exception(Exception::MESSAGE_TARGET_NOT_EMAIL); if (\count($foundTargets) !== \count($mergedTargets)) {
} throw new Exception(Exception::MESSAGE_TARGET_NOT_EMAIL);
foreach ($foundTargets as $target) { }
if ($target->isEmpty()) { foreach ($foundTargets as $target) {
throw new Exception(Exception::USER_TARGET_NOT_FOUND); if ($target->isEmpty()) {
throw new Exception(Exception::USER_TARGET_NOT_FOUND);
}
} }
} }
} }
@ -2793,7 +2800,7 @@ App::patch('/v1/messaging/messages/email/:messageId')
$message = $dbForProject->updateDocument('messages', $message->getId(), $message); $message = $dbForProject->updateDocument('messages', $message->getId(), $message);
if ($status === 'processing') { if ($status === MessageStatus::PROCESSING) {
$queueForMessaging $queueForMessaging
->setMessageId($message->getId()) ->setMessageId($message->getId())
->setProject($project) ->setProject($project)