Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add default avatars for one-to-one conversations
Now the avatar of the other user will be returned when getting the
avatar of a one-to-one conversation.

Signed-off-by: Daniel Calviño Sánchez <[email protected]>
  • Loading branch information
danxuliu committed Jan 4, 2021
commit 81836c1d8810c7aea95c301fbe40de1164ed565d
13 changes: 12 additions & 1 deletion lib/Avatar/RoomAvatar.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,20 @@ class RoomAvatar implements IAvatar {
/** @var LoggerInterface */
private $logger;

/** @var Util */
private $util;

public function __construct(
ISimpleFolder $folder,
Room $room,
IL10N $l,
LoggerInterface $logger) {
LoggerInterface $logger,
Util $util) {
$this->folder = $folder;
$this->room = $room;
$this->l = $l;
$this->logger = $logger;
$this->util = $util;
}

public function getRoom(): Room {
Expand Down Expand Up @@ -227,6 +232,12 @@ public function remove(): void {
public function getFile($size) {
$size = (int) $size;

if ($this->room->getType() === Room::ONE_TO_ONE_CALL) {
$userAvatar = $this->util->getUserAvatarForOtherParticipant($this->room);

return $userAvatar->getFile($size);
}

$extension = $this->getExtension();

if ($size === -1) {
Expand Down
2 changes: 1 addition & 1 deletion lib/Avatar/RoomAvatarProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public function getAvatar(string $id): IAvatar {
$folder = $this->appData->newFolder('avatar/' . $id);
}

return new RoomAvatar($folder, $room, $this->l, $this->logger);
return new RoomAvatar($folder, $room, $this->l, $this->logger, $this->util);
}

/**
Expand Down
45 changes: 44 additions & 1 deletion lib/Avatar/Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@
use OCA\Talk\Manager;
use OCA\Talk\Participant;
use OCA\Talk\Room;
use OCA\Talk\Service\ParticipantService;
use OCA\Talk\TalkSession;
use OCP\IAvatar;
use OCP\IAvatarManager;

class Util {

Expand All @@ -40,21 +43,33 @@ class Util {
/** @var TalkSession */
protected $session;

/** @var IAvatarManager */
private $avatarManager;

/** @var Manager */
private $manager;

/** @var ParticipantService */
private $participantService;

/**
* @param string|null $userId
* @param TalkSession $session
* @param IAvatarManager $avatarManager
* @param Manager $manager
* @param ParticipantService $participantService
*/
public function __construct(
?string $userId,
TalkSession $session,
Manager $manager) {
IAvatarManager $avatarManager,
Manager $manager,
ParticipantService $participantService) {
$this->userId = $userId;
$this->session = $session;
$this->avatarManager = $avatarManager;
$this->manager = $manager;
$this->participantService = $participantService;
}

/**
Expand All @@ -80,4 +95,32 @@ public function getCurrentParticipant(Room $room): Participant {
public function isRoomListableByUser(Room $room): bool {
return $this->manager->isRoomListableByUser($room, $this->userId);
}

/**
* @param Room $room
* @return IAvatar
* @throws \InvalidArgumentException if the given room is not a one-to-one
* room, the current participant is not a member of the room or
* there is no other participant in the room
*/
public function getUserAvatarForOtherParticipant(Room $room): IAvatar {
if ($room->getType() !== Room::ONE_TO_ONE_CALL) {
throw new \InvalidArgumentException('Not a one-to-one room');
}

$userIds = $this->participantService->getParticipantUserIds($room);
if (array_search($this->userId, $userIds) === false) {
throw new \InvalidArgumentException('Current participant is not a member of the room');
}
if (count($userIds) < 2) {
throw new \InvalidArgumentException('No other participant in the room');
}

$otherParticipantUserId = $userIds[0];
if ($otherParticipantUserId === $this->userId) {
$otherParticipantUserId = $userIds[1];
}

return $this->avatarManager->getAvatar($otherParticipantUserId);
}
}
14 changes: 14 additions & 0 deletions tests/integration/features/bootstrap/AvatarTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,20 @@ public function userDeletesAvatarForRoomWith(string $user, string $identifier, s
$this->assertStatusCode($this->response, $statusCode);
}

/**
* @Then last avatar is a default avatar of size :size
*
* @param string size
*/
public function lastAvatarIsADefaultAvatarOfSize(string $size) {
$this->theFollowingHeadersShouldBeSet(new TableNode([
[ 'Content-Type', 'image/png' ],
[ 'X-NC-IsCustomAvatar', '0' ]
]));
$this->lastAvatarIsASquareOfSize($size);
$this->lastAvatarIsNotASingleColor();
}

/**
* @Then last avatar is a custom avatar of size :size and color :color
*
Expand Down
6 changes: 4 additions & 2 deletions tests/integration/features/conversation/avatar.feature
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ Feature: avatar
And user "moderator" sets avatar for room "one-to-one room" from file "data/green-square-256.png" with 404
And user "not invited user" sets avatar for room "one-to-one room" from file "data/green-square-256.png" with 404
And user "guest" sets avatar for room "one-to-one room" from file "data/green-square-256.png" with 404
Then user "owner" gets avatar for room "one-to-one room" with size "256" with 404
And user "moderator" gets avatar for room "one-to-one room" with size "256" with 404
Then user "owner" gets avatar for room "one-to-one room"
And last avatar is a default avatar of size "128"
And user "moderator" gets avatar for room "one-to-one room"
And last avatar is a default avatar of size "128"



Expand Down