logo

pleroma-fe

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

notifications_fetcher.service.js (3694B)


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