Skip to content

Commit

Permalink
Merge 4520655 into a1e4511
Browse files Browse the repository at this point in the history
  • Loading branch information
andrey18106 authored Jul 5, 2023
2 parents a1e4511 + 4520655 commit 5ca4d6c
Show file tree
Hide file tree
Showing 9 changed files with 652 additions and 0 deletions.
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 @@ -24,11 +24,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 @@ -45,6 +48,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);

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

View check run for this annotation

Codecov / codecov/patch

lib/AppInfo/Application.php#L52-L53

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

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;

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

View check run for this annotation

Codecov / codecov/patch

lib/Listener/ProfilePickerReferenceListener.php#L36-L38

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

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

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

View check run for this annotation

Codecov / codecov/patch

lib/Listener/ProfilePickerReferenceListener.php#L41

Added line #L41 was not covered by tests
}
}
188 changes: 188 additions & 0 deletions lib/Reference/ProfilePickerReferenceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
<?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 {
private const RICH_OBJECT_TYPE = 'users_picker_profile';
private ?string $userId;
private IL10N $l10n;
private IURLGenerator $urlGenerator;
private IUserManager $userManager;
private IAccountManager $accountManager;

public function __construct(

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

View check run for this annotation

Codecov / codecov/patch

lib/Reference/ProfilePickerReferenceProvider.php#L49

Added line #L49 was not covered by tests
IL10N $l10n,
IURLGenerator $urlGenerator,
IUserManager $userManager,
IAccountManager $accountManager,
?string $userId
) {
$this->userId = $userId;
$this->l10n = $l10n;
$this->urlGenerator = $urlGenerator;
$this->userManager = $userManager;
$this->accountManager = $accountManager;

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

View check run for this annotation

Codecov / codecov/patch

lib/Reference/ProfilePickerReferenceProvider.php#L56-L60

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

/**
* @inheritDoc
*/
public function getId(): string {
return 'profile_picker';

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

View check run for this annotation

Codecov / codecov/patch

lib/Reference/ProfilePickerReferenceProvider.php#L66-L67

Added lines #L66 - L67 were not covered by tests
}

/**
* @inheritDoc
*/
public function getTitle(): string {
return $this->l10n->t('Profile picker');

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

View check run for this annotation

Codecov / codecov/patch

lib/Reference/ProfilePickerReferenceProvider.php#L73-L74

Added lines #L73 - L74 were not covered by tests
}

/**
* @inheritDoc
*/
public function getOrder(): int {
return 10;

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

View check run for this annotation

Codecov / codecov/patch

lib/Reference/ProfilePickerReferenceProvider.php#L80-L81

Added lines #L80 - L81 were not covered by tests
}

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

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

View check run for this annotation

Codecov / codecov/patch

lib/Reference/ProfilePickerReferenceProvider.php#L87-L88

Added lines #L87 - L88 were not covered by tests
}

/**
* @inheritDoc
*/
public function matchReference(string $referenceText): bool {
return $this->getObjectId($referenceText) !== null;

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

View check run for this annotation

Codecov / codecov/patch

lib/Reference/ProfilePickerReferenceProvider.php#L94-L95

Added lines #L94 - L95 were not covered by tests
}

/**
* @inheritDoc
*/
public function resolveReference(string $referenceText): ?IReference {
if (!$this->matchReference($referenceText)) {
return null;

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

View check run for this annotation

Codecov / codecov/patch

lib/Reference/ProfilePickerReferenceProvider.php#L101-L103

Added lines #L101 - L103 were not covered by tests
}

$userId = $this->getObjectId($referenceText);
$user = $this->userManager->get($userId);
if ($user !== null) {
$reference = new Reference($referenceText);

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

View check run for this annotation

Codecov / codecov/patch

lib/Reference/ProfilePickerReferenceProvider.php#L106-L109

Added lines #L106 - L109 were not covered by tests

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

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

View check run for this annotation

Codecov / codecov/patch

lib/Reference/ProfilePickerReferenceProvider.php#L111-L113

Added lines #L111 - L113 were not covered by tests

$bio = $this->accountManager->getAccount($user)->getProperty(IAccountManager::PROPERTY_BIOGRAPHY);
$bio = $bio->getScope() !== IAccountManager::SCOPE_PRIVATE ? $bio->getValue() : null;
$headline = $this->accountManager->getAccount($user)->getProperty(IAccountManager::PROPERTY_HEADLINE);
$location = $this->accountManager->getAccount($user)->getProperty(IAccountManager::PROPERTY_ADDRESS);
$website = $this->accountManager->getAccount($user)->getProperty(IAccountManager::PROPERTY_WEBSITE);
$organisation = $this->accountManager->getAccount($user)->getProperty(IAccountManager::PROPERTY_ORGANISATION);
$role = $this->accountManager->getAccount($user)->getProperty(IAccountManager::PROPERTY_ROLE);

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

View check run for this annotation

Codecov / codecov/patch

lib/Reference/ProfilePickerReferenceProvider.php#L115-L121

Added lines #L115 - L121 were not covered by tests

// for clients who can't render the reference widgets
$reference->setTitle($userDisplayName);
$reference->setDescription($userEmail ?? $userDisplayName);
$reference->setImageUrl($userAvatarUrl);

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

View check run for this annotation

Codecov / codecov/patch

lib/Reference/ProfilePickerReferenceProvider.php#L124-L126

Added lines #L124 - L126 were not covered by tests

// for the Vue reference widget
$reference->setRichObject(
self::RICH_OBJECT_TYPE,

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

View check run for this annotation

Codecov / codecov/patch

lib/Reference/ProfilePickerReferenceProvider.php#L129-L130

Added lines #L129 - L130 were not covered by tests
[
'user_id' => $userId,
'title' => $userDisplayName,
'subline' => $userEmail ?? $userDisplayName,
'email' => $userEmail,
'bio' => isset($bio) && $bio !== '' ? substr_replace($bio, '...', 80, strlen($bio)) : null,
'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,

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

View check run for this annotation

Codecov / codecov/patch

lib/Reference/ProfilePickerReferenceProvider.php#L132-L143

Added lines #L132 - L143 were not covered by tests
]
);
return $reference;

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

View check run for this annotation

Codecov / codecov/patch

lib/Reference/ProfilePickerReferenceProvider.php#L146

Added line #L146 was not covered by tests
}
return null;

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

View check run for this annotation

Codecov / codecov/patch

lib/Reference/ProfilePickerReferenceProvider.php#L148

Added line #L148 was not covered by tests
}

private function getObjectId(string $url): ?string {
$baseUrl = $this->urlGenerator->getBaseUrl();
$baseWithIndex = $baseUrl . '/index.php';

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

View check run for this annotation

Codecov / codecov/patch

lib/Reference/ProfilePickerReferenceProvider.php#L151-L153

Added lines #L151 - L153 were not covered by tests

preg_match('/^' . preg_quote($baseUrl, '/') . '\/u\/(\w+)$/', $url, $matches);
if (count($matches) > 1) {
return $matches[1];

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

View check run for this annotation

Codecov / codecov/patch

lib/Reference/ProfilePickerReferenceProvider.php#L155-L157

Added lines #L155 - L157 were not covered by tests
}
preg_match('/^' . preg_quote($baseWithIndex, '/') . '\/u\/(\w+)$/', $url, $matches);
if (count($matches) > 1) {
return $matches[1];

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

View check run for this annotation

Codecov / codecov/patch

lib/Reference/ProfilePickerReferenceProvider.php#L159-L161

Added lines #L159 - L161 were not covered by tests
}

return null;

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

View check run for this annotation

Codecov / codecov/patch

lib/Reference/ProfilePickerReferenceProvider.php#L164

Added line #L164 was not covered by tests
}

private function getOpenStreetLocationUrl($location): string {
return 'https://www.openstreetmap.org/search?query=' . urlencode($location);

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

View check run for this annotation

Codecov / codecov/patch

lib/Reference/ProfilePickerReferenceProvider.php#L167-L168

Added lines #L167 - L168 were not covered by tests
}

/**
* @inheritDoc
*/
public function getCachePrefix(string $referenceId): string {
return $this->userId ?? '';

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

View check run for this annotation

Codecov / codecov/patch

lib/Reference/ProfilePickerReferenceProvider.php#L174-L175

Added lines #L174 - L175 were not covered by tests
}

/**
* @inheritDoc
*/
public function getCacheKey(string $referenceId): ?string {
$objectId = $this->getObjectId($referenceId);
if ($objectId !== null) {
return $objectId;

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

View check run for this annotation

Codecov / codecov/patch

lib/Reference/ProfilePickerReferenceProvider.php#L181-L184

Added lines #L181 - L184 were not covered by tests
}
return $referenceId;

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

View check run for this annotation

Codecov / codecov/patch

lib/Reference/ProfilePickerReferenceProvider.php#L186

Added line #L186 was not covered by tests
}
}
Loading

0 comments on commit 5ca4d6c

Please sign in to comment.