From f4918f208e4ef09d197c00e8eeab6963cd55b0a8 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Tue, 28 Jan 2020 12:51:43 +0100 Subject: [PATCH] Unify the code a bit Signed-off-by: Joas Schilling --- apps/files/lib/Activity/Provider.php | 52 +++++++++++++------ .../lib/Activity/Providers/Base.php | 48 +++++++++++------ .../lib/Activity/Providers/Groups.php | 4 +- .../lib/Activity/Providers/RemoteShares.php | 23 ++------ 4 files changed, 74 insertions(+), 53 deletions(-) diff --git a/apps/files/lib/Activity/Provider.php b/apps/files/lib/Activity/Provider.php index 56d9f750fdf..c8d2794a181 100644 --- a/apps/files/lib/Activity/Provider.php +++ b/apps/files/lib/Activity/Provider.php @@ -29,6 +29,7 @@ use OCP\Activity\IEventMerger; use OCP\Activity\IManager; use OCP\Activity\IProvider; use OCP\Contacts\IManager as IContactsManager; +use OCP\Federation\ICloudIdManager; use OCP\Files\Folder; use OCP\Files\InvalidPathException; use OCP\Files\IRootFolder; @@ -65,10 +66,13 @@ class Provider implements IProvider { /** @var IEventMerger */ protected $eventMerger; + /** @var ICloudIdManager */ + protected $cloudIdManager; + /** @var IContactsManager */ protected $contactsManager; - /** @var string[] cached displayNames - key is the UID and value the displayname */ + /** @var string[] cached displayNames - key is the cloud id and value the displayname */ protected $displayNames = []; protected $fileIsEncrypted = false; @@ -78,6 +82,7 @@ class Provider implements IProvider { IManager $activityManager, IUserManager $userManager, IRootFolder $rootFolder, + ICloudIdManager $cloudIdManager, IContactsManager $contactsManager, IEventMerger $eventMerger) { $this->languageFactory = $languageFactory; @@ -85,6 +90,7 @@ class Provider implements IProvider { $this->activityManager = $activityManager; $this->userManager = $userManager; $this->rootFolder = $rootFolder; + $this->cloudIdManager = $cloudIdManager; $this->contactsManager = $contactsManager; $this->eventMerger = $eventMerger; } @@ -483,28 +489,41 @@ class Provider implements IProvider { * @return array */ protected function getUser($uid) { - if (!isset($this->displayNames[$uid])) { - $this->displayNames[$uid] = $this->getDisplayName($uid); + // First try local user + $user = $this->userManager->get($uid); + if ($user instanceof IUser) { + return [ + 'type' => 'user', + 'id' => $user->getUID(), + 'name' => $user->getDisplayName(), + ]; + } + + // Then a contact from the addressbook + if ($this->cloudIdManager->isValidCloudId($uid)) { + $cloudId = $this->cloudIdManager->resolveCloudId($uid); + return [ + 'type' => 'user', + 'id' => $cloudId->getUser(), + 'name' => $this->getDisplayNameFromAddressBook($cloudId->getDisplayId()), + 'server' => $cloudId->getRemote(), + ]; } + // Fallback to empty dummy data return [ 'type' => 'user', 'id' => $uid, - 'name' => $this->displayNames[$uid], + 'name' => $uid, ]; } - /** - * @param string $uid - * @return string - */ - protected function getDisplayName($uid) { - $user = $this->userManager->get($uid); - if ($user instanceof IUser) { - return $user->getDisplayName(); + protected function getDisplayNameFromAddressBook(string $search): string { + if (isset($this->displayNames[$search])) { + return $this->displayNames[$search]; } - $addressBookContacts = $this->contactsManager->search($uid, ['CLOUD']); + $addressBookContacts = $this->contactsManager->search($search, ['CLOUD']); foreach ($addressBookContacts as $contact) { if (isset($contact['isLocalSystemBook'])) { continue; @@ -516,15 +535,16 @@ class Provider implements IProvider { $cloudIds = [$cloudIds]; } - $lowerSearch = strtolower($uid); + $lowerSearch = strtolower($search); foreach ($cloudIds as $cloudId) { if (strtolower($cloudId) === $lowerSearch) { - return $contact['FN'] . " ($cloudId)"; + $this->displayNames[$search] = $contact['FN'] . " ($cloudId)"; + return $this->displayNames[$search]; } } } } - return $uid; + return $search; } } diff --git a/apps/files_sharing/lib/Activity/Providers/Base.php b/apps/files_sharing/lib/Activity/Providers/Base.php index 77ebfc26447..b63eb346f7c 100644 --- a/apps/files_sharing/lib/Activity/Providers/Base.php +++ b/apps/files_sharing/lib/Activity/Providers/Base.php @@ -27,6 +27,7 @@ use OCP\Activity\IEvent; use OCP\Activity\IManager; use OCP\Activity\IProvider; use OCP\Contacts\IManager as IContactsManager; +use OCP\Federation\ICloudIdManager; use OCP\IL10N; use OCP\IURLGenerator; use OCP\IUser; @@ -53,6 +54,9 @@ abstract class Base implements IProvider { /** @var IContactsManager */ protected $contactsManager; + /** @var ICloudIdManager */ + protected $cloudIdManager; + /** @var array */ protected $displayNames = []; @@ -60,11 +64,13 @@ abstract class Base implements IProvider { IURLGenerator $url, IManager $activityManager, IUserManager $userManager, + ICloudIdManager $cloudIdManager, IContactsManager $contactsManager) { $this->languageFactory = $languageFactory; $this->url = $url; $this->activityManager = $activityManager; $this->userManager = $userManager; + $this->cloudIdManager = $cloudIdManager; $this->contactsManager = $contactsManager; } @@ -163,31 +169,40 @@ abstract class Base implements IProvider { * @return array */ protected function getUser($uid) { - if (!isset($this->displayNames[$uid])) { - $this->displayNames[$uid] = $this->getDisplayName($uid); + // First try local user + $user = $this->userManager->get($uid); + if ($user instanceof IUser) { + return [ + 'type' => 'user', + 'id' => $user->getUID(), + 'name' => $user->getDisplayName(), + ]; } + // Then a contact from the addressbook + if ($this->cloudIdManager->isValidCloudId($uid)) { + $cloudId = $this->cloudIdManager->resolveCloudId($uid); + return [ + 'type' => 'user', + 'id' => $cloudId->getUser(), + 'name' => $this->getDisplayNameFromAddressBook($cloudId->getDisplayId()), + 'server' => $cloudId->getRemote(), + ]; + } + + // Fallback to empty dummy data return [ 'type' => 'user', 'id' => $uid, - 'name' => $this->displayNames[$uid], + 'name' => $uid, ]; } - /** - * @param string $uid - * @return string - */ - protected function getDisplayName($uid) { - $user = $this->userManager->get($uid); - if ($user instanceof IUser) { - return $user->getDisplayName(); + protected function getDisplayNameFromAddressBook(string $search): string { + if (isset($this->displayNames[$search])) { + return $this->displayNames[$search]; } - return $this->getDisplayNameForContact($uid); - } - - protected function getDisplayNameForContact(string $search): string { $addressBookContacts = $this->contactsManager->search($search, ['CLOUD']); foreach ($addressBookContacts as $contact) { if (isset($contact['isLocalSystemBook'])) { @@ -203,7 +218,8 @@ abstract class Base implements IProvider { $lowerSearch = strtolower($search); foreach ($cloudIds as $cloudId) { if (strtolower($cloudId) === $lowerSearch) { - return $contact['FN'] . " ($cloudId)"; + $this->displayNames[$search] = $contact['FN'] . " ($cloudId)"; + return $this->displayNames[$search]; } } } diff --git a/apps/files_sharing/lib/Activity/Providers/Groups.php b/apps/files_sharing/lib/Activity/Providers/Groups.php index 35fd03e0caa..8a061f9373d 100644 --- a/apps/files_sharing/lib/Activity/Providers/Groups.php +++ b/apps/files_sharing/lib/Activity/Providers/Groups.php @@ -27,6 +27,7 @@ namespace OCA\Files_Sharing\Activity\Providers; use OCP\Activity\IEvent; use OCP\Activity\IManager; use OCP\Contacts\IManager as IContactsManager; +use OCP\Federation\ICloudIdManager; use OCP\IGroup; use OCP\IGroupManager; use OCP\IURLGenerator; @@ -53,9 +54,10 @@ class Groups extends Base { IURLGenerator $url, IManager $activityManager, IUserManager $userManager, + ICloudIdManager $cloudIdManager, IContactsManager $contactsManager, IGroupManager $groupManager) { - parent::__construct($languageFactory, $url, $activityManager, $userManager, $contactsManager); + parent::__construct($languageFactory, $url, $activityManager, $userManager, $cloudIdManager, $contactsManager); $this->groupManager = $groupManager; } diff --git a/apps/files_sharing/lib/Activity/Providers/RemoteShares.php b/apps/files_sharing/lib/Activity/Providers/RemoteShares.php index 28c62845f81..36d4dbbb8fd 100644 --- a/apps/files_sharing/lib/Activity/Providers/RemoteShares.php +++ b/apps/files_sharing/lib/Activity/Providers/RemoteShares.php @@ -34,8 +34,6 @@ use OCP\L10N\IFactory; class RemoteShares extends Base { - protected $cloudIdManager; - const SUBJECT_REMOTE_SHARE_ACCEPTED = 'remote_share_accepted'; const SUBJECT_REMOTE_SHARE_DECLINED = 'remote_share_declined'; const SUBJECT_REMOTE_SHARE_RECEIVED = 'remote_share_received'; @@ -48,8 +46,7 @@ class RemoteShares extends Base { IContactsManager $contactsManager, ICloudIdManager $cloudIdManager ) { - parent::__construct($languageFactory, $url, $activityManager, $userManager, $contactsManager); - $this->cloudIdManager = $cloudIdManager; + parent::__construct($languageFactory, $url, $activityManager, $userManager, $cloudIdManager, $contactsManager); } /** @@ -123,7 +120,7 @@ class RemoteShares extends Base { 'id' => $parameters[1], 'name' => $parameters[1], ], - 'user' => $this->getFederatedUser($parameters[0]), + 'user' => $this->getUser($parameters[0]), ]; case self::SUBJECT_REMOTE_SHARE_ACCEPTED: case self::SUBJECT_REMOTE_SHARE_DECLINED: @@ -133,23 +130,9 @@ class RemoteShares extends Base { } return [ 'file' => $this->getFile($fileParameter), - 'user' => $this->getFederatedUser($parameters[0]), + 'user' => $this->getUser($parameters[0]), ]; } throw new \InvalidArgumentException(); } - - /** - * @param string $cloudId - * @return array - */ - protected function getFederatedUser($cloudId) { - $remoteUser = $this->cloudIdManager->resolveCloudId($cloudId); - return [ - 'type' => 'user', - 'id' => $remoteUser->getUser(), - 'name' => $this->getDisplayNameForContact($remoteUser->getDisplayId()), - 'server' => $remoteUser->getRemote(), - ]; - } } -- 2.39.5