logo

pleroma-fe

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

timeline_fetcher.service.js (3249B)


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