diff options
author | Daniel Weipert <git@mail.dweipert.de> | 2025-09-19 14:29:17 +0200 |
---|---|---|
committer | Daniel Weipert <git@mail.dweipert.de> | 2025-09-19 14:29:17 +0200 |
commit | 2ae0c2fa2a0bb5a7cd1fd9da1c6d2a6090126e67 (patch) | |
tree | f8cd230b33f36cbee57df276e52f1d393c296e13 /src/Events | |
parent | a20d44d64f02f86d23cba1f1a886352c5f744a1a (diff) |
sending messages creates message event in database
Diffstat (limited to 'src/Events')
-rw-r--r-- | src/Events/Event.php | 6 | ||||
-rw-r--r-- | src/Events/PresenceEvent.php | 2 | ||||
-rw-r--r-- | src/Events/RoomMemberEvent.php | 7 | ||||
-rw-r--r-- | src/Events/RoomMessageEvent.php | 86 | ||||
-rw-r--r-- | src/Events/RoomNameEvent.php | 2 |
5 files changed, 98 insertions, 5 deletions
diff --git a/src/Events/Event.php b/src/Events/Event.php index cab1b14..7dbf242 100644 --- a/src/Events/Event.php +++ b/src/Events/Event.php @@ -11,6 +11,10 @@ abstract class Event ) {} - abstract public function fromJson(string $json): static; + abstract public function fromJson(string $json): self; + + /** + * @return array<string, mixed> + */ abstract public function toJsonEncodeable(): array; } diff --git a/src/Events/PresenceEvent.php b/src/Events/PresenceEvent.php index e3a54b3..cd230c5 100644 --- a/src/Events/PresenceEvent.php +++ b/src/Events/PresenceEvent.php @@ -19,7 +19,7 @@ class PresenceEvent extends Event parent::__construct(EventType::PRESENCE); } - public function fromJson(string $json): static + public function fromJson(string $json): self { } diff --git a/src/Events/RoomMemberEvent.php b/src/Events/RoomMemberEvent.php index 02793ea..79ba0cc 100644 --- a/src/Events/RoomMemberEvent.php +++ b/src/Events/RoomMemberEvent.php @@ -3,19 +3,20 @@ namespace App\Events; use App\Types\EventType; +use App\Types\MembershipState; class RoomMemberEvent extends Event { public function __construct( private string $sender, private string $stateKey, - private string $membership, + private MembershipState $membership, ) { parent::__construct(EventType::ROOM_MEMBER); } - public function fromJson(string $json): static + public function fromJson(string $json): self { } @@ -25,6 +26,8 @@ class RoomMemberEvent extends Event "type" => $this->type, "sender" => $this->sender, "state_key" => $this->stateKey, + "event_id" => "\$0", + "origin_server_ts" => time(), "content" => [ "membership" => $this->membership, ], diff --git a/src/Events/RoomMessageEvent.php b/src/Events/RoomMessageEvent.php new file mode 100644 index 0000000..3ccc408 --- /dev/null +++ b/src/Events/RoomMessageEvent.php @@ -0,0 +1,86 @@ +<?php + +namespace App\Events; + +use App\Database; +use App\Support\ConnectsToDatabase; +use App\Types\EventType; +use App\Types\MembershipState; +use App\Types\MessageType; + +class RoomMessageEvent extends Event implements ConnectsToDatabase +{ + public function __construct( + private string $id = "", + private string $sender = "", + private \DateTime $originServerTimestamp = new \DateTime("now"), + private array $content = [], + private array $unsigned = [], + private string $roomId = "", + ) + { + parent::__construct(EventType::ROOM_MESSAGE); + } + + public function fromJson(string $json): self + { + } + + public function toJsonEncodeable(): array + { + return [ + "type" => $this->type, + "sender" => $this->sender, + "event_id" => $this->id, + "origin_server_ts" => $this->originServerTimestamp->format("U.v"), + "content" => [ + "body" => $this->body, + "msgtype" => MessageType::TEXT, + ], + "unsigned" => [ + "age" => 1234, + "membership" => MembershipState::JOIN, + ], + "room_id" => $this->roomId, + ]; + } + + public static function fromDatabase(array $row): self + { + } + + public static function fetch(): ?self + { + } + + public static function fetchAll(): array + { + } + + public function insert(): bool + { + return !! Database::getInstance()->query( + <<<SQL + insert into room_events (id, type, sender, origin_server_timestamp, content, unsigned, room_id) + values (:id, :type, :sender, to_timestamp(:origin_server_timestamp), :content, :unsigned, :room_id) + SQL, + [ + "id" => $this->id, + "type" => $this->type->value, + "sender" => $this->sender, + "origin_server_timestamp" => (new \DateTime("now"))->format("U.v"), + "content" => json_encode($this->content), + "unsigned" => json_encode($this->unsigned), + "room_id" => $this->roomId, + ] + ); + } + + public function update(): bool + { + } + + public function delete(): bool + { + } +} diff --git a/src/Events/RoomNameEvent.php b/src/Events/RoomNameEvent.php index 59abbe2..279e6c1 100644 --- a/src/Events/RoomNameEvent.php +++ b/src/Events/RoomNameEvent.php @@ -15,7 +15,7 @@ class RoomNameEvent extends Event parent::__construct(EventType::ROOM_NAME); } - public function fromJson(string $json): static + public function fromJson(string $json): self { } |