pleroma-fe/src/stores/notifications.js

189 lines
5.7 KiB
JavaScript
Raw Normal View History

2026-08-10 17:43:24 +03:00
import { defineStore } from 'pinia'
2026-01-06 16:23:17 +02:00
import {
closeAllDesktopNotifications,
closeDesktopNotification,
} from '../services/desktop_notification_utils/desktop_notification_utils.js'
import {
isValidNotification,
2026-01-06 16:22:52 +02:00
maybeShowNotification,
} from '../services/notification_utils/notification_utils.js'
2026-08-11 16:33:43 +03:00
import {
isStatusNotification,
} from '../services/notification_utils/notification_utils_sw.js'
import { useI18nStore } from 'src/stores/i18n.js'
import { useMergedConfigStore } from 'src/stores/merged_config.js'
2026-06-16 17:32:26 +03:00
import { useOAuthStore } from 'src/stores/oauth.js'
2026-01-29 20:40:00 +02:00
import { useReportsStore } from 'src/stores/reports.js'
2026-08-10 22:26:22 +03:00
import { useStatusesStore } from 'src/stores/statuses.js'
2026-02-13 13:59:00 +02:00
import { useSyncConfigStore } from 'src/stores/sync_config.js'
2026-08-10 15:00:59 +03:00
import { useUsersStore } from 'src/stores/users.js'
2026-01-29 20:40:00 +02:00
import { dismissNotification, markNotificationsAsSeen } from 'src/api/user.js'
2026-06-15 20:02:22 +03:00
2026-08-10 17:43:24 +03:00
export const defaultState = () => ({
desktopNotificationSilence: true,
maxId: 0,
minId: Number.POSITIVE_INFINITY,
data: [],
2026-08-10 17:43:24 +03:00
statusNotificationRelations: new WeakMap(),
idStore: new Map(),
2026-01-06 16:22:52 +02:00
loading: false,
})
2026-08-10 17:43:24 +03:00
export const useNotificationsStore = defineStore('notifications', {
state: defaultState,
actions: {
clearNotifications() {
2025-02-04 15:23:21 +02:00
const blankState = defaultState()
2026-08-10 17:43:24 +03:00
Object.keys(defaultState()).forEach((k) => {
this[k] = blankState[k]
2025-02-04 15:23:21 +02:00
})
},
2026-08-10 17:43:24 +03:00
updateNotificationsMinMaxId(id) {
this.maxId = id > this.maxId ? id : this.maxId
this.minId = id < this.minId ? id : this.minId
},
2026-08-10 17:43:24 +03:00
setNotificationsLoading(value) {
this.loading = value
},
2026-08-10 17:43:24 +03:00
setNotificationsSilence(value) {
this.desktopNotificationSilence = value
},
2026-08-10 17:43:24 +03:00
updateNotification({ id, updater }) {
const notification = this.idStore.get(id)
notification && updater(notification)
2026-01-06 16:22:52 +02:00
},
2026-08-10 17:43:24 +03:00
addNewNotifications(result) {
const { timestamp, data: notifications } = result
useUsersStore().addNewUsers({
timestamp,
data: notifications.map((n) => n.from_profile),
})
notifications.forEach(
(n) => (n.from_profile = useUsersStore().findUser(n.from_profile.id)),
)
const validNotifications = notifications.filter((notification) => {
// If invalid notification, update ids but don't add it to store
if (!isValidNotification(notification)) {
console.error('Invalid notification:', notification)
2026-08-10 17:43:24 +03:00
this.updateNotificationsMinMaxId(notification.id)
return false
}
return true
})
2026-01-06 16:22:52 +02:00
const statusNotifications = validNotifications.filter(
(notification) =>
isStatusNotification(notification.type) && notification.status,
)
// Synchronous commit to add all the statuses
2026-08-10 22:26:22 +03:00
useStatusesStore().addNewStatuses({
2026-08-10 17:43:24 +03:00
timestamp,
2026-01-06 16:22:52 +02:00
statuses: statusNotifications.map(
(notification) => notification.status,
),
})
// Update references to statuses in notifications to ones in the store
2026-01-06 16:22:52 +02:00
statusNotifications.forEach((notification) => {
const id = notification.status.id
2026-08-10 22:26:22 +03:00
const referenceStatus = useStatusesStore().allStatuses.get(id)
if (referenceStatus) {
notification.status = referenceStatus
}
})
2026-01-06 16:22:52 +02:00
validNotifications.forEach((notification) => {
if (notification.type === 'pleroma:report') {
2025-01-30 21:56:07 +02:00
useReportsStore().addReport(notification.report)
}
if (notification.type === 'pleroma:emoji_reaction') {
2026-08-10 22:26:22 +03:00
useStatusesStore().fetchEmojiReactionsBy(notification.status.id)
}
// Only add a new notification if we don't have one for the same action
2026-08-10 17:43:24 +03:00
if (!this.idStore.has(notification.id)) {
this.updateNotificationsMinMaxId(notification.id)
notifications.forEach((notification) => {
this.data.push(notification)
this.idStore.set(notification.id, notification)
})
this.statusNotificationRelations.set(
notification.status,
this.idStore.get(notification.id),
)
maybeShowNotification(
useMergedConfigStore().mergedConfig.notificationVisibility,
2026-02-13 13:59:00 +02:00
Object.values(useSyncConfigStore().prefsStorage.simple.muteFilters),
2026-01-06 16:22:52 +02:00
notification,
useI18nStore().i18n,
)
} else if (notification.seen) {
2026-08-10 17:43:24 +03:00
this.idStore.get(notification.id).seen = true
}
})
},
2026-08-10 17:43:24 +03:00
notificationClicked(id) {
const notification = this.idStore.get(id)
2023-11-16 20:41:41 +02:00
const { type, seen } = notification
if (!seen) {
switch (type) {
case 'mention':
case 'pleroma:report':
case 'follow_request':
break
default:
2026-08-10 17:43:24 +03:00
this.markSingleNotificationAsSeen({ id })
2023-11-16 20:41:41 +02:00
}
}
},
2026-08-10 17:43:24 +03:00
markNotificationsAsSeen() {
this.data.forEach((notification) => {
notification.seen = true
})
2026-06-13 03:10:00 +03:00
markNotificationsAsSeen({
2026-08-10 17:43:24 +03:00
id: this.maxId,
2026-08-10 15:00:59 +03:00
credentials: useUsersStore().currentUser.credentials,
2026-06-13 03:10:00 +03:00
}).then(() => {
2026-08-10 17:43:24 +03:00
closeAllDesktopNotifications()
2026-06-13 03:10:00 +03:00
})
2026-01-06 16:22:52 +02:00
},
2026-08-10 17:43:24 +03:00
markSingleNotificationAsSeen({ id }) {
const notification = this.idStore.get(id)
if (notification) notification.seen = true
2026-06-13 03:10:00 +03:00
markNotificationsAsSeen({
single: true,
id,
2026-08-10 15:00:59 +03:00
credentials: useUsersStore().currentUser.credentials,
2026-06-13 03:10:00 +03:00
}).then(() => {
2026-08-10 17:43:24 +03:00
closeDesktopNotification(id)
2026-06-13 03:10:00 +03:00
})
2026-01-06 16:22:52 +02:00
},
2026-08-10 17:43:24 +03:00
dismissNotificationLocal(id) {
this.data = this.data.filter((n) => n.id !== id)
delete this.idStore.delete(id)
},
2026-08-10 17:43:24 +03:00
dismissNotification(id) {
this.dismissNotificationLocal(id)
2026-06-15 20:02:22 +03:00
dismissNotification({
id,
2026-06-16 17:32:26 +03:00
credentials: useOAuthStore().token,
2026-06-15 20:02:22 +03:00
})
},
2026-01-06 16:22:52 +02:00
},
2026-08-10 17:43:24 +03:00
})