From 688b7b6aa19b0031fa300251da88c73f1452a45a Mon Sep 17 00:00:00 2001 From: Prem Palanisamy Date: Wed, 11 Mar 2026 11:30:21 +0000 Subject: [PATCH 1/2] feat: migrate push targets with user resources Write push targets directly to DB during GROUP_AUTH and resolve providerInternalId after provider creation in GROUP_MESSAGING. --- src/Migration/Destinations/Appwrite.php | 60 +++++++++++++++++++++++++ src/Migration/Resources/Auth/User.php | 18 +++++++- src/Migration/Sources/Appwrite.php | 1 + 3 files changed, 77 insertions(+), 2 deletions(-) diff --git a/src/Migration/Destinations/Appwrite.php b/src/Migration/Destinations/Appwrite.php index 256fe00c2f..4b5130e1c6 100644 --- a/src/Migration/Destinations/Appwrite.php +++ b/src/Migration/Destinations/Appwrite.php @@ -1264,6 +1264,10 @@ class Appwrite extends Destination $this->users->updateLabels($resource->getId(), $resource->getLabels()); } + if (!empty($resource->getTargets())) { + $this->importUserTargets($resource->getId(), $resource->getTargets()); + } + break; case Resource::TYPE_TEAM: /** @var Team $resource */ @@ -1722,6 +1726,49 @@ class Appwrite extends Destination return $resource; } + /** + * Import user targets not auto-created by the server (e.g. push). + * providerInternalId is resolved later in createProvider(). + * + * @param array> $targets + */ + protected function importUserTargets(string $userId, array $targets): void + { + $userDoc = null; + + foreach ($targets as $target) { + switch ($target['providerType'] ?? '') { + case 'email': + case 'sms': + // Auto-created by the server when a user is created with an email/phone + break; + case 'push': + $userDoc ??= $this->dbForProject->getDocument('users', $userId); + + $this->dbForProject->createDocument('targets', new UtopiaDocument([ + '$id' => $target['$id'], + '$permissions' => [ + Permission::read(Role::user($userId)), + Permission::update(Role::user($userId)), + Permission::delete(Role::user($userId)), + ], + 'userId' => $userId, + 'userInternalId' => $userDoc->getSequence(), + 'providerType' => $target['providerType'], + 'providerId' => $target['providerId'] ?? null, + 'identifier' => $target['identifier'], + 'name' => $target['name'] ?? null, + 'expired' => $target['expired'] ?? false, + ])); + break; + } + } + + if ($userDoc !== null) { + $this->dbForProject->purgeCachedDocument('users', $userId); + } + } + /** * @throws AppwriteException * @throws \Exception @@ -1845,6 +1892,19 @@ class Appwrite extends Destination ), default => throw new \Exception('Unknown provider: ' . $resource->getProvider()), }; + + // Resolve providerInternalId for push targets that were written during GROUP_AUTH + // before the provider existed on the destination. + $provider = $this->dbForProject->getDocument('providers', $id); + $targets = $this->dbForProject->find('targets', [ + Query::equal('providerId', [$id]), + Query::isNull('providerInternalId'), + ]); + + foreach ($targets as $target) { + $target->setAttribute('providerInternalId', $provider->getSequence()); + $this->dbForProject->updateDocument('targets', $target->getId(), $target); + } } /** diff --git a/src/Migration/Resources/Auth/User.php b/src/Migration/Resources/Auth/User.php index e1cf80b638..de91fec7d3 100644 --- a/src/Migration/Resources/Auth/User.php +++ b/src/Migration/Resources/Auth/User.php @@ -19,6 +19,7 @@ class User extends Resource * @param bool $phoneVerified * @param bool $disabled * @param array $preferences + * @param array> $targets */ public function __construct( string $id, @@ -31,7 +32,8 @@ class User extends Resource private readonly bool $emailVerified = false, private readonly bool $phoneVerified = false, private readonly bool $disabled = false, - private readonly array $preferences = [] + private readonly array $preferences = [], + private readonly array $targets = [], ) { $this->id = $id; } @@ -53,7 +55,8 @@ class User extends Resource $array['emailVerified'] ?? false, $array['phoneVerified'] ?? false, $array['disabled'] ?? false, - $array['preferences'] ?? [] + $array['preferences'] ?? [], + $array['targets'] ?? [], ); } @@ -74,6 +77,7 @@ class User extends Resource 'phoneVerified' => $this->phoneVerified, 'disabled' => $this->disabled, 'preferences' => $this->preferences, + 'targets' => $this->targets, ]; } @@ -172,4 +176,14 @@ class User extends Resource { return $this->preferences; } + + /** + * Get Targets + * + * @return array> + */ + public function getTargets(): array + { + return $this->targets; + } } diff --git a/src/Migration/Sources/Appwrite.php b/src/Migration/Sources/Appwrite.php index ca00f1beba..0aa7a02430 100644 --- a/src/Migration/Sources/Appwrite.php +++ b/src/Migration/Sources/Appwrite.php @@ -617,6 +617,7 @@ class Appwrite extends Source $user['phoneVerification'] ?? false, !$user['status'], $user['prefs'] ?? [], + $user['targets'] ?? [], ); $lastDocument = $user['$id']; From e70a69812d0cd4ecfb9911e6e8c83d46cb8815a4 Mon Sep 17 00:00:00 2001 From: Prem Palanisamy Date: Wed, 11 Mar 2026 12:07:11 +0000 Subject: [PATCH 2/2] fix: add timestamps and cache invalidation for push target migration --- src/Migration/Destinations/Appwrite.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/Migration/Destinations/Appwrite.php b/src/Migration/Destinations/Appwrite.php index 4b5130e1c6..05c62470cf 100644 --- a/src/Migration/Destinations/Appwrite.php +++ b/src/Migration/Destinations/Appwrite.php @@ -1745,8 +1745,13 @@ class Appwrite extends Destination case 'push': $userDoc ??= $this->dbForProject->getDocument('users', $userId); + $createdAt = $this->normalizeDateTime($target['$createdAt'] ?? null); + $updatedAt = $this->normalizeDateTime($target['$updatedAt'] ?? null, $createdAt); + $this->dbForProject->createDocument('targets', new UtopiaDocument([ '$id' => $target['$id'], + '$createdAt' => $createdAt, + '$updatedAt' => $updatedAt, '$permissions' => [ Permission::read(Role::user($userId)), Permission::update(Role::user($userId)), @@ -1901,9 +1906,16 @@ class Appwrite extends Destination Query::isNull('providerInternalId'), ]); + $userIds = []; + foreach ($targets as $target) { $target->setAttribute('providerInternalId', $provider->getSequence()); $this->dbForProject->updateDocument('targets', $target->getId(), $target); + $userIds[$target->getAttribute('userId')] = true; + } + + foreach (array_keys($userIds) as $userId) { + $this->dbForProject->purgeCachedDocument('users', $userId); } }