Skip to content

Commit

Permalink
Mark mention notifications as read, when the user pulls the chat mess…
Browse files Browse the repository at this point in the history
…ages of the room

Signed-off-by: Joas Schilling <coding@schilljs.com>
  • Loading branch information
nickvergessen committed Nov 20, 2017
1 parent f4308ef commit 6346c5a
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 10 deletions.
5 changes: 4 additions & 1 deletion lib/Chat/ChatManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand All @@ -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
Expand Down
18 changes: 18 additions & 0 deletions lib/Chat/Notifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
2 changes: 1 addition & 1 deletion lib/Controller/ChatController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
18 changes: 15 additions & 3 deletions tests/php/Chat/ChatManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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'),
Expand Down Expand Up @@ -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'),
Expand Down
10 changes: 5 additions & 5 deletions tests/php/Controller/ChatControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down Expand Up @@ -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'),
Expand Down Expand Up @@ -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'),
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 6346c5a

Please sign in to comment.