Extract the EmojiService from user status and add an OCP interface

Signed-off-by: Joas Schilling <coding@schilljs.com>
pull/31703/head
Joas Schilling 2 years ago committed by Vitor Mattos
parent 768da60ae2
commit 902476fa20
No known key found for this signature in database
GPG Key ID: B7AB4B76A7CA7318

@ -31,7 +31,6 @@ return array(
'OCA\\UserStatus\\Migration\\Version0002Date20200902144824' => $baseDir . '/../lib/Migration/Version0002Date20200902144824.php',
'OCA\\UserStatus\\Migration\\Version1000Date20201111130204' => $baseDir . '/../lib/Migration/Version1000Date20201111130204.php',
'OCA\\UserStatus\\Migration\\Version2301Date20210809144824' => $baseDir . '/../lib/Migration/Version2301Date20210809144824.php',
'OCA\\UserStatus\\Service\\EmojiService' => $baseDir . '/../lib/Service/EmojiService.php',
'OCA\\UserStatus\\Service\\JSDataService' => $baseDir . '/../lib/Service/JSDataService.php',
'OCA\\UserStatus\\Service\\PredefinedStatusService' => $baseDir . '/../lib/Service/PredefinedStatusService.php',
'OCA\\UserStatus\\Service\\StatusService' => $baseDir . '/../lib/Service/StatusService.php',

@ -46,7 +46,6 @@ class ComposerStaticInitUserStatus
'OCA\\UserStatus\\Migration\\Version0002Date20200902144824' => __DIR__ . '/..' . '/../lib/Migration/Version0002Date20200902144824.php',
'OCA\\UserStatus\\Migration\\Version1000Date20201111130204' => __DIR__ . '/..' . '/../lib/Migration/Version1000Date20201111130204.php',
'OCA\\UserStatus\\Migration\\Version2301Date20210809144824' => __DIR__ . '/..' . '/../lib/Migration/Version2301Date20210809144824.php',
'OCA\\UserStatus\\Service\\EmojiService' => __DIR__ . '/..' . '/../lib/Service/EmojiService.php',
'OCA\\UserStatus\\Service\\JSDataService' => __DIR__ . '/..' . '/../lib/Service/JSDataService.php',
'OCA\\UserStatus\\Service\\PredefinedStatusService' => __DIR__ . '/..' . '/../lib/Service/PredefinedStatusService.php',
'OCA\\UserStatus\\Service\\StatusService' => __DIR__ . '/..' . '/../lib/Service/StatusService.php',

@ -25,8 +25,8 @@ declare(strict_types=1);
*/
namespace OCA\UserStatus;
use OCA\UserStatus\Service\EmojiService;
use OCP\Capabilities\ICapability;
use OCP\IEmojiHelper;
/**
* Class Capabilities
@ -34,17 +34,10 @@ use OCP\Capabilities\ICapability;
* @package OCA\UserStatus
*/
class Capabilities implements ICapability {
private IEmojiHelper $emojiHelper;
/** @var EmojiService */
private $emojiService;
/**
* Capabilities constructor.
*
* @param EmojiService $emojiService
*/
public function __construct(EmojiService $emojiService) {
$this->emojiService = $emojiService;
public function __construct(IEmojiHelper $emojiHelper) {
$this->emojiHelper = $emojiHelper;
}
/**
@ -54,7 +47,7 @@ class Capabilities implements ICapability {
return [
'user_status' => [
'enabled' => true,
'supports_emoji' => $this->emojiService->doesPlatformSupportEmoji(),
'supports_emoji' => $this->emojiHelper->doesPlatformSupportEmoji(),
],
];
}

@ -1,102 +0,0 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2020, Georg Ehrke
*
* @author Georg Ehrke <oc.list@georgehrke.com>
* @author Joas Schilling <coding@schilljs.com>
*
* @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\UserStatus\Service;
use OCP\IDBConnection;
/**
* Class EmojiService
*
* @package OCA\UserStatus\Service
*/
class EmojiService {
/** @var IDBConnection */
private $db;
/**
* EmojiService constructor.
*
* @param IDBConnection $db
*/
public function __construct(IDBConnection $db) {
$this->db = $db;
}
/**
* @return bool
*/
public function doesPlatformSupportEmoji(): bool {
return $this->db->supports4ByteText() &&
\class_exists(\IntlBreakIterator::class);
}
/**
* @param string $emoji
* @return bool
*/
public function isValidEmoji(string $emoji): bool {
$intlBreakIterator = \IntlBreakIterator::createCharacterInstance();
$intlBreakIterator->setText($emoji);
$characterCount = 0;
while ($intlBreakIterator->next() !== \IntlBreakIterator::DONE) {
$characterCount++;
}
if ($characterCount !== 1) {
return false;
}
$codePointIterator = \IntlBreakIterator::createCodePointInstance();
$codePointIterator->setText($emoji);
foreach ($codePointIterator->getPartsIterator() as $codePoint) {
$codePointType = \IntlChar::charType($codePoint);
// If the current code-point is an emoji or a modifier (like a skin-tone)
// just continue and check the next character
if ($codePointType === \IntlChar::CHAR_CATEGORY_MODIFIER_SYMBOL ||
$codePointType === \IntlChar::CHAR_CATEGORY_MODIFIER_LETTER ||
$codePointType === \IntlChar::CHAR_CATEGORY_OTHER_SYMBOL ||
$codePointType === \IntlChar::CHAR_CATEGORY_GENERAL_OTHER_TYPES) {
continue;
}
// If it's neither a modifier nor an emoji, we only allow
// a zero-width-joiner or a variation selector 16
$codePointValue = \IntlChar::ord($codePoint);
if ($codePointValue === 8205 || $codePointValue === 65039) {
continue;
}
return false;
}
return true;
}
}

@ -37,6 +37,7 @@ use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\DB\Exception;
use OCP\IConfig;
use OCP\IEmojiHelper;
use OCP\IUser;
use OCP\UserStatus\IUserStatus;
@ -56,8 +57,7 @@ class StatusService {
/** @var PredefinedStatusService */
private $predefinedStatusService;
/** @var EmojiService */
private $emojiService;
private IEmojiHelper $emojiHelper;
/** @var bool */
private $shareeEnumeration;
@ -95,24 +95,15 @@ class StatusService {
/** @var int */
public const MAXIMUM_MESSAGE_LENGTH = 80;
/**
* StatusService constructor.
*
* @param UserStatusMapper $mapper
* @param ITimeFactory $timeFactory
* @param PredefinedStatusService $defaultStatusService
* @param EmojiService $emojiService
* @param IConfig $config
*/
public function __construct(UserStatusMapper $mapper,
ITimeFactory $timeFactory,
PredefinedStatusService $defaultStatusService,
EmojiService $emojiService,
IEmojiHelper $emojiHelper,
IConfig $config) {
$this->mapper = $mapper;
$this->timeFactory = $timeFactory;
$this->predefinedStatusService = $defaultStatusService;
$this->emojiService = $emojiService;
$this->emojiHelper = $emojiHelper;
$this->shareeEnumeration = $config->getAppValue('core', 'shareapi_allow_share_dialog_user_enumeration', 'yes') === 'yes';
$this->shareeEnumerationInGroupOnly = $this->shareeEnumeration && $config->getAppValue('core', 'shareapi_restrict_user_enumeration_to_group', 'no') === 'yes';
$this->shareeEnumerationPhone = $this->shareeEnumeration && $config->getAppValue('core', 'shareapi_restrict_user_enumeration_to_phone', 'no') === 'yes';
@ -334,7 +325,7 @@ class StatusService {
}
// Check if statusIcon contains only one character
if ($statusIcon !== null && !$this->emojiService->isValidEmoji($statusIcon)) {
if ($statusIcon !== null && !$this->emojiHelper->isValidSingleEmoji($statusIcon)) {
throw new InvalidStatusIconException('Status-Icon is longer than one character');
}
// Check for maximum length of custom message

@ -26,13 +26,13 @@ declare(strict_types=1);
namespace OCA\UserStatus\Tests;
use OCA\UserStatus\Capabilities;
use OCA\UserStatus\Service\EmojiService;
use OCP\IEmojiHelper;
use Test\TestCase;
class CapabilitiesTest extends TestCase {
/** @var EmojiService|\PHPUnit\Framework\MockObject\MockObject */
private $emojiService;
/** @var IEmojiHelper|\PHPUnit\Framework\MockObject\MockObject */
private $emojiHelper;
/** @var Capabilities */
private $capabilities;
@ -40,8 +40,8 @@ class CapabilitiesTest extends TestCase {
protected function setUp(): void {
parent::setUp();
$this->emojiService = $this->createMock(EmojiService::class);
$this->capabilities = new Capabilities($this->emojiService);
$this->emojiHelper = $this->createMock(IEmojiHelper::class);
$this->capabilities = new Capabilities($this->emojiHelper);
}
/**
@ -50,7 +50,7 @@ class CapabilitiesTest extends TestCase {
* @dataProvider getCapabilitiesDataProvider
*/
public function testGetCapabilities(bool $supportsEmojis): void {
$this->emojiService->expects($this->once())
$this->emojiHelper->expects($this->once())
->method('doesPlatformSupportEmoji')
->willReturn($supportsEmojis);

@ -35,13 +35,13 @@ use OCA\UserStatus\Exception\InvalidMessageIdException;
use OCA\UserStatus\Exception\InvalidStatusIconException;
use OCA\UserStatus\Exception\InvalidStatusTypeException;
use OCA\UserStatus\Exception\StatusMessageTooLongException;
use OCA\UserStatus\Service\EmojiService;
use OCA\UserStatus\Service\PredefinedStatusService;
use OCA\UserStatus\Service\StatusService;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\DB\Exception;
use OCP\IConfig;
use OCP\IEmojiHelper;
use OCP\UserStatus\IUserStatus;
use Test\TestCase;
@ -56,8 +56,8 @@ class StatusServiceTest extends TestCase {
/** @var PredefinedStatusService|\PHPUnit\Framework\MockObject\MockObject */
private $predefinedStatusService;
/** @var EmojiService|\PHPUnit\Framework\MockObject\MockObject */
private $emojiService;
/** @var IEmojiHelper|\PHPUnit\Framework\MockObject\MockObject */
private $emojiHelper;
/** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */
private $config;
@ -71,7 +71,7 @@ class StatusServiceTest extends TestCase {
$this->mapper = $this->createMock(UserStatusMapper::class);
$this->timeFactory = $this->createMock(ITimeFactory::class);
$this->predefinedStatusService = $this->createMock(PredefinedStatusService::class);
$this->emojiService = $this->createMock(EmojiService::class);
$this->emojiHelper = $this->createMock(IEmojiHelper::class);
$this->config = $this->createMock(IConfig::class);
@ -84,7 +84,7 @@ class StatusServiceTest extends TestCase {
$this->service = new StatusService($this->mapper,
$this->timeFactory,
$this->predefinedStatusService,
$this->emojiService,
$this->emojiHelper,
$this->config);
}
@ -138,7 +138,7 @@ class StatusServiceTest extends TestCase {
$this->service = new StatusService($this->mapper,
$this->timeFactory,
$this->predefinedStatusService,
$this->emojiService,
$this->emojiHelper,
$this->config);
$this->assertEquals([], $this->service->findAllRecentStatusChanges(20, 50));
@ -155,7 +155,7 @@ class StatusServiceTest extends TestCase {
$this->service = new StatusService($this->mapper,
$this->timeFactory,
$this->predefinedStatusService,
$this->emojiService,
$this->emojiHelper,
$this->config);
$this->assertEquals([], $this->service->findAllRecentStatusChanges(20, 50));
@ -519,7 +519,7 @@ class StatusServiceTest extends TestCase {
->willThrowException(new DoesNotExistException(''));
}
$this->emojiService->method('isValidEmoji')
$this->emojiHelper->method('isValidSingleEmoji')
->with($statusIcon)
->willReturn($supportsEmoji);

@ -411,6 +411,7 @@ return array(
'OCP\\IDBConnection' => $baseDir . '/lib/public/IDBConnection.php',
'OCP\\IDateTimeFormatter' => $baseDir . '/lib/public/IDateTimeFormatter.php',
'OCP\\IDateTimeZone' => $baseDir . '/lib/public/IDateTimeZone.php',
'OCP\\IEmojiHelper' => $baseDir . '/lib/public/IEmojiHelper.php',
'OCP\\IEventSource' => $baseDir . '/lib/public/IEventSource.php',
'OCP\\IGroup' => $baseDir . '/lib/public/IGroup.php',
'OCP\\IGroupManager' => $baseDir . '/lib/public/IGroupManager.php',
@ -821,7 +822,6 @@ return array(
'OC\\Command\\FileAccess' => $baseDir . '/lib/private/Command/FileAccess.php',
'OC\\Command\\QueueBus' => $baseDir . '/lib/private/Command/QueueBus.php',
'OC\\Comments\\Comment' => $baseDir . '/lib/private/Comments/Comment.php',
'OC\\Comments\\EmojiHelper' => $baseDir . '/lib/private/Comments/EmojiHelper.php',
'OC\\Comments\\Manager' => $baseDir . '/lib/private/Comments/Manager.php',
'OC\\Comments\\ManagerFactory' => $baseDir . '/lib/private/Comments/ManagerFactory.php',
'OC\\Config' => $baseDir . '/lib/private/Config.php',
@ -1089,6 +1089,7 @@ return array(
'OC\\Diagnostics\\QueryLogger' => $baseDir . '/lib/private/Diagnostics/QueryLogger.php',
'OC\\DirectEditing\\Manager' => $baseDir . '/lib/private/DirectEditing/Manager.php',
'OC\\DirectEditing\\Token' => $baseDir . '/lib/private/DirectEditing/Token.php',
'OC\\EmojiHelper' => $baseDir . '/lib/private/EmojiHelper.php',
'OC\\Encryption\\DecryptAll' => $baseDir . '/lib/private/Encryption/DecryptAll.php',
'OC\\Encryption\\EncryptionWrapper' => $baseDir . '/lib/private/Encryption/EncryptionWrapper.php',
'OC\\Encryption\\Exceptions\\DecryptionFailedException' => $baseDir . '/lib/private/Encryption/Exceptions/DecryptionFailedException.php',

@ -440,6 +440,7 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c
'OCP\\IDBConnection' => __DIR__ . '/../../..' . '/lib/public/IDBConnection.php',
'OCP\\IDateTimeFormatter' => __DIR__ . '/../../..' . '/lib/public/IDateTimeFormatter.php',
'OCP\\IDateTimeZone' => __DIR__ . '/../../..' . '/lib/public/IDateTimeZone.php',
'OCP\\IEmojiHelper' => __DIR__ . '/../../..' . '/lib/public/IEmojiHelper.php',
'OCP\\IEventSource' => __DIR__ . '/../../..' . '/lib/public/IEventSource.php',
'OCP\\IGroup' => __DIR__ . '/../../..' . '/lib/public/IGroup.php',
'OCP\\IGroupManager' => __DIR__ . '/../../..' . '/lib/public/IGroupManager.php',
@ -850,7 +851,6 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c
'OC\\Command\\FileAccess' => __DIR__ . '/../../..' . '/lib/private/Command/FileAccess.php',
'OC\\Command\\QueueBus' => __DIR__ . '/../../..' . '/lib/private/Command/QueueBus.php',
'OC\\Comments\\Comment' => __DIR__ . '/../../..' . '/lib/private/Comments/Comment.php',
'OC\\Comments\\EmojiHelper' => __DIR__ . '/../../..' . '/lib/private/Comments/EmojiHelper.php',
'OC\\Comments\\Manager' => __DIR__ . '/../../..' . '/lib/private/Comments/Manager.php',
'OC\\Comments\\ManagerFactory' => __DIR__ . '/../../..' . '/lib/private/Comments/ManagerFactory.php',
'OC\\Config' => __DIR__ . '/../../..' . '/lib/private/Config.php',
@ -1118,6 +1118,7 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c
'OC\\Diagnostics\\QueryLogger' => __DIR__ . '/../../..' . '/lib/private/Diagnostics/QueryLogger.php',
'OC\\DirectEditing\\Manager' => __DIR__ . '/../../..' . '/lib/private/DirectEditing/Manager.php',
'OC\\DirectEditing\\Token' => __DIR__ . '/../../..' . '/lib/private/DirectEditing/Token.php',
'OC\\EmojiHelper' => __DIR__ . '/../../..' . '/lib/private/EmojiHelper.php',
'OC\\Encryption\\DecryptAll' => __DIR__ . '/../../..' . '/lib/private/Encryption/DecryptAll.php',
'OC\\Encryption\\EncryptionWrapper' => __DIR__ . '/../../..' . '/lib/private/Encryption/EncryptionWrapper.php',
'OC\\Encryption\\Exceptions\\DecryptionFailedException' => __DIR__ . '/../../..' . '/lib/private/Encryption/Exceptions/DecryptionFailedException.php',

@ -39,6 +39,7 @@ use OCP\Comments\NotFoundException;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IConfig;
use OCP\IDBConnection;
use OCP\IEmojiHelper;
use OCP\IUser;
use OCP\IInitialStateService;
use OCP\PreConditionNotMetException;
@ -59,7 +60,7 @@ class Manager implements ICommentsManager {
/** @var ITimeFactory */
protected $timeFactory;
/** @var EmojiHelper */
/** @var IEmojiHelper */
protected $emojiHelper;
/** @var IInitialStateService */
@ -81,7 +82,7 @@ class Manager implements ICommentsManager {
LoggerInterface $logger,
IConfig $config,
ITimeFactory $timeFactory,
EmojiHelper $emojiHelper,
IEmojiHelper $emojiHelper,
IInitialStateService $initialStateService) {
$this->dbConn = $dbConn;
$this->logger = $logger;
@ -153,7 +154,7 @@ class Manager implements ICommentsManager {
throw new \UnexpectedValueException('Actor, Object and Verb information must be provided for saving');
}
if ($comment->getVerb() === 'reaction' && !$this->emojiHelper->isValidEmoji($comment->getMessage())) {
if ($comment->getVerb() === 'reaction' && !$this->emojiHelper->isValidSingleEmoji($comment->getMessage())) {
// 4 characters: laptop + person + gender + skin color => "🧑🏽‍💻" is a single emoji from the picker
throw new \UnexpectedValueException('Reactions can only be a single emoji');
}

@ -24,41 +24,24 @@ declare(strict_types=1);
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OC\Comments;
namespace OC;
use OCP\IDBConnection;
use OCP\IEmojiHelper;
/**
* Copied OCA\UserStatus\Service\EmojiService
* Needs to be unified later
*/
class EmojiHelper {
/** @var IDBConnection */
private $db;
class EmojiHelper implements IEmojiHelper {
private IDBConnection $db;
/**
* EmojiService constructor.
*
* @param IDBConnection $db
*/
public function __construct(IDBConnection $db) {
$this->db = $db;
}
/**
* @return bool
*/
public function doesPlatformSupportEmoji(): bool {
return $this->db->supports4ByteText() &&
\class_exists(\IntlBreakIterator::class);
}
/**
* @param string $emoji
* @return bool
*/
public function isValidEmoji(string $emoji): bool {
public function isValidSingleEmoji(string $emoji): bool {
$intlBreakIterator = \IntlBreakIterator::createCharacterInstance();
$intlBreakIterator->setText($emoji);

@ -1431,6 +1431,8 @@ class Server extends ServerContainer implements IServerContainer {
$this->registerAlias(IInitialStateService::class, InitialStateService::class);
$this->registerAlias(\OCP\IEmojiHelper::class, \OC\EmojiHelper::class);
$this->registerAlias(\OCP\UserStatus\IManager::class, \OC\UserStatus\Manager::class);
$this->registerAlias(IBroker::class, Broker::class);

@ -0,0 +1,39 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2022 Joas Schilling <coding@schilljs.com>
*
* @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 OCP;
/**
* @since 24.0.0
*/
interface IEmojiHelper {
/**
* @since 24.0.0
*/
public function doesPlatformSupportEmoji(): bool;
/**
* @since 24.0.0
*/
public function isValidSingleEmoji(string $emoji): bool;
}

@ -3,8 +3,8 @@
namespace Test\Comments;
use OC\Comments\Comment;
use OC\Comments\EmojiHelper;
use OC\Comments\Manager;
use OC\EmojiHelper;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\Comments\IComment;
use OCP\Comments\ICommentsEventHandler;

@ -1,7 +1,6 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2020, Georg Ehrke
*
@ -23,25 +22,24 @@ declare(strict_types=1);
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCA\UserStatus\Tests\Service;
namespace Test;
use OCA\UserStatus\Service\EmojiService;
use OC\EmojiHelper;
use OCP\IDBConnection;
use Test\TestCase;
use OCP\IEmojiHelper;
class EmojiServiceTest extends TestCase {
class EmojiHelperTest extends TestCase {
/** @var IDBConnection|\PHPUnit\Framework\MockObject\MockObject */
private $db;
/** @var EmojiService */
private $service;
private IEmojiHelper $helper;
protected function setUp(): void {
parent::setUp();
$this->db = $this->createMock(IDBConnection::class);
$this->service = new EmojiService($this->db);
$this->helper = new EmojiHelper($this->db);
}
/**
@ -55,7 +53,7 @@ class EmojiServiceTest extends TestCase {
->method('supports4ByteText')
->willReturn($supports4ByteText);
$this->assertEquals($expected, $this->service->doesPlatformSupportEmoji());
$this->assertEquals($expected, $this->helper->doesPlatformSupportEmoji());
}
/**
@ -72,15 +70,15 @@ class EmojiServiceTest extends TestCase {
* @param string $emoji
* @param bool $expected
*
* @dataProvider isValidEmojiDataProvider
* @dataProvider isValidSingleEmojiDataProvider
*/
public function testIsValidEmoji(string $emoji, bool $expected): void {
$actual = $this->service->isValidEmoji($emoji);
public function testIsValidSingleEmoji(string $emoji, bool $expected): void {
$actual = $this->helper->isValidSingleEmoji($emoji);
$this->assertEquals($expected, $actual);
}
public function isValidEmojiDataProvider(): array {
public function isValidSingleEmojiDataProvider(): array {
return [
['🏝', true],
['📱', true],
Loading…
Cancel
Save