Refactor presence API functions and update expiry test to improve clarity and maintainability. Renamed 'triggerPresenceEvent' to 'getQueueForEventsForProject' and adjusted expiry handling in tests for better validation.

This commit is contained in:
ArnabChatterjee20k
2026-04-30 15:56:31 +05:30
parent 2c06e4185f
commit 32dc870eaf
4 changed files with 23 additions and 20 deletions
+10 -2
View File
@@ -313,7 +313,7 @@ if (!function_exists('triggerPresenceUsage')) {
}
}
if (!function_exists('triggerPresenceEvent')) {
if (!function_exists('getQueueForEventsForProject')) {
function getQueueForEventsForProject(Document $project, User $user): QueueEvent
{
global $register;
@@ -330,7 +330,9 @@ if (!function_exists('triggerPresenceEvent')) {
return $queueForEvents;
}
}
if (!function_exists('triggerPresenceEvent')) {
function triggerPresenceEvent(
Server $server,
Realtime $realtime,
@@ -344,13 +346,16 @@ if (!function_exists('triggerPresenceEvent')) {
}
try {
global $container;
$queueForEvents = getQueueForEventsForProject($project, $user);
$queueForEvents
->setEvent($eventName)
->setParam('presenceId', $presence->getId())
->setPayload($presence->getArrayCopy());
(new QueueRealtime())
/** @var QueueRealtime $queueForRealtime */
$queueForRealtime = $container->get('queueForRealtime');
$queueForRealtime
->setProject($project)
->setUser($user)
->from($queueForEvents)
@@ -399,6 +404,9 @@ global $container;
$container->set('pools', function ($register) {
return $register->get('pools');
}, ['register']);
$container->set('queueForRealtime', function () {
return new QueueRealtime();
}, []);
$realtime = getRealtime();
$presenceState = new PresenceState();
Generated
+6 -6
View File
@@ -3850,16 +3850,16 @@
},
{
"name": "utopia-php/database",
"version": "5.3.22",
"version": "5.4.2",
"source": {
"type": "git",
"url": "https://github.com/utopia-php/database.git",
"reference": "d765945da6b3141852014b2f96ecf1fe7e3d6ba7"
"reference": "a1bb3e2a4fba13032ea625b21a21039c43cffeda"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/utopia-php/database/zipball/d765945da6b3141852014b2f96ecf1fe7e3d6ba7",
"reference": "d765945da6b3141852014b2f96ecf1fe7e3d6ba7",
"url": "https://api.github.com/repos/utopia-php/database/zipball/a1bb3e2a4fba13032ea625b21a21039c43cffeda",
"reference": "a1bb3e2a4fba13032ea625b21a21039c43cffeda",
"shasum": ""
},
"require": {
@@ -3903,9 +3903,9 @@
],
"support": {
"issues": "https://github.com/utopia-php/database/issues",
"source": "https://github.com/utopia-php/database/tree/5.3.22"
"source": "https://github.com/utopia-php/database/tree/5.4.2"
},
"time": "2026-04-20T07:12:46+00:00"
"time": "2026-04-30T09:59:57+00:00"
},
{
"name": "utopia-php/detector",
+1 -8
View File
@@ -14,7 +14,6 @@ use Utopia\Database\Helpers\Permission;
use Utopia\Database\Helpers\Role;
use Utopia\Database\Query;
use Utopia\Database\Validator\Authorization;
use Utopia\System\System;
class PresenceState
{
@@ -66,7 +65,7 @@ class PresenceState
$presenceCreated = false;
try {
if ($this->getSupportForUniqueIndexBasedUpsert()) {
if ($dbForProject->getAdapter()->getSupportForUpsertOnUniqueIndex()) {
$presenceCreated = $dbForProject->findOne('presenceLogs', [Query::equal('userId', [$userId])])->isEmpty();
$presence = $dbForProject->upsertDocument('presenceLogs', $presenceDocument);
} else {
@@ -127,12 +126,6 @@ class PresenceState
});
}
private function getSupportForUniqueIndexBasedUpsert(): bool
{
$adapter = \strtolower(System::getEnv('_APP_DB_ADAPTER', 'mariadb'));
return !\in_array($adapter, ['mongodb', 'postgres', 'postgresql'], true);
}
private function assertPermissionsAgainstAuthorization(array $permissions, Authorization $authorization): void
{
foreach (Database::PERMISSIONS as $type) {
@@ -37,8 +37,8 @@ class PresenceExpiryTest extends Scope
{
$projectId = $this->getProject()['$id'];
$userId = $this->getUser()['$id'];
// Must match the format used by the maintenance worker query.
$expiredAt = DateTime::format((new \DateTime())->modify('-120 seconds'));
// Set a near-future expiry to satisfy validation, then wait until it is in the past.
$expiresAt = DateTime::format((new \DateTime())->modify('+2 seconds'));
$createServer = $this->client->call(
Client::METHOD_PUT,
@@ -68,16 +68,18 @@ class PresenceExpiryTest extends Scope
],
[
'userId' => $userId,
'expiresAt' => $expiredAt,
'expiresAt' => $expiresAt,
]
);
$this->assertEquals(200, $expireServer['headers']['status-code']);
$this->assertEquals(
(new \DateTime($expiredAt))->getTimestamp(),
(new \DateTime($expiresAt))->getTimestamp(),
(new \DateTime($expireServer['body']['expiresAt']))->getTimestamp()
);
\sleep(3);
$stdout = '';
$stderr = '';
$code = Console::execute('docker exec appwrite maintenance --type=trigger', '', $stdout, $stderr);