Skip to content

Commit

Permalink
Added more response to get room info API
Browse files Browse the repository at this point in the history
  • Loading branch information
stevandoMoodle committed Feb 15, 2023
1 parent 0105395 commit 4f657d0
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 13 deletions.
25 changes: 19 additions & 6 deletions application/src/Controller/MatrixController.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,27 +168,40 @@ public function createRoom(string $serverID, Request $request) : JsonResponse {
}

$payload = json_decode($request->getContent());
$roomName = $payload->name;
$roomName = $payload->name ?? rand();
$host = $request->getHost();

// Create a mock room ID. This isn't the way Synapse does it (I think), but it's a good enough approximation.
$roomID = '!'. substr(hash('sha256', ($serverID . $roomName . (string)time())), 0, 18) . ':' . $host;
$response['room_id'] = $roomID;

// Store the room in the DB.
$entityManager = $this->getDoctrine()->getManager();
$room = new Rooms();

$room->setRoomid($roomID);
$room->setName($payload->name);
$room->setTopic($payload->topic);
$room->setName($roomName);
$room->setTopic($payload->topic ?? null);
$room->setServerid($serverID);
$room->setCreator($accessCheck['user_id']);
if (isset($payload->room_alias_name) && !empty($payload->room_alias_name)) {
$room_alias = "#{$payload->room_alias_name}:{$host}";
$check_alias = $entityManager->getRepository(Rooms::class)->findOneBy(['roomalias' => $room_alias]);
if (empty($check_alias)) {
$room->setRoomAlias($room_alias);
$response['room_alias'] = $room_alias;
} else {
return new JsonResponse((object) [
'errcode' => 'M_ROOM_IN_USE',
'error' => 'Room alias already taken'
], 400);
}
}

$entityManager->persist($room);
$entityManager->flush();

return new JsonResponse((object) [
'room_id' => $roomID,
], 200);
return new JsonResponse((object) $response, 200);
}

/**
Expand Down
46 changes: 45 additions & 1 deletion application/src/Controller/SynapseController.php
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ public function inviteUser(string $serverID, string $roomID, Request $request) :
/**
* Delete a room.
*
* @Route("/rooms/{roomID}", name="deleteRoom")
* @Route("/rooms/{roomID}", methods={"DELETE"}, name="deleteRoom")
* @param string $serverID
* @param Request $request
* @return JsonResponse
Expand Down Expand Up @@ -337,4 +337,48 @@ public function deleteRoom(string $serverID, string $roomID, Request $request) :
'delete_id' => substr(hash('sha256', (date("Ymdhms"))), 0, 18)
], 200);
}

/**
* Get a room detail.
*
* @Route("/rooms/{roomID}", methods={"GET"}, name="roomInfo")
* @param string $serverID
* @param Request $request
* @return JsonResponse
*/
public function roomInfo(string $serverID, string $roomID, Request $request) : JsonResponse {
// 1. Check call auth.
// 2. Check HTTP method is accepted.
$accessCheck = $this->authHttpCheck(['GET'], $request);
if (!$accessCheck['status']) {
return $accessCheck['message'];
}

// Check if room exists.
$check = $this->roomExists($roomID, true);
if (!$check['status']) {
return $check['message'];
}
$room = $check['room'];

// Get all joined members.
$room_members = $this->getDoctrine()
->getRepository(Roommembers::class)
->findBy(['roomid' => $roomID, 'serverid' => $serverID]);
if (!empty($room_members)) {
$room_members = count((array)$room_members);
} else {
$room_members = 0;
}

return new JsonResponse((object) [
'room_id' => $room->getRoomid(),
'name' => $room->getName(),
'canonical_alias' => $room->getRoomAlias(),
'joined_members' => $room_members,
'creator' => $room->getCreator(),
'avatar' => $room->getAvatar(),
'topic' => $room->getTopic()
], 200);
}
}
34 changes: 34 additions & 0 deletions application/src/Entity/Rooms.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,16 @@ class Rooms
*/
private $avatar;

/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $roomalias;

/**
* @ORM\Column(type="string", length=255)
*/
private $creator;

public function getId(): ?int
{
return $this->id;
Expand Down Expand Up @@ -109,4 +119,28 @@ public function setAvatar(?string $avatar): self

return $this;
}

public function getRoomAlias(): ?string
{
return $this->roomalias;
}

public function setRoomAlias(?string $roomalias): self
{
$this->roomalias = $roomalias;

return $this;
}

public function getCreator(): ?string
{
return $this->creator;
}

public function setCreator(?string $creator): self
{
$this->creator = $creator;

return $this;
}
}
13 changes: 8 additions & 5 deletions application/src/Service/ApiCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,18 @@ public function checkAuth($request): array
], 401);
} else {
$authToken = substr($authHeader, 7);
if (!$this->isValidAuthToken($authToken)){
$check_token = $this->isValidAuthToken($authToken);
if (empty($check_token)){
// Auth token is not valid.
$response['status'] = false;
$response['message'] = new JsonResponse((object) [
'errcode' => 'M_UNKNOWN_TOKEN',
'error' => 'Invalid access token passed.'
], 401);
} else {
$response['user_id'] = $check_token->getUserid()->getUserid();
}

}
return $response;
}
Expand All @@ -59,12 +63,11 @@ public function checkAuth($request): array
* Check if supplied auth token is valid.
*
* @param string $authToken
* @return bool
* @return object
*/
private function isValidAuthToken(string $authToken): bool
private function isValidAuthToken(string $authToken): ?object
{
$check = $this->entityManger->getRepository(Tokens::class)->findOneBy(['accesstoken' => $authToken]);
return !empty($check);
return $this->entityManger->getRepository(Tokens::class)->findOneBy(['accesstoken' => $authToken]);
}

/**
Expand Down
4 changes: 3 additions & 1 deletion application/src/Traits/GeneralTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public function authHttpCheck(array $requestMethod, Request $request, bool $doAu
// Auth check failed, return error info.
return $authCheck;
}
$response['user_id'] = $authCheck['user_id'];
}

// Check HTTP method is accepted.
Expand All @@ -35,7 +36,8 @@ public function authHttpCheck(array $requestMethod, Request $request, bool $doAu
return $methodCheck;
}

return ['status' => true];
$response['status'] = true;
return $response;
}

/**
Expand Down

0 comments on commit 4f657d0

Please sign in to comment.