You are here

public function SocialPrivateMessageService::updateUnreadCount in Open Social 10.0.x

Same name and namespace in other branches
  1. 8.9 modules/social_features/social_private_message/src/Service/SocialPrivateMessageService.php \Drupal\social_private_message\Service\SocialPrivateMessageService::updateUnreadCount()
  2. 8 modules/social_features/social_private_message/src/Service/SocialPrivateMessageService.php \Drupal\social_private_message\Service\SocialPrivateMessageService::updateUnreadCount()
  3. 8.2 modules/social_features/social_private_message/src/Service/SocialPrivateMessageService.php \Drupal\social_private_message\Service\SocialPrivateMessageService::updateUnreadCount()
  4. 8.3 modules/social_features/social_private_message/src/Service/SocialPrivateMessageService.php \Drupal\social_private_message\Service\SocialPrivateMessageService::updateUnreadCount()
  5. 8.4 modules/social_features/social_private_message/src/Service/SocialPrivateMessageService.php \Drupal\social_private_message\Service\SocialPrivateMessageService::updateUnreadCount()
  6. 8.5 modules/social_features/social_private_message/src/Service/SocialPrivateMessageService.php \Drupal\social_private_message\Service\SocialPrivateMessageService::updateUnreadCount()
  7. 8.6 modules/social_features/social_private_message/src/Service/SocialPrivateMessageService.php \Drupal\social_private_message\Service\SocialPrivateMessageService::updateUnreadCount()
  8. 8.7 modules/social_features/social_private_message/src/Service/SocialPrivateMessageService.php \Drupal\social_private_message\Service\SocialPrivateMessageService::updateUnreadCount()
  9. 8.8 modules/social_features/social_private_message/src/Service/SocialPrivateMessageService.php \Drupal\social_private_message\Service\SocialPrivateMessageService::updateUnreadCount()
  10. 10.3.x modules/social_features/social_private_message/src/Service/SocialPrivateMessageService.php \Drupal\social_private_message\Service\SocialPrivateMessageService::updateUnreadCount()
  11. 10.1.x modules/social_features/social_private_message/src/Service/SocialPrivateMessageService.php \Drupal\social_private_message\Service\SocialPrivateMessageService::updateUnreadCount()
  12. 10.2.x modules/social_features/social_private_message/src/Service/SocialPrivateMessageService.php \Drupal\social_private_message\Service\SocialPrivateMessageService::updateUnreadCount()

Update the unread thread count.

Return value

int The number of unread threads.

File

modules/social_features/social_private_message/src/Service/SocialPrivateMessageService.php, line 65

Class

SocialPrivateMessageService
Class SocialPrivateMessageService.

Namespace

Drupal\social_private_message\Service

Code

public function updateUnreadCount() {
  $unread = 0;

  // Get the user.
  $uid = $this->currentUser
    ->id();

  // Get all the thread id's for this user.
  $threads = $this
    ->getAllThreadIdsForUser($uid);
  if (empty($threads)) {
    return $unread;
  }

  // Check the last time someone other than the current user added
  // something to the threads.
  $thread_last_messages = $this
    ->getLastMessagesFromOtherUsers($uid, $threads);
  if (empty($thread_last_messages)) {
    return $unread;
  }
  foreach ($thread_last_messages as $thread_id => $last_message) {

    // Check if the user has a timestamp on the thread.
    $thread_last_check = $this->userData
      ->get('private_message', $uid, 'private_message_thread:' . $thread_id);
    if ($thread_last_check === NULL) {
      $thread_last_check = 0;
    }

    // Check if someone send a message after your last check.
    if ($last_message > $thread_last_check) {
      $unread++;
    }
  }
  return $unread;
}