Browse Source

Work with avatar identier stored in room table

Signed-off-by: Vitor Mattos <vitor@php.rio>
pull/8333/head
Vitor Mattos 3 years ago
committed by Joas Schilling
parent
commit
dc10d3b7c3
No known key found for this signature in database GPG Key ID: 74434EFE0D2E2205
  1. 2
      appinfo/info.xml
  2. 4
      lib/Controller/AvatarController.php
  3. 1
      lib/Manager.php
  4. 56
      lib/Migration/Version16000Date20221116163301.php
  5. 1
      lib/Model/SelectHelper.php
  6. 11
      lib/Room.php
  7. 2
      lib/Search/ConversationSearch.php
  8. 42
      lib/Service/AvatarService.php
  9. 11
      lib/Service/RoomService.php
  10. 2
      tests/integration/features/bootstrap/FeatureContext.php
  11. 13
      tests/php/Chat/ChatManagerTest.php
  12. 1
      tests/php/Service/RoomServiceTest.php

2
appinfo/info.xml

@ -16,7 +16,7 @@ And in the works for the [coming versions](https://github.com/nextcloud/spreed/m
]]></description>
<version>16.0.0-dev.1</version>
<version>16.0.0-dev.2</version>
<licence>agpl</licence>
<author>Daniel Calviño Sánchez</author>

4
lib/Controller/AvatarController.php

@ -66,7 +66,9 @@ class AvatarController extends AEnvironmentAwareController {
try {
$file = $this->request->getUploadedFile('file');
$this->avatarService->setAvatarFromRequest($this->getRoom(), $file);
return new DataResponse();
return new DataResponse([
'avatar' => $this->avatarService->getAvatarUrl($this->getRoom(), $this->userSession->getUser()->getUID()),
]);
} catch (InvalidArgumentException $e) {
return new DataResponse(['message' => $e->getMessage()], Http::STATUS_BAD_REQUEST);
} catch (\Exception $e) {

1
lib/Manager.php

@ -172,6 +172,7 @@ class Manager {
(string) $row['name'],
(string) $row['description'],
(string) $row['password'],
(string) $row['avatar'],
(string) $row['remote_server'],
(string) $row['remote_token'],
(int) $row['active_guests'],

56
lib/Migration/Version16000Date20221116163301.php

@ -0,0 +1,56 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2022 Vitor Mattos <vitor@php.rio>
*
* @author Vitor Mattos <vitor@php.rio>
*
* @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\Migration;
use Closure;
use OCP\DB\ISchemaWrapper;
use OCP\DB\Types;
use OCP\Migration\IOutput;
use OCP\Migration\SimpleMigrationStep;
class Version16000Date20221116163301 extends SimpleMigrationStep {
/**
* @param IOutput $output
* @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
* @param array $options
* @return null|ISchemaWrapper
*/
public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper {
/** @var ISchemaWrapper $schema */
$schema = $schemaClosure();
$table = $schema->getTable('talk_rooms');
if (!$table->hasColumn('avatar')) {
$table->addColumn('avatar', Types::STRING, [
'notnull' => false,
'length' => 255,
]);
return $schema;
}
return null;
}
}

1
lib/Model/SelectHelper.php

@ -40,6 +40,7 @@ class SelectHelper {
->addSelect($alias . 'name')
->addSelect($alias . 'description')
->addSelect($alias . 'password')
->addSelect($alias . 'avatar')
->addSelect($alias . 'active_guests')
->addSelect($alias . 'active_since')
->addSelect($alias . 'default_permissions')

11
lib/Room.php

@ -168,6 +168,7 @@ class Room {
private string $name;
private string $description;
private string $password;
private string $avatar;
private string $remoteServer;
private string $remoteToken;
private int $activeGuests;
@ -202,6 +203,7 @@ class Room {
string $name,
string $description,
string $password,
string $avatar,
string $remoteServer,
string $remoteToken,
int $activeGuests,
@ -233,6 +235,7 @@ class Room {
$this->name = $name;
$this->description = $description;
$this->password = $password;
$this->avatar = $avatar;
$this->remoteServer = $remoteServer;
$this->remoteToken = $remoteToken;
$this->activeGuests = $activeGuests;
@ -487,6 +490,14 @@ class Room {
$this->password = $password;
}
public function setAvatar(string $avatar): void {
$this->avatar = $avatar;
}
public function getAvatar(): string {
return $this->avatar;
}
public function getRemoteServer(): string {
return $this->remoteServer;
}

2
lib/Search/ConversationSearch.php

@ -121,7 +121,7 @@ class ConversationSearch implements IProvider {
$icon = $this->url->linkToRouteAbsolute('ocs.spreed.Avatar.getAvatar', [
'token' => $room->getToken(),
'apiVersion' => 'v1',
'v' => $this->avatarService->getAvatarVersion($room, $user->getUID()),
'v' => $room->getAvatar(),
]);
} else {
$icon = '';

42
lib/Service/AvatarService.php

@ -33,17 +33,20 @@ use OCP\Files\IAppData;
use OCP\Files\NotFoundException;
use OCP\Files\SimpleFS\InMemoryFile;
use OCP\Files\SimpleFS\ISimpleFile;
use OCP\Files\SimpleFS\ISimpleFolder;
use OCP\IAvatarManager;
use OCP\IConfig;
use OCP\IL10N;
use OCP\IURLGenerator;
use OCP\IUser;
use Sabre\DAV\UUIDUtil;
class AvatarService {
private IAppData $appData;
private IL10N $l;
private IConfig $config;
private IURLGenerator $url;
private RoomService $roomService;
private IAvatarManager $avatarManager;
public function __construct(
@ -51,21 +54,23 @@ class AvatarService {
IL10N $l,
IConfig $config,
IURLGenerator $url,
RoomService $roomService,
IAvatarManager $avatarManager
) {
$this->appData = $appData;
$this->l = $l;
$this->config = $config;
$this->url = $url;
$this->roomService = $roomService;
$this->avatarManager = $avatarManager;
}
public function setAvatarFromRequest(Room $room, array $file): void {
public function setAvatarFromRequest(Room $room, ?array $file): void {
if ($room->getType() === Room::TYPE_ONE_TO_ONE) {
throw new InvalidArgumentException($this->l->t('One to one rooms always need to show the other users avatar'));
}
if (is_null($file)) {
if (is_null($file) || !is_array($file)) {
throw new InvalidArgumentException($this->l->t('No image file provided'));
}
@ -110,24 +115,39 @@ class AvatarService {
throw new InvalidArgumentException($this->l->t('Unknown filetype'));
}
$token = $room->getToken();
$avatarFolder = $this->getAvatarFolder($token);
$avatarName = UUIDUtil::getUUID();
$avatarFolder->newFile($avatarName, $image->data());
$room->setAvatar($avatarName);
$this->roomService->setAvatar($room, $avatarName);
}
private function getAvatarFolder(string $token): ISimpleFolder {
try {
$folder = $this->appData->getFolder('room-avatar');
} catch (NotFoundException $e) {
$folder = $this->appData->newFolder('room-avatar');
}
$token = $room->getToken();
$content = $image->data();
$folder->newFile($token, $content);
try {
$avatarFolder = $folder->getFolder($token);
} catch (NotFoundException $e) {
$avatarFolder = $folder->newFolder($token);
}
return $avatarFolder;
}
public function getAvatar(Room $room, ?IUser $user): ISimpleFile {
$token = $room->getToken();
try {
$folder = $this->appData->getFolder('room-avatar');
if ($folder->fileExists($token)) {
$file = $folder->getFile($token);
$avatar = $room->getAvatar();
if ($avatar) {
try {
$folder = $this->appData->getFolder('room-avatar');
if ($folder->fileExists($token)) {
$file = $folder->getFolder($token)->getFile($avatar);
}
} catch (NotFoundException $e) {
}
} catch (NotFoundException $e) {
}
// Fallback
if (!isset($file)) {
@ -188,7 +208,7 @@ class AvatarService {
return $this->url->linkToRouteAbsolute('ocs.spreed.Avatar.getAvatar', [
'token' => $room->getToken(),
'apiVersion' => 'v1',
'v' => $this->getAvatarVersion($room, $userId),
'v' => $room->getAvatar(),
]);
}
}

11
lib/Service/RoomService.php

@ -337,6 +337,17 @@ class RoomService {
return true;
}
public function setAvatar(Room $room, $avatarName): bool {
if ($room->getType() === Room::TYPE_ONE_TO_ONE) {
return false;
}
$update = $this->db->getQueryBuilder();
$update->update('talk_rooms')
->set('avatar', $avatarName)
->where($update->expr()->eq('id', $update->createNamedParameter($room->getId(), IQueryBuilder::PARAM_INT)));
return true;
}
/**
* @param Room $room
* @param int $newType Currently it is only allowed to change between `Room::TYPE_GROUP` and `Room::TYPE_PUBLIC`

2
tests/integration/features/bootstrap/FeatureContext.php

@ -41,6 +41,8 @@ class FeatureContext implements Context, SnippetAcceptingContext {
protected static $identifierToToken;
/** @var string[] */
protected static $tokenToIdentifier;
/** @var array[] */
protected static $identifierToAvatar;
/** @var string[] */
protected static $sessionIdToUser;
/** @var string[] */

13
tests/php/Chat/ChatManagerTest.php

@ -32,6 +32,7 @@ use OCA\Talk\Model\AttendeeMapper;
use OCA\Talk\Participant;
use OCA\Talk\Room;
use OCA\Talk\Service\AttachmentService;
use OCA\Talk\Service\AvatarService;
use OCA\Talk\Service\ParticipantService;
use OCA\Talk\Service\PollService;
use OCA\Talk\Service\RoomService;
@ -72,6 +73,8 @@ class ChatManagerTest extends TestCase {
protected $pollService;
/** @var Notifier|MockObject */
protected $notifier;
/** @var AvatarService|MockObject */
protected $avatarService;
/** @var ITimeFactory|MockObject */
protected $timeFactory;
/** @var AttachmentService|MockObject */
@ -92,6 +95,7 @@ class ChatManagerTest extends TestCase {
$this->roomService = $this->createMock(RoomService::class);
$this->pollService = $this->createMock(PollService::class);
$this->notifier = $this->createMock(Notifier::class);
$this->avatarService = $this->createMock(AvatarService::class);
$this->timeFactory = $this->createMock(ITimeFactory::class);
$this->attachmentService = $this->createMock(AttachmentService::class);
$this->referenceManager = $this->createMock(IReferenceManager::class);
@ -108,6 +112,7 @@ class ChatManagerTest extends TestCase {
$this->roomService,
$this->pollService,
$this->notifier,
$this->avatarService,
$cacheFactory,
$this->timeFactory,
$this->attachmentService,
@ -135,6 +140,7 @@ class ChatManagerTest extends TestCase {
$this->roomService,
$this->pollService,
$this->notifier,
$this->avatarService,
$cacheFactory,
$this->timeFactory,
$this->attachmentService,
@ -155,6 +161,7 @@ class ChatManagerTest extends TestCase {
$this->roomService,
$this->pollService,
$this->notifier,
$this->avatarService,
$cacheFactory,
$this->timeFactory,
$this->attachmentService,
@ -698,7 +705,7 @@ class ChatManagerTest extends TestCase {
'actor_type' => Attendee::ACTOR_USERS,
'actor_id' => 'user',
])],
[['id' => 'all', 'label' => 'test', 'source' => 'calls']]
[['id' => 'all', 'label' => 'test', 'source' => 'calls', 'avatar' => '']]
],
[
'all',
@ -707,7 +714,7 @@ class ChatManagerTest extends TestCase {
'actor_type' => Attendee::ACTOR_USERS,
'actor_id' => 'user',
])],
[['id' => 'all', 'label' => 'test', 'source' => 'calls']]
[['id' => 'all', 'label' => 'test', 'source' => 'calls', 'avatar' => '']]
],
[
'here',
@ -716,7 +723,7 @@ class ChatManagerTest extends TestCase {
'actor_type' => Attendee::ACTOR_GUESTS,
'actor_id' => 'guest',
])],
[['id' => 'all', 'label' => 'test', 'source' => 'calls']]
[['id' => 'all', 'label' => 'test', 'source' => 'calls', 'avatar' => '']]
]
];
}

1
tests/php/Service/RoomServiceTest.php

@ -373,6 +373,7 @@ class RoomServiceTest extends TestCase {
'passy',
'',
'',
'',
0,
Attendee::PERMISSIONS_DEFAULT,
Attendee::PERMISSIONS_DEFAULT,

Loading…
Cancel
Save