3 * JavaScript API for the History module, with client-side caching.
5 * May only be loaded for authenticated users, with the History module enabled.
8 (function ($, Drupal, drupalSettings, storage) {
9 const currentUserID = parseInt(drupalSettings.user.uid, 10);
11 // Any comment that is older than 30 days is automatically considered read,
12 // so for these we don't need to perform a request at all!
13 const secondsIn30Days = 2592000;
14 const thirtyDaysAgo = Math.round(new Date().getTime() / 1000) - secondsIn30Days;
16 // Use the data embedded in the page, if available.
17 let embeddedLastReadTimestamps = false;
18 if (drupalSettings.history && drupalSettings.history.lastReadTimestamps) {
19 embeddedLastReadTimestamps = drupalSettings.history.lastReadTimestamps;
28 * Fetch "last read" timestamps for the given nodes.
30 * @param {Array} nodeIDs
31 * An array of node IDs.
32 * @param {function} callback
33 * A callback that is called after the requested timestamps were fetched.
35 fetchTimestamps(nodeIDs, callback) {
36 // Use the data embedded in the page, if available.
37 if (embeddedLastReadTimestamps) {
43 url: Drupal.url('history/get_node_read_timestamps'),
45 data: { 'node_ids[]': nodeIDs },
48 Object.keys(results || {}).forEach((nodeID) => {
49 storage.setItem(`Drupal.history.${currentUserID}.${nodeID}`, results[nodeID]);
57 * Get the last read timestamp for the given node.
59 * @param {number|string} nodeID
66 // Use the data embedded in the page, if available.
67 if (embeddedLastReadTimestamps && embeddedLastReadTimestamps[nodeID]) {
68 return parseInt(embeddedLastReadTimestamps[nodeID], 10);
70 return parseInt(storage.getItem(`Drupal.history.${currentUserID}.${nodeID}`) || 0, 10);
74 * Marks a node as read, store the last read timestamp client-side.
76 * @param {number|string} nodeID
81 url: Drupal.url(`history/${nodeID}/read`),
85 // If the data is embedded in the page, don't store on the client
87 if (embeddedLastReadTimestamps && embeddedLastReadTimestamps[nodeID]) {
91 storage.setItem(`Drupal.history.${currentUserID}.${nodeID}`, timestamp);
97 * Determines whether a server check is necessary.
99 * Any content that is >30 days old never gets a "new" or "updated"
100 * indicator. Any content that was published before the oldest known reading
101 * also never gets a "new" or "updated" indicator, because it must've been
104 * @param {number|string} nodeID
106 * @param {number} contentTimestamp
107 * The time at which some content (e.g. a comment) was published.
110 * Whether a server check is necessary for the given node and its
113 needsServerCheck(nodeID, contentTimestamp) {
114 // First check if the content is older than 30 days, then we can bail
116 if (contentTimestamp < thirtyDaysAgo) {
120 // Use the data embedded in the page, if available.
121 if (embeddedLastReadTimestamps && embeddedLastReadTimestamps[nodeID]) {
122 return contentTimestamp > parseInt(embeddedLastReadTimestamps[nodeID], 10);
125 const minLastReadTimestamp = parseInt(storage.getItem(`Drupal.history.${currentUserID}.${nodeID}`) || 0, 10);
126 return contentTimestamp > minLastReadTimestamp;
129 }(jQuery, Drupal, drupalSettings, window.localStorage));