Skip to content

Commit

Permalink
Roommembers => RoomMember adding ORM relationships
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewnicols committed Jul 19, 2023
1 parent bcf39ea commit 8ba2aba
Show file tree
Hide file tree
Showing 8 changed files with 257 additions and 275 deletions.
7 changes: 1 addition & 6 deletions application/src/Controller/BackOfficeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use App\Entity\Externalids;
use App\Entity\Medias;
use App\Entity\Passwords;
use App\Entity\Roommembers;
use App\Entity\RoomMember;
use App\Entity\Rooms;
use App\Entity\Threepids;
use App\Entity\Tokens;
Expand Down Expand Up @@ -103,12 +103,7 @@ public function backOfficeReset(string $serverID) : JsonResponse
{
$entities = [
Users::class,
Tokens::class,
Passwords::class,
Rooms::class,
Roommembers::class,
Threepids::class,
Externalids::class,
Medias::class
];

Expand Down
98 changes: 64 additions & 34 deletions application/src/Controller/MatrixController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use App\Entity\Passwords;
use App\Entity\Rooms;
use App\Entity\Roommembers;
use App\Entity\RoomMember;
use App\Entity\Tokens;
use App\Entity\Users;
use App\Traits\GeneralTrait;
Expand Down Expand Up @@ -280,18 +280,27 @@ public function createRoom(string $serverID, Request $request):JsonResponse {
* @param Request $request
* @return JsonResponse
*/
public function kick(string $roomID, Request $request):JsonResponse {
public function kick(
string $serverID,
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'];
}

$entityManager = $this->getDoctrine()->getManager();

// Check room exists.
$roomCheck = $this->roomExists($roomID);
if (!$roomCheck['status']) {
return $roomCheck['message'];
$room = $entityManager->getRepository(Rooms::class)->findOneBy([
'serverid' => $serverID,
'roomid' => $roomID,
]);
if (!$room) {
return $this->getUnknownRoomResponse();
}

$payload = json_decode($request->getContent());
Expand All @@ -300,20 +309,32 @@ public function kick(string $roomID, Request $request):JsonResponse {
return $check['message'];
}

$roommembers = $this->getRoomMember($roomID, $payload->user_id);
if (empty($roommembers)) {
$user = $entityManager->getRepository(Users::class)->findOneBy([
'serverid' => $serverID,
'userid' => $payload->user_id,
]);
if (!$user) {
return $this->getUnknownRoomResponse();
}

$membership = $entityManager->getRepository(RoomMember::class)->findOneBy([
'serverid' => $serverID,
'room' => $room,
'user' => $user,
]);

if (empty($membership)) {
return new JsonResponse((object) [
'errcode' => 'M_NOT_MEMBER',
'error' => 'The target user_id is not a room member.'
], 403);
}

// Update th membership.
$roommembers->setState('leave');
$roommembers->setReason($payload->reason ?? null);
// Update the membership.
$membership->setState('leave');
$membership->setReason($payload->reason ?? null);

$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($roommembers);
$entityManager->persist($membership);
$entityManager->flush();
return new JsonResponse((object)[]);
}
Expand All @@ -339,12 +360,17 @@ public function roomState(string $serverID, string $roomID, string $eventType, R
return $accessCheck['message'];
}

// Check room exists. If exists, "room" property is added.
$roomCheck = $this->roomExists($roomID, true);
if (!$roomCheck['status']) {
return $roomCheck['message'];
$entityManager = $this->getDoctrine()->getManager();

// Check room exists.
$room = $entityManager->getRepository(Rooms::class)->findOneBy([
'serverid' => $serverID,
'roomid' => $roomID,
]);
if (!$room) {
return $this->getUnknownRoomResponse();
}
$room = $roomCheck['room'];

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

if ($eventType == 'm.room.topic') {
Expand Down Expand Up @@ -414,27 +440,31 @@ public function getJoinedMembers(string $serverID, string $roomID, Request $requ
return $accessCheck['message'];
}

$entityManager = $this->getDoctrine()->getManager();
$room = $entityManager->getRepository(Rooms::class)->findOneBy([
'serverid' => $serverID,
'roomid' => $roomID,
]);

if (!$room) {
return $this->getUnknownRoomResponse();
}

// Get all joined members.
$room_members = $this->getDoctrine()
->getRepository(Roommembers::class)
->findBy(['roomid' => $roomID, 'serverid' => $serverID, 'state' => null]);

$joined_members = new stdClass();
foreach ($room_members as $member) {
$userid = $member->getUserid();

$user = $this->getDoctrine()
->getRepository(Users::class)
->findBy(['userid' => $userid, 'serverid' => $serverID])[0];

$userdetail = new stdClass();
$userdetail->avatar_url = $user->getAvatarurl();
$userdetail->display_name = $user->getDisplayname();
$joined_members->{$userid} = $userdetail;
$members = $this->getDoctrine()
->getRepository(RoomMember::class)
->findBy(['room' => $room, 'serverid' => $serverID, 'state' => null]);

$memberinfo = [];
foreach ($members as $member) {
$memberinfo[$member->getUser()->getUserid()] = (object) [
'avatar_url' => $member->getUser()->getAvatarurl(),
'display_name' => $member->getUser()->getDisplayname(),
];
}

return new JsonResponse((object) [
'joined' => $joined_members
'joined' => (object) $memberinfo,
], 200);
}
}
122 changes: 73 additions & 49 deletions application/src/Controller/SynapseController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use Symfony\Component\HttpFoundation\Request;
use App\Entity\Users;
use App\Entity\Threepids;
use App\Entity\Roommembers;
use App\Entity\RoomMember;
use App\Entity\Rooms;
use App\Entity\Tokens;
use App\Traits\GeneralTrait;
Expand Down Expand Up @@ -244,7 +244,7 @@ private function upsertUser(string $serverID, string $userID, Request $request,
/**
* Invite user into a room.
*
* @Route("/v1/join/{roomID}", name="inviteUser")
* @Route("/v1/join/{roomID}", name="inviteUser", methods={"POST"})
* @param string $serverID
* @param Request $request
* @return JsonResponse
Expand All @@ -257,40 +257,67 @@ public function inviteUser(string $serverID, string $roomID, Request $request):
return $accessCheck['message'];
}

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

// Check room exists.
$room = $entityManager->getRepository(Rooms::class)->findOneBy([
'serverid' => $serverID,
'roomid' => $roomID,
]);
if (!$room) {
return $this->getUnknownRoomResponse();
}

$payload = json_decode($request->getContent());
$check = $this->validateRequest((array)$payload, ['user_id']);
if (!$check['status']) {
return $check['message'];
}
$userID = $payload->user_id;

// Check if the user has already been invited.
$check = $this->isUserInvited($roomID, $userID);
if (!$check['status']) {
return $check['message'];
$user = $entityManager->getRepository(Users::class)->findOneBy([
'serverid' => $serverID,
'userid' => $payload->user_id,
]);
if (!$user) {
return $this->getUnknownRoomResponse();
}

// Check if the user is banned from the group.
$check = $this->isUserBanned($roomID, $userID);
if (!$check['status']) {
return $check['message'];
}
$membership = $entityManager->getRepository(RoomMember::class)->findOneBy([
'serverid' => $serverID,
'user' => $user,
'room' => $room,
'state' => null,
]);

if ($membership) {
if ($membership->getAccepted()) {
// Already a member.
return new JsonResponse((object) [
'errcode' => 'M_USER_EXISTS',
'error' => 'The invitee is already a member of the room'
], 403);
}

// Store the room member in the DB.
$entityManager = $this->getDoctrine()->getManager();
$roomMember = new Roommembers();
if ($membership->getBanned()) {
return new JsonResponse((object) [
'errcode' => 'M_USER_IS_BANNED',
'error' => 'you cannot invite the user due to being banned from the group.'
], 403);
}

$roomMember->setRoomid($roomID);
$roomMember->setUserid($userID);
$roomMember->setAccepted(true);
$roomMember->setBanned();
$roomMember->setServerid($serverID);
// Thenw aht!???
$membership->setAccepted(true);
} else {
// Store the room member in the DB.
$entityManager = $this->getDoctrine()->getManager();
$roomMember = new RoomMember();

$roomMember->setRoom($room);
$roomMember->setUser($user);
$roomMember->setAccepted(true);
$roomMember->setBanned();
$roomMember->setServerid($serverID);
}

$entityManager->persist($roomMember);
$entityManager->flush();
Expand All @@ -317,19 +344,16 @@ public function deleteRoom(string $serverID, string $roomID, Request $request):
}

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

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

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

return new JsonResponse((object) [
Expand All @@ -353,28 +377,28 @@ public function roomInfo(string $serverID, string $roomID, Request $request): Js
return $accessCheck['message'];
}

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

// Check room exists.
$room = $entityManager->getRepository(Rooms::class)->findOneBy([
'serverid' => $serverID,
'roomid' => $roomID,
]);
if (!$room) {
return $this->getUnknownRoomResponse();
}
$room = $check['room'];

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

return new JsonResponse((object) [
'room_id' => $room->getRoomid(),
'name' => $room->getName(),
'canonical_alias' => $room->getRoomAlias(),
'joined_members' => $room_members,
'joined_members' => $memberCount,
'creator' => $room->getCreator(),
'avatar' => $room->getAvatar(),
'topic' => $room->getTopic()
Expand Down
Loading

0 comments on commit 8ba2aba

Please sign in to comment.