@@ -34,68 +34,68 @@ |
||
| 34 | 34 | */ |
| 35 | 35 | class EmojiHelper { |
| 36 | 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 | - } |
|
| 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 | 101 | } |
@@ -47,1592 +47,1592 @@ |
||
| 47 | 47 | |
| 48 | 48 | class Manager implements ICommentsManager { |
| 49 | 49 | |
| 50 | - /** @var IDBConnection */ |
|
| 51 | - protected $dbConn; |
|
| 52 | - |
|
| 53 | - /** @var LoggerInterface */ |
|
| 54 | - protected $logger; |
|
| 55 | - |
|
| 56 | - /** @var IConfig */ |
|
| 57 | - protected $config; |
|
| 58 | - |
|
| 59 | - /** @var ITimeFactory */ |
|
| 60 | - protected $timeFactory; |
|
| 61 | - |
|
| 62 | - /** @var EmojiHelper */ |
|
| 63 | - protected $emojiHelper; |
|
| 64 | - |
|
| 65 | - /** @var IInitialStateService */ |
|
| 66 | - protected $initialStateService; |
|
| 67 | - |
|
| 68 | - /** @var IComment[] */ |
|
| 69 | - protected $commentsCache = []; |
|
| 70 | - |
|
| 71 | - /** @var \Closure[] */ |
|
| 72 | - protected $eventHandlerClosures = []; |
|
| 73 | - |
|
| 74 | - /** @var ICommentsEventHandler[] */ |
|
| 75 | - protected $eventHandlers = []; |
|
| 76 | - |
|
| 77 | - /** @var \Closure[] */ |
|
| 78 | - protected $displayNameResolvers = []; |
|
| 79 | - |
|
| 80 | - public function __construct(IDBConnection $dbConn, |
|
| 81 | - LoggerInterface $logger, |
|
| 82 | - IConfig $config, |
|
| 83 | - ITimeFactory $timeFactory, |
|
| 84 | - EmojiHelper $emojiHelper, |
|
| 85 | - IInitialStateService $initialStateService) { |
|
| 86 | - $this->dbConn = $dbConn; |
|
| 87 | - $this->logger = $logger; |
|
| 88 | - $this->config = $config; |
|
| 89 | - $this->timeFactory = $timeFactory; |
|
| 90 | - $this->emojiHelper = $emojiHelper; |
|
| 91 | - $this->initialStateService = $initialStateService; |
|
| 92 | - } |
|
| 93 | - |
|
| 94 | - /** |
|
| 95 | - * converts data base data into PHP native, proper types as defined by |
|
| 96 | - * IComment interface. |
|
| 97 | - * |
|
| 98 | - * @param array $data |
|
| 99 | - * @return array |
|
| 100 | - */ |
|
| 101 | - protected function normalizeDatabaseData(array $data) { |
|
| 102 | - $data['id'] = (string)$data['id']; |
|
| 103 | - $data['parent_id'] = (string)$data['parent_id']; |
|
| 104 | - $data['topmost_parent_id'] = (string)$data['topmost_parent_id']; |
|
| 105 | - $data['creation_timestamp'] = new \DateTime($data['creation_timestamp']); |
|
| 106 | - if (!is_null($data['latest_child_timestamp'])) { |
|
| 107 | - $data['latest_child_timestamp'] = new \DateTime($data['latest_child_timestamp']); |
|
| 108 | - } |
|
| 109 | - $data['children_count'] = (int)$data['children_count']; |
|
| 110 | - $data['reference_id'] = $data['reference_id'] ?? null; |
|
| 111 | - if ($this->supportReactions()) { |
|
| 112 | - $list = json_decode($data['reactions'], true); |
|
| 113 | - // Ordering does not work on the database with group concat and Oracle, |
|
| 114 | - // So we simply sort on the output. |
|
| 115 | - if (is_array($list)) { |
|
| 116 | - uasort($list, static function ($a, $b) { |
|
| 117 | - if ($a === $b) { |
|
| 118 | - return 0; |
|
| 119 | - } |
|
| 120 | - return ($a > $b) ? -1 : 1; |
|
| 121 | - }); |
|
| 122 | - } |
|
| 123 | - $data['reactions'] = $list; |
|
| 124 | - } |
|
| 125 | - return $data; |
|
| 126 | - } |
|
| 127 | - |
|
| 128 | - |
|
| 129 | - /** |
|
| 130 | - * @param array $data |
|
| 131 | - * @return IComment |
|
| 132 | - */ |
|
| 133 | - public function getCommentFromData(array $data): IComment { |
|
| 134 | - return new Comment($this->normalizeDatabaseData($data)); |
|
| 135 | - } |
|
| 136 | - |
|
| 137 | - /** |
|
| 138 | - * prepares a comment for an insert or update operation after making sure |
|
| 139 | - * all necessary fields have a value assigned. |
|
| 140 | - * |
|
| 141 | - * @param IComment $comment |
|
| 142 | - * @return IComment returns the same updated IComment instance as provided |
|
| 143 | - * by parameter for convenience |
|
| 144 | - * @throws \UnexpectedValueException |
|
| 145 | - */ |
|
| 146 | - protected function prepareCommentForDatabaseWrite(IComment $comment) { |
|
| 147 | - if (!$comment->getActorType() |
|
| 148 | - || $comment->getActorId() === '' |
|
| 149 | - || !$comment->getObjectType() |
|
| 150 | - || $comment->getObjectId() === '' |
|
| 151 | - || !$comment->getVerb() |
|
| 152 | - ) { |
|
| 153 | - throw new \UnexpectedValueException('Actor, Object and Verb information must be provided for saving'); |
|
| 154 | - } |
|
| 155 | - |
|
| 156 | - if ($comment->getVerb() === 'reaction' && !$this->emojiHelper->isValidEmoji($comment->getMessage())) { |
|
| 157 | - // 4 characters: laptop + person + gender + skin color => " |
|