Skip to content

Commit

Permalink
Always send notifications for one2one chats
Browse files Browse the repository at this point in the history
Signed-off-by: Joas Schilling <coding@schilljs.com>
  • Loading branch information
nickvergessen committed Jul 9, 2018
1 parent d0c5226 commit c796aec
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 3 deletions.
6 changes: 5 additions & 1 deletion lib/Chat/ChatManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,11 @@ public function sendMessage(Room $chat, $actorType, $actorId, $message, \DateTim

$this->commentsManager->save($comment);

$this->notifier->notifyMentionedUsers($chat, $comment);
if ($chat->getType() === Room::ONE_TO_ONE_CALL) {
$this->notifier->notifyOtherParticipant($chat, $comment);
} else {
$this->notifier->notifyMentionedUsers($chat, $comment);
}
return $comment;
}

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 @@ -90,6 +91,53 @@ public function notifyMentionedUsers(Room $chat, IComment $comment) {
}
}

/**
* 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
15 changes: 13 additions & 2 deletions lib/Notification/Notifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public function prepare(INotification $notification, $languageCode) {
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 @@ -163,7 +163,18 @@ 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(
$l->t('%s sent you in a private message', [$user->getDisplayName()])
)
->setRichSubject(
$l->t('{user} sent you in 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

0 comments on commit c796aec

Please sign in to comment.