summaryrefslogtreecommitdiffstats
path: root/apps/comments
diff options
context:
space:
mode:
authorArthur Schiwon <blizzz@arthur-schiwon.de>2016-10-14 00:19:31 +0200
committerArthur Schiwon <blizzz@arthur-schiwon.de>2016-10-19 00:33:55 +0200
commitfea3e20a805de12546312c68557ddbd8498b9414 (patch)
tree4cc3f481c51f3feab18c29164a57acbe70d86f5f /apps/comments
parente115bf96e742909b78150d04305b67196b94115c (diff)
downloadnextcloud-server-fea3e20a805de12546312c68557ddbd8498b9414.tar.gz
nextcloud-server-fea3e20a805de12546312c68557ddbd8498b9414.zip
move mention extraction to (I)Comment and report mentions via DAV
Signed-off-by: Arthur Schiwon <blizzz@arthur-schiwon.de>
Diffstat (limited to 'apps/comments')
-rw-r--r--apps/comments/lib/Notification/Listener.php30
-rw-r--r--apps/comments/tests/Unit/Notification/ListenerTest.php142
2 files changed, 30 insertions, 142 deletions
diff --git a/apps/comments/lib/Notification/Listener.php b/apps/comments/lib/Notification/Listener.php
index 426e85cac83..d30c59c93d5 100644
--- a/apps/comments/lib/Notification/Listener.php
+++ b/apps/comments/lib/Notification/Listener.php
@@ -61,7 +61,7 @@ class Listener {
public function evaluate(CommentsEvent $event) {
$comment = $event->getComment();
- $mentions = $this->extractMentions($comment->getMessage());
+ $mentions = $this->extractMentions($comment->getMentions());
if(empty($mentions)) {
// no one to notify
return;
@@ -69,16 +69,15 @@ class Listener {
$notification = $this->instantiateNotification($comment);
- foreach($mentions as $mention) {
- $user = substr($mention, 1); // @username → username
- if( ($comment->getActorType() === 'users' && $user === $comment->getActorId())
- || !$this->userManager->userExists($user)
+ foreach($mentions as $uid) {
+ if( ($comment->getActorType() === 'users' && $uid === $comment->getActorId())
+ || !$this->userManager->userExists($uid)
) {
// do not notify unknown users or yourself
continue;
}
- $notification->setUser($user);
+ $notification->setUser($uid);
if( $event->getEvent() === CommentsEvent::EVENT_DELETE
|| $event->getEvent() === CommentsEvent::EVENT_PRE_UPDATE)
{
@@ -111,16 +110,21 @@ class Listener {
}
/**
- * extracts @-mentions out of a message body.
+ * flattens the mention array returned from comments to a list of user ids.
*
- * @param string $message
- * @return string[] containing the mentions, e.g. ['@alice', '@bob']
+ * @param array $mentions
+ * @return string[] containing the mentions, e.g. ['alice', 'bob']
*/
- public function extractMentions($message) {
- $ok = preg_match_all('/\B@[a-z0-9_\-@\.\']+/i', $message, $mentions);
- if(!$ok || !isset($mentions[0]) || !is_array($mentions[0])) {
+ public function extractMentions(array $mentions) {
+ if(empty($mentions)) {
return [];
}
- return array_unique($mentions[0]);
+ $uids = [];
+ foreach($mentions as $mention) {
+ if($mention['type'] === 'user') {
+ $uids[] = $mention['id'];
+ }
+ }
+ return $uids;
}
}
diff --git a/apps/comments/tests/Unit/Notification/ListenerTest.php b/apps/comments/tests/Unit/Notification/ListenerTest.php
index 12f388fcff9..3007b78cb3d 100644
--- a/apps/comments/tests/Unit/Notification/ListenerTest.php
+++ b/apps/comments/tests/Unit/Notification/ListenerTest.php
@@ -72,10 +72,6 @@ class ListenerTest extends TestCase {
* @param string $notificationMethod
*/
public function testEvaluate($eventType, $notificationMethod) {
- $message = '@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!';
-
/** @var IComment|\PHPUnit_Framework_MockObject_MockObject $comment */
$comment = $this->getMockBuilder('\OCP\Comments\IComment')->getMock();
$comment->expects($this->any())
@@ -85,8 +81,15 @@ class ListenerTest extends TestCase {
->method('getCreationDateTime')
->will($this->returnValue(new \DateTime()));
$comment->expects($this->once())
- ->method('getMessage')
- ->will($this->returnValue($message));
+ ->method('getMentions')
+ ->willReturn([
+ [ 'type' => 'user', 'id' => 'foobar'],
+ [ 'type' => 'user', 'id' => 'barfoo'],
+ [ 'type' => 'user', 'id' => 'foo@bar.com'],
+ [ 'type' => 'user', 'id' => 'bar@foo.org@foobar.io'],
+ [ 'type' => 'user', 'id' => '23452-4333-54353-2342'],
+ [ 'type' => 'user', 'id' => 'yolo'],
+ ]);
/** @var CommentsEvent|\PHPUnit_Framework_MockObject_MockObject $event */
$event = $this->getMockBuilder('\OCP\Comments\CommentsEvent')
@@ -134,8 +137,6 @@ class ListenerTest extends TestCase {
* @param string $eventType
*/
public function testEvaluateNoMentions($eventType) {
- $message = 'a boring comment without mentions';
-
/** @var IComment|\PHPUnit_Framework_MockObject_MockObject $comment */
$comment = $this->getMockBuilder('\OCP\Comments\IComment')->getMock();
$comment->expects($this->any())
@@ -145,8 +146,8 @@ class ListenerTest extends TestCase {
->method('getCreationDateTime')
->will($this->returnValue(new \DateTime()));
$comment->expects($this->once())
- ->method('getMessage')
- ->will($this->returnValue($message));
+ ->method('getMentions')
+ ->willReturn([]);
/** @var CommentsEvent|\PHPUnit_Framework_MockObject_MockObject $event */
$event = $this->getMockBuilder('\OCP\Comments\CommentsEvent')
@@ -173,8 +174,6 @@ class ListenerTest extends TestCase {
}
public function testEvaluateUserDoesNotExist() {
- $message = '@foobar bla bla bla';
-
/** @var IComment|\PHPUnit_Framework_MockObject_MockObject $comment */
$comment = $this->getMockBuilder('\OCP\Comments\IComment')->getMock();
$comment->expects($this->any())
@@ -184,8 +183,8 @@ class ListenerTest extends TestCase {
->method('getCreationDateTime')
->will($this->returnValue(new \DateTime()));
$comment->expects($this->once())
- ->method('getMessage')
- ->will($this->returnValue($message));
+ ->method('getMentions')
+ ->willReturn([[ 'type' => 'user', 'id' => 'foobar']]);
/** @var CommentsEvent|\PHPUnit_Framework_MockObject_MockObject $event */
$event = $this->getMockBuilder('\OCP\Comments\CommentsEvent')
@@ -221,119 +220,4 @@ class ListenerTest extends TestCase {
$this->listener->evaluate($event);
}
-
- /**
- * @dataProvider eventProvider
- * @param string $eventType
- * @param string $notificationMethod
- */
- public function testEvaluateOneMentionPerUser($eventType, $notificationMethod) {
- $message = '@foobar bla bla bla @foobar';
-
- /** @var IComment|\PHPUnit_Framework_MockObject_MockObject $comment */
- $comment = $this->getMockBuilder('\OCP\Comments\IComment')->getMock();
- $comment->expects($this->any())
- ->method('getObjectType')
- ->will($this->returnValue('files'));
- $comment->expects($this->any())
- ->method('getCreationDateTime')
- ->will($this->returnValue(new \DateTime()));
- $comment->expects($this->once())
- ->method('getMessage')
- ->will($this->returnValue($message));
-
- /** @var CommentsEvent|\PHPUnit_Framework_MockObject_MockObject $event */
- $event = $this->getMockBuilder('\OCP\Comments\CommentsEvent')
- ->disableOriginalConstructor()
- ->getMock();
- $event->expects($this->once())
- ->method('getComment')
- ->will($this->returnValue($comment));
- $event->expects(($this->any()))
- ->method(('getEvent'))
- ->will($this->returnValue($eventType));
-
- /** @var INotification|\PHPUnit_Framework_MockObject_MockObject $notification */
- $notification = $this->getMockBuilder('\OCP\Notification\INotification')->getMock();
- $notification->expects($this->any())
- ->method($this->anything())
- ->will($this->returnValue($notification));
- $notification->expects($this->once())
- ->method('setUser');
-
- $this->notificationManager->expects($this->once())
- ->method('createNotification')
- ->will($this->returnValue($notification));
- $this->notificationManager->expects($this->once())
- ->method($notificationMethod)
- ->with($this->isInstanceOf('\OCP\Notification\INotification'));
-
- $this->userManager->expects($this->once())
- ->method('userExists')
- ->withConsecutive(
- ['foobar']
- )
- ->will($this->returnValue(true));
-
- $this->listener->evaluate($event);
- }
-
- /**
- * @dataProvider eventProvider
- * @param string $eventType
- */
- public function testEvaluateNoSelfMention($eventType) {
- $message = '@foobar bla bla bla';
-
- /** @var IComment|\PHPUnit_Framework_MockObject_MockObject $comment */
- $comment = $this->getMockBuilder('\OCP\Comments\IComment')->getMock();
- $comment->expects($this->any())
- ->method('getObjectType')
- ->will($this->returnValue('files'));
- $comment->expects($this->any())
- ->method('getActorType')
- ->will($this->returnValue('users'));
- $comment->expects($this->any())
- ->method('getActorId')
- ->will($this->returnValue('foobar'));
- $comment->expects($this->any())
- ->method('getCreationDateTime')
- ->will($this->returnValue(new \DateTime()));
- $comment->expects($this->once())
- ->method('getMessage')
- ->will($this->returnValue($message));
-
- /** @var CommentsEvent|\PHPUnit_Framework_MockObject_MockObject $event */
- $event = $this->getMockBuilder('\OCP\Comments\CommentsEvent')
- ->disableOriginalConstructor()
- ->getMock();
- $event->expects($this->once())
- ->method('getComment')
- ->will($this->returnValue($comment));
- $event->expects(($this->any()))
- ->method(('getEvent'))
- ->will($this->returnValue($eventType));
-
- /** @var INotification|\PHPUnit_Framework_MockObject_MockObject $notification */
- $notification = $this->getMockBuilder('\OCP\Notification\INotification')->getMock();
- $notification->expects($this->any())
- ->method($this->anything())
- ->will($this->returnValue($notification));
- $notification->expects($this->never())
- ->method('setUser');
-
- $this->notificationManager->expects($this->once())
- ->method('createNotification')
- ->will($this->returnValue($notification));
- $this->notificationManager->expects($this->never())
- ->method('notify');
- $this->notificationManager->expects($this->never())
- ->method('markProcessed');
-
- $this->userManager->expects($this->never())
- ->method('userExists');
-
- $this->listener->evaluate($event);
- }
-
}