You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

142 lines
4.6 KiB

  1. import api, { getLinks } from '../api';
  2. import IntlMessageFormat from 'intl-messageformat';
  3. import { fetchRelationships } from './accounts';
  4. import {
  5. importFetchedAccount,
  6. importFetchedAccounts,
  7. importFetchedStatus,
  8. importFetchedStatuses,
  9. } from './importer';
  10. import { defineMessages } from 'react-intl';
  11. import { unescapeHTML } from '../utils/html';
  12. export const NOTIFICATIONS_UPDATE = 'NOTIFICATIONS_UPDATE';
  13. export const NOTIFICATIONS_UPDATE_NOOP = 'NOTIFICATIONS_UPDATE_NOOP';
  14. export const NOTIFICATIONS_EXPAND_REQUEST = 'NOTIFICATIONS_EXPAND_REQUEST';
  15. export const NOTIFICATIONS_EXPAND_SUCCESS = 'NOTIFICATIONS_EXPAND_SUCCESS';
  16. export const NOTIFICATIONS_EXPAND_FAIL = 'NOTIFICATIONS_EXPAND_FAIL';
  17. export const NOTIFICATIONS_CLEAR = 'NOTIFICATIONS_CLEAR';
  18. export const NOTIFICATIONS_SCROLL_TOP = 'NOTIFICATIONS_SCROLL_TOP';
  19. defineMessages({
  20. mention: { id: 'notification.mention', defaultMessage: '{name} mentioned you' },
  21. });
  22. const fetchRelatedRelationships = (dispatch, notifications) => {
  23. const accountIds = notifications.filter(item => item.type === 'follow').map(item => item.account.id);
  24. if (accountIds.length > 0) {
  25. dispatch(fetchRelationships(accountIds));
  26. }
  27. };
  28. export function updateNotifications(notification, intlMessages, intlLocale) {
  29. return (dispatch, getState) => {
  30. const showInColumn = getState().getIn(['settings', 'notifications', 'shows', notification.type], true);
  31. const showAlert = getState().getIn(['settings', 'notifications', 'alerts', notification.type], true);
  32. const playSound = getState().getIn(['settings', 'notifications', 'sounds', notification.type], true);
  33. if (showInColumn) {
  34. dispatch(importFetchedAccount(notification.account));
  35. if (notification.status) {
  36. dispatch(importFetchedStatus(notification.status));
  37. }
  38. dispatch({
  39. type: NOTIFICATIONS_UPDATE,
  40. notification,
  41. meta: playSound ? { sound: 'boop' } : undefined,
  42. });
  43. fetchRelatedRelationships(dispatch, [notification]);
  44. } else if (playSound) {
  45. dispatch({
  46. type: NOTIFICATIONS_UPDATE_NOOP,
  47. meta: { sound: 'boop' },
  48. });
  49. }
  50. // Desktop notifications
  51. if (typeof window.Notification !== 'undefined' && showAlert) {
  52. const title = new IntlMessageFormat(intlMessages[`notification.${notification.type}`], intlLocale).format({ name: notification.account.display_name.length > 0 ? notification.account.display_name : notification.account.username });
  53. const body = (notification.status && notification.status.spoiler_text.length > 0) ? notification.status.spoiler_text : unescapeHTML(notification.status ? notification.status.content : '');
  54. const notify = new Notification(title, { body, icon: notification.account.avatar, tag: notification.id });
  55. notify.addEventListener('click', () => {
  56. window.focus();
  57. notify.close();
  58. });
  59. }
  60. };
  61. };
  62. const excludeTypesFromSettings = state => state.getIn(['settings', 'notifications', 'shows']).filter(enabled => !enabled).keySeq().toJS();
  63. export function expandNotifications({ maxId } = {}) {
  64. return (dispatch, getState) => {
  65. if (getState().getIn(['notifications', 'isLoading'])) {
  66. return;
  67. }
  68. const params = {
  69. max_id: maxId,
  70. exclude_types: excludeTypesFromSettings(getState()),
  71. };
  72. dispatch(expandNotificationsRequest());
  73. api(getState).get('/api/v1/notifications', { params }).then(response => {
  74. const next = getLinks(response).refs.find(link => link.rel === 'next');
  75. dispatch(importFetchedAccounts(response.data.map(item => item.account)));
  76. dispatch(importFetchedStatuses(response.data.map(item => item.status).filter(status => !!status)));
  77. dispatch(expandNotificationsSuccess(response.data, next ? next.uri : null));
  78. fetchRelatedRelationships(dispatch, response.data);
  79. }).catch(error => {
  80. dispatch(expandNotificationsFail(error));
  81. });
  82. };
  83. };
  84. export function expandNotificationsRequest() {
  85. return {
  86. type: NOTIFICATIONS_EXPAND_REQUEST,
  87. };
  88. };
  89. export function expandNotificationsSuccess(notifications, next) {
  90. return {
  91. type: NOTIFICATIONS_EXPAND_SUCCESS,
  92. notifications,
  93. next,
  94. };
  95. };
  96. export function expandNotificationsFail(error) {
  97. return {
  98. type: NOTIFICATIONS_EXPAND_FAIL,
  99. error,
  100. };
  101. };
  102. export function clearNotifications() {
  103. return (dispatch, getState) => {
  104. dispatch({
  105. type: NOTIFICATIONS_CLEAR,
  106. });
  107. api(getState).post('/api/v1/notifications/clear');
  108. };
  109. };
  110. export function scrollTopNotifications(top) {
  111. return {
  112. type: NOTIFICATIONS_SCROLL_TOP,
  113. top,
  114. };
  115. };