notifications.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. $(document).ready(function() {
  2. $('.nav-link.nav-notification').on('click', function(e) {
  3. e.preventDefault();
  4. let el = $(this);
  5. if(el.attr('data-toggle') == 'tooltip') {
  6. el.attr('data-toggle', 'dropdown');
  7. el.tooltip('dispose');
  8. }
  9. let container = $('.navbar .nav-notification-dropdown');
  10. if(pixelfed.notifications) {
  11. return;
  12. }
  13. axios.get('/api/v2/notifications')
  14. .then((res) => {
  15. $('.nav-notification-dropdown .loader').hide();
  16. let data = res.data;
  17. data.forEach(function(v, k) {
  18. let action = v.action;
  19. let notification = $('<li>').addClass('dropdown-item py-3')
  20. if(v.read_at == null) {
  21. notification.attr('style', 'border: 1px solid #6cb2eb;background-color: #eff8ff;border-bottom:none;');
  22. } else {
  23. notification.attr('style', 'border-bottom: 1px solid #ccc;');
  24. }
  25. switch(action) {
  26. case 'comment':
  27. let avatar = $('<span>')
  28. .attr('class', 'notification-icon pr-3');
  29. let avatarImg = $('<img>')
  30. .attr('width', '32px')
  31. .attr('height', '32px')
  32. .attr('class', 'rounded-circle')
  33. .attr('style', 'border: 1px solid #ccc')
  34. .attr('src', v.actor.avatar);
  35. avatar = avatar.append(avatarImg);
  36. let text = $('<span>')
  37. .attr('href', v.url)
  38. .attr('class', 'font-weight-bold')
  39. .html(v.rendered);
  40. notification.append(avatar);
  41. notification.append(text);
  42. container.append(notification);
  43. break;
  44. case 'follow':
  45. avatar = $('<span>')
  46. .attr('class', 'notification-icon pr-3');
  47. avatarImg = $('<img>')
  48. .attr('width', '32px')
  49. .attr('height', '32px')
  50. .attr('class', 'rounded-circle')
  51. .attr('style', 'border: 1px solid #ccc')
  52. .attr('src', v.actor.avatar);
  53. avatar = avatar.append(avatarImg);
  54. text = $('<span>')
  55. .attr('href', v.url)
  56. .attr('class', 'font-weight-bold')
  57. .html(v.rendered);
  58. notification.append(avatar);
  59. notification.append(text);
  60. container.append(notification);
  61. break;
  62. }
  63. });
  64. let all = $('<a>')
  65. .attr('class', 'dropdown-item py-3 text-center text-primary font-weight-bold')
  66. .attr('href', '/account/activity')
  67. .attr('style', 'border-top:1px solid #ccc')
  68. .text('View all notifications');
  69. container.append(all);
  70. pixelfed.notifications = true;
  71. }).catch((err) => {
  72. $('.nav-notification-dropdown .loader').addClass('font-weight-bold').text('Something went wrong. Please try again later.');
  73. });
  74. });
  75. $('.notification-action[data-type="mark_read"]').on('click', function(e) {
  76. e.preventDefault();
  77. axios.post('/api/v2/notifications', {
  78. 'action': 'mark_read'
  79. }).then(res => {
  80. pixelfed.notifications = false;
  81. ls.del('n.lastCheck');
  82. ls.del('n.count');
  83. swal(
  84. 'Success!',
  85. 'All of your notifications have been marked as read.',
  86. 'success'
  87. );
  88. }).catch(err => {
  89. swal(
  90. 'Something went wrong!',
  91. 'An error occurred, please try again later.',
  92. 'error'
  93. );
  94. });
  95. });
  96. pixelfed.n.showCount = (count = 1) => {
  97. let el = $('.nav-link.nav-notification');
  98. el.tooltip('dispose');
  99. el.attr('title', count)
  100. el.attr('data-toggle', 'tooltip');
  101. el.tooltip({
  102. template: '<div class="tooltip notification-tooltip" role="tooltip"><div class="arrow"></div><div class="tooltip-inner bg-danger px-3"></div></div>'
  103. });
  104. setTimeout(function() {
  105. el.fadeIn(function() {
  106. el.tooltip('show')
  107. });
  108. }, 500);
  109. }
  110. pixelfed.n.sound = () => {
  111. let beep = new Audio('/static/beep.mp3');
  112. beep.play();
  113. }
  114. pixelfed.n.check = (count) => {
  115. // pixelfed.n.sound();
  116. pixelfed.n.showCount(count);
  117. }
  118. pixelfed.n.fetch = (force = false) => {
  119. let now = Date.now();
  120. let ts = ls.get('n.lastCheck');
  121. let count = ls.get('n.count');
  122. let offset = now - 9e5;
  123. if(ts == null) {
  124. ts = now;
  125. }
  126. if(!force && count != null || ts > offset) {
  127. //pixelfed.n.showCount(count);
  128. ls.set('n.lastCheck', ts);
  129. return;
  130. }
  131. axios.get('/api/v2/notifications')
  132. .then(res => {
  133. let len = res.data.length;
  134. if(len > 0) {
  135. ls.set('n.count', len);
  136. ls.set('n.lastCheck', Date.now());
  137. pixelfed.n.check(len);
  138. }
  139. }).catch(err => {
  140. })
  141. }
  142. if($('body').hasClass('loggedIn') == true) {
  143. pixelfed.n.fetch();
  144. }
  145. });