diff --git a/lib/Chat/ChatManager.php b/lib/Chat/ChatManager.php index 81aa0f468bfe..9938f00f0b52 100644 --- a/lib/Chat/ChatManager.php +++ b/lib/Chat/ChatManager.php @@ -90,6 +90,7 @@ public function sendMessage($chatId, $actorType, $actorId, $message, \DateTime $ * maximum time to wait must be set using the $timeout parameter. * * @param string $chatId + * @param string $userId * @param int $timeout the maximum number of seconds to wait for messages * @param int $offset optional, starting point * @param \DateTime|null $notOlderThan optional, the date and time of the @@ -98,7 +99,7 @@ public function sendMessage($chatId, $actorType, $actorId, $message, \DateTime $ * creation date and message are relevant), or an empty array if the * timeout expired. */ - public function receiveMessages($chatId, $timeout, $offset = 0, \DateTime $notOlderThan = null) { + public function receiveMessages($chatId, $userId, $timeout, $offset = 0, \DateTime $notOlderThan = null) { $comments = []; $commentsFound = false; @@ -114,6 +115,8 @@ public function receiveMessages($chatId, $timeout, $offset = 0, \DateTime $notOl } } + $this->notifier->markMentionNotificationsRead($chatId, $userId); + if ($commentsFound) { // The limit and offset of getForObject can not be based on the // number of comments, as more comments may have been added between diff --git a/lib/Chat/Notifier.php b/lib/Chat/Notifier.php index 273d01922eeb..2471db3f5378 100644 --- a/lib/Chat/Notifier.php +++ b/lib/Chat/Notifier.php @@ -104,6 +104,24 @@ public function removePendingNotificationsForRoom($roomId) { $this->notificationManager->markProcessed($notification); } + /** + * Removes all the pending mention notifications for the room + * + * @param string $roomId + * @param string $userId + */ + public function markMentionNotificationsRead($roomId, $userId) { + $notification = $this->notificationManager->createNotification(); + + $notification + ->setApp('spreed') + ->setObject('room', $roomId) + ->setSubject('mention') + ->setUser($userId); + + $this->notificationManager->markProcessed($notification); + } + /** * Returns the IDs of the users mentioned in the given comment. * diff --git a/lib/Controller/ChatController.php b/lib/Controller/ChatController.php index b9073c07e158..fee134b0c906 100644 --- a/lib/Controller/ChatController.php +++ b/lib/Controller/ChatController.php @@ -206,7 +206,7 @@ public function receiveMessages($token, $offset = 0, $notOlderThanTimestamp = 0, $timeout = $maximumTimeout; } - $comments = $this->chatManager->receiveMessages((string) $room->getId(), $timeout, $offset, $notOlderThan); + $comments = $this->chatManager->receiveMessages((string) $room->getId(), $this->userId, $timeout, $offset, $notOlderThan); return new DataResponse(array_map(function(IComment $comment) use ($token) { $displayName = null; diff --git a/tests/php/Chat/ChatManagerTest.php b/tests/php/Chat/ChatManagerTest.php index 131b11d99ab0..22e470a704a7 100644 --- a/tests/php/Chat/ChatManagerTest.php +++ b/tests/php/Chat/ChatManagerTest.php @@ -121,8 +121,12 @@ public function testReceiveMessages() { $this->newComment(108, 'users', 'testUser', new \DateTime('@' . 1000000016), 'testMessage1') ]); + $this->notifier->expects($this->once()) + ->method('markMentionNotificationsRead') + ->with('testChatId', 'userId'); + $timeout = 42; - $comments = $this->chatManager->receiveMessages('testChatId', $timeout, $offset, $notOlderThan); + $comments = $this->chatManager->receiveMessages('testChatId', 'userId', $timeout, $offset, $notOlderThan); $expected = [ $this->newComment(110, 'users', 'testUnknownUser', new \DateTime('@' . 1000000042), 'testMessage3'), $this->newComment(109, 'guests', 'testSpreedSession', new \DateTime('@' . 1000000023), 'testMessage2') @@ -154,8 +158,12 @@ public function testReceiveMessagesMoreCommentsThanExpected() { $this->newComment(108, 'users', 'testUser', new \DateTime('@' . 1000000016), 'testMessage1') ]); + $this->notifier->expects($this->once()) + ->method('markMentionNotificationsRead') + ->with('testChatId', 'userId'); + $timeout = 42; - $comments = $this->chatManager->receiveMessages('testChatId', $timeout, $offset, $notOlderThan); + $comments = $this->chatManager->receiveMessages('testChatId', 'userId', $timeout, $offset, $notOlderThan); $expected = [ $this->newComment(111, 'users', 'testUser', new \DateTime('@' . 1000000108), 'testMessage4'), $this->newComment(110, 'users', 'testUnknownUser', new \DateTime('@' . 1000000042), 'testMessage3'), @@ -185,8 +193,12 @@ public function testReceiveMessagesNoOffset() { $this->newComment(108, 'users', 'testUser', new \DateTime('@' . 1000000016), 'testMessage1') ]); + $this->notifier->expects($this->once()) + ->method('markMentionNotificationsRead') + ->with('testChatId', 'userId'); + $timeout = 42; - $comments = $this->chatManager->receiveMessages('testChatId', $timeout, $offset, $notOlderThan); + $comments = $this->chatManager->receiveMessages('testChatId', 'userId', $timeout, $offset, $notOlderThan); $expected = [ $this->newComment(110, 'users', 'testUnknownUser', new \DateTime('@' . 1000000042), 'testMessage3'), $this->newComment(109, 'guests', 'testSpreedSession', new \DateTime('@' . 1000000023), 'testMessage2'), diff --git a/tests/php/Controller/ChatControllerTest.php b/tests/php/Controller/ChatControllerTest.php index e349a75d7298..827f4ac2c8b6 100644 --- a/tests/php/Controller/ChatControllerTest.php +++ b/tests/php/Controller/ChatControllerTest.php @@ -319,7 +319,7 @@ public function testReceiveMessagesByUser() { $timestamp = 1000000000; $this->chatManager->expects($this->once()) ->method('receiveMessages') - ->with('1234', $timeout, $offset, new \DateTime('@' . $timestamp)) + ->with('1234', $this->userId, $timeout, $offset, new \DateTime('@' . $timestamp)) ->willReturn([ $this->newComment(111, 'users', 'testUser', new \DateTime('@' . 1000000016), 'testMessage4'), $this->newComment(110, 'users', 'testUnknownUser', new \DateTime('@' . 1000000015), 'testMessage3'), @@ -377,7 +377,7 @@ public function testReceiveMessagesByUserNotJoinedButInRoom() { $timestamp = 1000000000; $this->chatManager->expects($this->once()) ->method('receiveMessages') - ->with('1234', $timeout, $offset, new \DateTime('@' . $timestamp)) + ->with('1234', $this->userId, $timeout, $offset, new \DateTime('@' . $timestamp)) ->willReturn([ $this->newComment(111, 'users', 'testUser', new \DateTime('@' . 1000000016), 'testMessage4'), $this->newComment(110, 'users', 'testUnknownUser', new \DateTime('@' . 1000000015), 'testMessage3'), @@ -465,7 +465,7 @@ public function testReceiveMessagesByGuest() { $timestamp = 1000000000; $this->chatManager->expects($this->once()) ->method('receiveMessages') - ->with('1234', $timeout, $offset, new \DateTime('@' . $timestamp)) + ->with('1234', null, $timeout, $offset, new \DateTime('@' . $timestamp)) ->willReturn([ $this->newComment(111, 'users', 'testUser', new \DateTime('@' . 1000000016), 'testMessage4'), $this->newComment(110, 'users', 'testUnknownUser', new \DateTime('@' . 1000000015), 'testMessage3'), @@ -546,7 +546,7 @@ public function testReceiveMessagesTimeoutExpired() { $timestamp = 1000000000; $this->chatManager->expects($this->once()) ->method('receiveMessages') - ->with('1234', $timeout, $offset, new \DateTime('@' . $timestamp)) + ->with('1234', $this->userId, $timeout, $offset, new \DateTime('@' . $timestamp)) ->willReturn([]); $response = $this->controller->receiveMessages('testToken', $offset, $timestamp, $timeout); @@ -579,7 +579,7 @@ public function testReceiveMessagesTimeoutTooLarge() { $timestamp = 1000000000; $this->chatManager->expects($this->once()) ->method('receiveMessages') - ->with('1234', $maximumTimeout, $offset, new \DateTime('@' . $timestamp)) + ->with('1234', $this->userId, $maximumTimeout, $offset, new \DateTime('@' . $timestamp)) ->willReturn([]); $response = $this->controller->receiveMessages('testToken', $offset, $timestamp, $timeout);