1
0
Fork 0
mirror of synced 2024-06-03 03:14:50 +12:00
appwrite/src/Appwrite/Messaging/Adapter/Realtime.php

301 lines
11 KiB
PHP
Raw Normal View History

2021-06-29 02:34:28 +12:00
<?php
namespace Appwrite\Messaging\Adapter;
2021-06-30 23:36:58 +12:00
use Appwrite\Database\Document;
2021-06-29 02:34:28 +12:00
use Appwrite\Messaging\Adapter;
2021-06-30 23:36:58 +12:00
use Utopia\App;
2021-06-29 02:34:28 +12:00
class Realtime extends Adapter
{
/**
* Connection Tree
*
* [CONNECTION_ID] ->
* 'projectId' -> [PROJECT_ID]
* 'roles' -> [ROLE_x, ROLE_Y]
* 'channels' -> [CHANNEL_NAME_X, CHANNEL_NAME_Y, CHANNEL_NAME_Z]
*/
public array $connections = [];
/**
* Subscription Tree
*
* [PROJECT_ID] ->
* [ROLE_X] ->
* [CHANNEL_NAME_X] -> [CONNECTION_ID]
* [CHANNEL_NAME_Y] -> [CONNECTION_ID]
* [CHANNEL_NAME_Z] -> [CONNECTION_ID]
* [ROLE_Y] ->
* [CHANNEL_NAME_X] -> [CONNECTION_ID]
* [CHANNEL_NAME_Y] -> [CONNECTION_ID]
* [CHANNEL_NAME_Z] -> [CONNECTION_ID]
*/
public array $subscriptions = [];
/**
* Adds a subscribtion.
* @param string $projectId Project ID.
* @param mixed $connection Unique Identifier - Connection ID.
* @param array $roles Roles of the Subscription.
* @param array $channels Subscribed Channels.
* @return void
*/
public function subscribe(string $projectId, mixed $connection, array $roles, array $channels): void
{
2021-07-14 03:18:02 +12:00
//TODO: merge project & channel to a single layer
2021-06-29 02:34:28 +12:00
if (!isset($this->subscriptions[$projectId])) { // Init Project
$this->subscriptions[$projectId] = [];
}
foreach ($roles as $role) {
if (!isset($this->subscriptions[$projectId][$role])) { // Add user first connection
$this->subscriptions[$projectId][$role] = [];
}
foreach ($channels as $channel => $list) {
$this->subscriptions[$projectId][$role][$channel][$connection] = true;
}
}
$this->connections[$connection] = [
'projectId' => $projectId,
'roles' => $roles,
'channels' => $channels
];
}
/**
2021-07-14 03:18:02 +12:00
* Removes Subscription.
2021-06-29 02:34:28 +12:00
*
* @param mixed $connection
* @return void
*/
public function unsubscribe(mixed $connection): void
{
$projectId = $this->connections[$connection]['projectId'] ?? '';
$roles = $this->connections[$connection]['roles'] ?? [];
foreach ($roles as $role) {
foreach ($this->subscriptions[$projectId][$role] as $channel => $list) {
unset($this->subscriptions[$projectId][$role][$channel][$connection]); // Remove connection
if (empty($this->subscriptions[$projectId][$role][$channel])) {
unset($this->subscriptions[$projectId][$role][$channel]); // Remove channel when no connections
}
}
if (empty($this->subscriptions[$projectId][$role])) {
unset($this->subscriptions[$projectId][$role]); // Remove role when no channels
}
}
if (empty($this->subscriptions[$projectId])) { // Remove project when no roles
unset($this->subscriptions[$projectId]);
}
unset($this->connections[$connection]);
}
/**
* Checks if Channel has a subscriber.
* @param string $projectId
* @param string $role
* @param string $channel
2021-07-14 03:18:02 +12:00
* @return bool
2021-06-29 02:34:28 +12:00
*/
public function hasSubscriber(string $projectId, string $role, string $channel = ''): bool
{
2021-07-14 03:18:02 +12:00
//TODO: look into moving it to an abstract class in the parent class
2021-06-29 02:34:28 +12:00
if (empty($channel)) {
return array_key_exists($projectId, $this->subscriptions)
&& array_key_exists($role, $this->subscriptions[$projectId]);
}
return array_key_exists($projectId, $this->subscriptions)
&& array_key_exists($role, $this->subscriptions[$projectId])
&& array_key_exists($channel, $this->subscriptions[$projectId][$role]);
}
/**
* Sends an event to the Realtime Server.
2021-06-30 23:36:58 +12:00
* @param string $project
2021-06-29 02:34:28 +12:00
* @param array $payload
2021-06-30 23:36:58 +12:00
* @param string $event
* @param array $channels
2021-07-14 03:18:02 +12:00
* @param array $roles
2021-06-30 23:36:58 +12:00
* @param array $options
2021-06-29 02:34:28 +12:00
* @return void
*/
2021-07-14 03:18:02 +12:00
public static function send(string $project, array $payload, string $event, array $channels, array $roles, array $options = []): void
2021-06-29 02:34:28 +12:00
{
2021-07-26 21:05:42 +12:00
if (empty($channels) || empty($roles) || empty($project)) return;
2021-06-30 23:36:58 +12:00
$permissionsChanged = array_key_exists('permissionsChanged', $options) && $options['permissionsChanged'];
$userId = array_key_exists('userId', $options) ? $options['userId'] : null;
2021-07-14 03:18:02 +12:00
$redis = new \Redis(); //TODO: make this part of the constructor
2021-06-30 23:36:58 +12:00
$redis->connect(App::getEnv('_APP_REDIS_HOST', ''), App::getEnv('_APP_REDIS_PORT', ''));
$redis->publish('realtime', json_encode([
'project' => $project,
2021-07-14 03:18:02 +12:00
'roles' => $roles,
2021-06-30 23:36:58 +12:00
'permissionsChanged' => $permissionsChanged,
'userId' => $userId,
'data' => [
'event' => $event,
'channels' => $channels,
'timestamp' => time(),
'payload' => $payload
]
]));
2021-06-29 02:34:28 +12:00
}
/**
* Identifies the receivers of all subscriptions, based on the permissions and event.
*
* Example of performance with an event with user:XXX permissions and with X users spread across 10 different channels:
* - 0.014 ms (±6.88%) | 10 Connections / 100 Subscriptions
* - 0.070 ms (±3.71%) | 100 Connections / 1,000 Subscriptions
* - 0.846 ms (±2.74%) | 1,000 Connections / 10,000 Subscriptions
* - 10.866 ms (±1.01%) | 10,000 Connections / 100,000 Subscriptions
* - 110.201 ms (±2.32%) | 100,000 Connections / 1,000,000 Subscriptions
* - 1,121.328 ms (±0.84%) | 1,000,000 Connections / 10,000,000 Subscriptions
*
* @param array $event
*/
2021-07-14 03:18:02 +12:00
public function getSubscribers(array $event)
2021-06-29 02:34:28 +12:00
{
2021-07-14 03:18:02 +12:00
//TODO: do comments
2021-06-29 02:34:28 +12:00
$receivers = [];
if (isset($this->subscriptions[$event['project']])) {
foreach ($this->subscriptions[$event['project']] as $role => $subscription) {
foreach ($event['data']['channels'] as $channel) {
if (
\array_key_exists($channel, $this->subscriptions[$event['project']][$role])
2021-07-14 03:18:02 +12:00
&& (\in_array($role, $event['roles']) || \in_array('*', $event['roles']))
2021-06-29 02:34:28 +12:00
) {
2021-07-14 03:18:02 +12:00
foreach (array_keys($this->subscriptions[$event['project']][$role][$channel]) as $id) {
$receivers[$id] = 0;
2021-06-29 02:34:28 +12:00
}
break;
}
}
}
}
return array_keys($receivers);
}
2021-06-30 23:36:58 +12:00
/**
* Converts the channels from the Query Params into an array.
* Also renames the account channel to account.USER_ID and removes all illegal account channel variations.
* @param array $channels
2021-07-14 03:18:02 +12:00
* @param string $userId
2021-06-30 23:36:58 +12:00
* @return array
*/
2021-07-14 03:18:02 +12:00
public static function convertChannels(array $channels, string $userId): array
2021-06-30 23:36:58 +12:00
{
$channels = array_flip($channels);
foreach ($channels as $key => $value) {
switch (true) {
case strpos($key, 'account.') === 0:
unset($channels[$key]);
break;
case $key === 'account':
2021-07-14 03:18:02 +12:00
if (!empty($userId)) {
$channels['account.' . $userId] = $value;
2021-06-30 23:36:58 +12:00
}
unset($channels['account']);
break;
}
}
if (\array_key_exists('account', $channels)) {
2021-07-14 03:18:02 +12:00
if ($userId) {
$channels['account.' . $userId] = $channels['account'];
2021-06-30 23:36:58 +12:00
}
unset($channels['account']);
}
return $channels;
}
/**
* Create channels array based on the event name and payload.
*
* @return void
*/
public static function fromPayload(string $event, Document $payload): array
{
$channels = [];
2021-07-14 03:18:02 +12:00
$roles = [];
2021-06-30 23:36:58 +12:00
$permissionsChanged = false;
switch (true) {
case strpos($event, 'account.recovery.') === 0:
case strpos($event, 'account.sessions.') === 0:
case strpos($event, 'account.verification.') === 0:
$channels[] = 'account';
2021-06-30 23:36:58 +12:00
$channels[] = 'account.' . $payload->getAttribute('userId');
2021-07-14 03:18:02 +12:00
$roles = ['user:' . $payload->getAttribute('userId')];
2021-06-30 23:36:58 +12:00
break;
case strpos($event, 'account.') === 0:
$channels[] = 'account';
2021-06-30 23:36:58 +12:00
$channels[] = 'account.' . $payload->getId();
2021-07-14 03:18:02 +12:00
$roles = ['user:' . $payload->getId()];
2021-06-30 23:36:58 +12:00
break;
case strpos($event, 'teams.memberships') === 0:
$permissionsChanged = in_array($event, ['teams.memberships.update', 'teams.memberships.delete', 'teams.memberships.update.status']);
$channels[] = 'memberships';
$channels[] = 'memberships.' . $payload->getId();
2021-07-14 03:18:02 +12:00
$roles = ['team:' . $payload->getAttribute('teamId')];
2021-06-30 23:36:58 +12:00
break;
case strpos($event, 'teams.') === 0:
$permissionsChanged = $event === 'teams.create';
$channels[] = 'teams';
$channels[] = 'teams.' . $payload->getId();
2021-07-14 03:18:02 +12:00
$roles = ['team:' . $payload->getId()];
2021-06-30 23:36:58 +12:00
break;
case strpos($event, 'database.collections.') === 0:
$channels[] = 'collections';
$channels[] = 'collections.' . $payload->getId();
2021-07-14 03:18:02 +12:00
$roles = $payload->getAttribute('$permissions.read');
2021-06-30 23:36:58 +12:00
break;
case strpos($event, 'database.documents.') === 0:
$channels[] = 'documents';
$channels[] = 'collections.' . $payload->getAttribute('$collection') . '.documents';
$channels[] = 'documents.' . $payload->getId();
2021-07-14 03:18:02 +12:00
$roles = $payload->getAttribute('$permissions.read');
2021-06-30 23:36:58 +12:00
break;
case strpos($event, 'storage.') === 0:
$channels[] = 'files';
$channels[] = 'files.' . $payload->getId();
2021-07-14 03:18:02 +12:00
$roles = $payload->getAttribute('$permissions.read');
2021-06-30 23:36:58 +12:00
break;
case strpos($event, 'functions.executions.') === 0:
if (!empty($payload->getAttribute('$permissions.read'))) {
$channels[] = 'executions';
$channels[] = 'executions.' . $payload->getId();
$channels[] = 'functions.' . $payload->getAttribute('functionId');
2021-07-14 03:18:02 +12:00
$roles = $payload->getAttribute('$permissions.read');
2021-06-30 23:36:58 +12:00
}
break;
}
return [
'channels' => $channels,
2021-07-14 03:18:02 +12:00
'roles' => $roles,
2021-06-30 23:36:58 +12:00
'permissionsChanged' => $permissionsChanged
];
}
2021-06-29 02:34:28 +12:00
}