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


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