You are here

public function PrivateMessageService::getPreviousMessages in Private Message 8.2

Same name and namespace in other branches
  1. 8 src/Service/PrivateMessageService.php \Drupal\private_message\Service\PrivateMessageService::getPreviousMessages()

Retrieve old messages for a user that were created before the given ID.

Parameters

int $threadId: The ID of the thread from which messages should be retrieved.

int $messageId: The ID before which messages should be retrieved.

Return value

array An array containing the following to keys:

Overrides PrivateMessageServiceInterface::getPreviousMessages

File

src/Service/PrivateMessageService.php, line 204

Class

PrivateMessageService
The Private Message service for the private message module.

Namespace

Drupal\private_message\Service

Code

public function getPreviousMessages($threadId, $messageId) {
  $return = [
    'messages' => [],
    'next_exists' => FALSE,
  ];
  $private_message_thread = $this->pmThreadManager
    ->load($threadId);
  if ($private_message_thread && $private_message_thread
    ->isMember($this->currentUser
    ->id())) {
    $user = $this->userManager
      ->load($this->currentUser
      ->id());
    $messages = $private_message_thread
      ->filterUserDeletedMessages($user);
    $start_index = FALSE;
    $settings = $this->configFactory
      ->get('core.entity_view_display.private_message_thread.private_message_thread.default')
      ->get('content.private_messages.settings');
    $count = $settings['ajax_previous_load_count'];
    $total = count($messages);
    foreach ($messages as $index => $message) {
      if ($message
        ->id() >= $messageId) {
        $start_index = $index - $count >= 0 ? $index - $count : 0;
        $slice_count = $index > $count ? $count : $index;
        break;
      }
    }
    if ($start_index !== FALSE) {
      $messages = array_splice($messages, $start_index, $slice_count);
      if (count($messages)) {
        $order = $settings['message_order'];
        if ($order == 'desc') {
          $messages = array_reverse($messages);
        }
        $return['messages'] = $messages;
        $old_messages = $total - ($start_index + $slice_count);
        $return['next_exists'] = $old_messages + count($messages) < $total;
      }
    }
  }
  return $return;
}