2018-08-13 13:17:10 +03:00
|
|
|
import apiService from '../api/api.service.js'
|
|
|
|
|
2019-07-05 10:02:14 +03:00
|
|
|
const update = ({ store, notifications, older }) => {
|
2018-08-13 13:17:10 +03:00
|
|
|
store.dispatch('setNotificationsError', { value: false })
|
|
|
|
store.dispatch('addNewNotifications', { notifications, older })
|
|
|
|
}
|
|
|
|
|
2019-07-05 10:02:14 +03:00
|
|
|
const fetchAndUpdate = ({ store, credentials, older = false }) => {
|
2018-08-13 13:17:10 +03:00
|
|
|
const args = { credentials }
|
2019-10-06 23:28:30 +03:00
|
|
|
const { getters } = store
|
2018-08-13 13:17:10 +03:00
|
|
|
const rootState = store.rootState || store.state
|
|
|
|
const timelineData = rootState.statuses.notifications
|
2019-10-06 23:28:30 +03:00
|
|
|
const hideMutedPosts = getters.mergedConfig.hideMutedPosts
|
2020-02-04 04:37:29 +09:00
|
|
|
const allowFollowingMove = rootState.users.currentUser.allow_following_move
|
2019-09-04 12:19:39 +03:00
|
|
|
|
|
|
|
args['withMuted'] = !hideMutedPosts
|
2018-08-13 13:17:10 +03:00
|
|
|
|
2020-02-04 04:37:29 +09:00
|
|
|
args['withMove'] = !allowFollowingMove
|
|
|
|
|
2019-03-18 12:30:34 -04:00
|
|
|
args['timeline'] = 'notifications'
|
2018-08-13 13:17:10 +03:00
|
|
|
if (older) {
|
|
|
|
if (timelineData.minId !== Number.POSITIVE_INFINITY) {
|
|
|
|
args['until'] = timelineData.minId
|
|
|
|
}
|
2019-03-18 12:30:34 -04:00
|
|
|
return fetchNotifications({ store, args, older })
|
2018-08-13 13:17:10 +03:00
|
|
|
} else {
|
2019-03-18 12:30:34 -04:00
|
|
|
// fetch new notifications
|
2019-03-13 14:08:03 -04:00
|
|
|
if (timelineData.maxId !== Number.POSITIVE_INFINITY) {
|
2019-02-25 12:12:49 -05:00
|
|
|
args['since'] = timelineData.maxId
|
|
|
|
}
|
2019-03-18 12:30:34 -04:00
|
|
|
const result = fetchNotifications({ store, args, older })
|
|
|
|
|
|
|
|
// load unread notifications repeatedly to provide consistency between browser tabs
|
|
|
|
const notifications = timelineData.data
|
2020-01-14 11:13:59 +02:00
|
|
|
const readNotifsIds = notifications.filter(n => n.seen).map(n => n.id)
|
|
|
|
if (readNotifsIds.length) {
|
|
|
|
args['since'] = Math.max(...readNotifsIds)
|
2019-03-18 12:30:34 -04:00
|
|
|
fetchNotifications({ store, args, older })
|
|
|
|
}
|
2018-08-13 13:17:10 +03:00
|
|
|
|
2019-03-18 12:30:34 -04:00
|
|
|
return result
|
|
|
|
}
|
|
|
|
}
|
2018-08-13 13:17:10 +03:00
|
|
|
|
2019-03-18 12:30:34 -04:00
|
|
|
const fetchNotifications = ({ store, args, older }) => {
|
2018-08-13 13:17:10 +03:00
|
|
|
return apiService.fetchTimeline(args)
|
|
|
|
.then((notifications) => {
|
2019-03-18 12:30:34 -04:00
|
|
|
update({ store, notifications, older })
|
2019-01-29 21:04:52 +02:00
|
|
|
return notifications
|
2018-08-13 13:17:10 +03:00
|
|
|
}, () => store.dispatch('setNotificationsError', { value: true }))
|
2018-08-22 15:51:03 +03:00
|
|
|
.catch(() => store.dispatch('setNotificationsError', { value: true }))
|
2018-08-13 13:17:10 +03:00
|
|
|
}
|
|
|
|
|
2019-07-05 10:02:14 +03:00
|
|
|
const startFetching = ({ credentials, store }) => {
|
2018-08-13 13:17:10 +03:00
|
|
|
fetchAndUpdate({ credentials, store })
|
|
|
|
const boundFetchAndUpdate = () => fetchAndUpdate({ credentials, store })
|
2018-08-20 19:58:49 +03: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 13:17:10 +03:00
|
|
|
return setInterval(boundFetchAndUpdate, 10000)
|
|
|
|
}
|
|
|
|
|
|
|
|
const notificationsFetcher = {
|
|
|
|
fetchAndUpdate,
|
|
|
|
startFetching
|
|
|
|
}
|
|
|
|
|
|
|
|
export default notificationsFetcher
|