Skip to content

Commit

Permalink
fix(events): Migrate files integration to typed events
Browse files Browse the repository at this point in the history
Signed-off-by: Joas Schilling <coding@schilljs.com>
  • Loading branch information
nickvergessen committed Oct 30, 2023
1 parent 609bcc3 commit b6a662b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 29 deletions.
7 changes: 6 additions & 1 deletion lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,10 @@
use OCA\Talk\Events\AttendeesAddedEvent;
use OCA\Talk\Events\AttendeesRemovedEvent;
use OCA\Talk\Events\BeforeChatMessageSentEvent;
use OCA\Talk\Events\BeforeGuestJoinedRoomEvent;
use OCA\Talk\Events\BeforeParticipantModifiedEvent;
use OCA\Talk\Events\BeforeRoomsFetchEvent;
use OCA\Talk\Events\BeforeUserJoinedRoomEvent;
use OCA\Talk\Events\BotInstallEvent;
use OCA\Talk\Events\BotUninstallEvent;
use OCA\Talk\Events\CallEndedForEveryoneEvent;
Expand Down Expand Up @@ -170,6 +172,10 @@ public function register(IRegistrationContext $context): void {
// Command listener
$context->registerEventListener(BeforeChatMessageSentEvent::class, CommandListener::class);

// Files integration listeners
$context->registerEventListener(BeforeGuestJoinedRoomEvent::class, FilesListener::class);
$context->registerEventListener(BeforeUserJoinedRoomEvent::class, FilesListener::class);

// Reference listeners
$context->registerEventListener(AttendeesAddedEvent::class, ReferenceInvalidationListener::class);
$context->registerEventListener(AttendeesRemovedEvent::class, ReferenceInvalidationListener::class);
Expand Down Expand Up @@ -247,7 +253,6 @@ public function boot(IBootContext $context): void {
SystemMessageListener::register($dispatcher);
ParserListener::register($dispatcher);
PublicShareAuthListener::register($dispatcher);
FilesListener::register($dispatcher);
SignalingListener::register($dispatcher);
CollaboratorsListener::register($dispatcher);
}
Expand Down
55 changes: 28 additions & 27 deletions lib/Files/Listener.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,17 @@

namespace OCA\Talk\Files;

use OCA\Talk\Events\JoinRoomGuestEvent;
use OCA\Talk\Events\JoinRoomUserEvent;
use OCA\Talk\Events\BeforeGuestJoinedRoomEvent;
use OCA\Talk\Events\BeforeUserJoinedRoomEvent;
use OCA\Talk\Exceptions\ParticipantNotFoundException;
use OCA\Talk\Exceptions\UnauthorizedException;
use OCA\Talk\Model\Attendee;
use OCA\Talk\Room;
use OCA\Talk\Service\ParticipantService;
use OCA\Talk\TalkSession;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
use OCP\IUserManager;
use OCP\Server;

/**
* Custom behaviour for rooms for files.
Expand All @@ -51,8 +51,10 @@
* These rooms are associated to a "file" object, and their custom behaviour is
* provided by calling the methods of this class as a response to different room
* events.
*
* @template-implements IEventListener<Event>
*/
class Listener {
class Listener implements IEventListener {

public function __construct(
protected Util $util,
Expand All @@ -62,29 +64,28 @@ public function __construct(
) {
}

public static function register(IEventDispatcher $dispatcher): void {
$listener = static function (JoinRoomUserEvent $event): void {
$listener = Server::get(self::class);

try {
$listener->preventUsersWithoutAccessToTheFileFromJoining($event->getRoom(), $event->getUser()->getUID());
$listener->addUserAsPersistentParticipant($event->getRoom(), $event->getUser()->getUID());
} catch (UnauthorizedException $e) {
$event->setCancelJoin(true);
}
public function handle(Event $event): void {
match (get_class($event)) {
BeforeUserJoinedRoomEvent::class => $this->beforeUserJoinedRoomEvent($event),
BeforeGuestJoinedRoomEvent::class => $this->beforeGuestJoinedRoomEvent($event),
};
$dispatcher->addListener(Room::EVENT_BEFORE_ROOM_CONNECT, $listener);
}

$listener = static function (JoinRoomGuestEvent $event): void {
$listener = Server::get(self::class);
protected function beforeUserJoinedRoomEvent(BeforeUserJoinedRoomEvent $event): void {
try {
$this->preventUsersWithoutAccessToTheFileFromJoining($event->getRoom(), $event->getUser()->getUID());
$this->addUserAsPersistentParticipant($event->getRoom(), $event->getUser()->getUID());
} catch (UnauthorizedException) {
$event->setCancelJoin(true);
}
}

try {
$listener->preventGuestsFromJoiningIfNotPubliclyAccessible($event->getRoom());
} catch (UnauthorizedException $e) {
$event->setCancelJoin(true);
}
};
$dispatcher->addListener(Room::EVENT_BEFORE_GUEST_CONNECT, $listener);
protected function beforeGuestJoinedRoomEvent(BeforeGuestJoinedRoomEvent $event): void {
try {
$this->preventGuestsFromJoiningIfNotPubliclyAccessible($event->getRoom());
} catch (UnauthorizedException) {
$event->setCancelJoin(true);
}
}

/**
Expand All @@ -103,7 +104,7 @@ public static function register(IEventDispatcher $dispatcher): void {
* @param string $userId
* @throws UnauthorizedException
*/
public function preventUsersWithoutAccessToTheFileFromJoining(Room $room, string $userId): void {
protected function preventUsersWithoutAccessToTheFileFromJoining(Room $room, string $userId): void {
if ($room->getObjectType() !== 'file') {
return;
}
Expand Down Expand Up @@ -132,7 +133,7 @@ public function preventUsersWithoutAccessToTheFileFromJoining(Room $room, string
* @param Room $room
* @param string $userId
*/
public function addUserAsPersistentParticipant(Room $room, string $userId): void {
protected function addUserAsPersistentParticipant(Room $room, string $userId): void {
if ($room->getObjectType() !== 'file') {
return;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Files/TemplateLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public function handle(Event $event): void {
}

Util::addStyle(Application::APP_ID, 'icons');
if (strpos($this->request->getPathInfo(), '/apps/maps') !== 0) {
if (!str_starts_with($this->request->getPathInfo(), '/apps/maps')) {
Util::addScript(Application::APP_ID, 'talk-files-sidebar');
}

Expand Down

0 comments on commit b6a662b

Please sign in to comment.