Merge pull request #16331 from nextcloud/feature/noid/talk-guest-mentions

Allow guest mentions of talk to be parsed
This commit is contained in:
Roeland Jago Douma 2019-07-12 10:35:54 +02:00 committed by GitHub
commit c193c0d466
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 6 deletions

View file

@ -226,14 +226,19 @@ class Comment implements IComment {
*
*/
public function getMentions() {
$ok = preg_match_all("/\B(?<![^a-z0-9_\-@\.\'\s])@(\"[a-z0-9_\-@\.\' ]+\"|[a-z0-9_\-@\.\']+)/i", $this->getMessage(), $mentions);
$ok = preg_match_all("/\B(?<![^a-z0-9_\-@\.\'\s])@(\"guest\/[a-f0-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]);
$result = [];
foreach ($uids as $uid) {
$result[] = ['type' => 'user', 'id' => trim(substr($uid, 1), '"')];
$cleanUid = trim(substr($uid, 1), '"');
if (strpos($cleanUid, 'guest/') === 0) {
$result[] = ['type' => 'guest', 'id' => $cleanUid];
} else {
$result[] = ['type' => 'user', 'id' => $cleanUid];
}
}
return $result;
}

View file

@ -155,13 +155,21 @@ class CommentTest extends TestCase {
[
'Also @"user with spaces" are now supported', ['user with spaces']
],
[
'Also @"guest/0123456789abcdef" are now supported', [], null, ['guest/0123456789abcdef']
],
];
}
/**
* @dataProvider mentionsProvider
*
* @param string $message
* @param array $expectedUids
* @param string|null $author
* @param array $expectedGuests
*/
public function testMentions($message, $expectedUids, $author = null) {
public function testMentions(string $message, array $expectedUids, ?string $author = null, array $expectedGuests = []): void {
$comment = new Comment();
$comment->setMessage($message);
if(!is_null($author)) {
@ -169,9 +177,15 @@ class CommentTest extends TestCase {
}
$mentions = $comment->getMentions();
while($mention = array_shift($mentions)) {
$uid = array_shift($expectedUids);
$this->assertSame('user', $mention['type']);
$this->assertSame($uid, $mention['id']);
if ($mention['type'] === 'user') {
$id = array_shift($expectedUids);
} else if ($mention['type'] === 'guest') {
$id = array_shift($expectedGuests);
} else {
$this->fail('Unexpected mention type');
continue;
}
$this->assertSame($id, $mention['id']);
}
$this->assertEmpty($mentions);
$this->assertEmpty($expectedUids);