diff options
Diffstat (limited to 'src/Models')
| -rw-r--r-- | src/Models/RoomEvent.php | 12 | ||||
| -rw-r--r-- | src/Models/User.php | 34 |
2 files changed, 41 insertions, 5 deletions
diff --git a/src/Models/RoomEvent.php b/src/Models/RoomEvent.php index 96dc204..11d74b0 100644 --- a/src/Models/RoomEvent.php +++ b/src/Models/RoomEvent.php @@ -3,6 +3,7 @@ namespace App\Models; use App\Database; +use Matrix\Data\UnsignedData; use Matrix\Enums\EventType; use Matrix\Events\ClientEvent; use Matrix\Events\StateEvent; @@ -19,6 +20,15 @@ class RoomEvent */ public static function transformEvent(array $row): ClientEvent { + $rowUnsigned = json_decode($row["unsigned"], true); + $unsigned = new UnsignedData( + age: $row["age"] ?? ((time() - new \DateTime($row["origin_server_timestamp"])->getTimestamp()) * 1000), + membership: $row["membership"] ?? null, + previousContent: $row["previous_content"] ?? null, + redactedBecause: $row["redacted_because"] ?? null, + transactionId: $row["transaction_id"] ?? null, + ); + return new ClientEvent( content: json_decode($row["content"], true), eventId: $row["id"], @@ -26,7 +36,7 @@ class RoomEvent roomId: $row["room_id"], sender: $row["sender"], type: EventType::from($row["type"]), - unsigned: json_decode($row["unsigned"], true), + unsigned: $unsigned, ); } diff --git a/src/Models/User.php b/src/Models/User.php index c92f5b3..a30bee0 100644 --- a/src/Models/User.php +++ b/src/Models/User.php @@ -4,14 +4,16 @@ namespace App\Models; use App\Database; use App\Errors\AppException; -use App\Errors\ErrorCode; use App\Errors\UnauthorizedError; use App\Support\ConnectsToDatabase; +use Matrix\Enums\ErrorCode; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; class User implements ConnectsToDatabase { + private string $deviceId; + public function __construct( private string $id, private string $name, @@ -62,7 +64,7 @@ class User implements ConnectsToDatabase public static function fetchWithAccessToken(string $accessToken): ?self { $row = Database::getInstance()->query(<<<SQL - select users.* from users left join tokens on tokens.user_id = users.id where tokens.access_token=:access_token + select users.*, tokens.device_id from users left join tokens on tokens.user_id = users.id where tokens.access_token=:access_token SQL, [ "access_token" => $accessToken, ])->fetch(); @@ -71,7 +73,10 @@ class User implements ConnectsToDatabase return null; } - return self::fromDatabase($row); + $user = self::fromDatabase($row); + $user->setDeviceId($row["device_id"]); + + return $user; } public static function new(string $id, string $name): self @@ -82,6 +87,11 @@ class User implements ConnectsToDatabase public static function authenticateWithRequest(Request $request): self { $accessToken = str_replace("Bearer ", "", $request->headers->get("authorization") ?: ""); + + if (empty($accessToken)) { + throw new AppException(ErrorCode::UNAUTHORIZED, "Missing access token", Response::HTTP_UNAUTHORIZED); + } + $user = self::fetchWithAccessToken($accessToken); if (empty($user)) { @@ -132,9 +142,25 @@ class User implements ConnectsToDatabase return $this->name; } + public function setDeviceId(string $id): void + { + $this->deviceId = $id; + } + + public function getDeviceId(): string + { + return $this->deviceId; + } + public function fetchDevice(string $id): ?Device { - return Device::fetch($id, $this->id); + $device = Device::fetch($id, $this->id); + + if ($device) { + $this->setDeviceId($device->getId()); + } + + return $device; } /** |
