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