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.

169 lines
5.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. 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. const isLoadingMore = !!maxId;
  77. if (notifications.get('isLoading')) {
  78. done();
  79. return;
  80. }
  81. const params = {
  82. max_id: maxId,
  83. exclude_types: excludeTypesFromSettings(getState()),
  84. };
  85. if (!maxId && notifications.get('items').size > 0) {
  86. params.since_id = notifications.getIn(['items', 0]);
  87. }
  88. dispatch(expandNotificationsRequest(isLoadingMore));
  89. api(getState).get('/api/v1/notifications', { params }).then(response => {
  90. const next = getLinks(response).refs.find(link => link.rel === 'next');
  91. dispatch(importFetchedAccounts(response.data.map(item => item.account)));
  92. dispatch(importFetchedStatuses(response.data.map(item => item.status).filter(status => !!status)));
  93. dispatch(expandNotificationsSuccess(response.data, next ? next.uri : null, isLoadingMore));
  94. fetchRelatedRelationships(dispatch, response.data);
  95. done();
  96. }).catch(error => {
  97. dispatch(expandNotificationsFail(error, isLoadingMore));
  98. done();
  99. });
  100. };
  101. };
  102. export function expandNotificationsRequest(isLoadingMore) {
  103. return {
  104. type: NOTIFICATIONS_EXPAND_REQUEST,
  105. skipLoading: !isLoadingMore,
  106. };
  107. };
  108. export function expandNotificationsSuccess(notifications, next, isLoadingMore) {
  109. return {
  110. type: NOTIFICATIONS_EXPAND_SUCCESS,
  111. notifications,
  112. next,
  113. skipLoading: !isLoadingMore,
  114. };
  115. };
  116. export function expandNotificationsFail(error, isLoadingMore) {
  117. return {
  118. type: NOTIFICATIONS_EXPAND_FAIL,
  119. error,
  120. skipLoading: !isLoadingMore,
  121. };
  122. };
  123. export function clearNotifications() {
  124. return (dispatch, getState) => {
  125. dispatch({
  126. type: NOTIFICATIONS_CLEAR,
  127. });
  128. api(getState).post('/api/v1/notifications/clear');
  129. };
  130. };
  131. export function scrollTopNotifications(top) {
  132. return {
  133. type: NOTIFICATIONS_SCROLL_TOP,
  134. top,
  135. };
  136. };