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

162 lines
3.6 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
2016-10-28 14:26:51 +02:00
import apiService from '../api/api.service.js'
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-01-29 20:40:00 +02:00
import { useInstanceStore } from 'src/stores/instance.js'
import { useInterfaceStore } from 'src/stores/interface.js'
2026-01-06 16:22:52 +02:00
const update = ({
store,
statuses,
timeline,
showImmediately,
userId,
listId,
pagination,
}) => {
2016-10-28 14:26:51 +02:00
const ccTimeline = camelCase(timeline)
2016-10-26 19:03:55 +02:00
store.dispatch('addNewStatuses', {
2016-10-28 14:26:51 +02:00
timeline: ccTimeline,
userId,
2022-08-06 17:26:43 +03:00
listId,
2016-10-28 14:26:51 +02:00
statuses,
showImmediately,
2026-01-06 16:22:52 +02:00
pagination,
2016-10-28 14:26:51 +02:00
})
}
2016-10-26 19:03:55 +02:00
const fetchAndUpdate = ({
store,
credentials,
timeline = 'friends',
older = false,
showImmediately = false,
userId = false,
2022-08-06 17:26:43 +03:00
listId = false,
statusId = false,
bookmarkFolderId = false,
tag = false,
until,
2026-01-06 16:22:52 +02:00
since,
}) => {
2016-10-28 14:26:51 +02:00
const args = { timeline, credentials }
2016-11-06 17:44:05 +01:00
const rootState = store.rootState || store.state
const { getters } = store
2016-11-06 17:44:05 +01:00
const timelineData = rootState.statuses.timelines[camelCase(timeline)]
2020-06-30 17:02:38 +03:00
const { hideMutedPosts, replyVisibility } = getters.mergedConfig
const loggedIn = !!rootState.users.currentUser
2016-10-26 19:03:55 +02:00
2016-10-28 14:26:51 +02:00
if (older) {
2022-07-31 12:35:48 +03:00
args.until = until || timelineData.minId
2016-10-28 14:26:51 +02:00
} else {
if (since === undefined) {
2022-07-31 12:35:48 +03:00
args.since = timelineData.maxId
} else if (since !== null) {
2022-07-31 12:35:48 +03:00
args.since = since
}
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.userId = userId
2022-08-06 17:26:43 +03:00
args.listId = listId
args.statusId = statusId
args.bookmarkFolderId = bookmarkFolderId
2022-07-31 12:35:48 +03:00
args.tag = tag
args.withMuted = !hideMutedPosts
2026-01-06 16:22:52 +02:00
if (
loggedIn &&
['friends', 'public', 'publicAndExternal', 'bubble'].includes(timeline)
) {
2022-07-31 12:35:48 +03:00
args.replyVisibility = replyVisibility
}
2017-06-12 16:00:46 +02:00
const numStatusesBeforeFetch = timelineData.statuses.length
2026-01-06 16:22:52 +02:00
return apiService
.fetchTimeline(args)
.then((response) => {
2020-11-10 12:52:54 +02:00
if (response.errors) {
if (timeline === 'favorites') {
2026-01-29 20:33:59 +02:00
useInstanceStore().featureSet.pleromaPublicFavouritesAvailable = false
return
}
2020-11-10 12:52:54 +02:00
throw new Error(`${response.status} ${response.statusText}`)
2019-12-05 11:48:37 +09:00
}
const { data: statuses, pagination } = response
2026-01-06 16:22:52 +02:00
if (
!older &&
statuses.length >= 20 &&
!timelineData.loading &&
numStatusesBeforeFetch > 0
) {
2022-07-31 12:35:48 +03:00
store.dispatch('queueFlush', { timeline, id: timelineData.maxId })
}
2026-01-06 16:22:52 +02:00
update({
store,
statuses,
timeline,
showImmediately,
userId,
listId,
pagination,
})
return { statuses, pagination }
2020-11-10 12:52:54 +02:00
})
.catch((error) => {
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
})
2016-10-28 14:26:51 +02:00
}
2016-10-26 19:03:55 +02:00
2026-01-06 16:22:52 +02:00
const startFetching = ({
timeline = 'friends',
credentials,
store,
userId = false,
listId = false,
statusId = false,
bookmarkFolderId = false,
tag = false,
}) => {
const rootState = store.rootState || store.state
const timelineData = rootState.statuses.timelines[camelCase(timeline)]
const showImmediately = timelineData.visibleStatuses.length === 0
timelineData.userId = userId
2022-08-06 17:26:43 +03:00
timelineData.listId = listId
timelineData.bookmarkFolderId = bookmarkFolderId
2026-01-06 16:22:52 +02:00
fetchAndUpdate({
timeline,
credentials,
store,
showImmediately,
userId,
listId,
statusId,
bookmarkFolderId,
tag,
})
const boundFetchAndUpdate = () =>
2026-01-06 16:22:52 +02:00
fetchAndUpdate({
timeline,
credentials,
store,
userId,
listId,
statusId,
bookmarkFolderId,
tag,
})
2020-09-04 11:19:53 +03:00
return promiseInterval(boundFetchAndUpdate, 10000)
2016-10-28 14:26:51 +02:00
}
const timelineFetcher = {
2016-11-06 17:44:05 +01:00
fetchAndUpdate,
2026-01-06 16:22:52 +02:00
startFetching,
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