Skip to content

Commit

Permalink
Merge pull request #29140 from nextcloud/feature/noid/prepare-for-gro…
Browse files Browse the repository at this point in the history
…up-mentions

Prepare for group mentions in talk (and comments?)
  • Loading branch information
nickvergessen authored Oct 13, 2021
2 parents 5b77643 + 3487aee commit e962786
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
20 changes: 11 additions & 9 deletions lib/private/Comments/Comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -227,21 +227,23 @@ public function setMessage($message, $maxLength = self::MAX_MESSAGE_LENGTH) {
*
*/
public function getMentions() {
$ok = preg_match_all("/\B(?<![^a-z0-9_\-@\.\'\s])@(\"guest\/[a-f0-9]+\"|\"[a-z0-9_\-@\.\' ]+\"|[a-z0-9_\-@\.\']+)/i", $this->getMessage(), $mentions);
$ok = preg_match_all("/\B(?<![^a-z0-9_\-@\.\'\s])@(\"guest\/[a-f0-9]+\"|\"group\/[a-z0-9_\-@\.\' ]+\"|\"[a-z0-9_\-@\.\' ]+\"|[a-z0-9_\-@\.\']+)/i", $this->getMessage(), $mentions);
if (!$ok || !isset($mentions[0]) || !is_array($mentions[0])) {
return [];
}
$uids = array_unique($mentions[0]);
usort($uids, static function ($uid1, $uid2) {
return mb_strlen($uid2) <=> mb_strlen($uid1);
$mentionIds = array_unique($mentions[0]);
usort($mentionIds, static function ($mentionId1, $mentionId2) {
return mb_strlen($mentionId2) <=> mb_strlen($mentionId1);
});
$result = [];
foreach ($uids as $uid) {
$cleanUid = trim(substr($uid, 1), '"');
if (strpos($cleanUid, 'guest/') === 0) {
$result[] = ['type' => 'guest', 'id' => $cleanUid];
foreach ($mentionIds as $mentionId) {
$cleanId = trim(substr($mentionId, 1), '"');
if (strpos($cleanId, 'guest/') === 0) {
$result[] = ['type' => 'guest', 'id' => $cleanId];
} elseif (strpos($cleanId, 'group/') === 0) {
$result[] = ['type' => 'group', 'id' => substr($cleanId, 6)];
} else {
$result[] = ['type' => 'user', 'id' => $cleanUid];
$result[] = ['type' => 'user', 'id' => $cleanId];
}
}
return $result;
Expand Down
7 changes: 6 additions & 1 deletion tests/lib/Comments/CommentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,9 @@ public function mentionsProvider() {
[
'Also @"guest/0123456789abcdef" are now supported', [], null, ['guest/0123456789abcdef']
],
[
'Also @"group/My Group ID 321" are now supported', [], null, [], ['My Group ID 321']
],
];
}

Expand All @@ -171,7 +174,7 @@ public function mentionsProvider() {
* @param string|null $author
* @param array $expectedGuests
*/
public function testMentions(string $message, array $expectedUids, ?string $author = null, array $expectedGuests = []): void {
public function testMentions(string $message, array $expectedUids, ?string $author = null, array $expectedGuests = [], array $expectedGroups = []): void {
$comment = new Comment();
$comment->setMessage($message);
if (!is_null($author)) {
Expand All @@ -183,6 +186,8 @@ public function testMentions(string $message, array $expectedUids, ?string $auth
$id = array_shift($expectedUids);
} elseif ($mention['type'] === 'guest') {
$id = array_shift($expectedGuests);
} elseif ($mention['type'] === 'group') {
$id = array_shift($expectedGroups);
} else {
$this->fail('Unexpected mention type');
continue;
Expand Down

0 comments on commit e962786

Please sign in to comment.