diff options
author | Joas Schilling <coding@schilljs.com> | 2022-03-24 15:57:17 +0100 |
---|---|---|
committer | Vitor Mattos <vitor@php.rio> | 2022-04-28 07:09:44 -0300 |
commit | 902476fa20516ac3abe3c79dd10aad1c8b60910e (patch) | |
tree | 15f277f69f714ecdba91c62356cbd7e1f96b4dee /apps | |
parent | 768da60ae2bc11b415b52624121d70316c1e9de6 (diff) | |
download | nextcloud-server-902476fa20516ac3abe3c79dd10aad1c8b60910e.tar.gz nextcloud-server-902476fa20516ac3abe3c79dd10aad1c8b60910e.zip |
Extract the EmojiService from user status and add an OCP interface
Signed-off-by: Joas Schilling <coding@schilljs.com>
Diffstat (limited to 'apps')
-rw-r--r-- | apps/user_status/composer/composer/autoload_classmap.php | 1 | ||||
-rw-r--r-- | apps/user_status/composer/composer/autoload_static.php | 1 | ||||
-rw-r--r-- | apps/user_status/lib/Capabilities.php | 17 | ||||
-rw-r--r-- | apps/user_status/lib/Service/EmojiService.php | 102 | ||||
-rw-r--r-- | apps/user_status/lib/Service/StatusService.php | 19 | ||||
-rw-r--r-- | apps/user_status/tests/Unit/CapabilitiesTest.php | 12 | ||||
-rw-r--r-- | apps/user_status/tests/Unit/Service/EmojiServiceTest.php | 100 | ||||
-rw-r--r-- | apps/user_status/tests/Unit/Service/StatusServiceTest.php | 16 |
8 files changed, 24 insertions, 244 deletions
diff --git a/apps/user_status/composer/composer/autoload_classmap.php b/apps/user_status/composer/composer/autoload_classmap.php index 64da04fb8ba..9988231d76e 100644 --- a/apps/user_status/composer/composer/autoload_classmap.php +++ b/apps/user_status/composer/composer/autoload_classmap.php @@ -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', diff --git a/apps/user_status/composer/composer/autoload_static.php b/apps/user_status/composer/composer/autoload_static.php index c4ee7d031b5..1ad125615ea 100644 --- a/apps/user_status/composer/composer/autoload_static.php +++ b/apps/user_status/composer/composer/autoload_static.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', diff --git a/apps/user_status/lib/Capabilities.php b/apps/user_status/lib/Capabilities.php index f5b35896a56..8f26525286c 100644 --- a/apps/user_status/lib/Capabilities.php +++ b/apps/user_status/lib/Capabilities.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(), ], ]; } diff --git a/apps/user_status/lib/Service/EmojiService.php b/apps/user_status/lib/Service/EmojiService.php deleted file mode 100644 index 0f197933872..00000000000 --- a/apps/user_status/lib/Service/EmojiService.php +++ /dev/null @@ -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; - } -} diff --git a/apps/user_status/lib/Service/StatusService.php b/apps/user_status/lib/Service/StatusService.php index 5dd70e4ea5e..e038570fc00 100644 --- a/apps/user_status/lib/Service/StatusService.php +++ b/apps/user_status/lib/Service/StatusService.php @@ -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 diff --git a/apps/user_status/tests/Unit/CapabilitiesTest.php b/apps/user_status/tests/Unit/CapabilitiesTest.php index af36900d3bc..02874fcbf6d 100644 --- a/apps/user_status/tests/Unit/CapabilitiesTest.php +++ b/apps/user_status/tests/Unit/CapabilitiesTest.php @@ -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); diff --git a/apps/user_status/tests/Unit/Service/EmojiServiceTest.php b/apps/user_status/tests/Unit/Service/EmojiServiceTest.php deleted file mode 100644 index 454f1d75a0d..00000000000 --- a/apps/user_status/tests/Unit/Service/EmojiServiceTest.php +++ /dev/null @@ -1,100 +0,0 @@ -<?php - -declare(strict_types=1); - -/** - * @copyright Copyright (c) 2020, Georg Ehrke - * - * @author Georg Ehrke <oc.list@georgehrke.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\Tests\Service; - -use OCA\UserStatus\Service\EmojiService; -use OCP\IDBConnection; -use Test\TestCase; - -class EmojiServiceTest extends TestCase { - - /** @var IDBConnection|\PHPUnit\Framework\MockObject\MockObject */ - private $db; - - /** @var EmojiService */ - private $service; - - protected function setUp(): void { - parent::setUp(); - - $this->db = $this->createMock(IDBConnection::class); - $this->service = new EmojiService($this->db); - } - - /** - * @param bool $supports4ByteText - * @param bool $expected - * - * @dataProvider doesPlatformSupportEmojiDataProvider - */ - public function testDoesPlatformSupportEmoji(bool $supports4ByteText, bool $expected): void { - $this->db->expects($this->once()) - ->method('supports4ByteText') - ->willReturn($supports4ByteText); - - $this->assertEquals($expected, $this->service->doesPlatformSupportEmoji()); - } - - /** - * @return array - */ - public function doesPlatformSupportEmojiDataProvider(): array { - return [ - [true, true], - [false, false], - ]; - } - - /** - * @param string $emoji - * @param bool $expected - * - * @dataProvider isValidEmojiDataProvider - */ - public function testIsValidEmoji(string $emoji, bool $expected): void { - $actual = $this->service->isValidEmoji($emoji); - - $this->assertEquals($expected, $actual); - } - - public function isValidEmojiDataProvider(): array { - return [ - ['🏝', true], - ['📱', true], - ['🏢', true], - ['📱📠', false], - ['a', false], - ['0', false], - ['$', false], - // Test some more complex emojis with modifiers and zero-width-joiner - ['👩🏿💻', true], - ['🤷🏼♀️', true], - ['🏳️🌈', true], - ['👨👨👦👦', true], - ['👩❤️👩', true] - ]; - } -} diff --git a/apps/user_status/tests/Unit/Service/StatusServiceTest.php b/apps/user_status/tests/Unit/Service/StatusServiceTest.php index 3dd397e1d36..df31cf0d5ad 100644 --- a/apps/user_status/tests/Unit/Service/StatusServiceTest.php +++ b/apps/user_status/tests/Unit/Service/StatusServiceTest.php @@ -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); |