From 2609b30df0694f7d03e886c1782df54d4c9b01ac Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Wed, 24 Jan 2018 13:54:49 +0100 Subject: [PATCH] Fix group displaynames in activity Signed-off-by: Joas Schilling --- .../dav/lib/CalDAV/Activity/Provider/Base.php | 67 +++++++++++++------ .../lib/CalDAV/Activity/Provider/Calendar.php | 6 +- .../lib/CalDAV/Activity/Provider/Event.php | 6 +- .../lib/Activity/Providers/Groups.php | 64 +++++++++++++++--- 4 files changed, 108 insertions(+), 35 deletions(-) diff --git a/apps/dav/lib/CalDAV/Activity/Provider/Base.php b/apps/dav/lib/CalDAV/Activity/Provider/Base.php index b6d8a5be73..99ad903f24 100644 --- a/apps/dav/lib/CalDAV/Activity/Provider/Base.php +++ b/apps/dav/lib/CalDAV/Activity/Provider/Base.php @@ -26,6 +26,8 @@ namespace OCA\DAV\CalDAV\Activity\Provider; use OCA\DAV\CalDAV\CalDavBackend; use OCP\Activity\IEvent; use OCP\Activity\IProvider; +use OCP\IGroup; +use OCP\IGroupManager; use OCP\IL10N; use OCP\IUser; use OCP\IUserManager; @@ -35,14 +37,22 @@ abstract class Base implements IProvider { /** @var IUserManager */ protected $userManager; - /** @var string[] cached displayNames - key is the UID and value the displayname */ - protected $displayNames = []; + /** @var string[] */ + protected $userDisplayNames = []; + + /** @var IGroupManager */ + protected $groupManager; + + /** @var string[] */ + protected $groupDisplayNames = []; /** * @param IUserManager $userManager + * @param IGroupManager $groupManager */ - public function __construct(IUserManager $userManager) { + public function __construct(IUserManager $userManager, IGroupManager $groupManager) { $this->userManager = $userManager; + $this->groupManager = $groupManager; } /** @@ -112,31 +122,19 @@ abstract class Base implements IProvider { ]; } - /** - * @param string $id - * @return array - */ - protected function generateGroupParameter($id) { - return [ - 'type' => 'group', - 'id' => $id, - 'name' => $id, - ]; - } - /** * @param string $uid * @return array */ protected function generateUserParameter($uid) { - if (!isset($this->displayNames[$uid])) { - $this->displayNames[$uid] = $this->getDisplayName($uid); + if (!isset($this->userDisplayNames[$uid])) { + $this->userDisplayNames[$uid] = $this->getUserDisplayName($uid); } return [ 'type' => 'user', 'id' => $uid, - 'name' => $this->displayNames[$uid], + 'name' => $this->userDisplayNames[$uid], ]; } @@ -144,12 +142,39 @@ abstract class Base implements IProvider { * @param string $uid * @return string */ - protected function getDisplayName($uid) { + protected function getUserDisplayName($uid) { $user = $this->userManager->get($uid); if ($user instanceof IUser) { return $user->getDisplayName(); - } else { - return $uid; } + return $uid; + } + + /** + * @param string $gid + * @return array + */ + protected function generateGroupParameter($gid) { + if (!isset($this->groupDisplayNames[$gid])) { + $this->groupDisplayNames[$gid] = $this->getGroupDisplayName($gid); + } + + return [ + 'type' => 'group', + 'id' => $gid, + 'name' => $this->groupDisplayNames[$gid], + ]; + } + + /** + * @param string $gid + * @return string + */ + protected function getGroupDisplayName($gid) { + $group = $this->groupManager->get($gid); + if ($group instanceof IGroup) { + return $group->getDisplayName(); + } + return $gid; } } diff --git a/apps/dav/lib/CalDAV/Activity/Provider/Calendar.php b/apps/dav/lib/CalDAV/Activity/Provider/Calendar.php index ff12914428..db79b0f665 100644 --- a/apps/dav/lib/CalDAV/Activity/Provider/Calendar.php +++ b/apps/dav/lib/CalDAV/Activity/Provider/Calendar.php @@ -26,6 +26,7 @@ namespace OCA\DAV\CalDAV\Activity\Provider; use OCP\Activity\IEvent; use OCP\Activity\IEventMerger; use OCP\Activity\IManager; +use OCP\IGroupManager; use OCP\IL10N; use OCP\IURLGenerator; use OCP\IUserManager; @@ -63,10 +64,11 @@ class Calendar extends Base { * @param IURLGenerator $url * @param IManager $activityManager * @param IUserManager $userManager + * @param IGroupManager $groupManager * @param IEventMerger $eventMerger */ - public function __construct(IFactory $languageFactory, IURLGenerator $url, IManager $activityManager, IUserManager $userManager, IEventMerger $eventMerger) { - parent::__construct($userManager); + public function __construct(IFactory $languageFactory, IURLGenerator $url, IManager $activityManager, IUserManager $userManager, IGroupManager $groupManager, IEventMerger $eventMerger) { + parent::__construct($userManager, $groupManager); $this->languageFactory = $languageFactory; $this->url = $url; $this->activityManager = $activityManager; diff --git a/apps/dav/lib/CalDAV/Activity/Provider/Event.php b/apps/dav/lib/CalDAV/Activity/Provider/Event.php index eabd2e517c..f13cb0c266 100644 --- a/apps/dav/lib/CalDAV/Activity/Provider/Event.php +++ b/apps/dav/lib/CalDAV/Activity/Provider/Event.php @@ -26,6 +26,7 @@ namespace OCA\DAV\CalDAV\Activity\Provider; use OCP\Activity\IEvent; use OCP\Activity\IEventMerger; use OCP\Activity\IManager; +use OCP\IGroupManager; use OCP\IL10N; use OCP\IURLGenerator; use OCP\IUserManager; @@ -57,10 +58,11 @@ class Event extends Base { * @param IURLGenerator $url * @param IManager $activityManager * @param IUserManager $userManager + * @param IGroupManager $groupManager * @param IEventMerger $eventMerger */ - public function __construct(IFactory $languageFactory, IURLGenerator $url, IManager $activityManager, IUserManager $userManager, IEventMerger $eventMerger) { - parent::__construct($userManager); + public function __construct(IFactory $languageFactory, IURLGenerator $url, IManager $activityManager, IUserManager $userManager, IGroupManager $groupManager, IEventMerger $eventMerger) { + parent::__construct($userManager, $groupManager); $this->languageFactory = $languageFactory; $this->url = $url; $this->activityManager = $activityManager; diff --git a/apps/files_sharing/lib/Activity/Providers/Groups.php b/apps/files_sharing/lib/Activity/Providers/Groups.php index 53262e1931..9a8f7164c5 100644 --- a/apps/files_sharing/lib/Activity/Providers/Groups.php +++ b/apps/files_sharing/lib/Activity/Providers/Groups.php @@ -24,6 +24,12 @@ namespace OCA\Files_Sharing\Activity\Providers; use OCP\Activity\IEvent; +use OCP\Activity\IManager; +use OCP\IGroup; +use OCP\IGroupManager; +use OCP\IURLGenerator; +use OCP\IUserManager; +use OCP\L10N\IFactory; class Groups extends Base { @@ -32,6 +38,24 @@ class Groups extends Base { const SUBJECT_UNSHARED_GROUP_SELF = 'unshared_group_self'; const SUBJECT_UNSHARED_GROUP_BY = 'unshared_group_by'; + /** @var IGroupManager */ + protected $groupManager; + + /** @var string[] */ + protected $groupDisplayNames = []; + + /** + * @param IFactory $languageFactory + * @param IURLGenerator $url + * @param IManager $activityManager + * @param IUserManager $userManager + * @param IGroupManager $groupManager + */ + public function __construct(IFactory $languageFactory, IURLGenerator $url, IManager $activityManager, IUserManager $userManager, IGroupManager $groupManager) { + parent::__construct($languageFactory, $url, $activityManager, $userManager); + $this->groupManager = $groupManager; + } + /** * @param IEvent $event * @return IEvent @@ -103,24 +127,44 @@ class Groups extends Base { case self::SUBJECT_UNSHARED_GROUP_BY: return [ 'file' => $this->getFile($parameters[0], $event), - 'group' => [ - 'type' => 'group', - 'id' => $parameters[2], - 'name' => $parameters[2], - ], + 'group' => $this->generateGroupParameter($parameters[2]), 'actor' => $this->getUser($parameters[1]), ]; case self::SUBJECT_SHARED_GROUP_SELF: case self::SUBJECT_UNSHARED_GROUP_SELF: return [ 'file' => $this->getFile($parameters[0], $event), - 'group' => [ - 'type' => 'group', - 'id' => $parameters[1], - 'name' => $parameters[1], - ], + 'group' => $this->generateGroupParameter($parameters[1]), ]; } return []; } + + /** + * @param string $gid + * @return array + */ + protected function generateGroupParameter($gid) { + if (!isset($this->groupDisplayNames[$gid])) { + $this->groupDisplayNames[$gid] = $this->getGroupDisplayName($gid); + } + + return [ + 'type' => 'group', + 'id' => $gid, + 'name' => $this->groupDisplayNames[$gid], + ]; + } + + /** + * @param string $gid + * @return string + */ + protected function getGroupDisplayName($gid) { + $group = $this->groupManager->get($gid); + if ($group instanceof IGroup) { + return $group->getDisplayName(); + } + return $gid; + } }