Skip to content

Commit

Permalink
feat(picker): Move users_picker profile custom picker to contacts
Browse files Browse the repository at this point in the history
Signed-off-by: Julien Veyssier <julien-nc@posteo.net>
  • Loading branch information
andrey18106 authored and julien-nc committed Dec 4, 2024
1 parent 430505e commit cfa86a0
Show file tree
Hide file tree
Showing 11 changed files with 958 additions and 0 deletions.
8 changes: 8 additions & 0 deletions img/LICENSES.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Licenses

## profile.svg, profile-dark.svg

* Created by: Google
* License: Apache License version 2.0
* Link: https://pictogrammers.com/library/mdi/icon/account/
*
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 __construct() {

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

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

public function boot(IBootContext $context): void {
Expand Down
43 changes: 43 additions & 0 deletions lib/Listener/ProfilePickerReferenceListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

/**
* @copyright Copyright (c) 2023 Andrey Borysenko <andrey18106x@gmail.com>
*
* @author 2023 Andrey Borysenko <andrey18106x@gmail.com>
*
* @license AGPL-3.0-or-later
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

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');
}
}
189 changes: 189 additions & 0 deletions lib/Reference/ProfilePickerReferenceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
<?php

declare(strict_types=1);

/**
* @copyright Copyright (c) 2023 Andrey Borysenko <andrey18106x@gmail.com>
*
* @author 2023 Andrey Borysenko <andrey18106x@gmail.com>
*
* @license AGPL-3.0-or-later
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

namespace OCA\Contacts\Reference;


use OCP\Collaboration\Reference\ADiscoverableReferenceProvider;
use OCP\Collaboration\Reference\Reference;

use OCP\Collaboration\Reference\IReference;
use OCP\IL10N;
use OCP\IURLGenerator;

use OCA\Contacts\AppInfo\Application;
use OCP\Accounts\IAccountManager;
use OCP\IUserManager;

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 ?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;
}
$account = $this->accountManager->getAccount($user);
$profileEnabled = $account->getProperty(IAccountManager::PROPERTY_PROFILE_ENABLED)->getValue() === '1';
if (!$profileEnabled) {
return null;
}

$reference = new Reference($referenceText);

$userDisplayName = $user->getDisplayName();
$userEmail = $user->getEMailAddress();
$userAvatarUrl = $this->urlGenerator->linkToRouteAbsolute('core.avatar.getAvatar', ['userId' => $userId, 'size' => '64']);

$bio = $account->getProperty(IAccountManager::PROPERTY_BIOGRAPHY);
$bio = $bio->getScope() !== IAccountManager::SCOPE_PRIVATE ? $bio->getValue() : 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);

// for the Vue reference widget
$reference->setRichObject(
self::RICH_OBJECT_TYPE,
[
'user_id' => $userId,
'title' => $userDisplayName,
'subline' => $userEmail ?? $userDisplayName,
'email' => $userEmail,
'bio' => isset($bio) && $bio !== ''
? (mb_strlen($bio) > 80
? (mb_substr($bio, 0, 80) . '...')
: $bio)
: null,
'full_bio' => $bio,
'headline' => $headline->getScope() !== IAccountManager::SCOPE_PRIVATE ? $headline->getValue() : null,
'location' => $location->getScope() !== IAccountManager::SCOPE_PRIVATE ? $location->getValue() : null,
'location_url' => $location->getScope() !== IAccountManager::SCOPE_PRIVATE ? $this->getOpenStreetLocationUrl($location->getValue()) : null,
'website' => $website->getScope() !== IAccountManager::SCOPE_PRIVATE ? $website->getValue() : null,
'organisation' => $organisation->getScope() !== IAccountManager::SCOPE_PRIVATE ? $organisation->getValue() : null,
'role' => $role->getScope() !== IAccountManager::SCOPE_PRIVATE ? $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

0 comments on commit cfa86a0

Please sign in to comment.