Dark theme for guest avatar

And better caching policy

Signed-off-by: Carl Schwan <carl@carlschwan.eu>
pull/33752/head
Carl Schwan 3 years ago
parent f98ae2b5b0
commit 76d0165330
No known key found for this signature in database
GPG Key ID: C3AA6B3A5EFA7AC5
  1. 13
      core/Controller/GuestAvatarController.php
  2. 1
      core/routes.php
  3. 2
      lib/private/Avatar/Avatar.php
  4. 4
      lib/private/Avatar/GuestAvatar.php
  5. 10
      lib/private/Avatar/PlaceholderAvatar.php

@ -60,8 +60,9 @@ class GuestAvatarController extends Controller {
* @param string $size The desired avatar size, e.g. 64 for 64x64px
* @return FileDisplayResponse|Http\Response
*/
public function getAvatar(string $guestName, string $size) {
public function getAvatar(string $guestName, string $size, ?bool $dark = false) {
$size = (int) $size;
$dark = $dark === null ? false : $dark;
if ($size <= 64) {
if ($size !== 64) {
@ -94,7 +95,15 @@ class GuestAvatarController extends Controller {
}
// Cache for 30 minutes
$resp->cacheFor(1800);
$resp->cacheFor(1800, false, true);
return $resp;
}
/**
* @PublicPage
* @NoCSRFRequired
*/
public function getAvatarDark(string $guestName, string $size) {
return $this->getAvatar($guestName, $size, true);
}
}

@ -51,6 +51,7 @@ $application->registerRoutes($this, [
['name' => 'avatar#postCroppedAvatar', 'url' => '/avatar/cropped', 'verb' => 'POST'],
['name' => 'avatar#getTmpAvatar', 'url' => '/avatar/tmp', 'verb' => 'GET'],
['name' => 'avatar#postAvatar', 'url' => '/avatar/', 'verb' => 'POST'],
['name' => 'GuestAvatar#getAvatarDark', 'url' => '/avatar/guest/{guestName}/{size}/dark', 'verb' => 'GET'],
['name' => 'GuestAvatar#getAvatar', 'url' => '/avatar/guest/{guestName}/{size}', 'verb' => 'GET'],
['name' => 'CSRFToken#index', 'url' => '/csrftoken', 'verb' => 'GET'],
['name' => 'login#tryLogin', 'url' => '/login', 'verb' => 'POST'],

@ -150,7 +150,7 @@ abstract class Avatar implements IAvatar {
protected function generateAvatar(string $userDisplayName, int $size, bool $dark): string {
$text = $this->getAvatarText();
$textColor = $this->avatarBackgroundColor($userDisplayName);
$backgroundColor = $textColor->alphaBlending(0.1, $dark ? new Color() : new Color(255, 255, 255));
$backgroundColor = $textColor->alphaBlending(0.1, $dark ? new Color(0, 0, 0) : new Color(255, 255, 255));
$im = imagecreatetruecolor($size, $size);
$background = imagecolorallocate(

@ -84,8 +84,8 @@ class GuestAvatar extends Avatar {
/**
* Generates an avatar for the guest.
*/
public function getFile(int $size): ISimpleFile {
$avatar = $this->generateAvatar($this->userDisplayName, $size);
public function getFile(int $size, bool $darkTheme = false): ISimpleFile {
$avatar = $this->generateAvatar($this->userDisplayName, $size, $darkTheme);
return new InMemoryFile('avatar.png', $avatar);
}

@ -108,13 +108,13 @@ class PlaceholderAvatar extends Avatar {
* @throws \OCP\Files\NotPermittedException
* @throws \OCP\PreConditionNotMetException
*/
public function getFile(int $size): ISimpleFile {
public function getFile(int $size, bool $darkTheme = false): ISimpleFile {
$ext = 'png';
if ($size === -1) {
$path = 'avatar-placeholder.' . $ext;
$path = 'avatar-placeholder' . ($darkTheme ? '-dark' : '') . '.' . $ext;
} else {
$path = 'avatar-placeholder.' . $size . '.' . $ext;
$path = 'avatar-placeholder' . ($darkTheme ? '-dark' : '') . '.' . $size . '.' . $ext;
}
try {
@ -124,8 +124,8 @@ class PlaceholderAvatar extends Avatar {
throw new NotFoundException;
}
if (!$data = $this->generateAvatarFromSvg($size)) {
$data = $this->generateAvatar($this->getDisplayName(), $size);
if (!$data = $this->generateAvatarFromSvg($size, $darkTheme)) {
$data = $this->generateAvatar($this->getDisplayName(), $size, $darkTheme);
}
try {

Loading…
Cancel
Save