diff --git a/core/modules/comment/comment.module b/core/modules/comment/comment.module index e272e2e..064494d 100644 --- a/core/modules/comment/comment.module +++ b/core/modules/comment/comment.module @@ -552,7 +552,7 @@ function comment_entity_view(EntityInterface $entity, EntityViewDisplayInterface // Embed the metadata for the "X new comments" link (if any) on this node. $entity->content['links']['#post_render_cache']['history_attach_timestamp'] = array( - array('node_id' => $entity->id()), + array('entity_type' => $entity->getEntityTypeId(), 'entity_id' => $entity->id()), ); $entity->content['links']['#post_render_cache']['Drupal\comment\CommentViewBuilder::attachNewCommentsLinkMetadata'] = array( array('entity_type' => $entity->getEntityTypeId(), 'entity_id' => $entity->id(), 'field_name' => $field_name), @@ -1153,10 +1153,11 @@ function comment_load($cid, $reset = FALSE) { * The number of new comments or FALSE if the user is not logged in. */ function comment_num_new($entity_id, $entity_type, $field_name = NULL, $timestamp = 0) { - if (\Drupal::currentUser()->isAuthenticated() && \Drupal::moduleHandler()->moduleExists('history')) { + $account = \Drupal::currentUser(); + if ($account->isAuthenticated() && \Drupal::moduleHandler()->moduleExists('history')) { // Retrieve the timestamp at which the current user last viewed this entity. if (!$timestamp) { - $timestamp = \Drupal::service('history.repository')->getLastViewed($entity_type, array($entity_id)); + $timestamp = \Drupal::service('history.manager')->getLastViewed($entity_type, array($entity_id), $account); $timestamp = $timestamp[$entity_id]; } $timestamp = ($timestamp > HISTORY_READ_LIMIT ? $timestamp : HISTORY_READ_LIMIT); diff --git a/core/modules/comment/lib/Drupal/comment/CommentViewBuilder.php b/core/modules/comment/lib/Drupal/comment/CommentViewBuilder.php index 53d0ad7..eb39d03 100644 --- a/core/modules/comment/lib/Drupal/comment/CommentViewBuilder.php +++ b/core/modules/comment/lib/Drupal/comment/CommentViewBuilder.php @@ -143,7 +143,7 @@ public function buildContent(array $entities, array $displays, $view_mode, $lang // Embed the metadata for the comment "new" indicators on this node. $entity->content['#post_render_cache']['history_attach_timestamp'] = array( - array('node_id' => $commented_entity->id()), + array('entity_type' => $commented_entity->getEntityTypeId(), 'entity_id' => $commented_entity->id()), ); } } diff --git a/core/modules/forum/lib/Drupal/forum/ForumManager.php b/core/modules/forum/lib/Drupal/forum/ForumManager.php index 108d2c8..a4e6acd 100644 --- a/core/modules/forum/lib/Drupal/forum/ForumManager.php +++ b/core/modules/forum/lib/Drupal/forum/ForumManager.php @@ -249,7 +249,7 @@ public function getTopics($tid) { $topic->new = 0; } else { - $history = $this->lastVisit($topic->id()); + $history = $this->lastVisit($topic->id(), $user); $topic->new_replies = $this->numberNew($topic->id(), $history); $topic->new = $topic->new_replies || ($topic->last_comment_timestamp > $history); } @@ -333,17 +333,18 @@ protected function numberNew($nid, $timestamp) { * * @param int $nid * The node ID. + * @param \Drupal\Core\Session\AccountInterface $account + * The user account to retrieve history for. * * @return int * The timestamp when the user last viewed this node, if the user has * previously viewed the node; otherwise HISTORY_READ_LIMIT. */ - protected function lastVisit($nid) { + protected function lastVisit($nid, $account) { if (isset($this->history[$nid])) { return $this->history[$nid]; } - - $this->history += $this->historyRepository->getLastViewed('node', array($nid)); + $this->history += $this->historyRepository->getLastViewed('node', array($nid), $account); if (empty($this->history[$nid])) { $this->history[$nid] = HISTORY_READ_LIMIT; } diff --git a/core/modules/history/history.module b/core/modules/history/history.module index 0743473..8bc07ad 100644 --- a/core/modules/history/history.module +++ b/core/modules/history/history.module @@ -46,7 +46,8 @@ function history_help($path, $arg) { * @deprecated Use \Drupal\history\HistoryRepositoryInterface::getLastViewed() instead. */ function history_read($nid) { - $history = \Drupal::service('history.repository')->getLastViewed('node', array($nid)); + $account = \Drupal::currentUser(); + $history = \Drupal::service('history.manager')->getLastViewed('node', array($nid), $account); return $history[$nid]; } @@ -64,7 +65,8 @@ function history_read($nid) { * @deprecated Use \Drupal\history\HistoryRepositoryInterface::getLastViewed() instead. */ function history_read_multiple($nids) { - return \Drupal::service('history.repository')->getLastViewed('node', $nids); + $account = \Drupal::currentUser(); + return \Drupal::service('history.manager')->getLastViewed('node', $nids, $account); } /** @@ -82,7 +84,8 @@ function history_write($nid, AccountInterface $account = NULL) { if (!isset($account)) { $account = \Drupal::currentUser(); } - \Drupal::service('history.repository')->updateLastViewed('node', $nid, $account); + $node = entity_load('node', $nid); + \Drupal::service('history.manager')->updateLastViewed($node, $account); } /** @@ -171,18 +174,20 @@ function history_library_info() { * - #attached * @param array $context * An array with the following keys: - * - node_id: the node ID for which to attach the last read timestamp. + * - entity_id: the entity ID for which to attach the last read timestamp. * * @return array $element * The updated $element. */ function history_attach_timestamp(array $element, array $context) { + $account = \Drupal::currentUser(); + $history = \Drupal::service('history.manager')->getLastViewed($context['entity_type'], array($context['entity_id']), $account); $element['#attached']['js'][] = array( 'type' => 'setting', 'data' => array( 'history' => array( 'lastReadTimestamps' => array( - $context['node_id'] => (int) history_read($context['node_id']), + $context['node_id'] => $history[$context['entity_id']], ) ), ), diff --git a/core/modules/history/lib/Drupal/history/Controller/HistoryController.php b/core/modules/history/lib/Drupal/history/Controller/HistoryController.php index 49bf3ff..6d51320 100644 --- a/core/modules/history/lib/Drupal/history/Controller/HistoryController.php +++ b/core/modules/history/lib/Drupal/history/Controller/HistoryController.php @@ -53,11 +53,14 @@ public static function create(ContainerInterface $container) { * @param \Symfony\Component\HttpFoundation\Request $request * The request of the page. * - * @return Symfony\Component\HttpFoundation\JsonResponse + * @return \Symfony\Component\HttpFoundation\JsonResponse * The JSON response. + * @throws \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException + * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException */ public function getNodeReadTimestamps(Request $request) { - if ($this->currentUser()->isAnonymous()) { + $account = $this->currentUser(); + if ($account->isAnonymous()) { throw new AccessDeniedHttpException(); } @@ -65,7 +68,7 @@ public function getNodeReadTimestamps(Request $request) { if (!isset($nids)) { throw new NotFoundHttpException(); } - return new JsonResponse($this->historyRepository->getLastViewed('node', $nids)); + return new JsonResponse($this->historyManager->getLastViewed('node', $nids, $account)); } /** @@ -75,16 +78,21 @@ public function getNodeReadTimestamps(Request $request) { * The request of the page. * @param \Drupal\node\NodeInterface $node * The node whose "last read" timestamp should be updated. + * + * @return \Symfony\Component\HttpFoundation\JsonResponse + * The JSON response. + * @throws \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException */ public function readNode(Request $request, NodeInterface $node) { - if ($this->currentUser()->isAnonymous()) { + $account = $this->currentUser(); + if ($account->isAnonymous()) { throw new AccessDeniedHttpException(); } // Update the history table, stating that this user viewed this node. - $this->historyRepository->updateLastViewed($node, $this->currentUser()); + $this->historyManager->updateLastViewed($node, $account); - $history = $this->historyRepository->getLastViewed('node', array($node->id())); + $history = $this->historyManager->getLastViewed('node', array($node->id()), $account); return new JsonResponse($history[$node->id()]); } diff --git a/core/modules/history/lib/Drupal/history/HistoryRepository.php b/core/modules/history/lib/Drupal/history/HistoryRepository.php index 0bbabb1..e91ba67 100644 --- a/core/modules/history/lib/Drupal/history/HistoryRepository.php +++ b/core/modules/history/lib/Drupal/history/HistoryRepository.php @@ -53,7 +53,7 @@ public function __construct(Connection $connection, AccountInterface $current_us /** * {@inheritdoc} */ - public function getLastViewed($entity_type, $entity_ids) { + public function getLastViewed($entity_type, $entity_ids, AccountInterface $account) { $return = array(); $entities_to_read = array(); @@ -72,7 +72,7 @@ public function getLastViewed($entity_type, $entity_ids) { $result = $this->connection->select('history', 'h') ->fields('h', array('entity_id', 'timestamp')) - ->condition('uid', $this->currentUser->id()) + ->condition('uid', $account->id()) ->condition('entity_type', $entity_type) ->condition('entity_id', array_keys($entities_to_read), 'IN') ->execute(); diff --git a/core/modules/history/lib/Drupal/history/HistoryRepositoryInterface.php b/core/modules/history/lib/Drupal/history/HistoryRepositoryInterface.php index e648464..7f9f0dc 100644 --- a/core/modules/history/lib/Drupal/history/HistoryRepositoryInterface.php +++ b/core/modules/history/lib/Drupal/history/HistoryRepositoryInterface.php @@ -22,16 +22,18 @@ * The entity type. * @param array $entity_ids * The entity IDs. + * @param \Drupal\Core\Session\AccountInterface $account + * The user account to get the history for. * * @return array * Array of timestamps keyed by entity ID. If a entity has been previously * viewed by the user, the timestamp in seconds of when the last view * occurred; otherwise, zero. */ - public function getLastViewed($entity_type, $entity_ids); + public function getLastViewed($entity_type, $entity_ids, AccountInterface $account); /** - * Updates 'last viewed' timestamp of the entity for the current user. + * Updates 'last viewed' timestamp of the entity for the user account. * * @param \Drupal\Core\Entity\EntityInterface $entity * The entity that history should be updated. @@ -49,7 +51,7 @@ public function purge(); * Deletes history of the user account. * * @param \Drupal\Core\Session\AccountInterface $account - * The user account to purge history + * The user account to purge history. */ public function deleteByUser(AccountInterface $account);