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

50 lines
1.5 KiB
JavaScript

import { camelCase } from 'lodash'
import apiService from '../api/api.service.js'
const update = ({store, statuses, timeline, showImmediately}) => {
const ccTimeline = camelCase(timeline)
store.dispatch('setError', { value: false })
store.dispatch('addNewStatuses', {
timeline: ccTimeline,
statuses,
showImmediately
})
}
const fetchAndUpdate = ({store, credentials, timeline = 'friends', older = false, showImmediately = false, userId = false, tag = false}) => {
const args = { timeline, credentials }
const rootState = store.rootState || store.state
const timelineData = rootState.statuses.timelines[camelCase(timeline)]
if (older) {
args['until'] = timelineData.minVisibleId
} else {
args['since'] = timelineData.maxId
}
args['userId'] = userId
args['tag'] = tag
return apiService.fetchTimeline(args)
.then((statuses) => {
if (!older && statuses.length >= 20) {
store.dispatch('queueFlush', { timeline: timeline, id: timelineData.maxId })
}
update({store, statuses, timeline, showImmediately})
}, () => store.dispatch('setError', { value: true }))
}
const startFetching = ({timeline = 'friends', credentials, store, userId = false, tag = false}) => {
fetchAndUpdate({timeline, credentials, store, showImmediately: true, userId, tag})
const boundFetchAndUpdate = () => fetchAndUpdate({ timeline, credentials, store, userId, tag })
return setInterval(boundFetchAndUpdate, 10000)
}
const timelineFetcher = {
fetchAndUpdate,
startFetching
}
export default timelineFetcher