logo

pleroma-fe

My custom branche(s) on git.pleroma.social/pleroma/pleroma-fe git clone https://hacktivis.me/git/pleroma-fe.git

notifications_fetcher.service.js (3564B)


  1. import apiService from '../api/api.service.js'
  2. import { promiseInterval } from '../promise_interval/promise_interval.js'
  3. // For using include_types when fetching notifications.
  4. // Note: chat_mention excluded as pleroma-fe polls them separately
  5. const mastoApiNotificationTypes = [
  6. 'mention',
  7. 'favourite',
  8. 'reblog',
  9. 'follow',
  10. 'move',
  11. 'pleroma:emoji_reaction',
  12. 'pleroma:report'
  13. ]
  14. const update = ({ store, notifications, older }) => {
  15. store.dispatch('addNewNotifications', { notifications, older })
  16. }
  17. const fetchAndUpdate = ({ store, credentials, older = false, since }) => {
  18. const args = { credentials }
  19. const { getters } = store
  20. const rootState = store.rootState || store.state
  21. const timelineData = rootState.notifications
  22. const hideMutedPosts = getters.mergedConfig.hideMutedPosts
  23. args.includeTypes = mastoApiNotificationTypes
  24. args.withMuted = !hideMutedPosts
  25. args.timeline = 'notifications'
  26. if (older) {
  27. if (timelineData.minId !== Number.POSITIVE_INFINITY) {
  28. args.until = timelineData.minId
  29. }
  30. return fetchNotifications({ store, args, older })
  31. } else {
  32. // fetch new notifications
  33. if (since === undefined && timelineData.maxId !== Number.POSITIVE_INFINITY) {
  34. args.since = timelineData.maxId
  35. } else if (since !== null) {
  36. args.since = since
  37. }
  38. const result = fetchNotifications({ store, args, older })
  39. // If there's any unread notifications, try fetch notifications since
  40. // the newest read notification to check if any of the unread notifs
  41. // have changed their 'seen' state (marked as read in another session), so
  42. // we can update the state in this session to mark them as read as well.
  43. // The normal maxId-check does not tell if older notifications have changed
  44. const notifications = timelineData.data
  45. const readNotifsIds = notifications.filter(n => n.seen).map(n => n.id)
  46. const unreadNotifsIds = notifications.filter(n => !n.seen).map(n => n.id)
  47. if (readNotifsIds.length > 0 && readNotifsIds.length > 0) {
  48. const minId = Math.min(...unreadNotifsIds) // Oldest known unread notification
  49. if (minId !== Infinity) {
  50. args.since = false // Don't use since_id since it sorta conflicts with min_id
  51. args.minId = minId - 1 // go beyond
  52. fetchNotifications({ store, args, older })
  53. }
  54. }
  55. return result
  56. }
  57. }
  58. const fetchNotifications = ({ store, args, older }) => {
  59. return apiService.fetchTimeline(args)
  60. .then((response) => {
  61. if (response.errors) {
  62. throw new Error(`${response.status} ${response.statusText}`)
  63. }
  64. const notifications = response.data
  65. update({ store, notifications, older })
  66. return notifications
  67. })
  68. .catch((error) => {
  69. store.dispatch('pushGlobalNotice', {
  70. level: 'error',
  71. messageKey: 'notifications.error',
  72. messageArgs: [error.message],
  73. timeout: 5000
  74. })
  75. console.error(error)
  76. })
  77. }
  78. const startFetching = ({ credentials, store }) => {
  79. // Initially there's set flag to silence all desktop notifications so
  80. // that there won't spam of them when user just opened up the FE we
  81. // reset that flag after a while to show new notifications once again.
  82. setTimeout(() => store.dispatch('setNotificationsSilence', false), 10000)
  83. const boundFetchAndUpdate = () => fetchAndUpdate({ credentials, store })
  84. boundFetchAndUpdate()
  85. return promiseInterval(boundFetchAndUpdate, 10000)
  86. }
  87. const notificationsFetcher = {
  88. fetchAndUpdate,
  89. startFetching
  90. }
  91. export default notificationsFetcher