Skip to content

Commit

Permalink
Code refactor and invite user API
Browse files Browse the repository at this point in the history
  • Loading branch information
stevandoMoodle committed Jan 24, 2023
1 parent c357dd8 commit da041c6
Show file tree
Hide file tree
Showing 12 changed files with 632 additions and 49 deletions.
3 changes: 2 additions & 1 deletion application/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"ext-ctype": "*",
"ext-iconv": "*",
"composer/package-versions-deprecated": "1.11.99.2",
"doctrine/annotations": "^1.13",
"doctrine/annotations": "^1.14",
"doctrine/doctrine-bundle": "^2.4",
"doctrine/doctrine-migrations-bundle": "^3.1",
"doctrine/orm": "^2.9",
Expand All @@ -21,6 +21,7 @@
"symfony/proxy-manager-bridge": "5.3.*",
"symfony/runtime": "5.3.*",
"symfony/twig-bundle": "5.3.*",
"symfony/validator": "5.3.*",
"symfony/yaml": "5.3.*"
},
"config": {
Expand Down
113 changes: 112 additions & 1 deletion application/composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions application/config/packages/validator.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
framework:
validation:
email_validation_mode: html5

# Enables validator auto-mapping support.
# For instance, basic validation constraints will be inferred from Doctrine's metadata.
#auto_mapping:
# App\Entity\: []

when@test:
framework:
validation:
not_compromised_password: false
108 changes: 66 additions & 42 deletions application/src/Controller/MatrixController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Request;
use App\Service\ApiCheck;
use App\Entity\Rooms;
use App\Entity\Roommembers;
use App\Traits\GeneralTrait;
use App\Traits\MatrixSynapseTrait;

/**
* API Controller to serve a mock of the Matrix API.
Expand All @@ -16,6 +18,8 @@
*/
class MatrixController extends AbstractController {

use GeneralTrait, MatrixSynapseTrait;

/**
* @Route("", name="endpoint")
*/
Expand All @@ -36,19 +40,11 @@ public function endpoint(): JsonResponse
* @return JsonResponse
*/
public function createRoom(string $serverID, Request $request): JsonResponse {
// Check call auth.
$authCheck = ApiCheck::checkAuth($request);
if (!$authCheck['status']) {
// Auth check failed, return error info.
return $authCheck['message'];
}

// Check HTTP method is accepted.
$method = $request->getMethod();
$methodCheck = ApiCheck::checkMethod(['POST'], $method);
if (!$methodCheck['status']) {
// Method check failed, return error info.
return $methodCheck['message'];
// 1. Check call auth.
// 2. Check HTTP method is accepted.
$accessCheck = $this->authHttpCheck('POST', $request);
if (!$accessCheck['status']) {
return $accessCheck['message'];
}

$payload = json_decode($request->getContent());
Expand Down Expand Up @@ -83,30 +79,16 @@ public function createRoom(string $serverID, Request $request): JsonResponse {
* @return JsonResponse
*/
public function roomState(string $serverID, string $roomID, string $eventType, Request $request): JsonResponse {
// Check call auth.
$authCheck = ApiCheck::checkAuth($request);
if (!$authCheck['status']) {
// Auth check failed, return error info.
return $authCheck['message'];
}

// Check HTTP method is accepted.
$method = $request->getMethod();
$methodCheck = ApiCheck::checkMethod(['PUT'], $method);
if (!$methodCheck['status']) {
// Method check failed, return error info.
return $methodCheck['message'];
}

// Check room exists.
$room = $this->roomExists($roomID);
if (empty($room)) {
return new JsonResponse((object) [
'errcode' => 'M_FORBIDDEN',
'error' => 'Unknown room'
], 403);
// 1. Check call auth.
// 2. Check HTTP method is accepted.
$accessCheck = $this->authHttpCheck('PUT', $request);
if (!$accessCheck['status']) {
return $accessCheck['message'];
}

// 3. Check room exists. If exists, "room" property is added.
$roomCheck = $this->roomExists($roomID, true);
$room = $roomCheck['room'];
$payload = json_decode($request->getContent());

if ($eventType == 'm.room.topic') {
Expand Down Expand Up @@ -141,15 +123,57 @@ public function roomState(string $serverID, string $roomID, string $eventType, R
}

/**
* Check if room exists.
* Invite user into a room.
*
* @param string $roomID
* @return object|null
* @Route("/rooms/{roomID}/invite", name="inviteUser")
* @param Request $request
* @return JsonResponse
*/
private function roomExists(string $roomID): ?object
{
public function inviteUser(string $roomID, Request $request): JsonResponse {
// 1. Check call auth.
// 2. Check HTTP method is accepted.
$accessCheck = $this->authHttpCheck('POST', $request);
if (!$accessCheck['status']) {
return $accessCheck['message'];
}

// Check if room exists.
$this->roomExists($roomID);

$payload = json_decode($request->getContent());
$userID = $payload->userid;

// Check if the user has already been invited.
$this->isUserInvited($roomID, $userID);

// Check if the user is banned from the group.
$this->isUserBanned($roomID, $userID);

// Check if "currentuserid" is sent with the body.
if (!isset($payload->currentuserid)) {
return new JsonResponse((object) [
'errcode' => 'M_BAD_JSON',
'message' => '"currentuserid" has not been sent as part of the body'
], 400);
}

// Check if the inviter is a member of the group.
$this->validateRoomInviter($roomID, $payload->currentuserid);

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

$roomMember->setRoomid($roomID);
$roomMember->setReason($payload->reason);
$roomMember->setUserid($userID);
$roomMember->setAccepted();

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

return $entityManager->getRepository(Rooms::class)->findOneBy(['roomid' => $roomID]);
return new JsonResponse((object) [
'message' => 'The user has been invited to join the room'
], 200);
}
}
Loading

0 comments on commit da041c6

Please sign in to comment.