You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

notification.js 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. const {AppSubUrl, csrf, NotificationSettings} = window.config;
  2. export function initNotificationsTable() {
  3. $('#notification_table .button').on('click', async function () {
  4. const data = await updateNotification(
  5. $(this).data('url'),
  6. $(this).data('status'),
  7. $(this).data('page'),
  8. $(this).data('q'),
  9. $(this).data('notification-id'),
  10. );
  11. $('#notification_div').replaceWith(data);
  12. initNotificationsTable();
  13. await updateNotificationCount();
  14. return false;
  15. });
  16. }
  17. export function initNotificationCount() {
  18. const notificationCount = $('.notification_count');
  19. if (!notificationCount.length) {
  20. return;
  21. }
  22. if (NotificationSettings.EventSourceUpdateTime > 0 && !!window.EventSource) {
  23. // Try to connect to the event source first
  24. const source = new EventSource(`${AppSubUrl}/user/events`);
  25. source.addEventListener('notification-count', async (e) => {
  26. try {
  27. const data = JSON.parse(e.data);
  28. const notificationCount = $('.notification_count');
  29. if (data.Count === 0) {
  30. notificationCount.addClass('hidden');
  31. } else {
  32. notificationCount.removeClass('hidden');
  33. }
  34. notificationCount.text(`${data.Count}`);
  35. await updateNotificationTable();
  36. } catch (error) {
  37. console.error(error);
  38. }
  39. });
  40. source.addEventListener('logout', async (e) => {
  41. if (e.data !== 'here') {
  42. return;
  43. }
  44. source.close();
  45. window.location.href = AppSubUrl;
  46. });
  47. return;
  48. }
  49. if (NotificationSettings.MinTimeout <= 0) {
  50. return;
  51. }
  52. const fn = (timeout, lastCount) => {
  53. setTimeout(async () => {
  54. await updateNotificationCountWithCallback(fn, timeout, lastCount);
  55. }, timeout);
  56. };
  57. fn(NotificationSettings.MinTimeout, notificationCount.text());
  58. }
  59. async function updateNotificationCountWithCallback(callback, timeout, lastCount) {
  60. const currentCount = $('.notification_count').text();
  61. if (lastCount !== currentCount) {
  62. callback(NotificationSettings.MinTimeout, currentCount);
  63. return;
  64. }
  65. const newCount = await updateNotificationCount();
  66. let needsUpdate = false;
  67. if (lastCount !== newCount) {
  68. needsUpdate = true;
  69. timeout = NotificationSettings.MinTimeout;
  70. } else if (timeout < NotificationSettings.MaxTimeout) {
  71. timeout += NotificationSettings.TimeoutStep;
  72. }
  73. callback(timeout, newCount);
  74. if (needsUpdate) {
  75. await updateNotificationTable();
  76. }
  77. }
  78. async function updateNotificationTable() {
  79. const notificationDiv = $('#notification_div');
  80. if (notificationDiv.length > 0) {
  81. const data = await $.ajax({
  82. type: 'GET',
  83. url: `${AppSubUrl}/notifications?${notificationDiv.data('params')}`,
  84. data: {
  85. 'div-only': true,
  86. }
  87. });
  88. notificationDiv.replaceWith(data);
  89. initNotificationsTable();
  90. }
  91. }
  92. async function updateNotificationCount() {
  93. const data = await $.ajax({
  94. type: 'GET',
  95. url: `${AppSubUrl}/api/v1/notifications/new`,
  96. headers: {
  97. 'X-Csrf-Token': csrf,
  98. },
  99. });
  100. const notificationCount = $('.notification_count');
  101. if (data.new === 0) {
  102. notificationCount.addClass('hidden');
  103. } else {
  104. notificationCount.removeClass('hidden');
  105. }
  106. notificationCount.text(`${data.new}`);
  107. return `${data.new}`;
  108. }
  109. async function updateNotification(url, status, page, q, notificationID) {
  110. if (status !== 'pinned') {
  111. $(`#notification_${notificationID}`).remove();
  112. }
  113. return $.ajax({
  114. type: 'POST',
  115. url,
  116. data: {
  117. _csrf: csrf,
  118. notification_id: notificationID,
  119. status,
  120. page,
  121. q,
  122. noredirect: true,
  123. },
  124. });
  125. }