diff options
Diffstat (limited to 'matrix-specification/Events')
22 files changed, 692 insertions, 66 deletions
diff --git a/matrix-specification/Events/ClientEvent.php b/matrix-specification/Events/ClientEvent.php index be1e354..dda4ead 100644 --- a/matrix-specification/Events/ClientEvent.php +++ b/matrix-specification/Events/ClientEvent.php @@ -2,31 +2,54 @@ namespace Matrix\Events; +use Matrix\Data\UnsignedData; use Matrix\Enums\EventType; -class ClientEvent extends ClientEventWithoutRoomId +class ClientEvent extends SenderEvent { public function __construct( array $content, - string $eventId, - int $originServerTimestamp, + protected string $eventId, + protected int $originServerTimestamp, protected string $roomId, string $sender, - string $stateKey, EventType $type, - ?UnsignedData $unsigned = null, + protected ?UnsignedData $unsigned = null, ) { - parent::__construct($content, $eventId, $originServerTimestamp, $sender, $stateKey, $type); + parent::__construct($content, $sender, $type); } public function jsonSerialize(): array { - $clientEvent = parent::jsonSerialize(); - $clientEvent += [ + return [ + "content" => $this->content ?: new \stdClass, + "event_id" => $this->eventId, + "origin_server_ts" => $this->originServerTimestamp, "room_id" => $this->roomId, + "sender" => $this->sender, + "type" => $this->type, + "unsigned" => $this->unsigned ?? new \stdClass, ]; + } + + public function getId(): string + { + return $this->eventId; + } - return $clientEvent; + public function getOriginServerTimestamp(): int + { + return $this->originServerTimestamp; + } + + public function getRoomId(): string + { + return $this->roomId; + } + + public function getUnsigned(): ?UnsignedData + { + return $this->unsigned; } } diff --git a/matrix-specification/Events/ClientEventWithoutRoomId.php b/matrix-specification/Events/ClientEventWithoutRoomId.php index 0410d5b..f10d125 100644 --- a/matrix-specification/Events/ClientEventWithoutRoomId.php +++ b/matrix-specification/Events/ClientEventWithoutRoomId.php @@ -4,31 +4,33 @@ namespace Matrix\Events; use Matrix\Enums\EventType; -class ClientEventWithoutRoomId extends SenderEvent +class ClientEventWithoutRoomId extends ClientEvent { public function __construct( array $content, - protected string $eventId, - protected int $originServerTimestamp, + string $eventId, + int $originServerTimestamp, string $sender, - protected string $stateKey, EventType $type, - protected ?UnsignedData $unsigned = null, + ?UnsignedData $unsigned = null, ) { - parent::__construct($content, $sender, $type); + parent::__construct( + $content, + $eventId, + $originServerTimestamp, + "", + $sender, + $type, + $unsigned, + ); } public function jsonSerialize(): array { - return [ - "content" => $this->content ?: new \stdClass, - "event_id" => $this->eventId, - "origin_server_ts" => $this->originServerTimestamp, - "sender" => $this->sender, - "state_key" => $this->stateKey, - "type" => $this->type, - "unsigned" => $this->unsigned ?? new \stdClass, - ]; + $clientEvent = parent::jsonSerialize(); + unset($clientEvent["room_id"]); + + return $clientEvent; } } diff --git a/matrix-specification/Events/Event.php b/matrix-specification/Events/Event.php index fe85f48..5b3e450 100644 --- a/matrix-specification/Events/Event.php +++ b/matrix-specification/Events/Event.php @@ -14,4 +14,17 @@ abstract class Event implements \JsonSerializable protected EventType $type, ) {} + + /** + * @return array<string, mixed> + */ + public function getContent(): array + { + return $this->content; + } + + public function getType(): EventType + { + return $this->type; + } } diff --git a/matrix-specification/Events/Message/FormattedMessageEvent.php b/matrix-specification/Events/Message/FormattedMessageEvent.php new file mode 100644 index 0000000..92e73a1 --- /dev/null +++ b/matrix-specification/Events/Message/FormattedMessageEvent.php @@ -0,0 +1,39 @@ +<?php + +namespace Matrix\Events\Message; + +use Matrix\Data\UnsignedData; +use Matrix\Enums\EventType; +use Matrix\Enums\MessageFormat; +use Matrix\Enums\MessageType; + +class FormattedMessageEvent extends MessageEvent +{ + public function __construct( + string $eventId, + int $originServerTimestamp, + string $roomId, + string $sender, + string $body, + MessageType $msgtype, + ?MessageFormat $format = null, + ?string $formattedBody = null, + ?UnsignedData $unsigned = null, + ) + { + parent::__construct( + array_filter([ + "body" => $body, + "format" => $format, + "formatted_body" => $formattedBody, + "msgtype" => $msgtype, + ], fn ($value) => ! is_null($value)), + $eventId, + $originServerTimestamp, + $roomId, + $sender, + EventType::ROOM_MESSAGE, + $unsigned, + ); + } +} diff --git a/matrix-specification/Events/Message/MessageEvent.php b/matrix-specification/Events/Message/MessageEvent.php new file mode 100644 index 0000000..c06475b --- /dev/null +++ b/matrix-specification/Events/Message/MessageEvent.php @@ -0,0 +1,35 @@ +<?php + +namespace Matrix\Events\Message; + +use Matrix\Data\UnsignedData; +use Matrix\Enums\EventType; +use Matrix\Enums\MessageType; +use Matrix\Events\ClientEvent; + +class MessageEvent extends ClientEvent +{ + public function __construct( + string $eventId, + int $originServerTimestamp, + string $roomId, + string $sender, + string $body, + MessageType $msgtype, + ?UnsignedData $unsigned = null, + ) + { + parent::__construct( + array_filter([ + "body" => $body, + "msgtype" => $msgtype, + ], fn ($value) => ! is_null($value)), + $eventId, + $originServerTimestamp, + $roomId, + $sender, + EventType::ROOM_MESSAGE, + $unsigned, + ); + } +} diff --git a/matrix-specification/Events/Message/TextMessageEvent.php b/matrix-specification/Events/Message/TextMessageEvent.php new file mode 100644 index 0000000..f51317e --- /dev/null +++ b/matrix-specification/Events/Message/TextMessageEvent.php @@ -0,0 +1,38 @@ +<?php + +namespace Matrix\Events\Message; + +use Matrix\Data\UnsignedData; +use Matrix\Enums\EventType; +use Matrix\Enums\MessageFormat; +use Matrix\Enums\MessageType; + +class TextMessageEvent extends FormattedMessageEvent +{ + public function __construct( + string $eventId, + int $originServerTimestamp, + string $roomId, + string $sender, + string $body, + ?MessageFormat $format = null, + ?string $formattedBody = null, + ?UnsignedData $unsigned = null, + ) + { + parent::__construct( + array_filter([ + "body" => $body, + "format" => $format, + "formatted_body" => $formattedBody, + "msgtype" => MessageType::TEXT, + ], fn ($value) => ! is_null($value)), + $eventId, + $originServerTimestamp, + $roomId, + $sender, + EventType::ROOM_MESSAGE, + $unsigned, + ); + } +} diff --git a/matrix-specification/Events/PresenceEvent.php b/matrix-specification/Events/PresenceEvent.php index 7854444..da40af4 100644 --- a/matrix-specification/Events/PresenceEvent.php +++ b/matrix-specification/Events/PresenceEvent.php @@ -30,13 +30,4 @@ class PresenceEvent extends SenderEvent EventType::PRESENCE ); } - - public function jsonSerialize(): array - { - return [ - "content" => $this->content, - "sender" => $this->sender, - "type" => $this->type, - ]; - } } diff --git a/matrix-specification/Events/Room/AvatarEvent.php b/matrix-specification/Events/Room/AvatarEvent.php new file mode 100644 index 0000000..c9e18f2 --- /dev/null +++ b/matrix-specification/Events/Room/AvatarEvent.php @@ -0,0 +1,37 @@ +<?php + +namespace Matrix\Events\Room; + +use Matrix\Data\Room\AvatarInfo; +use Matrix\Data\UnsignedData; +use Matrix\Enums\EventType; +use Matrix\Events\StateEvent; + +class AvatarEvent extends StateEvent +{ + public function __construct( + string $eventId, + int $originServerTimestamp, + string $roomId, + string $sender, + ?AvatarInfo $info = null, + ?string $url = null, + string $stateKey = "", + ?UnsignedData $unsigned = null, + ) + { + parent::__construct( + array_filter([ + "info" => $info, + "url" => $url, + ], fn ($value) => ! is_null($value)), + $eventId, + $originServerTimestamp, + $roomId, + $sender, + $stateKey, + EventType::ROOM_AVATAR, + $unsigned, + ); + } +} diff --git a/matrix-specification/Events/Room/CanonicalAliasEvent.php b/matrix-specification/Events/Room/CanonicalAliasEvent.php new file mode 100644 index 0000000..63de661 --- /dev/null +++ b/matrix-specification/Events/Room/CanonicalAliasEvent.php @@ -0,0 +1,39 @@ +<?php + +namespace Matrix\Events\Room; + +use Matrix\Data\UnsignedData; +use Matrix\Enums\EventType; +use Matrix\Events\StateEvent; + +class CanonicalAliasEvent extends StateEvent +{ + /** + * @param ?string[] $altAliases + */ + public function __construct( + string $eventId, + int $originServerTimestamp, + string $roomId, + string $sender, + ?string $alias = null, + ?array $altAliases = null, + string $stateKey = "", + ?UnsignedData $unsigned = null, + ) + { + parent::__construct( + array_filter([ + "alias" => $alias, + "alt_aliases" => $altAliases, + ], fn ($value) => ! is_null($value)), + $eventId, + $originServerTimestamp, + $roomId, + $sender, + $stateKey, + EventType::ROOM_CANONICAL_ALIAS, + $unsigned, + ); + } +} diff --git a/matrix-specification/Events/Room/CreateEvent.php b/matrix-specification/Events/Room/CreateEvent.php new file mode 100644 index 0000000..eccda10 --- /dev/null +++ b/matrix-specification/Events/Room/CreateEvent.php @@ -0,0 +1,54 @@ +<?php + +namespace Matrix\Events\Room; + +use Matrix\Data\Room\PreviousRoom; +use Matrix\Data\UnsignedData; +use Matrix\Enums\EventType; +use Matrix\Events\StateEvent; + +class CreateEvent extends StateEvent +{ + /** + * @param ?string[] $additionalCreators + */ + public function __construct( + string $eventId, + int $originServerTimestamp, + string $roomId, + string $sender, + string $roomVersion, + ?array $additionalCreators = null, + ?string $creator = null, + bool $federate = true, + ?PreviousRoom $predecessor = null, + ?string $roomType = null, + string $stateKey = "", + ?UnsignedData $unsigned = null, + ) + { + if (intval($roomVersion) <= 10) { + if (empty($creator)) { + throw new \InvalidArgumentException("creator is required in room versions 1 - 10"); + } + } + + parent::__construct( + array_filter([ + "additional_creators" => $additionalCreators, + "creator" => intval($roomVersion) <= 10 ? $creator : $sender, + "m.federate" => $federate, + "predecessor" => $predecessor, + "room_version" => $roomVersion, + "type" => $roomType, + ], fn ($value) => ! is_null($value)), + $eventId, + $originServerTimestamp, + $roomId, + $sender, + $stateKey, + EventType::ROOM_CREATE, + $unsigned, + ); + } +} diff --git a/matrix-specification/Events/Room/GuestAccessEvent.php b/matrix-specification/Events/Room/GuestAccessEvent.php new file mode 100644 index 0000000..a0eeddb --- /dev/null +++ b/matrix-specification/Events/Room/GuestAccessEvent.php @@ -0,0 +1,35 @@ +<?php + +namespace Matrix\Events\Room; + +use Matrix\Data\UnsignedData; +use Matrix\Enums\EventType; +use Matrix\Enums\RoomGuestAccess; +use Matrix\Events\StateEvent; + +class GuestAccessEvent extends StateEvent +{ + public function __construct( + string $eventId, + int $originServerTimestamp, + string $roomId, + string $sender, + RoomGuestAccess $guestAccess, + string $stateKey = "", + ?UnsignedData $unsigned = null, + ) + { + parent::__construct( + [ + "guest_access" => $guestAccess, + ], + $eventId, + $originServerTimestamp, + $roomId, + $sender, + $stateKey, + EventType::ROOM_GUEST_ACCESS, + $unsigned, + ); + } +} diff --git a/matrix-specification/Events/Room/HistoryVisibilityEvent.php b/matrix-specification/Events/Room/HistoryVisibilityEvent.php new file mode 100644 index 0000000..76672bf --- /dev/null +++ b/matrix-specification/Events/Room/HistoryVisibilityEvent.php @@ -0,0 +1,35 @@ +<?php + +namespace Matrix\Events\Room; + +use Matrix\Data\UnsignedData; +use Matrix\Enums\EventType; +use Matrix\Enums\RoomHistoryVisibility; +use Matrix\Events\StateEvent; + +class HistoryVisibilityEvent extends StateEvent +{ + public function __construct( + string $eventId, + int $originServerTimestamp, + string $roomId, + string $sender, + RoomHistoryVisibility $historyVisibility, + string $stateKey = "", + ?UnsignedData $unsigned = null, + ) + { + parent::__construct( + [ + "history_visibility" => $historyVisibility, + ], + $eventId, + $originServerTimestamp, + $roomId, + $sender, + $stateKey, + EventType::ROOM_HISTORY_VISIBILITY, + $unsigned, + ); + } +} diff --git a/matrix-specification/Events/Room/JoinRulesEvent.php b/matrix-specification/Events/Room/JoinRulesEvent.php new file mode 100644 index 0000000..337c9d2 --- /dev/null +++ b/matrix-specification/Events/Room/JoinRulesEvent.php @@ -0,0 +1,40 @@ +<?php + +namespace Matrix\Events\Room; + +use Matrix\Data\UnsignedData; +use Matrix\Enums\EventType; +use Matrix\Enums\RoomJoinRule; +use Matrix\Events\StateEvent; + +class JoinRulesEvent extends StateEvent +{ + /** + * @param ?AllowCondition[] $allow + */ + public function __construct( + string $eventId, + int $originServerTimestamp, + string $roomId, + string $sender, + RoomJoinRule $joinRule, + ?array $allow = null, + string $stateKey = "", + ?UnsignedData $unsigned = null, + ) + { + parent::__construct( + array_filter([ + "allow" => $allow, + "join_rule" => $joinRule, + ], fn ($value) => ! is_null($value)), + $eventId, + $originServerTimestamp, + $roomId, + $sender, + $stateKey, + EventType::ROOM_JOIN_RULES, + $unsigned, + ); + } +} diff --git a/matrix-specification/Events/Room/MemberEvent.php b/matrix-specification/Events/Room/MemberEvent.php new file mode 100644 index 0000000..0f27289 --- /dev/null +++ b/matrix-specification/Events/Room/MemberEvent.php @@ -0,0 +1,47 @@ +<?php + +namespace Matrix\Events\Room; + +use Matrix\Data\UnsignedData; +use Matrix\Enums\EventType; +use Matrix\Enums\MembershipState; +use Matrix\Events\StateEvent; + +class MemberEvent extends StateEvent +{ + public function __construct( + string $eventId, + int $originServerTimestamp, + string $roomId, + string $sender, + string $stateKey, + bool $isDirect, + MembershipState $membership, + ?string $avatarUrl = null, + ?string $displayName = null, + ?string $joinAuthorisedViaUsersServer = null, + ?string $reason = null, + ?object $thirdPartyInvite = null, # TODO: https://spec.matrix.org/v1.17/client-server-api/#mroommember_thirdpartyinvite + ?UnsignedData $unsigned = null, + ) + { + parent::__construct( + array_filter([ + "avatar_url" => $avatarUrl, + "displayname" => $displayName, + "is_direct" => $isDirect, + "join_authorised_via_users_server" => $joinAuthorisedViaUsersServer, + "membership" => $membership, + "reason" => $reason, + "third_party_invite" => $thirdPartyInvite, + ], fn ($value) => ! is_null($value)), + $eventId, + $originServerTimestamp, + $roomId, + $sender, + $stateKey, + EventType::ROOM_MEMBER, + $unsigned, + ); + } +} diff --git a/matrix-specification/Events/Room/NameEvent.php b/matrix-specification/Events/Room/NameEvent.php new file mode 100644 index 0000000..187b36a --- /dev/null +++ b/matrix-specification/Events/Room/NameEvent.php @@ -0,0 +1,34 @@ +<?php + +namespace Matrix\Events\Room; + +use Matrix\Data\UnsignedData; +use Matrix\Enums\EventType; +use Matrix\Events\StateEvent; + +class NameEvent extends StateEvent +{ + public function __construct( + string $eventId, + int $originServerTimestamp, + string $roomId, + string $sender, + string $name, + string $stateKey = "", + ?UnsignedData $unsigned = null, + ) + { + parent::__construct( + [ + "name" => $name, + ], + $eventId, + $originServerTimestamp, + $roomId, + $sender, + $stateKey, + EventType::ROOM_NAME, + $unsigned, + ); + } +} diff --git a/matrix-specification/Events/Room/PinnedEventsEvent.php b/matrix-specification/Events/Room/PinnedEventsEvent.php new file mode 100644 index 0000000..c0a98b0 --- /dev/null +++ b/matrix-specification/Events/Room/PinnedEventsEvent.php @@ -0,0 +1,37 @@ +<?php + +namespace Matrix\Events\Room; + +use Matrix\Data\UnsignedData; +use Matrix\Enums\EventType; +use Matrix\Events\StateEvent; + +class PinnedEventsEvent extends StateEvent +{ + /** + * @param string[] $pinned + */ + public function __construct( + string $eventId, + int $originServerTimestamp, + string $roomId, + string $sender, + array $pinned, + string $stateKey = "", + ?UnsignedData $unsigned = null, + ) + { + parent::__construct( + [ + "pinned" => $pinned, + ], + $eventId, + $originServerTimestamp, + $roomId, + $sender, + $stateKey, + EventType::ROOM_CANONICAL_ALIAS, + $unsigned, + ); + } +} diff --git a/matrix-specification/Events/Room/PowerLevelsEvent.php b/matrix-specification/Events/Room/PowerLevelsEvent.php new file mode 100644 index 0000000..8b609f3 --- /dev/null +++ b/matrix-specification/Events/Room/PowerLevelsEvent.php @@ -0,0 +1,57 @@ +<?php + +namespace Matrix\Events\Room; + +use Matrix\Data\UnsignedData; +use Matrix\Enums\EventType; +use Matrix\Events\StateEvent; + +class PowerLevelsEvent extends StateEvent +{ + /** + * @param ?array<string, int> $events This is a mapping from event type to power level required. + * @param ?array<string, int> $notifications This is a mapping from key to power level for that notifications key. + * @param ?array<string, int> $events This is a mapping from user_id to power level for that user. + */ + public function __construct( + string $eventId, + int $originServerTimestamp, + string $roomId, + string $sender, + ?int $ban = 50, + ?array $events = null, + ?int $eventsDefault = 0, + ?int $invite = 0, + ?int $kick = 50, + ?array $notifications = null, # TODO: https://spec.matrix.org/v1.17/client-server-api/#mroompower_levels_notifications + ?int $redact = 50, + ?int $stateDefault = 50, + ?array $users = null, + ?int $usersDefault = 0, + string $stateKey = "", + ?UnsignedData $unsigned = null, + ) + { + parent::__construct( + array_filter([ + "ban" => $ban, + "events" => $events, + "events_default" => $eventsDefault, + "invite" => $invite, + "kick" => $kick, + "notifications" => $notifications, + "redact" => $redact, + "state_default" => $stateDefault, + "users" => $users, + "users_default" => $usersDefault, + ], fn ($value) => ! is_null($value)), + $eventId, + $originServerTimestamp, + $roomId, + $sender, + $stateKey, + EventType::ROOM_POWER_LEVELS, + $unsigned, + ); + } +} diff --git a/matrix-specification/Events/Room/TopicEvent.php b/matrix-specification/Events/Room/TopicEvent.php new file mode 100644 index 0000000..8e01a3b --- /dev/null +++ b/matrix-specification/Events/Room/TopicEvent.php @@ -0,0 +1,37 @@ +<?php + +namespace Matrix\Events\Room; + +use Matrix\Data\Room\TopicContentBlock; +use Matrix\Data\UnsignedData; +use Matrix\Enums\EventType; +use Matrix\Events\StateEvent; + +class TopicEvent extends StateEvent +{ + public function __construct( + string $eventId, + int $originServerTimestamp, + string $roomId, + string $sender, + string $topic, + ?TopicContentBlock $topicContentBlock = null, + string $stateKey = "", + ?UnsignedData $unsigned = null, + ) + { + parent::__construct( + array_filter([ + "m.topic" => $topicContentBlock, + "topic" => $topic, + ], fn ($value) => ! is_null($value)), + $eventId, + $originServerTimestamp, + $roomId, + $sender, + $stateKey, + EventType::ROOM_TOPIC, + $unsigned, + ); + } +} diff --git a/matrix-specification/Events/SenderEvent.php b/matrix-specification/Events/SenderEvent.php index f3d4ceb..86ba65e 100644 --- a/matrix-specification/Events/SenderEvent.php +++ b/matrix-specification/Events/SenderEvent.php @@ -17,4 +17,18 @@ abstract class SenderEvent extends Event { parent::__construct($content, $type); } + + public function jsonSerialize(): array + { + return [ + "content" => $this->content, + "sender" => $this->sender, + "type" => $this->type, + ]; + } + + public function getSender(): string + { + return $this->sender; + } } diff --git a/matrix-specification/Events/StateEvent.php b/matrix-specification/Events/StateEvent.php new file mode 100644 index 0000000..7fe7c83 --- /dev/null +++ b/matrix-specification/Events/StateEvent.php @@ -0,0 +1,50 @@ +<?php + +namespace Matrix\Events; + +use Matrix\Data\UnsignedData; +use Matrix\Enums\EventType; + +class StateEvent extends ClientEvent +{ + public function __construct( + array $content, + string $eventId, + int $originServerTimestamp, + string $roomId, + string $sender, + protected string $stateKey, + EventType $type, + ?UnsignedData $unsigned = null, + ) + { + parent::__construct( + $content, + $eventId, + $originServerTimestamp, + $roomId, + $sender, + $type, + $unsigned, + ); + } + + public function jsonSerialize(): array + { + return [ + "content" => $this->content ?: new \stdClass, + "event_id" => $this->eventId, + "origin_server_ts" => $this->originServerTimestamp, + "room_id" => $this->roomId, + "sender" => $this->sender, + "state_key" => $this->stateKey, + "type" => $this->type, + "unsigned" => $this->unsigned ?? new \stdClass, + ]; + } + + public function getStateKey(): string + { + return $this->stateKey; + } +} diff --git a/matrix-specification/Events/StrippedStateEvent.php b/matrix-specification/Events/StrippedStateEvent.php index 6e8aea6..3eea582 100644 --- a/matrix-specification/Events/StrippedStateEvent.php +++ b/matrix-specification/Events/StrippedStateEvent.php @@ -12,11 +12,11 @@ class StrippedStateEvent extends SenderEvent public function __construct( array $content, string $sender, - private string $stateKey, + protected string $stateKey, EventType $type, ) { - parent::__construct($content, $sender, $type); + parent::__construct($content, $type); } public function jsonSerialize(): array diff --git a/matrix-specification/Events/UnsignedData.php b/matrix-specification/Events/UnsignedData.php deleted file mode 100644 index 3c5cd46..0000000 --- a/matrix-specification/Events/UnsignedData.php +++ /dev/null @@ -1,31 +0,0 @@ -<?php - -namespace Matrix\Events; - -use Matrix\Enums\MembershipState; - -/** - * @see https://spec.matrix.org/v1.16/client-server-api/#definition-clientevent_unsigneddata - */ -class UnsignedData implements \JsonSerializable -{ - public function __construct( - private ?int $age = null, - private ?MembershipState $membership = null, - private ?array $previousContent = null, - private ?ClientEvent $redactedBecause = null, - private ?string $transactionId = null, - ) - {} - - public function jsonSerialize(): array - { - return array_filter([ - "age" => $this->age, - "membership" => $this->membership, - "prev_content" => $this->previousContent, - "redacted_because" => $this->redactedBecause, - "transaction_id" => $this->transactionId, - ], fn ($value) => ! is_null($value)); - } -} |
