nas/app/javascript/mastodon/actions/notifications.js
2025-04-24 08:48:28 +09:00

137 lines
5.5 KiB
JavaScript

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,
} from './importer';
import { submitMarkers } from './markers';
import { notificationsUpdate } from "./notifications_typed";
import { register as registerPushNotifications } from './push_notifications';
import { STATUS_EMOJI_REACTION_UPDATE } from './statuses';
export * from "./notifications_typed";
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';
const messages = defineMessages({
// mention: { id: 'notification.mention', defaultMessage: '{name} mentioned you' },
group: { id: 'notifications.group', defaultMessage: '{count} notifications' },
'message_admin.report': { id: 'notification.admin.report', defaultMessage: '{name} reported {target}' },
'message_admin.sign_up': { id: 'notification.admin.sign_up', defaultMessage: '{name} signed up' },
message_emoji_reaction: { id: 'notification.emoji_reaction', defaultMessage: '{name} reacted your post with emoji' },
message_favourite: { id: 'notification.favourite', defaultMessage: '{name} favorited your post' },
message_follow: { id: 'notification.follow', defaultMessage: '{name} followed you' },
message_list_status: { id: 'notification.list_status', defaultMessage: '{name} post is added to {listName}' },
message_mention: { id: 'notification.mention', defaultMessage: 'Mention' },
message_poll: { id: 'notification.poll', defaultMessage: 'A poll you voted in has ended' },
message_reblog: { id: 'notification.reblog', defaultMessage: '{name} boosted your post' },
message_status: { id: 'notification.status', defaultMessage: '{name} just posted' },
message_status_reference: { id: 'notification.status_reference', defaultMessage: '{name} linked your post' },
message_update: { id: 'notification.update', defaultMessage: '{name} edited a post' },
});
export function updateEmojiReactions(emoji_reaction) {
return (dispatch) =>
dispatch({
type: STATUS_EMOJI_REACTION_UPDATE,
emoji_reaction,
});
}
export function updateNotifications(notification, intlMessages, intlLocale) {
return (dispatch, getState) => {
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());
// `notificationsUpdate` is still used in `user_lists` and `relationships` reducers
dispatch(importFetchedAccount(notification.account));
dispatch(notificationsUpdate({ notification, playSound: playSound && !filtered}));
// Desktop notifications
if (typeof window.Notification !== 'undefined' && showAlert && !filtered) {
const messageTemplate = intlMessages[`notification.${notification.type}`] || messages[`message_${notification.type}`] || '[NO MESSAGE DEFINITION]';
const title = new IntlMessageFormat(messageTemplate, intlLocale).format({
name: notification.account.display_name.length > 0 ? notification.account.display_name : notification.account.username,
listName: notification.list && notification.list.title,
});
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 = () => {};
// 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,
};
}