Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(comments): Allow mentions of federated users, groups and teams #43875

Merged
merged 1 commit into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion lib/private/Comments/Comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ public function setMessage($message, $maxLength = self::MAX_MESSAGE_LENGTH): ICo
*
*/
public function getMentions(): array {
$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);
$ok = preg_match_all("/\B(?<![^a-z0-9_\-@\.\'\s])@(\"guest\/[a-f0-9]+\"|\"(?:federated_)?(?:group|team|user){1}\/[a-z0-9_\-@\.\' \/:]+\"|\"[a-z0-9_\-@\.\' ]+\"|[a-z0-9_\-@\.\']+)/i", $this->getMessage(), $mentions);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit:
I think you can remove some \ since . lose it's special meaning between [ ]

Suggested change
$ok = preg_match_all("/\B(?<![^a-z0-9_\-@\.\'\s])@(\"guest\/[a-f0-9]+\"|\"(?:federated_)?(?:group|team|user){1}\/[a-z0-9_\-@\.\' \/:]+\"|\"[a-z0-9_\-@\.\' ]+\"|[a-z0-9_\-@\.\']+)/i", $this->getMessage(), $mentions);
$ok = preg_match_all("/\B(?<![^a-z0-9_\-@.'\s])@(\"guest\/[a-f0-9]+\"|\"(?:federated_)?(?:group|team|user){1}\/[a-z0-9_\-@.' \/:]+\"|\"[a-z0-9_\-@.' ]+\"|[a-z0-9_\-@.']+)/i", $this->getMessage(), $mentions);

if (!$ok || !isset($mentions[0])) {
return [];
}
Expand All @@ -230,11 +230,21 @@ public function getMentions(): array {
});
$result = [];
foreach ($mentionIds as $mentionId) {
// Cut-off the @ and remove wrapping double-quotes
$cleanId = trim(substr($mentionId, 1), '"');

if (str_starts_with($cleanId, 'guest/')) {
$result[] = ['type' => 'guest', 'id' => $cleanId];
} elseif (str_starts_with($cleanId, 'federated_group/')) {
$result[] = ['type' => 'federated_group', 'id' => substr($cleanId, 16)];
} elseif (str_starts_with($cleanId, 'group/')) {
$result[] = ['type' => 'group', 'id' => substr($cleanId, 6)];
} elseif (str_starts_with($cleanId, 'federated_team/')) {
$result[] = ['type' => 'federated_team', 'id' => substr($cleanId, 15)];
} elseif (str_starts_with($cleanId, 'team/')) {
$result[] = ['type' => 'team', 'id' => substr($cleanId, 5)];
} elseif (str_starts_with($cleanId, 'federated_user/')) {
$result[] = ['type' => 'federated_user', 'id' => substr($cleanId, 15)];
} else {
$result[] = ['type' => 'user', 'id' => $cleanId];
}
Expand Down
67 changes: 38 additions & 29 deletions tests/lib/Comments/CommentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,39 +134,63 @@ public function testSetUberlongMessage() {
$comment->setMessage($msg);
}

public function mentionsProvider() {
public function mentionsProvider(): array {
return [
[
'@alice @bob look look, a cook!', ['alice', 'bob']
'@alice @bob look look, a cook!',
[['type' => 'user', 'id' => 'alice'], ['type' => 'user', 'id' => 'bob']],
],
[
'no mentions in this message', []
'no mentions in this message',
[]
],
[
'@alice @bob look look, a duplication @alice test @bob!', ['alice', 'bob']
'@alice @bob look look, a duplication @alice test @bob!',
[['type' => 'user', 'id' => 'alice'], ['type' => 'user', 'id' => 'bob']],
],
[
'@alice is the author, notify @bob, nevertheless mention her!', ['alice', 'bob'], 'alice'
'@alice is the author, notify @bob, nevertheless mention her!',
[['type' => 'user', 'id' => 'alice'], ['type' => 'user', 'id' => 'bob']],
/* author: */ 'alice'
],
[
'@foobar and @barfoo you should know, @foo@bar.com is valid' .
' and so is @bar@foo.org@foobar.io I hope that clarifies everything.' .
' cc @23452-4333-54353-2342 @yolo!' .
' however the most important thing to know is that www.croissant.com/@oil is not valid' .
' and won\'t match anything at all',
['bar@foo.org@foobar.io', '23452-4333-54353-2342', 'foo@bar.com', 'foobar', 'barfoo', 'yolo']
[
['type' => 'user', 'id' => 'bar@foo.org@foobar.io'],
['type' => 'user', 'id' => '23452-4333-54353-2342'],
['type' => 'user', 'id' => 'foo@bar.com'],
['type' => 'user', 'id' => 'foobar'],
['type' => 'user', 'id' => 'barfoo'],
['type' => 'user', 'id' => 'yolo'],
],
],
[
'@@chef is also a valid mention, no matter how strange it looks', ['@chef']
'@@chef is also a valid mention, no matter how strange it looks',
[['type' => 'user', 'id' => '@chef']],
],
[
'Also @"user with spaces" are now supported', ['user with spaces']
'Also @"user with spaces" are now supported',
[['type' => 'user', 'id' => 'user with spaces']],
],
[
'Also @"guest/0123456789abcdef" are now supported', [], null, ['guest/0123456789abcdef']
'Also @"guest/0123456789abcdef" are now supported',
[['type' => 'guest', 'id' => 'guest/0123456789abcdef']],
],
[
'Also @"group/My Group ID 321" are now supported', [], null, [], ['My Group ID 321']
'Also @"group/My Group ID 321" are now supported',
[['type' => 'group', 'id' => 'My Group ID 321']],
],
[
'Welcome federation @"federated_group/My Group ID 321" @"federated_team/Former Cirle" @"federated_user/cloudId@http://example.tld:8080/nextcloud"! Now freshly supported',
[
['type' => 'federated_user', 'id' => 'cloudId@http://example.tld:8080/nextcloud'],
['type' => 'federated_group', 'id' => 'My Group ID 321'],
['type' => 'federated_team', 'id' => 'Former Cirle'],
],
],
];
}
Expand All @@ -175,31 +199,16 @@ public function mentionsProvider() {
* @dataProvider mentionsProvider
*
* @param string $message
* @param array $expectedUids
* @param string|null $author
* @param array $expectedGuests
* @param array $expectedMentions
* @param ?string $author
*/
public function testMentions(string $message, array $expectedUids, ?string $author = null, array $expectedGuests = [], array $expectedGroups = []): void {
public function testMentions(string $message, array $expectedMentions, ?string $author = null): void {
$comment = new Comment();
$comment->setMessage($message);
if (!is_null($author)) {
$comment->setActor('user', $author);
}
$mentions = $comment->getMentions();
while ($mention = array_shift($mentions)) {
if ($mention['type'] === 'user') {
$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;
}
$this->assertSame($id, $mention['id']);
}
$this->assertEmpty($mentions);
$this->assertEmpty($expectedUids);
$this->assertSame($expectedMentions, $mentions);
}
}
Loading