From 0d2a4da3470fdd025ee4edfbc0ffce659bbe4fbb Mon Sep 17 00:00:00 2001 From: ArnabChatterjee20k Date: Fri, 17 Apr 2026 12:01:56 +0530 Subject: [PATCH] updated the presence helper --- app/realtime.php | 23 ++++--------------- .../Modules/Presences/HTTP/Upsert.php | 20 ++++------------ 2 files changed, 9 insertions(+), 34 deletions(-) diff --git a/app/realtime.php b/app/realtime.php index 6dbee667e3..7fea934567 100644 --- a/app/realtime.php +++ b/app/realtime.php @@ -1,5 +1,6 @@ set('pools', function ($register) { }, ['register']); $realtime = getRealtime(); +$presenceState = new PresenceState(); /** * Table for statistics across all workers. @@ -915,7 +916,7 @@ $server->onOpen(function (int $connection, SwooleRequest $request) use ($server, } }); -$server->onMessage(function (int $connection, string $message) use ($server, $realtime, $containerId) { +$server->onMessage(function (int $connection, string $message) use ($server, $realtime, $containerId, $presenceState) { $project = null; $authorization = null; try { @@ -1219,24 +1220,10 @@ $server->onMessage(function (int $connection, string $message) use ($server, $re $presenceData['metadata'] = $message['data']['metadata']; } $presenceDocument = new Document($presenceData); - setPermission($presenceDocument, $message['data']['permissions'] ?? null, $authorization); + $presenceState->setPermissions($presenceDocument, $message['data']['permissions'] ?? null, $user, $authorization); $presenceId = $message['data']['presenceId'] ?? 'unique()'; - if ($presenceId !== 'unique()') { - $presenceDocument->setAttribute('$id', $presenceId); - } - /** @var Document|null $presence */ - $presence = null; - try { - $presence = $database->upsertDocument('presenceLogs', $presenceDocument); - } catch (Duplicate $th) { - // will be triggerd in case of mongodb adapter everytime as $id needs to be same as well here - // in mongodb , upsert works on basis of set and unset by comparing the document with the existing one - // if presenceId differs then it will create a new document and not update the existing one - // TODO: send better error message telling about the presenceId mismatch - $existingPresence = $database->findOne('presenceLogs', [Query::equal('userId', [$userId])]); - $presence = $database->updateDocument('presenceLogs', $existingPresence->getId(), $presenceDocument); - } + $presence = $presenceState->upsertForUser($database, $presenceDocument, $presenceId, $userId); $presence->removeAttribute('hostname'); diff --git a/src/Appwrite/Platform/Modules/Presences/HTTP/Upsert.php b/src/Appwrite/Platform/Modules/Presences/HTTP/Upsert.php index 5b6d6051a2..eece989311 100644 --- a/src/Appwrite/Platform/Modules/Presences/HTTP/Upsert.php +++ b/src/Appwrite/Platform/Modules/Presences/HTTP/Upsert.php @@ -2,6 +2,7 @@ namespace Appwrite\Platform\Modules\Presences\HTTP; +use Appwrite\Databases\PresenceState; use Appwrite\Event\Event; use Appwrite\Extend\Exception; use Appwrite\Platform\Modules\Presences\HTTP\Action as PresenceAction; @@ -13,7 +14,6 @@ use Appwrite\Utopia\Response; use Utopia\Database\Database; use Utopia\Database\DateTime; use Utopia\Database\Document; -use Utopia\Database\Exception\Duplicate; use Utopia\Database\Validator\Authorization; use Utopia\Database\Validator\Datetime as DatetimeValidator; use Utopia\Database\Validator\Permissions; @@ -120,22 +120,10 @@ class Upsert extends PresenceAction 'metadata' => $metadata, ]; + $presenceState = new PresenceState(); $presenceDocument = new Document($presenceData); - $this->setPermission($presenceDocument, $permissions, $user, $authorization); - - if ($presenceId !== 'unique()') { - $presenceDocument->setAttribute('$id', $presenceId); - } - try { - $presence = $dbForProject->upsertDocument('presenceLogs', $presenceDocument); - } catch (Duplicate $th) { - // will be triggerd in case of mongodb adapter everytime as $id needs to be same as well here - // in mongodb , upsert works on basis of set and unset by comparing the document with the existing one - // if presenceId differs then it will create a new document and not update the existing one - // TODO: send better error message telling about the presenceId mismatch - $existingPresence = $dbForProject->findOne('presenceLogs', [Query::equal('userId', [$userId])]); - $presence = $dbForProject->updateDocument('presenceLogs', $existingPresence->getId(), $presenceDocument); - } + $presenceState->setPermissions($presenceDocument, $permissions, $user, $authorization); + $presence = $presenceState->upsertForUser($dbForProject, $presenceDocument, $presenceId, $resolvedUserId); $queueForEvents->setParam('presenceId', $presence->getId()); $response->dynamic($presence, Response::MODEL_PRESENCE);