mirror of
https://github.com/appwrite/appwrite.git
synced 2026-11-18 20:36:03 +00:00
Merge pull request #158 from utopia-php/user_push_target_migration
This commit is contained in:
3 files changed
+89
-2
No files matched your search
@@ -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,54 @@ 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);
|
||||
|
||||
$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)),
|
||||
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 +1897,26 @@ 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'),
|
||||
]);
|
||||
|
||||
$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);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -617,6 +617,7 @@ class Appwrite extends Source
|
||||
$user['phoneVerification'] ?? false,
|
||||
!$user['status'],
|
||||
$user['prefs'] ?? [],
|
||||
$user['targets'] ?? [],
|
||||
);
|
||||
|
||||
$lastDocument = $user['$id'];
|
||||
|
||||
Reference in new issue
Block a user