Skip to content

Commit

Permalink
Merge pull request nextcloud#6675 from nextcloud/refactor-notifier
Browse files Browse the repository at this point in the history
Refactor notifyMentionedUsers method
  • Loading branch information
nickvergessen authored Jan 20, 2022
2 parents 28e50df + 7a412ab commit f4edb7f
Show file tree
Hide file tree
Showing 2 changed files with 273 additions and 447 deletions.
93 changes: 70 additions & 23 deletions lib/Chat/Notifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,28 +95,18 @@ public function __construct(INotificationManager $notificationManager,
* @return string[] Users that were mentioned
*/
public function notifyMentionedUsers(Room $chat, IComment $comment, array $alreadyNotifiedUsers): array {
$mentionedUserIds = $this->getMentionedUserIds($comment);
if (empty($mentionedUserIds)) {
return $alreadyNotifiedUsers;
}
$usersToNotify = $this->getUsersToNotify($chat, $comment, $alreadyNotifiedUsers);

$mentionedAll = array_search('all', $mentionedUserIds, true);

if ($mentionedAll !== false) {
$mentionedUserIds = array_unique(array_merge($mentionedUserIds, $this->participantService->getParticipantUserIds($chat)));
if (!$usersToNotify) {
return $alreadyNotifiedUsers;
}

$notification = $this->createNotification($chat, $comment, 'mention');
$shouldFlush = $this->notificationManager->defer();
foreach ($mentionedUserIds as $mentionedUserId) {
if (in_array($mentionedUserId, $alreadyNotifiedUsers, true)) {
continue;
}

if ($this->shouldMentionedUserBeNotified($mentionedUserId, $comment)) {
$notification->setUser($mentionedUserId);
foreach ($usersToNotify as $mentionedUser) {
if ($this->shouldMentionedUserBeNotified($mentionedUser['id'], $comment)) {
$notification->setUser($mentionedUser['id']);
$this->notificationManager->notify($notification);
$alreadyNotifiedUsers[] = $mentionedUserId;
$alreadyNotifiedUsers[] = $mentionedUser['id'];
}
}

Expand All @@ -127,6 +117,48 @@ public function notifyMentionedUsers(Room $chat, IComment $comment, array $alrea
return $alreadyNotifiedUsers;
}

private function getUsersToNotify(Room $chat, IComment $comment, array $alreadyNotifiedUsers): array {
$usersToNotify = $this->getMentionedUsers($comment);
$usersToNotify = $this->removeAlreadyNotifiedUsers($usersToNotify, $alreadyNotifiedUsers);
$usersToNotify = $this->addMentionAllToList($chat, $usersToNotify);

return $usersToNotify;
}

private function removeAlreadyNotifiedUsers(array $usersToNotify, array $alreadyNotifiedUsers): array {
return array_filter($usersToNotify, static function (array $user) use ($alreadyNotifiedUsers): bool {
return !in_array($user['id'], $alreadyNotifiedUsers, true);
});
}

private function addMentionAllToList(Room $chat, array $list): array {
$usersToNotify = array_filter($list, static function (array $user): bool {
return $user['id'] !== 'all';
});

if (count($list) === count($usersToNotify)) {
return $usersToNotify;
}

$chatParticipants = $this->participantService->getActorsByType($chat, Attendee::ACTOR_USERS);
foreach ($chatParticipants as $participant) {
$alreadyAddedToNotify = array_filter($list, static function ($user) use ($participant): bool {
return $user['id'] === $participant->getActorId();
});
if (!empty($alreadyAddedToNotify)) {
continue;
}

$usersToNotify[] = [
'id' => $participant->getActorId(),
'type' => $participant->getActorType()
];
}

return $usersToNotify;
}


/**
* Notifies the author that wrote the comment which was replied to
*
Expand All @@ -138,7 +170,8 @@ public function notifyMentionedUsers(Room $chat, IComment $comment, array $alrea
* @param Room $chat
* @param IComment $comment
* @param IComment $replyTo
* @return string[] Users that were mentioned
* @return [] Users that were mentioned
* @psalm-return array|array{array{id: string, type: string}}
*/
public function notifyReplyToAuthor(Room $chat, IComment $comment, IComment $replyTo): array {
if ($replyTo->getActorType() !== Attendee::ACTOR_USERS) {
Expand All @@ -154,7 +187,12 @@ public function notifyReplyToAuthor(Room $chat, IComment $comment, IComment $rep
$notification->setUser($replyTo->getActorId());
$this->notificationManager->notify($notification);

return [$replyTo->getActorId()];
return [
[
'id' => $replyTo->getActorId(),
'type' => $replyTo->getActorType(),
],
];
}

/**
Expand Down Expand Up @@ -257,20 +295,29 @@ public function markMentionNotificationsRead(Room $chat, ?string $userId): void
* @return string[] the mentioned user IDs
*/
public function getMentionedUserIds(IComment $comment): array {
$mentionedUsers = $this->getMentionedUsers($comment);
return array_map(static function ($mentionedUser) {
return $mentionedUser['id'];
}, $mentionedUsers);
}

private function getMentionedUsers(IComment $comment): array {
$mentions = $comment->getMentions();

if (empty($mentions)) {
return [];
}

$userIds = [];
$mentionedUsers = [];
foreach ($mentions as $mention) {
if ($mention['type'] === 'user') {
$userIds[] = $mention['id'];
$mentionedUsers[] = [
'id' => $mention['id'],
'type' => 'users'
];
}
}

return $userIds;
return $mentionedUsers;
}

/**
Expand Down
Loading

0 comments on commit f4edb7f

Please sign in to comment.