history.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /**
  2. * DO NOT EDIT THIS FILE.
  3. * See the following change record for more information,
  4. * https://www.drupal.org/node/2815083
  5. * @preserve
  6. **/
  7. (function ($, Drupal, drupalSettings, storage) {
  8. var currentUserID = parseInt(drupalSettings.user.uid, 10);
  9. var secondsIn30Days = 2592000;
  10. var thirtyDaysAgo = Math.round(new Date().getTime() / 1000) - secondsIn30Days;
  11. var embeddedLastReadTimestamps = false;
  12. if (drupalSettings.history && drupalSettings.history.lastReadTimestamps) {
  13. embeddedLastReadTimestamps = drupalSettings.history.lastReadTimestamps;
  14. }
  15. Drupal.history = {
  16. fetchTimestamps: function fetchTimestamps(nodeIDs, callback) {
  17. if (embeddedLastReadTimestamps) {
  18. callback();
  19. return;
  20. }
  21. $.ajax({
  22. url: Drupal.url('history/get_node_read_timestamps'),
  23. type: 'POST',
  24. data: { 'node_ids[]': nodeIDs },
  25. dataType: 'json',
  26. success: function success(results) {
  27. Object.keys(results || {}).forEach(function (nodeID) {
  28. storage.setItem('Drupal.history.' + currentUserID + '.' + nodeID, results[nodeID]);
  29. });
  30. callback();
  31. }
  32. });
  33. },
  34. getLastRead: function getLastRead(nodeID) {
  35. if (embeddedLastReadTimestamps && embeddedLastReadTimestamps[nodeID]) {
  36. return parseInt(embeddedLastReadTimestamps[nodeID], 10);
  37. }
  38. return parseInt(storage.getItem('Drupal.history.' + currentUserID + '.' + nodeID) || 0, 10);
  39. },
  40. markAsRead: function markAsRead(nodeID) {
  41. $.ajax({
  42. url: Drupal.url('history/' + nodeID + '/read'),
  43. type: 'POST',
  44. dataType: 'json',
  45. success: function success(timestamp) {
  46. if (embeddedLastReadTimestamps && embeddedLastReadTimestamps[nodeID]) {
  47. return;
  48. }
  49. storage.setItem('Drupal.history.' + currentUserID + '.' + nodeID, timestamp);
  50. }
  51. });
  52. },
  53. needsServerCheck: function needsServerCheck(nodeID, contentTimestamp) {
  54. if (contentTimestamp < thirtyDaysAgo) {
  55. return false;
  56. }
  57. if (embeddedLastReadTimestamps && embeddedLastReadTimestamps[nodeID]) {
  58. return contentTimestamp > parseInt(embeddedLastReadTimestamps[nodeID], 10);
  59. }
  60. var minLastReadTimestamp = parseInt(storage.getItem('Drupal.history.' + currentUserID + '.' + nodeID) || 0, 10);
  61. return contentTimestamp > minLastReadTimestamp;
  62. }
  63. };
  64. })(jQuery, Drupal, drupalSettings, window.localStorage);