mirror of
https://github.com/appwrite/appwrite.git
synced 2026-11-18 20:36:03 +00:00
feat: add messaging resource migration support
This commit is contained in:
1 parent
c2d016944c
commit
3583aac63b
16 files changed
+1340
No files matched your search
@@ -10,8 +10,10 @@ use Appwrite\Enums\Compression;
|
||||
use Appwrite\Enums\Framework;
|
||||
use Appwrite\Enums\PasswordHash;
|
||||
use Appwrite\Enums\Runtime;
|
||||
use Appwrite\Enums\SmtpEncryption;
|
||||
use Appwrite\InputFile;
|
||||
use Appwrite\Services\Functions;
|
||||
use Appwrite\Services\Messaging;
|
||||
use Appwrite\Services\Sites;
|
||||
use Appwrite\Services\Storage;
|
||||
use Appwrite\Services\Teams;
|
||||
@@ -27,6 +29,7 @@ use Utopia\Database\Exception\Limit as LimitException;
|
||||
use Utopia\Database\Exception\Structure as StructureException;
|
||||
use Utopia\Database\Helpers\ID;
|
||||
use Utopia\Database\Helpers\Permission;
|
||||
use Utopia\Database\Helpers\Role;
|
||||
use Utopia\Database\Query;
|
||||
use Utopia\Database\Validator\Index as IndexValidator;
|
||||
use Utopia\Database\Validator\Structure;
|
||||
@@ -46,6 +49,10 @@ use Utopia\Migration\Resources\Database\Table;
|
||||
use Utopia\Migration\Resources\Functions\Deployment;
|
||||
use Utopia\Migration\Resources\Functions\EnvVar;
|
||||
use Utopia\Migration\Resources\Functions\Func;
|
||||
use Utopia\Migration\Resources\Messaging\Message;
|
||||
use Utopia\Migration\Resources\Messaging\Provider;
|
||||
use Utopia\Migration\Resources\Messaging\Subscriber;
|
||||
use Utopia\Migration\Resources\Messaging\Topic;
|
||||
use Utopia\Migration\Resources\Sites\Deployment as SiteDeployment;
|
||||
use Utopia\Migration\Resources\Sites\EnvVar as SiteEnvVar;
|
||||
use Utopia\Migration\Resources\Sites\Site;
|
||||
@@ -61,6 +68,7 @@ class Appwrite extends Destination
|
||||
protected string $key;
|
||||
|
||||
private Functions $functions;
|
||||
private Messaging $messaging;
|
||||
private Sites $sites;
|
||||
private Storage $storage;
|
||||
private Teams $teams;
|
||||
@@ -95,6 +103,7 @@ class Appwrite extends Destination
|
||||
->setKey($key);
|
||||
|
||||
$this->functions = new Functions($this->client);
|
||||
$this->messaging = new Messaging($this->client);
|
||||
$this->sites = new Sites($this->client);
|
||||
$this->storage = new Storage($this->client);
|
||||
$this->teams = new Teams($this->client);
|
||||
@@ -138,6 +147,12 @@ class Appwrite extends Destination
|
||||
Resource::TYPE_DEPLOYMENT,
|
||||
Resource::TYPE_ENVIRONMENT_VARIABLE,
|
||||
|
||||
// Messaging
|
||||
Resource::TYPE_PROVIDER,
|
||||
Resource::TYPE_TOPIC,
|
||||
Resource::TYPE_SUBSCRIBER,
|
||||
Resource::TYPE_MESSAGE,
|
||||
|
||||
// Sites
|
||||
Resource::TYPE_SITE,
|
||||
Resource::TYPE_SITE_DEPLOYMENT,
|
||||
@@ -213,6 +228,39 @@ class Appwrite extends Destination
|
||||
$this->functions->create('', '', Runtime::NODE180());
|
||||
}
|
||||
|
||||
// Messaging
|
||||
if (\in_array(Resource::TYPE_PROVIDER, $resources)) {
|
||||
$scope = 'providers.read';
|
||||
$this->messaging->listProviders();
|
||||
|
||||
$scope = 'providers.write';
|
||||
$this->messaging->createSendgridProvider('', '');
|
||||
}
|
||||
|
||||
if (\in_array(Resource::TYPE_TOPIC, $resources)) {
|
||||
$scope = 'topics.read';
|
||||
$this->messaging->listTopics();
|
||||
|
||||
$scope = 'topics.write';
|
||||
$this->messaging->createTopic('', '');
|
||||
}
|
||||
|
||||
if (\in_array(Resource::TYPE_SUBSCRIBER, $resources)) {
|
||||
$scope = 'subscribers.read';
|
||||
$this->messaging->listSubscribers('');
|
||||
|
||||
$scope = 'subscribers.write';
|
||||
$this->messaging->createSubscriber('', '', '');
|
||||
}
|
||||
|
||||
if (\in_array(Resource::TYPE_MESSAGE, $resources)) {
|
||||
$scope = 'messages.read';
|
||||
$this->messaging->listMessages();
|
||||
|
||||
$scope = 'messages.write';
|
||||
$this->messaging->createEmail('', '', '', draft: true);
|
||||
}
|
||||
|
||||
// Sites
|
||||
if (\in_array(Resource::TYPE_SITE, $resources)) {
|
||||
$scope = 'sites.read';
|
||||
@@ -259,6 +307,7 @@ class Appwrite extends Destination
|
||||
Transfer::GROUP_STORAGE => $this->importFileResource($resource),
|
||||
Transfer::GROUP_AUTH => $this->importAuthResource($resource),
|
||||
Transfer::GROUP_FUNCTIONS => $this->importFunctionResource($resource),
|
||||
Transfer::GROUP_MESSAGING => $this->importMessagingResource($resource),
|
||||
Transfer::GROUP_SITES => $this->importSiteResource($resource),
|
||||
default => throw new \Exception('Invalid resource group'),
|
||||
};
|
||||
@@ -1540,6 +1589,42 @@ class Appwrite extends Destination
|
||||
return $deployment;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws AppwriteException
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function importMessagingResource(Resource $resource): Resource
|
||||
{
|
||||
switch ($resource->getName()) {
|
||||
case Resource::TYPE_PROVIDER:
|
||||
/** @var Provider $resource */
|
||||
$this->createProvider($resource);
|
||||
break;
|
||||
case Resource::TYPE_TOPIC:
|
||||
/** @var Topic $resource */
|
||||
$this->messaging->createTopic(
|
||||
$resource->getId(),
|
||||
$resource->getTopicName(),
|
||||
$resource->getSubscribe(),
|
||||
);
|
||||
break;
|
||||
case Resource::TYPE_SUBSCRIBER:
|
||||
/** @var Subscriber $resource */
|
||||
$this->createSubscriber($resource);
|
||||
break;
|
||||
case Resource::TYPE_MESSAGE:
|
||||
/** @var Message $resource */
|
||||
$this->createMessage($resource);
|
||||
break;
|
||||
default:
|
||||
throw new \Exception('Unknown messaging resource type: ' . $resource->getName());
|
||||
}
|
||||
|
||||
$resource->setStatus(Resource::STATUS_SUCCESS);
|
||||
|
||||
return $resource;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws AppwriteException
|
||||
*/
|
||||
@@ -1682,6 +1767,325 @@ class Appwrite extends Destination
|
||||
* @throws AppwriteException
|
||||
* @throws \Exception
|
||||
*/
|
||||
protected function createProvider(Provider $resource): bool
|
||||
{
|
||||
$credentials = $resource->getCredentials();
|
||||
$options = $resource->getOptions();
|
||||
$id = $resource->getId();
|
||||
$name = $resource->getProviderName();
|
||||
$enabled = $resource->getEnabled();
|
||||
|
||||
match ($resource->getProvider()) {
|
||||
'mailgun' => $this->messaging->createMailgunProvider(
|
||||
$id,
|
||||
$name,
|
||||
$credentials['apiKey'] ?? null,
|
||||
$credentials['domain'] ?? null,
|
||||
$credentials['isEuRegion'] ?? null,
|
||||
($options['fromName'] ?? '') ?: null,
|
||||
($options['fromEmail'] ?? '') ?: null,
|
||||
($options['replyToName'] ?? '') ?: null,
|
||||
($options['replyToEmail'] ?? '') ?: null,
|
||||
$enabled,
|
||||
),
|
||||
'sendgrid' => $this->messaging->createSendgridProvider(
|
||||
$id,
|
||||
$name,
|
||||
$credentials['apiKey'] ?? null,
|
||||
($options['fromName'] ?? '') ?: null,
|
||||
($options['fromEmail'] ?? '') ?: null,
|
||||
($options['replyToName'] ?? '') ?: null,
|
||||
($options['replyToEmail'] ?? '') ?: null,
|
||||
$enabled,
|
||||
),
|
||||
'resend' => $this->messaging->createResendProvider(
|
||||
$id,
|
||||
$name,
|
||||
$credentials['apiKey'] ?? null,
|
||||
($options['fromName'] ?? '') ?: null,
|
||||
($options['fromEmail'] ?? '') ?: null,
|
||||
($options['replyToName'] ?? '') ?: null,
|
||||
($options['replyToEmail'] ?? '') ?: null,
|
||||
$enabled,
|
||||
),
|
||||
'smtp' => $this->messaging->createSMTPProvider(
|
||||
$id,
|
||||
$name,
|
||||
$credentials['host'] ?? '',
|
||||
$credentials['port'] ?? null,
|
||||
($credentials['username'] ?? '') ?: null,
|
||||
($credentials['password'] ?? '') ?: null,
|
||||
match ($options['encryption'] ?? '') {
|
||||
'ssl' => SmtpEncryption::SSL(),
|
||||
'tls' => SmtpEncryption::TLS(),
|
||||
default => SmtpEncryption::NONE(),
|
||||
},
|
||||
$options['autoTLS'] ?? null,
|
||||
($options['mailer'] ?? '') ?: null,
|
||||
($options['fromName'] ?? '') ?: null,
|
||||
($options['fromEmail'] ?? '') ?: null,
|
||||
($options['replyToName'] ?? '') ?: null,
|
||||
($options['replyToEmail'] ?? '') ?: null,
|
||||
$enabled,
|
||||
),
|
||||
'msg91' => $this->messaging->createMsg91Provider(
|
||||
$id,
|
||||
$name,
|
||||
$credentials['templateId'] ?? null,
|
||||
$credentials['senderId'] ?? null,
|
||||
$credentials['authKey'] ?? null,
|
||||
$enabled,
|
||||
),
|
||||
'telesign' => $this->messaging->createTelesignProvider(
|
||||
$id,
|
||||
$name,
|
||||
($options['from'] ?? '') ?: null,
|
||||
$credentials['customerId'] ?? null,
|
||||
$credentials['apiKey'] ?? null,
|
||||
$enabled,
|
||||
),
|
||||
'textmagic' => $this->messaging->createTextmagicProvider(
|
||||
$id,
|
||||
$name,
|
||||
($options['from'] ?? '') ?: null,
|
||||
$credentials['username'] ?? null,
|
||||
$credentials['apiKey'] ?? null,
|
||||
$enabled,
|
||||
),
|
||||
'twilio' => $this->messaging->createTwilioProvider(
|
||||
$id,
|
||||
$name,
|
||||
($options['from'] ?? '') ?: null,
|
||||
$credentials['accountSid'] ?? null,
|
||||
$credentials['authToken'] ?? null,
|
||||
$enabled,
|
||||
),
|
||||
'vonage' => $this->messaging->createVonageProvider(
|
||||
$id,
|
||||
$name,
|
||||
($options['from'] ?? '') ?: null,
|
||||
$credentials['apiKey'] ?? null,
|
||||
$credentials['apiSecret'] ?? null,
|
||||
$enabled,
|
||||
),
|
||||
'fcm' => $this->messaging->createFCMProvider(
|
||||
$id,
|
||||
$name,
|
||||
$credentials['serviceAccountJSON'] ?? null,
|
||||
$enabled,
|
||||
),
|
||||
'apns' => $this->messaging->createAPNSProvider(
|
||||
$id,
|
||||
$name,
|
||||
$credentials['authKey'] ?? null,
|
||||
$credentials['authKeyId'] ?? null,
|
||||
$credentials['teamId'] ?? null,
|
||||
$credentials['bundleId'] ?? null,
|
||||
$options['sandbox'] ?? null,
|
||||
$enabled,
|
||||
),
|
||||
default => throw new \Exception('Unknown provider: ' . $resource->getProvider()),
|
||||
};
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \Exception
|
||||
*/
|
||||
protected function createSubscriber(Subscriber $resource): void
|
||||
{
|
||||
$target = $this->database->find('targets', [
|
||||
Query::equal('userId', [$resource->getUserId()]),
|
||||
Query::equal('providerType', [$resource->getProviderType()]),
|
||||
Query::limit(1),
|
||||
]);
|
||||
|
||||
if (empty($target)) {
|
||||
throw new \Exception('No matching target found for subscriber ' . $resource->getId() . ' with providerType ' . $resource->getProviderType());
|
||||
}
|
||||
|
||||
$target = $target[0];
|
||||
|
||||
$topic = $this->database->getDocument('topics', $resource->getTopicId());
|
||||
if ($topic->isEmpty()) {
|
||||
throw new \Exception('Topic not found: ' . $resource->getTopicId());
|
||||
}
|
||||
|
||||
$user = $this->database->getDocument('users', $resource->getUserId());
|
||||
if ($user->isEmpty()) {
|
||||
throw new \Exception('User not found: ' . $resource->getUserId());
|
||||
}
|
||||
|
||||
$createdAt = $this->normalizeDateTime($resource->getCreatedAt());
|
||||
$updatedAt = $this->normalizeDateTime($resource->getUpdatedAt(), $createdAt);
|
||||
|
||||
$this->database->createDocument('subscribers', new UtopiaDocument([
|
||||
'$id' => $resource->getId(),
|
||||
'$createdAt' => $createdAt,
|
||||
'$updatedAt' => $updatedAt,
|
||||
'$permissions' => [
|
||||
Permission::read(Role::user($resource->getUserId())),
|
||||
Permission::delete(Role::user($resource->getUserId())),
|
||||
],
|
||||
'topicId' => $topic->getId(),
|
||||
'topicInternalId' => $topic->getSequence(),
|
||||
'targetId' => $target->getId(),
|
||||
'targetInternalId' => $target->getSequence(),
|
||||
'userId' => $user->getId(),
|
||||
'userInternalId' => $user->getSequence(),
|
||||
'providerType' => $resource->getProviderType(),
|
||||
'search' => implode(' ', [
|
||||
$resource->getId(),
|
||||
$target->getId(),
|
||||
$user->getId(),
|
||||
$resource->getProviderType(),
|
||||
]),
|
||||
]));
|
||||
|
||||
$totalAttribute = match ($resource->getProviderType()) {
|
||||
'email' => 'emailTotal',
|
||||
'sms' => 'smsTotal',
|
||||
'push' => 'pushTotal',
|
||||
default => throw new \Exception('Unknown provider type: ' . $resource->getProviderType()),
|
||||
};
|
||||
|
||||
$this->database->increaseDocumentAttribute('topics', $resource->getTopicId(), $totalAttribute);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws AppwriteException
|
||||
* @throws \Exception
|
||||
*/
|
||||
protected function createMessage(Message $resource): bool
|
||||
{
|
||||
$resolvedTargets = $this->resolveMessageTargets($resource);
|
||||
$status = $resource->getMessageStatus();
|
||||
|
||||
if ($status === 'scheduled') {
|
||||
$scheduledAt = $resource->getScheduledAt();
|
||||
|
||||
if (!empty($scheduledAt) && new \DateTime($scheduledAt) > new \DateTime()) {
|
||||
return $this->createScheduledMessage($resource, $resolvedTargets);
|
||||
}
|
||||
|
||||
$status = 'draft';
|
||||
}
|
||||
|
||||
if ($status === 'processing') {
|
||||
$status = 'draft';
|
||||
}
|
||||
|
||||
$createdAt = $this->normalizeDateTime($resource->getCreatedAt());
|
||||
$updatedAt = $this->normalizeDateTime($resource->getUpdatedAt(), $createdAt);
|
||||
|
||||
$this->database->createDocument('messages', new UtopiaDocument([
|
||||
'$id' => $resource->getId(),
|
||||
'$createdAt' => $createdAt,
|
||||
'$updatedAt' => $updatedAt,
|
||||
'providerType' => $resource->getProviderType(),
|
||||
'topics' => $resource->getTopics(),
|
||||
'users' => $resource->getUsers(),
|
||||
'targets' => $resolvedTargets,
|
||||
'scheduledAt' => null,
|
||||
'deliveredAt' => $resource->getDeliveredAt() ?: null,
|
||||
'deliveryErrors' => $resource->getDeliveryErrors(),
|
||||
'deliveredTotal' => $resource->getDeliveredTotal(),
|
||||
'data' => $resource->getData(),
|
||||
'status' => $status,
|
||||
]));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string> $resolvedTargets
|
||||
* @throws AppwriteException
|
||||
* @throws \Exception
|
||||
*/
|
||||
protected function createScheduledMessage(Message $resource, array $resolvedTargets): bool
|
||||
{
|
||||
$data = $resource->getData();
|
||||
$topics = $resource->getTopics() ?: null;
|
||||
$users = $resource->getUsers() ?: null;
|
||||
$targets = $resolvedTargets ?: null;
|
||||
$scheduledAt = $resource->getScheduledAt();
|
||||
|
||||
match ($resource->getProviderType()) {
|
||||
'email' => $this->messaging->createEmail(
|
||||
$resource->getId(),
|
||||
$data['subject'] ?? '',
|
||||
$data['content'] ?? '',
|
||||
$topics,
|
||||
$users,
|
||||
$targets,
|
||||
$data['cc'] ?? null,
|
||||
$data['bcc'] ?? null,
|
||||
null,
|
||||
false,
|
||||
$data['html'] ?? null,
|
||||
$scheduledAt,
|
||||
),
|
||||
'sms' => $this->messaging->createSMS(
|
||||
$resource->getId(),
|
||||
$data['content'] ?? '',
|
||||
$topics,
|
||||
$users,
|
||||
$targets,
|
||||
false,
|
||||
$scheduledAt,
|
||||
),
|
||||
'push' => $this->messaging->createPush(
|
||||
$resource->getId(),
|
||||
$data['title'] ?? null,
|
||||
$data['body'] ?? null,
|
||||
$topics,
|
||||
$users,
|
||||
$targets,
|
||||
$data['data'] ?? null,
|
||||
$data['action'] ?? null,
|
||||
$data['image'] ?? null,
|
||||
$data['icon'] ?? null,
|
||||
$data['sound'] ?? null,
|
||||
$data['color'] ?? null,
|
||||
$data['tag'] ?? null,
|
||||
$data['badge'] ?? null,
|
||||
false,
|
||||
$scheduledAt,
|
||||
$data['contentAvailable'] ?? null,
|
||||
$data['critical'] ?? null,
|
||||
null,
|
||||
),
|
||||
default => throw new \Exception('Unknown provider type: ' . $resource->getProviderType()),
|
||||
};
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string>
|
||||
*/
|
||||
private function resolveMessageTargets(Message $resource): array
|
||||
{
|
||||
$targetUserMap = $resource->getTargetUserMap();
|
||||
|
||||
$userIds = array_values(array_unique(array_filter(
|
||||
array_map(fn ($sourceTargetId) => $targetUserMap[$sourceTargetId] ?? null, $resource->getTargets())
|
||||
)));
|
||||
|
||||
if (empty($userIds)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$targets = $this->database->find('targets', [
|
||||
Query::equal('userId', $userIds),
|
||||
Query::equal('providerType', [$resource->getProviderType()]),
|
||||
]);
|
||||
|
||||
return array_map(fn (UtopiaDocument $target) => $target->getId(), $targets);
|
||||
}
|
||||
|
||||
|
||||
private function importSiteDeployment(SiteDeployment $deployment): Resource
|
||||
{
|
||||
$site = $deployment->getSite();
|
||||
|
||||
@@ -70,6 +70,12 @@ class Local extends Destination
|
||||
Resource::TYPE_FUNCTION,
|
||||
Resource::TYPE_DEPLOYMENT,
|
||||
Resource::TYPE_ENVIRONMENT_VARIABLE,
|
||||
|
||||
// Messaging
|
||||
Resource::TYPE_PROVIDER,
|
||||
Resource::TYPE_TOPIC,
|
||||
Resource::TYPE_SUBSCRIBER,
|
||||
Resource::TYPE_MESSAGE,
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
@@ -46,6 +46,10 @@ abstract class Resource implements \JsonSerializable
|
||||
|
||||
public const TYPE_INDEX = 'index';
|
||||
|
||||
public const TYPE_PROVIDER = 'provider';
|
||||
|
||||
public const TYPE_TOPIC = 'topic';
|
||||
|
||||
// Children (Resources that are created by other resources)
|
||||
|
||||
public const TYPE_COLUMN = 'column';
|
||||
@@ -60,6 +64,10 @@ abstract class Resource implements \JsonSerializable
|
||||
|
||||
public const TYPE_ENVIRONMENT_VARIABLE = 'environment-variable';
|
||||
|
||||
public const TYPE_SUBSCRIBER = 'subscriber';
|
||||
|
||||
public const TYPE_MESSAGE = 'message';
|
||||
|
||||
// legacy terminologies
|
||||
public const TYPE_DOCUMENT = 'document';
|
||||
public const TYPE_ATTRIBUTE = 'attribute';
|
||||
@@ -89,6 +97,10 @@ abstract class Resource implements \JsonSerializable
|
||||
self::TYPE_ENVIRONMENT_VARIABLE,
|
||||
self::TYPE_TEAM,
|
||||
self::TYPE_MEMBERSHIP,
|
||||
self::TYPE_PROVIDER,
|
||||
self::TYPE_TOPIC,
|
||||
self::TYPE_SUBSCRIBER,
|
||||
self::TYPE_MESSAGE,
|
||||
|
||||
// legacy
|
||||
self::TYPE_DOCUMENT,
|
||||
|
||||
@@ -0,0 +1,172 @@
|
||||
<?php
|
||||
|
||||
namespace Utopia\Migration\Resources\Messaging;
|
||||
|
||||
use Utopia\Migration\Resource;
|
||||
use Utopia\Migration\Transfer;
|
||||
|
||||
class Message extends Resource
|
||||
{
|
||||
/**
|
||||
* @param string $id
|
||||
* @param string $providerType
|
||||
* @param array<string> $topics
|
||||
* @param array<string> $users
|
||||
* @param array<string> $targets
|
||||
* @param array<string, mixed> $data
|
||||
* @param string $messageStatus
|
||||
* @param string $scheduledAt
|
||||
* @param string $deliveredAt
|
||||
* @param array<string> $deliveryErrors
|
||||
* @param int $deliveredTotal
|
||||
* @param array<string, string> $targetUserMap Source target ID => source user ID mapping for ID resolution
|
||||
*/
|
||||
public function __construct(
|
||||
string $id,
|
||||
private readonly string $providerType,
|
||||
private readonly array $topics = [],
|
||||
private readonly array $users = [],
|
||||
private readonly array $targets = [],
|
||||
private readonly array $data = [],
|
||||
private readonly string $messageStatus = '',
|
||||
private readonly string $scheduledAt = '',
|
||||
private readonly string $deliveredAt = '',
|
||||
private readonly array $deliveryErrors = [],
|
||||
private readonly int $deliveredTotal = 0,
|
||||
private readonly array $targetUserMap = [],
|
||||
protected string $createdAt = '',
|
||||
protected string $updatedAt = '',
|
||||
) {
|
||||
$this->id = $id;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $array
|
||||
* @return self
|
||||
*/
|
||||
public static function fromArray(array $array): self
|
||||
{
|
||||
return new self(
|
||||
$array['id'],
|
||||
$array['providerType'] ?? '',
|
||||
$array['topics'] ?? [],
|
||||
$array['users'] ?? [],
|
||||
$array['targets'] ?? [],
|
||||
$array['data'] ?? [],
|
||||
$array['messageStatus'] ?? $array['status'] ?? '',
|
||||
$array['scheduledAt'] ?? '',
|
||||
$array['deliveredAt'] ?? '',
|
||||
$array['deliveryErrors'] ?? [],
|
||||
$array['deliveredTotal'] ?? 0,
|
||||
$array['targetUserMap'] ?? [],
|
||||
$array['createdAt'] ?? '',
|
||||
$array['updatedAt'] ?? '',
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function jsonSerialize(): array
|
||||
{
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'providerType' => $this->providerType,
|
||||
'topics' => $this->topics,
|
||||
'users' => $this->users,
|
||||
'targets' => $this->targets,
|
||||
'data' => $this->data,
|
||||
'messageStatus' => $this->messageStatus,
|
||||
'scheduledAt' => $this->scheduledAt,
|
||||
'deliveredAt' => $this->deliveredAt,
|
||||
'deliveryErrors' => $this->deliveryErrors,
|
||||
'deliveredTotal' => $this->deliveredTotal,
|
||||
'targetUserMap' => $this->targetUserMap,
|
||||
'createdAt' => $this->createdAt,
|
||||
'updatedAt' => $this->updatedAt,
|
||||
];
|
||||
}
|
||||
|
||||
public static function getName(): string
|
||||
{
|
||||
return Resource::TYPE_MESSAGE;
|
||||
}
|
||||
|
||||
public function getGroup(): string
|
||||
{
|
||||
return Transfer::GROUP_MESSAGING;
|
||||
}
|
||||
|
||||
public function getProviderType(): string
|
||||
{
|
||||
return $this->providerType;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string>
|
||||
*/
|
||||
public function getTopics(): array
|
||||
{
|
||||
return $this->topics;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string>
|
||||
*/
|
||||
public function getUsers(): array
|
||||
{
|
||||
return $this->users;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string>
|
||||
*/
|
||||
public function getTargets(): array
|
||||
{
|
||||
return $this->targets;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function getData(): array
|
||||
{
|
||||
return $this->data;
|
||||
}
|
||||
|
||||
public function getMessageStatus(): string
|
||||
{
|
||||
return $this->messageStatus;
|
||||
}
|
||||
|
||||
public function getScheduledAt(): string
|
||||
{
|
||||
return $this->scheduledAt;
|
||||
}
|
||||
|
||||
public function getDeliveredAt(): string
|
||||
{
|
||||
return $this->deliveredAt;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string>
|
||||
*/
|
||||
public function getDeliveryErrors(): array
|
||||
{
|
||||
return $this->deliveryErrors;
|
||||
}
|
||||
|
||||
public function getDeliveredTotal(): int
|
||||
{
|
||||
return $this->deliveredTotal;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, string>
|
||||
*/
|
||||
public function getTargetUserMap(): array
|
||||
{
|
||||
return $this->targetUserMap;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,115 @@
|
||||
<?php
|
||||
|
||||
namespace Utopia\Migration\Resources\Messaging;
|
||||
|
||||
use Utopia\Migration\Resource;
|
||||
use Utopia\Migration\Transfer;
|
||||
|
||||
class Provider extends Resource
|
||||
{
|
||||
/**
|
||||
* @param string $id
|
||||
* @param string $name
|
||||
* @param string $provider
|
||||
* @param string $type
|
||||
* @param bool $enabled
|
||||
* @param array<string, mixed> $credentials
|
||||
* @param array<string, mixed> $options
|
||||
*/
|
||||
public function __construct(
|
||||
string $id,
|
||||
private readonly string $name,
|
||||
private readonly string $provider,
|
||||
private readonly string $type,
|
||||
private readonly bool $enabled = true,
|
||||
private readonly array $credentials = [],
|
||||
private readonly array $options = [],
|
||||
protected string $createdAt = '',
|
||||
protected string $updatedAt = '',
|
||||
) {
|
||||
$this->id = $id;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $array
|
||||
* @return self
|
||||
*/
|
||||
public static function fromArray(array $array): self
|
||||
{
|
||||
return new self(
|
||||
$array['id'],
|
||||
$array['name'] ?? '',
|
||||
$array['provider'] ?? '',
|
||||
$array['type'] ?? '',
|
||||
$array['enabled'] ?? true,
|
||||
$array['credentials'] ?? [],
|
||||
$array['options'] ?? [],
|
||||
$array['createdAt'] ?? '',
|
||||
$array['updatedAt'] ?? '',
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function jsonSerialize(): array
|
||||
{
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'name' => $this->name,
|
||||
'provider' => $this->provider,
|
||||
'type' => $this->type,
|
||||
'enabled' => $this->enabled,
|
||||
'credentials' => $this->credentials,
|
||||
'options' => $this->options,
|
||||
'createdAt' => $this->createdAt,
|
||||
'updatedAt' => $this->updatedAt,
|
||||
];
|
||||
}
|
||||
|
||||
public static function getName(): string
|
||||
{
|
||||
return Resource::TYPE_PROVIDER;
|
||||
}
|
||||
|
||||
public function getGroup(): string
|
||||
{
|
||||
return Transfer::GROUP_MESSAGING;
|
||||
}
|
||||
|
||||
public function getProviderName(): string
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
public function getProvider(): string
|
||||
{
|
||||
return $this->provider;
|
||||
}
|
||||
|
||||
public function getType(): string
|
||||
{
|
||||
return $this->type;
|
||||
}
|
||||
|
||||
public function getEnabled(): bool
|
||||
{
|
||||
return $this->enabled;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function getCredentials(): array
|
||||
{
|
||||
return $this->credentials;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function getOptions(): array
|
||||
{
|
||||
return $this->options;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
<?php
|
||||
|
||||
namespace Utopia\Migration\Resources\Messaging;
|
||||
|
||||
use Utopia\Migration\Resource;
|
||||
use Utopia\Migration\Transfer;
|
||||
|
||||
class Subscriber extends Resource
|
||||
{
|
||||
/**
|
||||
* @param string $id
|
||||
* @param string $topicId
|
||||
* @param string $targetId
|
||||
* @param string $userId
|
||||
* @param string $userName
|
||||
* @param string $providerType
|
||||
*/
|
||||
public function __construct(
|
||||
string $id,
|
||||
private readonly string $topicId,
|
||||
private readonly string $targetId,
|
||||
private readonly string $userId = '',
|
||||
private readonly string $userName = '',
|
||||
private readonly string $providerType = '',
|
||||
protected string $createdAt = '',
|
||||
protected string $updatedAt = '',
|
||||
) {
|
||||
$this->id = $id;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $array
|
||||
* @return self
|
||||
*/
|
||||
public static function fromArray(array $array): self
|
||||
{
|
||||
return new self(
|
||||
$array['id'],
|
||||
$array['topicId'] ?? '',
|
||||
$array['targetId'] ?? '',
|
||||
$array['userId'] ?? '',
|
||||
$array['userName'] ?? '',
|
||||
$array['providerType'] ?? '',
|
||||
$array['createdAt'] ?? '',
|
||||
$array['updatedAt'] ?? '',
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function jsonSerialize(): array
|
||||
{
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'topicId' => $this->topicId,
|
||||
'targetId' => $this->targetId,
|
||||
'userId' => $this->userId,
|
||||
'userName' => $this->userName,
|
||||
'providerType' => $this->providerType,
|
||||
'createdAt' => $this->createdAt,
|
||||
'updatedAt' => $this->updatedAt,
|
||||
];
|
||||
}
|
||||
|
||||
public static function getName(): string
|
||||
{
|
||||
return Resource::TYPE_SUBSCRIBER;
|
||||
}
|
||||
|
||||
public function getGroup(): string
|
||||
{
|
||||
return Transfer::GROUP_MESSAGING;
|
||||
}
|
||||
|
||||
public function getTopicId(): string
|
||||
{
|
||||
return $this->topicId;
|
||||
}
|
||||
|
||||
public function getTargetId(): string
|
||||
{
|
||||
return $this->targetId;
|
||||
}
|
||||
|
||||
public function getUserId(): string
|
||||
{
|
||||
return $this->userId;
|
||||
}
|
||||
|
||||
public function getUserName(): string
|
||||
{
|
||||
return $this->userName;
|
||||
}
|
||||
|
||||
public function getProviderType(): string
|
||||
{
|
||||
return $this->providerType;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
<?php
|
||||
|
||||
namespace Utopia\Migration\Resources\Messaging;
|
||||
|
||||
use Utopia\Migration\Resource;
|
||||
use Utopia\Migration\Transfer;
|
||||
|
||||
class Topic extends Resource
|
||||
{
|
||||
/**
|
||||
* @param string $id
|
||||
* @param string $name
|
||||
* @param array<string> $subscribe
|
||||
*/
|
||||
public function __construct(
|
||||
string $id,
|
||||
private readonly string $name,
|
||||
private readonly array $subscribe = [],
|
||||
protected string $createdAt = '',
|
||||
protected string $updatedAt = '',
|
||||
) {
|
||||
$this->id = $id;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $array
|
||||
* @return self
|
||||
*/
|
||||
public static function fromArray(array $array): self
|
||||
{
|
||||
return new self(
|
||||
$array['id'],
|
||||
$array['name'] ?? '',
|
||||
$array['subscribe'] ?? [],
|
||||
$array['createdAt'] ?? '',
|
||||
$array['updatedAt'] ?? '',
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function jsonSerialize(): array
|
||||
{
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'name' => $this->name,
|
||||
'subscribe' => $this->subscribe,
|
||||
'createdAt' => $this->createdAt,
|
||||
'updatedAt' => $this->updatedAt,
|
||||
];
|
||||
}
|
||||
|
||||
public static function getName(): string
|
||||
{
|
||||
return Resource::TYPE_TOPIC;
|
||||
}
|
||||
|
||||
public function getGroup(): string
|
||||
{
|
||||
return Transfer::GROUP_MESSAGING;
|
||||
}
|
||||
|
||||
public function getTopicName(): string
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string>
|
||||
*/
|
||||
public function getSubscribe(): array
|
||||
{
|
||||
return $this->subscribe;
|
||||
}
|
||||
}
|
||||
@@ -36,6 +36,11 @@ abstract class Source extends Target
|
||||
return static::$defaultBatchSize;
|
||||
}
|
||||
|
||||
public function getMessagingBatchSize(): int
|
||||
{
|
||||
return static::$defaultBatchSize;
|
||||
}
|
||||
|
||||
public function getSitesBatchSize(): int
|
||||
{
|
||||
return static::$defaultBatchSize;
|
||||
@@ -94,6 +99,7 @@ abstract class Source extends Target
|
||||
Transfer::GROUP_DATABASES => Transfer::GROUP_DATABASES_RESOURCES,
|
||||
Transfer::GROUP_STORAGE => Transfer::GROUP_STORAGE_RESOURCES,
|
||||
Transfer::GROUP_FUNCTIONS => Transfer::GROUP_FUNCTIONS_RESOURCES,
|
||||
Transfer::GROUP_MESSAGING => Transfer::GROUP_MESSAGING_RESOURCES,
|
||||
Transfer::GROUP_SITES => Transfer::GROUP_SITES_RESOURCES,
|
||||
];
|
||||
|
||||
@@ -123,6 +129,9 @@ abstract class Source extends Target
|
||||
case Transfer::GROUP_FUNCTIONS:
|
||||
$this->exportGroupFunctions($this->getFunctionsBatchSize(), $resources);
|
||||
break;
|
||||
case Transfer::GROUP_MESSAGING:
|
||||
$this->exportGroupMessaging($this->getMessagingBatchSize(), $resources);
|
||||
break;
|
||||
case Transfer::GROUP_SITES:
|
||||
$this->exportGroupSites($this->getSitesBatchSize(), $resources);
|
||||
break;
|
||||
@@ -162,6 +171,14 @@ abstract class Source extends Target
|
||||
*/
|
||||
abstract protected function exportGroupFunctions(int $batchSize, array $resources): void;
|
||||
|
||||
/**
|
||||
* Export Messaging Group
|
||||
*
|
||||
* @param int $batchSize
|
||||
* @param array<string> $resources Resources to export
|
||||
*/
|
||||
abstract protected function exportGroupMessaging(int $batchSize, array $resources): void;
|
||||
|
||||
/**
|
||||
* Export Sites Group
|
||||
*
|
||||
|
||||
@@ -7,6 +7,7 @@ use Appwrite\Client;
|
||||
use Appwrite\Query;
|
||||
use Appwrite\Services\Databases;
|
||||
use Appwrite\Services\Functions;
|
||||
use Appwrite\Services\Messaging;
|
||||
use Appwrite\Services\Sites;
|
||||
use Appwrite\Services\Storage;
|
||||
use Appwrite\Services\Teams;
|
||||
@@ -44,6 +45,10 @@ use Utopia\Migration\Resources\Database\Table;
|
||||
use Utopia\Migration\Resources\Functions\Deployment;
|
||||
use Utopia\Migration\Resources\Functions\EnvVar;
|
||||
use Utopia\Migration\Resources\Functions\Func;
|
||||
use Utopia\Migration\Resources\Messaging\Message;
|
||||
use Utopia\Migration\Resources\Messaging\Provider;
|
||||
use Utopia\Migration\Resources\Messaging\Subscriber;
|
||||
use Utopia\Migration\Resources\Messaging\Topic;
|
||||
use Utopia\Migration\Resources\Sites\Deployment as SiteDeployment;
|
||||
use Utopia\Migration\Resources\Sites\EnvVar as SiteEnvVar;
|
||||
use Utopia\Migration\Resources\Sites\Site;
|
||||
@@ -72,6 +77,8 @@ class Appwrite extends Source
|
||||
|
||||
private Functions $functions;
|
||||
|
||||
private Messaging $messaging;
|
||||
|
||||
private Sites $sites;
|
||||
|
||||
private Reader $database;
|
||||
@@ -96,6 +103,7 @@ class Appwrite extends Source
|
||||
$this->teams = new Teams($this->client);
|
||||
$this->storage = new Storage($this->client);
|
||||
$this->functions = new Functions($this->client);
|
||||
$this->messaging = new Messaging($this->client);
|
||||
$this->sites = new Sites($this->client);
|
||||
|
||||
$this->headers['x-appwrite-project'] = $this->project;
|
||||
@@ -153,6 +161,12 @@ class Appwrite extends Source
|
||||
Resource::TYPE_DEPLOYMENT,
|
||||
Resource::TYPE_ENVIRONMENT_VARIABLE,
|
||||
|
||||
// Messaging
|
||||
Resource::TYPE_PROVIDER,
|
||||
Resource::TYPE_TOPIC,
|
||||
Resource::TYPE_SUBSCRIBER,
|
||||
Resource::TYPE_MESSAGE,
|
||||
|
||||
// Sites
|
||||
Resource::TYPE_SITE,
|
||||
Resource::TYPE_SITE_DEPLOYMENT,
|
||||
@@ -195,6 +209,7 @@ class Appwrite extends Source
|
||||
$this->reportDatabases($resources, $report, $resourceIds);
|
||||
$this->reportStorage($resources, $report, $resourceIds);
|
||||
$this->reportFunctions($resources, $report, $resourceIds);
|
||||
$this->reportMessaging($resources, $report, $resourceIds);
|
||||
$this->reportSites($resources, $report, $resourceIds);
|
||||
|
||||
$report['version'] = $this->call(
|
||||
@@ -1785,6 +1800,367 @@ class Appwrite extends Source
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string> $resources
|
||||
* @param array<string, int> $report
|
||||
* @param array<string, array<string>> $resourceIds
|
||||
*/
|
||||
private function reportMessaging(array $resources, array &$report, array $resourceIds = []): void
|
||||
{
|
||||
if (\in_array(Resource::TYPE_PROVIDER, $resources)) {
|
||||
$providerQueries = $this->buildQueries(
|
||||
resourceType: Resource::TYPE_PROVIDER,
|
||||
resourceIds: $resourceIds,
|
||||
limit: 1
|
||||
);
|
||||
$report[Resource::TYPE_PROVIDER] = $this->messaging->listProviders($providerQueries)['total'];
|
||||
}
|
||||
|
||||
if (\in_array(Resource::TYPE_TOPIC, $resources)) {
|
||||
$topicQueries = $this->buildQueries(
|
||||
resourceType: Resource::TYPE_TOPIC,
|
||||
resourceIds: $resourceIds,
|
||||
limit: 1
|
||||
);
|
||||
$report[Resource::TYPE_TOPIC] = $this->messaging->listTopics($topicQueries)['total'];
|
||||
}
|
||||
|
||||
if (\in_array(Resource::TYPE_SUBSCRIBER, $resources)) {
|
||||
$subscriberTotal = 0;
|
||||
$lastTopic = null;
|
||||
|
||||
while (true) {
|
||||
$topicQueries = [Query::limit(self::DEFAULT_PAGE_LIMIT)];
|
||||
if ($lastTopic) {
|
||||
$topicQueries[] = Query::cursorAfter($lastTopic);
|
||||
}
|
||||
|
||||
$topicResponse = $this->messaging->listTopics($topicQueries);
|
||||
if ($topicResponse['total'] == 0 || empty($topicResponse['topics'])) {
|
||||
break;
|
||||
}
|
||||
|
||||
foreach ($topicResponse['topics'] as $topic) {
|
||||
$subscriberTotal += $this->messaging->listSubscribers($topic['$id'], [Query::limit(1)])['total'];
|
||||
$lastTopic = $topic['$id'];
|
||||
}
|
||||
|
||||
if (\count($topicResponse['topics']) < self::DEFAULT_PAGE_LIMIT) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
$report[Resource::TYPE_SUBSCRIBER] = $subscriberTotal;
|
||||
}
|
||||
|
||||
if (\in_array(Resource::TYPE_MESSAGE, $resources)) {
|
||||
$messageQueries = $this->buildQueries(
|
||||
resourceType: Resource::TYPE_MESSAGE,
|
||||
resourceIds: $resourceIds,
|
||||
limit: 1
|
||||
);
|
||||
$report[Resource::TYPE_MESSAGE] = $this->messaging->listMessages($messageQueries)['total'];
|
||||
}
|
||||
}
|
||||
|
||||
protected function exportGroupMessaging(int $batchSize, array $resources): void
|
||||
{
|
||||
try {
|
||||
if (\in_array(Resource::TYPE_PROVIDER, $resources)) {
|
||||
$this->exportProviders($batchSize);
|
||||
}
|
||||
} catch (\Throwable $e) {
|
||||
$this->addError(new Exception(
|
||||
Resource::TYPE_PROVIDER,
|
||||
Transfer::GROUP_MESSAGING,
|
||||
message: $e->getMessage(),
|
||||
code: $e->getCode(),
|
||||
previous: $e
|
||||
));
|
||||
}
|
||||
|
||||
try {
|
||||
if (\in_array(Resource::TYPE_TOPIC, $resources)) {
|
||||
$this->exportTopics($batchSize);
|
||||
}
|
||||
} catch (\Throwable $e) {
|
||||
$this->addError(new Exception(
|
||||
Resource::TYPE_TOPIC,
|
||||
Transfer::GROUP_MESSAGING,
|
||||
message: $e->getMessage(),
|
||||
code: $e->getCode(),
|
||||
previous: $e
|
||||
));
|
||||
}
|
||||
|
||||
try {
|
||||
if (\in_array(Resource::TYPE_SUBSCRIBER, $resources)) {
|
||||
$this->exportSubscribers($batchSize);
|
||||
}
|
||||
} catch (\Throwable $e) {
|
||||
$this->addError(new Exception(
|
||||
Resource::TYPE_SUBSCRIBER,
|
||||
Transfer::GROUP_MESSAGING,
|
||||
message: $e->getMessage(),
|
||||
code: $e->getCode(),
|
||||
previous: $e
|
||||
));
|
||||
}
|
||||
|
||||
try {
|
||||
if (\in_array(Resource::TYPE_MESSAGE, $resources)) {
|
||||
$this->exportMessages($batchSize);
|
||||
}
|
||||
} catch (\Throwable $e) {
|
||||
$this->addError(new Exception(
|
||||
Resource::TYPE_MESSAGE,
|
||||
Transfer::GROUP_MESSAGING,
|
||||
message: $e->getMessage(),
|
||||
code: $e->getCode(),
|
||||
previous: $e
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private function exportProviders(int $batchSize): void
|
||||
{
|
||||
$lastDocument = null;
|
||||
|
||||
while (true) {
|
||||
$providers = [];
|
||||
|
||||
$queries = [Query::limit($batchSize)];
|
||||
|
||||
if ($this->rootResourceId !== '' && $this->rootResourceType === Resource::TYPE_PROVIDER) {
|
||||
$queries[] = Query::equal('$id', $this->rootResourceId);
|
||||
$queries[] = Query::limit(1);
|
||||
}
|
||||
|
||||
if ($lastDocument) {
|
||||
$queries[] = Query::cursorAfter($lastDocument);
|
||||
}
|
||||
|
||||
$response = $this->messaging->listProviders($queries);
|
||||
|
||||
if ($response['total'] == 0) {
|
||||
break;
|
||||
}
|
||||
|
||||
foreach ($response['providers'] as $provider) {
|
||||
$providers[] = new Provider(
|
||||
$provider['$id'],
|
||||
$provider['name'],
|
||||
$provider['provider'],
|
||||
$provider['type'],
|
||||
$provider['enabled'],
|
||||
$provider['credentials'] ?? [],
|
||||
$provider['options'] ?? [],
|
||||
$provider['$createdAt'] ?? '',
|
||||
$provider['$updatedAt'] ?? '',
|
||||
);
|
||||
|
||||
$lastDocument = $provider['$id'];
|
||||
}
|
||||
|
||||
$this->callback($providers);
|
||||
|
||||
if (\count($providers) < $batchSize) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function exportTopics(int $batchSize): void
|
||||
{
|
||||
$lastDocument = null;
|
||||
|
||||
while (true) {
|
||||
$topics = [];
|
||||
|
||||
$queries = [Query::limit($batchSize)];
|
||||
|
||||
if ($this->rootResourceId !== '' && $this->rootResourceType === Resource::TYPE_TOPIC) {
|
||||
$queries[] = Query::equal('$id', $this->rootResourceId);
|
||||
$queries[] = Query::limit(1);
|
||||
}
|
||||
|
||||
if ($lastDocument) {
|
||||
$queries[] = Query::cursorAfter($lastDocument);
|
||||
}
|
||||
|
||||
$response = $this->messaging->listTopics($queries);
|
||||
|
||||
if ($response['total'] == 0) {
|
||||
break;
|
||||
}
|
||||
|
||||
foreach ($response['topics'] as $topic) {
|
||||
$topics[] = new Topic(
|
||||
$topic['$id'],
|
||||
$topic['name'],
|
||||
$topic['subscribe'] ?? [],
|
||||
$topic['$createdAt'] ?? '',
|
||||
$topic['$updatedAt'] ?? '',
|
||||
);
|
||||
|
||||
$lastDocument = $topic['$id'];
|
||||
}
|
||||
|
||||
$this->callback($topics);
|
||||
|
||||
if (\count($topics) < $batchSize) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function exportSubscribers(int $batchSize): void
|
||||
{
|
||||
$topics = $this->cache->get(Topic::getName());
|
||||
|
||||
foreach ($topics as $topic) {
|
||||
/** @var Topic $topic */
|
||||
$lastDocument = null;
|
||||
|
||||
while (true) {
|
||||
$subscribers = [];
|
||||
|
||||
$queries = [Query::limit($batchSize)];
|
||||
|
||||
if ($lastDocument) {
|
||||
$queries[] = Query::cursorAfter($lastDocument);
|
||||
}
|
||||
|
||||
$response = $this->messaging->listSubscribers($topic->getId(), $queries);
|
||||
|
||||
if ($response['total'] == 0) {
|
||||
break;
|
||||
}
|
||||
|
||||
foreach ($response['subscribers'] as $subscriber) {
|
||||
$subscribers[] = new Subscriber(
|
||||
$subscriber['$id'],
|
||||
$subscriber['topicId'],
|
||||
$subscriber['targetId'],
|
||||
$subscriber['userId'] ?? '',
|
||||
$subscriber['userName'] ?? '',
|
||||
$subscriber['providerType'] ?? '',
|
||||
$subscriber['$createdAt'] ?? '',
|
||||
$subscriber['$updatedAt'] ?? '',
|
||||
);
|
||||
|
||||
$lastDocument = $subscriber['$id'];
|
||||
}
|
||||
|
||||
$this->callback($subscribers);
|
||||
|
||||
if (\count($subscribers) < $batchSize) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function exportMessages(int $batchSize): void
|
||||
{
|
||||
$targetUserMap = $this->buildTargetUserMap();
|
||||
$lastDocument = null;
|
||||
|
||||
while (true) {
|
||||
$messages = [];
|
||||
|
||||
$queries = [Query::limit($batchSize)];
|
||||
|
||||
if ($this->rootResourceId !== '' && $this->rootResourceType === Resource::TYPE_MESSAGE) {
|
||||
$queries[] = Query::equal('$id', $this->rootResourceId);
|
||||
$queries[] = Query::limit(1);
|
||||
}
|
||||
|
||||
if ($lastDocument) {
|
||||
$queries[] = Query::cursorAfter($lastDocument);
|
||||
}
|
||||
|
||||
$response = $this->messaging->listMessages($queries);
|
||||
|
||||
if ($response['total'] == 0) {
|
||||
break;
|
||||
}
|
||||
|
||||
foreach ($response['messages'] as $message) {
|
||||
$messageTargetMap = [];
|
||||
foreach ($message['targets'] ?? [] as $targetId) {
|
||||
if (isset($targetUserMap[$targetId])) {
|
||||
$messageTargetMap[$targetId] = $targetUserMap[$targetId];
|
||||
}
|
||||
}
|
||||
|
||||
$messages[] = new Message(
|
||||
$message['$id'],
|
||||
$message['providerType'] ?? '',
|
||||
$message['topics'] ?? [],
|
||||
$message['users'] ?? [],
|
||||
$message['targets'] ?? [],
|
||||
$message['data'] ?? [],
|
||||
$message['status'] ?? '',
|
||||
$message['scheduledAt'] ?? '',
|
||||
$message['deliveredAt'] ?? '',
|
||||
$message['deliveryErrors'] ?? [],
|
||||
$message['deliveredTotal'] ?? 0,
|
||||
$messageTargetMap,
|
||||
$message['$createdAt'] ?? '',
|
||||
$message['$updatedAt'] ?? '',
|
||||
);
|
||||
|
||||
$lastDocument = $message['$id'];
|
||||
}
|
||||
|
||||
$this->callback($messages);
|
||||
|
||||
if (\count($messages) < $batchSize) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Build a map of source target ID => source user ID
|
||||
* by iterating cached users and listing their targets.
|
||||
*
|
||||
* @return array<string, string>
|
||||
*/
|
||||
private function buildTargetUserMap(): array
|
||||
{
|
||||
$map = [];
|
||||
$users = $this->cache->get(User::getName());
|
||||
|
||||
foreach ($users as $user) {
|
||||
/** @var User $user */
|
||||
$lastTarget = null;
|
||||
|
||||
while (true) {
|
||||
$queries = [Query::limit(self::DEFAULT_PAGE_LIMIT)];
|
||||
|
||||
if ($lastTarget !== null) {
|
||||
$queries[] = Query::cursorAfter($lastTarget);
|
||||
}
|
||||
|
||||
$response = $this->users->listTargets($user->getId(), $queries);
|
||||
|
||||
foreach ($response['targets'] as $target) {
|
||||
$map[$target['$id']] = $user->getId();
|
||||
$lastTarget = $target['$id'];
|
||||
}
|
||||
|
||||
if (\count($response['targets']) < self::DEFAULT_PAGE_LIMIT) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $map;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws AppwriteException
|
||||
*/
|
||||
|
||||
@@ -372,6 +372,14 @@ class CSV extends Source
|
||||
throw new \Exception('Not Implemented');
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \Exception
|
||||
*/
|
||||
protected function exportGroupMessaging(int $batchSize, array $resources): void
|
||||
{
|
||||
throw new \Exception('Not Implemented');
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \Exception
|
||||
*/
|
||||
|
||||
@@ -809,6 +809,11 @@ class Firebase extends Source
|
||||
throw new \Exception('Not implemented');
|
||||
}
|
||||
|
||||
protected function exportGroupMessaging(int $batchSize, array $resources): void
|
||||
{
|
||||
throw new \Exception('Not implemented');
|
||||
}
|
||||
|
||||
protected function exportGroupSites(int $batchSize, array $resources): void
|
||||
{
|
||||
throw new \Exception('Not implemented');
|
||||
|
||||
@@ -201,6 +201,14 @@ class JSON extends Source
|
||||
throw new \Exception('Not Implemented');
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \Exception
|
||||
*/
|
||||
protected function exportGroupMessaging(int $batchSize, array $resources): void
|
||||
{
|
||||
throw new \Exception('Not Implemented');
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \Exception
|
||||
*/
|
||||
|
||||
@@ -849,6 +849,11 @@ class NHost extends Source
|
||||
throw new \Exception('Not Implemented');
|
||||
}
|
||||
|
||||
protected function exportGroupMessaging(int $batchSize, array $resources): void
|
||||
{
|
||||
throw new \Exception('Not Implemented');
|
||||
}
|
||||
|
||||
protected function exportGroupSites(int $batchSize, array $resources): void
|
||||
{
|
||||
throw new \Exception('Not Implemented');
|
||||
|
||||
@@ -18,6 +18,8 @@ class Transfer
|
||||
|
||||
public const GROUP_SETTINGS = 'settings';
|
||||
|
||||
public const GROUP_MESSAGING = 'messaging';
|
||||
|
||||
public const GROUP_AUTH_RESOURCES = [
|
||||
Resource::TYPE_USER,
|
||||
Resource::TYPE_TEAM,
|
||||
@@ -52,6 +54,13 @@ class Transfer
|
||||
|
||||
public const GROUP_SETTINGS_RESOURCES = [];
|
||||
|
||||
public const GROUP_MESSAGING_RESOURCES = [
|
||||
Resource::TYPE_PROVIDER,
|
||||
Resource::TYPE_TOPIC,
|
||||
Resource::TYPE_SUBSCRIBER,
|
||||
Resource::TYPE_MESSAGE,
|
||||
];
|
||||
|
||||
public const ALL_PUBLIC_RESOURCES = [
|
||||
Resource::TYPE_USER,
|
||||
Resource::TYPE_TEAM,
|
||||
@@ -69,6 +78,10 @@ class Transfer
|
||||
Resource::TYPE_INDEX,
|
||||
Resource::TYPE_COLUMN,
|
||||
Resource::TYPE_ROW,
|
||||
Resource::TYPE_PROVIDER,
|
||||
Resource::TYPE_TOPIC,
|
||||
Resource::TYPE_SUBSCRIBER,
|
||||
Resource::TYPE_MESSAGE,
|
||||
|
||||
// legacy
|
||||
Resource::TYPE_DOCUMENT,
|
||||
@@ -83,6 +96,9 @@ class Transfer
|
||||
Resource::TYPE_SITE,
|
||||
Resource::TYPE_USER,
|
||||
Resource::TYPE_TEAM,
|
||||
Resource::TYPE_PROVIDER,
|
||||
Resource::TYPE_TOPIC,
|
||||
Resource::TYPE_MESSAGE,
|
||||
];
|
||||
|
||||
public const STORAGE_MAX_CHUNK_SIZE = 1024 * 1024 * 5; // 5MB
|
||||
@@ -343,6 +359,7 @@ class Transfer
|
||||
self::GROUP_AUTH => array_merge($resources, self::GROUP_AUTH_RESOURCES),
|
||||
self::GROUP_DATABASES => array_merge($resources, self::GROUP_DATABASES_RESOURCES),
|
||||
self::GROUP_SETTINGS => array_merge($resources, self::GROUP_SETTINGS_RESOURCES),
|
||||
self::GROUP_MESSAGING => array_merge($resources, self::GROUP_MESSAGING_RESOURCES),
|
||||
default => throw new \Exception('No service group found'),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -51,6 +51,10 @@ class MockDestination extends Destination
|
||||
Resource::TYPE_ENVIRONMENT_VARIABLE,
|
||||
Resource::TYPE_TEAM,
|
||||
Resource::TYPE_MEMBERSHIP,
|
||||
Resource::TYPE_PROVIDER,
|
||||
Resource::TYPE_TOPIC,
|
||||
Resource::TYPE_SUBSCRIBER,
|
||||
Resource::TYPE_MESSAGE,
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
@@ -80,6 +80,10 @@ class MockSource extends Source
|
||||
Resource::TYPE_ENVIRONMENT_VARIABLE,
|
||||
Resource::TYPE_TEAM,
|
||||
Resource::TYPE_MEMBERSHIP,
|
||||
Resource::TYPE_PROVIDER,
|
||||
Resource::TYPE_TOPIC,
|
||||
Resource::TYPE_SUBSCRIBER,
|
||||
Resource::TYPE_MESSAGE,
|
||||
|
||||
// legacy
|
||||
Resource::TYPE_DOCUMENT,
|
||||
@@ -161,6 +165,17 @@ class MockSource extends Source
|
||||
}
|
||||
}
|
||||
|
||||
protected function exportGroupMessaging(int $batchSize, array $resources): void
|
||||
{
|
||||
foreach (Transfer::GROUP_MESSAGING_RESOURCES as $resource) {
|
||||
if (!\in_array($resource, $resources)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$this->handleResourceTransfer(Transfer::GROUP_MESSAGING, $resource);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Export Sites Group
|
||||
*
|
||||
|
||||
Reference in new issue
Block a user