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

Always notify in one2one rooms #1029

Merged
merged 1 commit into from
Aug 2, 2018
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
4 changes: 3 additions & 1 deletion lib/Chat/ChatManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
use OCP\Comments\IComment;
use OCP\Comments\ICommentsManager;
use OCP\Comments\NotFoundException;
use OCP\IDBConnection;
use OCP\IUser;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\GenericEvent;
Expand Down Expand Up @@ -118,6 +117,9 @@ public function sendMessage(Room $chat, $actorType, $actorId, $message, \DateTim
$notifiedUsers = $this->notifier->notifyMentionedUsers($chat, $comment);
if (!empty($notifiedUsers)) {
$chat->markUsersAsMentioned($notifiedUsers, $creationDateTime);
} else if ($chat->getType() === Room::ONE_TO_ONE_CALL) {
// User was not mentioned, send a normal notification
$this->notifier->notifyOtherParticipant($chat, $comment);
}

$this->dispatcher->dispatch(self::class . '::sendMessage', new GenericEvent($chat, [
Expand Down
48 changes: 48 additions & 0 deletions lib/Chat/Notifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use OCA\Spreed\Exceptions\ParticipantNotFoundException;
use OCA\Spreed\Exceptions\RoomNotFoundException;
use OCA\Spreed\Manager;
use OCA\Spreed\Participant;
use OCA\Spreed\Room;
use OCP\Comments\IComment;
use OCP\Notification\IManager as INotificationManager;
Expand Down Expand Up @@ -95,6 +96,53 @@ public function notifyMentionedUsers(Room $chat, IComment $comment): array {
return $notifiedUsers;
}

/**
* Notifies the user mentioned in the comment.
*
* The comment must be a chat message comment. That is, its "objectId" must
* be the room ID.
*
* Not every user mentioned in the message is notified, but only those that
* are able to participate in the room.
*
* @param Room $chat
* @param IComment $comment
*/
public function notifyOtherParticipant(Room $chat, IComment $comment) {
$participants = $chat->getParticipants();

foreach ($participants['users'] as $userId => $participant) {
if ($userId === $comment->getActorId()) {
// Do not notify the author
continue;
}

if ($participant['sessionId'] && $participant['sessionId'] !== '0') {
// User is online
continue;
}

$notification = $this->notificationManager->createNotification();
$notification
->setApp('spreed')
->setObject('chat', $chat->getToken())
->setUser($userId)
->setSubject('chat', [
'userType' => $comment->getActorType(),
'userId' => $comment->getActorId(),
])
->setDateTime($comment->getCreationDateTime());

if (strlen($comment->getMessage()) > 64) {
$notification->setMessage(substr($comment->getMessage(), 0, 64), ['ellipsisEnd']);
} else {
$notification->setMessage($comment->getMessage());
}

$this->notificationManager->notify($notification);
}
}

/**
* Removes all the pending notifications for the room with the given ID.
*
Expand Down
13 changes: 11 additions & 2 deletions lib/Notification/Notifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public function prepare(INotification $notification, $languageCode): INotificati
if ($subject === 'call') {
return $this->parseCall($notification, $room, $l);
}
if ($subject === 'mention') {
if ($subject === 'mention' || $subject === 'chat') {
return $this->parseMention($notification, $room, $l);
}

Expand Down Expand Up @@ -157,7 +157,16 @@ protected function parseMention(INotification $notification, Room $room, IL10N $
}
$notification->setParsedMessage($parsedMessage);

if ($room->getType() === Room::ONE_TO_ONE_CALL) {
if ($notification->getSubject() === 'chat') {
$notification
->setParsedSubject(str_replace('{user}', $user->getDisplayName(), $l->t('{user} sent you a private message')))
->setRichSubject(
$l->t('{user} sent you a private message'), [
'user' => $richSubjectUser
]
);

} else if ($room->getType() === Room::ONE_TO_ONE_CALL) {
$notification
->setParsedSubject(
$l->t('%s mentioned you in a private conversation', [$user->getDisplayName()])
Expand Down
2 changes: 1 addition & 1 deletion tests/php/Notification/NotifierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ public function testPrepareMention($roomType, $subjectParameters, $messageParame
$notification->expects($this->once())
->method('getApp')
->willReturn('spreed');
$notification->expects($this->once())
$notification->expects($this->exactly(2))
->method('getSubject')
->willReturn('mention');
$notification->expects($this->once())
Expand Down