Commit e20c7b08 authored by Misha Lavrenchuk's avatar Misha Lavrenchuk Committed by Artem Sylchuk
Browse files

Issue #2977313 by ReINFaTe, Denist3r, mike82, sagesolutions: Include a...

Issue #2977313 by ReINFaTe, Denist3r, mike82, sagesolutions: Include a function in PrivateMessageMapper to count the new messages instead of only unread threads
parent 8a96bbcf
Loading
Loading
Loading
Loading
+36 −0
Original line number Diff line number Diff line
@@ -291,6 +291,42 @@ class PrivateMessageMapper implements PrivateMessageMapperInterface {
    )->fetchField();
  }

  /**
   * {@inheritdoc}
   */
  public function getUnreadMessageCount($uid, $lastCheckTimestamp) {
    $query = $this->database->select('private_messages', 'message');
    $query->join(
      'private_message_thread__private_messages',
      'thread_message',
      'message.id = thread_message.private_messages_target_id'
    );
    $query->join(
      'private_message_threads',
      'thread',
      'thread_message.entity_id = thread.id'
    );
    $query->join(
      'pm_thread_history',
      'thread_history',
      'thread_history.thread_id = thread.id AND thread_history.uid = :uid',
      [':uid' => $uid]
    );
    $query->join(
      'private_message_thread__members',
      'thread_member',
      'thread_member.entity_id = thread.id AND thread_member.members_target_id = :uid',
      [':uid' => $uid]
    );
    $query
      ->condition('thread.updated ', $lastCheckTimestamp, '>')
      ->condition('message.created', $lastCheckTimestamp, '>')
      ->condition('message.owner', $uid, '<>')
      ->where('thread_history.access_timestamp < thread.updated');
    $query = $query->countQuery();
    return $query->execute()->fetchField();
  }

  /**
   * {@inheritdoc}
   */
+16 −0
Original line number Diff line number Diff line
@@ -139,6 +139,22 @@ interface PrivateMessageMapperInterface {
   */
  public function getUnreadThreadCount($uid, $lastCheckTimestamp);

  /**
   * Get the current user's unread message count.
   *
   * Retrieves the number of the current user's messages that have been updated
   * since the last time this number was checked.
   *
   * @param int $uid
   *   The user ID of the user whose count should be retrieved.
   * @param int $lastCheckTimestamp
   *   A UNIX timestamp indicating the time after which to check.
   *
   * @return int
   *   The number of threads updated since the given timestamp
   */
  public function getUnreadMessageCount($uid, $lastCheckTimestamp);

  /**
   * Load the thread id of the thread that a private message belongs to.
   *
+11 −0
Original line number Diff line number Diff line
@@ -303,6 +303,17 @@ class PrivateMessageService implements PrivateMessageServiceInterface {
    return (int) $this->mapper->getUnreadThreadCount($uid, $last_check_timestamp);
  }

  /**
   * {@inheritdoc}
   */
  public function getUnreadMessageCount() {
    $uid = $this->currentUser->id();
    $last_check_timestamp = $this->userData->get(self::MODULE_KEY, $uid, self::LAST_CHECK_KEY);
    $last_check_timestamp = is_numeric($last_check_timestamp) ? $last_check_timestamp : 0;

    return (int) $this->mapper->getUnreadMessageCount($uid, $last_check_timestamp);
  }

  /**
   * {@inheritdoc}
   */
+8 −0
Original line number Diff line number Diff line
@@ -170,6 +170,14 @@ interface PrivateMessageServiceInterface {
   */
  public function getUnreadThreadCount();

  /**
   * Get the current user's unread messages count.
   *
   * @return int
   *   The number of updated threads
   */
  public function getUnreadMessageCount();

  /**
   * Marks a timestamp at which all threads are considered read.
   */