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'
|
2020-09-04 08:19:53 +00:00
|
|
|
import { promiseInterval } from '../promise_interval/promise_interval.js'
|
2016-10-26 17:03:55 +00:00
|
|
|
|
2020-07-03 19:45:49 +00:00
|
|
|
const update = ({ store, statuses, timeline, showImmediately, userId, pagination }) => {
|
2016-10-28 12:26:51 +00:00
|
|
|
const ccTimeline = camelCase(timeline)
|
2016-10-26 17:03:55 +00:00
|
|
|
|
2016-11-18 21:56:20 +00:00
|
|
|
store.dispatch('addNewStatuses', {
|
2016-10-28 12:26:51 +00:00
|
|
|
timeline: ccTimeline,
|
2018-12-03 06:29:33 +00:00
|
|
|
userId,
|
2016-10-28 12:26:51 +00:00
|
|
|
statuses,
|
2020-07-03 19:45:49 +00:00
|
|
|
showImmediately,
|
|
|
|
pagination
|
2016-10-28 12:26:51 +00:00
|
|
|
})
|
|
|
|
}
|
2016-10-26 17:03:55 +00:00
|
|
|
|
2019-10-06 20:28:30 +00:00
|
|
|
const fetchAndUpdate = ({
|
|
|
|
store,
|
|
|
|
credentials,
|
|
|
|
timeline = 'friends',
|
|
|
|
older = false,
|
|
|
|
showImmediately = false,
|
|
|
|
userId = false,
|
|
|
|
tag = false,
|
2021-01-13 20:17:10 +00:00
|
|
|
until,
|
|
|
|
since
|
2019-10-06 20:28:30 +00:00
|
|
|
}) => {
|
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
|
2019-10-06 20:28:30 +00:00
|
|
|
const { getters } = store
|
2016-11-06 16:44:05 +00:00
|
|
|
const timelineData = rootState.statuses.timelines[camelCase(timeline)]
|
2020-06-30 14:02:38 +00:00
|
|
|
const { hideMutedPosts, replyVisibility } = getters.mergedConfig
|
|
|
|
const loggedIn = !!rootState.users.currentUser
|
2016-10-26 17:03:55 +00:00
|
|
|
|
2016-10-28 12:26:51 +00:00
|
|
|
if (older) {
|
2019-02-28 01:45:08 +00:00
|
|
|
args['until'] = until || timelineData.minId
|
2016-10-28 12:26:51 +00:00
|
|
|
} else {
|
2021-01-13 20:17:10 +00:00
|
|
|
if (since === undefined) {
|
|
|
|
args['since'] = timelineData.maxId
|
|
|
|
} else if (since !== null) {
|
|
|
|
args['since'] = since
|
|
|
|
}
|
2016-10-28 12:26:51 +00:00
|
|
|
}
|
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
|
2019-03-02 13:07:14 +00:00
|
|
|
args['withMuted'] = !hideMutedPosts
|
2020-07-17 06:33:18 +00:00
|
|
|
if (loggedIn && ['friends', 'public', 'publicAndExternal'].includes(timeline)) {
|
|
|
|
args['replyVisibility'] = replyVisibility
|
|
|
|
}
|
2017-06-12 14:00:46 +00:00
|
|
|
|
2019-01-29 16:40:49 +00:00
|
|
|
const numStatusesBeforeFetch = timelineData.statuses.length
|
|
|
|
|
2016-11-06 16:44:05 +00:00
|
|
|
return apiService.fetchTimeline(args)
|
2020-07-03 19:45:49 +00:00
|
|
|
.then(response => {
|
2020-11-10 10:52:54 +00:00
|
|
|
if (response.errors) {
|
|
|
|
throw new Error(`${response.status} ${response.statusText}`)
|
2019-12-05 02:48:37 +00:00
|
|
|
}
|
2020-07-03 19:45:49 +00:00
|
|
|
|
|
|
|
const { data: statuses, pagination } = response
|
2019-01-29 16:40:49 +00:00
|
|
|
if (!older && statuses.length >= 20 && !timelineData.loading && numStatusesBeforeFetch > 0) {
|
2017-11-21 14:12:47 +00:00
|
|
|
store.dispatch('queueFlush', { timeline: timeline, id: timelineData.maxId })
|
|
|
|
}
|
2020-07-03 19:45:49 +00:00
|
|
|
update({ store, statuses, timeline, showImmediately, userId, pagination })
|
|
|
|
return { statuses, pagination }
|
2020-11-10 10:52:54 +00:00
|
|
|
})
|
|
|
|
.catch((error) => {
|
2021-03-09 00:38:10 +00:00
|
|
|
store.dispatch('pushGlobalNotice', {
|
|
|
|
level: 'error',
|
|
|
|
messageKey: 'timeline.error',
|
|
|
|
messageArgs: [error.message],
|
|
|
|
timeout: 5000
|
|
|
|
})
|
2020-11-10 10:52:54 +00:00
|
|
|
})
|
2016-10-28 12:26:51 +00:00
|
|
|
}
|
2016-10-26 17:03:55 +00:00
|
|
|
|
2019-07-05 07:02:14 +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
|
2018-12-03 06:29:33 +00:00
|
|
|
timelineData.userId = userId
|
2020-09-02 19:12:50 +00:00
|
|
|
fetchAndUpdate({ timeline, credentials, store, showImmediately, userId, tag })
|
2020-09-02 17:40:47 +00:00
|
|
|
const boundFetchAndUpdate = () =>
|
2020-09-02 19:12:50 +00:00
|
|
|
fetchAndUpdate({ timeline, credentials, store, userId, tag })
|
2021-12-05 15:02:10 +00:00
|
|
|
return promiseInterval(boundFetchAndUpdate, 20000)
|
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
|