Skip to content

Commit

Permalink
Merge pull request #35915 from nextcloud/enh/noid/display-name-federa…
Browse files Browse the repository at this point in the history
…ted-shares

display displayname on federated shares
  • Loading branch information
blizzz authored Jun 29, 2023
2 parents fc3ac4d + d2efd0e commit 7b7148c
Show file tree
Hide file tree
Showing 8 changed files with 195 additions and 31 deletions.
13 changes: 8 additions & 5 deletions apps/federatedfilesharing/lib/Notifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,9 @@ public function prepare(INotification $notification, string $languageCode): INot
$notification->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));

$params = $notification->getSubjectParameters();
$displayName = (count($params) > 3) ? $params[3] : '';
if ($params[0] !== $params[1] && $params[1] !== null) {
$remoteInitiator = $this->createRemoteUser($params[0]);
$remoteInitiator = $this->createRemoteUser($params[0], $displayName);
$remoteOwner = $this->createRemoteUser($params[1]);
$params[3] = $remoteInitiator['name'] . '@' . $remoteInitiator['server'];
$params[4] = $remoteOwner['name'] . '@' . $remoteOwner['server'];
Expand All @@ -121,7 +122,7 @@ public function prepare(INotification $notification, string $languageCode): INot
]
);
} else {
$remoteOwner = $this->createRemoteUser($params[0]);
$remoteOwner = $this->createRemoteUser($params[0], $displayName);
$params[3] = $remoteOwner['name'] . '@' . $remoteOwner['server'];

$notification->setRichSubject(
Expand Down Expand Up @@ -166,19 +167,21 @@ public function prepare(INotification $notification, string $languageCode): INot

/**
* @param string $cloudId
* @param string $displayName - overwrite display name
*
* @return array
*/
protected function createRemoteUser($cloudId, $displayName = null) {
protected function createRemoteUser(string $cloudId, string $displayName = '') {
try {
$resolvedId = $this->cloudIdManager->resolveCloudId($cloudId);
if ($displayName === null) {
if ($displayName === '') {
$displayName = $this->getDisplayName($resolvedId);
}
$user = $resolvedId->getUser();
$server = $resolvedId->getRemote();
} catch (HintException $e) {
$user = $cloudId;
$displayName = $cloudId;
$displayName = ($displayName !== '') ? $displayName : $cloudId;
$server = '';
}

Expand Down
43 changes: 36 additions & 7 deletions apps/federatedfilesharing/lib/OCM/CloudFederationProviderFiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,13 @@
use OCP\IURLGenerator;
use OCP\IUserManager;
use OCP\Notification\IManager as INotificationManager;
use OCP\Server;
use OCP\Share\Exceptions\ShareNotFound;
use OCP\Share\IManager;
use OCP\Share\IShare;
use OCP\Util;
use Psr\Container\ContainerExceptionInterface;
use Psr\Log\LoggerInterface;

class CloudFederationProviderFiles implements ICloudFederationProvider {

Expand Down Expand Up @@ -250,26 +253,29 @@ public function shareReceived(ICloudFederationShare $share) {
$this->externalShareManager->addShare($remote, $token, '', $name, $owner, $shareType,false, $shareWith, $remoteId);
$shareId = \OC::$server->getDatabaseConnection()->lastInsertId('*PREFIX*share_external');

// get DisplayName about the owner of the share
$ownerDisplayName = $this->getUserDisplayName($ownerFederatedId);

if ($shareType === IShare::TYPE_USER) {
$event = $this->activityManager->generateEvent();
$event->setApp('files_sharing')
->setType('remote_share')
->setSubject(RemoteShares::SUBJECT_REMOTE_SHARE_RECEIVED, [$ownerFederatedId, trim($name, '/')])
->setSubject(RemoteShares::SUBJECT_REMOTE_SHARE_RECEIVED, [$ownerFederatedId, trim($name, '/'), $ownerDisplayName])
->setAffectedUser($shareWith)
->setObject('remote_share', $shareId, $name);
\OC::$server->getActivityManager()->publish($event);
$this->notifyAboutNewShare($shareWith, $shareId, $ownerFederatedId, $sharedByFederatedId, $name);
$this->notifyAboutNewShare($shareWith, $shareId, $ownerFederatedId, $sharedByFederatedId, $name, $ownerDisplayName);
} else {
$groupMembers = $this->groupManager->get($shareWith)->getUsers();
foreach ($groupMembers as $user) {
$event = $this->activityManager->generateEvent();
$event->setApp('files_sharing')
->setType('remote_share')
->setSubject(RemoteShares::SUBJECT_REMOTE_SHARE_RECEIVED, [$ownerFederatedId, trim($name, '/')])
->setSubject(RemoteShares::SUBJECT_REMOTE_SHARE_RECEIVED, [$ownerFederatedId, trim($name, '/'), $ownerDisplayName])
->setAffectedUser($user->getUID())
->setObject('remote_share', $shareId, $name);
\OC::$server->getActivityManager()->publish($event);
$this->notifyAboutNewShare($user->getUID(), $shareId, $ownerFederatedId, $sharedByFederatedId, $name);
$this->notifyAboutNewShare($user->getUID(), $shareId, $ownerFederatedId, $sharedByFederatedId, $name, $ownerDisplayName);
}
}
return $shareId;
Expand Down Expand Up @@ -335,13 +341,13 @@ private function mapShareTypeToNextcloud($shareType) {
return $result;
}

private function notifyAboutNewShare($shareWith, $shareId, $ownerFederatedId, $sharedByFederatedId, $name): void {
private function notifyAboutNewShare($shareWith, $shareId, $ownerFederatedId, $sharedByFederatedId, $name, $displayName): void {
$notification = $this->notificationManager->createNotification();
$notification->setApp('files_sharing')
->setUser($shareWith)
->setDateTime(new \DateTime())
->setObject('remote_share', $shareId)
->setSubject('remote_share', [$ownerFederatedId, $sharedByFederatedId, trim($name, '/')]);
->setSubject('remote_share', [$ownerFederatedId, $sharedByFederatedId, trim($name, '/'), $displayName]);

$declineAction = $notification->createAction();
$declineAction->setLabel('decline')
Expand Down Expand Up @@ -579,6 +585,8 @@ private function unshare($id, array $notification) {
->where($qb->expr()->eq('parent', $qb->createNamedParameter((int)$share['id'])));
$qb->execute();

$ownerDisplayName = $this->getUserDisplayName($owner->getId());

if ((int)$share['share_type'] === IShare::TYPE_USER) {
if ($share['accepted']) {
$path = trim($mountpoint, '/');
Expand All @@ -594,7 +602,7 @@ private function unshare($id, array $notification) {
$event = $this->activityManager->generateEvent();
$event->setApp('files_sharing')
->setType('remote_share')
->setSubject(RemoteShares::SUBJECT_REMOTE_SHARE_UNSHARED, [$owner->getId(), $path])
->setSubject(RemoteShares::SUBJECT_REMOTE_SHARE_UNSHARED, [$owner->getId(), $path, $ownerDisplayName])
->setAffectedUser($user)
->setObject('remote_share', (int)$share['id'], $path);
\OC::$server->getActivityManager()->publish($event);
Expand Down Expand Up @@ -824,4 +832,25 @@ private function isS2SEnabled($incoming = false) {
public function getSupportedShareTypes() {
return ['user', 'group'];
}


public function getUserDisplayName(string $userId): string {
// check if gss is enabled and available
if (!$this->appManager->isInstalled('globalsiteselector')
|| !class_exists('\OCA\GlobalSiteSelector\Service\SlaveService')) {
return '';
}

try {
$slaveService = Server::get(\OCA\GlobalSiteSelector\Service\SlaveService::class);
} catch (\Throwable $e) {
Server::get(LoggerInterface::class)->error(
$e->getMessage(),
['exception' => $e]
);
return '';
}

return $slaveService->getUserDisplayName($this->cloudIdManager->removeProtocolFromUrl($userId), false);
}
}
8 changes: 5 additions & 3 deletions apps/files_sharing/lib/Activity/Providers/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,11 @@ protected function getFile($parameter, IEvent $event = null) {

/**
* @param string $uid
* @param string $overwriteDisplayName - overwrite display name, only if user is not local
*
* @return array
*/
protected function getUser($uid) {
protected function getUser(string $uid, string $overwriteDisplayName = '') {
// First try local user
$displayName = $this->userManager->getDisplayName($uid);
if ($displayName !== null) {
Expand All @@ -176,7 +178,7 @@ protected function getUser($uid) {
return [
'type' => 'user',
'id' => $cloudId->getUser(),
'name' => $this->getDisplayNameFromAddressBook($cloudId->getDisplayId()),
'name' => (($overwriteDisplayName !== '') ? $overwriteDisplayName : $this->getDisplayNameFromAddressBook($cloudId->getDisplayId())),
'server' => $cloudId->getRemote(),
];
}
Expand All @@ -185,7 +187,7 @@ protected function getUser($uid) {
return [
'type' => 'user',
'id' => $uid,
'name' => $uid,
'name' => (($overwriteDisplayName !== '') ? $overwriteDisplayName : $uid),
];
}

Expand Down
3 changes: 2 additions & 1 deletion apps/files_sharing/lib/Activity/Providers/RemoteShares.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,14 @@ protected function getParsedParameters(IEvent $event) {
switch ($subject) {
case self::SUBJECT_REMOTE_SHARE_RECEIVED:
case self::SUBJECT_REMOTE_SHARE_UNSHARED:
$displayName = (count($parameters) > 2) ? $parameters[2] : '';
return [
'file' => [
'type' => 'pending-federated-share',
'id' => $parameters[1],
'name' => $parameters[1],
],
'user' => $this->getUser($parameters[0]),
'user' => $this->getUser($parameters[0], $displayName)
];
case self::SUBJECT_REMOTE_SHARE_ACCEPTED:
case self::SUBJECT_REMOTE_SHARE_DECLINED:
Expand Down
Loading

0 comments on commit 7b7148c

Please sign in to comment.