From 43036a9ba67bf1f64cb83e865d6c3e6604d9cba2 Mon Sep 17 00:00:00 2001 From: Torsten Dittmann Date: Thu, 17 Jun 2021 11:37:52 +0200 Subject: [PATCH] feat(realtime): add membership events --- src/Appwrite/Event/Realtime.php | 11 ++-- src/Appwrite/Realtime/Server.php | 2 +- tests/e2e/Services/Realtime/RealtimeBase.php | 55 +++++++++++++++++++- 3 files changed, 58 insertions(+), 10 deletions(-) diff --git a/src/Appwrite/Event/Realtime.php b/src/Appwrite/Event/Realtime.php index 28eb2f7c34..5942064326 100644 --- a/src/Appwrite/Event/Realtime.php +++ b/src/Appwrite/Event/Realtime.php @@ -142,19 +142,14 @@ class Realtime break; case strpos($this->event, 'teams.memberships') === 0: + $this->permissionsChanged = in_array($this->event, ['teams.memberships.update', 'teams.memberships.delete', 'teams.memberships.update.status']); $this->channels[] = 'memberships'; $this->channels[] = 'memberships.' . $this->payload->getId(); $this->permissions = ['team:' . $this->payload->getAttribute('teamId')]; - break; - case strpos($this->event, 'teams.create') === 0: - $this->permissionsChanged = true; - $this->channels[] = 'teams'; - $this->channels[] = 'teams.' . $this->payload->getId(); - $this->permissions = ['user:' . $this->userId]; - break; case strpos($this->event, 'teams.') === 0: + $this->permissionsChanged = $this->event === 'teams.create'; $this->channels[] = 'teams'; $this->channels[] = 'teams.' . $this->payload->getId(); $this->permissions = ['team:' . $this->payload->getId()]; @@ -187,7 +182,7 @@ class Realtime $this->permissions = $this->payload->getAttribute('$permissions.read'); } break; - } + } } /** diff --git a/src/Appwrite/Realtime/Server.php b/src/Appwrite/Realtime/Server.php index e9e79913ec..4351e22ce2 100644 --- a/src/Appwrite/Realtime/Server.php +++ b/src/Appwrite/Realtime/Server.php @@ -324,7 +324,7 @@ class Server { $event = json_decode($payload, true); - if ($event['permissionsChanged'] && $event['userId']) { + if ($event['permissionsChanged'] && isset($event['userId'])) { $this->addPermission($event); } diff --git a/tests/e2e/Services/Realtime/RealtimeBase.php b/tests/e2e/Services/Realtime/RealtimeBase.php index 305345c11f..c17f90b934 100644 --- a/tests/e2e/Services/Realtime/RealtimeBase.php +++ b/tests/e2e/Services/Realtime/RealtimeBase.php @@ -687,7 +687,7 @@ trait RealtimeBase $client->close(); } - public function testChannelTeams() + public function testChannelTeams(): array { $user = $this->getUser(); $session = $user['session'] ?? ''; @@ -750,5 +750,58 @@ trait RealtimeBase $this->assertNotEmpty($response['payload']); $client->close(); + + return ['teamId' => $teamId]; + } + + /** + * @depends testChannelTeams + */ + public function testChannelMemberships(array $data) + { + $teamId = $data['teamId'] ?? ''; + + $user = $this->getUser(); + $session = $user['session'] ?? ''; + $projectId = $this->getProject()['$id']; + + $client = $this->getWebsocket(['memberships'], [ + 'origin' => 'http://localhost', + 'cookie' => 'a_session_'.$projectId.'='.$session + ]); + + $response = json_decode($client->receive(), true); + + $this->assertCount(1, $response); + $this->assertArrayHasKey('memberships', $response); + + $response = $this->client->call(Client::METHOD_GET, '/teams/'.$teamId.'/memberships', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders())); + + $membershipId = $response['body']['memberships'][0]['$id']; + + /** + * Test Update Membership + */ + $roles = ['admin', 'editor', 'uncle']; + $this->client->call(Client::METHOD_PATCH, '/teams/'.$teamId.'/memberships/'.$membershipId, array_merge([ + 'origin' => 'http://localhost', + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()), [ + 'roles' => $roles + ]); + + $response = json_decode($client->receive(), true); + $this->assertArrayHasKey('timestamp', $response); + $this->assertCount(2, $response['channels']); + $this->assertContains('memberships', $response['channels']); + $this->assertContains('memberships.' . $membershipId, $response['channels']); + $this->assertEquals('teams.memberships.update', $response['event']); + $this->assertNotEmpty($response['payload']); + + $client->close(); } }