You are here

private function PrivateMessageNotifier::shouldSend in Private Message 8.2

Determines if the message should be sent.

Checks individual user preferences as well as system defaults.

Parameters

\Drupal\Core\Session\AccountInterface $recipient: The potential recipient.

\Drupal\private_message\Entity\PrivateMessageInterface $message: The private message for which a notification is being sent.

\Drupal\private_message\Entity\PrivateMessageThreadInterface $thread: The private message thread.

Return value

bool A boolean indicating whether or not the message should be sent.

1 call to PrivateMessageNotifier::shouldSend()
PrivateMessageNotifier::notify in src/Service/PrivateMessageNotifier.php
Send a private message notification email.

File

src/Service/PrivateMessageNotifier.php, line 133

Class

PrivateMessageNotifier
A service class for sending notifications of private messages.

Namespace

Drupal\private_message\Service

Code

private function shouldSend(AccountInterface $recipient, PrivateMessageInterface $message, PrivateMessageThreadInterface $thread) {

  // Don't notify the user by default.
  $notify = FALSE;

  // Check if notifications have been enabled.
  if ($this->config
    ->get('enable_notifications')) {

    // Eligibility to receive notifications will be checked.
    $eligible_to_receive = FALSE;

    // Get the user default.
    $user_default = $this->userData
      ->get('private_message', $recipient
      ->id(), 'receive_notification');

    // Check if the user default is to notify.
    if ($user_default) {
      $eligible_to_receive = TRUE;
    }
    elseif (is_null($user_default) && $this->config
      ->get('notify_by_default')) {
      $eligible_to_receive = TRUE;
    }

    // If the user is eligible to receive notification, user and system
    // settings are  used to determine whether or not the notification should
    // be sent.
    if ($eligible_to_receive) {

      // Determine whether a user should always be notified of every message,
      // or if they should only be notified when they aren't viewing a thread.
      $notify_when_using = $this->userData
        ->get('private_message', $recipient
        ->id(), 'notify_when_using');

      // Check if the user has not yet set a value.
      if (is_null($notify_when_using)) {

        // The user has not yet set a value, so use the system default.
        $notify_when_using = $this->config
          ->get('notify_when_using');
      }

      // Get the number of seconds a user has set in their profile, after
      // which they should be considered 'away' from the thread.
      $away_time = $this->userData
        ->get('private_message', $recipient
        ->id(), 'number_of_seconds_considered_away');

      // Check if the user has not yet set a value.
      if (is_null($away_time)) {

        // The user has not yet set a value, so use the system default.
        $away_time = $this->config
          ->get('number_of_seconds_considered_away');
      }

      // Check if users should always be notified.
      if ($notify_when_using == 'yes') {
        $notify = TRUE;
      }
      elseif ($message
        ->getCreatedTime() - $thread
        ->getLastAccessTimestamp($recipient) > $away_time) {
        $notify = TRUE;
      }
    }
  }
  return $notify;
}