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,
|
|
|
|
importFetchedStatus,
|
|
|
|
} 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';
|
2019-03-17 03:13:29 +01:00
|
|
|
import { saveSettings } from './settings';
|
2023-05-28 16:38:10 +02:00
|
|
|
|
2023-11-03 16:00:03 +01:00
|
|
|
export * from "./notifications_typed";
|
|
|
|
|
2020-09-19 13:53:24 +02:00
|
|
|
export const NOTIFICATIONS_UPDATE_NOOP = 'NOTIFICATIONS_UPDATE_NOOP';
|
2016-11-20 19:39:18 +01:00
|
|
|
|
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
|
|
|
|
2024-08-09 16:56:39 +02:00
|
|
|
export const NOTIFICATION_REQUESTS_DISMISS_REQUEST = 'NOTIFICATION_REQUESTS_DISMISS_REQUEST';
|
|
|
|
export const NOTIFICATION_REQUESTS_DISMISS_SUCCESS = 'NOTIFICATION_REQUESTS_DISMISS_SUCCESS';
|
|
|
|
export const NOTIFICATION_REQUESTS_DISMISS_FAIL = 'NOTIFICATION_REQUESTS_DISMISS_FAIL';
|
|
|
|
|
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) => {
|
2022-05-07 04:50:40 +02:00
|
|
|
const activeFilter = getState().getIn(['settings', 'notifications', 'quickFilter', 'active']);
|
|
|
|
const showInColumn = activeFilter === 'all' ? getState().getIn(['settings', 'notifications', 'shows', notification.type], true) : activeFilter === notification.type;
|
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());
|
|
|
|
|
2019-03-05 20:15:43 +01:00
|
|
|
if (showInColumn) {
|
|
|
|
dispatch(importFetchedAccount(notification.account));
|
2016-11-20 19:39:18 +01:00
|
|
|
|
2019-03-05 20:15:43 +01:00
|
|
|
if (notification.status) {
|
|
|
|
dispatch(importFetchedStatus(notification.status));
|
|
|
|
}
|
|
|
|
|
2022-06-27 09:30:15 +02:00
|
|
|
if (notification.report) {
|
|
|
|
dispatch(importFetchedAccount(notification.report.target_account));
|
|
|
|
}
|
|
|
|
|
2025-01-03 17:12:08 +01:00
|
|
|
dispatch(notificationsUpdate({ notification, playSound: playSound && !filtered}));
|
2019-03-05 20:15:43 +01:00
|
|
|
} else if (playSound && !filtered) {
|
|
|
|
dispatch({
|
|
|
|
type: NOTIFICATIONS_UPDATE_NOOP,
|
|
|
|
meta: { sound: 'boop' },
|
|
|
|
});
|
|
|
|
}
|
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 = () => {};
|
|
|
|
|
2018-12-18 17:22:01 +01:00
|
|
|
export function setFilter (filterType) {
|
|
|
|
return dispatch => {
|
|
|
|
dispatch({
|
|
|
|
type: NOTIFICATIONS_FILTER_SET,
|
|
|
|
path: ['notifications', 'quickFilter', 'active'],
|
|
|
|
value: filterType,
|
|
|
|
});
|
2019-03-17 03:13:29 +01:00
|
|
|
dispatch(saveSettings());
|
2018-12-18 17:22:01 +01:00
|
|
|
};
|
2023-02-03 20:52:07 +01:00
|
|
|
}
|
2020-09-15 23:42:58 +02:00
|
|
|
|
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,
|
|
|
|
};
|
|
|
|
}
|