Move the validation and preparation of the name to the RoomService

Signed-off-by: Joas Schilling <coding@schilljs.com>
This commit is contained in:
Joas Schilling 2020-06-17 16:37:48 +02:00
parent 31c20949b4
commit 9357f4672d
No known key found for this signature in database
GPG key ID: 7076EA9751AACDDA
7 changed files with 46 additions and 39 deletions

View file

@ -93,15 +93,14 @@ class Create extends Base {
$owner = $input->getOption('owner');
$moderators = $input->getOption('moderator');
$name = trim($name);
if (!$this->validateRoomName($name)) {
$roomType = $public ? Room::PUBLIC_CALL : Room::GROUP_CALL;
try {
$room = $this->roomService->createConversation($roomType, $name);
} catch (\InvalidArgumentException $e) {
$output->writeln('<error>Invalid room name.</error>');
return 1;
}
$roomType = $public ? Room::PUBLIC_CALL : Room::GROUP_CALL;
$room = $this->roomService->createConversation($roomType, $name);
try {
$this->setRoomReadOnly($room, $readonly);

View file

@ -43,6 +43,7 @@ use Symfony\Component\Console\Input\InputDefinition;
trait TRoomCommand {
/** @var Manager */
protected $manager;
/** @var RoomService */
protected $roomService;
@ -52,7 +53,6 @@ trait TRoomCommand {
/** @var IGroupManager */
protected $groupManager;
public function __construct(Manager $manager,
RoomService $roomService,
IUserManager $userManager,

View file

@ -155,6 +155,7 @@ class FilesIntegrationController extends OCSController {
} else {
$name = $groupFolder->getName();
}
$name = $this->roomService->prepareConversationName($name);
$room = $this->roomService->createConversation(Room::PUBLIC_CALL, $name, null, 'file', $fileId);
}
@ -224,6 +225,7 @@ class FilesIntegrationController extends OCSController {
$room = $this->manager->getRoomByObject('file', $fileId);
} catch (RoomNotFoundException $e) {
$name = $share->getNode()->getName();
$name = $this->roomService->prepareConversationName($name);
$room = $this->roomService->createConversation(Room::PUBLIC_CALL, $name, null, 'file', $fileId);
}
} catch (NotFoundException $e) {

View file

@ -104,6 +104,7 @@ class PublicShareAuthController extends OCSController {
} else {
$roomName = trim($share->getTarget(), '/');
}
$roomName = $this->roomService->prepareConversationName($roomName);
// Create the room
$room = $this->roomService->createConversation(Room::PUBLIC_CALL, $roomName, $sharerUser, 'share:password', $shareToken);

View file

@ -631,7 +631,8 @@ class RoomController extends AEnvironmentAwareController {
}
// Create the room
$room = $this->roomService->createConversation(Room::GROUP_CALL, $targetGroup->getDisplayName(), $currentUser);
$name = $this->roomService->prepareConversationName($targetGroup->getDisplayName());
$room = $this->roomService->createConversation(Room::GROUP_CALL, $name, $currentUser);
$usersInGroup = $targetGroup->getUsers();
$participants = [];
@ -677,7 +678,8 @@ class RoomController extends AEnvironmentAwareController {
}
// Create the room
$room = $this->roomService->createConversation(Room::GROUP_CALL, $circle->getName(), $currentUser);
$name = $this->roomService->prepareConversationName($circle->getName());
$room = $this->roomService->createConversation(Room::GROUP_CALL, $name, $currentUser);
$participants = [];
foreach ($circle->getMembers() as $member) {
@ -715,22 +717,18 @@ class RoomController extends AEnvironmentAwareController {
* @return DataResponse
*/
protected function createEmptyRoom(string $roomName, bool $public = true): DataResponse {
$roomName = trim($roomName);
if ($roomName === '') {
return new DataResponse([], Http::STATUS_BAD_REQUEST);
}
$currentUser = $this->userManager->get($this->userId);
if (!$currentUser instanceof IUser) {
return new DataResponse([], Http::STATUS_NOT_FOUND);
}
$roomType = $public ? Room::PUBLIC_CALL : Room::GROUP_CALL;
// Create the room
if ($public) {
$room = $this->roomService->createConversation(Room::PUBLIC_CALL, $roomName, $currentUser);
} else {
$room = $this->roomService->createConversation(Room::GROUP_CALL, $roomName, $currentUser);
try {
$room = $this->roomService->createConversation($roomType, $roomName, $currentUser);
} catch (\InvalidArgumentException $e) {
return new DataResponse([], Http::STATUS_BAD_REQUEST);
}
return new DataResponse($this->formatRoom($room, $room->getParticipant($currentUser->getUID())), Http::STATUS_CREATED);

View file

@ -22,8 +22,7 @@ declare(strict_types=1);
namespace OCA\Talk\Service;
use OCA\Talk\Exceptions\ParticipantNotFoundException;
use InvalidArgumentException;
use OCA\Talk\Exceptions\RoomNotFoundException;
use OCA\Talk\Manager;
use OCA\Talk\Participant;
@ -47,7 +46,7 @@ class RoomService {
*/
public function createOneToOneConversation(IUser $actor, IUser $targetUser): Room {
if ($actor->getUID() === $targetUser->getUID()) {
throw new \InvalidArgumentException('invalid_invitee');
throw new InvalidArgumentException('invalid_invitee');
}
try {
@ -68,9 +67,19 @@ class RoomService {
return $room;
}
/**
* @param int $type
* @param string $name
* @param IUser|null $owner
* @param string $objectType
* @param string $objectId
* @return Room
* @throws InvalidArgumentException on too long or empty names
*/
public function createConversation(int $type, string $name, ?IUser $owner = null, string $objectType = '', string $objectId = ''): Room {
$name = trim($name);
if ($name === '' || isset($name[255])) {
throw new \InvalidArgumentException('name');
throw new InvalidArgumentException('name');
}
$room = $this->manager->createRoom($type, $name, $objectType, $objectId);
@ -84,4 +93,8 @@ class RoomService {
return $room;
}
public function prepareConversationName(string $objectName): string {
return rtrim(mb_substr(ltrim($objectName), 0, 64));
}
}

View file

@ -287,11 +287,17 @@ class CreateTest extends TestCase {
* @dataProvider invalidProvider
*/
public function testInvalid(array $input, string $expectedOutput): void {
$this->roomService
->method('createConversation')
->willReturnCallback(function (int $type, string $name): Room {
return $this->roomMockContainer->create(['name' => $name, 'type' => $type]);
});
if ($input['name'] !== 'PHPUnit Test Room') {
$this->roomService
->method('createConversation')
->willThrowException(new \InvalidArgumentException('name'));
} else {
$this->roomService
->method('createConversation')
->willReturnCallback(function (int $type, string $name): Room {
return $this->roomMockContainer->create(['name' => $name, 'type' => $type]);
});
}
$this->roomMockContainer->registerCallback(function (object $room) {
/** @var Room|MockObject $room */
@ -313,18 +319,6 @@ class CreateTest extends TestCase {
],
"Invalid room name.\n",
],
[
[
'name' => ' ',
],
"Invalid room name.\n",
],
[
[
'name' => str_repeat('x', 256),
],
"Invalid room name.\n",
],
[
[
'name' => 'PHPUnit Test Room',