import { IntlMessageFormat } from 'intl-messageformat'; import { defineMessages } from 'react-intl'; import { unescapeHTML } from '../utils/html'; import { requestNotificationPermission } from '../utils/notifications'; import { fetchFollowRequests } from './accounts'; import { importFetchedAccount, importFetchedStatus, } from './importer'; import { submitMarkers } from './markers'; import { notificationsUpdate } from "./notifications_typed"; import { register as registerPushNotifications } from './push_notifications'; import { saveSettings } from './settings'; export * from "./notifications_typed"; export const NOTIFICATIONS_UPDATE_NOOP = 'NOTIFICATIONS_UPDATE_NOOP'; export const NOTIFICATIONS_FILTER_SET = 'NOTIFICATIONS_FILTER_SET'; export const NOTIFICATIONS_SET_BROWSER_SUPPORT = 'NOTIFICATIONS_SET_BROWSER_SUPPORT'; export const NOTIFICATIONS_SET_BROWSER_PERMISSION = 'NOTIFICATIONS_SET_BROWSER_PERMISSION'; 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'; defineMessages({ mention: { id: 'notification.mention', defaultMessage: '{name} mentioned you' }, group: { id: 'notifications.group', defaultMessage: '{count} notifications' }, }); export function updateNotifications(notification, intlMessages, intlLocale) { return (dispatch, getState) => { const activeFilter = getState().getIn(['settings', 'notifications', 'quickFilter', 'active']); const showInColumn = activeFilter === 'all' ? getState().getIn(['settings', 'notifications', 'shows', notification.type], true) : activeFilter === notification.type; const showAlert = getState().getIn(['settings', 'notifications', 'alerts', notification.type], true); const playSound = getState().getIn(['settings', 'notifications', 'sounds', notification.type], true); let filtered = false; if (['mention', 'status'].includes(notification.type) && notification.status.filtered) { const filters = notification.status.filtered.filter(result => result.filter.context.includes('notifications')); if (filters.some(result => result.filter.filter_action === 'hide')) { return; } filtered = filters.length > 0; } if (['follow_request'].includes(notification.type)) { dispatch(fetchFollowRequests()); } dispatch(submitMarkers()); if (showInColumn) { dispatch(importFetchedAccount(notification.account)); if (notification.status) { dispatch(importFetchedStatus(notification.status)); } if (notification.report) { dispatch(importFetchedAccount(notification.report.target_account)); } dispatch(notificationsUpdate({ notification, playSound: playSound && !filtered})); } else if (playSound && !filtered) { dispatch({ type: NOTIFICATIONS_UPDATE_NOOP, meta: { sound: 'boop' }, }); } // Desktop notifications if (typeof window.Notification !== 'undefined' && showAlert && !filtered) { const title = new IntlMessageFormat(intlMessages[`notification.${notification.type}`], intlLocale).format({ name: notification.account.display_name.length > 0 ? notification.account.display_name : notification.account.username }); const body = (notification.status && notification.status.spoiler_text.length > 0) ? notification.status.spoiler_text : unescapeHTML(notification.status ? notification.status.content : ''); const notify = new Notification(title, { body, icon: notification.account.avatar, tag: notification.id }); notify.addEventListener('click', () => { window.focus(); notify.close(); }); } }; } const noOp = () => {}; export function setFilter (filterType) { return dispatch => { dispatch({ type: NOTIFICATIONS_FILTER_SET, path: ['notifications', 'quickFilter', 'active'], value: filterType, }); dispatch(saveSettings()); }; } // 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)); }).catch(console.warn); } }; } export function requestBrowserPermission(callback = noOp) { return dispatch => { requestNotificationPermission((permission) => { dispatch(setBrowserPermission(permission)); callback(permission); if (permission === 'granted') { dispatch(registerPushNotifications()); } }); }; } export function setBrowserSupport (value) { return { type: NOTIFICATIONS_SET_BROWSER_SUPPORT, value, }; } export function setBrowserPermission (value) { return { type: NOTIFICATIONS_SET_BROWSER_PERMISSION, value, }; }