-
Notifications
You must be signed in to change notification settings - Fork 175
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
Move users_picker profile custom picker to contacts #4231
Open
julien-nc
wants to merge
4
commits into
main
Choose a base branch
from
enh/noid/profile-picker-move
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
23fc538
feat(picker): Move users_picker profile custom picker to contacts
andrey18106 69b73de
chore: fix licenses
julien-nc 4fb561f
fix: profile picker tests
julien-nc a7cc035
feat(profilepicker): check fields visibility in reference provider
julien-nc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
/** | ||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace OCA\Contacts\Listener; | ||
|
||
use OCA\Contacts\AppInfo\Application; | ||
use OCP\Collaboration\Reference\RenderReferenceEvent; | ||
use OCP\EventDispatcher\Event; | ||
use OCP\EventDispatcher\IEventListener; | ||
use OCP\Util; | ||
|
||
class ProfilePickerReferenceListener implements IEventListener { | ||
public function handle(Event $event): void { | ||
if (!$event instanceof RenderReferenceEvent) { | ||
return; | ||
} | ||
|
||
Util::addScript(Application::APP_ID, Application::APP_ID . '-reference'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,180 @@ | ||
<?php | ||
/** | ||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace OCA\Contacts\Reference; | ||
|
||
use OCA\Contacts\AppInfo\Application; | ||
use OCP\Accounts\IAccountManager; | ||
|
||
use OCP\Collaboration\Reference\ADiscoverableReferenceProvider; | ||
use OCP\Collaboration\Reference\IReference; | ||
use OCP\Collaboration\Reference\Reference; | ||
|
||
use OCP\IL10N; | ||
use OCP\IURLGenerator; | ||
use OCP\IUserManager; | ||
use OCP\Profile\IProfileManager; | ||
|
||
class ProfilePickerReferenceProvider extends ADiscoverableReferenceProvider { | ||
public const RICH_OBJECT_TYPE = 'users_picker_profile'; | ||
|
||
public function __construct( | ||
private IL10N $l10n, | ||
private IURLGenerator $urlGenerator, | ||
private IUserManager $userManager, | ||
private IAccountManager $accountManager, | ||
private IProfileManager $profileManager, | ||
private ?string $userId, | ||
) { | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function getId(): string { | ||
return 'profile_picker'; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function getTitle(): string { | ||
return $this->l10n->t('Profile picker'); | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function getOrder(): int { | ||
return 10; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function getIconUrl(): string { | ||
return $this->urlGenerator->imagePath(Application::APP_ID, 'profile-dark.svg'); | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function matchReference(string $referenceText): bool { | ||
return $this->getObjectId($referenceText) !== null; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function resolveReference(string $referenceText): ?IReference { | ||
if (!$this->matchReference($referenceText)) { | ||
return null; | ||
} | ||
|
||
$userId = $this->getObjectId($referenceText); | ||
$user = $this->userManager->get($userId); | ||
if ($user === null) { | ||
return null; | ||
} | ||
if (!$this->profileManager->isProfileEnabled($user)) { | ||
return null; | ||
} | ||
$account = $this->accountManager->getAccount($user); | ||
|
||
$currentUser = $this->userManager->get($this->userId); | ||
|
||
$reference = new Reference($referenceText); | ||
|
||
$userDisplayName = $user->getDisplayName(); | ||
$userEmail = $user->getEMailAddress(); | ||
$userAvatarUrl = $this->urlGenerator->linkToRouteAbsolute('core.avatar.getAvatar', ['userId' => $userId, 'size' => '64']); | ||
|
||
$bioProperty = $account->getProperty(IAccountManager::PROPERTY_BIOGRAPHY); | ||
$bio = null; | ||
$fullBio = null; | ||
if ($this->profileManager->isProfileFieldVisible(IAccountManager::PROPERTY_BIOGRAPHY, $user, $currentUser)) { | ||
$fullBio = $bioProperty->getValue(); | ||
$bio = $fullBio !== '' | ||
? (mb_strlen($fullBio) > 80 | ||
? (mb_substr($fullBio, 0, 80) . '...') | ||
: $fullBio) | ||
: null; | ||
} | ||
$headline = $account->getProperty(IAccountManager::PROPERTY_HEADLINE); | ||
$location = $account->getProperty(IAccountManager::PROPERTY_ADDRESS); | ||
$website = $account->getProperty(IAccountManager::PROPERTY_WEBSITE); | ||
$organisation = $account->getProperty(IAccountManager::PROPERTY_ORGANISATION); | ||
$role = $account->getProperty(IAccountManager::PROPERTY_ROLE); | ||
|
||
// for clients who can't render the reference widgets | ||
$reference->setTitle($userDisplayName); | ||
$reference->setDescription($userEmail ?? $userDisplayName); | ||
$reference->setImageUrl($userAvatarUrl); | ||
|
||
$isLocationVisible = $this->profileManager->isProfileFieldVisible(IAccountManager::PROPERTY_ADDRESS, $user, $currentUser); | ||
|
||
// for the Vue reference widget | ||
$reference->setRichObject( | ||
self::RICH_OBJECT_TYPE, | ||
[ | ||
'user_id' => $userId, | ||
'title' => $userDisplayName, | ||
'subline' => $userEmail ?? $userDisplayName, | ||
'email' => $userEmail, | ||
'bio' => $bio, | ||
'full_bio' => $fullBio, | ||
'headline' => $this->profileManager->isProfileFieldVisible(IAccountManager::PROPERTY_HEADLINE, $user, $currentUser) ? $headline->getValue() : null, | ||
'location' => $isLocationVisible ? $location->getValue() : null, | ||
'location_url' => $isLocationVisible ? $this->getOpenStreetLocationUrl($location->getValue()) : null, | ||
'website' => $this->profileManager->isProfileFieldVisible(IAccountManager::PROPERTY_WEBSITE, $user, $currentUser) ? $website->getValue() : null, | ||
'organisation' => $this->profileManager->isProfileFieldVisible(IAccountManager::PROPERTY_ORGANISATION, $user, $currentUser) ? $organisation->getValue() : null, | ||
'role' => $this->profileManager->isProfileFieldVisible(IAccountManager::PROPERTY_ROLE, $user, $currentUser) ? $role->getValue() : null, | ||
'url' => $referenceText, | ||
] | ||
); | ||
return $reference; | ||
} | ||
|
||
public function getObjectId(string $url): ?string { | ||
$baseUrl = $this->urlGenerator->getBaseUrl(); | ||
$baseWithIndex = $baseUrl . '/index.php'; | ||
|
||
preg_match('/^' . preg_quote($baseUrl, '/') . '\/u\/(\w+)$/', $url, $matches); | ||
if (count($matches) > 1) { | ||
return $matches[1]; | ||
} | ||
preg_match('/^' . preg_quote($baseWithIndex, '/') . '\/u\/(\w+)$/', $url, $matches); | ||
if (count($matches) > 1) { | ||
return $matches[1]; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
public function getOpenStreetLocationUrl($location): string { | ||
return 'https://www.openstreetmap.org/search?query=' . urlencode($location); | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function getCachePrefix(string $referenceId): string { | ||
return $this->userId ?? ''; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function getCacheKey(string $referenceId): ?string { | ||
$objectId = $this->getObjectId($referenceId); | ||
if ($objectId !== null) { | ||
return $objectId; | ||
} | ||
return $referenceId; | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With these new checks it looks like we are only exposing data that should be visible to the active user 👍
Looks good