2016-10-28 12:26:51 +00:00
|
|
|
import { camelCase } from 'lodash'
|
2016-10-26 17:03:55 +00:00
|
|
|
|
2016-10-28 12:26:51 +00:00
|
|
|
import apiService from '../api/api.service.js'
|
2016-10-26 17:03:55 +00:00
|
|
|
|
2016-10-28 12:26:51 +00:00
|
|
|
const update = ({store, statuses, timeline, showImmediately}) => {
|
|
|
|
const ccTimeline = camelCase(timeline)
|
2016-10-26 17:03:55 +00:00
|
|
|
|
2017-03-09 12:38:32 +00:00
|
|
|
store.dispatch('setError', { value: false })
|
2017-03-07 20:38:55 +00:00
|
|
|
|
2016-11-18 21:56:20 +00:00
|
|
|
store.dispatch('addNewStatuses', {
|
2016-10-28 12:26:51 +00:00
|
|
|
timeline: ccTimeline,
|
|
|
|
statuses,
|
|
|
|
showImmediately
|
|
|
|
})
|
|
|
|
}
|
2016-10-26 17:03:55 +00:00
|
|
|
|
2018-08-16 10:12:31 +00:00
|
|
|
const fetchAndUpdate = ({store, credentials, timeline = 'friends', older = false, showImmediately = false, userId = false, tag = false, until}) => {
|
2016-10-28 12:26:51 +00:00
|
|
|
const args = { timeline, credentials }
|
2016-11-06 16:44:05 +00:00
|
|
|
const rootState = store.rootState || store.state
|
|
|
|
const timelineData = rootState.statuses.timelines[camelCase(timeline)]
|
2016-10-26 17:03:55 +00:00
|
|
|
|
2016-10-28 12:26:51 +00:00
|
|
|
if (older) {
|
2018-08-16 10:12:31 +00:00
|
|
|
args['until'] = until || timelineData.minVisibleId
|
2016-10-28 12:26:51 +00:00
|
|
|
} else {
|
|
|
|
args['since'] = timelineData.maxId
|
|
|
|
}
|
2016-10-26 17:03:55 +00:00
|
|
|
|
2017-06-12 14:00:46 +00:00
|
|
|
args['userId'] = userId
|
2017-09-17 11:26:35 +00:00
|
|
|
args['tag'] = tag
|
2017-06-12 14:00:46 +00:00
|
|
|
|
2016-11-06 16:44:05 +00:00
|
|
|
return apiService.fetchTimeline(args)
|
2017-11-21 14:12:47 +00:00
|
|
|
.then((statuses) => {
|
2018-04-13 19:35:55 +00:00
|
|
|
if (!older && statuses.length >= 20 && !timelineData.loading) {
|
2017-11-21 14:12:47 +00:00
|
|
|
store.dispatch('queueFlush', { timeline: timeline, id: timelineData.maxId })
|
|
|
|
}
|
|
|
|
update({store, statuses, timeline, showImmediately})
|
|
|
|
}, () => store.dispatch('setError', { value: true }))
|
2016-10-28 12:26:51 +00:00
|
|
|
}
|
2016-10-26 17:03:55 +00:00
|
|
|
|
2017-09-17 11:26:35 +00:00
|
|
|
const startFetching = ({timeline = 'friends', credentials, store, userId = false, tag = false}) => {
|
2017-11-23 11:46:37 +00:00
|
|
|
const rootState = store.rootState || store.state
|
|
|
|
const timelineData = rootState.statuses.timelines[camelCase(timeline)]
|
|
|
|
const showImmediately = timelineData.visibleStatuses.length === 0
|
|
|
|
fetchAndUpdate({timeline, credentials, store, showImmediately, userId, tag})
|
2017-09-17 11:26:35 +00:00
|
|
|
const boundFetchAndUpdate = () => fetchAndUpdate({ timeline, credentials, store, userId, tag })
|
2017-02-16 10:17:47 +00:00
|
|
|
return setInterval(boundFetchAndUpdate, 10000)
|
2016-10-28 12:26:51 +00:00
|
|
|
}
|
|
|
|
const timelineFetcher = {
|
2016-11-06 16:44:05 +00:00
|
|
|
fetchAndUpdate,
|
2016-10-28 12:26:51 +00:00
|
|
|
startFetching
|
|
|
|
}
|
2016-10-26 17:03:55 +00:00
|
|
|
|
2016-10-28 12:26:51 +00:00
|
|
|
export default timelineFetcher
|