Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[stable18] Use contacts name on federated activities #19879

Merged
merged 3 commits into from
Mar 12, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Unify the code a bit
Signed-off-by: Joas Schilling <coding@schilljs.com>
  • Loading branch information
nickvergessen authored and backportbot[bot] committed Mar 10, 2020
commit 26568fa08b67b39d129c0e2f33c347cfba74e028
52 changes: 36 additions & 16 deletions apps/files/lib/Activity/Provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
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;
Expand Down Expand Up @@ -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;
Expand All @@ -78,13 +82,15 @@ public function __construct(IFactory $languageFactory,
IManager $activityManager,
IUserManager $userManager,
IRootFolder $rootFolder,
ICloudIdManager $cloudIdManager,
IContactsManager $contactsManager,
IEventMerger $eventMerger) {
$this->languageFactory = $languageFactory;
$this->url = $url;
$this->activityManager = $activityManager;
$this->userManager = $userManager;
$this->rootFolder = $rootFolder;
$this->cloudIdManager = $cloudIdManager;
$this->contactsManager = $contactsManager;
$this->eventMerger = $eventMerger;
}
Expand Down Expand Up @@ -479,28 +485,41 @@ protected function getParentEndToEndEncryptionContainer(Folder $userFolder, Node
* @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;
Expand All @@ -512,15 +531,16 @@ protected function getDisplayName($uid) {
$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;
}
}
48 changes: 32 additions & 16 deletions apps/files_sharing/lib/Activity/Providers/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
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;
Expand All @@ -53,18 +54,23 @@ abstract class Base implements IProvider {
/** @var IContactsManager */
protected $contactsManager;

/** @var ICloudIdManager */
protected $cloudIdManager;

/** @var array */
protected $displayNames = [];

public function __construct(IFactory $languageFactory,
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;
}

Expand Down Expand Up @@ -163,31 +169,40 @@ protected function getFile($parameter, IEvent $event = null) {
* @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'])) {
Expand All @@ -203,7 +218,8 @@ protected function getDisplayNameForContact(string $search): string {
$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];
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion apps/files_sharing/lib/Activity/Providers/Groups.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
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;
Expand All @@ -53,9 +54,10 @@ public function __construct(IFactory $languageFactory,
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;
}

Expand Down
23 changes: 3 additions & 20 deletions apps/files_sharing/lib/Activity/Providers/RemoteShares.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@

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';
Expand All @@ -48,8 +46,7 @@ public function __construct(IFactory $languageFactory,
IContactsManager $contactsManager,
ICloudIdManager $cloudIdManager
) {
parent::__construct($languageFactory, $url, $activityManager, $userManager, $contactsManager);
$this->cloudIdManager = $cloudIdManager;
parent::__construct($languageFactory, $url, $activityManager, $userManager, $cloudIdManager, $contactsManager);
}

/**
Expand Down Expand Up @@ -123,7 +120,7 @@ protected function getParsedParameters(IEvent $event) {
'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:
Expand All @@ -133,23 +130,9 @@ protected function getParsedParameters(IEvent $event) {
}
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(),
];
}
}