-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProfileFieldDirectorySearchService.php
More file actions
151 lines (125 loc) · 4.28 KB
/
ProfileFieldDirectorySearchService.php
File metadata and controls
151 lines (125 loc) · 4.28 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
<?php
/**
* SPDX-FileCopyrightText: 2026 LibreCode coop and LibreCode contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
declare(strict_types=1);
namespace OCA\ProfileFields\Search;
use InvalidArgumentException;
use OCA\ProfileFields\Db\FieldValue;
use OCA\ProfileFields\Db\FieldValueMapper;
use OCA\ProfileFields\Enum\FieldExposurePolicy;
use OCA\ProfileFields\Enum\FieldVisibility;
use OCA\ProfileFields\Service\FieldDefinitionService;
use OCP\IGroupManager;
use OCP\IUser;
use OCP\IUserManager;
class ProfileFieldDirectorySearchService {
private const MAX_MATCHES_PER_USER = 3;
public function __construct(
private FieldDefinitionService $fieldDefinitionService,
private FieldValueMapper $fieldValueMapper,
private IUserManager $userManager,
private IGroupManager $groupManager,
) {
}
/**
* @return array{total: int, items: list<array{
* user_uid: string,
* display_name: string,
* matched_fields: list<array{
* field_key: string,
* field_label: string,
* value: string
* }>
* }>}
*/
public function search(?IUser $actor, string $term, int $limit, int $offset): array {
if ($limit < 1) {
throw new InvalidArgumentException('limit must be greater than 0');
}
if ($offset < 0) {
throw new InvalidArgumentException('offset must be greater than or equal to 0');
}
$normalizedTerm = trim(mb_strtolower($term));
if ($normalizedTerm === '') {
return ['total' => 0, 'items' => []];
}
$actorUid = $actor?->getUID();
$actorIsAdmin = $actorUid !== null && $this->groupManager->isAdmin($actorUid);
$definitionsById = [];
foreach ($this->fieldDefinitionService->findActiveOrdered() as $definition) {
$definitionsById[$definition->getId()] = $definition;
}
if ($definitionsById === []) {
return ['total' => 0, 'items' => []];
}
$matchesByUserUid = [];
foreach ($this->fieldValueMapper->findAllOrdered() as $value) {
$definition = $definitionsById[$value->getFieldDefinitionId()] ?? null;
if ($definition === null) {
continue;
}
if (!$this->isSearchableForActor(FieldExposurePolicy::from($definition->getExposurePolicy()), $value->getCurrentVisibility(), $actorIsAdmin, $actorUid !== null)) {
continue;
}
$scalarValue = $this->extractScalarValue($value);
if ($scalarValue === null || !str_contains(mb_strtolower($scalarValue), $normalizedTerm)) {
continue;
}
$userUid = $value->getUserUid();
if (!isset($matchesByUserUid[$userUid])) {
$user = $this->userManager->get($userUid);
$matchesByUserUid[$userUid] = [
'user_uid' => $userUid,
'display_name' => $this->resolveDisplayName($user, $userUid),
'matched_fields' => [],
];
}
if (count($matchesByUserUid[$userUid]['matched_fields']) >= self::MAX_MATCHES_PER_USER) {
continue;
}
$matchesByUserUid[$userUid]['matched_fields'][] = [
'field_key' => $definition->getFieldKey(),
'field_label' => $definition->getLabel(),
'value' => $scalarValue,
];
}
$matches = array_values($matchesByUserUid);
usort($matches, static function (array $left, array $right): int {
return [$left['display_name'], $left['user_uid']] <=> [$right['display_name'], $right['user_uid']];
});
return [
'total' => count($matches),
'items' => array_slice($matches, $offset, $limit),
];
}
private function extractScalarValue(FieldValue $value): ?string {
$decoded = json_decode($value->getValueJson(), true);
$scalar = $decoded['value'] ?? null;
if (is_array($scalar) || is_object($scalar) || $scalar === null) {
return null;
}
return trim((string)$scalar);
}
private function isSearchableForActor(FieldExposurePolicy $exposurePolicy, string $currentVisibility, bool $actorIsAdmin, bool $actorIsAuthenticated): bool {
if ($actorIsAdmin) {
return true;
}
if (!$exposurePolicy->isUserVisible()) {
return false;
}
return match (FieldVisibility::from($currentVisibility)) {
FieldVisibility::PUBLIC => true,
FieldVisibility::USERS => $actorIsAuthenticated,
FieldVisibility::PRIVATE => false,
};
}
private function resolveDisplayName(?IUser $user, string $fallbackUserUid): string {
if ($user === null) {
return $fallbackUserUid;
}
$displayName = trim($user->getDisplayName());
return $displayName !== '' ? $displayName : $fallbackUserUid;
}
}