pleroma-fe/src/services/notifications_fetcher/notifications_fetcher.service.js

131 lines
4.4 KiB
JavaScript
Raw Normal View History

2018-08-13 13:17:10 +03:00
import apiService from '../api/api.service.js'
2020-09-04 11:19:53 +03:00
import { promiseInterval } from '../promise_interval/promise_interval.js'
2018-08-13 13:17:10 +03:00
2026-01-29 20:40:00 +02:00
import { useInstanceStore } from 'src/stores/instance.js'
import { useInstanceCapabilitiesStore } from 'src/stores/instance_capabilities.js'
2026-01-29 20:40:00 +02:00
import { useInterfaceStore } from 'src/stores/interface.js'
2026-02-23 19:55:43 +02:00
import { useSyncConfigStore } from 'src/stores/sync_config.js'
2026-01-29 20:40:00 +02:00
2019-07-05 10:02:14 +03:00
const update = ({ store, notifications, older }) => {
2018-08-13 13:17:10 +03:00
store.dispatch('addNewNotifications', { notifications, older })
}
//
// For using include_types when fetching notifications.
// Note: chat_mention excluded as pleroma-fe polls them separately
const mastoApiNotificationTypes = new Set([
'mention',
'status',
'favourite',
'reblog',
'follow',
'follow_request',
'move',
'poll',
'pleroma:emoji_reaction',
2026-01-06 16:22:52 +02:00
'pleroma:report',
])
2018-08-13 13:17:10 +03:00
const fetchAndUpdate = ({ store, credentials, older = false, since }) => {
2018-08-13 13:17:10 +03:00
const args = { credentials }
const rootState = store.rootState || store.state
const timelineData = rootState.notifications
2026-02-23 19:55:43 +02:00
const hideMutedPosts = useSyncConfigStore().mergedConfig.hideMutedPosts
if (useInstanceCapabilitiesStore().pleromaChatMessagesAvailable) {
mastoApiNotificationTypes.add('pleroma:chat_mention')
}
2022-08-06 22:15:34 -06:00
args.includeTypes = mastoApiNotificationTypes
2022-07-31 12:35:48 +03:00
args.withMuted = !hideMutedPosts
2018-08-13 13:17:10 +03:00
2022-07-31 12:35:48 +03:00
args.timeline = 'notifications'
2018-08-13 13:17:10 +03:00
if (older) {
if (timelineData.minId !== Number.POSITIVE_INFINITY) {
2022-07-31 12:35:48 +03:00
args.until = timelineData.minId
2018-08-13 13:17:10 +03:00
}
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
2026-01-06 16:22:52 +02:00
if (
since === undefined &&
timelineData.maxId !== Number.POSITIVE_INFINITY
) {
2022-07-31 12:35:48 +03:00
args.since = timelineData.maxId
} else if (since !== null) {
2022-07-31 12:35:48 +03:00
args.since = since
}
2019-03-18 12:30:34 -04:00
const result = fetchNotifications({ store, args, older })
// 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 12:30:34 -04:00
const notifications = timelineData.data
2026-01-06 16:22:52 +02:00
const readNotifsIds = notifications.filter((n) => n.seen).map((n) => n.id)
const unreadNotifsIds = notifications
.filter((n) => !n.seen)
.map((n) => n.id)
if (readNotifsIds.length > 0 && readNotifsIds.length > 0) {
const minId = Math.min(...unreadNotifsIds) // Oldest known unread notification
2023-11-21 15:29:49 +02:00
if (minId !== Infinity) {
args.since = false // Don't use since_id since it sorta conflicts with min_id
args.minId = minId - 1 // go beyond
fetchNotifications({ store, args, older })
}
2019-03-18 12:30:34 -04: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 }) => {
2026-01-06 16:22:52 +02:00
return apiService
.fetchTimeline(args)
.then((response) => {
if (response.errors) {
2026-01-06 16:22:52 +02:00
if (
response.status === 400 &&
response.statusText.includes('Invalid value for enum')
) {
response.statusText
2025-06-29 15:24:04 +03:00
.matchAll(/(\w+) - Invalid value for enum./g)
.toArray()
2026-01-06 16:22:52 +02:00
.map((x) => x[1])
.forEach((x) => mastoApiNotificationTypes.delete(x))
return fetchNotifications({ store, args, older })
} else {
throw new Error(`${response.status} ${response.statusText}`)
}
}
const notifications = response.data
2019-03-18 12:30:34 -04:00
update({ store, notifications, older })
return notifications
})
.catch((error) => {
2023-04-05 21:06:37 -06:00
useInterfaceStore().pushGlobalNotice({
level: 'error',
messageKey: 'notifications.error',
messageArgs: [error.message],
2026-01-06 16:22:52 +02:00
timeout: 5000,
})
2020-12-04 12:48:15 +02:00
console.error(error)
})
2018-08-13 13:17:10 +03:00
}
2019-07-05 10:02:14 +03:00
const startFetching = ({ 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)
const boundFetchAndUpdate = () => fetchAndUpdate({ credentials, store })
boundFetchAndUpdate()
2020-09-04 11:19:53 +03:00
return promiseInterval(boundFetchAndUpdate, 10000)
2018-08-13 13:17:10 +03:00
}
const notificationsFetcher = {
fetchAndUpdate,
2026-01-06 16:22:52 +02:00
startFetching,
2018-08-13 13:17:10 +03:00
}
export default notificationsFetcher