3 namespace Drupal\history\Controller;
5 use Symfony\Component\HttpFoundation\Request;
6 use Symfony\Component\HttpFoundation\JsonResponse;
7 use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
8 use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
9 use Drupal\Core\Controller\ControllerBase;
10 use Drupal\node\NodeInterface;
13 * Returns responses for History module routes.
15 class HistoryController extends ControllerBase {
18 * Returns a set of nodes' last read timestamps.
20 * @param \Symfony\Component\HttpFoundation\Request $request
21 * The request of the page.
23 * @return Symfony\Component\HttpFoundation\JsonResponse
26 public function getNodeReadTimestamps(Request $request) {
27 if ($this->currentUser()->isAnonymous()) {
28 throw new AccessDeniedHttpException();
31 $nids = $request->request->get('node_ids');
33 throw new NotFoundHttpException();
35 return new JsonResponse(history_read_multiple($nids));
39 * Marks a node as read by the current user right now.
41 * @param \Symfony\Component\HttpFoundation\Request $request
42 * The request of the page.
43 * @param \Drupal\node\NodeInterface $node
44 * The node whose "last read" timestamp should be updated.
46 public function readNode(Request $request, NodeInterface $node) {
47 if ($this->currentUser()->isAnonymous()) {
48 throw new AccessDeniedHttpException();
51 // Update the history table, stating that this user viewed this node.
52 history_write($node->id());
54 return new JsonResponse((int) history_read($node->id()));