2023-05-31 23:43:39 +02:00
|
|
|
import { IntlMessageFormat } from 'intl-messageformat';
|
2023-05-28 16:38:10 +02:00
|
|
|
import { defineMessages } from 'react-intl';
|
|
|
|
|
2023-11-15 12:01:51 +01:00
|
|
|
import { unescapeHTML } from '../utils/html';
|
|
|
|
import { requestNotificationPermission } from '../utils/notifications';
|
2023-05-28 16:38:10 +02:00
|
|
|
|
2024-09-24 19:02:36 +02:00
|
|
|
import { fetchFollowRequests } from './accounts';
|
2019-03-05 20:15:43 +01:00
|
|
|
import {
|
|
|
|
importFetchedAccount,
|
|
|
|
} from './importer';
|
2020-05-29 16:14:16 +02:00
|
|
|
import { submitMarkers } from './markers';
|
2023-11-03 16:00:03 +01:00
|
|
|
import { notificationsUpdate } from "./notifications_typed";
|
2023-09-12 18:27:01 +02:00
|
|
|
import { register as registerPushNotifications } from './push_notifications';
|
2023-05-28 16:38:10 +02:00
|
|
|
|
2023-11-03 16:00:03 +01:00
|
|
|
export * from "./notifications_typed";
|
|
|
|
|
2018-12-18 17:22:01 +01:00
|
|
|
export const NOTIFICATIONS_FILTER_SET = 'NOTIFICATIONS_FILTER_SET';
|
|
|
|
|
2020-12-15 18:43:54 +01:00
|
|
|
export const NOTIFICATIONS_SET_BROWSER_SUPPORT = 'NOTIFICATIONS_SET_BROWSER_SUPPORT';
|
|
|
|
export const NOTIFICATIONS_SET_BROWSER_PERMISSION = 'NOTIFICATIONS_SET_BROWSER_PERMISSION';
|
2020-10-13 00:37:21 +02:00
|
|
|
|
2017-06-23 23:05:04 +09:00
|
|
|
defineMessages({
|
2017-05-12 14:46:21 +02:00
|
|
|
mention: { id: 'notification.mention', defaultMessage: '{name} mentioned you' },
|
2025-01-03 17:12:08 +01:00
|
|
|
group: { id: 'notifications.group', defaultMessage: '{count} notifications' },
|
2019-07-16 06:30:47 +02:00
|
|
|
});
|
|
|
|
|
2016-11-21 10:24:50 +01:00
|
|
|
export function updateNotifications(notification, intlMessages, intlLocale) {
|
2017-01-02 14:09:57 +01:00
|
|
|
return (dispatch, getState) => {
|
2019-03-05 20:15:43 +01:00
|
|
|
const showAlert = getState().getIn(['settings', 'notifications', 'alerts', notification.type], true);
|
|
|
|
const playSound = getState().getIn(['settings', 'notifications', 'sounds', notification.type], true);
|
2018-07-08 20:13:00 +02:00
|
|
|
|
|
|
|
let filtered = false;
|
|
|
|
|
2022-06-28 09:42:13 +02:00
|
|
|
if (['mention', 'status'].includes(notification.type) && notification.status.filtered) {
|
|
|
|
const filters = notification.status.filtered.filter(result => result.filter.context.includes('notifications'));
|
2018-07-08 20:13:00 +02:00
|
|
|
|
2022-06-28 09:42:13 +02:00
|
|
|
if (filters.some(result => result.filter.filter_action === 'hide')) {
|
2019-06-18 18:23:08 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-06-28 09:42:13 +02:00
|
|
|
filtered = filters.length > 0;
|
2018-07-08 20:13:00 +02:00
|
|
|
}
|
2017-01-17 20:09:03 +01:00
|
|
|
|
2021-08-25 11:46:29 -04:00
|
|
|
if (['follow_request'].includes(notification.type)) {
|
|
|
|
dispatch(fetchFollowRequests());
|
|
|
|
}
|
|
|
|
|
2020-05-29 16:14:16 +02:00
|
|
|
dispatch(submitMarkers());
|
|
|
|
|
2025-01-06 19:22:07 +01:00
|
|
|
// `notificationsUpdate` is still used in `user_lists` and `relationships` reducers
|
|
|
|
dispatch(importFetchedAccount(notification.account));
|
|
|
|
dispatch(notificationsUpdate({ notification, playSound: playSound && !filtered}));
|
2016-11-21 10:24:50 +01:00
|
|
|
|
|
|
|
// Desktop notifications
|
2018-07-08 20:13:00 +02:00
|
|
|
if (typeof window.Notification !== 'undefined' && showAlert && !filtered) {
|
2016-11-21 10:59:59 +01:00
|
|
|
const title = new IntlMessageFormat(intlMessages[`notification.${notification.type}`], intlLocale).format({ name: notification.account.display_name.length > 0 ? notification.account.display_name : notification.account.username });
|
2017-05-12 22:03:43 -04:00
|
|
|
const body = (notification.status && notification.status.spoiler_text.length > 0) ? notification.status.spoiler_text : unescapeHTML(notification.status ? notification.status.content : '');
|
2016-11-21 10:24:50 +01:00
|
|
|
|
2017-05-11 20:34:05 +09:00
|
|
|
const notify = new Notification(title, { body, icon: notification.account.avatar, tag: notification.id });
|
2023-11-15 12:40:52 +01:00
|
|
|
|
2017-05-11 20:34:05 +09:00
|
|
|
notify.addEventListener('click', () => {
|
|
|
|
window.focus();
|
|
|
|
notify.close();
|
|
|
|
});
|
2016-11-21 10:59:59 +01:00
|
|
|
}
|
2016-11-20 19:39:18 +01:00
|
|
|
};
|
2023-02-03 20:52:07 +01:00
|
|
|
}
|
2016-11-20 19:39:18 +01:00
|
|
|
|
2018-05-27 19:42:45 +02:00
|
|
|
const noOp = () => {};
|
|
|
|
|
2020-10-13 00:37:21 +02:00
|
|
|
// Browser support
|
|
|
|
export function setupBrowserNotifications() {
|
|
|
|
return dispatch => {
|
|
|
|
dispatch(setBrowserSupport('Notification' in window));
|
|
|
|
if ('Notification' in window) {
|
|
|
|
dispatch(setBrowserPermission(Notification.permission));
|
|
|
|
}
|
|
|
|
|
|
|
|
if ('Notification' in window && 'permissions' in navigator) {
|
|
|
|
navigator.permissions.query({ name: 'notifications' }).then((status) => {
|
|
|
|
status.onchange = () => dispatch(setBrowserPermission(Notification.permission));
|
2020-11-24 01:35:14 +09:00
|
|
|
}).catch(console.warn);
|
2020-10-13 00:37:21 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function requestBrowserPermission(callback = noOp) {
|
|
|
|
return dispatch => {
|
|
|
|
requestNotificationPermission((permission) => {
|
|
|
|
dispatch(setBrowserPermission(permission));
|
|
|
|
callback(permission);
|
2023-09-12 18:27:01 +02:00
|
|
|
|
|
|
|
if (permission === 'granted') {
|
|
|
|
dispatch(registerPushNotifications());
|
|
|
|
}
|
2020-10-13 00:37:21 +02:00
|
|
|
});
|
|
|
|
};
|
2023-02-03 20:52:07 +01:00
|
|
|
}
|
2020-10-13 00:37:21 +02:00
|
|
|
|
|
|
|
export function setBrowserSupport (value) {
|
|
|
|
return {
|
|
|
|
type: NOTIFICATIONS_SET_BROWSER_SUPPORT,
|
|
|
|
value,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function setBrowserPermission (value) {
|
|
|
|
return {
|
|
|
|
type: NOTIFICATIONS_SET_BROWSER_PERMISSION,
|
|
|
|
value,
|
|
|
|
};
|
|
|
|
}
|