Skip to content

Commit

Permalink
Add backoffice methods to fetch/set data
Browse files Browse the repository at this point in the history
This commit adds new endpoints to /backoffice for:
- /rooms - get all rooms with all room memberships
- /create - create a set of data to be used in testing
  • Loading branch information
andrewnicols committed Jul 19, 2023
1 parent 8ba2aba commit d04ca04
Showing 1 changed file with 140 additions and 1 deletion.
141 changes: 140 additions & 1 deletion application/src/Controller/BackOfficeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,143 @@ public function backOfficeReset(string $serverID) : JsonResponse

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);
}
}

0 comments on commit d04ca04

Please sign in to comment.