Skip to content

Commit

Permalink
fix(api): Properly typed avatar update
Browse files Browse the repository at this point in the history
Signed-off-by: Joas Schilling <[email protected]>
  • Loading branch information
nickvergessen authored and backportbot[bot] committed Sep 11, 2024
1 parent 97ff05c commit 17cc7bc
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 7 deletions.
29 changes: 29 additions & 0 deletions lib/Exceptions/RoomProperty/AvatarException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\Talk\Exceptions\RoomProperty;

class AvatarException extends \InvalidArgumentException {
public const REASON_TYPE = 'type';

/**
* @param self::REASON_* $reason
*/
public function __construct(
protected string $reason,
) {
parent::__construct($reason);
}

/**
* @return self::REASON_*
*/
public function getReason(): string {
return $this->reason;
}
}
17 changes: 10 additions & 7 deletions lib/Service/RoomService.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use OCA\Talk\Events\RoomPasswordVerifyEvent;
use OCA\Talk\Events\RoomSyncedEvent;
use OCA\Talk\Exceptions\RoomNotFoundException;
use OCA\Talk\Exceptions\RoomProperty\AvatarException;
use OCA\Talk\Exceptions\RoomProperty\BreakoutRoomModeException;
use OCA\Talk\Exceptions\RoomProperty\BreakoutRoomStatusException;
use OCA\Talk\Exceptions\RoomProperty\CallRecordingException;
Expand Down Expand Up @@ -420,9 +421,12 @@ public function setLobby(Room $room, int $newState, ?\DateTime $dateTime, bool $
}
}

public function setAvatar(Room $room, string $avatar): bool {
/**
* @throws AvatarException
*/
public function setAvatar(Room $room, string $avatar): void {
if ($room->getType() === Room::TYPE_ONE_TO_ONE || $room->getType() === Room::TYPE_ONE_TO_ONE_FORMER) {
return false;
throw new AvatarException(AvatarException::REASON_TYPE);
}

$oldAvatar = $room->getAvatar();
Expand All @@ -439,7 +443,6 @@ public function setAvatar(Room $room, string $avatar): bool {

$event = new RoomModifiedEvent($room, ARoomModifiedEvent::PROPERTY_AVATAR, $avatar, $oldAvatar);
$this->dispatcher->dispatchTyped($event);
return true;
}

/**
Expand Down Expand Up @@ -1079,11 +1082,11 @@ public function syncPropertiesFromHostRoom(Room $local, array $host): void {
// Add a fake suffix as we explode by the dot in the AvatarService, but the version doesn't have one.
$hostAvatar .= '.fed';
}
$success = $this->setAvatar($local, $hostAvatar);
if (!$success) {
$this->logger->error('An error occurred while trying to sync avatarVersion of ' . $local->getId() . ' to ' . $host['avatarVersion']);
} else {
try {
$this->setAvatar($local, $hostAvatar);
$changed[] = ARoomModifiedEvent::PROPERTY_AVATAR;
} catch (AvatarException $e) {
$this->logger->error('An error (' . $e->getReason() . ') occurred while trying to sync avatarVersion of ' . $local->getId() . ' to ' . $host['avatarVersion'], ['exception' => $e]);
}
}
if (isset($host['lastActivity']) && $host['lastActivity'] !== 0 && $host['lastActivity'] !== ((int)$local->getLastActivity()?->getTimestamp())) {
Expand Down

0 comments on commit 17cc7bc

Please sign in to comment.