Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: send event when deleting all sessions #3014

Closed
wants to merge 11 commits into from
22 changes: 10 additions & 12 deletions app/realtime.php
Original file line number Diff line number Diff line change
Expand Up @@ -321,19 +321,17 @@ function () use ($register, $db, $redis) {

if ($realtime->hasSubscriber($projectId, 'user:' . $userId)) {
$connection = array_key_first(reset($realtime->subscriptions[$projectId]['user:' . $userId]));
} else {
return;

[$database, $returnDatabase] = getDatabase($register, "_{$projectId}");

$user = $database->getDocument('users', $userId);

$roles = Auth::getRoles($user);

$realtime->subscribe($projectId, $connection, $roles, $realtime->connections[$connection]['channels']);

call_user_func($returnDatabase);
}

[$database, $returnDatabase] = getDatabase($register, "_{$projectId}");

$user = $database->getDocument('users', $userId);

$roles = Auth::getRoles($user);

$realtime->subscribe($projectId, $connection, $roles, $realtime->connections[$connection]['channels']);

call_user_func($returnDatabase);
}

$receivers = $realtime->getSubscribers($event);
Expand Down
14 changes: 13 additions & 1 deletion src/Appwrite/Messaging/Adapter/Realtime.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,14 +249,26 @@ public static function fromPayload(string $event, Document $payload, Document $p

switch (true) {
case strpos($event, 'account.recovery.') === 0:
case strpos($event, 'account.sessions.') === 0:
case strpos($event, 'account.verification.') === 0:
$channels[] = 'account';
$channels[] = 'account.' . $payload->getAttribute('userId');
$roles = ['user:' . $payload->getAttribute('userId')];

break;
case strpos($event, 'account.sessions.') === 0:
case strpos($event, 'users.sessions.') === 0:
$channels[] = 'account';
$userId = $payload->getAttribute('userId');
if (empty($userId)) {
$sessions = $payload->getAttribute('sessions', []);
$userId = isset($sessions[0]) ? $sessions[0]->getAttribute('userId') : '';
}
$channels[] = 'account.' . $userId;
$roles = ['user:' . $userId];

break;
case strpos($event, 'account.') === 0:
case strpos($event, 'users.') === 0:
$channels[] = 'account';
$channels[] = 'account.' . $payload->getId();
$roles = ['user:' . $payload->getId()];
Expand Down
121 changes: 119 additions & 2 deletions tests/e2e/Services/Realtime/RealtimeCustomClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1171,7 +1171,7 @@ public function testChannelTeams(): array
/**
* @depends testChannelTeams
*/
public function testChannelMemberships(array $data)
public function testChannelMemberships(array $data): array
{
$teamId = $data['teamId'] ?? '';

Expand Down Expand Up @@ -1205,7 +1205,7 @@ public function testChannelMemberships(array $data)
/**
* Test Update Membership
*/
$roles = ['admin', 'editor', 'uncle'];
$roles = ['owner', 'editor', 'uncle'];
$this->client->call(Client::METHOD_PATCH, '/teams/'.$teamId.'/memberships/'.$membershipId, array_merge([
'origin' => 'http:https://localhost',
'content-type' => 'application/json',
Expand All @@ -1227,6 +1227,123 @@ public function testChannelMemberships(array $data)
$this->assertEquals('teams.memberships.update', $response['data']['event']);
$this->assertNotEmpty($response['data']['payload']);

$client->close();
return ['teamId' => $teamId];
}


/**
* @depends testChannelMemberships
*/
public function testUpdateMembershipStatus(array $data)
{
$teamId = $data['teamId'] ?? '';

$user = $this->getUser();
$session = $user['session'] ?? '';
$projectId = $this->getProject()['$id'];

$client = $this->getWebsocket(['memberships'], [
'origin' => 'http:https://localhost',
'cookie' => 'a_session_'.$projectId.'='.$session
]);

$response = json_decode($client->receive(), true);

$this->assertArrayHasKey('type', $response);
$this->assertArrayHasKey('data', $response);
$this->assertEquals('connected', $response['type']);
$this->assertNotEmpty($response['data']);
$this->assertCount(1, $response['data']['channels']);
$this->assertContains('memberships', $response['data']['channels']);
$this->assertNotEmpty($response['data']['user']);
$this->assertEquals($user['$id'], $response['data']['user']['$id']);

/**
* Create Team Membership
*/
$email = uniqid().'[email protected]';
$name = 'Friend User';
$response = $this->client->call(Client::METHOD_POST, '/teams/'.$teamId.'/memberships', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'email' => $email,
'name' => $name,
'roles' => ['admin', 'editor'],
'url' => 'http:https://localhost:5000/join-us#title'
]);

$this->assertEquals(201, $response['headers']['status-code']);
$this->assertNotEmpty($response['body']['$id']);
$this->assertNotEmpty($response['body']['userId']);
$this->assertNotEmpty($response['body']['teamId']);
$this->assertCount(2, $response['body']['roles']);
$this->assertIsInt($response['body']['joined']);
$this->assertEquals(false, $response['body']['confirm']);

$lastEmail = $this->getLastEmail();

$secret = substr($lastEmail['text'], strpos($lastEmail['text'], '&secret=', 0) + 8, 256);
$membershipId = substr($lastEmail['text'], strpos($lastEmail['text'], '?membershipId=', 0) + 14, 20);
$userId = substr($lastEmail['text'], strpos($lastEmail['text'], '&userId=', 0) + 8, 20);

$response = json_decode($client->receive(), true);

$this->assertArrayHasKey('type', $response);
$this->assertArrayHasKey('data', $response);
$this->assertEquals('event', $response['type']);
$this->assertNotEmpty($response['data']);
$this->assertCount(2, $response['data']['channels']);
$this->assertContains('memberships', $response['data']['channels']);
$this->assertNotEmpty($response['data']['payload']);
$this->assertEquals($userId, $response['data']['payload']['userId']);
$this->assertEquals($membershipId, $response['data']['payload']['$id']);
$this->assertEquals($teamId, $response['data']['payload']['teamId']);
$this->assertEquals($email, $response['data']['payload']['email']);
$this->assertEquals($name, $response['data']['payload']['name']);
$this->assertCount(2, $response['data']['payload']['roles']);
$this->assertContains('admin', $response['data']['payload']['roles']);
$this->assertContains('editor', $response['data']['payload']['roles']);

/**
* Test Update Membership Status
*/
$response = $this->client->call(Client::METHOD_PATCH, '/teams/'.$teamId.'/memberships/'.$membershipId. '/status', array_merge([
'origin' => 'http:https://localhost',
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
]), [
'userId' => $userId,
'secret' => $secret,
]);

$this->assertEquals(200, $response['headers']['status-code']);

/**
* Check if realtime event was sent
*/
$response = json_decode($client->receive(), true);

$this->assertArrayHasKey('type', $response);
$this->assertArrayHasKey('data', $response);
$this->assertEquals('event', $response['type']);
$this->assertNotEmpty($response['data']);
$this->assertArrayHasKey('timestamp', $response['data']);
$this->assertCount(2, $response['data']['channels']);
$this->assertContains('memberships', $response['data']['channels']);
$this->assertContains('memberships.' . $membershipId, $response['data']['channels']);
$this->assertEquals('teams.memberships.update.status', $response['data']['event']);
$this->assertNotEmpty($response['data']['payload']);
$this->assertEquals($userId, $response['data']['payload']['userId']);
$this->assertEquals($membershipId, $response['data']['payload']['$id']);
$this->assertEquals($teamId, $response['data']['payload']['teamId']);
$this->assertEquals($email, $response['data']['payload']['email']);
$this->assertEquals($name, $response['data']['payload']['name']);
$this->assertCount(2, $response['data']['payload']['roles']);
$this->assertContains('admin', $response['data']['payload']['roles']);
$this->assertContains('editor', $response['data']['payload']['roles']);

$client->close();
}
}