From 9f996c2c511aeaa3665e289eb4fa139cee22491d Mon Sep 17 00:00:00 2001 From: Safat Date: Wed, 23 Aug 2023 14:04:05 +1000 Subject: [PATCH] Add route for space relationship api --- .../src/Controller/MatrixController.php | 70 +++++++++++++++++++ application/src/Entity/Room.php | 18 +++++ 2 files changed, 88 insertions(+) diff --git a/application/src/Controller/MatrixController.php b/application/src/Controller/MatrixController.php index 0b5f4a2..2bfb66d 100644 --- a/application/src/Controller/MatrixController.php +++ b/application/src/Controller/MatrixController.php @@ -252,6 +252,7 @@ public function createRoom(string $serverID, Request $request):JsonResponse { $room->setTopic($payload->topic ?? null); $room->setServerid($serverID); $room->setCreator($accessCheck['user_id']); + $room->setSpace(null); if (isset($payload->room_alias_name) && !empty($payload->room_alias_name)) { $room_alias = "#{$payload->room_alias_name}:{$host}"; $check_alias = $entityManager->getRepository(Room::class)->findOneBy(['roomalias' => $room_alias]); @@ -467,4 +468,73 @@ public function getJoinedMembers(string $serverID, string $roomID, Request $requ 'joined' => (object) $memberinfo, ], 200); } + + /** + * Verify the room space state api. + * + * @Route("/v3/rooms/{roomID}/state/{eventType}/{stateKey}") + * @param string $serverID + * @param string $eventType + * @param string $stateKey + * @param Request $request + * @return JsonResponse + */ + public function roomSpace(string $serverID, string $roomID, string $eventType, string $stateKey, Request $request):JsonResponse { + // 1. Check call auth. + // 2. Check HTTP method is accepted. + + $accessCheck = $this->authHttpCheck(['PUT'], $request); + if (!$accessCheck['status']) { + return $accessCheck['message']; + } + + $entityManager = $this->getDoctrine()->getManager(); + + // Check room exists. + $room = $entityManager->getRepository(Room::class)->findOneBy([ + 'serverid' => $serverID, + 'roomid' => $roomID, + ]); + if (!$room) { + return $this->getUnknownRoomResponse(); + } + + // Check space exists. + $space = $entityManager->getRepository(Room::class)->findOneBy([ + 'serverid' => $serverID, + 'roomid' => $stateKey, + ]); + if (!$space) { + return $this->getUnknownRoomResponse(); + } + + $payload = json_decode($request->getContent()); + + if ($eventType == 'm.space.child') { + $check = $this->validateRequest((array)$payload, ['via']); + if (!$check['status']) { + return $check['message']; + } + $room->setSpace($stateKey); + + } else { + // Unknown state. + return new JsonResponse((object) [ + 'errcode' => 'M_UNRECOGNIZED', + 'error' => 'Unrecognized request' + ], 404); + } + + $entityManager = $this->getDoctrine()->getManager(); + $entityManager->persist($room); + $entityManager->flush(); + + // Create a mock event ID. This isn't the way Synapse does it (I think), but it's a good enough approximation. + // This ID doesn't change if the seed data is the same. + $eventID = substr(hash('sha256', ($serverID . $roomID . $eventType)), 0, 44); + + return new JsonResponse((object) [ + 'event_id' => $eventID, + ], 200); + } } diff --git a/application/src/Entity/Room.php b/application/src/Entity/Room.php index 12a6311..f00d1f5 100644 --- a/application/src/Entity/Room.php +++ b/application/src/Entity/Room.php @@ -55,6 +55,11 @@ class Room */ private $creator; + /** + * @ORM\Column(type="string", length=255, nullable=true) + */ + private $space; + /** * @ORM\OneToMany(targetEntity=RoomMember::class, mappedBy="room", cascade={"persist", "remove"}) */ @@ -71,6 +76,7 @@ public function jsonSerialize(): \stdClass 'avatar' => $this->avatar, 'roomalias' => $this->roomalias, 'creator' => $this->creator, + 'space' => $this->space, ]; } @@ -168,6 +174,18 @@ public function setCreator(?string $creator): self return $this; } + public function getSpace(): ?string + { + return $this->space; + } + + public function setSpace(?string $space): self + { + $this->space = $space; + + return $this; + } + public function addMember(User $user): self { $roomMember = new RoomMember();