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.

165 lines
5.3 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. import { getFilters, regexFromFilters } from '../selectors';
  13. export const NOTIFICATIONS_UPDATE = 'NOTIFICATIONS_UPDATE';
  14. export const NOTIFICATIONS_UPDATE_NOOP = 'NOTIFICATIONS_UPDATE_NOOP';
  15. export const NOTIFICATIONS_EXPAND_REQUEST = 'NOTIFICATIONS_EXPAND_REQUEST';
  16. export const NOTIFICATIONS_EXPAND_SUCCESS = 'NOTIFICATIONS_EXPAND_SUCCESS';
  17. export const NOTIFICATIONS_EXPAND_FAIL = 'NOTIFICATIONS_EXPAND_FAIL';
  18. export const NOTIFICATIONS_CLEAR = 'NOTIFICATIONS_CLEAR';
  19. export const NOTIFICATIONS_SCROLL_TOP = 'NOTIFICATIONS_SCROLL_TOP';
  20. defineMessages({
  21. mention: { id: 'notification.mention', defaultMessage: '{name} mentioned you' },
  22. group: { id: 'notifications.group', defaultMessage: '{count} notifications' },
  23. });
  24. const fetchRelatedRelationships = (dispatch, notifications) => {
  25. const accountIds = notifications.filter(item => item.type === 'follow').map(item => item.account.id);
  26. if (accountIds.length > 0) {
  27. dispatch(fetchRelationships(accountIds));
  28. }
  29. };
  30. export function updateNotifications(notification, intlMessages, intlLocale) {
  31. return (dispatch, getState) => {
  32. const showInColumn = getState().getIn(['settings', 'notifications', 'shows', notification.type], true);
  33. const showAlert = getState().getIn(['settings', 'notifications', 'alerts', notification.type], true);
  34. const playSound = getState().getIn(['settings', 'notifications', 'sounds', notification.type], true);
  35. const filters = getFilters(getState(), { contextType: 'notifications' });
  36. let filtered = false;
  37. if (notification.type === 'mention') {
  38. const regex = regexFromFilters(filters);
  39. const searchIndex = notification.status.spoiler_text + '\n' + unescapeHTML(notification.status.content);
  40. filtered = regex && regex.test(searchIndex);
  41. }
  42. if (showInColumn) {
  43. dispatch(importFetchedAccount(notification.account));
  44. if (notification.status) {
  45. dispatch(importFetchedStatus(notification.status));
  46. }
  47. dispatch({
  48. type: NOTIFICATIONS_UPDATE,
  49. notification,
  50. meta: (playSound && !filtered) ? { sound: 'boop' } : undefined,
  51. });
  52. fetchRelatedRelationships(dispatch, [notification]);
  53. } else if (playSound && !filtered) {
  54. dispatch({
  55. type: NOTIFICATIONS_UPDATE_NOOP,
  56. meta: { sound: 'boop' },
  57. });
  58. }
  59. // Desktop notifications
  60. if (typeof window.Notification !== 'undefined' && showAlert && !filtered) {
  61. const title = new IntlMessageFormat(intlMessages[`notification.${notification.type}`], intlLocale).format({ name: notification.account.display_name.length > 0 ? notification.account.display_name : notification.account.username });
  62. const body = (notification.status && notification.status.spoiler_text.length > 0) ? notification.status.spoiler_text : unescapeHTML(notification.status ? notification.status.content : '');
  63. const notify = new Notification(title, { body, icon: notification.account.avatar, tag: notification.id });
  64. notify.addEventListener('click', () => {
  65. window.focus();
  66. notify.close();
  67. });
  68. }
  69. };
  70. };
  71. const excludeTypesFromSettings = state => state.getIn(['settings', 'notifications', 'shows']).filter(enabled => !enabled).keySeq().toJS();
  72. const noOp = () => {};
  73. export function expandNotifications({ maxId } = {}, done = noOp) {
  74. return (dispatch, getState) => {
  75. const notifications = getState().get('notifications');
  76. if (notifications.get('isLoading')) {
  77. done();
  78. return;
  79. }
  80. const params = {
  81. max_id: maxId,
  82. exclude_types: excludeTypesFromSettings(getState()),
  83. };
  84. if (!maxId && notifications.get('items').size > 0) {
  85. params.since_id = notifications.getIn(['items', 0]);
  86. }
  87. dispatch(expandNotificationsRequest());
  88. api(getState).get('/api/v1/notifications', { params }).then(response => {
  89. const next = getLinks(response).refs.find(link => link.rel === 'next');
  90. dispatch(importFetchedAccounts(response.data.map(item => item.account)));
  91. dispatch(importFetchedStatuses(response.data.map(item => item.status).filter(status => !!status)));
  92. dispatch(expandNotificationsSuccess(response.data, next ? next.uri : null));
  93. fetchRelatedRelationships(dispatch, response.data);
  94. done();
  95. }).catch(error => {
  96. dispatch(expandNotificationsFail(error));
  97. done();
  98. });
  99. };
  100. };
  101. export function expandNotificationsRequest() {
  102. return {
  103. type: NOTIFICATIONS_EXPAND_REQUEST,
  104. };
  105. };
  106. export function expandNotificationsSuccess(notifications, next) {
  107. return {
  108. type: NOTIFICATIONS_EXPAND_SUCCESS,
  109. notifications,
  110. next,
  111. };
  112. };
  113. export function expandNotificationsFail(error) {
  114. return {
  115. type: NOTIFICATIONS_EXPAND_FAIL,
  116. error,
  117. };
  118. };
  119. export function clearNotifications() {
  120. return (dispatch, getState) => {
  121. dispatch({
  122. type: NOTIFICATIONS_CLEAR,
  123. });
  124. api(getState).post('/api/v1/notifications/clear');
  125. };
  126. };
  127. export function scrollTopNotifications(top) {
  128. return {
  129. type: NOTIFICATIONS_SCROLL_TOP,
  130. top,
  131. };
  132. };