fix: improve messaging migration - remove target migration, fix subscriber resolution and message search

- Remove target export/import from messaging group (email/sms auto-created during user migration, push targets to be added under auth group separately)
- Fix createSubscriber() to resolve targets by providerType switch (push: direct ID, email/sms: userId + providerType)
- Fix createMessage() to include $permissions and search field matching server's messageSearch filter
- Remove targetUserMap and resolveMessageTargets (unnecessary indirection)
- Extract createTopic() and createProvider() into dedicated methods with proper return types
This commit is contained in:
Prem Palanisamy committed 2026-03-09 19:45:49 +00:00
1 parent 3583aac63b
commit f6b8f04266
4 files changed
+69 -111

No files matched your search

+50 -51
View File
@@ -1602,11 +1602,7 @@ class Appwrite extends Destination
break;
case Resource::TYPE_TOPIC:
/** @var Topic $resource */
$this->messaging->createTopic(
$resource->getId(),
$resource->getTopicName(),
$resource->getSubscribe(),
);
$this->createTopic($resource);
break;
case Resource::TYPE_SUBSCRIBER:
/** @var Subscriber $resource */
@@ -1767,7 +1763,7 @@ class Appwrite extends Destination
* @throws AppwriteException
* @throws \Exception
*/
protected function createProvider(Provider $resource): bool
protected function createProvider(Provider $resource): void
{
$credentials = $resource->getCredentials();
$options = $resource->getOptions();
@@ -1886,27 +1882,40 @@ class Appwrite extends Destination
),
default => throw new \Exception('Unknown provider: ' . $resource->getProvider()),
};
return true;
}
/**
* @throws \Exception
* @throws AppwriteException
*/
protected function createTopic(Topic $resource): void
{
$this->messaging->createTopic(
$resource->getId(),
$resource->getTopicName(),
$resource->getSubscribe(),
);
}
/**
* @throws AuthorizationException
* @throws StructureException
* @throws DatabaseException|\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),
]);
$target = match ($resource->getProviderType()) {
'push' => $this->database->getDocument('targets', $resource->getTargetId()),
'email', 'sms' => $this->database->findOne('targets', [
Query::equal('userId', [$resource->getUserId()]),
Query::equal('providerType', [$resource->getProviderType()]),
]),
default => throw new \Exception('Unknown provider type: ' . $resource->getProviderType()),
};
if (empty($target)) {
throw new \Exception('No matching target found for subscriber ' . $resource->getId() . ' with providerType ' . $resource->getProviderType());
if (!$target || $target->isEmpty()) {
throw new \Exception('Target not found for subscriber: ' . $resource->getId());
}
$target = $target[0];
$topic = $this->database->getDocument('topics', $resource->getTopicId());
if ($topic->isEmpty()) {
throw new \Exception('Topic not found: ' . $resource->getTopicId());
@@ -1957,16 +1966,18 @@ class Appwrite extends Destination
* @throws AppwriteException
* @throws \Exception
*/
protected function createMessage(Message $resource): bool
protected function createMessage(Message $resource): void
{
$resolvedTargets = $this->resolveMessageTargets($resource);
$resolvedTargets = $resource->getTargets();
$status = $resource->getMessageStatus();
if ($status === 'scheduled') {
$scheduledAt = $resource->getScheduledAt();
if (!empty($scheduledAt) && new \DateTime($scheduledAt) > new \DateTime()) {
return $this->createScheduledMessage($resource, $resolvedTargets);
$this->createScheduledMessage($resource, $resolvedTargets);
return;
}
$status = 'draft';
@@ -1979,10 +1990,19 @@ class Appwrite extends Destination
$createdAt = $this->normalizeDateTime($resource->getCreatedAt());
$updatedAt = $this->normalizeDateTime($resource->getUpdatedAt(), $createdAt);
$data = $resource->getData();
$searchContent = match ($resource->getProviderType()) {
'email' => $data['subject'] ?? '',
'sms' => $data['content'] ?? '',
'push' => $data['title'] ?? '',
default => '',
};
$this->database->createDocument('messages', new UtopiaDocument([
'$id' => $resource->getId(),
'$createdAt' => $createdAt,
'$updatedAt' => $updatedAt,
'$permissions' => [],
'providerType' => $resource->getProviderType(),
'topics' => $resource->getTopics(),
'users' => $resource->getUsers(),
@@ -1991,11 +2011,16 @@ class Appwrite extends Destination
'deliveredAt' => $resource->getDeliveredAt() ?: null,
'deliveryErrors' => $resource->getDeliveryErrors(),
'deliveredTotal' => $resource->getDeliveredTotal(),
'data' => $resource->getData(),
'data' => $data,
'status' => $status,
'search' => implode(' ', array_filter([
$resource->getId(),
$data['description'] ?? '',
$status,
$searchContent,
$resource->getProviderType(),
])),
]));
return true;
}
/**
@@ -2003,7 +2028,7 @@ class Appwrite extends Destination
* @throws AppwriteException
* @throws \Exception
*/
protected function createScheduledMessage(Message $resource, array $resolvedTargets): bool
protected function createScheduledMessage(Message $resource, array $resolvedTargets): void
{
$data = $resource->getData();
$topics = $resource->getTopics() ?: null;
@@ -2058,34 +2083,8 @@ class Appwrite extends Destination
),
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();
@@ -19,7 +19,6 @@ class Message extends Resource
* @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,
@@ -33,7 +32,6 @@ class Message extends Resource
private readonly string $deliveredAt = '',
private readonly array $deliveryErrors = [],
private readonly int $deliveredTotal = 0,
private readonly array $targetUserMap = [],
protected string $createdAt = '',
protected string $updatedAt = '',
) {
@@ -58,7 +56,6 @@ class Message extends Resource
$array['deliveredAt'] ?? '',
$array['deliveryErrors'] ?? [],
$array['deliveredTotal'] ?? 0,
$array['targetUserMap'] ?? [],
$array['createdAt'] ?? '',
$array['updatedAt'] ?? '',
);
@@ -81,7 +78,6 @@ class Message extends Resource
'deliveredAt' => $this->deliveredAt,
'deliveryErrors' => $this->deliveryErrors,
'deliveredTotal' => $this->deliveredTotal,
'targetUserMap' => $this->targetUserMap,
'createdAt' => $this->createdAt,
'updatedAt' => $this->updatedAt,
];
@@ -161,12 +157,4 @@ class Message extends Resource
{
return $this->deliveredTotal;
}
/**
* @return array<string, string>
*/
public function getTargetUserMap(): array
{
return $this->targetUserMap;
}
}
+13 -48
View File
@@ -1804,6 +1804,7 @@ class Appwrite extends Source
* @param array<string> $resources
* @param array<string, int> $report
* @param array<string, array<string>> $resourceIds
* @throws AppwriteException
*/
private function reportMessaging(array $resources, array &$report, array $resourceIds = []): void
{
@@ -1922,7 +1923,9 @@ class Appwrite extends Source
}
}
/**
* @throws AppwriteException
*/
private function exportProviders(int $batchSize): void
{
$lastDocument = null;
@@ -1971,6 +1974,9 @@ class Appwrite extends Source
}
}
/**
* @throws AppwriteException
*/
private function exportTopics(int $batchSize): void
{
$lastDocument = null;
@@ -2015,6 +2021,9 @@ class Appwrite extends Source
}
}
/**
* @throws AppwriteException
*/
private function exportSubscribers(int $batchSize): void
{
$topics = $this->cache->get(Topic::getName());
@@ -2062,9 +2071,11 @@ class Appwrite extends Source
}
}
/**
* @throws AppwriteException
*/
private function exportMessages(int $batchSize): void
{
$targetUserMap = $this->buildTargetUserMap();
$lastDocument = null;
while (true) {
@@ -2088,13 +2099,6 @@ class Appwrite extends Source
}
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'] ?? '',
@@ -2107,7 +2111,6 @@ class Appwrite extends Source
$message['deliveredAt'] ?? '',
$message['deliveryErrors'] ?? [],
$message['deliveredTotal'] ?? 0,
$messageTargetMap,
$message['$createdAt'] ?? '',
$message['$updatedAt'] ?? '',
);
@@ -2123,44 +2126,6 @@ class Appwrite extends Source
}
}
/**
* 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
*/
@@ -165,6 +165,12 @@ class MockSource extends Source
}
}
/**
* Export Messaging Group
*
* @param int $batchSize Max 100
* @param string[] $resources Resources to export
*/
protected function exportGroupMessaging(int $batchSize, array $resources): void
{
foreach (Transfer::GROUP_MESSAGING_RESOURCES as $resource) {