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

Move users_picker profile custom picker to contacts #4231

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion REUSE.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ SPDX-FileCopyrightText = "2024 Nextcloud GmbH and Nextcloud contributors"
SPDX-License-Identifier = "AGPL-3.0-or-later"

[[annotations]]
path = ["img/address-book.svg", "img/circles.svg", "img/clone.svg", "img/eye.svg", "img/language.svg", "img/phone.svg", "img/qrcode.svg", "img/recent-actors.svg", "img/social.svg", "img/sync.svg", "img/up.svg", "img/contacts.svg", "img/app.svg", "img/group.svg"]
path = ["img/address-book.svg", "img/circles.svg", "img/clone.svg", "img/eye.svg", "img/language.svg", "img/phone.svg", "img/qrcode.svg", "img/recent-actors.svg", "img/social.svg", "img/sync.svg", "img/up.svg", "img/contacts.svg", "img/app.svg", "img/group.svg", "img/profile.svg", "img/profile-dark.svg"]
precedence = "aggregate"
SPDX-FileCopyrightText = "2018-2024 Google LLC"
SPDX-License-Identifier = "Apache-2.0"
Expand Down
1 change: 1 addition & 0 deletions img/profile-dark.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions img/profile.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@

use OCA\Contacts\Dav\PatchPlugin;
use OCA\Contacts\Listener\LoadContactsFilesActions;
use OCA\Contacts\Listener\ProfilePickerReferenceListener;
use OCA\Contacts\Reference\ProfilePickerReferenceProvider;
use OCA\Files\Event\LoadAdditionalScriptsEvent;
use OCP\AppFramework\App;
use OCP\AppFramework\Bootstrap\IBootContext;
use OCP\AppFramework\Bootstrap\IBootstrap;
use OCP\AppFramework\Bootstrap\IRegistrationContext;
use OCP\Collaboration\Reference\RenderReferenceEvent;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\SabrePluginEvent;

Expand All @@ -28,6 +31,9 @@

public function register(IRegistrationContext $context): void {
$context->registerEventListener(LoadAdditionalScriptsEvent::class, LoadContactsFilesActions::class);

$context->registerEventListener(RenderReferenceEvent::class, ProfilePickerReferenceListener::class);
$context->registerReferenceProvider(ProfilePickerReferenceProvider::class);

Check warning on line 36 in lib/AppInfo/Application.php

View check run for this annotation

Codecov / codecov/patch

lib/AppInfo/Application.php#L35-L36

Added lines #L35 - L36 were not covered by tests
}

public function boot(IBootContext $context): void {
Expand Down
25 changes: 25 additions & 0 deletions lib/Listener/ProfilePickerReferenceListener.php
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;

Check warning on line 20 in lib/Listener/ProfilePickerReferenceListener.php

View check run for this annotation

Codecov / codecov/patch

lib/Listener/ProfilePickerReferenceListener.php#L18-L20

Added lines #L18 - L20 were not covered by tests
}

Util::addScript(Application::APP_ID, Application::APP_ID . '-reference');

Check warning on line 23 in lib/Listener/ProfilePickerReferenceListener.php

View check run for this annotation

Codecov / codecov/patch

lib/Listener/ProfilePickerReferenceListener.php#L23

Added line #L23 was not covered by tests
}
}
180 changes: 180 additions & 0 deletions lib/Reference/ProfilePickerReferenceProvider.php
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;

Check warning on line 54 in lib/Reference/ProfilePickerReferenceProvider.php

View check run for this annotation

Codecov / codecov/patch

lib/Reference/ProfilePickerReferenceProvider.php#L53-L54

Added lines #L53 - L54 were not covered by tests
}

/**
* @inheritDoc
*/
public function getIconUrl(): string {
return $this->urlGenerator->imagePath(Application::APP_ID, 'profile-dark.svg');

Check warning on line 61 in lib/Reference/ProfilePickerReferenceProvider.php

View check run for this annotation

Codecov / codecov/patch

lib/Reference/ProfilePickerReferenceProvider.php#L60-L61

Added lines #L60 - L61 were not covered by tests
}

/**
* @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;

Check warning on line 85 in lib/Reference/ProfilePickerReferenceProvider.php

View check run for this annotation

Codecov / codecov/patch

lib/Reference/ProfilePickerReferenceProvider.php#L85

Added line #L85 was not covered by tests
}
$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) . '...')

Check warning on line 104 in lib/Reference/ProfilePickerReferenceProvider.php

View check run for this annotation

Codecov / codecov/patch

lib/Reference/ProfilePickerReferenceProvider.php#L104

Added line #L104 was not covered by tests
: $fullBio)
: null;

Check warning on line 106 in lib/Reference/ProfilePickerReferenceProvider.php

View check run for this annotation

Codecov / codecov/patch

lib/Reference/ProfilePickerReferenceProvider.php#L106

Added line #L106 was not covered by tests
}
$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;
}
}
Loading
Loading