notifications.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. $(document).ready(function() {
  2. $('.nav-link.nav-notification').on('click', function(e) {
  3. e.preventDefault();
  4. let el = $(this);
  5. let container = $('.navbar .nav-notification-dropdown');
  6. if(pixelfed.notifications) {
  7. return;
  8. }
  9. axios.get('/api/v2/notifications')
  10. .then((res) => {
  11. $('.nav-notification-dropdown .loader').hide();
  12. let data = res.data;
  13. data.forEach(function(v, k) {
  14. let action = v.action;
  15. let notification = $('<li>').addClass('dropdown-item py-3')
  16. .attr('style', 'border-bottom: 1px solid #ccc')
  17. switch(action) {
  18. case 'comment':
  19. let avatar = $('<span>')
  20. .attr('class', 'notification-icon pr-3');
  21. let avatarImg = $('<img>')
  22. .attr('width', '32px')
  23. .attr('height', '32px')
  24. .attr('class', 'rounded-circle')
  25. .attr('style', 'border: 1px solid #ccc')
  26. .attr('src', v.actor.avatar);
  27. avatar = avatar.append(avatarImg);
  28. let text = $('<span>')
  29. .attr('href', v.url)
  30. .attr('class', 'font-weight-bold')
  31. .html(v.rendered);
  32. notification.append(avatar);
  33. notification.append(text);
  34. container.append(notification);
  35. break;
  36. case 'follow':
  37. avatar = $('<span>')
  38. .attr('class', 'notification-icon pr-3');
  39. avatarImg = $('<img>')
  40. .attr('width', '32px')
  41. .attr('height', '32px')
  42. .attr('class', 'rounded-circle')
  43. .attr('style', 'border: 1px solid #ccc')
  44. .attr('src', v.actor.avatar);
  45. avatar = avatar.append(avatarImg);
  46. text = $('<span>')
  47. .attr('href', v.url)
  48. .attr('class', 'font-weight-bold')
  49. .html(v.rendered);
  50. notification.append(avatar);
  51. notification.append(text);
  52. container.append(notification);
  53. break;
  54. }
  55. });
  56. let all = $('<a>')
  57. .attr('class', 'dropdown-item py-3 text-center text-primary font-weight-bold')
  58. .attr('href', '/account/activity')
  59. .text('View all notifications');
  60. container.append(all);
  61. pixelfed.notifications = true;
  62. }).catch((err) => {
  63. $('.nav-notification-dropdown .loader').addClass('font-weight-bold').text('Something went wrong. Please try again later.');
  64. });
  65. });
  66. });