1
0
Fork 0
mirror of synced 2024-06-02 02:44:47 +12:00

feat(realtime): add membership events

This commit is contained in:
Torsten Dittmann 2021-06-17 11:37:52 +02:00
parent 6ebf6bd155
commit 43036a9ba6
3 changed files with 58 additions and 10 deletions

View file

@ -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;
}
}
}
/**

View file

@ -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);
}

View file

@ -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();
}
}