Skip to content

Commit

Permalink
Merge pull request #3 from andrewnicols/refactorRelationships
Browse files Browse the repository at this point in the history
Refactor relationships to add new backoffice endpoints
  • Loading branch information
andrewnicols authored Aug 1, 2023
2 parents 16ba3ac + d04ca04 commit aedc4f4
Show file tree
Hide file tree
Showing 8 changed files with 399 additions and 279 deletions.
150 changes: 142 additions & 8 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 All @@ -119,10 +114,149 @@ public function backOfficeReset(string $serverID) : JsonResponse
->findBy(['serverid' => $serverID]);
foreach ($entities as $entity) {
$entityManager->remove($entity);
$entityManager->flush();
}
}
$entityManager->flush();

return new JsonResponse((object) ['reset' => true]);
}
}

/**
* @Route("/rooms", name="backOfficeGetAllRooms")
* @return JsonResponse
*/
public function getAllRooms(string $serverID): JSONResponse
{
$rooms = $this->getDoctrine()
->getRepository(Rooms::class)
->findBy(['serverid' => $serverID]);

$responsedata = (object) [
'rooms' => array_map(function ($room) {
$roomdata = $room->jsonSerialize();
$roomdata->members = array_map(
fn($membership) => $membership->getUser()->jsonSerialize(),
array_filter(
$room->getMembers()->toArray(),
function (RoomMember $membership): bool {
if ($membership->getState() !== null) {
return false;
}

return !$membership->getBanned() && $membership->getAccepted();
},
),
);
return $roomdata;
}, $rooms),
];

return new JsonResponse($responsedata);
}

/**
* @Route("/users", name="backOfficeGetAllUsers")
* @return JsonResponse
*/
public function getAllUsers(string $serverID): JSONResponse
{
$users = $this->getDoctrine()
->getRepository(Users::class)
->findBy(['serverid' => $serverID]);

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

/**
* @Route("/create", methods={"PUT"})
* @param string $serverID
* @return JsonResponse
*/
public function setData(
Request $request,
string $serverID,
): JsonResponse {
$entityManager = $this->getDoctrine()->getManager();
$payload = json_decode($request->getContent());
$host = $request->getHost();

$returndata = (object) [
'users' => [],
'rooms' => [],
];

$userRepository = $entityManager->getRepository(Users::class);
$admin = $userRepository->findOneBy(
[
'admin' => true,
'serverid' => $serverID,
]
);

if (property_exists($payload, 'users')) {
foreach ($payload->users as $userdata) {
$user = new Users();
$user->setServerid($serverID);
$user->setDisplayname($userdata->fullname);
$user->setUserid($userdata->id);
$user->setAdmin(false);
$entityManager->persist($user);
$returndata->users[$user->getUserid()] = $user->jsonSerialize();
}
}

if (property_exists($payload, 'rooms')) {
foreach ($payload->rooms as $roomdata) {
$roomName = $roomdata->name ?? rand();
$roomID = sprintf(
"!%s:%s",
substr(
hash('sha256', ($serverID . $roomName . (string) time())),
0,
18,
),
$host,
);

$room = new Rooms();
$room->setRoomid($roomID);
$room->setName($roomName);
$room->setTopic($roomdata->topic ?? null);
$room->setServerid($serverID);
$room->setCreator($admin->getUserid());
$entityManager->persist($room);
$returnroomdata = $room->jsonSerialize();
$returnroomdata->roomID = $roomID;
$returnroomdata->members = [];

if (property_exists($roomdata, 'members')) {
foreach ($roomdata->members as $userid) {
$user = $userRepository->findOneBy([
'serverid' => $serverID,
'userid' => $userid,
]);
$roomMember = new RoomMember();
$roomMember->setRoom($room);
$roomMember->setUser($user);
$roomMember->setAccepted(true);
$roomMember->setBanned();
$roomMember->setServerid($serverID);
$entityManager->persist($roomMember);

$returnroomdata->members[] = $user->getUserid();
}
}

$returndata->rooms[] = $returnroomdata;
}
}
$entityManager->flush();

return new JsonResponse($returndata, 200);
}
}
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);
}
}
Loading

0 comments on commit aedc4f4

Please sign in to comment.