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 (3003B)


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