pleroma-fe/src/services/timeline_fetcher/timeline_fetcher.service.js

146 lines
3.5 KiB
JavaScript
Raw Normal View History

2016-10-28 14:26:51 +02:00
import { camelCase } from 'lodash'
2026-01-08 17:26:52 +02:00
2020-09-04 11:19:53 +03:00
import { promiseInterval } from '../promise_interval/promise_interval.js'
2016-10-26 19:03:55 +02:00
import { useInstanceCapabilitiesStore } from 'src/stores/instance_capabilities.js'
2026-01-29 20:40:00 +02:00
import { useInterfaceStore } from 'src/stores/interface.js'
import { useMergedConfigStore } from 'src/stores/merged_config.js'
2026-08-10 22:26:22 +03:00
import { useStatusesStore } from 'src/stores/statuses.js'
2026-08-11 18:54:54 +03:00
import { ARGUMENT_MAP, useTimelinesStore } from 'src/stores/timelines.js'
2026-08-10 15:00:59 +03:00
import { useUsersStore } from 'src/stores/users.js'
2026-01-29 20:40:00 +02:00
2026-06-25 13:28:05 +03:00
import { fetchTimeline } from 'src/api/timelines.js'
2026-08-11 18:54:54 +03:00
const REPLY_VISIBILITY_TIMELINES = new Set([
'friends',
'public',
'publicAndExternal',
'bubble',
])
2016-10-26 19:03:55 +02:00
const fetchAndUpdate = ({
2026-08-11 18:54:54 +03:00
timeline,
argument,
credentials,
2026-08-11 18:54:54 +03:00
}, {
2026-06-16 23:29:19 +03:00
maxId,
sinceId,
2026-08-11 18:54:54 +03:00
older = false,
showImmediately = false,
}) => {
2026-08-11 18:54:54 +03:00
timeline.loading = true
const { hideMutedPosts, replyVisibility } =
useMergedConfigStore().mergedConfig
2026-08-11 18:54:54 +03:00
const loggedIn = useUsersStore().loggedIn
const args = { timeline: timeline.name, credentials }
args[ARGUMENT_MAP[timeline.name]] = argument
2016-10-26 19:03:55 +02:00
2016-10-28 14:26:51 +02:00
if (older) {
2026-06-22 15:31:46 +03:00
// When minId = 0 we need to fetch without maxId param
2026-08-11 18:54:54 +03:00
args.maxId = maxId || timeline.minId || null
2016-10-28 14:26:51 +02:00
} else {
if (sinceId === undefined) {
2026-08-11 18:54:54 +03:00
args.sinceId = timeline.maxId
} else if (sinceId !== null) {
args.sinceId = sinceId
}
2016-10-28 14:26:51 +02:00
}
2016-10-26 19:03:55 +02:00
2022-07-31 12:35:48 +03:00
args.withMuted = !hideMutedPosts
2026-08-11 18:54:54 +03:00
if (loggedIn && REPLY_VISIBILITY_TIMELINES.has(timeline)) {
2022-07-31 12:35:48 +03:00
args.replyVisibility = replyVisibility
}
2017-06-12 16:00:46 +02:00
2026-08-11 18:54:54 +03:00
const numStatusesBeforeFetch = timeline.statuses.size
2026-06-13 03:10:00 +03:00
return fetchTimeline(args)
2026-01-06 16:22:52 +02:00
.then((response) => {
2026-08-11 18:54:54 +03:00
const { data: statuses, pagination, timestamp } = response
2026-01-06 16:22:52 +02:00
if (
!older &&
statuses.length >= 20 &&
2026-08-11 18:54:54 +03:00
!timeline.loading &&
2026-01-06 16:22:52 +02:00
numStatusesBeforeFetch > 0
) {
2026-08-11 18:54:54 +03:00
useTimelinesStore().queueFlush(timeline.name, timeline.maxId)
}
2026-08-11 18:54:54 +03:00
const processed = useStatusesStore()
.addNewStatuses({ statuses, timestamp })
.filter(Boolean)
console.log(timeline.name, argument, showImmediately, processed.length)
useTimelinesStore().addStatusesToTimeline(
timeline.name,
argument,
{
statuses,
showImmediately,
pagination,
}
)
return { statuses, pagination }
2020-11-10 12:52:54 +02:00
})
.catch((error) => {
2026-06-22 20:15:35 +03:00
if (error.statusCode === 403 && timeline === 'favorites') {
useInstanceCapabilitiesStore().pleromaPublicFavouritesAvailable = false
return
}
2023-04-05 21:06:37 -06:00
useInterfaceStore().pushGlobalNotice({
level: 'error',
messageKey: 'timeline.error',
messageArgs: [error.message],
2026-01-06 16:22:52 +02:00
timeout: 5000,
})
2020-11-10 12:52:54 +02:00
})
2026-08-11 18:54:54 +03:00
.finally(() => {
timeline.loading = false
})
2016-10-28 14:26:51 +02:00
}
2016-10-26 19:03:55 +02:00
2026-08-11 20:25:43 +03:00
const timelineFetcher = (timeline, argument, credentials) => {
2026-08-11 18:54:54 +03:00
const state = {
interval: null
}
const boundFetchAndUpdate = ({
showImmediately,
maxId,
sinceId,
older,
} = {}) => fetchAndUpdate({
2026-01-06 16:22:52 +02:00
timeline,
2026-08-11 18:54:54 +03:00
argument,
2026-01-06 16:22:52 +02:00
credentials,
2026-08-11 18:54:54 +03:00
}, {
maxId,
sinceId,
older,
2026-01-06 16:22:52 +02:00
showImmediately,
})
2026-08-11 18:54:54 +03:00
const startFetching = () => {
if (state.interval) throw new Error('Interval already exists!')
boundFetchAndUpdate({
showImmediately: timeline.visibleStatusesIds.size === 0
2026-01-06 16:22:52 +02:00
})
2026-08-11 18:54:54 +03:00
state.interval = promiseInterval(boundFetchAndUpdate, 10000)
}
const stopFetching = () => {
state.interval.stop()
state.interval = null
}
return {
startFetching,
stopFetching,
fetchAndUpdate: boundFetchAndUpdate,
}
2016-10-28 14:26:51 +02:00
}
2016-10-26 19:03:55 +02:00
2016-10-28 14:26:51 +02:00
export default timelineFetcher