Skip to content

Commit

Permalink
Added delete room API
Browse files Browse the repository at this point in the history
  • Loading branch information
stevandoMoodle committed Feb 13, 2023
1 parent 6ee459a commit 5838d8e
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 6 deletions.
39 changes: 39 additions & 0 deletions application/src/Controller/SynapseController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use App\Entity\Users;
use App\Entity\Threepids;
use App\Entity\Roommembers;
use App\Entity\Rooms;
use App\Entity\Tokens;
use App\Traits\GeneralTrait;
use App\Traits\MatrixSynapseTrait;
Expand Down Expand Up @@ -298,4 +299,42 @@ public function inviteUser(string $serverID, string $roomID, Request $request) :
'room_id' => $roomID
], 200);
}

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

$entityManager = $this->getDoctrine()->getManager();
$roommembers = $this->getDoctrine()
->getRepository(Roommembers::class)
->findBy(['serverid' => $serverID, 'roomid' => $roomID]);
foreach ($roommembers as $entity) {
$entityManager->remove($entity);
$entityManager->flush();
}

$room = $this->getDoctrine()
->getRepository(Rooms::class)
->findBy(['roomid' => $roomID]);
if (!empty($room)) {
$entityManager->remove($room[0]);
$entityManager->flush();
}

return new JsonResponse((object) [
'delete_id' => substr(hash('sha256', (date("Ymdhms"))), 0, 18)
], 200);
}
}
14 changes: 8 additions & 6 deletions application/src/Traits/GeneralTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,16 @@ trait GeneralTrait {
* @param Request $request
* @return array
*/
public function authHttpCheck(array $requestMethod, Request $request) : array {
public function authHttpCheck(array $requestMethod, Request $request, bool $doAuthChecking = true) : array {
$apiCheck = new ApiCheck($this->getDoctrine()->getManager());

// Check call auth.
$authCheck = $apiCheck->checkAuth($request);
if (!$authCheck['status']) {
// Auth check failed, return error info.
return $authCheck;
if ($doAuthChecking) {
// Check call auth.
$authCheck = $apiCheck->checkAuth($request);
if (!$authCheck['status']) {
// Auth check failed, return error info.
return $authCheck;
}
}

// Check HTTP method is accepted.
Expand Down

0 comments on commit 5838d8e

Please sign in to comment.