logo

pleroma-fe

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

timeline_fetcher.service.js (2918B)


  1. import { camelCase } from 'lodash'
  2. import apiService from '../api/api.service.js'
  3. import { promiseInterval } from '../promise_interval/promise_interval.js'
  4. const update = ({ store, statuses, timeline, showImmediately, userId, listId, pagination }) => {
  5. const ccTimeline = camelCase(timeline)
  6. store.dispatch('addNewStatuses', {
  7. timeline: ccTimeline,
  8. userId,
  9. listId,
  10. statuses,
  11. showImmediately,
  12. pagination
  13. })
  14. }
  15. const fetchAndUpdate = ({
  16. store,
  17. credentials,
  18. timeline = 'friends',
  19. older = false,
  20. showImmediately = false,
  21. userId = false,
  22. listId = false,
  23. tag = false,
  24. until,
  25. since
  26. }) => {
  27. const args = { timeline, credentials }
  28. const rootState = store.rootState || store.state
  29. const { getters } = store
  30. const timelineData = rootState.statuses.timelines[camelCase(timeline)]
  31. const { hideMutedPosts, replyVisibility } = getters.mergedConfig
  32. const loggedIn = !!rootState.users.currentUser
  33. if (older) {
  34. args.until = until || timelineData.minId
  35. } else {
  36. if (since === undefined) {
  37. args.since = timelineData.maxId
  38. } else if (since !== null) {
  39. args.since = since
  40. }
  41. }
  42. args.userId = userId
  43. args.listId = listId
  44. args.tag = tag
  45. args.withMuted = !hideMutedPosts
  46. if (loggedIn && ['friends', 'public', 'publicAndExternal'].includes(timeline)) {
  47. args.replyVisibility = replyVisibility
  48. }
  49. const numStatusesBeforeFetch = timelineData.statuses.length
  50. return apiService.fetchTimeline(args)
  51. .then(response => {
  52. if (response.errors) {
  53. throw new Error(`${response.status} ${response.statusText}`)
  54. }
  55. const { data: statuses, pagination } = response
  56. if (!older && statuses.length >= 20 && !timelineData.loading && numStatusesBeforeFetch > 0) {
  57. store.dispatch('queueFlush', { timeline, id: timelineData.maxId })
  58. }
  59. update({ store, statuses, timeline, showImmediately, userId, listId, pagination })
  60. return { statuses, pagination }
  61. })
  62. .catch((error) => {
  63. store.dispatch('pushGlobalNotice', {
  64. level: 'error',
  65. messageKey: 'timeline.error',
  66. messageArgs: [error.message],
  67. timeout: 5000
  68. })
  69. })
  70. }
  71. const startFetching = ({ timeline = 'friends', credentials, store, userId = false, listId = false, tag = false }) => {
  72. const rootState = store.rootState || store.state
  73. const timelineData = rootState.statuses.timelines[camelCase(timeline)]
  74. const showImmediately = timelineData.visibleStatuses.length === 0
  75. timelineData.userId = userId
  76. timelineData.listId = listId
  77. fetchAndUpdate({ timeline, credentials, store, showImmediately, userId, listId, tag })
  78. const boundFetchAndUpdate = () =>
  79. fetchAndUpdate({ timeline, credentials, store, userId, listId, tag })
  80. return promiseInterval(boundFetchAndUpdate, 10000)
  81. }
  82. const timelineFetcher = {
  83. fetchAndUpdate,
  84. startFetching
  85. }
  86. export default timelineFetcher