Skip to content

Commit 37106e0

Browse files
authored
Merge pull request #31700 from nextcloud/bugfix/noid/allow-4-char-emojis
Allow reactions also with other combined emojis 🧑🏾‍💻
2 parents 04d4666 + 3b24a4f commit 37106e0

5 files changed

Lines changed: 118 additions & 9 deletions

File tree

lib/composer/composer/autoload_classmap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -808,6 +808,7 @@
808808
'OC\\Command\\FileAccess' => $baseDir . '/lib/private/Command/FileAccess.php',
809809
'OC\\Command\\QueueBus' => $baseDir . '/lib/private/Command/QueueBus.php',
810810
'OC\\Comments\\Comment' => $baseDir . '/lib/private/Comments/Comment.php',
811+
'OC\\Comments\\EmojiHelper' => $baseDir . '/lib/private/Comments/EmojiHelper.php',
811812
'OC\\Comments\\Manager' => $baseDir . '/lib/private/Comments/Manager.php',
812813
'OC\\Comments\\ManagerFactory' => $baseDir . '/lib/private/Comments/ManagerFactory.php',
813814
'OC\\Config' => $baseDir . '/lib/private/Config.php',

lib/composer/composer/autoload_static.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -837,6 +837,7 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c
837837
'OC\\Command\\FileAccess' => __DIR__ . '/../../..' . '/lib/private/Command/FileAccess.php',
838838
'OC\\Command\\QueueBus' => __DIR__ . '/../../..' . '/lib/private/Command/QueueBus.php',
839839
'OC\\Comments\\Comment' => __DIR__ . '/../../..' . '/lib/private/Comments/Comment.php',
840+
'OC\\Comments\\EmojiHelper' => __DIR__ . '/../../..' . '/lib/private/Comments/EmojiHelper.php',
840841
'OC\\Comments\\Manager' => __DIR__ . '/../../..' . '/lib/private/Comments/Manager.php',
841842
'OC\\Comments\\ManagerFactory' => __DIR__ . '/../../..' . '/lib/private/Comments/ManagerFactory.php',
842843
'OC\\Config' => __DIR__ . '/../../..' . '/lib/private/Config.php',
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* @copyright Copyright (c) 2020, Georg Ehrke
7+
*
8+
* @author Georg Ehrke <oc.list@georgehrke.com>
9+
* @author Joas Schilling <coding@schilljs.com>
10+
*
11+
* @license GNU AGPL version 3 or any later version
12+
*
13+
* This program is free software: you can redistribute it and/or modify
14+
* it under the terms of the GNU Affero General Public License as
15+
* published by the Free Software Foundation, either version 3 of the
16+
* License, or (at your option) any later version.
17+
*
18+
* This program is distributed in the hope that it will be useful,
19+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
20+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21+
* GNU Affero General Public License for more details.
22+
*
23+
* You should have received a copy of the GNU Affero General Public License
24+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
25+
*
26+
*/
27+
namespace OC\Comments;
28+
29+
use OCP\IDBConnection;
30+
31+
/**
32+
* Copied OCA\UserStatus\Service\EmojiService
33+
* Needs to be unified later
34+
*/
35+
class EmojiHelper {
36+
37+
/** @var IDBConnection */
38+
private $db;
39+
40+
/**
41+
* EmojiService constructor.
42+
*
43+
* @param IDBConnection $db
44+
*/
45+
public function __construct(IDBConnection $db) {
46+
$this->db = $db;
47+
}
48+
49+
/**
50+
* @return bool
51+
*/
52+
public function doesPlatformSupportEmoji(): bool {
53+
return $this->db->supports4ByteText() &&
54+
\class_exists(\IntlBreakIterator::class);
55+
}
56+
57+
/**
58+
* @param string $emoji
59+
* @return bool
60+
*/
61+
public function isValidEmoji(string $emoji): bool {
62+
$intlBreakIterator = \IntlBreakIterator::createCharacterInstance();
63+
$intlBreakIterator->setText($emoji);
64+
65+
$characterCount = 0;
66+
while ($intlBreakIterator->next() !== \IntlBreakIterator::DONE) {
67+
$characterCount++;
68+
}
69+
70+
if ($characterCount !== 1) {
71+
return false;
72+
}
73+
74+
$codePointIterator = \IntlBreakIterator::createCodePointInstance();
75+
$codePointIterator->setText($emoji);
76+
77+
foreach ($codePointIterator->getPartsIterator() as $codePoint) {
78+
$codePointType = \IntlChar::charType($codePoint);
79+
80+
// If the current code-point is an emoji or a modifier (like a skin-tone)
81+
// just continue and check the next character
82+
if ($codePointType === \IntlChar::CHAR_CATEGORY_MODIFIER_SYMBOL ||
83+
$codePointType === \IntlChar::CHAR_CATEGORY_MODIFIER_LETTER ||
84+
$codePointType === \IntlChar::CHAR_CATEGORY_OTHER_SYMBOL ||
85+
$codePointType === \IntlChar::CHAR_CATEGORY_GENERAL_OTHER_TYPES) {
86+
continue;
87+
}
88+
89+
// If it's neither a modifier nor an emoji, we only allow
90+
// a zero-width-joiner or a variation selector 16
91+
$codePointValue = \IntlChar::ord($codePoint);
92+
if ($codePointValue === 8205 || $codePointValue === 65039) {
93+
continue;
94+
}
95+
96+
return false;
97+
}
98+
99+
return true;
100+
}
101+
}

lib/private/Comments/Manager.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ class Manager implements ICommentsManager {
5959
/** @var ITimeFactory */
6060
protected $timeFactory;
6161

62+
/** @var EmojiHelper */
63+
protected $emojiHelper;
64+
6265
/** @var IInitialStateService */
6366
protected $initialStateService;
6467

@@ -78,11 +81,13 @@ public function __construct(IDBConnection $dbConn,
7881
LoggerInterface $logger,
7982
IConfig $config,
8083
ITimeFactory $timeFactory,
84+
EmojiHelper $emojiHelper,
8185
IInitialStateService $initialStateService) {
8286
$this->dbConn = $dbConn;
8387
$this->logger = $logger;
8488
$this->config = $config;
8589
$this->timeFactory = $timeFactory;
90+
$this->emojiHelper = $emojiHelper;
8691
$this->initialStateService = $initialStateService;
8792
}
8893

@@ -148,8 +153,9 @@ protected function prepareCommentForDatabaseWrite(IComment $comment) {
148153
throw new \UnexpectedValueException('Actor, Object and Verb information must be provided for saving');
149154
}
150155

151-
if ($comment->getVerb() === 'reaction' && mb_strlen($comment->getMessage()) > 2) {
152-
throw new \UnexpectedValueException('Reactions cannot be longer than 2 chars (emoji with skin tone have two chars)');
156+
if ($comment->getVerb() === 'reaction' && !$this->emojiHelper->isValidEmoji($comment->getMessage())) {
157+
// 4 characters: laptop + person + gender + skin color => "🧑🏽‍💻" is a single emoji from the picker
158+
throw new \UnexpectedValueException('Reactions can only be a single emoji');
153159
}
154160

155161
if ($comment->getId() === '') {

tests/lib/Comments/ManagerTest.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Test\Comments;
44

55
use OC\Comments\Comment;
6+
use OC\Comments\EmojiHelper;
67
use OC\Comments\Manager;
78
use OCP\AppFramework\Utility\ITimeFactory;
89
use OCP\Comments\IComment;
@@ -74,6 +75,7 @@ protected function getManager() {
7475
$this->createMock(LoggerInterface::class),
7576
$this->createMock(IConfig::class),
7677
$this->createMock(ITimeFactory::class),
78+
new EmojiHelper($this->connection),
7779
$this->createMock(IInitialStateService::class)
7880
);
7981
}
@@ -1181,15 +1183,13 @@ public function testReactionMessageSize($reactionString, $valid) {
11811183

11821184
public function providerTestReactionMessageSize(): array {
11831185
return [
1184-
['a', true],
1185-
['1', true],
1186-
['12', true],
1187-
['123', false],
1186+
['a', false],
1187+
['1', false],
11881188
['👍', true],
1189-
['👍👍', true],
1189+
['👍👍', false],
11901190
['👍🏽', true],
1191-
['👍🏽👍', false],
1192-
['👍🏽👍🏽', false],
1191+
['👨🏽‍💻', true],
1192+
['👨🏽‍💻👍', false],
11931193
];
11941194
}
11951195

0 commit comments

Comments
 (0)