-
-
Notifications
You must be signed in to change notification settings - Fork 5.1k
Expand file tree
/
Copy pathProfilePickerReferenceProvider.php
More file actions
200 lines (174 loc) · 5.78 KB
/
Copy pathProfilePickerReferenceProvider.php
File metadata and controls
200 lines (174 loc) · 5.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Profile\Reference;
use OCA\Profile\AppInfo\Application;
use OCP\Accounts\IAccountManager;
use OCP\Collaboration\Reference\ADiscoverableReferenceProvider;
use OCP\Collaboration\Reference\IReference;
use OCP\Collaboration\Reference\Reference;
use OCP\IAppConfig;
use OCP\IL10N;
use OCP\IURLGenerator;
use OCP\IUserManager;
use OCP\Profile\IProfileManager;
class ProfilePickerReferenceProvider extends ADiscoverableReferenceProvider {
public const RICH_OBJECT_TYPE = 'profile_widget';
private bool $enabled;
public function __construct(
private IL10N $l10n,
private IURLGenerator $urlGenerator,
private IUserManager $userManager,
private IAccountManager $accountManager,
private IProfileManager $profileManager,
private IAppConfig $appConfig,
private ?string $userId,
) {
$profileEnabledGlobally = $this->profileManager->isProfileEnabled();
$profilePickerEnabled = filter_var(
$this->appConfig->getValueString('settings', 'profile_picker_enabled', '1'),
FILTER_VALIDATE_BOOLEAN,
FILTER_NULL_ON_FAILURE,
);
$this->enabled = $profileEnabledGlobally && $profilePickerEnabled;
}
/**
* @inheritDoc
*/
#[\Override]
public function getId(): string {
return 'profile_picker';
}
/**
* @inheritDoc
*/
#[\Override]
public function getTitle(): string {
return $this->l10n->t('Profile picker');
}
/**
* @inheritDoc
*/
#[\Override]
public function getOrder(): int {
return 10;
}
/**
* @inheritDoc
*/
#[\Override]
public function getIconUrl(): string {
return $this->urlGenerator->imagePath(Application::APP_ID, 'app-dark.svg');
}
/**
* @inheritDoc
*/
#[\Override]
public function matchReference(string $referenceText): bool {
if (!$this->enabled) {
return false;
}
return $this->getObjectId($referenceText) !== null;
}
/**
* @inheritDoc
*/
#[\Override]
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
*/
#[\Override]
public function getCachePrefix(string $referenceId): string {
return $this->userId ?? '';
}
/**
* @inheritDoc
*/
#[\Override]
public function getCacheKey(string $referenceId): ?string {
$objectId = $this->getObjectId($referenceId);
if ($objectId !== null) {
return $objectId;
}
return $referenceId;
}
}