From 453660de5498e4f313337cebcfb263faa68bf658 Mon Sep 17 00:00:00 2001 From: Elizabeth Danzberger Date: Tue, 22 Oct 2024 13:36:37 -0400 Subject: [PATCH] fix: get user by display name Signed-off-by: Elizabeth Danzberger --- lib/Controller/MentionController.php | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/lib/Controller/MentionController.php b/lib/Controller/MentionController.php index 0a183360d5..3e5496ef4d 100644 --- a/lib/Controller/MentionController.php +++ b/lib/Controller/MentionController.php @@ -16,6 +16,7 @@ use OCP\AppFramework\Utility\ITimeFactory; use OCP\Files\IRootFolder; use OCP\IRequest; +use OCP\IUserManager; use OCP\Notification\IManager; class MentionController extends Controller { @@ -25,6 +26,7 @@ public function __construct( private IRootFolder $rootFolder, private IManager $manager, private ITimeFactory $timeFactory, + private IUserManager $userManager, private ?string $userId, ) { parent::__construct($appName, $request); @@ -39,14 +41,22 @@ public function mention(int $fileId, string $mention): DataResponse { return new DataResponse([], Http::STATUS_NOT_FOUND); } - $userFolder = $this->rootFolder->getUserFolder($mention); + // Reverse the array of users to pop off the first user later + $userResults = array_reverse($this->userManager->searchDisplayName($mention, 1)); + if (count($userResults) < 1) { + return new DataResponse([], Http::STATUS_NOT_FOUND); + } + + // Get the first user returned in the array + $user = array_pop($userResults); + $userFolder = $this->rootFolder->getUserFolder($user->getUID()); $file = $userFolder->getFirstNodeById($fileId); if ($file === null) { return new DataResponse([], Http::STATUS_NOT_FOUND); } $notification = $this->manager->createNotification(); - $notification->setUser($mention) + $notification->setUser($user->getUID()) ->setApp(Application::APPNAME) ->setSubject(Notifier::TYPE_MENTIONED, [ Notifier::SUBJECT_MENTIONED_SOURCE_USER => $this->userId, @@ -58,7 +68,6 @@ public function mention(int $fileId, string $mention): DataResponse { $notification->setDateTime(\DateTime::createFromImmutable($this->timeFactory->now())); $this->manager->notify($notification); return new DataResponse([], Http::STATUS_OK); - } return new DataResponse([], Http::STATUS_NOT_FOUND);