yandere_fe/src/services/timeline_fetcher/timeline_fetcher.service.js

89 lines
2.6 KiB
JavaScript
Raw Normal View History

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