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 <[email protected]>
  • Loading branch information
nickvergessen committed Jul 31, 2018
1 parent fe64e04 commit ad8afc6
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 5 deletions.
6 changes: 5 additions & 1 deletion lib/Chat/ChatManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,11 @@ public function sendMessage(Room $chat, $actorType, $actorId, $message, \DateTim
// Update last_message
$chat->setLastMessage($comment);

$this->notifier->notifyMentionedUsers($chat, $comment);
$usersNotified = $this->notifier->notifyMentionedUsers($chat, $comment);
if ($usersNotified === 0 || $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, [
'comment' => $comment,
Expand Down
57 changes: 55 additions & 2 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 @@ -74,19 +75,71 @@ public function __construct(INotificationManager $notificationManager,
*
* @param Room $chat
* @param IComment $comment
* @return int Number of users notified
*/
public function notifyMentionedUsers(Room $chat, IComment $comment) {
public function notifyMentionedUsers(Room $chat, IComment $comment): int {
$mentionedUserIds = $this->getMentionedUserIds($comment);
if (empty($mentionedUserIds)) {
return;
return 0;
}

$usersNotified = 0;
foreach ($mentionedUserIds as $mentionedUserId) {
if ($this->shouldUserBeNotified($mentionedUserId, $comment)) {
$notification = $this->createNotification($chat, $comment, $mentionedUserId);

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

return $usersNotified;
}

/**
* 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);
}
}

Expand Down
15 changes: 13 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,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 ad8afc6

Please sign in to comment.