1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* @copyright Copyright 2022 Carl Schwan <[email protected]> |
7
|
|
|
* @license AGPL-3.0-or-later |
8
|
|
|
* |
9
|
|
|
* This code is free software: you can redistribute it and/or modify |
10
|
|
|
* it under the terms of the GNU Affero General Public License, version 3, |
11
|
|
|
* as published by the Free Software Foundation. |
12
|
|
|
* |
13
|
|
|
* This program is distributed in the hope that it will be useful, |
14
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
15
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16
|
|
|
* GNU Affero General Public License for more details. |
17
|
|
|
* |
18
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3, |
19
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/> |
20
|
|
|
* |
21
|
|
|
*/ |
22
|
|
|
|
23
|
|
|
|
24
|
|
|
namespace OC\User; |
25
|
|
|
|
26
|
|
|
use OCP\EventDispatcher\Event; |
27
|
|
|
use OCP\EventDispatcher\IEventListener; |
28
|
|
|
use OCP\ICache; |
29
|
|
|
use OCP\ICacheFactory; |
30
|
|
|
use OCP\IUserManager; |
31
|
|
|
use OCP\User\Events\UserChangedEvent; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Class that cache the relation UserId -> Display name |
35
|
|
|
* |
36
|
|
|
* This saves fetching the user from a user backend and later on fetching |
37
|
|
|
* their preferences. It's generally not an issue if this data is slightly |
38
|
|
|
* outdated. |
39
|
|
|
*/ |
40
|
|
|
class DisplayNameCache implements IEventListener { |
41
|
|
|
private ICache $internalCache; |
42
|
|
|
private IUserManager $userManager; |
43
|
|
|
|
44
|
|
|
public function __construct(ICacheFactory $cacheFactory, IUserManager $userManager) { |
45
|
|
|
$this->internalCache = $cacheFactory->createDistributed('displayNameMappingCache'); |
46
|
|
|
$this->userManager = $userManager; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function getDisplayName(string $userId) { |
50
|
|
|
$displayName = $this->internalCache->get($userId); |
51
|
|
|
if ($displayName) { |
52
|
|
|
return $displayName; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$user = $this->userManager->get($userId); |
56
|
|
|
if ($user) { |
57
|
|
|
$displayName = $user->getDisplayName(); |
58
|
|
|
} else { |
59
|
|
|
$displayName = $userId; |
60
|
|
|
} |
61
|
|
|
$this->internalCache->set($userId, $displayName, 60 * 10); // 10 minutes |
62
|
|
|
|
63
|
|
|
return $displayName; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function clear(): void { |
67
|
|
|
$this->internalCache->clear(); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function handle(Event $event): void { |
71
|
|
|
if ($event instanceof UserChangedEvent && $event->getFeature() === 'displayName') { |
72
|
|
|
$userId = $event->getUser()->getUID(); |
73
|
|
|
$newDisplayName = $event->getValue(); |
74
|
|
|
$this->internalCache->set($userId, $newDisplayName, 60 * 10); // 10 minutes |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|