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) {
12 var currentUserID = parseInt(drupalSettings.user.uid, 10);
14 // Any comment that is older than 30 days is automatically considered read,
15 // so for these we don't need to perform a request at all!
16 var thirtyDaysAgo = Math.round(new Date().getTime() / 1000) - 30 * 24 * 60 * 60;
18 // Use the data embedded in the page, if available.
19 var embeddedLastReadTimestamps = false;
20 if (drupalSettings.history && drupalSettings.history.lastReadTimestamps) {
21 embeddedLastReadTimestamps = drupalSettings.history.lastReadTimestamps;
30 * Fetch "last read" timestamps for the given nodes.
32 * @param {Array} nodeIDs
33 * An array of node IDs.
34 * @param {function} callback
35 * A callback that is called after the requested timestamps were fetched.
37 fetchTimestamps: function (nodeIDs, callback) {
38 // Use the data embedded in the page, if available.
39 if (embeddedLastReadTimestamps) {
45 url: Drupal.url('history/get_node_read_timestamps'),
47 data: {'node_ids[]': nodeIDs},
49 success: function (results) {
50 for (var nodeID in results) {
51 if (results.hasOwnProperty(nodeID)) {
52 storage.setItem('Drupal.history.' + currentUserID + '.' + nodeID, results[nodeID]);
61 * Get the last read timestamp for the given node.
63 * @param {number|string} nodeID
69 getLastRead: function (nodeID) {
70 // Use the data embedded in the page, if available.
71 if (embeddedLastReadTimestamps && embeddedLastReadTimestamps[nodeID]) {
72 return parseInt(embeddedLastReadTimestamps[nodeID], 10);
74 return parseInt(storage.getItem('Drupal.history.' + currentUserID + '.' + nodeID) || 0, 10);
78 * Marks a node as read, store the last read timestamp client-side.
80 * @param {number|string} nodeID
83 markAsRead: function (nodeID) {
85 url: Drupal.url('history/' + nodeID + '/read'),
88 success: function (timestamp) {
89 // If the data is embedded in the page, don't store on the client
91 if (embeddedLastReadTimestamps && embeddedLastReadTimestamps[nodeID]) {
95 storage.setItem('Drupal.history.' + currentUserID + '.' + nodeID, timestamp);
101 * Determines whether a server check is necessary.
103 * Any content that is >30 days old never gets a "new" or "updated"
104 * indicator. Any content that was published before the oldest known reading
105 * also never gets a "new" or "updated" indicator, because it must've been
108 * @param {number|string} nodeID
110 * @param {number} contentTimestamp
111 * The time at which some content (e.g. a comment) was published.
114 * Whether a server check is necessary for the given node and its
117 needsServerCheck: function (nodeID, contentTimestamp) {
118 // First check if the content is older than 30 days, then we can bail
120 if (contentTimestamp < thirtyDaysAgo) {
124 // Use the data embedded in the page, if available.
125 if (embeddedLastReadTimestamps && embeddedLastReadTimestamps[nodeID]) {
126 return contentTimestamp > parseInt(embeddedLastReadTimestamps[nodeID], 10);
129 var minLastReadTimestamp = parseInt(storage.getItem('Drupal.history.' + currentUserID + '.' + nodeID) || 0, 10);
130 return contentTimestamp > minLastReadTimestamp;
134 })(jQuery, Drupal, drupalSettings, window.localStorage);