summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/Controllers/KeyController.php10
-rwxr-xr-xsrc/Controllers/RoomController.php21
-rwxr-xr-xsrc/Controllers/SyncController.php12
-rwxr-xr-xsrc/Controllers/UserController.php32
-rw-r--r--src/Events/Event.php6
-rw-r--r--src/Events/PresenceEvent.php2
-rw-r--r--src/Events/RoomMemberEvent.php7
-rw-r--r--src/Events/RoomMessageEvent.php86
-rw-r--r--src/Events/RoomNameEvent.php2
-rw-r--r--src/Router/routes_client_server.php21
-rw-r--r--src/Types/MembershipState.php12
11 files changed, 202 insertions, 9 deletions
diff --git a/src/Controllers/KeyController.php b/src/Controllers/KeyController.php
index b2a17a6..a8b4fb1 100644
--- a/src/Controllers/KeyController.php
+++ b/src/Controllers/KeyController.php
@@ -4,7 +4,9 @@ namespace App\Controllers;
use App\Errors\AppException;
use App\Errors\ErrorCode;
+use App\Errors\UnauthorizedError;
use App\Models\Tokens;
+use App\Models\User;
use App\Support\RequestValidator;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
@@ -27,11 +29,19 @@ class KeyController
*/
public function upload(Request $request): Response
{
+ $accessToken = str_replace("Bearer ", "", $request->headers->get("authorization") ?: "");
+ $user = User::fetchWithAccessToken($accessToken);
+
+ if (empty($user)) {
+ throw new UnauthorizedError();
+ }
+
$body = json_decode($request->getContent(), true);
RequestValidator::validateJson();
return new JsonResponse([
"one_time_key_counts" => [
+ "curve25519" => 0,
"signed_curve25519" => count($body["one_time_keys"])
],
]);
diff --git a/src/Controllers/RoomController.php b/src/Controllers/RoomController.php
index 367d754..c02d5ce 100755
--- a/src/Controllers/RoomController.php
+++ b/src/Controllers/RoomController.php
@@ -6,10 +6,12 @@ use App\Database;
use App\Errors\AppException;
use App\Errors\ErrorCode;
use App\Errors\UnauthorizedError;
+use App\Events\RoomMessageEvent;
use App\Models\User;
use App\Support\Parser;
use App\Support\RequestValidator;
use App\Types\EventType;
+use App\Types\MembershipState;
use App\Types\MessageType;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
@@ -81,8 +83,25 @@ class RoomController
$message = $body["body"];
$messageType = MessageType::from($body["msgtype"]);
+ $eventId = "\$" . md5(random_bytes(512)) . ":" . $_ENV["DOMAIN"];
+ $event = new RoomMessageEvent(
+ id: $eventId,
+ sender: $user->getId(),
+ originServerTimestamp: new \DateTime("now"),
+ content: [
+ "body" => $message,
+ "msgtype" => $messageType->value,
+ ],
+ unsigned: [
+ "age" => 1234,
+ "membership" => MembershipState::JOIN->value,
+ ],
+ roomId: $roomId,
+ );
+ $event->insert();
+
return new JsonResponse([
- "event_id" => "\$asdfghjkl:" . $_ENV["DOMAIN"],
+ "event_id" => $eventId,
]);
}
}
diff --git a/src/Controllers/SyncController.php b/src/Controllers/SyncController.php
index b7d51c3..243fab5 100755
--- a/src/Controllers/SyncController.php
+++ b/src/Controllers/SyncController.php
@@ -5,7 +5,10 @@ namespace App\Controllers;
use App\Database;
use App\Errors\UnauthorizedError;
use App\Events\PresenceEvent;
+use App\Events\RoomMemberEvent;
+use App\Events\RoomMessageEvent;
use App\Models\User;
+use App\Types\MembershipState;
use App\Types\PresenceState;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
@@ -62,14 +65,17 @@ class SyncController
],
"timeline" => [
- "events" => [],
+ "events" => [
+ (new RoomMemberEvent($user->getId(), $user->getId(), MembershipState::JOIN))->toJsonEncodeable(),
+ (new RoomMessageEvent($user->getId(), "Hallo Test Nachricht"))->toJsonEncodeable(),
+ ],
"limited" => false,
"prev_batch" => "",
],
"unread_notifications" => [
- "highlight_count" => 1,
- "notification_count" => 2,
+ "highlight_count" => random_int(0, 1),
+ "notification_count" => random_int(0, 10),
],
"unread_thread_notifications" => new \stdClass(),
diff --git a/src/Controllers/UserController.php b/src/Controllers/UserController.php
new file mode 100755
index 0000000..a056e1d
--- /dev/null
+++ b/src/Controllers/UserController.php
@@ -0,0 +1,32 @@
+<?php
+
+namespace App\Controllers;
+
+use App\Errors\UnauthorizedError;
+use App\Models\User;
+use Symfony\Component\HttpFoundation\Request;
+use Symfony\Component\HttpFoundation\Response;
+use Symfony\Component\HttpFoundation\JsonResponse;
+
+class UserController
+{
+ /**
+ * POST /_matrix/client/r0/user/{userId}/filter
+ * POST /_matrix/client/v3/user/{userId}/filter
+ *
+ * @see https://spec.matrix.org/v1.16/client-server-api/#post_matrixclientv3useruseridfilter
+ */
+ public function uploadFilter(Request $request): Response
+ {
+ $accessToken = str_replace("Bearer ", "", $request->headers->get("authorization") ?: "");
+ $user = User::fetchWithAccessToken($accessToken);
+
+ if (empty($user)) {
+ throw new UnauthorizedError();
+ }
+
+ return new JsonResponse([
+ "filter_id" => "1234",
+ ]);
+ }
+}
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
{
}
diff --git a/src/Router/routes_client_server.php b/src/Router/routes_client_server.php
index 7543892..fd39d08 100644
--- a/src/Router/routes_client_server.php
+++ b/src/Router/routes_client_server.php
@@ -9,6 +9,7 @@ use App\Controllers\RoomController;
use App\Controllers\ServerDiscoveryController;
use App\Controllers\ServerImplementationController;
use App\Controllers\SyncController;
+use App\Controllers\UserController;
use Symfony\Component\Routing\Loader\Configurator\RouteConfigurator;
return function (RouteConfigurator $routes): void
@@ -35,6 +36,11 @@ return function (RouteConfigurator $routes): void
->methods(["GET"]);
$routes
+ ->add("matrix_client_r0_login", "/_matrix/client/r0/login")
+ ->controller([LoginController::class, "login"])
+ ->methods(["POST"]);
+
+ $routes
->add("matrix_client_v3_login", "/_matrix/client/v3/login")
->controller([LoginController::class, "login"])
->methods(["POST"]);
@@ -45,11 +51,21 @@ return function (RouteConfigurator $routes): void
->methods(["POST"]);
$routes
+ ->add("matrix_client_r0_keys_upload", "/_matrix/client/r0/keys/upload")
+ ->controller([KeyController::class, "upload"])
+ ->methods(["POST"]);
+
+ $routes
->add("matrix_client_v3_sync", "/_matrix/client/v3/sync")
->controller([SyncController::class, "sync"])
->methods(["GET"]);
$routes
+ ->add("matrix_client_r0_sync", "/_matrix/client/r0/sync")
+ ->controller([SyncController::class, "sync"])
+ ->methods(["GET"]);
+
+ $routes
->add("matrix_client_v3_refresh", "/_matrix/client/v3/refresh")
->controller([KeyController::class, "refresh"])
->methods(["POST"]);
@@ -68,4 +84,9 @@ return function (RouteConfigurator $routes): void
->add("matrix_client_v3_rooms_id_send_event_transaction", "/_matrix/client/v3/rooms/{roomId}/send/{eventType}/{txnId}")
->controller([RoomController::class, "send"])
->methods(["PUT"]);
+
+ $routes
+ ->add("matrix_client_r0_user_id_filter", "/_matrix/client/r0/user/{userId}/filter")
+ ->controller([UserController::class, "uploadFilter"])
+ ->methods(["POST"]);
};
diff --git a/src/Types/MembershipState.php b/src/Types/MembershipState.php
new file mode 100644
index 0000000..a4b2a1d
--- /dev/null
+++ b/src/Types/MembershipState.php
@@ -0,0 +1,12 @@
+<?php
+
+namespace App\Types;
+
+enum MembershipState: string
+{
+ case INVITE = "invite";
+ case JOIN = "join";
+ case LEAVE = "leave";
+ case BAN = "ban";
+ case KNOCK = "knock";
+}