You are here

class PrivateMessageService in Private Message 8.2

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

The Private Message service for the private message module.

Hierarchy

Expanded class hierarchy of PrivateMessageService

1 file declares its use of PrivateMessageService
PrivateMessageCommands.php in src/Commands/PrivateMessageCommands.php
1 string reference to 'PrivateMessageService'
private_message.services.yml in ./private_message.services.yml
private_message.services.yml

File

src/Service/PrivateMessageService.php, line 22

Namespace

Drupal\private_message\Service
View source
class PrivateMessageService implements PrivateMessageServiceInterface {

  /**
   * The private message mapper service.
   *
   * @var \Drupal\private_message\Mapper\PrivateMessageMapperInterface
   */
  protected $mapper;

  /**
   * The current user.
   *
   * @var \Drupal\Core\Session\AccountProxyInterface
   */
  protected $currentUser;

  /**
   * The configuration factory.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * The user data service.
   *
   * @var \Drupal\user\UserDataInterface
   */
  protected $userData;

  /**
   * Cache Tags Invalidator.
   *
   * @var \Drupal\Core\Cache\CacheTagsInvalidatorInterface
   */
  protected $cacheTagsInvalidator;

  /**
   * The private message thread manager.
   *
   * @var \Drupal\Core\Entity\Sql\SqlContentEntityStorage
   */
  protected $pmThreadManager;

  /**
   * The user entity manager.
   *
   * @var \Crupal\user\UserStorageInterface
   */
  protected $userManager;

  /**
   * The time service.
   *
   * @var \Drupal\Component\Datetime\TimeInterface
   */
  protected $time;

  /**
   * Constructs a PrivateMessageService object.
   *
   * @param Drupal\private_message\Mapper\PrivateMessageMapperInterface $mapper
   *   The private message mapper service.
   * @param \Drupal\Core\Session\AccountProxyInterface $currentUser
   *   The current user.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory
   *   The configuration factory.
   * @param \Drupal\user\UserDataInterface $userData
   *   The user data service.
   * @param \Drupal\Core\Cache\CacheTagsInvalidatorInterface $cacheTagsInvalidator
   *   The cache tags invalidator interface.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
   *   The entity type manager interface.
   * @param \Drupal\Component\Datetime\TimeInterface $time
   *   The time service.
   */
  public function __construct(PrivateMessageMapperInterface $mapper, AccountProxyInterface $currentUser, ConfigFactoryInterface $configFactory, UserDataInterface $userData, CacheTagsInvalidatorInterface $cacheTagsInvalidator, EntityTypeManagerInterface $entityTypeManager, TimeInterface $time) {
    $this->mapper = $mapper;
    $this->currentUser = $currentUser;
    $this->configFactory = $configFactory;
    $this->userData = $userData;
    $this->cacheTagsInvalidator = $cacheTagsInvalidator;
    $this->pmThreadManager = $entityTypeManager
      ->getStorage('private_message_thread');
    $this->userManager = $entityTypeManager
      ->getStorage('user');
    $this->time = $time;
  }

  /**
   * {@inheritdoc}
   */
  public function getThreadForMembers(array $members) {
    $thread_id = $this->mapper
      ->getThreadIdForMembers($members);
    if ($thread_id) {
      return $this->pmThreadManager
        ->load($thread_id);
    }
    else {
      return $this
        ->createPrivateMessageThread($members);
    }
  }

  /**
   * {@inheritdoc}
   */
  public function getFirstThreadForUser(UserInterface $user) {
    $thread_id = $this->mapper
      ->getFirstThreadIdForUser($user);
    if ($thread_id) {
      return $this->pmThreadManager
        ->load($thread_id);
    }
    return FALSE;
  }

  /**
   * {@inheritdoc}
   */
  public function getThreadsForUser($count, $timestamp = FALSE) {
    $return = [
      'threads' => [],
      'next_exists' => FALSE,
    ];
    $user = $this->userManager
      ->load($this->currentUser
      ->id());
    $thread_ids = $this->mapper
      ->getThreadIdsForUser($user, $count, $timestamp);
    if (count($thread_ids)) {
      $threads = $this->pmThreadManager
        ->loadMultiple($thread_ids);
      if (count($threads)) {
        $last_thread = end($threads);
        $last_timestamp = $last_thread
          ->get('updated')->value;
        $return['next_exists'] = $this->mapper
          ->checkForNextThread($user, $last_timestamp);
        $return['threads'] = $threads;
      }
    }
    return $return;
  }

  /**
   * {@inheritdoc}
   */
  public function getCountThreadsForUser() {
    $user = $this->userManager
      ->load($this->currentUser
      ->id());
    $thread_ids = $this->mapper
      ->getThreadIdsForUser($user);
    return count($thread_ids);
  }

  /**
   * {@inheritdoc}
   */
  public function getNewMessages($threadId, $messageId) {
    $response = [];
    $private_message_thread = $this->pmThreadManager
      ->load($threadId);
    if ($private_message_thread && $private_message_thread
      ->isMember($this->currentUser
      ->id())) {
      $messages = $private_message_thread
        ->getMessages();
      $from_index = FALSE;
      foreach ($messages as $index => $message) {
        if ($message
          ->id() > $messageId) {
          $from_index = $index;
          break;
        }
      }
      if ($from_index !== FALSE) {
        $response = array_splice($messages, $from_index);
      }
    }
    return $response;
  }

  /**
   * {@inheritdoc}
   */
  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;
  }

  /**
   * {@inheritdoc}
   */
  public function getUsersFromString($string, $count) {
    $user_ids = $this->mapper
      ->getUserIdsFromString($string, $count);
    $accounts = [];
    if (count($user_ids)) {
      $accounts = $this->userManager
        ->loadMultiple($user_ids);
    }
    return $accounts;
  }

  /**
   * {@inheritdoc}
   */
  public function getUpdatedInboxThreads(array $existingThreadInfo, $count = FALSE) {
    $thread_info = $this->mapper
      ->getUpdatedInboxThreadIds(array_keys($existingThreadInfo), $count);
    $new_threads = [];
    $thread_ids = [];
    $ids_to_load = [];
    foreach (array_keys($thread_info) as $thread_id) {
      $thread_ids[] = $thread_id;
      if (!isset($existingThreadInfo[$thread_id]) || $existingThreadInfo[$thread_id] != $thread_info[$thread_id]->updated) {
        $ids_to_load[] = $thread_id;
      }
    }
    if (count($ids_to_load)) {
      $new_threads = $this->pmThreadManager
        ->loadMultiple($ids_to_load);
    }
    return [
      'thread_ids' => $thread_ids,
      'new_threads' => $new_threads,
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function validatePrivateMessageMemberUsername($username) {
    return $this->mapper
      ->checkPrivateMessageMemberExists($username);
  }

  /**
   * {@inheritdoc}
   */
  public function getUnreadThreadCount() {
    $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
      ->getUnreadThreadCount($uid, $last_check_timestamp);
  }

  /**
   * {@inheritdoc}
   */
  public function updateLastCheckTime() {
    $uid = $this->currentUser
      ->id();
    $this->userData
      ->set(self::MODULE_KEY, $uid, self::LAST_CHECK_KEY, $this->time
      ->getRequestTime());
    $tags[] = 'private_message_notification_block:uid:' . $this->currentUser
      ->id();
    $this->cacheTagsInvalidator
      ->invalidateTags($tags);
  }

  /**
   * {@inheritdoc}
   */
  public function updateThreadAccessTime(PrivateMessageThreadInterface $thread) {
    $thread
      ->updateLastAccessTime($this->currentUser);
  }

  /**
   * {@inheritdoc}
   */
  public function getThreadFromMessage(PrivateMessageInterface $privateMessage) {
    $thread_id = $this->mapper
      ->getThreadIdFromMessage($privateMessage);
    if ($thread_id) {
      return $this->pmThreadManager
        ->load($thread_id);
    }
    return FALSE;
  }

  /**
   * {@inheritdoc}
   */
  public function createRenderablePrivateMessageThreadLink(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, $view_mode) {
    if ($display
      ->getComponent('private_message_link')) {
      if ($entity instanceof UserInterface) {
        $author = $entity;
      }
      else {
        $author = $entity
          ->getOwner();
      }
      $current_user = \Drupal::currentUser();
      if ($current_user
        ->isAuthenticated()) {
        if ($current_user
          ->hasPermission('use private messaging system') && $current_user
          ->id() != $author
          ->id()) {
          $members = [
            $current_user,
            $author,
          ];
          $thread_id = $this->mapper
            ->getThreadIdForMembers($members);
          if ($thread_id) {
            $url = Url::fromRoute('entity.private_message_thread.canonical', [
              'private_message_thread' => $thread_id,
            ], [
              'attributes' => [
                'class' => [
                  'private_message_link',
                ],
              ],
            ]);
            $build['private_message_link'] = [
              '#type' => 'link',
              '#url' => $url,
              '#title' => t('Send private message'),
              '#prefix' => '<div class="private_message_link_wrapper">',
              '#suffix' => '</div>',
            ];
          }
          else {
            $url = Url::fromRoute('private_message.private_message_create', [], [
              'query' => [
                'recipient' => $author
                  ->id(),
              ],
            ]);
            $build['private_message_link'] = [
              '#type' => 'link',
              '#url' => $url,
              '#title' => t('Send private message'),
              '#prefix' => '<div class="private_message_link_wrapper">',
              '#suffix' => '</div>',
            ];
          }
        }
      }
      else {
        $url = Url::fromRoute('user.login');
        $build['private_message_link'] = [
          '#type' => 'link',
          '#url' => $url,
          '#title' => t('Send private message'),
          '#prefix' => '<div class="private_message_link_wrapper">',
          '#suffix' => '</div>',
        ];
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public function getThreadIds() {
    return $this->mapper
      ->getThreadIds();
  }

  /**
   * Create a new private message thread for the given users.
   *
   * @param \Drupal\user\Entity\User[] $members
   *   An array of users who will be members of the given thread.
   *
   * @return \Drupal\private_message\Entity\PrivateMessageThread
   *   The new private message thread.
   */
  protected function createPrivateMessageThread(array $members) {
    $thread = $this->pmThreadManager
      ->create();
    foreach ($members as $member) {
      $thread
        ->addMember($member);
    }
    $thread
      ->save();
    return $thread;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PrivateMessageService::$cacheTagsInvalidator protected property Cache Tags Invalidator.
PrivateMessageService::$configFactory protected property The configuration factory.
PrivateMessageService::$currentUser protected property The current user.
PrivateMessageService::$mapper protected property The private message mapper service.
PrivateMessageService::$pmThreadManager protected property The private message thread manager.
PrivateMessageService::$time protected property The time service.
PrivateMessageService::$userData protected property The user data service.
PrivateMessageService::$userManager protected property The user entity manager.
PrivateMessageService::createPrivateMessageThread protected function Create a new private message thread for the given users.
PrivateMessageService::createRenderablePrivateMessageThreadLink public function Add a link to send a private message to the owner of the given entity. Overrides PrivateMessageServiceInterface::createRenderablePrivateMessageThreadLink
PrivateMessageService::getCountThreadsForUser public function Retrieve the number of threads a user has. Overrides PrivateMessageServiceInterface::getCountThreadsForUser
PrivateMessageService::getFirstThreadForUser public function Get the most recently updated thread for the given user. Overrides PrivateMessageServiceInterface::getFirstThreadForUser
PrivateMessageService::getNewMessages public function Retrieve a users private messages created after the given ID. Overrides PrivateMessageServiceInterface::getNewMessages
PrivateMessageService::getPreviousMessages public function Retrieve old messages for a user that were created before the given ID. Overrides PrivateMessageServiceInterface::getPreviousMessages
PrivateMessageService::getThreadForMembers public function Retrieves the private message thread for the given members. Overrides PrivateMessageServiceInterface::getThreadForMembers
PrivateMessageService::getThreadFromMessage public function Load the thread that a private message belongs to. Overrides PrivateMessageServiceInterface::getThreadFromMessage
PrivateMessageService::getThreadIds public function Retrieve the IDs of all threads in the system. Overrides PrivateMessageServiceInterface::getThreadIds
PrivateMessageService::getThreadsForUser public function Retrieve private message threads for a given user. Overrides PrivateMessageServiceInterface::getThreadsForUser
PrivateMessageService::getUnreadThreadCount public function Get the current user's unread threads count. Overrides PrivateMessageServiceInterface::getUnreadThreadCount
PrivateMessageService::getUpdatedInboxThreads public function Retrieve inbox information for the current user. Overrides PrivateMessageServiceInterface::getUpdatedInboxThreads
PrivateMessageService::getUsersFromString public function Get a list of User objects whose account names begin with the given string. Overrides PrivateMessageServiceInterface::getUsersFromString
PrivateMessageService::updateLastCheckTime public function Marks a timestamp at which all threads are considered read. Overrides PrivateMessageServiceInterface::updateLastCheckTime
PrivateMessageService::updateThreadAccessTime public function Updates the last access time for the current user on the given thread. Overrides PrivateMessageServiceInterface::updateThreadAccessTime
PrivateMessageService::validatePrivateMessageMemberUsername public function Determine whether given user is allowed to be used in a thread. Overrides PrivateMessageServiceInterface::validatePrivateMessageMemberUsername
PrivateMessageService::__construct public function Constructs a PrivateMessageService object.
PrivateMessageServiceInterface::LAST_CHECK_KEY constant The timestamp at which unread private messages were marked as read.
PrivateMessageServiceInterface::MODULE_KEY constant The machine name of the private message module.