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.
This commit is contained in:
Prem Palanisamy committed 2026-03-11 11:30:21 +00:00
1 parent bbdd8efd36
commit 688b7b6aa1
3 files changed
+77 -2

No files matched your search

+60
View File
@@ -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<array<string, mixed>> $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);
}
}
/**
+16 -2
View File
@@ -19,6 +19,7 @@ class User extends Resource
* @param bool $phoneVerified
* @param bool $disabled
* @param array<string, mixed> $preferences
* @param array<array<string, mixed>> $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<array<string, mixed>>
*/
public function getTargets(): array
{
return $this->targets;
}
}
+1
View File
@@ -617,6 +617,7 @@ class Appwrite extends Source
$user['phoneVerification'] ?? false,
!$user['status'],
$user['prefs'] ?? [],
$user['targets'] ?? [],
);
$lastDocument = $user['$id'];