Skip to content

Commit

Permalink
Issue #3445902 by bramtenhove: Apply static caching to the SocialPriv…
Browse files Browse the repository at this point in the history
…ateMessageService
  • Loading branch information
bramtenhove committed May 8, 2024
1 parent 5e91473 commit fc0858c
Showing 1 changed file with 31 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,41 +63,46 @@ public function deleteUserDataThreadInfo(EntityBase $entity) {
* The number of unread threads.
*/
public function updateUnreadCount() {
$unread = 0;
$unread = &drupal_static(__FUNCTION__);

// Get the user.
$uid = $this->currentUser->id();
/** @var \Drupal\user\UserStorageInterface $user_storage */
$user_storage = $this->userManager;
/** @var \Drupal\user\UserInterface $user */
$user = $user_storage->load($uid);
if (!isset($unread)) {
$unread = 0;

// Get all the thread id's for this user.
$threads = $this->mapper->getThreadIdsForUser($user);
// Get the user.
$uid = $this->currentUser->id();
/** @var \Drupal\user\UserStorageInterface $user_storage */
$user_storage = $this->userManager;
/** @var \Drupal\user\UserInterface $user */
$user = $user_storage->load($uid);

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

// 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;
}
if (empty($threads)) {
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 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;
}

// Check if someone send a message after your last check.
if ($last_message > $thread_last_check) {
$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;
}

Expand Down

0 comments on commit fc0858c

Please sign in to comment.