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