notifications migrated to pinia
This commit is contained in:
parent
21319d3e52
commit
a196ed88e8
11 changed files with 122 additions and 146 deletions
|
|
@ -4,14 +4,16 @@ import {
|
|||
showDesktopNotification as swDesktopNotification,
|
||||
} from '../sw/sw.js'
|
||||
|
||||
import { useNotificationsStore } from 'src/stores/notifications.js'
|
||||
|
||||
const state = { failCreateNotif: false }
|
||||
|
||||
export const showDesktopNotification = (rootState, desktopNotificationOpts) => {
|
||||
export const showDesktopNotification = (desktopNotificationOpts) => {
|
||||
if (
|
||||
!('Notification' in window && window.Notification.permission === 'granted')
|
||||
)
|
||||
return
|
||||
if (rootState.notifications.desktopNotificationSilence) {
|
||||
if (useNotificationsStore().desktopNotificationSilence) {
|
||||
return
|
||||
}
|
||||
|
||||
|
|
@ -30,7 +32,7 @@ export const showDesktopNotification = (rootState, desktopNotificationOpts) => {
|
|||
}
|
||||
}
|
||||
|
||||
export const closeDesktopNotification = (rootState, { id }) => {
|
||||
export const closeDesktopNotification = (id) => {
|
||||
if (
|
||||
!('Notification' in window && window.Notification.permission === 'granted')
|
||||
)
|
||||
|
|
|
|||
|
|
@ -1,6 +1,8 @@
|
|||
import { showDesktopNotification } from '../desktop_notification_utils/desktop_notification_utils.js'
|
||||
import { muteFilterHits } from '../status_parser/status_parser.js'
|
||||
|
||||
import { useNotificationsStore } from 'src/stores/notifications.js'
|
||||
|
||||
import FaviconService from 'src/services/favicon_service/favicon_service.js'
|
||||
|
||||
export const ACTIONABLE_NOTIFICATION_TYPES = new Set([
|
||||
|
|
@ -11,8 +13,6 @@ export const ACTIONABLE_NOTIFICATION_TYPES = new Set([
|
|||
|
||||
let cachedBadgeUrl = null
|
||||
|
||||
export const notificationsFromStore = (store) => store.state.notifications.data
|
||||
|
||||
const visibleTypes = (notificationVisibility) => {
|
||||
return [
|
||||
notificationVisibility.likes && 'like',
|
||||
|
|
@ -69,14 +69,11 @@ const isMutedNotification = (muteFilters, notification) => {
|
|||
}
|
||||
|
||||
export const maybeShowNotification = (
|
||||
store,
|
||||
notificationVisibility,
|
||||
muteFilters,
|
||||
notification,
|
||||
i18n,
|
||||
) => {
|
||||
const rootState = store.rootState || store.state
|
||||
|
||||
if (notification.seen) return
|
||||
if (!visibleTypes(notificationVisibility).includes(notification.type)) return
|
||||
if (
|
||||
|
|
@ -86,28 +83,23 @@ export const maybeShowNotification = (
|
|||
return
|
||||
|
||||
const notificationObject = prepareNotificationObject(notification, i18n)
|
||||
showDesktopNotification(rootState, notificationObject)
|
||||
showDesktopNotification(notificationObject)
|
||||
}
|
||||
|
||||
export const filteredNotificationsFromStore = (
|
||||
store,
|
||||
notificationVisibility,
|
||||
types,
|
||||
) => {
|
||||
export const filteredNotifications = (notificationVisibility, types) => {
|
||||
// map is just to clone the array since sort mutates it and it causes some issues
|
||||
const sortedNotifications = notificationsFromStore(store).sort(sortById)
|
||||
const sortedNotifications = useNotificationsStore().data.sort(sortById)
|
||||
// TODO implement sorting elsewhere and make it optional
|
||||
return sortedNotifications.filter((notification) =>
|
||||
(types || visibleTypes(notificationVisibility)).includes(notification.type),
|
||||
)
|
||||
}
|
||||
|
||||
export const unseenNotificationsFromStore = (
|
||||
store,
|
||||
export const unseenNotifications = (
|
||||
notificationVisibility,
|
||||
ignoreInactionableSeen,
|
||||
) => {
|
||||
return filteredNotificationsFromStore(store, notificationVisibility).filter(
|
||||
return filteredNotifications(notificationVisibility).filter(
|
||||
({ seen, type }) => {
|
||||
if (!ignoreInactionableSeen) return !seen
|
||||
if (seen) return false
|
||||
|
|
|
|||
|
|
@ -3,12 +3,10 @@ import { promiseInterval } from '../promise_interval/promise_interval.js'
|
|||
import { useInstanceCapabilitiesStore } from 'src/stores/instance_capabilities.js'
|
||||
import { useInterfaceStore } from 'src/stores/interface.js'
|
||||
import { useMergedConfigStore } from 'src/stores/merged_config.js'
|
||||
import { useNotificationsStore } from 'src/stores/notifications.js'
|
||||
|
||||
import { fetchTimeline } from 'src/api/timelines.js'
|
||||
|
||||
const update = ({ store, notifications, older }) => {
|
||||
store.dispatch('addNewNotifications', { notifications, older })
|
||||
}
|
||||
//
|
||||
// For using include_types when fetching notifications.
|
||||
// Note: chat_mention excluded as pleroma-fe polls them separately
|
||||
|
|
@ -25,10 +23,9 @@ const mastoApiNotificationTypes = new Set([
|
|||
'pleroma:report',
|
||||
])
|
||||
|
||||
const fetchAndUpdate = ({ store, credentials, older = false, sinceId }) => {
|
||||
const fetchAndUpdate = ({ credentials, older = false, sinceId }) => {
|
||||
const args = { credentials }
|
||||
const rootState = store.rootState || store.state
|
||||
const timelineData = rootState.notifications
|
||||
const timelineData = useNotificationsStore()
|
||||
const hideMutedPosts = useMergedConfigStore().mergedConfig.hideMutedPosts
|
||||
|
||||
if (useInstanceCapabilitiesStore().pleromaChatMessagesAvailable) {
|
||||
|
|
@ -43,7 +40,7 @@ const fetchAndUpdate = ({ store, credentials, older = false, sinceId }) => {
|
|||
if (timelineData.minId !== Number.POSITIVE_INFINITY) {
|
||||
args.maxId = timelineData.minId
|
||||
}
|
||||
return fetchNotifications({ store, args, older })
|
||||
return fetchNotifications({ args, older })
|
||||
} else {
|
||||
// fetch new notifications
|
||||
if (
|
||||
|
|
@ -54,7 +51,7 @@ const fetchAndUpdate = ({ store, credentials, older = false, sinceId }) => {
|
|||
} else if (sinceId !== null) {
|
||||
args.sinceId = sinceId
|
||||
}
|
||||
const result = fetchNotifications({ store, args, older })
|
||||
const result = fetchNotifications({ args, older })
|
||||
|
||||
// If there's any unread notifications, try fetch notifications since
|
||||
// the newest read notification to check if any of the unread notifs
|
||||
|
|
@ -72,7 +69,7 @@ const fetchAndUpdate = ({ store, credentials, older = false, sinceId }) => {
|
|||
if (minId !== Infinity) {
|
||||
args.sinceId = null // Don't use since_id since it sorta conflicts with min_id
|
||||
args.minId = minId - 1 // go beyond
|
||||
fetchNotifications({ store, args, older })
|
||||
fetchNotifications({ args, older })
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -80,11 +77,13 @@ const fetchAndUpdate = ({ store, credentials, older = false, sinceId }) => {
|
|||
}
|
||||
}
|
||||
|
||||
const fetchNotifications = ({ store, args, older }) => {
|
||||
const fetchNotifications = ({ args, older }) => {
|
||||
return fetchTimeline(args)
|
||||
.then((response) => {
|
||||
const notifications = response.data
|
||||
update({ store, notifications, older })
|
||||
|
||||
useNotificationsStore().addNewNotifications(response)
|
||||
|
||||
return notifications
|
||||
})
|
||||
.catch((error) => {
|
||||
|
|
@ -97,7 +96,7 @@ const fetchNotifications = ({ store, args, older }) => {
|
|||
.toArray()
|
||||
.map((x) => x[1])
|
||||
.forEach((x) => mastoApiNotificationTypes.delete(x))
|
||||
return fetchNotifications({ store, args, older })
|
||||
return fetchNotifications({ args, older })
|
||||
}
|
||||
|
||||
useInterfaceStore().pushGlobalNotice({
|
||||
|
|
@ -114,7 +113,7 @@ 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)
|
||||
setTimeout(() => useNotificationsStore().setNotificationsSilence(false), 10000)
|
||||
const boundFetchAndUpdate = () => fetchAndUpdate({ credentials, store })
|
||||
boundFetchAndUpdate()
|
||||
return promiseInterval(boundFetchAndUpdate, 10000)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue