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
Update one-to-one conversation avatars when the user avatar changes
The image itself is always got directly from the user avatar, so it is
always up to date. However, as the version is specific to the room
avatar and stored in the database it needs to be explicitly bumped.

Signed-off-by: Daniel Calviño Sánchez <[email protected]>
  • Loading branch information
danxuliu committed Jan 4, 2021
commit a878ce2ae2f29f3220e051c19d109ca9a9831a3e
2 changes: 2 additions & 0 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

use OCA\Files_Sharing\Event\BeforeTemplateRenderedEvent;
use OCA\Talk\Activity\Listener as ActivityListener;
use OCA\Talk\Avatar\Listener as AvatarListener;
use OCA\Talk\Avatar\RoomAvatarProvider;
use OCA\Talk\Capabilities;
use OCA\Talk\Chat\Changelog\Listener as ChangelogListener;
Expand Down Expand Up @@ -134,6 +135,7 @@ public function boot(IBootContext $context): void {
ChangelogListener::register($dispatcher);
ShareListener::register($dispatcher);
Operation::register($dispatcher);
AvatarListener::register($dispatcher);

$this->registerRoomActivityHooks($dispatcher);
$this->registerChatHooks($dispatcher);
Expand Down
84 changes: 84 additions & 0 deletions lib/Avatar/Listener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

declare(strict_types=1);

/**
* @copyright Copyright (c) 2020, Daniel Calviño Sánchez ([email protected])
*
* @author Daniel Calviño Sánchez <[email protected]>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

namespace OCA\Talk\Avatar;

use OCA\Talk\Manager;
use OCA\Talk\Room;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\IUser;
use Symfony\Component\EventDispatcher\GenericEvent;

class Listener {

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

/**
* @param Manager $manager
*/
public function __construct(
Manager $manager) {
$this->manager = $manager;
}

public static function register(IEventDispatcher $dispatcher): void {
$listener = static function (GenericEvent $event) {
if ($event->getArgument('feature') !== 'avatar') {
return;
}

/** @var self $listener */
$listener = \OC::$server->query(self::class);
$listener->updateRoomAvatarsFromChangedUserAvatar($event->getSubject());
};
$dispatcher->addListener(IUser::class . '::changeUser', $listener);
}

/**
* Updates the associated room avatars from the changed user avatar
*
* The avatar versions of all the one-to-one conversations of that user are
* bumped.
*
* Note that the avatar seen by the user who has changed her avatar will not
* change, as she will get the avatar of the other user, but even if the
* avatar images are independent the avatar version is a shared value and
* needs to be bumped for both.
*
* @param IUser $user the user whose avatar changed
*/
public function updateRoomAvatarsFromChangedUserAvatar(IUser $user): void {
$rooms = $this->manager->getRoomsForUser($user->getUID());
foreach ($rooms as $room) {
if ($room->getType() !== Room::ONE_TO_ONE_CALL) {
continue;
}

$room->setAvatar($room->getAvatarId(), $room->getAvatarVersion() + 1);
}
}
}
43 changes: 43 additions & 0 deletions tests/integration/features/bootstrap/AvatarTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,49 @@ public function userDeletesAvatarForRoomWith(string $user, string $identifier, s
$this->assertStatusCode($this->response, $statusCode);
}

/**
* @When logged in user posts temporary avatar from file :source
*
* @param string $source
*/
public function loggedInUserPostsTemporaryAvatarFromFile(string $source) {
$file = \GuzzleHttp\Psr7\stream_for(fopen($source, 'r'));

$this->sendingToWithRequestToken('POST', '/index.php/avatar',
[
'multipart' => [
[
'name' => 'files[]',
'contents' => $file
]
]
]);
$this->assertStatusCode($this->response, '200');
}

/**
* @When logged in user crops temporary avatar
*
* @param TableNode $crop
*/
public function loggedInUserCropsTemporaryAvatar(TableNode $crop) {
$parameters = [];
foreach ($crop->getRowsHash() as $key => $value) {
$parameters[] = 'crop[' . $key . ']=' . $value;
}

$this->sendingToWithRequestToken('POST', '/index.php/avatar/cropped?' . implode('&', $parameters));
$this->assertStatusCode($this->response, '200');
}

/**
* @When logged in user deletes the user avatar
*/
public function loggedInUserDeletesTheUserAvatar() {
$this->sendingToWithRequesttoken('DELETE', '/index.php/avatar');
$this->assertStatusCode($this->response, '200');
}

/**
* @Then last avatar is a default avatar of size :size
*
Expand Down
53 changes: 53 additions & 0 deletions tests/integration/features/conversation/avatar.feature
Original file line number Diff line number Diff line change
Expand Up @@ -531,3 +531,56 @@ Feature: avatar
And user "not invited but joined user" is participant of the following rooms (v3)
| avatarId | avatarVersion |
| icon-public | 3 |



Scenario: one-to-one room avatar is updated when user avatar is updated
Given user "owner" creates room "one-to-one room"
| roomType | 1 |
| invite | moderator |
When user "owner" logs in
And logged in user posts temporary avatar from file "data/green-square-256.png"
And logged in user crops temporary avatar
| x | 0 |
| y | 0 |
| w | 256 |
| h | 256 |
Then user "owner" gets avatar for room "one-to-one room" with size "256"
And last avatar is a default avatar of size "256"
And user "moderator" gets avatar for room "one-to-one room" with size "256"
# Although the user avatar is a custom avatar the room avatar is still a
# default avatar.
And the following headers should be set
| Content-Type | image/png |
| X-NC-IsCustomAvatar | 0 |
And last avatar is a square of size "256"
And last avatar is a single "#00FF00" color
And user "owner" is participant of the following rooms (v3)
| avatarId | avatarVersion |
| user | 2 |
And user "moderator" is participant of the following rooms (v3)
| avatarId | avatarVersion |
| user | 2 |

Scenario: one-to-one room avatar is updated when user avatar is deleted
Given user "owner" creates room "one-to-one room"
| roomType | 1 |
| invite | moderator |
And user "owner" logs in
And logged in user posts temporary avatar from file "data/green-square-256.png"
And logged in user crops temporary avatar
| x | 0 |
| y | 0 |
| w | 256 |
| h | 256 |
When logged in user deletes the user avatar
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"
And user "owner" is participant of the following rooms (v3)
| avatarId | avatarVersion |
| user | 3 |
And user "moderator" is participant of the following rooms (v3)
| avatarId | avatarVersion |
| user | 3 |
5 changes: 5 additions & 0 deletions tests/psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
<code>getSettingsManager</code>
</UndefinedInterfaceMethod>
</file>
<file src="lib/Avatar/Listener.php">
<InvalidArgument occurrences="1">
<code>$listener</code>
</InvalidArgument>
</file>
<file src="lib/BackgroundJob/CheckReferenceIdColumn.php">
<UndefinedClass occurrences="1">
<code>SchemaWrapper</code>
Expand Down