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

54 lines
1.5 KiB
JavaScript
Raw Normal View History

2016-10-28 14:26:51 +02:00
import { camelCase } from 'lodash'
2016-10-26 19:03:55 +02:00
2016-10-28 14:26:51 +02:00
import apiService from '../api/api.service.js'
2016-10-26 19:03:55 +02:00
2016-10-28 14:26:51 +02:00
const update = ({store, statuses, timeline, showImmediately}) => {
const ccTimeline = camelCase(timeline)
2016-10-26 19:03:55 +02:00
store.dispatch('setError', { value: false })
2017-03-07 21:38:55 +01:00
store.dispatch('addNewStatuses', {
2016-10-28 14:26:51 +02:00
timeline: ccTimeline,
statuses,
showImmediately
})
}
2016-10-26 19:03:55 +02:00
2016-10-28 14:26:51 +02:00
const fetchAndUpdate = ({store, credentials, timeline = 'friends', older = false, showImmediately = false}) => {
2016-11-06 17:44:05 +01:00
const rootState = store.rootState || store.state
const timelineData = rootState.statuses.timelines[camelCase(timeline)]
2016-10-26 19:03:55 +02:00
2017-06-04 15:22:41 +02:00
return fetchStatuses({timelineData, store, credentials, timeline, older})
.then((statuses) => update({store, statuses, timeline, showImmediately}),
() => store.dispatch('setError', { value: true }))
}
const fetchStatuses = ({timelineData, credentials, timeline = 'friends', older = false, userId = false}) => {
const args = { timeline, credentials }
2016-10-28 14:26:51 +02:00
if (older) {
args['until'] = timelineData.minVisibleId
} else {
args['since'] = timelineData.maxId
}
2016-10-26 19:03:55 +02:00
2017-06-04 15:22:41 +02:00
if (timeline === 'user') {
args['userId'] = userId
}
2016-11-06 17:44:05 +01:00
return apiService.fetchTimeline(args)
2016-10-28 14:26:51 +02:00
}
2016-10-26 19:03:55 +02:00
2016-10-28 14:26:51 +02:00
const startFetching = ({ timeline = 'friends', credentials, store }) => {
fetchAndUpdate({timeline, credentials, store, showImmediately: true})
const boundFetchAndUpdate = () => fetchAndUpdate({ timeline, credentials, store })
return setInterval(boundFetchAndUpdate, 10000)
2016-10-28 14:26:51 +02:00
}
const timelineFetcher = {
2016-11-06 17:44:05 +01:00
fetchAndUpdate,
2017-06-04 15:22:41 +02:00
startFetching,
fetchStatuses
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