Skip to content

Commit

Permalink
Merge pull request #7 from safatshahin/MDL-78551
Browse files Browse the repository at this point in the history
MDL-78551: Add route for space relationship api
  • Loading branch information
andrewnicols authored Aug 23, 2023
2 parents 369f430 + 9f996c2 commit fbbdd64
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 0 deletions.
70 changes: 70 additions & 0 deletions application/src/Controller/MatrixController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down Expand Up @@ -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);
}
}
18 changes: 18 additions & 0 deletions application/src/Entity/Room.php
Original file line number Diff line number Diff line change
Expand Up @@ -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"})
*/
Expand All @@ -71,6 +76,7 @@ public function jsonSerialize(): \stdClass
'avatar' => $this->avatar,
'roomalias' => $this->roomalias,
'creator' => $this->creator,
'space' => $this->space,
];
}

Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit fbbdd64

Please sign in to comment.