Skip to content

Commit

Permalink
Update avatars on update
Browse files Browse the repository at this point in the history
Signed-off-by: Carl Schwan <carl@carlschwan.eu>
  • Loading branch information
CarlSchwan committed Sep 9, 2022
1 parent f3ec1d3 commit aea3d11
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 27 deletions.
2 changes: 1 addition & 1 deletion core/Controller/AvatarController.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public function getAvatarDark(string $userId, int $size) {
*
* @return JSONResponse|FileDisplayResponse
*/
public function getAvatar(string $userId, int $size, bool $darkTheme = false) {
public function getAvatar(string $userId, int $size) {
if ($size <= 64) {
if ($size !== 64) {
$this->logger->debug('Avatar requested in deprecated size ' . $size);
Expand Down
6 changes: 3 additions & 3 deletions core/Controller/GuestAvatarController.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ public function __construct(
* @param string $size The desired avatar size, e.g. 64 for 64x64px
* @return FileDisplayResponse|Http\Response
*/
public function getAvatar(string $guestName, string $size, ?bool $dark = false) {
public function getAvatar(string $guestName, string $size, ?bool $darkTheme = false) {
$size = (int) $size;
$dark = $dark === null ? false : $dark;
$darkTheme = $darkTheme ?? false;

if ($size <= 64) {
if ($size !== 64) {
Expand All @@ -78,7 +78,7 @@ public function getAvatar(string $guestName, string $size, ?bool $dark = false)

try {
$avatar = $this->avatarManager->getGuestAvatar($guestName);
$avatarFile = $avatar->getFile($size);
$avatarFile = $avatar->getFile($size, $darkTheme);

$resp = new FileDisplayResponse(
$avatarFile,
Expand Down
4 changes: 2 additions & 2 deletions dist/user_status-menu.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/user_status-menu.js.map

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions lib/private/Avatar/Avatar.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,10 @@ public function get(int $size = 64, bool $darkTheme = false) {
* @return string
*
*/
protected function getAvatarVector(int $size, bool $dark): string {
protected function getAvatarVector(int $size, bool $darkTheme): string {
$userDisplayName = $this->getDisplayName();
$fgRGB = $this->avatarBackgroundColor($userDisplayName);
$bgRGB = $fgRGB->alphaBlending(0.1, $dark ? new Color(0, 0, 0) : new Color(255, 255, 255));
$bgRGB = $fgRGB->alphaBlending(0.1, $darkTheme ? new Color(0, 0, 0) : new Color(255, 255, 255));
$fill = sprintf("%02x%02x%02x", $bgRGB->red(), $bgRGB->green(), $bgRGB->blue());
$fgFill = sprintf("%02x%02x%02x", $fgRGB->red(), $fgRGB->green(), $fgRGB->blue());
$text = $this->getAvatarText();
Expand All @@ -125,13 +125,13 @@ protected function getAvatarVector(int $size, bool $dark): string {
/**
* Generate png avatar from svg with Imagick
*/
protected function generateAvatarFromSvg(int $size, bool $dark): ?string {
protected function generateAvatarFromSvg(int $size, bool $darkTheme): ?string {
if (!extension_loaded('imagick')) {
return null;
}
try {
$font = __DIR__ . '/../../../core/fonts/NotoSans-Regular.ttf';
$svg = $this->getAvatarVector($size, $dark);
$svg = $this->getAvatarVector($size, $darkTheme);
$avatar = new Imagick();
$avatar->setFont($font);
$avatar->readImageBlob($svg);
Expand All @@ -147,10 +147,10 @@ protected function generateAvatarFromSvg(int $size, bool $dark): ?string {
/**
* Generate png avatar with GD
*/
protected function generateAvatar(string $userDisplayName, int $size, bool $dark): string {
protected function generateAvatar(string $userDisplayName, int $size, bool $darkTheme): string {
$text = $this->getAvatarText();
$textColor = $this->avatarBackgroundColor($userDisplayName);
$backgroundColor = $textColor->alphaBlending(0.1, $dark ? new Color(0, 0, 0) : new Color(255, 255, 255));
$backgroundColor = $textColor->alphaBlending(0.1, $darkTheme ? new Color(0, 0, 0) : new Color(255, 255, 255));

$im = imagecreatetruecolor($size, $size);
$background = imagecolorallocate(
Expand Down
20 changes: 7 additions & 13 deletions lib/private/Repair/ClearGeneratedAvatarCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,35 +30,29 @@
use OCP\Migration\IRepairStep;

class ClearGeneratedAvatarCache implements IRepairStep {

/** @var AvatarManager */
protected $avatarManager;

/** @var IConfig */
private $config;
protected AvatarManager $avatarManager;
private IConfig $config;

public function __construct(IConfig $config, AvatarManager $avatarManager) {
$this->config = $config;
$this->avatarManager = $avatarManager;
}

public function getName() {
public function getName(): string {
return 'Clear every generated avatar on major updates';
}

/**
* Check if this repair step should run
*
* @return boolean
*/
private function shouldRun() {
private function shouldRun(): bool {
$versionFromBeforeUpdate = $this->config->getSystemValue('version', '0.0.0.0');

// was added to 15.0.0.4
return version_compare($versionFromBeforeUpdate, '15.0.0.4', '<=');
// was added to 25.0.0.10
return version_compare($versionFromBeforeUpdate, '25.0.0.10', '<=');
}

public function run(IOutput $output) {
public function run(IOutput $output): void {
if ($this->shouldRun()) {
try {
$this->avatarManager->clearCachedAvatars();
Expand Down
2 changes: 1 addition & 1 deletion version.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
// between betas, final and RCs. This is _not_ the public version number. Reset minor/patchlevel
// when updating major/minor version number.

$OC_Version = [25, 0, 0, 9];
$OC_Version = [25, 0, 0, 10];

// The human readable string
$OC_VersionString = '25.0.0 beta 5';
Expand Down

0 comments on commit aea3d11

Please sign in to comment.