2018-08-13 10:17:10 +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'
|
2018-08-13 10:17:10 +00:00
|
|
|
|
2019-07-05 07:02:14 +00:00
|
|
|
const update = ({ store, notifications, older }) => {
|
2018-08-13 10:17:10 +00:00
|
|
|
store.dispatch('addNewNotifications', { notifications, older })
|
|
|
|
}
|
|
|
|
|
2019-07-05 07:02:14 +00:00
|
|
|
const fetchAndUpdate = ({ store, credentials, older = false }) => {
|
2018-08-13 10:17:10 +00:00
|
|
|
const args = { credentials }
|
2019-10-06 20:28:30 +00:00
|
|
|
const { getters } = store
|
2018-08-13 10:17:10 +00:00
|
|
|
const rootState = store.rootState || store.state
|
|
|
|
const timelineData = rootState.statuses.notifications
|
2019-10-06 20:28:30 +00:00
|
|
|
const hideMutedPosts = getters.mergedConfig.hideMutedPosts
|
2019-09-04 09:19:39 +00:00
|
|
|
|
|
|
|
args['withMuted'] = !hideMutedPosts
|
2018-08-13 10:17:10 +00:00
|
|
|
|
2019-03-18 16:30:34 +00:00
|
|
|
args['timeline'] = 'notifications'
|
2018-08-13 10:17:10 +00:00
|
|
|
if (older) {
|
|
|
|
if (timelineData.minId !== Number.POSITIVE_INFINITY) {
|
|
|
|
args['until'] = timelineData.minId
|
|
|
|
}
|
2019-03-18 16:30:34 +00:00
|
|
|
return fetchNotifications({ store, args, older })
|
2018-08-13 10:17:10 +00:00
|
|
|
} else {
|
2019-03-18 16:30:34 +00:00
|
|
|
// fetch new notifications
|
2019-03-13 18:08:03 +00:00
|
|
|
if (timelineData.maxId !== Number.POSITIVE_INFINITY) {
|
2019-02-25 17:12:49 +00:00
|
|
|
args['since'] = timelineData.maxId
|
|
|
|
}
|
2019-03-18 16:30:34 +00:00
|
|
|
const result = fetchNotifications({ store, args, older })
|
|
|
|
|
2020-07-01 14:55:42 +00:00
|
|
|
// If there's any unread notifications, try fetch notifications since
|
|
|
|
// the newest read notification to check if any of the unread notifs
|
|
|
|
// have changed their 'seen' state (marked as read in another session), so
|
|
|
|
// we can update the state in this session to mark them as read as well.
|
|
|
|
// The normal maxId-check does not tell if older notifications have changed
|
2019-03-18 16:30:34 +00:00
|
|
|
const notifications = timelineData.data
|
2020-01-14 09:13:59 +00:00
|
|
|
const readNotifsIds = notifications.filter(n => n.seen).map(n => n.id)
|
2020-07-01 14:55:42 +00:00
|
|
|
const numUnseenNotifs = notifications.length - readNotifsIds.length
|
2020-07-17 09:01:01 +00:00
|
|
|
if (numUnseenNotifs > 0 && readNotifsIds.length > 0) {
|
2020-01-14 09:13:59 +00:00
|
|
|
args['since'] = Math.max(...readNotifsIds)
|
2019-03-18 16:30:34 +00:00
|
|
|
fetchNotifications({ store, args, older })
|
|
|
|
}
|
2020-09-02 17:40:47 +00:00
|
|
|
|
2019-03-18 16:30:34 +00:00
|
|
|
return result
|
|
|
|
}
|
|
|
|
}
|
2018-08-13 10:17:10 +00:00
|
|
|
|
2019-03-18 16:30:34 +00:00
|
|
|
const fetchNotifications = ({ store, args, older }) => {
|
2018-08-13 10:17:10 +00:00
|
|
|
return apiService.fetchTimeline(args)
|
2020-11-10 12:28:10 +00:00
|
|
|
.then((response) => {
|
|
|
|
if (response.errors) {
|
|
|
|
throw new Error(`${response.status} ${response.statusText}`)
|
|
|
|
}
|
|
|
|
const notifications = response.data
|
2019-03-18 16:30:34 +00:00
|
|
|
update({ store, notifications, older })
|
2019-01-29 19:04:52 +00:00
|
|
|
return notifications
|
2020-11-10 12:28:10 +00:00
|
|
|
})
|
|
|
|
.catch((error) => {
|
|
|
|
store.dispatch('pushGlobalNotice', {
|
|
|
|
level: 'error',
|
|
|
|
messageKey: 'notifications.error',
|
|
|
|
messageArgs: [error.message],
|
|
|
|
timeout: 5000
|
|
|
|
})
|
|
|
|
})
|
2018-08-13 10:17:10 +00:00
|
|
|
}
|
|
|
|
|
2019-07-05 07:02:14 +00:00
|
|
|
const startFetching = ({ credentials, store }) => {
|
2018-08-20 16:58:49 +00: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)
|
2020-09-02 19:12:50 +00:00
|
|
|
const boundFetchAndUpdate = () => fetchAndUpdate({ credentials, store })
|
|
|
|
boundFetchAndUpdate()
|
2020-09-04 08:19:53 +00:00
|
|
|
return promiseInterval(boundFetchAndUpdate, 10000)
|
2018-08-13 10:17:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const notificationsFetcher = {
|
|
|
|
fetchAndUpdate,
|
|
|
|
startFetching
|
|
|
|
}
|
|
|
|
|
|
|
|
export default notificationsFetcher
|