yandere_fe/src/services/notifications_fetcher/notifications_fetcher.service.js

70 lines
2.4 KiB
JavaScript
Raw Normal View History

2018-08-13 03:17:10 -07:00
import apiService from '../api/api.service.js'
2019-07-05 00:02:14 -07:00
const update = ({ store, notifications, older }) => {
2018-08-13 03:17:10 -07:00
store.dispatch('setNotificationsError', { value: false })
store.dispatch('addNewNotifications', { notifications, older })
}
2019-07-05 00:02:14 -07:00
const fetchAndUpdate = ({ store, credentials, older = false }) => {
2018-08-13 03:17:10 -07:00
const args = { credentials }
const { getters } = store
2018-08-13 03:17:10 -07:00
const rootState = store.rootState || store.state
const timelineData = rootState.statuses.notifications
const hideMutedPosts = getters.mergedConfig.hideMutedPosts
args['withMuted'] = !hideMutedPosts
2018-08-13 03:17:10 -07:00
2019-03-18 09:30:34 -07:00
args['timeline'] = 'notifications'
2018-08-13 03:17:10 -07:00
if (older) {
if (timelineData.minId !== Number.POSITIVE_INFINITY) {
args['until'] = timelineData.minId
}
2019-03-18 09:30:34 -07:00
return fetchNotifications({ store, args, older })
2018-08-13 03:17:10 -07:00
} else {
2019-03-18 09:30:34 -07:00
// fetch new notifications
if (timelineData.maxId !== Number.POSITIVE_INFINITY) {
args['since'] = timelineData.maxId
}
2019-03-18 09:30:34 -07:00
const result = fetchNotifications({ store, args, older })
// load unread notifications repeatedly to provide consistency between browser tabs
const notifications = timelineData.data
const readNotifsIds = notifications.filter(n => n.seen).map(n => n.id)
if (readNotifsIds.length) {
const possibleMax = Math.max(...readNotifsIds)
if (possibleMax !== timelineData.maxId) {
args['since'] = possibleMax
fetchNotifications({ store, args, older })
}
2019-03-18 09:30:34 -07:00
}
2018-08-13 03:17:10 -07:00
2019-03-18 09:30:34 -07:00
return result
}
}
2018-08-13 03:17:10 -07:00
2019-03-18 09:30:34 -07:00
const fetchNotifications = ({ store, args, older }) => {
2018-08-13 03:17:10 -07:00
return apiService.fetchTimeline(args)
.then((notifications) => {
2019-03-18 09:30:34 -07:00
update({ store, notifications, older })
return notifications
2018-08-13 03:17:10 -07:00
}, () => store.dispatch('setNotificationsError', { value: true }))
.catch(() => store.dispatch('setNotificationsError', { value: true }))
2018-08-13 03:17:10 -07:00
}
2019-07-05 00:02:14 -07:00
const startFetching = ({ credentials, store }) => {
2018-08-13 03:17:10 -07:00
fetchAndUpdate({ credentials, store })
const boundFetchAndUpdate = () => fetchAndUpdate({ credentials, store })
// Initially there's set flag to silence all desktop notifications so
// that there won't spam of them when user just opened up the FE we
// reset that flag after a while to show new notifications once again.
setTimeout(() => store.dispatch('setNotificationsSilence', false), 10000)
2018-08-13 03:17:10 -07:00
return setInterval(boundFetchAndUpdate, 10000)
}
const notificationsFetcher = {
fetchAndUpdate,
startFetching
}
export default notificationsFetcher