Skip to content

Add invite route. #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ https://spec.matrix.org/v1.5/client-server-api/
* Room manipulations (Update room topic, name and avatar): expected method (PUT)
* Get members of existing room: expected method (GET)
* Upload media: expected method (POST)
* Invite a user to participate in a particular room: expected method (POST)

## API coverage
The following are the currently mocked API endpoints. These should respond with the same HTTP status code, content type and response content as a real Synapse server.
Expand Down
76 changes: 76 additions & 0 deletions application/src/Controller/MatrixController.php
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,82 @@ public function kick(
return new JsonResponse((object)[]);
}

/**
* Invite a user into an existing Matrix room.
*
* @param string $serverID The server ID.
* @param string $roomID The Matrix rooms ID.
* @param Request $request The current raw request.
*
* @Route("/r0/rooms/{roomID}/invite")
* @Route("/v3/rooms/{roomID}/invite")
*
* @return JsonResponse
*/
public function invite(
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.
$room = $entityManager->getRepository(Room::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'];
}

$user = $entityManager->getRepository(User::class)->findOneBy([
'serverid' => $serverID,
'userid' => $payload->user_id,
]);
if ($user) {
// Cannot invite user that is already in the room.
$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 invitee is already a member of the room.'
], 403);
}

// Store the room member in the DB.
$roomMember = new RoomMember();

$roomMember->setRoom($room);
$roomMember->setUser($user);
$roomMember->setAccepted(false); // This API only allows a member to be invited. It does not force the join.
$roomMember->setBanned();
$roomMember->setServerid($serverID);

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

return new JsonResponse((object)[], 200);
}

/**
* Update various room state components.
*
Expand Down