Skip to content

Commit

Permalink
Fixed some APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
stevandoMoodle committed Mar 6, 2023
1 parent a7e297e commit e3af21e
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 51 deletions.
79 changes: 29 additions & 50 deletions application/src/Controller/MatrixController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use App\Entity\Users;
use App\Traits\GeneralTrait;
use App\Traits\MatrixSynapseTrait;
use stdClass;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Annotation\Route;
Expand All @@ -17,14 +18,14 @@
/**
* API Controller to serve a mock of the Matrix API.
*
* @Route("/{serverID}/_matrix/client/r0")
* @Route("/{serverID}/_matrix/client")
*/
class MatrixController extends AbstractController {

use GeneralTrait, MatrixSynapseTrait;

/**
* @Route("", name="endpoint")
* @Route("/r0", name="endpoint")
*/
public function endpoint(): JsonResponse
{
Expand All @@ -37,7 +38,7 @@ public function endpoint(): JsonResponse
/**
* Login a user.
*
* @Route("/login", name="login")
* @Route("/r0/login", name="login")
* @param string $serverID
* @param Request $request
* @return JsonResponse
Expand Down Expand Up @@ -117,7 +118,7 @@ public function login(string $serverID, Request $request) : JsonResponse {
/**
* Refresh the tokens.
*
* @Route("/refresh", name="refresh")
* @Route("/r0/refresh", name="refresh")
* @param string $serverID
* @param Request $request
* @return JsonResponse
Expand Down Expand Up @@ -154,7 +155,7 @@ public function refresh(string $serverID, Request $request) : JsonResponse {
/**
* Create Matrix room.
*
* @Route("/createRoom", name="createRoom")
* @Route("/r0/createRoom", name="createRoom")
* @param string $serverID
* @param Request $request
* @return JsonResponse
Expand Down Expand Up @@ -207,7 +208,7 @@ public function createRoom(string $serverID, Request $request) : JsonResponse {
/**
* Create Matrix room.
*
* @Route("/rooms/{roomID}/kick", name="kick")
* @Route("/r0/rooms/{roomID}/kick", name="kick")
* @param Request $request
* @return JsonResponse
*/
Expand Down Expand Up @@ -245,8 +246,8 @@ public function kick(string $roomID, Request $request) : JsonResponse {
/**
* Update various room state components.
*
* @Route("/rooms/{roomID}/state/{eventType}")
* @Route("/rooms/{roomID}/state/{eventType}/")
* @Route("/r0/rooms/{roomID}/state/{eventType}")
* @Route("/r0/rooms/{roomID}/state/{eventType}/")
* @param string $serverID
* @param string $eventType
* @param Request $request
Expand Down Expand Up @@ -312,65 +313,43 @@ public function roomState(string $serverID, string $roomID, string $eventType, R
}

/**
* Invite user into a room.
* Gets all joined members of a group.
*
* @Route("/rooms/{roomID}/invite", name="inviteUser")
* @Route("/r0/rooms/{roomID}/joined_members", name="getJoinedMembers")
* @param string $serverID
* @param string $roomID
* @param Request $request
* @return JsonResponse
*/
public function inviteUser(string $serverID, string $roomID, Request $request) : JsonResponse {
public function getJoinedMembers(string $serverID, string $roomID, Request $request) : JsonResponse {
// 1. Check call auth.
// 2. Check HTTP method is accepted.
$accessCheck = $this->authHttpCheck(['POST'], $request);
$accessCheck = $this->authHttpCheck(['GET'], $request);
if (!$accessCheck['status']) {
return $accessCheck['message'];
}

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

$payload = json_decode($request->getContent());
$userID = $payload->userid;
// Get all joined members.
$room_members = $this->getDoctrine()
->getRepository(Roommembers::class)
->findBy(['roomid' => $roomID, 'serverid' => $serverID]);

// Check if the user has already been invited.
$check = $this->isUserInvited($roomID, $userID);
if (!$check['status']) {
return $check['message'];
}
$joined_members = new stdClass();
foreach ($room_members as $member) {
$userid = $member->getUserid();

// Check if the user is banned from the group.
$check = $this->isUserBanned($roomID, $userID);
if (!$check['status']) {
return $check['message'];
}
$user = $this->getDoctrine()
->getRepository(Users::class)
->findBy(['userid' => $userid, 'serverid' => $serverID])[0];

// 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);
$userdetail = new stdClass();
$userdetail->avatar_url = $user->getAvatarurl();
$userdetail->display_name = $user->getDisplayname();
$joined_members->{$userid} = $userdetail;
}

// 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();
$roomMember->setServerid($serverID);

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

return new JsonResponse((object) [
'message' => 'The user has been invited to join the room'
'joined' => $joined_members
], 200);
}
}
2 changes: 1 addition & 1 deletion application/src/Controller/SynapseController.php
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ private function upsertUser(string $serverID, string $userID, Request $request,
/**
* Invite user into a room.
*
* @Route("/v2/join/{roomID}", name="inviteUser")
* @Route("/v1/join/{roomID}", name="inviteUser")
* @param string $serverID
* @param Request $request
* @return JsonResponse
Expand Down
17 changes: 17 additions & 0 deletions application/src/Entity/Users.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ class Users
*/
private $passwords;

/**
* @ORM\Column(type="text", nullable=true)
*/
private $avatarurl;

public function __construct()
{
$this->threepids = new ArrayCollection();
Expand Down Expand Up @@ -167,6 +172,18 @@ public function setPasswordpattern(string $passwordpattern = null): self
return $this;
}

public function getAvatarurl(): ?string
{
return $this->avatarurl;
}

public function setAvatarurl(string $avatarurl = null): self
{
$this->avatarurl = $avatarurl;

return $this;
}

/**
* @return Collection<int, Externalids>
*/
Expand Down

0 comments on commit e3af21e

Please sign in to comment.