diff options
author | Joas Schilling <coding@schilljs.com> | 2022-09-13 15:16:54 +0300 |
---|---|---|
committer | Joas Schilling <coding@schilljs.com> | 2022-09-13 15:58:22 +0300 |
commit | 47af0226135041b9d92bd14c037c243497acda4b (patch) | |
tree | 49f73c193d7d2de9699b65a2444adb9477baa1ab | |
parent | 973fe619a84b5576c058b82ce90d17abbb87f5a0 (diff) |
Use IUserManager::getDisplayName()
Signed-off-by: Joas Schilling <coding@schilljs.com>
-rw-r--r-- | lib/Activity/Provider.php | 11 | ||||
-rw-r--r-- | lib/Controller/APIController.php | 6 | ||||
-rw-r--r-- | lib/Dashboard/Widget.php | 6 | ||||
-rw-r--r-- | lib/Notification/Notifier.php | 8 | ||||
-rw-r--r-- | tests/Notification/NotifierTest.php | 24 |
5 files changed, 10 insertions, 45 deletions
diff --git a/lib/Activity/Provider.php b/lib/Activity/Provider.php index bc677b1..2bb8248 100644 --- a/lib/Activity/Provider.php +++ b/lib/Activity/Provider.php @@ -30,7 +30,6 @@ use OCP\Activity\IEvent; use OCP\Activity\IManager as IActivityManager; use OCP\Activity\IProvider; use OCP\IURLGenerator; -use OCP\IUser; use OCP\IUserManager; use OCP\L10N\IFactory; @@ -173,7 +172,7 @@ class Provider implements IProvider { protected function generateUserParameter(string $uid): array { if (!isset($this->displayNames[$uid])) { - $this->displayNames[$uid] = $this->getDisplayName($uid); + $this->displayNames[$uid] = $this->userManager->getDisplayName($uid) ?? $uid; } return [ @@ -182,12 +181,4 @@ class Provider implements IProvider { 'name' => $this->displayNames[$uid], ]; } - - protected function getDisplayName(string $uid): string { - $user = $this->userManager->get($uid); - if ($user instanceof IUser) { - return $user->getDisplayName(); - } - return $uid; - } } diff --git a/lib/Controller/APIController.php b/lib/Controller/APIController.php index 9306a38..3a7e5a5 100644 --- a/lib/Controller/APIController.php +++ b/lib/Controller/APIController.php @@ -143,11 +143,7 @@ class APIController extends OCSController { } protected function renderAnnouncement(Announcement $announcement): array { - $displayName = $announcement->getUser(); - $user = $this->userManager->get($announcement->getUser()); - if ($user instanceof IUser) { - $displayName = $user->getDisplayName(); - } + $displayName = $this->userManager->getDisplayName($announcement->getUser()) ?? $announcement->getUser(); $result = [ 'id' => $announcement->getId(), diff --git a/lib/Dashboard/Widget.php b/lib/Dashboard/Widget.php index d71e116..e4d5b2b 100644 --- a/lib/Dashboard/Widget.php +++ b/lib/Dashboard/Widget.php @@ -111,11 +111,7 @@ class Widget implements IWidget { } protected function renderAnnouncement(Announcement $announcement): array { - $displayName = $announcement->getUser(); - $user = $this->userManager->get($announcement->getUser()); - if ($user instanceof IUser) { - $displayName = $user->getDisplayName(); - } + $displayName = $this->userManager->getDisplayName($announcement->getUser()) ?? $announcement->getUser(); $result = [ 'id' => $announcement->getId(), diff --git a/lib/Notification/Notifier.php b/lib/Notification/Notifier.php index e8a5ee4..0d447e1 100644 --- a/lib/Notification/Notifier.php +++ b/lib/Notification/Notifier.php @@ -28,7 +28,6 @@ namespace OCA\AnnouncementCenter\Notification; use OCA\AnnouncementCenter\Manager; use OCA\AnnouncementCenter\Model\AnnouncementDoesNotExistException; use OCP\IURLGenerator; -use OCP\IUser; use OCP\IUserManager; use OCP\L10N\IFactory; use OCP\Notification\AlreadyProcessedException; @@ -113,12 +112,7 @@ class Notifier implements INotifier { } $params = $notification->getSubjectParameters(); - $user = $this->userManager->get($params[0]); - if ($user instanceof IUser) { - $displayName = $user->getDisplayName(); - } else { - $displayName = $params[0]; - } + $displayName = $this->userManager->getDisplayName($params[0]) ?? $params[0]; $link = $this->urlGenerator->linkToRouteAbsolute('announcementcenter.page.index', [ 'announcement' => $notification->getObjectId(), diff --git a/tests/Notification/NotifierTest.php b/tests/Notification/NotifierTest.php index ae72fce..f1835cc 100644 --- a/tests/Notification/NotifierTest.php +++ b/tests/Notification/NotifierTest.php @@ -35,7 +35,6 @@ use OCP\L10N\IFactory; use OCP\Notification\AlreadyProcessedException; use OCP\Notification\IManager; use OCP\Notification\INotification; -use OCP\IUser; use PHPUnit\Framework\MockObject\MockObject; class NotifierTest extends TestCase { @@ -136,22 +135,11 @@ class NotifierTest extends TestCase { $this->notifier->prepare($notification, 'en'); } - /** - * @return IUser|MockObject - */ - protected function getUserMock() { - $user = $this->createMock(IUser::class); - $user->expects(self::exactly(2)) - ->method('getDisplayName') - ->willReturn('Author'); - return $user; - } - public function dataPrepare(): array { $message = "message\nmessage message message message message message message message message message message messagemessagemessagemessagemessagemessagemessage"; return [ ['author', 'subject', 'message', 'message', '42', null, 'author announced “subject”', 'message'], - ['author1', 'subject', 'message', 'message', '42', $this->getUserMock(), 'Author announced “subject”', 'message'], + ['author1', 'subject', 'message', 'message', '42', 'Author', 'Author announced “subject”', 'message'], ['author2', "subject\nsubject", $message, $message, '21', null, 'author2 announced “subject subject”', $message], ]; } @@ -164,11 +152,11 @@ class NotifierTest extends TestCase { * @param string $message * @param string $plainMessage * @param int $objectId - * @param \OCP\IUser $userObject + * @param ?string $userDisplayName * @param string $expectedSubject * @param string $expectedMessage */ - public function testPrepare($author, $subject, $message, $plainMessage, $objectId, $userObject, $expectedSubject, $expectedMessage): void { + public function testPrepare($author, $subject, $message, $plainMessage, $objectId, $userDisplayName, $expectedSubject, $expectedMessage): void { /** @var INotification|MockObject $notification */ $notification = $this->createMock(INotification::class); @@ -196,9 +184,9 @@ class NotifierTest extends TestCase { ->with($objectId, false) ->willReturn($announcement); $this->userManager->expects(self::once()) - ->method('get') + ->method('getDisplayName') ->with($author) - ->willReturn($userObject); + ->willReturn($userDisplayName); $notification->expects(self::once()) ->method('setParsedMessage') @@ -215,7 +203,7 @@ class NotifierTest extends TestCase { 'user' => [ 'type' => 'user', 'id' => 'author', - 'name' => $userObject instanceof IUser ? $userObject->getDisplayName() : $author, + 'name' => $userDisplayName ?? $author, ], 'announcement' => [ 'type' => 'announcement', |