Skip to content

Commit 23fc538

Browse files
andrey18106julien-nc
authored andcommitted
feat(picker): Move users_picker profile custom picker to contacts
Signed-off-by: Julien Veyssier <[email protected]>
1 parent 430505e commit 23fc538

11 files changed

+957
-0
lines changed

img/LICENSES.md

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Licenses
2+
3+
## profile.svg, profile-dark.svg
4+
5+
* Created by: Google
6+
* License: Apache License version 2.0
7+
* Link: https://pictogrammers.com/library/mdi/icon/account/
8+
*

img/profile-dark.svg

+1
Loading

img/profile.svg

+1
Loading

lib/AppInfo/Application.php

+6
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,14 @@
77

88
use OCA\Contacts\Dav\PatchPlugin;
99
use OCA\Contacts\Listener\LoadContactsFilesActions;
10+
use OCA\Contacts\Listener\ProfilePickerReferenceListener;
11+
use OCA\Contacts\Reference\ProfilePickerReferenceProvider;
1012
use OCA\Files\Event\LoadAdditionalScriptsEvent;
1113
use OCP\AppFramework\App;
1214
use OCP\AppFramework\Bootstrap\IBootContext;
1315
use OCP\AppFramework\Bootstrap\IBootstrap;
1416
use OCP\AppFramework\Bootstrap\IRegistrationContext;
17+
use OCP\Collaboration\Reference\RenderReferenceEvent;
1518
use OCP\EventDispatcher\IEventDispatcher;
1619
use OCP\SabrePluginEvent;
1720

@@ -28,6 +31,9 @@ public function __construct() {
2831

2932
public function register(IRegistrationContext $context): void {
3033
$context->registerEventListener(LoadAdditionalScriptsEvent::class, LoadContactsFilesActions::class);
34+
35+
$context->registerEventListener(RenderReferenceEvent::class, ProfilePickerReferenceListener::class);
36+
$context->registerReferenceProvider(ProfilePickerReferenceProvider::class);
3137
}
3238

3339
public function boot(IBootContext $context): void {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* @copyright Copyright (c) 2023 Andrey Borysenko <[email protected]>
7+
*
8+
* @author 2023 Andrey Borysenko <[email protected]>
9+
*
10+
* @license AGPL-3.0-or-later
11+
*
12+
* This program is free software: you can redistribute it and/or modify
13+
* it under the terms of the GNU Affero General Public License as
14+
* published by the Free Software Foundation, either version 3 of the
15+
* License, or (at your option) any later version.
16+
*
17+
* This program is distributed in the hope that it will be useful,
18+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
19+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20+
* GNU Affero General Public License for more details.
21+
*
22+
* You should have received a copy of the GNU Affero General Public License
23+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
24+
*
25+
*/
26+
27+
namespace OCA\Contacts\Listener;
28+
29+
use OCA\Contacts\AppInfo\Application;
30+
use OCP\Collaboration\Reference\RenderReferenceEvent;
31+
use OCP\EventDispatcher\Event;
32+
use OCP\EventDispatcher\IEventListener;
33+
use OCP\Util;
34+
35+
class ProfilePickerReferenceListener implements IEventListener {
36+
public function handle(Event $event): void {
37+
if (!$event instanceof RenderReferenceEvent) {
38+
return;
39+
}
40+
41+
Util::addScript(Application::APP_ID, Application::APP_ID . '-reference');
42+
}
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* @copyright Copyright (c) 2023 Andrey Borysenko <[email protected]>
7+
*
8+
* @author 2023 Andrey Borysenko <[email protected]>
9+
*
10+
* @license AGPL-3.0-or-later
11+
*
12+
* This program is free software: you can redistribute it and/or modify
13+
* it under the terms of the GNU Affero General Public License as
14+
* published by the Free Software Foundation, either version 3 of the
15+
* License, or (at your option) any later version.
16+
*
17+
* This program is distributed in the hope that it will be useful,
18+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
19+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20+
* GNU Affero General Public License for more details.
21+
*
22+
* You should have received a copy of the GNU Affero General Public License
23+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
24+
*
25+
*/
26+
27+
namespace OCA\Contacts\Reference;
28+
29+
use OCA\Contacts\AppInfo\Application;
30+
use OCP\Accounts\IAccountManager;
31+
32+
use OCP\Collaboration\Reference\ADiscoverableReferenceProvider;
33+
use OCP\Collaboration\Reference\IReference;
34+
use OCP\Collaboration\Reference\Reference;
35+
36+
use OCP\IL10N;
37+
use OCP\IURLGenerator;
38+
use OCP\IUserManager;
39+
40+
class ProfilePickerReferenceProvider extends ADiscoverableReferenceProvider {
41+
public const RICH_OBJECT_TYPE = 'users_picker_profile';
42+
43+
public function __construct(
44+
private IL10N $l10n,
45+
private IURLGenerator $urlGenerator,
46+
private IUserManager $userManager,
47+
private IAccountManager $accountManager,
48+
private ?string $userId,
49+
) {
50+
}
51+
52+
/**
53+
* @inheritDoc
54+
*/
55+
public function getId(): string {
56+
return 'profile_picker';
57+
}
58+
59+
/**
60+
* @inheritDoc
61+
*/
62+
public function getTitle(): string {
63+
return $this->l10n->t('Profile picker');
64+
}
65+
66+
/**
67+
* @inheritDoc
68+
*/
69+
public function getOrder(): int {
70+
return 10;
71+
}
72+
73+
/**
74+
* @inheritDoc
75+
*/
76+
public function getIconUrl(): string {
77+
return $this->urlGenerator->imagePath(Application::APP_ID, 'profile-dark.svg');
78+
}
79+
80+
/**
81+
* @inheritDoc
82+
*/
83+
public function matchReference(string $referenceText): bool {
84+
return $this->getObjectId($referenceText) !== null;
85+
}
86+
87+
/**
88+
* @inheritDoc
89+
*/
90+
public function resolveReference(string $referenceText): ?IReference {
91+
if (!$this->matchReference($referenceText)) {
92+
return null;
93+
}
94+
95+
$userId = $this->getObjectId($referenceText);
96+
$user = $this->userManager->get($userId);
97+
if ($user === null) {
98+
return null;
99+
}
100+
$account = $this->accountManager->getAccount($user);
101+
$profileEnabled = $account->getProperty(IAccountManager::PROPERTY_PROFILE_ENABLED)->getValue() === '1';
102+
if (!$profileEnabled) {
103+
return null;
104+
}
105+
106+
$reference = new Reference($referenceText);
107+
108+
$userDisplayName = $user->getDisplayName();
109+
$userEmail = $user->getEMailAddress();
110+
$userAvatarUrl = $this->urlGenerator->linkToRouteAbsolute('core.avatar.getAvatar', ['userId' => $userId, 'size' => '64']);
111+
112+
$bio = $account->getProperty(IAccountManager::PROPERTY_BIOGRAPHY);
113+
$bio = $bio->getScope() !== IAccountManager::SCOPE_PRIVATE ? $bio->getValue() : null;
114+
$headline = $account->getProperty(IAccountManager::PROPERTY_HEADLINE);
115+
$location = $account->getProperty(IAccountManager::PROPERTY_ADDRESS);
116+
$website = $account->getProperty(IAccountManager::PROPERTY_WEBSITE);
117+
$organisation = $account->getProperty(IAccountManager::PROPERTY_ORGANISATION);
118+
$role = $account->getProperty(IAccountManager::PROPERTY_ROLE);
119+
120+
// for clients who can't render the reference widgets
121+
$reference->setTitle($userDisplayName);
122+
$reference->setDescription($userEmail ?? $userDisplayName);
123+
$reference->setImageUrl($userAvatarUrl);
124+
125+
// for the Vue reference widget
126+
$reference->setRichObject(
127+
self::RICH_OBJECT_TYPE,
128+
[
129+
'user_id' => $userId,
130+
'title' => $userDisplayName,
131+
'subline' => $userEmail ?? $userDisplayName,
132+
'email' => $userEmail,
133+
'bio' => isset($bio) && $bio !== ''
134+
? (mb_strlen($bio) > 80
135+
? (mb_substr($bio, 0, 80) . '...')
136+
: $bio)
137+
: null,
138+
'full_bio' => $bio,
139+
'headline' => $headline->getScope() !== IAccountManager::SCOPE_PRIVATE ? $headline->getValue() : null,
140+
'location' => $location->getScope() !== IAccountManager::SCOPE_PRIVATE ? $location->getValue() : null,
141+
'location_url' => $location->getScope() !== IAccountManager::SCOPE_PRIVATE ? $this->getOpenStreetLocationUrl($location->getValue()) : null,
142+
'website' => $website->getScope() !== IAccountManager::SCOPE_PRIVATE ? $website->getValue() : null,
143+
'organisation' => $organisation->getScope() !== IAccountManager::SCOPE_PRIVATE ? $organisation->getValue() : null,
144+
'role' => $role->getScope() !== IAccountManager::SCOPE_PRIVATE ? $role->getValue() : null,
145+
'url' => $referenceText,
146+
]
147+
);
148+
return $reference;
149+
}
150+
151+
public function getObjectId(string $url): ?string {
152+
$baseUrl = $this->urlGenerator->getBaseUrl();
153+
$baseWithIndex = $baseUrl . '/index.php';
154+
155+
preg_match('/^' . preg_quote($baseUrl, '/') . '\/u\/(\w+)$/', $url, $matches);
156+
if (count($matches) > 1) {
157+
return $matches[1];
158+
}
159+
preg_match('/^' . preg_quote($baseWithIndex, '/') . '\/u\/(\w+)$/', $url, $matches);
160+
if (count($matches) > 1) {
161+
return $matches[1];
162+
}
163+
164+
return null;
165+
}
166+
167+
public function getOpenStreetLocationUrl($location): string {
168+
return 'https://www.openstreetmap.org/search?query=' . urlencode($location);
169+
}
170+
171+
/**
172+
* @inheritDoc
173+
*/
174+
public function getCachePrefix(string $referenceId): string {
175+
return $this->userId ?? '';
176+
}
177+
178+
/**
179+
* @inheritDoc
180+
*/
181+
public function getCacheKey(string $referenceId): ?string {
182+
$objectId = $this->getObjectId($referenceId);
183+
if ($objectId !== null) {
184+
return $objectId;
185+
}
186+
return $referenceId;
187+
}
188+
}

0 commit comments

Comments
 (0)