diff options
author | Joas Schilling <coding@schilljs.com> | 2020-01-28 14:51:43 +0300 |
---|---|---|
committer | Roeland Jago Douma <roeland@famdouma.nl> | 2020-03-10 21:36:57 +0300 |
commit | f4918f208e4ef09d197c00e8eeab6963cd55b0a8 (patch) | |
tree | cf56db69da468375a82bf5f40bc9e6bcfe483359 /apps/files_sharing/lib | |
parent | bbef026aa075f4432210fcc8f089283f56f2bf72 (diff) |
Unify the code a bit
Signed-off-by: Joas Schilling <coding@schilljs.com>
Diffstat (limited to 'apps/files_sharing/lib')
-rw-r--r-- | apps/files_sharing/lib/Activity/Providers/Base.php | 48 | ||||
-rw-r--r-- | apps/files_sharing/lib/Activity/Providers/Groups.php | 4 | ||||
-rw-r--r-- | apps/files_sharing/lib/Activity/Providers/RemoteShares.php | 23 |
3 files changed, 38 insertions, 37 deletions
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(), - ]; - } } |