appwrite/src/Appwrite/Event/Realtime.php

115 lines
2.8 KiB
PHP
Raw Normal View History

2024-11-04 15:05:54 +00:00
<?php
namespace Appwrite\Event;
use Appwrite\Messaging\Adapter;
2024-11-04 15:05:54 +00:00
use Appwrite\Messaging\Adapter\Realtime as RealtimeAdapter;
use Utopia\Database\Document;
use Utopia\Database\Exception;
2024-11-04 15:05:54 +00:00
class Realtime extends Event
{
2025-02-27 07:23:13 +00:00
protected array $subscribers = [];
2025-02-18 14:04:59 +00:00
private Adapter $realtime;
protected bool $critical = false;
2024-11-04 15:05:54 +00:00
public function __construct()
{
$this->realtime = new Adapter\Realtime();
2024-11-04 15:05:54 +00:00
}
2025-02-18 14:04:59 +00:00
/**
* Get Realtime payload for this event.
*
* @return array
*/
2024-11-04 15:05:54 +00:00
public function getRealtimePayload(): array
{
$payload = [];
foreach ($this->payload as $key => $value) {
if (!isset($this->sensitive[$key])) {
$payload[$key] = $value;
}
}
return $payload;
}
2025-02-18 14:04:59 +00:00
/**
2025-02-27 07:23:13 +00:00
* Set subscribers for this realtime event.
2025-02-18 14:04:59 +00:00
*
2025-02-27 07:23:13 +00:00
* @param array $subscribers
2025-03-13 11:04:20 +00:00
* @return self
2025-02-18 14:04:59 +00:00
*/
2025-02-27 07:23:13 +00:00
public function setSubscribers(array $subscribers): self
2025-02-18 14:04:59 +00:00
{
2025-02-27 07:23:13 +00:00
$this->subscribers = $subscribers;
2025-02-18 14:04:59 +00:00
return $this;
}
/**
2025-02-27 07:23:13 +00:00
* Get subscribers for this realtime event.
2025-02-18 14:04:59 +00:00
*
* @return array
*/
2025-02-27 07:23:13 +00:00
public function getSubscribers(): array
2025-02-18 14:04:59 +00:00
{
2025-02-27 07:23:13 +00:00
return $this->subscribers;
2025-02-18 14:04:59 +00:00
}
2024-11-04 15:05:54 +00:00
/**
* Execute Event.
*
* @return string|bool
* @throws Exception
2024-11-04 15:05:54 +00:00
*/
public function trigger(): string|bool
{
2024-11-08 20:55:53 +00:00
if ($this->paused || empty($this->event)) {
2024-11-04 15:05:54 +00:00
return false;
}
2025-06-11 06:51:48 +00:00
$allEvents = Event::generateEvents($this->getEvent(), $this->getParams());
2025-05-09 09:17:01 +00:00
2024-11-04 15:05:54 +00:00
$payload = new Document($this->getPayload());
$db = $this->getContext('database');
$bucket = $this->getContext('bucket');
2025-05-09 09:17:01 +00:00
// Can be Tables API or Collections API; generated channels include both!
2025-05-07 05:03:54 +00:00
$tableOrCollection = $this->getContext('table') ?? $this->getContext('collection');
2024-11-04 15:05:54 +00:00
$target = RealtimeAdapter::fromPayload(
2025-06-11 06:51:48 +00:00
event: $allEvents[0],
2024-11-04 15:05:54 +00:00
payload: $payload,
project: $this->getProject(),
database: $db,
2025-05-09 09:17:01 +00:00
collection: $tableOrCollection,
2024-11-04 15:05:54 +00:00
bucket: $bucket,
);
2025-02-27 07:23:13 +00:00
$projectIds = !empty($this->getSubscribers())
? $this->getSubscribers()
2025-02-26 16:24:05 +00:00
: [$target['projectId'] ?? $this->getProject()->getId()];
2025-02-26 16:50:12 +00:00
foreach ($projectIds as $projectId) {
$this->realtime->send(
2025-02-26 16:50:12 +00:00
projectId: $projectId,
payload: $this->getRealtimePayload(),
2025-06-11 06:51:48 +00:00
events: $allEvents,
2025-02-26 16:50:12 +00:00
channels: $target['channels'],
roles: $target['roles'],
options: [
'permissionsChanged' => $target['permissionsChanged'],
'userId' => $this->getParam('userId')
]
);
}
2024-11-04 15:05:54 +00:00
return true;
}
}