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
|
|
|
|
2026-02-05 00:28:45 +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'
|
2026-03-24 21:42:22 +02:00
|
|
|
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-06-17 17:58:14 +03:00
|
|
|
|
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
|
|
|
|
2019-10-06 23:28:30 +03:00
|
|
|
const fetchAndUpdate = ({
|
2026-08-11 18:54:54 +03:00
|
|
|
timeline,
|
|
|
|
|
argument,
|
2019-10-06 23:28:30 +03:00
|
|
|
credentials,
|
2026-08-11 18:54:54 +03:00
|
|
|
}, {
|
2026-06-16 23:29:19 +03:00
|
|
|
maxId,
|
2026-06-16 23:14:52 +03:00
|
|
|
sinceId,
|
2026-08-11 18:54:54 +03:00
|
|
|
older = false,
|
|
|
|
|
showImmediately = false,
|
2019-10-06 23:28:30 +03:00
|
|
|
}) => {
|
2026-08-11 18:54:54 +03:00
|
|
|
timeline.loading = true
|
2026-03-24 21:42:22 +02:00
|
|
|
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 {
|
2026-06-16 23:14:52 +03:00
|
|
|
if (sinceId === undefined) {
|
2026-08-11 18:54:54 +03:00
|
|
|
args.sinceId = timeline.maxId
|
2026-06-16 23:14:52 +03:00
|
|
|
} else if (sinceId !== null) {
|
|
|
|
|
args.sinceId = sinceId
|
2021-01-13 22:17:10 +02:00
|
|
|
}
|
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
|
2020-07-17 09:33:18 +03:00
|
|
|
}
|
2017-06-12 16:00:46 +02:00
|
|
|
|
2026-08-11 18:54:54 +03:00
|
|
|
const numStatusesBeforeFetch = timeline.statuses.size
|
2019-01-29 18:40:49 +02:00
|
|
|
|
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)
|
2017-11-21 16:12:47 +02:00
|
|
|
}
|
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,
|
|
|
|
|
}
|
|
|
|
|
)
|
2020-07-03 19:45:49 +00:00
|
|
|
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({
|
2021-03-09 02:38:10 +02:00
|
|
|
level: 'error',
|
|
|
|
|
messageKey: 'timeline.error',
|
|
|
|
|
messageArgs: [error.message],
|
2026-01-06 16:22:52 +02:00
|
|
|
timeout: 5000,
|
2021-03-09 02:38:10 +02:00
|
|
|
})
|
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
|