-
Notifications
You must be signed in to change notification settings - Fork 149
Expand file tree
/
Copy pathSetAvatar.class.php
More file actions
62 lines (52 loc) · 1.99 KB
/
SetAvatar.class.php
File metadata and controls
62 lines (52 loc) · 1.99 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
<?php
namespace wcf\command\user;
use wcf\data\file\File;
use wcf\data\file\FileAction;
use wcf\data\user\User;
use wcf\data\user\UserEditor;
use wcf\event\user\AvatarChanged;
use wcf\system\cache\runtime\UserProfileRuntimeCache;
use wcf\system\event\EventHandler;
use wcf\system\user\group\assignment\UserGroupAssignmentHandler;
use wcf\system\user\storage\UserStorageHandler;
use wcf\system\user\UserProfileHandler;
use wcf\system\WCF;
/**
* Sets the avatar of a user.
*
* @author Olaf Braun
* @copyright 2001-2024 WoltLab GmbH
* @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
* @since 6.2
*/
final class SetAvatar
{
public function __construct(
private readonly User $user,
private readonly ?File $file = null
) {}
public function __invoke(): void
{
if ($this->file === null && $this->user->avatarFileID !== null) {
(new FileAction([$this->user->avatarFileID], 'delete'))->executeAction();
}
$pathname = null;
if ($this->file !== null) {
$filename = $this->file->getSourceFilenameWebp() ?? $this->file->getSourceFilename();
$pathname = $this->file->getRelativePath() . $filename;
}
(new UserEditor($this->user))->update([
'avatarFileID' => $this->file?->fileID,
'avatarPathname' => $pathname,
'avatarID' => null,
]);
UserStorageHandler::getInstance()->reset([$this->user->userID], 'avatar');
UserProfileRuntimeCache::getInstance()->removeObject($this->user->userID);
// Setting an avatar could satisfy the condition to assign a user to user groups.
UserGroupAssignmentHandler::getInstance()->checkUsers([$this->user->userID]);
if ($this->user->userID === WCF::getUser()->userID) {
UserProfileHandler::getInstance()->reloadUserProfile();
}
EventHandler::getInstance()->fire(new AvatarChanged($this->user, $this->file));
}
}