appwrite/src/Appwrite/Event/Messaging.php

198 lines
4.1 KiB
PHP
Raw Normal View History

<?php
namespace Appwrite\Event;
2023-10-13 12:56:54 +00:00
use Utopia\Database\Document;
use Utopia\Queue\Client;
2024-03-06 17:34:21 +00:00
use Utopia\Queue\Connection;
class Messaging extends Event
{
2024-02-20 12:06:35 +00:00
protected string $type = '';
protected ?string $messageId = null;
2023-11-15 20:00:47 +00:00
protected ?Document $message = null;
protected ?array $recipients = null;
2023-11-29 04:09:44 +00:00
protected ?string $scheduledAt = null;
2023-11-15 20:00:47 +00:00
protected ?string $providerType = null;
2023-11-15 20:03:05 +00:00
public function __construct(protected Connection $connection)
{
parent::__construct($connection);
$this
->setQueue(Event::MESSAGING_QUEUE_NAME)
->setClass(Event::MESSAGING_CLASS_NAME);
}
2023-09-05 17:40:33 +00:00
2024-02-20 12:06:35 +00:00
/**
* Sets type for the build event.
*
* @param string $type Can be `MESSAGE_TYPE_INTERNAL` or `MESSAGE_TYPE_EXTERNAL`.
* @return self
*/
public function setType(string $type): self
{
$this->type = $type;
return $this;
}
/**
* Returns set type for the function event.
*
* @return string
*/
public function getType(): string
{
return $this->type;
}
2023-11-15 20:00:47 +00:00
/**
* Sets recipient for the messaging event.
*
* @param string[] $recipients
* @return self
*/
public function setRecipients(array $recipients): self
{
$this->recipients = $recipients;
return $this;
}
/**
* Returns set recipient for messaging event.
*
* @return string[]
*/
public function getRecipient(): array
{
return $this->recipients;
}
/**
* Sets message document for the messaging event.
*
* @param Document $message
* @return self
*/
public function setMessage(Document $message): self
{
$this->message = $message;
return $this;
}
/**
* Returns message document for the messaging event.
*
* @return string
*/
public function getMessage(): Document
{
return $this->message;
}
/**
* Sets message ID for the messaging event.
*
* @param string $message
* @return self
*/
public function setMessageId(string $messageId): self
{
$this->messageId = $messageId;
return $this;
}
/**
* Returns set message ID for the messaging event.
*
* @return string
*/
public function getMessageId(): string
{
return $this->messageId;
}
2023-11-15 20:00:47 +00:00
/**
* Sets provider type for the messaging event.
*
* @param string $providerType
* @return self
*/
public function setProviderType(string $providerType): self
{
$this->providerType = $providerType;
return $this;
}
/**
* Returns set provider type for the messaging event.
*
* @return string
*/
public function getProviderType(): string
{
return $this->providerType;
}
/**
2023-11-29 04:09:44 +00:00
* Sets Scheduled delivery time for the messaging event.
*
2023-11-29 04:09:44 +00:00
* @param string $scheduledAt
2023-10-09 11:59:26 +00:00
* @return self
*/
2023-11-29 04:09:44 +00:00
public function setScheduledAt(string $scheduledAt): self
2023-10-09 11:59:26 +00:00
{
2023-11-29 04:09:44 +00:00
$this->scheduledAt = $scheduledAt;
2023-10-09 11:59:26 +00:00
return $this;
}
/**
* Returns set Delivery Time for the messaging event.
*
* @return string
*/
2023-11-29 04:09:44 +00:00
public function getScheduledAt(): string
2023-10-09 11:59:26 +00:00
{
2023-11-29 04:09:44 +00:00
return $this->scheduledAt;
2023-10-09 11:59:26 +00:00
}
2023-10-13 12:56:54 +00:00
/**
* Set project for this event.
*
* @param Document $project
* @return self
*/
public function setProject(Document $project): self
{
$this->project = $project;
return $this;
}
2023-10-09 11:59:26 +00:00
/**
* Executes the event and sends it to the messaging worker.
2023-10-13 12:56:54 +00:00
* @return string|bool
* @throws \InvalidArgumentException
*/
2023-10-09 11:59:26 +00:00
public function trigger(): string | bool
{
$client = new Client($this->queue, $this->connection);
2023-10-13 12:56:54 +00:00
return $client->enqueue([
2024-02-20 13:20:09 +00:00
'type' => $this->type,
'project' => $this->project,
'user' => $this->user,
'messageId' => $this->messageId,
2023-11-15 20:00:47 +00:00
'message' => $this->message,
'recipients' => $this->recipients,
'providerType' => $this->providerType,
]);
}
2023-11-15 20:03:05 +00:00
}