2018-08-13 03:17:10 -07:00
|
|
|
import apiService from '../api/api.service.js'
|
|
|
|
|
|
|
|
const update = ({store, notifications, older}) => {
|
|
|
|
store.dispatch('setNotificationsError', { value: false })
|
|
|
|
|
|
|
|
store.dispatch('addNewNotifications', { notifications, older })
|
|
|
|
}
|
|
|
|
|
|
|
|
const fetchAndUpdate = ({store, credentials, older = false}) => {
|
|
|
|
const args = { credentials }
|
|
|
|
const rootState = store.rootState || store.state
|
|
|
|
const timelineData = rootState.statuses.notifications
|
|
|
|
|
|
|
|
if (older) {
|
|
|
|
if (timelineData.minId !== Number.POSITIVE_INFINITY) {
|
|
|
|
args['until'] = timelineData.minId
|
|
|
|
}
|
|
|
|
} else {
|
2019-02-25 09:12:49 -08:00
|
|
|
// load unread notifications repeadedly to provide consistency between browser tabs
|
|
|
|
const notifications = timelineData.data
|
|
|
|
const unread = notifications.filter(n => !n.seen).map(n => n.id)
|
|
|
|
if (!unread.length) {
|
|
|
|
args['since'] = timelineData.maxId
|
|
|
|
} else {
|
|
|
|
args['since'] = Math.min(...unread) - 1
|
|
|
|
if (timelineData.maxId !== Math.max(...unread)) {
|
|
|
|
args['until'] = Math.max(...unread, args['since'] + 20)
|
|
|
|
}
|
|
|
|
}
|
2018-08-13 03:17:10 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
args['timeline'] = 'notifications'
|
|
|
|
|
|
|
|
return apiService.fetchTimeline(args)
|
|
|
|
.then((notifications) => {
|
|
|
|
update({store, notifications, older})
|
2019-01-29 11:04:52 -08:00
|
|
|
return notifications
|
2018-08-13 03:17:10 -07:00
|
|
|
}, () => store.dispatch('setNotificationsError', { value: true }))
|
2018-08-22 05:51:03 -07:00
|
|
|
.catch(() => store.dispatch('setNotificationsError', { value: true }))
|
2018-08-13 03:17:10 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
const startFetching = ({credentials, store}) => {
|
|
|
|
fetchAndUpdate({ credentials, store })
|
|
|
|
const boundFetchAndUpdate = () => fetchAndUpdate({ credentials, store })
|
2018-08-20 09:58:49 -07:00
|
|
|
// 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
|