Skip to content

Commit 6a32033

Browse files
committed
Dark theme for guest avatar
And better caching policy Signed-off-by: Carl Schwan <carl@carlschwan.eu>
1 parent da2f5c4 commit 6a32033

File tree

5 files changed

+20
-10
lines changed

5 files changed

+20
-10
lines changed

core/Controller/GuestAvatarController.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,9 @@ public function __construct(
6060
* @param string $size The desired avatar size, e.g. 64 for 64x64px
6161
* @return FileDisplayResponse|Http\Response
6262
*/
63-
public function getAvatar(string $guestName, string $size) {
63+
public function getAvatar(string $guestName, string $size, ?bool $dark = false) {
6464
$size = (int) $size;
65+
$dark = $dark === null ? false : $dark;
6566

6667
if ($size <= 64) {
6768
if ($size !== 64) {
@@ -94,7 +95,15 @@ public function getAvatar(string $guestName, string $size) {
9495
}
9596

9697
// Cache for 30 minutes
97-
$resp->cacheFor(1800);
98+
$resp->cacheFor(1800, false, true);
9899
return $resp;
99100
}
101+
102+
/**
103+
* @PublicPage
104+
* @NoCSRFRequired
105+
*/
106+
public function getAvatarDark(string $guestName, string $size) {
107+
return $this->getAvatar($guestName, $size, true);
108+
}
100109
}

core/routes.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
['name' => 'avatar#postCroppedAvatar', 'url' => '/avatar/cropped', 'verb' => 'POST'],
5252
['name' => 'avatar#getTmpAvatar', 'url' => '/avatar/tmp', 'verb' => 'GET'],
5353
['name' => 'avatar#postAvatar', 'url' => '/avatar/', 'verb' => 'POST'],
54+
['name' => 'GuestAvatar#getAvatarDark', 'url' => '/avatar/guest/{guestName}/{size}/dark', 'verb' => 'GET'],
5455
['name' => 'GuestAvatar#getAvatar', 'url' => '/avatar/guest/{guestName}/{size}', 'verb' => 'GET'],
5556
['name' => 'CSRFToken#index', 'url' => '/csrftoken', 'verb' => 'GET'],
5657
['name' => 'login#tryLogin', 'url' => '/login', 'verb' => 'POST'],

lib/private/Avatar/Avatar.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ protected function generateAvatarFromSvg(int $size, bool $dark): ?string {
150150
protected function generateAvatar(string $userDisplayName, int $size, bool $dark): string {
151151
$text = $this->getAvatarText();
152152
$textColor = $this->avatarBackgroundColor($userDisplayName);
153-
$backgroundColor = $textColor->alphaBlending(0.1, $dark ? new Color() : new Color(255, 255, 255));
153+
$backgroundColor = $textColor->alphaBlending(0.1, $dark ? new Color(0, 0, 0) : new Color(255, 255, 255));
154154

155155
$im = imagecreatetruecolor($size, $size);
156156
$background = imagecolorallocate(

lib/private/Avatar/GuestAvatar.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,8 @@ public function remove(bool $silent = false): void {
8484
/**
8585
* Generates an avatar for the guest.
8686
*/
87-
public function getFile(int $size): ISimpleFile {
88-
$avatar = $this->generateAvatar($this->userDisplayName, $size);
87+
public function getFile(int $size, bool $darkTheme = false): ISimpleFile {
88+
$avatar = $this->generateAvatar($this->userDisplayName, $size, $darkTheme);
8989
return new InMemoryFile('avatar.png', $avatar);
9090
}
9191

lib/private/Avatar/PlaceholderAvatar.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,13 +108,13 @@ public function remove(bool $silent = false): void {
108108
* @throws \OCP\Files\NotPermittedException
109109
* @throws \OCP\PreConditionNotMetException
110110
*/
111-
public function getFile(int $size): ISimpleFile {
111+
public function getFile(int $size, bool $darkTheme = false): ISimpleFile {
112112
$ext = 'png';
113113

114114
if ($size === -1) {
115-
$path = 'avatar-placeholder.' . $ext;
115+
$path = 'avatar-placeholder' . ($darkTheme ? '-dark' : '') . '.' . $ext;
116116
} else {
117-
$path = 'avatar-placeholder.' . $size . '.' . $ext;
117+
$path = 'avatar-placeholder' . ($darkTheme ? '-dark' : '') . '.' . $size . '.' . $ext;
118118
}
119119

120120
try {
@@ -124,8 +124,8 @@ public function getFile(int $size): ISimpleFile {
124124
throw new NotFoundException;
125125
}
126126

127-
if (!$data = $this->generateAvatarFromSvg($size)) {
128-
$data = $this->generateAvatar($this->getDisplayName(), $size);
127+
if (!$data = $this->generateAvatarFromSvg($size, $darkTheme)) {
128+
$data = $this->generateAvatar($this->getDisplayName(), $size, $darkTheme);
129129
}
130130

131131
try {

0 commit comments

Comments
 (0)